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