2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.43 2002-04-04 20:50:36 adam
8 * Multi register works with record paths and data1 profile path
10 * Revision 1.42 2002/04/04 14:14:13 adam
11 * Multiple registers (alpha early)
13 * Revision 1.41 2000/11/29 14:24:01 adam
14 * Script configure uses yaz pthreads options. Added locking for
15 * zebra_register_{lock,unlock}.
17 * Revision 1.40 2000/10/17 12:37:09 adam
18 * Fixed notification of live-updates. Fixed minor problem with mf_init
19 * where it didn't handle shadow area file names correctly.
21 * Revision 1.39 2000/05/05 13:48:03 adam
22 * Fixed locking for metafiles.
24 * Revision 1.38 2000/03/20 19:08:35 adam
25 * Added remote record import using Z39.50 extended services and Segment
28 * Revision 1.37 2000/03/15 15:00:30 adam
29 * First work on threaded version.
31 * Revision 1.36 1999/12/08 15:03:11 adam
32 * Implemented bf_reset.
34 * Revision 1.35 1999/10/14 14:33:50 adam
35 * Added truncation 5=106.
37 * Revision 1.34 1999/05/26 07:49:12 adam
40 * Revision 1.33 1999/05/12 13:08:06 adam
41 * First version of ISAMS.
43 * Revision 1.32 1999/04/28 14:53:07 adam
44 * Fixed stupid bug regarding split-files.
46 * Revision 1.31 1999/02/18 12:49:33 adam
47 * Changed file naming scheme for register files as well as record
50 * Revision 1.30 1999/02/02 14:50:02 adam
51 * Updated WIN32 code specific sections. Changed header.
53 * Revision 1.29 1998/05/27 14:28:34 adam
54 * Fixed bug in mf_write. 'Cap off' byte written at wrong offset.
56 * Revision 1.28 1998/05/20 10:00:35 adam
57 * Fixed register spec so that colon isn't treated as size separator
58 * unless followed by [0-9+-] in order to allow DOS drive specifications.
60 * Revision 1.27 1998/02/10 11:55:07 adam
63 * Revision 1.26 1997/10/27 14:25:38 adam
66 * Revision 1.25 1997/09/18 08:59:16 adam
67 * Extra generic handle for the character mapping routines.
69 * Revision 1.24 1997/09/17 12:19:06 adam
70 * Zebra version corresponds to YAZ version 1.4.
71 * Changed Zebra server so that it doesn't depend on global common_resource.
73 * Revision 1.23 1997/09/09 13:37:53 adam
74 * Partial port to WIN95/NT.
76 * Revision 1.22 1997/09/04 13:56:39 adam
77 * Added O_BINARY to open calls.
79 * Revision 1.21 1996/10/29 13:56:18 adam
80 * Include of zebrautl.h instead of alexutil.h.
82 * Revision 1.20 1996/05/14 12:10:16 quinn
85 * Revision 1.19 1996/05/01 07:16:30 quinn
88 * Revision 1.18 1996/04/09 06:47:30 adam
89 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
91 * Revision 1.17 1996/03/20 13:29:11 quinn
94 * Revision 1.16 1995/12/12 15:57:57 adam
95 * Implemented mf_unlink. cf_unlink uses mf_unlink.
97 * Revision 1.15 1995/12/08 16:21:14 adam
98 * Work on commit/update.
100 * Revision 1.14 1995/12/05 13:12:37 quinn
103 * Revision 1.13 1995/11/30 17:00:50 adam
104 * Several bug fixes. Commit system runs now.
106 * Revision 1.12 1995/11/24 17:26:11 quinn
107 * Mostly about making some ISAM stuff in the config file optional.
109 * Revision 1.11 1995/11/13 09:32:43 quinn
112 * Revision 1.10 1995/09/04 12:33:22 adam
113 * Various cleanup. YAZ util used instead.
115 * Revision 1.9 1994/11/04 14:26:39 quinn
118 * Revision 1.8 1994/10/05 16:56:42 quinn
121 * Revision 1.7 1994/09/19 14:12:37 quinn
124 * Revision 1.6 1994/09/14 13:10:15 quinn
125 * Corrected some bugs in the init-phase
127 * Revision 1.5 1994/09/12 08:01:51 quinn
130 * Revision 1.4 1994/09/01 14:51:07 quinn
131 * Allowed mf_write to write beyond eof+1.
133 * Revision 1.3 1994/08/24 09:37:17 quinn
134 * Changed reaction to read return values.
136 * Revision 1.2 1994/08/23 14:50:48 quinn
139 * Revision 1.1 1994/08/23 14:41:33 quinn
140 * First functional version.
146 * TODO: The size estimates in init may not be accurate due to
147 * only partially written final blocks.
150 #include <sys/types.h>
164 #include <zebra-lock.h>
165 #include <zebrautl.h>
168 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
171 * If no definition is given, use current directory, unlimited.
173 char dirname[FILENAME_MAX+1];
174 mf_dir **dp = &ma->dirs, *dir = *dp;
180 const char *ad0 = ad;
181 int i = 0, fact = 1, multi;
184 while (*ad == ' ' || *ad == '\t')
188 if (!yaz_is_abspath(ad) && base)
190 strcpy (dirname, base);
196 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
198 if (i < FILENAME_MAX)
205 logf (LOG_WARN, "Missing colon after path: %s", ad0);
210 logf (LOG_WARN, "Empty path: %s", ad0);
213 while (*ad == ' ' || *ad == '\t')
223 if (*ad < '0' || *ad > '9')
225 logf (LOG_FATAL, "Missing size after path: %s", ad0);
229 while (*ad >= '0' && *ad <= '9')
230 size = size*10 + (*ad++ - '0');
233 case 'B': case 'b': multi = 1; break;
234 case 'K': case 'k': multi = 1024; break;
235 case 'M': case 'm': multi = 1048576; break;
236 case 'G': case 'g': multi = 1073741824; break;
238 logf (LOG_FATAL, "Missing unit: %s", ad0);
241 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
245 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
247 strcpy(dir->name, dirname);
248 dir->max_bytes = dir->avail_bytes = fact * size * multi;
254 static int file_position(MFile mf, int pos, int offset)
256 int off = 0, c = mf->cur_file, ps;
258 if ((c > 0 && pos <= mf->files[c-1].top) ||
259 (c < mf->no_files -1 && pos > mf->files[c].top))
262 while (c + 1 < mf->no_files && mf->files[c].top < pos)
264 off += mf->files[c].blocks;
267 assert(c < mf->no_files);
270 off = c ? (mf->files[c-1].top + 1) : 0;
271 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
272 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
274 if (!mf->wr && errno == ENOENT && off == 0)
276 logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
279 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
282 logf (LOG_WARN|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
289 static int cmp_part_file(const void *p1, const void *p2)
291 return ((part_file *)p1)->number - ((part_file *)p2)->number;
295 * Create a new area, cotaining metafiles in directories.
296 * Find the part-files in each directory, and inventory the existing metafiles.
298 MFile_area mf_init(const char *name, const char *spec, const char *base)
300 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
303 part_file *part_f = 0;
307 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
309 logf (LOG_DEBUG, "mf_init(%s)", name);
310 strcpy(ma->name, name);
313 if (scan_areadef(ma, spec, base) < 0)
315 logf (LOG_WARN, "Failed to access description of '%s'", name);
318 /* look at each directory */
319 for (dirp = ma->dirs; dirp; dirp = dirp->next)
321 if (!(dd = opendir(dirp->name)))
323 logf (LOG_WARN|LOG_ERRNO, "Failed to open directory %s",
327 /* look at each file */
328 while ((dent = readdir(dd)))
330 int len = strlen(dent->d_name);
331 const char *cp = strrchr (dent->d_name, '-');
332 if (strchr (".-", *dent->d_name))
334 if (len < 5 || !cp || strcmp (dent->d_name + len - 3, ".mf"))
337 memcpy (metaname, dent->d_name, cp - dent->d_name);
338 metaname[ cp - dent->d_name] = '\0';
340 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
343 if (!strcmp(meta_f->name, metaname))
345 part_f = &meta_f->files[meta_f->no_files++];
352 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
353 zebra_mutex_init (&meta_f->mutex);
355 meta_f->next = ma->mfiles;
357 meta_f->cur_file = -1;
359 strcpy(meta_f->name, metaname);
360 part_f = &meta_f->files[0];
361 meta_f->no_files = 1;
363 part_f->number = number;
366 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
367 part_f->path = xstrdup(tmpnam);
369 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
371 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
375 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
377 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
382 if (dirp->max_bytes >= 0)
383 dirp->avail_bytes -= part_f->bytes;
387 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
389 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
391 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
397 void mf_destroy(MFile_area ma)
415 meta_file *m = meta_f;
417 for (i = 0; i<m->no_files; i++)
419 xfree (m->files[i].path);
421 zebra_mutex_destroy (&meta_f->mutex);
422 meta_f = meta_f->next;
428 void mf_reset(MFile_area ma)
438 meta_file *m = meta_f;
441 for (i = 0; i<m->no_files; i++)
443 unlink (m->files[i].path);
444 xfree (m->files[i].path);
446 meta_f = meta_f->next;
454 * If !ma, Use MF_DEFAULT_AREA.
456 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
460 char tmp[FILENAME_MAX+1];
463 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
464 wflag ? "RW" : "RDONLY");
466 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
467 if (!strcmp(name, mnew->name))
476 mnew = (meta_file *) xmalloc(sizeof(*mnew));
477 strcpy(mnew->name, name);
478 /* allocate one, empty file */
479 zebra_mutex_init (&mnew->mutex);
481 mnew->files[0].bytes = 0;
482 mnew->files[0].blocks = 0;
483 mnew->files[0].top = -1;
484 mnew->files[0].number = 0;
485 mnew->files[0].fd = -1;
486 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
487 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
488 mnew->min_bytes_creat; dp = dp->next);
491 logf (LOG_FATAL, "Insufficient space for new mfile.");
494 mnew->files[0].dir = dp;
495 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
496 mnew->files[0].path = xstrdup(tmp);
498 mnew->next = ma->mfiles;
503 for (i = 0; i < mnew->no_files; i++)
505 if (mnew->files[i].bytes % block_size)
506 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
508 mnew->files[i].blocks = mnew->files[i].bytes / block_size;
512 mnew->blocksize = block_size;
513 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
518 for (i = 0; i < mnew->no_files; i++)
520 mnew->files[i].blocks = mnew->files[i].bytes / mnew->blocksize;
521 if (i == mnew->no_files - 1)
522 mnew->files[i].top = -1;
525 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
526 : (mnew->files[i].blocks - 1);
534 int mf_close(MFile mf)
538 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
540 for (i = 0; i < mf->no_files; i++)
541 if (mf->files[i].fd >= 0)
543 close(mf->files[i].fd);
544 mf->files[i].fd = -1;
551 * Read one block from a metafile. Interface mirrors bfile.
553 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
557 zebra_mutex_lock (&mf->mutex);
558 if ((rd = file_position(mf, no, offset)) < 0)
562 zebra_mutex_unlock (&mf->mutex);
568 toread = nbytes ? nbytes : mf->blocksize;
569 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
571 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
572 mf->files[mf->cur_file].path);
575 zebra_mutex_unlock (&mf->mutex);
585 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
587 int ps, nblocks, towrite;
589 char tmp[FILENAME_MAX+1];
590 unsigned char dummych = '\xff';
592 zebra_mutex_lock (&mf->mutex);
593 if ((ps = file_position(mf, no, offset)) < 0)
595 /* file needs to grow */
596 while (ps >= mf->files[mf->cur_file].blocks)
598 /* file overflow - allocate new file */
599 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
600 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
601 mf->files[mf->cur_file].dir->avail_bytes)
604 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
607 logf (LOG_DEBUG, "Capping off file %s at pos %d",
608 mf->files[mf->cur_file].path, nblocks);
609 if ((ps = file_position(mf,
610 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
611 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
613 logf (LOG_DEBUG, "ps = %d", ps);
614 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
616 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
619 mf->files[mf->cur_file].blocks += nblocks;
620 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
621 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
625 logf (LOG_DEBUG, "Creating new file.");
626 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
627 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
630 logf (LOG_FATAL, "Cannot allocate more space for %s",
634 mf->files[mf->cur_file].top = (mf->cur_file ?
635 mf->files[mf->cur_file-1].top : -1) +
636 mf->files[mf->cur_file].blocks;
637 mf->files[++(mf->cur_file)].top = -1;
638 mf->files[mf->cur_file].dir = dp;
639 mf->files[mf->cur_file].number =
640 mf->files[mf->cur_file-1].number + 1;
641 mf->files[mf->cur_file].blocks =
642 mf->files[mf->cur_file].bytes = 0;
643 mf->files[mf->cur_file].fd = -1;
644 sprintf(tmp, "%s/%s-%d.mf", dp->name, mf->name,
645 mf->files[mf->cur_file].number);
646 mf->files[mf->cur_file].path = xstrdup(tmp);
648 /* open new file and position at beginning */
649 if ((ps = file_position(mf, no, offset)) < 0)
654 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
655 mf->files[mf->cur_file].blocks += nblocks;
656 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
657 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
658 mf->files[mf->cur_file].dir->avail_bytes -=
659 nblocks * mf->blocksize;
662 towrite = nbytes ? nbytes : mf->blocksize;
663 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
665 logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
666 mf->name, mf->cur_file);
669 zebra_mutex_unlock (&mf->mutex);
674 * Destroy a metafile, unlinking component files. File must be open.
676 int mf_unlink(MFile mf)
680 for (i = 0; i < mf->no_files; i++)
681 unlink (mf->files[i].path);
686 * Unlink the file by name, rather than MFile-handle. File should be closed.
688 int mf_unlink_name(MFile_area ma, const char *name)