2 * Copyright (C) 1994-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.41.2.2 2002-08-02 10:34:27 adam
8 * mf_write fix. version update
10 * Revision 1.41.2.1 2002/07/23 12:33:21 adam
13 * Revision 1.41.4.1 2002/07/23 09:32:41 adam
14 * 64 bit file access on WIN32
16 * Revision 1.41 2000/11/29 14:24:01 adam
17 * Script configure uses yaz pthreads options. Added locking for
18 * zebra_register_{lock,unlock}.
20 * Revision 1.40 2000/10/17 12:37:09 adam
21 * Fixed notification of live-updates. Fixed minor problem with mf_init
22 * where it didn't handle shadow area file names correctly.
24 * Revision 1.39 2000/05/05 13:48:03 adam
25 * Fixed locking for metafiles.
27 * Revision 1.38 2000/03/20 19:08:35 adam
28 * Added remote record import using Z39.50 extended services and Segment
31 * Revision 1.37 2000/03/15 15:00:30 adam
32 * First work on threaded version.
34 * Revision 1.36 1999/12/08 15:03:11 adam
35 * Implemented bf_reset.
37 * Revision 1.35 1999/10/14 14:33:50 adam
38 * Added truncation 5=106.
40 * Revision 1.34 1999/05/26 07:49:12 adam
43 * Revision 1.33 1999/05/12 13:08:06 adam
44 * First version of ISAMS.
46 * Revision 1.32 1999/04/28 14:53:07 adam
47 * Fixed stupid bug regarding split-files.
49 * Revision 1.31 1999/02/18 12:49:33 adam
50 * Changed file naming scheme for register files as well as record
53 * Revision 1.30 1999/02/02 14:50:02 adam
54 * Updated WIN32 code specific sections. Changed header.
56 * Revision 1.29 1998/05/27 14:28:34 adam
57 * Fixed bug in mf_write. 'Cap off' byte written at wrong offset.
59 * Revision 1.28 1998/05/20 10:00:35 adam
60 * Fixed register spec so that colon isn't treated as size separator
61 * unless followed by [0-9+-] in order to allow DOS drive specifications.
63 * Revision 1.27 1998/02/10 11:55:07 adam
66 * Revision 1.26 1997/10/27 14:25:38 adam
69 * Revision 1.25 1997/09/18 08:59:16 adam
70 * Extra generic handle for the character mapping routines.
72 * Revision 1.24 1997/09/17 12:19:06 adam
73 * Zebra version corresponds to YAZ version 1.4.
74 * Changed Zebra server so that it doesn't depend on global common_resource.
76 * Revision 1.23 1997/09/09 13:37:53 adam
77 * Partial port to WIN95/NT.
79 * Revision 1.22 1997/09/04 13:56:39 adam
80 * Added O_BINARY to open calls.
82 * Revision 1.21 1996/10/29 13:56:18 adam
83 * Include of zebrautl.h instead of alexutil.h.
85 * Revision 1.20 1996/05/14 12:10:16 quinn
88 * Revision 1.19 1996/05/01 07:16:30 quinn
91 * Revision 1.18 1996/04/09 06:47:30 adam
92 * Function scan_areadef doesn't use sscanf (%n fails on this Linux).
94 * Revision 1.17 1996/03/20 13:29:11 quinn
97 * Revision 1.16 1995/12/12 15:57:57 adam
98 * Implemented mf_unlink. cf_unlink uses mf_unlink.
100 * Revision 1.15 1995/12/08 16:21:14 adam
101 * Work on commit/update.
103 * Revision 1.14 1995/12/05 13:12:37 quinn
106 * Revision 1.13 1995/11/30 17:00:50 adam
107 * Several bug fixes. Commit system runs now.
109 * Revision 1.12 1995/11/24 17:26:11 quinn
110 * Mostly about making some ISAM stuff in the config file optional.
112 * Revision 1.11 1995/11/13 09:32:43 quinn
115 * Revision 1.10 1995/09/04 12:33:22 adam
116 * Various cleanup. YAZ util used instead.
118 * Revision 1.9 1994/11/04 14:26:39 quinn
121 * Revision 1.8 1994/10/05 16:56:42 quinn
124 * Revision 1.7 1994/09/19 14:12:37 quinn
127 * Revision 1.6 1994/09/14 13:10:15 quinn
128 * Corrected some bugs in the init-phase
130 * Revision 1.5 1994/09/12 08:01:51 quinn
133 * Revision 1.4 1994/09/01 14:51:07 quinn
134 * Allowed mf_write to write beyond eof+1.
136 * Revision 1.3 1994/08/24 09:37:17 quinn
137 * Changed reaction to read return values.
139 * Revision 1.2 1994/08/23 14:50:48 quinn
142 * Revision 1.1 1994/08/23 14:41:33 quinn
143 * First functional version.
149 * TODO: The size estimates in init may not be accurate due to
150 * only partially written final blocks.
153 #include <sys/types.h>
167 #include <zebra-lock.h>
168 #include <zebrautl.h>
171 static int scan_areadef(MFile_area ma, const char *name, const char *ad)
174 * If no definition is given, use current directory, unlimited.
176 char dirname[FILENAME_MAX+1];
177 mf_dir **dp = &ma->dirs, *dir = *dp;
183 const char *ad0 = ad;
184 int i = 0, fact = 1, multi;
185 mfile_off_t size = 0;
187 while (*ad == ' ' || *ad == '\t')
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);
282 (mf->files[c].fd, (mfile_off_t) (ps = pos-off) * mf->blocksize + offset,
283 SEEK_SET) == (mfile_off_t) (-1))
285 logf (LOG_WARN|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
292 static int cmp_part_file(const void *p1, const void *p2)
294 return ((part_file *)p1)->number - ((part_file *)p2)->number;
298 * Create a new area, cotaining metafiles in directories.
299 * Find the part-files in each directory, and inventory the existing metafiles.
301 MFile_area mf_init(const char *name, const char *spec)
303 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
306 part_file *part_f = 0;
310 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
312 logf (LOG_DEBUG, "mf_init(%s)", name);
313 strcpy(ma->name, name);
316 if (scan_areadef(ma, name, spec) < 0)
318 logf (LOG_WARN, "Failed to access description of '%s'", name);
321 /* look at each directory */
322 for (dirp = ma->dirs; dirp; dirp = dirp->next)
324 if (!(dd = opendir(dirp->name)))
326 logf (LOG_WARN|LOG_ERRNO, "Failed to open directory %s",
330 /* look at each file */
331 while ((dent = readdir(dd)))
333 int len = strlen(dent->d_name);
334 const char *cp = strrchr (dent->d_name, '-');
335 if (strchr (".-", *dent->d_name))
337 if (len < 5 || !cp || strcmp (dent->d_name + len - 3, ".mf"))
340 memcpy (metaname, dent->d_name, cp - dent->d_name);
341 metaname[ cp - dent->d_name] = '\0';
343 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
346 if (!strcmp(meta_f->name, metaname))
348 part_f = &meta_f->files[meta_f->no_files++];
355 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
356 zebra_mutex_init (&meta_f->mutex);
358 meta_f->next = ma->mfiles;
360 meta_f->cur_file = -1;
362 strcpy(meta_f->name, metaname);
363 part_f = &meta_f->files[0];
364 meta_f->no_files = 1;
366 part_f->number = number;
369 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
370 part_f->path = xstrdup(tmpnam);
372 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
374 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
384 (fd, (mfile_off_t) 0, SEEK_END)) == (mfile_off_t)(-1))
386 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
391 if (dirp->max_bytes >= 0)
392 dirp->avail_bytes -= part_f->bytes;
396 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
398 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
400 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
406 void mf_destroy(MFile_area ma)
424 meta_file *m = meta_f;
426 for (i = 0; i<m->no_files; i++)
428 xfree (m->files[i].path);
430 zebra_mutex_destroy (&meta_f->mutex);
431 meta_f = meta_f->next;
437 void mf_reset(MFile_area ma)
447 meta_file *m = meta_f;
450 for (i = 0; i<m->no_files; i++)
452 unlink (m->files[i].path);
453 xfree (m->files[i].path);
455 meta_f = meta_f->next;
463 * If !ma, Use MF_DEFAULT_AREA.
465 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
469 char tmp[FILENAME_MAX+1];
472 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
473 wflag ? "RW" : "RDONLY");
475 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
476 if (!strcmp(name, mnew->name))
485 mnew = (meta_file *) xmalloc(sizeof(*mnew));
486 strcpy(mnew->name, name);
487 /* allocate one, empty file */
488 zebra_mutex_init (&mnew->mutex);
490 mnew->files[0].bytes = 0;
491 mnew->files[0].blocks = 0;
492 mnew->files[0].top = -1;
493 mnew->files[0].number = 0;
494 mnew->files[0].fd = -1;
495 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
496 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
497 mnew->min_bytes_creat; dp = dp->next);
500 logf (LOG_FATAL, "Insufficient space for new mfile.");
503 mnew->files[0].dir = dp;
504 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
505 mnew->files[0].path = xstrdup(tmp);
507 mnew->next = ma->mfiles;
512 for (i = 0; i < mnew->no_files; i++)
514 if (mnew->files[i].bytes % block_size)
515 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
517 mnew->files[i].blocks = mnew->files[i].bytes / block_size;
521 mnew->blocksize = block_size;
522 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
527 for (i = 0; i < mnew->no_files; i++)
529 mnew->files[i].blocks = mnew->files[i].bytes / mnew->blocksize;
530 if (i == mnew->no_files - 1)
531 mnew->files[i].top = -1;
534 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
535 : (mnew->files[i].blocks - 1);
543 int mf_close(MFile mf)
547 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
549 for (i = 0; i < mf->no_files; i++)
550 if (mf->files[i].fd >= 0)
552 close(mf->files[i].fd);
553 mf->files[i].fd = -1;
560 * Read one block from a metafile. Interface mirrors bfile.
562 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
566 zebra_mutex_lock (&mf->mutex);
567 if ((rd = file_position(mf, no, offset)) < 0)
571 zebra_mutex_unlock (&mf->mutex);
577 toread = nbytes ? nbytes : mf->blocksize;
578 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
580 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
581 mf->files[mf->cur_file].path);
584 zebra_mutex_unlock (&mf->mutex);
594 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
596 int ps, nblocks, towrite;
598 char tmp[FILENAME_MAX+1];
599 unsigned char dummych = '\xff';
601 zebra_mutex_lock (&mf->mutex);
602 if ((ps = file_position(mf, no, offset)) < 0)
604 /* file needs to grow */
605 while (ps >= mf->files[mf->cur_file].blocks)
607 off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
609 /* file overflow - allocate new file */
610 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
611 needed > mf->files[mf->cur_file].dir->avail_bytes)
614 if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
617 logf (LOG_DEBUG, "Capping off file %s at pos %d",
618 mf->files[mf->cur_file].path, nblocks);
619 if ((ps = file_position(mf,
620 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
621 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
623 logf (LOG_DEBUG, "ps = %d", ps);
624 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
626 logf (LOG_ERRNO|LOG_FATAL, "write dummy");
629 mf->files[mf->cur_file].blocks += nblocks;
630 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
631 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
635 logf (LOG_DEBUG, "Creating new file.");
636 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
637 dp->avail_bytes < needed; dp = dp->next);
640 logf (LOG_FATAL, "Cannot allocate more space for %s",
644 mf->files[mf->cur_file].top = (mf->cur_file ?
645 mf->files[mf->cur_file-1].top : -1) +
646 mf->files[mf->cur_file].blocks;
647 mf->files[++(mf->cur_file)].top = -1;
648 mf->files[mf->cur_file].dir = dp;
649 mf->files[mf->cur_file].number =
650 mf->files[mf->cur_file-1].number + 1;
651 mf->files[mf->cur_file].blocks = 0;
652 mf->files[mf->cur_file].bytes = 0;
653 mf->files[mf->cur_file].fd = -1;
654 sprintf(tmp, "%s/%s-%d.mf", dp->name, mf->name,
655 mf->files[mf->cur_file].number);
656 mf->files[mf->cur_file].path = xstrdup(tmp);
658 /* open new file and position at beginning */
659 if ((ps = file_position(mf, no, offset)) < 0)
664 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
665 mf->files[mf->cur_file].blocks += nblocks;
666 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
667 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
668 mf->files[mf->cur_file].dir->avail_bytes -=
669 nblocks * mf->blocksize;
672 towrite = nbytes ? nbytes : mf->blocksize;
673 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
675 logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
676 mf->name, mf->cur_file);
679 zebra_mutex_unlock (&mf->mutex);
684 * Destroy a metafile, unlinking component files. File must be open.
686 int mf_unlink(MFile mf)
690 for (i = 0; i < mf->no_files; i++)
691 unlink (mf->files[i].path);
696 * Unlink the file by name, rather than MFile-handle. File should be closed.
698 int mf_unlink_name(MFile_area ma, const char *name)