2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1996-05-14 12:10:16 quinn
10 * Revision 1.19 1996/05/01 07:16:30 quinn
13 * Revision 1.18 1996/04/09 06:47:30 adam
14 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
16 * Revision 1.17 1996/03/20 13:29:11 quinn
19 * Revision 1.16 1995/12/12 15:57:57 adam
20 * Implemented mf_unlink. cf_unlink uses mf_unlink.
22 * Revision 1.15 1995/12/08 16:21:14 adam
23 * Work on commit/update.
25 * Revision 1.14 1995/12/05 13:12:37 quinn
28 * Revision 1.13 1995/11/30 17:00:50 adam
29 * Several bug fixes. Commit system runs now.
31 * Revision 1.12 1995/11/24 17:26:11 quinn
32 * Mostly about making some ISAM stuff in the config file optional.
34 * Revision 1.11 1995/11/13 09:32:43 quinn
37 * Revision 1.10 1995/09/04 12:33:22 adam
38 * Various cleanup. YAZ util used instead.
40 * Revision 1.9 1994/11/04 14:26:39 quinn
43 * Revision 1.8 1994/10/05 16:56:42 quinn
46 * Revision 1.7 1994/09/19 14:12:37 quinn
49 * Revision 1.6 1994/09/14 13:10:15 quinn
50 * Corrected some bugs in the init-phase
52 * Revision 1.5 1994/09/12 08:01:51 quinn
55 * Revision 1.4 1994/09/01 14:51:07 quinn
56 * Allowed mf_write to write beyond eof+1.
58 * Revision 1.3 1994/08/24 09:37:17 quinn
59 * Changed reaction to read return values.
61 * Revision 1.2 1994/08/23 14:50:48 quinn
64 * Revision 1.1 1994/08/23 14:41:33 quinn
65 * First functional version.
71 * TODO: The size estimates in init may not be accurate due to
72 * only partially written final blocks.
75 #include <sys/types.h>
87 static MFile_area_struct *open_areas = 0;
88 static MFile_area_struct *default_area = 0;
90 static int scan_areadef(MFile_area ma, const char *name)
93 * If no definition is given, use current directory, unlimited.
95 const char *ad = res_get_def(common_resource, name, ".:-1b");
96 char dirname[FILENAME_MAX+1];
97 mf_dir **dp = &ma->dirs, *dir = *dp;
101 logf (LOG_FATAL, "Could not find resource '%s'", name);
106 const char *ad0 = ad;
107 int i = 0, fact = 1, multi, size = 0;
109 while (*ad == ' ' || *ad == '\t')
113 while (*ad && *ad != ':')
115 if (i < FILENAME_MAX)
122 logf (LOG_FATAL, "Missing colon after path: %s", ad0);
127 logf (LOG_FATAL, "Empty path: %s", ad0);
130 while (*ad == ' ' || *ad == '\t')
140 if (*ad < '0' || *ad > '9')
142 logf (LOG_FATAL, "Missing size after path: %s", ad0);
146 while (*ad >= '0' && *ad <= '9')
147 size = size*10 + (*ad++ - '0');
150 case 'B': case 'b': multi = 1; break;
151 case 'K': case 'k': multi = 1024; break;
152 case 'M': case 'm': multi = 1048576; break;
154 logf (LOG_FATAL, "Missing unit: %s", ad0);
157 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
161 *dp = dir = xmalloc(sizeof(mf_dir));
163 strcpy(dir->name, dirname);
164 dir->max_bytes = dir->avail_bytes = fact * size * multi;
170 static int file_position(MFile mf, int pos, int offset)
172 int off = 0, c = mf->cur_file, ps;
174 if ((c > 0 && pos <= mf->files[c-1].top) ||
175 (c < mf->no_files -1 && pos > mf->files[c].top))
178 while (c + 1 < mf->no_files && mf->files[c].top < pos)
180 off += mf->files[c].blocks;
183 assert(c < mf->no_files);
186 off = c ? (mf->files[c-1].top + 1) : 0;
187 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
188 mf->wr ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
190 if (!mf->wr && errno == ENOENT && off == 0)
192 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
195 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
198 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
205 static int cmp_part_file(const void *p1, const void *p2)
207 return ((part_file *)p1)->number - ((part_file *)p2)->number;
211 * Create a new area, cotaining metafiles in directories.
212 * Find the part-files in each directory, and inventory the existing metafiles.
214 MFile_area mf_init(const char *name)
216 MFile_area ma = xmalloc(sizeof(MFile_area_struct)), mp;
219 part_file *part_f = 0;
223 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
225 logf (LOG_DEBUG, "mf_init(%s)", name);
226 for (mp = open_areas; mp; mp = mp->next)
227 if (!strcmp(name, mp->name))
229 ma = xmalloc(sizeof(MFile_area_struct));
230 strcpy(ma->name, name);
231 ma->next = open_areas;
235 if (scan_areadef(ma, name) < 0)
237 logf (LOG_FATAL, "Failed to access description of '%s'", name);
240 /* look at each directory */
241 for (dirp = ma->dirs; dirp; dirp = dirp->next)
243 if (!(dd = opendir(dirp->name)))
245 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", dirp->name);
248 /* look at each file */
249 while ((dent = readdir(dd)))
251 if (*dent->d_name == '.')
253 if (sscanf(dent->d_name, "%[^.].mf.%d", metaname, &number) != 2)
255 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
258 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
261 if (!strcmp(meta_f->name, metaname))
263 part_f = &meta_f->files[meta_f->no_files++];
270 meta_f = xmalloc(sizeof(*meta_f));
272 meta_f->next = ma->mfiles;
274 meta_f->cur_file = -1;
276 strcpy(meta_f->name, metaname);
277 part_f = &meta_f->files[0];
278 meta_f->no_files = 1;
280 part_f->number = number;
283 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
284 part_f->path = xstrdup(tmpnam);
286 if ((fd = open(part_f->path, O_RDONLY)) < 0)
288 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
292 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
294 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
299 if (dirp->max_bytes >= 0)
300 dirp->avail_bytes -= part_f->bytes;
304 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
306 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
308 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
316 * If !ma, Use MF_DEFAULT_AREA.
318 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
320 struct meta_file *new;
322 char tmp[FILENAME_MAX+1];
325 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
326 wflag ? "RW" : "RDONLY");
329 if (!default_area && !(default_area = mf_init(MF_DEFAULT_AREA)))
331 logf (LOG_FATAL, "Failed to open default area.");
336 for (new = ma->mfiles; new; new = new->next)
337 if (!strcmp(name, new->name))
344 new = xmalloc(sizeof(*new));
345 strcpy(new->name, name);
346 /* allocate one, empty file */
348 new->files[0].bytes = new->files[0].blocks = 0;
349 new->files[0].top = -1;
350 new->files[0].number = 0;
351 new->files[0].fd = -1;
352 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
353 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
354 new->min_bytes_creat; dp = dp->next);
357 logf (LOG_FATAL, "Insufficient space for new mfile.");
360 new->files[0].dir = dp;
361 sprintf(tmp, "%s/%s.mf.%d", dp->name, new->name, 0);
362 new->files[0].path = xstrdup(tmp);
367 for (i = 0; i < new->no_files; i++)
369 if (new->files[i].bytes % block_size)
370 new->files[i].bytes += block_size - new->files[i].bytes %
372 new->files[i].blocks = new->files[i].bytes / block_size;
376 new->blocksize = block_size;
377 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
382 for (i = 0; i < new->no_files; i++)
384 new->files[i].blocks = new->files[i].bytes / new->blocksize;
385 if (i == new->no_files - 1)
386 new->files[i].top = -1;
388 new->files[i].top = i ? (new->files[i-1].top + new->files[i].blocks)
389 : (new->files[i].blocks - 1);
397 int mf_close(MFile mf)
401 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
403 for (i = 0; i < mf->no_files; i++)
404 if (mf->files[i].fd >= 0)
406 close(mf->files[i].fd);
407 mf->files[i].fd = -1;
414 * Read one block from a metafile. Interface mirrors bfile.
416 int mf_read(MFile mf, int no, int offset, int num, void *buf)
420 if ((rd = file_position(mf, no, offset)) < 0)
425 toread = num ? num : mf->blocksize;
426 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
428 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
429 mf->files[mf->cur_file].path);
432 else if (rd < toread)
441 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
443 int ps, nblocks, towrite;
445 char tmp[FILENAME_MAX+1];
446 unsigned char dummych = '\xff';
448 if ((ps = file_position(mf, no, offset)) < 0)
450 /* file needs to grow */
451 while (ps >= mf->files[mf->cur_file].blocks)
453 /* file overflow - allocate new file */
454 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
455 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
456 mf->files[mf->cur_file].dir->avail_bytes)
459 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
462 logf (LOG_DEBUG, "Capping off file %s at pos %d",
463 mf->files[mf->cur_file].path, nblocks);
464 if ((ps = file_position(mf,
465 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
466 mf->files[mf->cur_file].blocks + nblocks, 0)) < 0)
468 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
470 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
473 mf->files[mf->cur_file].blocks += nblocks;
474 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
475 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
479 logf (LOG_DEBUG, "Creating new file.");
480 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
481 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
484 logf (LOG_FATAL, "Cannot allocate more space for %s",
488 mf->files[mf->cur_file].top = (mf->cur_file ?
489 mf->files[mf->cur_file-1].top : -1) +
490 mf->files[mf->cur_file].blocks;
491 mf->files[++(mf->cur_file)].top = -1;
492 mf->files[mf->cur_file].dir = dp;
493 mf->files[mf->cur_file].number =
494 mf->files[mf->cur_file-1].number + 1;
495 mf->files[mf->cur_file].blocks =
496 mf->files[mf->cur_file].bytes = 0;
497 mf->files[mf->cur_file].fd = -1;
498 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
499 mf->files[mf->cur_file].number);
500 mf->files[mf->cur_file].path = xstrdup(tmp);
502 /* open new file and position at beginning */
503 if ((ps = file_position(mf, no, offset)) < 0)
508 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
509 mf->files[mf->cur_file].blocks += nblocks;
510 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
511 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
512 mf->files[mf->cur_file].dir->avail_bytes -=
513 nblocks * mf->blocksize;
516 towrite = num ? num : mf->blocksize;
517 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
519 logf (LOG_FATAL|LOG_ERRNO, "Write failed");
526 * Destroy a metafile, unlinking component files. File must be open.
528 int mf_unlink(MFile mf)
532 for (i = 0; i < mf->no_files; i++)
533 unlink (mf->files[i].path);
538 * Unlink the file by name, rather than MFile-handle. File should be closed.
540 int mf_unlink_name(MFile_area ma, const char *name)