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