2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 1999-02-02 14:50:02 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.29 1998/05/27 14:28:34 adam
11 * Fixed bug in mf_write. 'Cap off' byte written at wrong offset.
13 * Revision 1.28 1998/05/20 10:00:35 adam
14 * Fixed register spec so that colon isn't treated as size separator
15 * unless followed by [0-9+-] in order to allow DOS drive specifications.
17 * Revision 1.27 1998/02/10 11:55:07 adam
20 * Revision 1.26 1997/10/27 14:25:38 adam
23 * Revision 1.25 1997/09/18 08:59:16 adam
24 * Extra generic handle for the character mapping routines.
26 * Revision 1.24 1997/09/17 12:19:06 adam
27 * Zebra version corresponds to YAZ version 1.4.
28 * Changed Zebra server so that it doesn't depend on global common_resource.
30 * Revision 1.23 1997/09/09 13:37:53 adam
31 * Partial port to WIN95/NT.
33 * Revision 1.22 1997/09/04 13:56:39 adam
34 * Added O_BINARY to open calls.
36 * Revision 1.21 1996/10/29 13:56:18 adam
37 * Include of zebrautl.h instead of alexutil.h.
39 * Revision 1.20 1996/05/14 12:10:16 quinn
42 * Revision 1.19 1996/05/01 07:16:30 quinn
45 * Revision 1.18 1996/04/09 06:47:30 adam
46 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
48 * Revision 1.17 1996/03/20 13:29:11 quinn
51 * Revision 1.16 1995/12/12 15:57:57 adam
52 * Implemented mf_unlink. cf_unlink uses mf_unlink.
54 * Revision 1.15 1995/12/08 16:21:14 adam
55 * Work on commit/update.
57 * Revision 1.14 1995/12/05 13:12:37 quinn
60 * Revision 1.13 1995/11/30 17:00:50 adam
61 * Several bug fixes. Commit system runs now.
63 * Revision 1.12 1995/11/24 17:26:11 quinn
64 * Mostly about making some ISAM stuff in the config file optional.
66 * Revision 1.11 1995/11/13 09:32:43 quinn
69 * Revision 1.10 1995/09/04 12:33:22 adam
70 * Various cleanup. YAZ util used instead.
72 * Revision 1.9 1994/11/04 14:26:39 quinn
75 * Revision 1.8 1994/10/05 16:56:42 quinn
78 * Revision 1.7 1994/09/19 14:12:37 quinn
81 * Revision 1.6 1994/09/14 13:10:15 quinn
82 * Corrected some bugs in the init-phase
84 * Revision 1.5 1994/09/12 08:01:51 quinn
87 * Revision 1.4 1994/09/01 14:51:07 quinn
88 * Allowed mf_write to write beyond eof+1.
90 * Revision 1.3 1994/08/24 09:37:17 quinn
91 * Changed reaction to read return values.
93 * Revision 1.2 1994/08/23 14:50:48 quinn
96 * Revision 1.1 1994/08/23 14:41:33 quinn
97 * First functional version.
103 * TODO: The size estimates in init may not be accurate due to
104 * only partially written final blocks.
107 #include <sys/types.h>
121 #include <zebrautl.h>
124 static int scan_areadef(MFile_area ma, const char *name, const char *ad)
127 * If no definition is given, use current directory, unlimited.
129 char dirname[FILENAME_MAX+1];
130 mf_dir **dp = &ma->dirs, *dir = *dp;
136 const char *ad0 = ad;
137 int i = 0, fact = 1, multi, size = 0;
139 while (*ad == ' ' || *ad == '\t')
145 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
147 if (i < FILENAME_MAX)
154 logf (LOG_FATAL, "Missing colon after path: %s", ad0);
159 logf (LOG_FATAL, "Empty path: %s", ad0);
162 while (*ad == ' ' || *ad == '\t')
172 if (*ad < '0' || *ad > '9')
174 logf (LOG_FATAL, "Missing size after path: %s", ad0);
178 while (*ad >= '0' && *ad <= '9')
179 size = size*10 + (*ad++ - '0');
182 case 'B': case 'b': multi = 1; break;
183 case 'K': case 'k': multi = 1024; break;
184 case 'M': case 'm': multi = 1048576; break;
186 logf (LOG_FATAL, "Missing unit: %s", ad0);
189 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
193 *dp = dir = xmalloc(sizeof(mf_dir));
195 strcpy(dir->name, dirname);
196 dir->max_bytes = dir->avail_bytes = fact * size * multi;
202 static int file_position(MFile mf, int pos, int offset)
204 int off = 0, c = mf->cur_file, ps;
206 if ((c > 0 && pos <= mf->files[c-1].top) ||
207 (c < mf->no_files -1 && pos > mf->files[c].top))
210 while (c + 1 < mf->no_files && mf->files[c].top < pos)
212 off += mf->files[c].blocks;
215 assert(c < mf->no_files);
218 off = c ? (mf->files[c-1].top + 1) : 0;
219 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
220 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
222 if (!mf->wr && errno == ENOENT && off == 0)
224 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
227 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
230 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
237 static int cmp_part_file(const void *p1, const void *p2)
239 return ((part_file *)p1)->number - ((part_file *)p2)->number;
243 * Create a new area, cotaining metafiles in directories.
244 * Find the part-files in each directory, and inventory the existing metafiles.
246 MFile_area mf_init(const char *name, const char *spec)
248 MFile_area ma = xmalloc(sizeof(*ma));
251 part_file *part_f = 0;
255 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
257 logf (LOG_DEBUG, "mf_init(%s)", name);
258 strcpy(ma->name, name);
261 if (scan_areadef(ma, name, spec) < 0)
263 logf (LOG_FATAL, "Failed to access description of '%s'", name);
266 /* look at each directory */
267 for (dirp = ma->dirs; dirp; dirp = dirp->next)
269 if (!(dd = opendir(dirp->name)))
271 logf (LOG_FATAL|LOG_ERRNO, "Failed to open %s", dirp->name);
274 /* look at each file */
275 while ((dent = readdir(dd)))
277 if (*dent->d_name == '.')
279 if (sscanf(dent->d_name, "%[^.].mf.%d", metaname, &number) != 2)
281 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
284 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
287 if (!strcmp(meta_f->name, metaname))
289 part_f = &meta_f->files[meta_f->no_files++];
296 meta_f = xmalloc(sizeof(*meta_f));
298 meta_f->next = ma->mfiles;
300 meta_f->cur_file = -1;
302 strcpy(meta_f->name, metaname);
303 part_f = &meta_f->files[0];
304 meta_f->no_files = 1;
306 part_f->number = number;
309 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
310 part_f->path = xstrdup(tmpnam);
312 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
314 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
318 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
320 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
325 if (dirp->max_bytes >= 0)
326 dirp->avail_bytes -= part_f->bytes;
330 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
332 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
334 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
340 void mf_destroy(MFile_area ma)
358 meta_file *m = meta_f;
360 for (i = 0; i<m->no_files; i++)
362 xfree (m->files[i].path);
364 meta_f = meta_f->next;
372 * If !ma, Use MF_DEFAULT_AREA.
374 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
376 struct meta_file *mnew;
378 char tmp[FILENAME_MAX+1];
381 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
382 wflag ? "RW" : "RDONLY");
384 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
385 if (!strcmp(name, mnew->name))
392 mnew = xmalloc(sizeof(*mnew));
393 strcpy(mnew->name, name);
394 /* allocate one, empty file */
396 mnew->files[0].bytes = mnew->files[0].blocks = 0;
397 mnew->files[0].top = -1;
398 mnew->files[0].number = 0;
399 mnew->files[0].fd = -1;
400 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
401 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
402 mnew->min_bytes_creat; dp = dp->next);
405 logf (LOG_FATAL, "Insufficient space for new mfile.");
408 mnew->files[0].dir = dp;
409 sprintf(tmp, "%s/%s.mf.%d", dp->name, mnew->name, 0);
410 mnew->files[0].path = xstrdup(tmp);
412 mnew->next = ma->mfiles;
417 for (i = 0; i < mnew->no_files; i++)
419 if (mnew->files[i].bytes % block_size)
420 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
422 mnew->files[i].blocks = mnew->files[i].bytes / block_size;
426 mnew->blocksize = block_size;
427 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
432 for (i = 0; i < mnew->no_files; i++)
434 mnew->files[i].blocks = mnew->files[i].bytes / mnew->blocksize;
435 if (i == mnew->no_files - 1)
436 mnew->files[i].top = -1;
439 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
440 : (mnew->files[i].blocks - 1);
448 int mf_close(MFile mf)
452 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
454 for (i = 0; i < mf->no_files; i++)
455 if (mf->files[i].fd >= 0)
457 close(mf->files[i].fd);
458 mf->files[i].fd = -1;
465 * Read one block from a metafile. Interface mirrors bfile.
467 int mf_read(MFile mf, int no, int offset, int num, void *buf)
471 if ((rd = file_position(mf, no, offset)) < 0)
476 toread = num ? num : mf->blocksize;
477 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
479 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
480 mf->files[mf->cur_file].path);
483 else if (rd < toread)
492 int mf_write(MFile mf, int no, int offset, int num, const void *buf)
494 int ps, nblocks, towrite;
496 char tmp[FILENAME_MAX+1];
497 unsigned char dummych = '\xff';
499 if ((ps = file_position(mf, no, offset)) < 0)
501 /* file needs to grow */
502 while (ps >= mf->files[mf->cur_file].blocks)
504 /* file overflow - allocate new file */
505 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
506 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
507 mf->files[mf->cur_file].dir->avail_bytes)
510 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
513 logf (LOG_DEBUG, "Capping off file %s at pos %d",
514 mf->files[mf->cur_file].path, nblocks);
515 if ((ps = file_position(mf,
516 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
517 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
519 logf (LOG_DEBUG, "ps = %d", ps);
520 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
522 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
525 mf->files[mf->cur_file].blocks += nblocks;
526 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
527 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
531 logf (LOG_DEBUG, "Creating new file.");
532 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
533 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
536 logf (LOG_FATAL, "Cannot allocate more space for %s",
540 mf->files[mf->cur_file].top = (mf->cur_file ?
541 mf->files[mf->cur_file-1].top : -1) +
542 mf->files[mf->cur_file].blocks;
543 mf->files[++(mf->cur_file)].top = -1;
544 mf->files[mf->cur_file].dir = dp;
545 mf->files[mf->cur_file].number =
546 mf->files[mf->cur_file-1].number + 1;
547 mf->files[mf->cur_file].blocks =
548 mf->files[mf->cur_file].bytes = 0;
549 mf->files[mf->cur_file].fd = -1;
550 sprintf(tmp, "%s/%s.mf.%d", dp->name, mf->name,
551 mf->files[mf->cur_file].number);
552 mf->files[mf->cur_file].path = xstrdup(tmp);
554 /* open new file and position at beginning */
555 if ((ps = file_position(mf, no, offset)) < 0)
560 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
561 mf->files[mf->cur_file].blocks += nblocks;
562 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
563 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
564 mf->files[mf->cur_file].dir->avail_bytes -=
565 nblocks * mf->blocksize;
568 towrite = num ? num : mf->blocksize;
569 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
571 logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
572 mf->name, mf->cur_file);
579 * Destroy a metafile, unlinking component files. File must be open.
581 int mf_unlink(MFile mf)
585 for (i = 0; i < mf->no_files; i++)
586 unlink (mf->files[i].path);
591 * Unlink the file by name, rather than MFile-handle. File should be closed.
593 int mf_unlink_name(MFile_area ma, const char *name)