2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1997-09-09 13:37:53 adam
8 * Partial port to WIN95/NT.
10 * Revision 1.22 1997/09/04 13:56:39 adam
11 * Added O_BINARY to open calls.
13 * Revision 1.21 1996/10/29 13:56:18 adam
14 * Include of zebrautl.h instead of alexutil.h.
16 * Revision 1.20 1996/05/14 12:10:16 quinn
19 * Revision 1.19 1996/05/01 07:16:30 quinn
22 * Revision 1.18 1996/04/09 06:47:30 adam
23 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
25 * Revision 1.17 1996/03/20 13:29:11 quinn
28 * Revision 1.16 1995/12/12 15:57:57 adam
29 * Implemented mf_unlink. cf_unlink uses mf_unlink.
31 * Revision 1.15 1995/12/08 16:21:14 adam
32 * Work on commit/update.
34 * Revision 1.14 1995/12/05 13:12:37 quinn
37 * Revision 1.13 1995/11/30 17:00:50 adam
38 * Several bug fixes. Commit system runs now.
40 * Revision 1.12 1995/11/24 17:26:11 quinn
41 * Mostly about making some ISAM stuff in the config file optional.
43 * Revision 1.11 1995/11/13 09:32:43 quinn
46 * Revision 1.10 1995/09/04 12:33:22 adam
47 * Various cleanup. YAZ util used instead.
49 * Revision 1.9 1994/11/04 14:26:39 quinn
52 * Revision 1.8 1994/10/05 16:56:42 quinn
55 * Revision 1.7 1994/09/19 14:12:37 quinn
58 * Revision 1.6 1994/09/14 13:10:15 quinn
59 * Corrected some bugs in the init-phase
61 * Revision 1.5 1994/09/12 08:01:51 quinn
64 * Revision 1.4 1994/09/01 14:51:07 quinn
65 * Allowed mf_write to write beyond eof+1.
67 * Revision 1.3 1994/08/24 09:37:17 quinn
68 * Changed reaction to read return values.
70 * Revision 1.2 1994/08/23 14:50:48 quinn
73 * Revision 1.1 1994/08/23 14:41:33 quinn
74 * First functional version.
80 * TODO: The size estimates in init may not be accurate due to
81 * only partially written final blocks.
84 #include <sys/types.h>
101 static MFile_area_struct *open_areas = 0;
102 static MFile_area_struct *default_area = 0;
104 static int scan_areadef(MFile_area ma, const char *name)
107 * If no definition is given, use current directory, unlimited.
109 const char *ad = res_get_def(common_resource, name, ".:-1b");
110 char dirname[FILENAME_MAX+1];
111 mf_dir **dp = &ma->dirs, *dir = *dp;
115 logf (LOG_FATAL, "Could not find resource '%s'", name);
120 const char *ad0 = ad;
121 int i = 0, fact = 1, multi, size = 0;
123 while (*ad == ' ' || *ad == '\t')
127 while (*ad && *ad != ':')
129 if (i < FILENAME_MAX)
136 logf (LOG_FATAL, "Missing colon after path: %s", ad0);
141 logf (LOG_FATAL, "Empty path: %s", ad0);
144 while (*ad == ' ' || *ad == '\t')
154 if (*ad < '0' || *ad > '9')
156 logf (LOG_FATAL, "Missing size after path: %s", ad0);
160 while (*ad >= '0' && *ad <= '9')
161 size = size*10 + (*ad++ - '0');
164 case 'B': case 'b': multi = 1; break;
165 case 'K': case 'k': multi = 1024; break;
166 case 'M': case 'm': multi = 1048576; break;
168 logf (LOG_FATAL, "Missing unit: %s", ad0);
171 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
175 *dp = dir = xmalloc(sizeof(mf_dir));
177 strcpy(dir->name, dirname);
178 dir->max_bytes = dir->avail_bytes = fact * size * multi;
184 static int file_position(MFile mf, int pos, int offset)
186 int off = 0, c = mf->cur_file, ps;
188 if ((c > 0 && pos <= mf->files[c-1].top) ||
189 (c < mf->no_files -1 && pos > mf->files[c].top))
192 while (c + 1 < mf->no_files && mf->files[c].top < pos)
194 off += mf->files[c].blocks;
197 assert(c < mf->no_files);
200 off = c ? (mf->files[c-1].top + 1) : 0;
201 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
202 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
204 if (!mf->wr && errno == ENOENT && off == 0)
206 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
209 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
212 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
219 static int cmp_part_file(const void *p1, const void *p2)
221 return ((part_file *)p1)->number - ((part_file *)p2)->number;
225 * Create a new area, cotaining metafiles in directories.
226 * Find the part-files in each directory, and inventory the existing metafiles.
228 MFile_area mf_init(const char *name)
230 MFile_area ma = xmalloc(sizeof(MFile_area_struct)), mp;
233 part_file *part_f = 0;
237 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
239 logf (LOG_DEBUG, "mf_init(%s)", name);
240 for (mp = open_areas; mp; mp = mp->next)
241 if (!strcmp(name, mp->name))
243 ma = xmalloc(sizeof(MFile_area_struct));
244 strcpy(ma->name, name);
245 ma->next = open_areas;
249 if (scan_areadef(ma, name) < 0)
251 logf (LOG_FATAL, "Failed to access description of '%s'", name);
254 /* look at each directory */
255 for (dirp = ma->dirs; dirp; dirp = dirp->next)
257 if (!(dd = opendir(dirp->name)))
259 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", dirp->name);
262 /* look at each file */
263 while ((dent = readdir(dd)))
265 if (*dent->d_name == '.')
267 if (sscanf(dent->d_name, "%[^.].mf.%d", metaname, &number) != 2)
269 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
272 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
275 if (!strcmp(meta_f->name, metaname))
277 part_f = &meta_f->files[meta_f->no_files++];
284 meta_f = xmalloc(sizeof(*meta_f));
286 meta_f->next = ma->mfiles;
288 meta_f->cur_file = -1;
290 strcpy(meta_f->name, metaname);
291 part_f = &meta_f->files[0];
292 meta_f->no_files = 1;
294 part_f->number = number;
297 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
298 part_f->path = xstrdup(tmpnam);
300 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
302 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
306 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
308 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
313 if (dirp->max_bytes >= 0)
314 dirp->avail_bytes -= part_f->bytes;
318 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
320 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
322 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
330 * If !ma, Use MF_DEFAULT_AREA.
332 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
334 struct meta_file *new;
336 char tmp[FILENAME_MAX+1];
339 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
340 wflag ? "RW" : "RDONLY");
343 if (!default_area && !(default_area = mf_init(MF_DEFAULT_AREA)))
345 logf (LOG_FATAL, "Failed to open default area.");
350 for (new = ma->mfiles; new; new = new->next)
351 if (!strcmp(name, new->name))
358 new = xmalloc(sizeof(*new));
359 strcpy(new->name, name);
360 /* allocate one, empty file */
362 new->files[0].bytes = new->files[0].blocks = 0;
363 new->files[0].top = -1;
364 new->files[0].number = 0;
365 new->files[0].fd = -1;
366 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
367 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
368 new->min_bytes_creat; dp = dp->next);
371 logf (LOG_FATAL, "Insufficient space for new mfile.");
374 new->files[0].dir = dp;
375 sprintf(tmp, "%s/%s.mf.%d", dp->name, new->name, 0);
376 new->files[0].path = xstrdup(tmp);
381 for (i = 0; i < new->no_files; i++)
383 if (new->files[i].bytes % block_size)
384 new->files[i].bytes += block_size - new->files[i].bytes %
386 new->files[i].blocks = new->files[i].bytes / block_size;
390 new->blocksize = block_size;
391 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
396 for (i = 0; i < new->no_files; i++)
398 new->files[i].blocks = new->files[i].bytes / new->blocksize;
399 if (i == new->no_files - 1)
400 new->files[i].top = -1;
402 new->files[i].top = i ? (new->files[i-1].top + new->files[i].blocks)
403 : (new->files[i].blocks - 1);
411 int mf_close(MFile mf)
415 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
417 for (i = 0; i < mf->no_files; i++)
418 if (mf->files[i].fd >= 0)
420 close(mf->files[i].fd);
421 mf->files[i].fd = -1;
428 * Read one block from a metafile. Interface mirrors bfile.
430 int mf_read(MFile mf, int no, int offset, int num, void *buf)
434 if ((rd = file_position(mf, no, offset)) < 0)
439 toread = num ? num : mf->blocksize;
440 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
442 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
443 mf->files[mf->cur_file].path);
446 else if (rd < toread)
455 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
457 int ps, nblocks, towrite;
459 char tmp[FILENAME_MAX+1];
460 unsigned char dummych = '\xff';
462 if ((ps = file_position(mf, no, offset)) < 0)
464 /* file needs to grow */
465 while (ps >= mf->files[mf->cur_file].blocks)
467 /* file overflow - allocate new file */
468 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
469 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
470 mf->files[mf->cur_file].dir->avail_bytes)
473 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
476 logf (LOG_DEBUG, "Capping off file %s at pos %d",
477 mf->files[mf->cur_file].path, nblocks);
478 if ((ps = file_position(mf,
479 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
480 mf->files[mf->cur_file].blocks + nblocks, 0)) < 0)
482 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
484 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
487 mf->files[mf->cur_file].blocks += nblocks;
488 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
489 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
493 logf (LOG_DEBUG, "Creating new file.");
494 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
495 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
498 logf (LOG_FATAL, "Cannot allocate more space for %s",
502 mf->files[mf->cur_file].top = (mf->cur_file ?
503 mf->files[mf->cur_file-1].top : -1) +
504 mf->files[mf->cur_file].blocks;
505 mf->files[++(mf->cur_file)].top = -1;
506 mf->files[mf->cur_file].dir = dp;
507 mf->files[mf->cur_file].number =
508 mf->files[mf->cur_file-1].number + 1;
509 mf->files[mf->cur_file].blocks =
510 mf->files[mf->cur_file].bytes = 0;
511 mf->files[mf->cur_file].fd = -1;
512 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
513 mf->files[mf->cur_file].number);
514 mf->files[mf->cur_file].path = xstrdup(tmp);
516 /* open new file and position at beginning */
517 if ((ps = file_position(mf, no, offset)) < 0)
522 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
523 mf->files[mf->cur_file].blocks += nblocks;
524 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
525 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
526 mf->files[mf->cur_file].dir->avail_bytes -=
527 nblocks * mf->blocksize;
530 towrite = num ? num : mf->blocksize;
531 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
533 logf (LOG_FATAL|LOG_ERRNO, "Write failed");
540 * Destroy a metafile, unlinking component files. File must be open.
542 int mf_unlink(MFile mf)
546 for (i = 0; i < mf->no_files; i++)
547 unlink (mf->files[i].path);
552 * Unlink the file by name, rather than MFile-handle. File should be closed.
554 int mf_unlink_name(MFile_area ma, const char *name)