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