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