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