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