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