2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1997-09-17 12:19:06 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.23 1997/09/09 13:37:53 adam
12 * Partial port to WIN95/NT.
14 * Revision 1.22 1997/09/04 13:56:39 adam
15 * Added O_BINARY to open calls.
17 * Revision 1.21 1996/10/29 13:56:18 adam
18 * Include of zebrautl.h instead of alexutil.h.
20 * Revision 1.20 1996/05/14 12:10:16 quinn
23 * Revision 1.19 1996/05/01 07:16:30 quinn
26 * Revision 1.18 1996/04/09 06:47:30 adam
27 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
29 * Revision 1.17 1996/03/20 13:29:11 quinn
32 * Revision 1.16 1995/12/12 15:57:57 adam
33 * Implemented mf_unlink. cf_unlink uses mf_unlink.
35 * Revision 1.15 1995/12/08 16:21:14 adam
36 * Work on commit/update.
38 * Revision 1.14 1995/12/05 13:12:37 quinn
41 * Revision 1.13 1995/11/30 17:00:50 adam
42 * Several bug fixes. Commit system runs now.
44 * Revision 1.12 1995/11/24 17:26:11 quinn
45 * Mostly about making some ISAM stuff in the config file optional.
47 * Revision 1.11 1995/11/13 09:32:43 quinn
50 * Revision 1.10 1995/09/04 12:33:22 adam
51 * Various cleanup. YAZ util used instead.
53 * Revision 1.9 1994/11/04 14:26:39 quinn
56 * Revision 1.8 1994/10/05 16:56:42 quinn
59 * Revision 1.7 1994/09/19 14:12:37 quinn
62 * Revision 1.6 1994/09/14 13:10:15 quinn
63 * Corrected some bugs in the init-phase
65 * Revision 1.5 1994/09/12 08:01:51 quinn
68 * Revision 1.4 1994/09/01 14:51:07 quinn
69 * Allowed mf_write to write beyond eof+1.
71 * Revision 1.3 1994/08/24 09:37:17 quinn
72 * Changed reaction to read return values.
74 * Revision 1.2 1994/08/23 14:50:48 quinn
77 * Revision 1.1 1994/08/23 14:41:33 quinn
78 * First functional version.
84 * TODO: The size estimates in init may not be accurate due to
85 * only partially written final blocks.
88 #include <sys/types.h>
102 #include <zebrautl.h>
105 static int scan_areadef(MFile_area ma, const char *name, const char *ad)
108 * If no definition is given, use current directory, unlimited.
110 char dirname[FILENAME_MAX+1];
111 mf_dir **dp = &ma->dirs, *dir = *dp;
117 const char *ad0 = ad;
118 int i = 0, fact = 1, multi, size = 0;
120 while (*ad == ' ' || *ad == '\t')
124 while (*ad && *ad != ':')
126 if (i < FILENAME_MAX)
133 logf (LOG_FATAL, "Missing colon after path: %s", ad0);
138 logf (LOG_FATAL, "Empty path: %s", ad0);
141 while (*ad == ' ' || *ad == '\t')
151 if (*ad < '0' || *ad > '9')
153 logf (LOG_FATAL, "Missing size after path: %s", ad0);
157 while (*ad >= '0' && *ad <= '9')
158 size = size*10 + (*ad++ - '0');
161 case 'B': case 'b': multi = 1; break;
162 case 'K': case 'k': multi = 1024; break;
163 case 'M': case 'm': multi = 1048576; break;
165 logf (LOG_FATAL, "Missing unit: %s", ad0);
168 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
172 *dp = dir = xmalloc(sizeof(mf_dir));
174 strcpy(dir->name, dirname);
175 dir->max_bytes = dir->avail_bytes = fact * size * multi;
181 static int file_position(MFile mf, int pos, int offset)
183 int off = 0, c = mf->cur_file, ps;
185 if ((c > 0 && pos <= mf->files[c-1].top) ||
186 (c < mf->no_files -1 && pos > mf->files[c].top))
189 while (c + 1 < mf->no_files && mf->files[c].top < pos)
191 off += mf->files[c].blocks;
194 assert(c < mf->no_files);
197 off = c ? (mf->files[c-1].top + 1) : 0;
198 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
199 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
201 if (!mf->wr && errno == ENOENT && off == 0)
203 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
206 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
209 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
216 static int cmp_part_file(const void *p1, const void *p2)
218 return ((part_file *)p1)->number - ((part_file *)p2)->number;
222 * Create a new area, cotaining metafiles in directories.
223 * Find the part-files in each directory, and inventory the existing metafiles.
225 MFile_area mf_init(const char *name, const char *spec)
227 MFile_area ma = xmalloc(sizeof(MFile_area_struct));
230 part_file *part_f = 0;
234 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
236 logf (LOG_DEBUG, "mf_init(%s)", name);
237 ma = xmalloc(sizeof(MFile_area_struct));
238 strcpy(ma->name, name);
241 if (scan_areadef(ma, name, spec) < 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");
334 for (new = ma->mfiles; new; new = new->next)
335 if (!strcmp(name, new->name))
342 new = xmalloc(sizeof(*new));
343 strcpy(new->name, name);
344 /* allocate one, empty file */
346 new->files[0].bytes = new->files[0].blocks = 0;
347 new->files[0].top = -1;
348 new->files[0].number = 0;
349 new->files[0].fd = -1;
350 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
351 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
352 new->min_bytes_creat; dp = dp->next);
355 logf (LOG_FATAL, "Insufficient space for new mfile.");
358 new->files[0].dir = dp;
359 sprintf(tmp, "%s/%s.mf.%d", dp->name, new->name, 0);
360 new->files[0].path = xstrdup(tmp);
365 for (i = 0; i < new->no_files; i++)
367 if (new->files[i].bytes % block_size)
368 new->files[i].bytes += block_size - new->files[i].bytes %
370 new->files[i].blocks = new->files[i].bytes / block_size;
374 new->blocksize = block_size;
375 new->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
380 for (i = 0; i < new->no_files; i++)
382 new->files[i].blocks = new->files[i].bytes / new->blocksize;
383 if (i == new->no_files - 1)
384 new->files[i].top = -1;
386 new->files[i].top = i ? (new->files[i-1].top + new->files[i].blocks)
387 : (new->files[i].blocks - 1);
395 int mf_close(MFile mf)
399 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
401 for (i = 0; i < mf->no_files; i++)
402 if (mf->files[i].fd >= 0)
404 close(mf->files[i].fd);
405 mf->files[i].fd = -1;
412 * Read one block from a metafile. Interface mirrors bfile.
414 int mf_read(MFile mf, int no, int offset, int num, void *buf)
418 if ((rd = file_position(mf, no, offset)) < 0)
423 toread = num ? num : mf->blocksize;
424 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
426 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
427 mf->files[mf->cur_file].path);
430 else if (rd < toread)
439 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
441 int ps, nblocks, towrite;
443 char tmp[FILENAME_MAX+1];
444 unsigned char dummych = '\xff';
446 if ((ps = file_position(mf, no, offset)) < 0)
448 /* file needs to grow */
449 while (ps >= mf->files[mf->cur_file].blocks)
451 /* file overflow - allocate new file */
452 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
453 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
454 mf->files[mf->cur_file].dir->avail_bytes)
457 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
460 logf (LOG_DEBUG, "Capping off file %s at pos %d",
461 mf->files[mf->cur_file].path, nblocks);
462 if ((ps = file_position(mf,
463 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
464 mf->files[mf->cur_file].blocks + nblocks, 0)) < 0)
466 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
468 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
471 mf->files[mf->cur_file].blocks += nblocks;
472 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
473 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
477 logf (LOG_DEBUG, "Creating new file.");
478 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
479 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
482 logf (LOG_FATAL, "Cannot allocate more space for %s",
486 mf->files[mf->cur_file].top = (mf->cur_file ?
487 mf->files[mf->cur_file-1].top : -1) +
488 mf->files[mf->cur_file].blocks;
489 mf->files[++(mf->cur_file)].top = -1;
490 mf->files[mf->cur_file].dir = dp;
491 mf->files[mf->cur_file].number =
492 mf->files[mf->cur_file-1].number + 1;
493 mf->files[mf->cur_file].blocks =
494 mf->files[mf->cur_file].bytes = 0;
495 mf->files[mf->cur_file].fd = -1;
496 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
497 mf->files[mf->cur_file].number);
498 mf->files[mf->cur_file].path = xstrdup(tmp);
500 /* open new file and position at beginning */
501 if ((ps = file_position(mf, no, offset)) < 0)
506 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
507 mf->files[mf->cur_file].blocks += nblocks;
508 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
509 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
510 mf->files[mf->cur_file].dir->avail_bytes -=
511 nblocks * mf->blocksize;
514 towrite = num ? num : mf->blocksize;
515 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
517 logf (LOG_FATAL|LOG_ERRNO, "Write failed");
524 * Destroy a metafile, unlinking component files. File must be open.
526 int mf_unlink(MFile mf)
530 for (i = 0; i < mf->no_files; i++)
531 unlink (mf->files[i].path);
536 * Unlink the file by name, rather than MFile-handle. File should be closed.
538 int mf_unlink_name(MFile_area ma, const char *name)