2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1996-03-20 13:29:11 quinn
10 * Revision 1.16 1995/12/12 15:57:57 adam
11 * Implemented mf_unlink. cf_unlink uses mf_unlink.
13 * Revision 1.15 1995/12/08 16:21:14 adam
14 * Work on commit/update.
16 * Revision 1.14 1995/12/05 13:12:37 quinn
19 * Revision 1.13 1995/11/30 17:00:50 adam
20 * Several bug fixes. Commit system runs now.
22 * Revision 1.12 1995/11/24 17:26:11 quinn
23 * Mostly about making some ISAM stuff in the config file optional.
25 * Revision 1.11 1995/11/13 09:32:43 quinn
28 * Revision 1.10 1995/09/04 12:33:22 adam
29 * Various cleanup. YAZ util used instead.
31 * Revision 1.9 1994/11/04 14:26:39 quinn
34 * Revision 1.8 1994/10/05 16:56:42 quinn
37 * Revision 1.7 1994/09/19 14:12:37 quinn
40 * Revision 1.6 1994/09/14 13:10:15 quinn
41 * Corrected some bugs in the init-phase
43 * Revision 1.5 1994/09/12 08:01:51 quinn
46 * Revision 1.4 1994/09/01 14:51:07 quinn
47 * Allowed mf_write to write beyond eof+1.
49 * Revision 1.3 1994/08/24 09:37:17 quinn
50 * Changed reaction to read return values.
52 * Revision 1.2 1994/08/23 14:50:48 quinn
55 * Revision 1.1 1994/08/23 14:41:33 quinn
56 * First functional version.
62 * TODO: The size estimates in init may not be accurate due to
63 * only partially written final blocks.
66 #include <sys/types.h>
78 static MFile_area_struct *open_areas = 0;
79 static MFile_area_struct *default_area = 0;
81 static int scan_areadef(MFile_area ma, const char *name)
84 * If no definition is given, use current directory, unlimited.
86 const char *ad = res_get_def(common_resource, name, ".:-1b");
87 int offset = 0, rs, size, multi, rd;
88 char dirname[FILENAME_MAX+1], unit;
89 mf_dir **dp = &ma->dirs, *dir = *dp;
93 logf (LOG_FATAL, "Could not find resource '%s'", name);
98 rs = sscanf(ad + offset, "%[^:]:%d%c %n", dirname, &size, &unit, &rd);
103 logf (LOG_FATAL, "Illegal directory description: %s", ad + offset);
108 case 'B': case 'b': multi = 1; break;
109 case 'K': case 'k': multi = 1024; break;
110 case 'M': case 'm': multi = 1048576; break;
112 logf (LOG_FATAL, "Illegal unit: %c in %s", unit, ad + offset);
115 *dp = dir = xmalloc(sizeof(mf_dir));
117 strcpy(dir->name, dirname);
118 dir->max_bytes = dir->avail_bytes = size * multi;
125 static int file_position(MFile mf, int pos, int offset)
127 int off = 0, c = mf->cur_file, ps;
129 if ((c > 0 && pos <= mf->files[c-1].top) ||
130 (c < mf->no_files -1 && pos > mf->files[c].top))
133 while (mf->files[c].top >= 0 && mf->files[c].top < pos)
135 off += mf->files[c].blocks;
138 assert(c < mf->no_files);
141 off = c ? (mf->files[c-1].top + 1) : 0;
142 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
143 mf->wr ? O_RDWR|O_CREAT : O_RDONLY, 0666)) < 0)
145 if (!mf->wr && errno == ENOENT && off == 0)
147 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
150 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
153 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
160 static int cmp_part_file(const void *p1, const void *p2)
162 return ((part_file *)p1)->number - ((part_file *)p2)->number;
166 * Create a new area, cotaining metafiles in directories.
167 * Find the part-files in each directory, and inventory the existing metafiles.
169 MFile_area mf_init(const char *name)
171 MFile_area ma = xmalloc(sizeof(MFile_area_struct)), mp;
174 part_file *part_f = 0;
178 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
180 logf (LOG_DEBUG, "mf_init(%s)", name);
181 for (mp = open_areas; mp; mp = mp->next)
182 if (!strcmp(name, mp->name))
184 ma = xmalloc(sizeof(MFile_area_struct));
185 strcpy(ma->name, name);
186 ma->next = open_areas;
190 if (scan_areadef(ma, name) < 0)
192 logf (LOG_FATAL, "Failed to access description of '%s'", name);
195 /* look at each directory */
196 for (dirp = ma->dirs; dirp; dirp = dirp->next)
198 if (!(dd = opendir(dirp->name)))
200 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", dirp->name);
203 /* look at each file */
204 while ((dent = readdir(dd)))
206 if (*dent->d_name == '.')
208 if (sscanf(dent->d_name, "%[^.].mf.%d", metaname, &number) != 2)
210 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
213 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
216 if (!strcmp(meta_f->name, metaname))
218 part_f = &meta_f->files[meta_f->no_files++];
225 meta_f = xmalloc(sizeof(*meta_f));
227 meta_f->next = ma->mfiles;
229 meta_f->cur_file = -1;
231 strcpy(meta_f->name, metaname);
232 part_f = &meta_f->files[0];
233 meta_f->no_files = 1;
235 part_f->number = number;
238 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
239 part_f->path = xstrdup(tmpnam);
241 if ((fd = open(part_f->path, O_RDONLY)) < 0)
243 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
247 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
249 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
254 if (dirp->max_bytes >= 0)
255 dirp->avail_bytes -= part_f->bytes;
259 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
261 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
263 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
271 * If !ma, Use MF_DEFAULT_AREA.
273 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
275 struct meta_file *new;
277 char tmp[FILENAME_MAX+1];
280 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
281 wflag ? "RW" : "RDONLY");
284 if (!default_area && !(default_area = mf_init(MF_DEFAULT_AREA)))
286 logf (LOG_FATAL, "Failed to open default area.");
291 for (new = ma->mfiles; new; new = new->next)
292 if (!strcmp(name, new->name))
299 new = xmalloc(sizeof(*new));
300 strcpy(new->name, name);
301 /* allocate one, empty file */
303 new->files[0].bytes = new->files[0].blocks = 0;
304 new->files[0].top = -1;
305 new->files[0].number = 0;
306 new->files[0].fd = -1;
307 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
308 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
309 new->min_bytes_creat; dp = dp->next);
312 logf (LOG_FATAL, "Insufficient space for new mfile.");
315 new->files[0].dir = dp;
316 sprintf(tmp, "%s/%s.mf.%d", dp->name, new->name, 0);
317 new->files[0].path = xstrdup(tmp);
322 for (i = 0; i < new->no_files; i++)
324 if (new->files[i].bytes % block_size)
325 new->files[i].bytes += block_size - new->files[i].bytes %
327 new->files[i].blocks = new->files[i].bytes / block_size;
331 new->blocksize = block_size;
332 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
337 for (i = 0; i < new->no_files; i++)
339 new->files[i].blocks = new->files[i].bytes / new->blocksize;
340 if (i == new->no_files)
341 new->files[i].top = -1;
343 new->files[i].top = i ? (new->files[i-1].top + new->files[i].blocks)
344 : (new->files[i].blocks - 1);
352 int mf_close(MFile mf)
356 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
358 for (i = 0; i < mf->no_files; i++)
359 if (mf->files[i].fd >= 0)
361 close(mf->files[i].fd);
362 mf->files[i].fd = -1;
369 * Read one block from a metafile. Interface mirrors bfile.
371 int mf_read(MFile mf, int no, int offset, int num, void *buf)
375 if ((rd = file_position(mf, no, offset)) < 0)
380 toread = num ? num : mf->blocksize;
381 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
383 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
384 mf->files[mf->cur_file].path);
387 else if (rd < toread)
396 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
398 int ps, nblocks, towrite;
400 char tmp[FILENAME_MAX+1];
401 unsigned char dummych = '\xff';
403 if ((ps = file_position(mf, no, offset)) < 0)
405 /* file needs to grow */
406 while (ps >= mf->files[mf->cur_file].blocks)
408 /* file overflow - allocate new file */
409 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
410 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
411 mf->files[mf->cur_file].dir->avail_bytes)
414 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
417 logf (LOG_DEBUG, "Capping off file %s at pos %d",
418 mf->files[mf->cur_file].path, nblocks);
419 if ((ps = file_position(mf,
420 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
421 mf->files[mf->cur_file].blocks + nblocks, 0)) < 0)
423 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
425 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
428 mf->files[mf->cur_file].blocks += nblocks;
429 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
430 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
434 logf (LOG_DEBUG, "Creating new file.");
435 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
436 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
439 logf (LOG_FATAL, "Cannot allocate more space for %s",
443 mf->files[mf->cur_file].top = (mf->cur_file ?
444 mf->files[mf->cur_file-1].top : -1) +
445 mf->files[mf->cur_file].blocks;
446 mf->files[++(mf->cur_file)].top = -1;
447 mf->files[mf->cur_file].dir = dp;
448 mf->files[mf->cur_file].number =
449 mf->files[mf->cur_file-1].number + 1;
450 mf->files[mf->cur_file].blocks =
451 mf->files[mf->cur_file].bytes = 0;
452 mf->files[mf->cur_file].fd = -1;
453 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
454 mf->files[mf->cur_file].number);
455 mf->files[mf->cur_file].path = xstrdup(tmp);
457 /* open new file and position at beginning */
458 if ((ps = file_position(mf, no, offset)) < 0)
463 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
464 mf->files[mf->cur_file].blocks += nblocks;
465 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
466 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
467 mf->files[mf->cur_file].dir->avail_bytes -=
468 nblocks * mf->blocksize;
471 towrite = num ? num : mf->blocksize;
472 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
474 logf (LOG_FATAL|LOG_ERRNO, "Write failed");
481 * Destroy a metafile, unlinking component files. File must be open.
483 int mf_unlink(MFile mf)
487 for (i = 0; i < mf->no_files; i++)
488 unlink (mf->files[i].path);
493 * Unlink the file by name, rather than MFile-handle. File should be closed.
495 int mf_unlink_name(MFile_area ma, const char *name)