2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1997-09-04 13:56:39 adam
8 * Added O_BINARY to open calls.
10 * Revision 1.21 1996/10/29 13:56:18 adam
11 * Include of zebrautl.h instead of alexutil.h.
13 * Revision 1.20 1996/05/14 12:10:16 quinn
16 * Revision 1.19 1996/05/01 07:16:30 quinn
19 * Revision 1.18 1996/04/09 06:47:30 adam
20 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
22 * Revision 1.17 1996/03/20 13:29:11 quinn
25 * Revision 1.16 1995/12/12 15:57:57 adam
26 * Implemented mf_unlink. cf_unlink uses mf_unlink.
28 * Revision 1.15 1995/12/08 16:21:14 adam
29 * Work on commit/update.
31 * Revision 1.14 1995/12/05 13:12:37 quinn
34 * Revision 1.13 1995/11/30 17:00:50 adam
35 * Several bug fixes. Commit system runs now.
37 * Revision 1.12 1995/11/24 17:26:11 quinn
38 * Mostly about making some ISAM stuff in the config file optional.
40 * Revision 1.11 1995/11/13 09:32:43 quinn
43 * Revision 1.10 1995/09/04 12:33:22 adam
44 * Various cleanup. YAZ util used instead.
46 * Revision 1.9 1994/11/04 14:26:39 quinn
49 * Revision 1.8 1994/10/05 16:56:42 quinn
52 * Revision 1.7 1994/09/19 14:12:37 quinn
55 * Revision 1.6 1994/09/14 13:10:15 quinn
56 * Corrected some bugs in the init-phase
58 * Revision 1.5 1994/09/12 08:01:51 quinn
61 * Revision 1.4 1994/09/01 14:51:07 quinn
62 * Allowed mf_write to write beyond eof+1.
64 * Revision 1.3 1994/08/24 09:37:17 quinn
65 * Changed reaction to read return values.
67 * Revision 1.2 1994/08/23 14:50:48 quinn
70 * Revision 1.1 1994/08/23 14:41:33 quinn
71 * First functional version.
77 * TODO: The size estimates in init may not be accurate due to
78 * only partially written final blocks.
81 #include <sys/types.h>
93 static MFile_area_struct *open_areas = 0;
94 static MFile_area_struct *default_area = 0;
96 static int scan_areadef(MFile_area ma, const char *name)
99 * If no definition is given, use current directory, unlimited.
101 const char *ad = res_get_def(common_resource, name, ".:-1b");
102 char dirname[FILENAME_MAX+1];
103 mf_dir **dp = &ma->dirs, *dir = *dp;
107 logf (LOG_FATAL, "Could not find resource '%s'", name);
112 const char *ad0 = ad;
113 int i = 0, fact = 1, multi, size = 0;
115 while (*ad == ' ' || *ad == '\t')
119 while (*ad && *ad != ':')
121 if (i < FILENAME_MAX)
128 logf (LOG_FATAL, "Missing colon after path: %s", ad0);
133 logf (LOG_FATAL, "Empty path: %s", ad0);
136 while (*ad == ' ' || *ad == '\t')
146 if (*ad < '0' || *ad > '9')
148 logf (LOG_FATAL, "Missing size after path: %s", ad0);
152 while (*ad >= '0' && *ad <= '9')
153 size = size*10 + (*ad++ - '0');
156 case 'B': case 'b': multi = 1; break;
157 case 'K': case 'k': multi = 1024; break;
158 case 'M': case 'm': multi = 1048576; break;
160 logf (LOG_FATAL, "Missing unit: %s", ad0);
163 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
167 *dp = dir = xmalloc(sizeof(mf_dir));
169 strcpy(dir->name, dirname);
170 dir->max_bytes = dir->avail_bytes = fact * size * multi;
176 static int file_position(MFile mf, int pos, int offset)
178 int off = 0, c = mf->cur_file, ps;
180 if ((c > 0 && pos <= mf->files[c-1].top) ||
181 (c < mf->no_files -1 && pos > mf->files[c].top))
184 while (c + 1 < mf->no_files && mf->files[c].top < pos)
186 off += mf->files[c].blocks;
189 assert(c < mf->no_files);
192 off = c ? (mf->files[c-1].top + 1) : 0;
193 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
194 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
196 if (!mf->wr && errno == ENOENT && off == 0)
198 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
201 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
204 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
211 static int cmp_part_file(const void *p1, const void *p2)
213 return ((part_file *)p1)->number - ((part_file *)p2)->number;
217 * Create a new area, cotaining metafiles in directories.
218 * Find the part-files in each directory, and inventory the existing metafiles.
220 MFile_area mf_init(const char *name)
222 MFile_area ma = xmalloc(sizeof(MFile_area_struct)), mp;
225 part_file *part_f = 0;
229 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
231 logf (LOG_DEBUG, "mf_init(%s)", name);
232 for (mp = open_areas; mp; mp = mp->next)
233 if (!strcmp(name, mp->name))
235 ma = xmalloc(sizeof(MFile_area_struct));
236 strcpy(ma->name, name);
237 ma->next = open_areas;
241 if (scan_areadef(ma, name) < 0)
243 logf (LOG_FATAL, "Failed to access description of '%s'", name);
246 /* look at each directory */
247 for (dirp = ma->dirs; dirp; dirp = dirp->next)
249 if (!(dd = opendir(dirp->name)))
251 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", dirp->name);
254 /* look at each file */
255 while ((dent = readdir(dd)))
257 if (*dent->d_name == '.')
259 if (sscanf(dent->d_name, "%[^.].mf.%d", metaname, &number) != 2)
261 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
264 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
267 if (!strcmp(meta_f->name, metaname))
269 part_f = &meta_f->files[meta_f->no_files++];
276 meta_f = xmalloc(sizeof(*meta_f));
278 meta_f->next = ma->mfiles;
280 meta_f->cur_file = -1;
282 strcpy(meta_f->name, metaname);
283 part_f = &meta_f->files[0];
284 meta_f->no_files = 1;
286 part_f->number = number;
289 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
290 part_f->path = xstrdup(tmpnam);
292 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
294 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
298 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
300 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
305 if (dirp->max_bytes >= 0)
306 dirp->avail_bytes -= part_f->bytes;
310 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
312 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
314 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
322 * If !ma, Use MF_DEFAULT_AREA.
324 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
326 struct meta_file *new;
328 char tmp[FILENAME_MAX+1];
331 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
332 wflag ? "RW" : "RDONLY");
335 if (!default_area && !(default_area = mf_init(MF_DEFAULT_AREA)))
337 logf (LOG_FATAL, "Failed to open default area.");
342 for (new = ma->mfiles; new; new = new->next)
343 if (!strcmp(name, new->name))
350 new = xmalloc(sizeof(*new));
351 strcpy(new->name, name);
352 /* allocate one, empty file */
354 new->files[0].bytes = new->files[0].blocks = 0;
355 new->files[0].top = -1;
356 new->files[0].number = 0;
357 new->files[0].fd = -1;
358 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
359 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
360 new->min_bytes_creat; dp = dp->next);
363 logf (LOG_FATAL, "Insufficient space for new mfile.");
366 new->files[0].dir = dp;
367 sprintf(tmp, "%s/%s.mf.%d", dp->name, new->name, 0);
368 new->files[0].path = xstrdup(tmp);
373 for (i = 0; i < new->no_files; i++)
375 if (new->files[i].bytes % block_size)
376 new->files[i].bytes += block_size - new->files[i].bytes %
378 new->files[i].blocks = new->files[i].bytes / block_size;
382 new->blocksize = block_size;
383 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
388 for (i = 0; i < new->no_files; i++)
390 new->files[i].blocks = new->files[i].bytes / new->blocksize;
391 if (i == new->no_files - 1)
392 new->files[i].top = -1;
394 new->files[i].top = i ? (new->files[i-1].top + new->files[i].blocks)
395 : (new->files[i].blocks - 1);
403 int mf_close(MFile mf)
407 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
409 for (i = 0; i < mf->no_files; i++)
410 if (mf->files[i].fd >= 0)
412 close(mf->files[i].fd);
413 mf->files[i].fd = -1;
420 * Read one block from a metafile. Interface mirrors bfile.
422 int mf_read(MFile mf, int no, int offset, int num, void *buf)
426 if ((rd = file_position(mf, no, offset)) < 0)
431 toread = num ? num : mf->blocksize;
432 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
434 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
435 mf->files[mf->cur_file].path);
438 else if (rd < toread)
447 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
449 int ps, nblocks, towrite;
451 char tmp[FILENAME_MAX+1];
452 unsigned char dummych = '\xff';
454 if ((ps = file_position(mf, no, offset)) < 0)
456 /* file needs to grow */
457 while (ps >= mf->files[mf->cur_file].blocks)
459 /* file overflow - allocate new file */
460 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
461 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
462 mf->files[mf->cur_file].dir->avail_bytes)
465 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
468 logf (LOG_DEBUG, "Capping off file %s at pos %d",
469 mf->files[mf->cur_file].path, nblocks);
470 if ((ps = file_position(mf,
471 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
472 mf->files[mf->cur_file].blocks + nblocks, 0)) < 0)
474 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
476 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
479 mf->files[mf->cur_file].blocks += nblocks;
480 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
481 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
485 logf (LOG_DEBUG, "Creating new file.");
486 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
487 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
490 logf (LOG_FATAL, "Cannot allocate more space for %s",
494 mf->files[mf->cur_file].top = (mf->cur_file ?
495 mf->files[mf->cur_file-1].top : -1) +
496 mf->files[mf->cur_file].blocks;
497 mf->files[++(mf->cur_file)].top = -1;
498 mf->files[mf->cur_file].dir = dp;
499 mf->files[mf->cur_file].number =
500 mf->files[mf->cur_file-1].number + 1;
501 mf->files[mf->cur_file].blocks =
502 mf->files[mf->cur_file].bytes = 0;
503 mf->files[mf->cur_file].fd = -1;
504 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
505 mf->files[mf->cur_file].number);
506 mf->files[mf->cur_file].path = xstrdup(tmp);
508 /* open new file and position at beginning */
509 if ((ps = file_position(mf, no, offset)) < 0)
514 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
515 mf->files[mf->cur_file].blocks += nblocks;
516 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
517 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
518 mf->files[mf->cur_file].dir->avail_bytes -=
519 nblocks * mf->blocksize;
522 towrite = num ? num : mf->blocksize;
523 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
525 logf (LOG_FATAL|LOG_ERRNO, "Write failed");
532 * Destroy a metafile, unlinking component files. File must be open.
534 int mf_unlink(MFile mf)
538 for (i = 0; i < mf->no_files; i++)
539 unlink (mf->files[i].path);
544 * Unlink the file by name, rather than MFile-handle. File should be closed.
546 int mf_unlink_name(MFile_area ma, const char *name)