2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.38 2000-03-20 19:08:35 adam
8 * Added remote record import using Z39.50 extended services and Segment
11 * Revision 1.37 2000/03/15 15:00:30 adam
12 * First work on threaded version.
14 * Revision 1.36 1999/12/08 15:03:11 adam
15 * Implemented bf_reset.
17 * Revision 1.35 1999/10/14 14:33:50 adam
18 * Added truncation 5=106.
20 * Revision 1.34 1999/05/26 07:49:12 adam
23 * Revision 1.33 1999/05/12 13:08:06 adam
24 * First version of ISAMS.
26 * Revision 1.32 1999/04/28 14:53:07 adam
27 * Fixed stupid bug regarding split-files.
29 * Revision 1.31 1999/02/18 12:49:33 adam
30 * Changed file naming scheme for register files as well as record
33 * Revision 1.30 1999/02/02 14:50:02 adam
34 * Updated WIN32 code specific sections. Changed header.
36 * Revision 1.29 1998/05/27 14:28:34 adam
37 * Fixed bug in mf_write. 'Cap off' byte written at wrong offset.
39 * Revision 1.28 1998/05/20 10:00:35 adam
40 * Fixed register spec so that colon isn't treated as size separator
41 * unless followed by [0-9+-] in order to allow DOS drive specifications.
43 * Revision 1.27 1998/02/10 11:55:07 adam
46 * Revision 1.26 1997/10/27 14:25:38 adam
49 * Revision 1.25 1997/09/18 08:59:16 adam
50 * Extra generic handle for the character mapping routines.
52 * Revision 1.24 1997/09/17 12:19:06 adam
53 * Zebra version corresponds to YAZ version 1.4.
54 * Changed Zebra server so that it doesn't depend on global common_resource.
56 * Revision 1.23 1997/09/09 13:37:53 adam
57 * Partial port to WIN95/NT.
59 * Revision 1.22 1997/09/04 13:56:39 adam
60 * Added O_BINARY to open calls.
62 * Revision 1.21 1996/10/29 13:56:18 adam
63 * Include of zebrautl.h instead of alexutil.h.
65 * Revision 1.20 1996/05/14 12:10:16 quinn
68 * Revision 1.19 1996/05/01 07:16:30 quinn
71 * Revision 1.18 1996/04/09 06:47:30 adam
72 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
74 * Revision 1.17 1996/03/20 13:29:11 quinn
77 * Revision 1.16 1995/12/12 15:57:57 adam
78 * Implemented mf_unlink. cf_unlink uses mf_unlink.
80 * Revision 1.15 1995/12/08 16:21:14 adam
81 * Work on commit/update.
83 * Revision 1.14 1995/12/05 13:12:37 quinn
86 * Revision 1.13 1995/11/30 17:00:50 adam
87 * Several bug fixes. Commit system runs now.
89 * Revision 1.12 1995/11/24 17:26:11 quinn
90 * Mostly about making some ISAM stuff in the config file optional.
92 * Revision 1.11 1995/11/13 09:32:43 quinn
95 * Revision 1.10 1995/09/04 12:33:22 adam
96 * Various cleanup. YAZ util used instead.
98 * Revision 1.9 1994/11/04 14:26:39 quinn
101 * Revision 1.8 1994/10/05 16:56:42 quinn
104 * Revision 1.7 1994/09/19 14:12:37 quinn
107 * Revision 1.6 1994/09/14 13:10:15 quinn
108 * Corrected some bugs in the init-phase
110 * Revision 1.5 1994/09/12 08:01:51 quinn
113 * Revision 1.4 1994/09/01 14:51:07 quinn
114 * Allowed mf_write to write beyond eof+1.
116 * Revision 1.3 1994/08/24 09:37:17 quinn
117 * Changed reaction to read return values.
119 * Revision 1.2 1994/08/23 14:50:48 quinn
122 * Revision 1.1 1994/08/23 14:41:33 quinn
123 * First functional version.
129 * TODO: The size estimates in init may not be accurate due to
130 * only partially written final blocks.
133 #include <sys/types.h>
147 #include <zebra-lock.h>
148 #include <zebrautl.h>
151 static int scan_areadef(MFile_area ma, const char *name, const char *ad)
154 * If no definition is given, use current directory, unlimited.
156 char dirname[FILENAME_MAX+1];
157 mf_dir **dp = &ma->dirs, *dir = *dp;
163 const char *ad0 = ad;
164 int i = 0, fact = 1, multi;
167 while (*ad == ' ' || *ad == '\t')
173 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
175 if (i < FILENAME_MAX)
182 logf (LOG_WARN, "Missing colon after path: %s", ad0);
187 logf (LOG_WARN, "Empty path: %s", ad0);
190 while (*ad == ' ' || *ad == '\t')
200 if (*ad < '0' || *ad > '9')
202 logf (LOG_FATAL, "Missing size after path: %s", ad0);
206 while (*ad >= '0' && *ad <= '9')
207 size = size*10 + (*ad++ - '0');
210 case 'B': case 'b': multi = 1; break;
211 case 'K': case 'k': multi = 1024; break;
212 case 'M': case 'm': multi = 1048576; break;
213 case 'G': case 'g': multi = 1073741824; break;
215 logf (LOG_FATAL, "Missing unit: %s", ad0);
218 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
222 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
224 strcpy(dir->name, dirname);
225 dir->max_bytes = dir->avail_bytes = fact * size * multi;
231 static int file_position(MFile mf, int pos, int offset)
233 int off = 0, c = mf->cur_file, ps;
235 if ((c > 0 && pos <= mf->files[c-1].top) ||
236 (c < mf->no_files -1 && pos > mf->files[c].top))
239 while (c + 1 < mf->no_files && mf->files[c].top < pos)
241 off += mf->files[c].blocks;
244 assert(c < mf->no_files);
247 off = c ? (mf->files[c-1].top + 1) : 0;
248 if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
249 mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
251 if (!mf->wr && errno == ENOENT && off == 0)
253 logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
256 if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
259 logf (LOG_WARN|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
266 static int cmp_part_file(const void *p1, const void *p2)
268 return ((part_file *)p1)->number - ((part_file *)p2)->number;
272 * Create a new area, cotaining metafiles in directories.
273 * Find the part-files in each directory, and inventory the existing metafiles.
275 MFile_area mf_init(const char *name, const char *spec)
277 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
280 part_file *part_f = 0;
284 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
286 logf (LOG_DEBUG, "mf_init(%s)", name);
287 strcpy(ma->name, name);
290 if (scan_areadef(ma, name, spec) < 0)
292 logf (LOG_WARN, "Failed to access description of '%s'", name);
295 /* look at each directory */
296 for (dirp = ma->dirs; dirp; dirp = dirp->next)
298 if (!(dd = opendir(dirp->name)))
300 logf (LOG_WARN|LOG_ERRNO, "Failed to open directory %s",
304 /* look at each file */
305 while ((dent = readdir(dd)))
307 if (*dent->d_name == '.')
309 if (sscanf(dent->d_name, "%[^-]-%d.mf", metaname, &number) != 2)
311 logf (LOG_DEBUG, "bf: %s is not a part-file.", dent->d_name);
314 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
317 if (!strcmp(meta_f->name, metaname))
319 part_f = &meta_f->files[meta_f->no_files++];
326 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
328 meta_f->next = ma->mfiles;
330 meta_f->cur_file = -1;
332 strcpy(meta_f->name, metaname);
333 part_f = &meta_f->files[0];
334 meta_f->no_files = 1;
336 part_f->number = number;
339 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
340 part_f->path = xstrdup(tmpnam);
342 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
344 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
348 if ((part_f->bytes = lseek(fd, 0, SEEK_END)) < 0)
350 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
355 if (dirp->max_bytes >= 0)
356 dirp->avail_bytes -= part_f->bytes;
360 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
362 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
364 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
370 void mf_destroy(MFile_area ma)
388 meta_file *m = meta_f;
390 for (i = 0; i<m->no_files; i++)
392 xfree (m->files[i].path);
394 zebra_mutex_destroy (&meta_f->mutex);
395 meta_f = meta_f->next;
401 void mf_reset(MFile_area ma)
411 meta_file *m = meta_f;
414 for (i = 0; i<m->no_files; i++)
416 unlink (m->files[i].path);
417 xfree (m->files[i].path);
419 meta_f = meta_f->next;
427 * If !ma, Use MF_DEFAULT_AREA.
429 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
433 char tmp[FILENAME_MAX+1];
436 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
437 wflag ? "RW" : "RDONLY");
439 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
440 if (!strcmp(name, mnew->name))
449 mnew = (meta_file *) xmalloc(sizeof(*mnew));
450 strcpy(mnew->name, name);
451 /* allocate one, empty file */
452 zebra_mutex_init (&mnew->mutex);
454 mnew->files[0].bytes = 0;
455 mnew->files[0].blocks = 0;
456 mnew->files[0].top = -1;
457 mnew->files[0].number = 0;
458 mnew->files[0].fd = -1;
459 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
460 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
461 mnew->min_bytes_creat; dp = dp->next);
464 logf (LOG_FATAL, "Insufficient space for new mfile.");
467 mnew->files[0].dir = dp;
468 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
469 mnew->files[0].path = xstrdup(tmp);
471 mnew->next = ma->mfiles;
476 for (i = 0; i < mnew->no_files; i++)
478 if (mnew->files[i].bytes % block_size)
479 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
481 mnew->files[i].blocks = mnew->files[i].bytes / block_size;
485 mnew->blocksize = block_size;
486 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
491 for (i = 0; i < mnew->no_files; i++)
493 mnew->files[i].blocks = mnew->files[i].bytes / mnew->blocksize;
494 if (i == mnew->no_files - 1)
495 mnew->files[i].top = -1;
498 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
499 : (mnew->files[i].blocks - 1);
507 int mf_close(MFile mf)
511 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
513 for (i = 0; i < mf->no_files; i++)
514 if (mf->files[i].fd >= 0)
516 close(mf->files[i].fd);
517 mf->files[i].fd = -1;
524 * Read one block from a metafile. Interface mirrors bfile.
526 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
530 zebra_mutex_lock (&mf->mutex);
531 if ((rd = file_position(mf, no, offset)) < 0)
535 zebra_mutex_unlock (&mf->mutex);
541 toread = nbytes ? nbytes : mf->blocksize;
542 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
544 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
545 mf->files[mf->cur_file].path);
548 zebra_mutex_unlock (&mf->mutex);
558 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
560 int ps, nblocks, towrite;
562 char tmp[FILENAME_MAX+1];
563 unsigned char dummych = '\xff';
565 zebra_mutex_lock (&mf->mutex);
566 if ((ps = file_position(mf, no, offset)) < 0)
568 /* file needs to grow */
569 while (ps >= mf->files[mf->cur_file].blocks)
571 /* file overflow - allocate new file */
572 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
573 (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
574 mf->files[mf->cur_file].dir->avail_bytes)
577 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
580 logf (LOG_DEBUG, "Capping off file %s at pos %d",
581 mf->files[mf->cur_file].path, nblocks);
582 if ((ps = file_position(mf,
583 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
584 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
586 logf (LOG_DEBUG, "ps = %d", ps);
587 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
589 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
592 mf->files[mf->cur_file].blocks += nblocks;
593 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
594 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
598 logf (LOG_DEBUG, "Creating new file.");
599 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
600 dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
603 logf (LOG_FATAL, "Cannot allocate more space for %s",
607 mf->files[mf->cur_file].top = (mf->cur_file ?
608 mf->files[mf->cur_file-1].top : -1) +
609 mf->files[mf->cur_file].blocks;
610 mf->files[++(mf->cur_file)].top = -1;
611 mf->files[mf->cur_file].dir = dp;
612 mf->files[mf->cur_file].number =
613 mf->files[mf->cur_file-1].number + 1;
614 mf->files[mf->cur_file].blocks =
615 mf->files[mf->cur_file].bytes = 0;
616 mf->files[mf->cur_file].fd = -1;
617 sprintf(tmp, "%s/%s-%d.mf", dp->name, mf->name,
618 mf->files[mf->cur_file].number);
619 mf->files[mf->cur_file].path = xstrdup(tmp);
621 /* open new file and position at beginning */
622 if ((ps = file_position(mf, no, offset)) < 0)
627 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
628 mf->files[mf->cur_file].blocks += nblocks;
629 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
630 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
631 mf->files[mf->cur_file].dir->avail_bytes -=
632 nblocks * mf->blocksize;
635 towrite = nbytes ? nbytes : mf->blocksize;
636 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
638 logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
639 mf->name, mf->cur_file);
642 zebra_mutex_unlock (&mf->mutex);
647 * Destroy a metafile, unlinking component files. File must be open.
649 int mf_unlink(MFile mf)
653 for (i = 0; i < mf->no_files; i++)
654 unlink (mf->files[i].path);
659 * Unlink the file by name, rather than MFile-handle. File should be closed.
661 int mf_unlink_name(MFile_area ma, const char *name)