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