Added truncation 5=106.
[idzebra-moved-to-github.git] / bfile / mfile.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: mfile.c,v $
7  * Revision 1.35  1999-10-14 14:33:50  adam
8  * Added truncation 5=106.
9  *
10  * Revision 1.34  1999/05/26 07:49:12  adam
11  * C++ compilation.
12  *
13  * Revision 1.33  1999/05/12 13:08:06  adam
14  * First version of ISAMS.
15  *
16  * Revision 1.32  1999/04/28 14:53:07  adam
17  * Fixed stupid bug regarding split-files.
18  *
19  * Revision 1.31  1999/02/18 12:49:33  adam
20  * Changed file naming scheme for register files as well as record
21  * store/index files.
22  *
23  * Revision 1.30  1999/02/02 14:50:02  adam
24  * Updated WIN32 code specific sections. Changed header.
25  *
26  * Revision 1.29  1998/05/27 14:28:34  adam
27  * Fixed bug in mf_write. 'Cap off' byte written at wrong offset.
28  *
29  * Revision 1.28  1998/05/20 10:00:35  adam
30  * Fixed register spec so that colon isn't treated as size separator
31  * unless followed by [0-9+-] in order to allow DOS drive specifications.
32  *
33  * Revision 1.27  1998/02/10 11:55:07  adam
34  * Minor changes.
35  *
36  * Revision 1.26  1997/10/27 14:25:38  adam
37  * Fixed memory leaks.
38  *
39  * Revision 1.25  1997/09/18 08:59:16  adam
40  * Extra generic handle for the character mapping routines.
41  *
42  * Revision 1.24  1997/09/17 12:19:06  adam
43  * Zebra version corresponds to YAZ version 1.4.
44  * Changed Zebra server so that it doesn't depend on global common_resource.
45  *
46  * Revision 1.23  1997/09/09 13:37:53  adam
47  * Partial port to WIN95/NT.
48  *
49  * Revision 1.22  1997/09/04 13:56:39  adam
50  * Added O_BINARY to open calls.
51  *
52  * Revision 1.21  1996/10/29 13:56:18  adam
53  * Include of zebrautl.h instead of alexutil.h.
54  *
55  * Revision 1.20  1996/05/14 12:10:16  quinn
56  * Bad areadef scan
57  *
58  * Revision 1.19  1996/05/01  07:16:30  quinn
59  * Fixed ancient bug.
60  *
61  * Revision 1.18  1996/04/09  06:47:30  adam
62  * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
63  *
64  * Revision 1.17  1996/03/20 13:29:11  quinn
65  * Bug-fix
66  *
67  * Revision 1.16  1995/12/12  15:57:57  adam
68  * Implemented mf_unlink. cf_unlink uses mf_unlink.
69  *
70  * Revision 1.15  1995/12/08  16:21:14  adam
71  * Work on commit/update.
72  *
73  * Revision 1.14  1995/12/05  13:12:37  quinn
74  * Added <errno.h>
75  *
76  * Revision 1.13  1995/11/30  17:00:50  adam
77  * Several bug fixes. Commit system runs now.
78  *
79  * Revision 1.12  1995/11/24  17:26:11  quinn
80  * Mostly about making some ISAM stuff in the config file optional.
81  *
82  * Revision 1.11  1995/11/13  09:32:43  quinn
83  * Comment work.
84  *
85  * Revision 1.10  1995/09/04  12:33:22  adam
86  * Various cleanup. YAZ util used instead.
87  *
88  * Revision 1.9  1994/11/04  14:26:39  quinn
89  * bug-fix.
90  *
91  * Revision 1.8  1994/10/05  16:56:42  quinn
92  * Minor.
93  *
94  * Revision 1.7  1994/09/19  14:12:37  quinn
95  * dunno.
96  *
97  * Revision 1.6  1994/09/14  13:10:15  quinn
98  * Corrected some bugs in the init-phase
99  *
100  * Revision 1.5  1994/09/12  08:01:51  quinn
101  * Small
102  *
103  * Revision 1.4  1994/09/01  14:51:07  quinn
104  * Allowed mf_write to write beyond eof+1.
105  *
106  * Revision 1.3  1994/08/24  09:37:17  quinn
107  * Changed reaction to read return values.
108  *
109  * Revision 1.2  1994/08/23  14:50:48  quinn
110  * Fixed mf_close().
111  *
112  * Revision 1.1  1994/08/23  14:41:33  quinn
113  * First functional version.
114  *
115  */
116
117
118  /*
119   * TODO: The size estimates in init may not be accurate due to
120   * only partially written final blocks.
121   */
122
123 #include <sys/types.h>
124 #include <fcntl.h>
125 #ifdef WIN32
126 #include <io.h>
127 #else
128 #include <unistd.h>
129 #endif
130 #include <direntz.h>
131 #include <string.h>
132 #include <stdlib.h>
133 #include <stdio.h>
134 #include <assert.h>
135 #include <errno.h>
136
137 #include <zebrautl.h>
138 #include <mfile.h>
139
140 static int scan_areadef(MFile_area ma, const char *name, const char *ad)
141 {
142     /*
143      * If no definition is given, use current directory, unlimited.
144      */
145     char dirname[FILENAME_MAX+1]; 
146     mf_dir **dp = &ma->dirs, *dir = *dp;
147
148     if (!ad)
149         ad = ".:-1b";
150     for (;;)
151     {
152         const char *ad0 = ad;
153         int i = 0, fact = 1, multi, size = 0;
154
155         while (*ad == ' ' || *ad == '\t')
156             ad++;
157         if (!*ad)
158             break;
159         while (*ad)
160         {
161             if (*ad == ':' && strchr ("+-0123456789", ad[1]))
162                 break;
163             if (i < FILENAME_MAX)
164                 dirname[i++] = *ad;
165             ad++;
166         }
167         dirname[i] = '\0';
168         if (*ad++ != ':')
169         {
170             logf (LOG_WARN, "Missing colon after path: %s", ad0);
171             return -1;
172         }
173         if (i == 0)
174         {
175             logf (LOG_WARN, "Empty path: %s", ad0);
176             return -1;
177         }
178         while (*ad == ' ' || *ad == '\t')
179             ad++;
180         if (*ad == '-')
181         {
182             fact = -1;
183             ad++;
184         }
185         else if (*ad == '+')
186             ad++;
187         size = 0;
188         if (*ad < '0' || *ad > '9')
189         {
190             logf (LOG_FATAL, "Missing size after path: %s", ad0);
191             return -1;
192         }
193         size = 0;
194         while (*ad >= '0' && *ad <= '9')
195             size = size*10 + (*ad++ - '0');
196         switch (*ad)
197         {
198             case 'B': case 'b': multi = 1; break;
199             case 'K': case 'k': multi = 1024; break;
200             case 'M': case 'm': multi = 1048576; break;
201             case '\0':
202                 logf (LOG_FATAL, "Missing unit: %s", ad0);
203                 return -1;
204             default:
205                 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
206                 return -1;
207         }
208         ad++;
209         *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
210         dir->next = 0;
211         strcpy(dir->name, dirname);
212         dir->max_bytes = dir->avail_bytes = fact * size * multi;
213         dp = &dir->next;
214     }
215     return 0;
216 }
217
218 static int file_position(MFile mf, int pos, int offset)
219 {
220     int off = 0, c = mf->cur_file, ps;
221
222     if ((c > 0 && pos <= mf->files[c-1].top) ||
223         (c < mf->no_files -1 && pos > mf->files[c].top))
224     {
225         c = 0;
226         while (c + 1 < mf->no_files && mf->files[c].top < pos)
227         {
228             off += mf->files[c].blocks;
229             c++;
230         }
231         assert(c < mf->no_files);
232     }
233     else
234         off = c ? (mf->files[c-1].top + 1) : 0;
235     if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
236         mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
237     {
238         if (!mf->wr && errno == ENOENT && off == 0)
239             return -2;
240         logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
241         return -1;
242     }
243     if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
244         SEEK_SET) < 0)
245     {
246         logf (LOG_WARN|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
247         return -1;
248     }
249     mf->cur_file = c;
250     return ps;
251 }
252
253 static int cmp_part_file(const void *p1, const void *p2)
254 {
255     return ((part_file *)p1)->number - ((part_file *)p2)->number;
256 }
257
258 /*
259  * Create a new area, cotaining metafiles in directories.
260  * Find the part-files in each directory, and inventory the existing metafiles.
261  */
262 MFile_area mf_init(const char *name, const char *spec)
263 {
264     MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
265     mf_dir *dirp;
266     meta_file *meta_f;
267     part_file *part_f = 0;
268     DIR *dd;
269     struct dirent *dent;
270     int fd, number;
271     char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
272     
273     logf (LOG_DEBUG, "mf_init(%s)", name);
274     strcpy(ma->name, name);
275     ma->mfiles = 0;
276     ma->dirs = 0;
277     if (scan_areadef(ma, name, spec) < 0)
278     {
279         logf (LOG_WARN, "Failed to access description of '%s'", name);
280         return 0;
281     }
282     /* look at each directory */
283     for (dirp = ma->dirs; dirp; dirp = dirp->next)
284     {
285         if (!(dd = opendir(dirp->name)))
286         {
287             logf (LOG_WARN|LOG_ERRNO, "Failed to open directory %s",
288                                      dirp->name);
289             return 0;
290         }
291         /* look at each file */
292         while ((dent = readdir(dd)))
293         {
294             if (*dent->d_name == '.')
295                 continue;
296             if (sscanf(dent->d_name, "%[^-]-%d.mf", metaname, &number) != 2)
297             {
298                 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
299                 continue;
300             }
301             for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
302             {
303                 /* known metafile */
304                 if (!strcmp(meta_f->name, metaname))
305                 {
306                     part_f = &meta_f->files[meta_f->no_files++];
307                     break;
308                 }
309             }
310             /* new metafile */
311             if (!meta_f)
312             {
313                 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
314                 meta_f->ma = ma;
315                 meta_f->next = ma->mfiles;
316                 meta_f->open = 0;
317                 meta_f->cur_file = -1;
318                 ma->mfiles = meta_f;
319                 strcpy(meta_f->name, metaname);
320                 part_f = &meta_f->files[0];
321                 meta_f->no_files = 1;
322             }
323             part_f->number = number;
324             part_f->dir = dirp;
325             part_f->fd = -1;
326             sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
327             part_f->path = xstrdup(tmpnam);
328             /* get size */
329             if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
330             {
331                 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
332                       dent->d_name);
333                 return 0;
334             }
335             if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
336             {
337                 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
338                       dent->d_name);
339                 return 0;
340             }
341             close(fd);
342             if (dirp->max_bytes >= 0)
343                 dirp->avail_bytes -= part_f->bytes;
344         }
345         closedir(dd);
346     }
347     for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
348     {
349         logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
350               meta_f->no_files);
351         qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
352               cmp_part_file);
353     }
354     return ma;
355 }
356
357 void mf_destroy(MFile_area ma)
358 {
359     mf_dir *dp;
360     meta_file *meta_f;
361
362     if (!ma)
363         return;
364     dp = ma->dirs;
365     while (dp)
366     {
367         mf_dir *d = dp;
368         dp = dp->next;
369         xfree (d);
370     }
371     meta_f = ma->mfiles;
372     while (meta_f)
373     {
374         int i;
375         meta_file *m = meta_f;
376         
377         for (i = 0; i<m->no_files; i++)
378         {
379             xfree (m->files[i].path);
380         }
381         meta_f = meta_f->next;
382         xfree (m);
383     }
384     xfree (ma);
385 }
386
387 /*
388  * Open a metafile.
389  * If !ma, Use MF_DEFAULT_AREA.
390  */
391 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
392 {
393     meta_file *mnew;
394     int i;
395     char tmp[FILENAME_MAX+1];
396     mf_dir *dp;
397
398     logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
399          wflag ? "RW" : "RDONLY");
400     assert (ma);
401     for (mnew = ma->mfiles; mnew; mnew = mnew->next)
402         if (!strcmp(name, mnew->name))
403         {
404             if (mnew->open)
405                 abort();
406             else
407                 break;
408         }
409     if (!mnew)
410     {
411         mnew = (meta_file *) xmalloc(sizeof(*mnew));
412         strcpy(mnew->name, name);
413         /* allocate one, empty file */
414         mnew->no_files = 1;
415         mnew->files[0].bytes = mnew->files[0].blocks = 0;
416         mnew->files[0].top = -1;
417         mnew->files[0].number = 0;
418         mnew->files[0].fd = -1;
419         mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
420         for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
421             mnew->min_bytes_creat; dp = dp->next);
422         if (!dp)
423         {
424             logf (LOG_FATAL, "Insufficient space for new mfile.");
425             return 0;
426         }
427         mnew->files[0].dir = dp;
428         sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
429         mnew->files[0].path = xstrdup(tmp);
430         mnew->ma = ma;
431         mnew->next = ma->mfiles;
432         ma->mfiles = mnew;
433     }
434     else
435     {
436         for (i = 0; i < mnew->no_files; i++)
437         {
438             if (mnew->files[i].bytes % block_size)
439                 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
440                     block_size;
441             mnew->files[i].blocks = mnew->files[i].bytes / block_size;
442         }
443         assert(!mnew->open);
444     }
445     mnew->blocksize = block_size;
446     mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
447     mnew->wr=wflag;
448     mnew->cur_file = 0;
449     mnew->open = 1;
450
451     for (i = 0; i < mnew->no_files; i++)
452     {
453         mnew->files[i].blocks = mnew->files[i].bytes / mnew->blocksize;
454         if (i == mnew->no_files - 1)
455             mnew->files[i].top = -1;
456         else
457             mnew->files[i].top =
458                 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
459                 : (mnew->files[i].blocks - 1);
460     }
461     return mnew;
462 }
463
464 /*
465  * Close a metafile.
466  */
467 int mf_close(MFile mf)
468 {
469     int i;
470
471     logf (LOG_DEBUG, "mf_close(%s)", mf->name);
472     assert(mf->open);
473     for (i = 0; i < mf->no_files; i++)
474         if (mf->files[i].fd >= 0)
475         {
476             close(mf->files[i].fd);
477             mf->files[i].fd = -1;
478         }
479     mf->open = 0;
480     return 0;
481 }
482
483 /*
484  * Read one block from a metafile. Interface mirrors bfile.
485  */
486 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
487 {
488     int rd, toread;
489
490     if ((rd = file_position(mf, no, offset)) < 0)
491     {
492         if (rd == -2)
493             return 0;
494         else
495             exit(1);
496     }
497     toread = nbytes ? nbytes : mf->blocksize;
498     if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
499     {
500         logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
501               mf->files[mf->cur_file].path);
502         exit(1);
503     }
504     else if (rd < toread)
505         return 0;
506     else
507         return 1;
508 }
509
510 /*
511  * Write.
512  */
513 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
514 {
515     int ps, nblocks, towrite;
516     mf_dir *dp;
517     char tmp[FILENAME_MAX+1];
518     unsigned char dummych = '\xff';
519
520     if ((ps = file_position(mf, no, offset)) < 0)
521         exit(1);
522     /* file needs to grow */
523     while (ps >= mf->files[mf->cur_file].blocks)
524     {
525         /* file overflow - allocate new file */
526         if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
527             (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
528             mf->files[mf->cur_file].dir->avail_bytes)
529         {
530             /* cap off file? */
531             if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
532                 mf->blocksize) > 0)
533             {
534                 logf (LOG_DEBUG, "Capping off file %s at pos %d",
535                     mf->files[mf->cur_file].path, nblocks);
536                 if ((ps = file_position(mf,
537                     (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
538                     mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
539                         exit(1);
540                 logf (LOG_DEBUG, "ps = %d", ps);
541                 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
542                 {
543                     logf (LOG_ERRNO|LOG_FATAL, "write dummy");
544                     exit(1);
545                 }
546                 mf->files[mf->cur_file].blocks += nblocks;
547                 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
548                 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
549                     mf->blocksize;
550             }
551             /* get other bit */
552             logf (LOG_DEBUG, "Creating new file.");
553             for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
554                 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
555             if (!dp)
556             {
557                 logf (LOG_FATAL, "Cannot allocate more space for %s",
558                       mf->name);
559                 exit(1);
560             }
561             mf->files[mf->cur_file].top = (mf->cur_file ?
562                 mf->files[mf->cur_file-1].top : -1) +
563                 mf->files[mf->cur_file].blocks;
564             mf->files[++(mf->cur_file)].top = -1;
565             mf->files[mf->cur_file].dir = dp;
566             mf->files[mf->cur_file].number =
567                 mf->files[mf->cur_file-1].number + 1;
568             mf->files[mf->cur_file].blocks =
569                 mf->files[mf->cur_file].bytes = 0;
570             mf->files[mf->cur_file].fd = -1;
571             sprintf(tmp, "%s/%s-%d.mf", dp->name, mf->name,
572                 mf->files[mf->cur_file].number);
573             mf->files[mf->cur_file].path = xstrdup(tmp);
574             mf->no_files++;
575             /* open new file and position at beginning */
576             if ((ps = file_position(mf, no, offset)) < 0)
577                 exit(1);
578         }
579         else
580         {
581             nblocks = ps - mf->files[mf->cur_file].blocks + 1;
582             mf->files[mf->cur_file].blocks += nblocks;
583             mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
584             if (mf->files[mf->cur_file].dir->max_bytes >= 0)
585                 mf->files[mf->cur_file].dir->avail_bytes -=
586                 nblocks * mf->blocksize;
587         }
588     }
589     towrite = nbytes ? nbytes : mf->blocksize;
590     if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
591     {
592         logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
593                 mf->name, mf->cur_file);
594         exit(1);
595     }
596     return 0;
597 }
598
599 /*
600  * Destroy a metafile, unlinking component files. File must be open.
601  */
602 int mf_unlink(MFile mf)
603 {
604     int i;
605
606     for (i = 0; i < mf->no_files; i++)
607         unlink (mf->files[i].path);
608     return 0;
609 }
610
611 /*
612  * Unlink the file by name, rather than MFile-handle. File should be closed.
613  */
614 int mf_unlink_name(MFile_area ma, const char *name)
615 {
616     abort();
617     return 0;
618 }