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