2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.42 2002-04-04 14:14:13 adam
8 * Multiple registers (alpha early)
10 * Revision 1.41 2000/11/29 14:24:01 adam
11 * Script configure uses yaz pthreads options. Added locking for
12 * zebra_register_{lock,unlock}.
14 * Revision 1.40 2000/10/17 12:37:09 adam
15 * Fixed notification of live-updates. Fixed minor problem with mf_init
16 * where it didn't handle shadow area file names correctly.
18 * Revision 1.39 2000/05/05 13:48:03 adam
19 * Fixed locking for metafiles.
21 * Revision 1.38 2000/03/20 19:08:35 adam
22 * Added remote record import using Z39.50 extended services and Segment
25 * Revision 1.37 2000/03/15 15:00:30 adam
26 * First work on threaded version.
28 * Revision 1.36 1999/12/08 15:03:11 adam
29 * Implemented bf_reset.
31 * Revision 1.35 1999/10/14 14:33:50 adam
32 * Added truncation 5=106.
34 * Revision 1.34 1999/05/26 07:49:12 adam
37 * Revision 1.33 1999/05/12 13:08:06 adam
38 * First version of ISAMS.
40 * Revision 1.32 1999/04/28 14:53:07 adam
41 * Fixed stupid bug regarding split-files.
43 * Revision 1.31 1999/02/18 12:49:33 adam
44 * Changed file naming scheme for register files as well as record
47 * Revision 1.30 1999/02/02 14:50:02 adam
48 * Updated WIN32 code specific sections. Changed header.
50 * Revision 1.29 1998/05/27 14:28:34 adam
51 * Fixed bug in mf_write. 'Cap off' byte written at wrong offset.
53 * Revision 1.28 1998/05/20 10:00:35 adam
54 * Fixed register spec so that colon isn't treated as size separator
55 * unless followed by [0-9+-] in order to allow DOS drive specifications.
57 * Revision 1.27 1998/02/10 11:55:07 adam
60 * Revision 1.26 1997/10/27 14:25:38 adam
63 * Revision 1.25 1997/09/18 08:59:16 adam
64 * Extra generic handle for the character mapping routines.
66 * Revision 1.24 1997/09/17 12:19:06 adam
67 * Zebra version corresponds to YAZ version 1.4.
68 * Changed Zebra server so that it doesn't depend on global common_resource.
70 * Revision 1.23 1997/09/09 13:37:53 adam
71 * Partial port to WIN95/NT.
73 * Revision 1.22 1997/09/04 13:56:39 adam
74 * Added O_BINARY to open calls.
76 * Revision 1.21 1996/10/29 13:56:18 adam
77 * Include of zebrautl.h instead of alexutil.h.
79 * Revision 1.20 1996/05/14 12:10:16 quinn
82 * Revision 1.19 1996/05/01 07:16:30 quinn
85 * Revision 1.18 1996/04/09 06:47:30 adam
86 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
88 * Revision 1.17 1996/03/20 13:29:11 quinn
91 * Revision 1.16 1995/12/12 15:57:57 adam
92 * Implemented mf_unlink. cf_unlink uses mf_unlink.
94 * Revision 1.15 1995/12/08 16:21:14 adam
95 * Work on commit/update.
97 * Revision 1.14 1995/12/05 13:12:37 quinn
100 * Revision 1.13 1995/11/30 17:00:50 adam
101 * Several bug fixes. Commit system runs now.
103 * Revision 1.12 1995/11/24 17:26:11 quinn
104 * Mostly about making some ISAM stuff in the config file optional.
106 * Revision 1.11 1995/11/13 09:32:43 quinn
109 * Revision 1.10 1995/09/04 12:33:22 adam
110 * Various cleanup. YAZ util used instead.
112 * Revision 1.9 1994/11/04 14:26:39 quinn
115 * Revision 1.8 1994/10/05 16:56:42 quinn
118 * Revision 1.7 1994/09/19 14:12:37 quinn
121 * Revision 1.6 1994/09/14 13:10:15 quinn
122 * Corrected some bugs in the init-phase
124 * Revision 1.5 1994/09/12 08:01:51 quinn
127 * Revision 1.4 1994/09/01 14:51:07 quinn
128 * Allowed mf_write to write beyond eof+1.
130 * Revision 1.3 1994/08/24 09:37:17 quinn
131 * Changed reaction to read return values.
133 * Revision 1.2 1994/08/23 14:50:48 quinn
136 * Revision 1.1 1994/08/23 14:41:33 quinn
137 * First functional version.
143 * TODO: The size estimates in init may not be accurate due to
144 * only partially written final blocks.
147 #include <sys/types.h>
161 #include <zebra-lock.h>
162 #include <zebrautl.h>
165 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
168 * If no definition is given, use current directory, unlimited.
170 char dirname[FILENAME_MAX+1];
171 mf_dir **dp = &ma->dirs, *dir = *dp;
177 const char *ad0 = ad;
178 int i = 0, fact = 1, multi;
181 while (*ad == ' ' || *ad == '\t')
185 if (!zebra_is_abspath(ad) && base)
187 strcpy (dirname, base);
193 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
195 if (i < FILENAME_MAX)
202 logf (LOG_WARN, "Missing colon after path: %s", ad0);
207 logf (LOG_WARN, "Empty path: %s", ad0);
210 while (*ad == ' ' || *ad == '\t')
220 if (*ad < '0' || *ad > '9')
222 logf (LOG_FATAL, "Missing size after path: %s", ad0);
226 while (*ad >= '0' && *ad <= '9')
227 size = size*10 + (*ad++ - '0');
230 case 'B': case 'b': multi = 1; break;
231 case 'K': case 'k': multi = 1024; break;
232 case 'M': case 'm': multi = 1048576; break;
233 case 'G': case 'g': multi = 1073741824; break;
235 logf (LOG_FATAL, "Missing unit: %s", ad0);
238 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
242 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
244 strcpy(dir->name, dirname);
245 dir->max_bytes = dir->avail_bytes = fact * size * multi;
251 static int file_position(MFile mf, int pos, int offset)
253 int off = 0, c = mf->cur_file, ps;
255 if ((c > 0 && pos <= mf->files[c-1].top) ||
256 (c < mf->no_files -1 && pos > mf->files[c].top))
259 while (c + 1 < mf->no_files && mf->files[c].top < pos)
261 off += mf->files[c].blocks;
264 assert(c < mf->no_files);
267 off = c ? (mf->files[c-1].top + 1) : 0;
268 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
269 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
271 if (!mf->wr && errno == ENOENT && off == 0)
273 logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
276 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
279 logf (LOG_WARN|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
286 static int cmp_part_file(const void *p1, const void *p2)
288 return ((part_file *)p1)->number - ((part_file *)p2)->number;
292 * Create a new area, cotaining metafiles in directories.
293 * Find the part-files in each directory, and inventory the existing metafiles.
295 MFile_area mf_init(const char *name, const char *spec, const char *base)
297 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
300 part_file *part_f = 0;
304 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
306 logf (LOG_DEBUG, "mf_init(%s)", name);
307 strcpy(ma->name, name);
310 if (scan_areadef(ma, spec, base) < 0)
312 logf (LOG_WARN, "Failed to access description of '%s'", name);
315 /* look at each directory */
316 for (dirp = ma->dirs; dirp; dirp = dirp->next)
318 if (!(dd = opendir(dirp->name)))
320 logf (LOG_WARN|LOG_ERRNO, "Failed to open directory %s",
324 /* look at each file */
325 while ((dent = readdir(dd)))
327 int len = strlen(dent->d_name);
328 const char *cp = strrchr (dent->d_name, '-');
329 if (strchr (".-", *dent->d_name))
331 if (len < 5 || !cp || strcmp (dent->d_name + len - 3, ".mf"))
334 memcpy (metaname, dent->d_name, cp - dent->d_name);
335 metaname[ cp - dent->d_name] = '\0';
337 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
340 if (!strcmp(meta_f->name, metaname))
342 part_f = &meta_f->files[meta_f->no_files++];
349 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
350 zebra_mutex_init (&meta_f->mutex);
352 meta_f->next = ma->mfiles;
354 meta_f->cur_file = -1;
356 strcpy(meta_f->name, metaname);
357 part_f = &meta_f->files[0];
358 meta_f->no_files = 1;
360 part_f->number = number;
363 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
364 part_f->path = xstrdup(tmpnam);
366 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
368 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
372 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
374 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
379 if (dirp->max_bytes >= 0)
380 dirp->avail_bytes -= part_f->bytes;
384 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
386 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
388 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
394 void mf_destroy(MFile_area ma)
412 meta_file *m = meta_f;
414 for (i = 0; i<m->no_files; i++)
416 xfree (m->files[i].path);
418 zebra_mutex_destroy (&meta_f->mutex);
419 meta_f = meta_f->next;
425 void mf_reset(MFile_area ma)
435 meta_file *m = meta_f;
438 for (i = 0; i<m->no_files; i++)
440 unlink (m->files[i].path);
441 xfree (m->files[i].path);
443 meta_f = meta_f->next;
451 * If !ma, Use MF_DEFAULT_AREA.
453 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
457 char tmp[FILENAME_MAX+1];
460 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
461 wflag ? "RW" : "RDONLY");
463 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
464 if (!strcmp(name, mnew->name))
473 mnew = (meta_file *) xmalloc(sizeof(*mnew));
474 strcpy(mnew->name, name);
475 /* allocate one, empty file */
476 zebra_mutex_init (&mnew->mutex);
478 mnew->files[0].bytes = 0;
479 mnew->files[0].blocks = 0;
480 mnew->files[0].top = -1;
481 mnew->files[0].number = 0;
482 mnew->files[0].fd = -1;
483 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
484 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
485 mnew->min_bytes_creat; dp = dp->next);
488 logf (LOG_FATAL, "Insufficient space for new mfile.");
491 mnew->files[0].dir = dp;
492 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
493 mnew->files[0].path = xstrdup(tmp);
495 mnew->next = ma->mfiles;
500 for (i = 0; i < mnew->no_files; i++)
502 if (mnew->files[i].bytes % block_size)
503 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
505 mnew->files[i].blocks = mnew->files[i].bytes / block_size;
509 mnew->blocksize = block_size;
510 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
515 for (i = 0; i < mnew->no_files; i++)
517 mnew->files[i].blocks = mnew->files[i].bytes / mnew->blocksize;
518 if (i == mnew->no_files - 1)
519 mnew->files[i].top = -1;
522 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
523 : (mnew->files[i].blocks - 1);
531 int mf_close(MFile mf)
535 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
537 for (i = 0; i < mf->no_files; i++)
538 if (mf->files[i].fd >= 0)
540 close(mf->files[i].fd);
541 mf->files[i].fd = -1;
548 * Read one block from a metafile. Interface mirrors bfile.
550 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
554 zebra_mutex_lock (&mf->mutex);
555 if ((rd = file_position(mf, no, offset)) < 0)
559 zebra_mutex_unlock (&mf->mutex);
565 toread = nbytes ? nbytes : mf->blocksize;
566 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
568 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
569 mf->files[mf->cur_file].path);
572 zebra_mutex_unlock (&mf->mutex);
582 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
584 int ps, nblocks, towrite;
586 char tmp[FILENAME_MAX+1];
587 unsigned char dummych = '\xff';
589 zebra_mutex_lock (&mf->mutex);
590 if ((ps = file_position(mf, no, offset)) < 0)
592 /* file needs to grow */
593 while (ps >= mf->files[mf->cur_file].blocks)
595 /* file overflow - allocate new file */
596 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
597 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
598 mf->files[mf->cur_file].dir->avail_bytes)
601 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
604 logf (LOG_DEBUG, "Capping off file %s at pos %d",
605 mf->files[mf->cur_file].path, nblocks);
606 if ((ps = file_position(mf,
607 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
608 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
610 logf (LOG_DEBUG, "ps = %d", ps);
611 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
613 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
616 mf->files[mf->cur_file].blocks += nblocks;
617 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
618 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
622 logf (LOG_DEBUG, "Creating new file.");
623 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
624 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
627 logf (LOG_FATAL, "Cannot allocate more space for %s",
631 mf->files[mf->cur_file].top = (mf->cur_file ?
632 mf->files[mf->cur_file-1].top : -1) +
633 mf->files[mf->cur_file].blocks;
634 mf->files[++(mf->cur_file)].top = -1;
635 mf->files[mf->cur_file].dir = dp;
636 mf->files[mf->cur_file].number =
637 mf->files[mf->cur_file-1].number + 1;
638 mf->files[mf->cur_file].blocks =
639 mf->files[mf->cur_file].bytes = 0;
640 mf->files[mf->cur_file].fd = -1;
641 sprintf(tmp, "%s/%s-%d.mf", dp->name, mf->name,
642 mf->files[mf->cur_file].number);
643 mf->files[mf->cur_file].path = xstrdup(tmp);
645 /* open new file and position at beginning */
646 if ((ps = file_position(mf, no, offset)) < 0)
651 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
652 mf->files[mf->cur_file].blocks += nblocks;
653 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
654 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
655 mf->files[mf->cur_file].dir->avail_bytes -=
656 nblocks * mf->blocksize;
659 towrite = nbytes ? nbytes : mf->blocksize;
660 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
662 logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
663 mf->name, mf->cur_file);
666 zebra_mutex_unlock (&mf->mutex);
671 * Destroy a metafile, unlinking component files. File must be open.
673 int mf_unlink(MFile mf)
677 for (i = 0; i < mf->no_files; i++)
678 unlink (mf->files[i].path);
683 * Unlink the file by name, rather than MFile-handle. File should be closed.
685 int mf_unlink_name(MFile_area ma, const char *name)