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