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