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