1 /* $Id: mfile.c,v 1.60 2005-04-15 10:47:47 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 * TODO: The size estimates in init may not be accurate due to
28 * only partially written final blocks.
31 #include <sys/types.h>
45 #include <zebra-lock.h>
46 #include <idzebra/util.h>
47 #include <yaz/yaz-util.h>
50 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
53 * If no definition is given, use current directory, unlimited.
55 char dirname[FILENAME_MAX+1];
56 mf_dir **dp = &ma->dirs, *dir = *dp;
63 int i = 0, fact = 1, multi;
66 while (*ad == ' ' || *ad == '\t')
70 if (!yaz_is_abspath(ad) && base)
72 strcpy (dirname, base);
78 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
87 yaz_log (YLOG_WARN, "Missing colon after path: %s", ad0);
92 yaz_log (YLOG_WARN, "Empty path: %s", ad0);
95 while (*ad == ' ' || *ad == '\t')
105 if (*ad < '0' || *ad > '9')
107 yaz_log (YLOG_FATAL, "Missing size after path: %s", ad0);
111 while (*ad >= '0' && *ad <= '9')
112 size = size*10 + (*ad++ - '0');
115 case 'B': case 'b': multi = 1; break;
116 case 'K': case 'k': multi = 1024; break;
117 case 'M': case 'm': multi = 1048576; break;
118 case 'G': case 'g': multi = 1073741824; break;
120 yaz_log (YLOG_FATAL, "Missing unit: %s", ad0);
123 yaz_log (YLOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
127 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
129 strcpy(dir->name, dirname);
130 dir->max_bytes = dir->avail_bytes = fact * size * multi;
136 static zint file_position(MFile mf, zint pos, int offset)
139 int c = mf->cur_file;
141 if ((c > 0 && pos <= mf->files[c-1].top) ||
142 (c < mf->no_files -1 && pos > mf->files[c].top))
145 while (c + 1 < mf->no_files && mf->files[c].top < pos)
147 off += mf->files[c].blocks;
150 assert(c < mf->no_files);
153 off = c ? (mf->files[c-1].top + 1) : 0;
154 if (mf->files[c].fd < 0)
156 if ((mf->files[c].fd = open(mf->files[c].path,
158 (O_BINARY|O_RDWR|O_CREAT) :
159 (O_BINARY|O_RDONLY), 0666)) < 0)
161 if (!mf->wr && errno == ENOENT && off == 0)
163 yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to open %s", mf->files[c].path);
168 if (mfile_seek(mf->files[c].fd, ps * (mfile_off_t) mf->blocksize + offset,
171 yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
172 yaz_log(YLOG_WARN, "pos=" ZINT_FORMAT " off=" ZINT_FORMAT " blocksize=%d offset=%d",
173 pos, off, mf->blocksize, offset);
180 static int cmp_part_file(const void *p1, const void *p2)
182 zint d = ((part_file *)p1)->number - ((part_file *)p2)->number;
191 * Create a new area, cotaining metafiles in directories.
192 * Find the part-files in each directory, and inventory the existing metafiles.
194 MFile_area mf_init(const char *name, const char *spec, const char *base)
196 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
199 part_file *part_f = 0;
203 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
205 yaz_log (YLOG_DEBUG, "mf_init(%s)", name);
206 strcpy(ma->name, name);
209 if (scan_areadef(ma, spec, base) < 0)
211 yaz_log (YLOG_WARN, "Failed to access description of '%s'", name);
214 /* look at each directory */
215 for (dirp = ma->dirs; dirp; dirp = dirp->next)
217 if (!(dd = opendir(dirp->name)))
219 yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to open directory %s",
223 /* look at each file */
224 while ((dent = readdir(dd)))
226 int len = strlen(dent->d_name);
227 const char *cp = strrchr (dent->d_name, '-');
228 if (strchr (".-", *dent->d_name))
230 if (len < 5 || !cp || strcmp (dent->d_name + len - 3, ".mf"))
233 memcpy (metaname, dent->d_name, cp - dent->d_name);
234 metaname[ cp - dent->d_name] = '\0';
236 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
239 if (!strcmp(meta_f->name, metaname))
241 part_f = &meta_f->files[meta_f->no_files++];
248 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
249 zebra_mutex_init (&meta_f->mutex);
251 meta_f->next = ma->mfiles;
253 meta_f->cur_file = -1;
254 meta_f->unlink_flag = 0;
256 strcpy(meta_f->name, metaname);
257 part_f = &meta_f->files[0];
258 meta_f->no_files = 1;
260 part_f->number = number;
263 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
264 part_f->path = xstrdup(tmpnam);
266 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
268 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Failed to access %s",
272 if ((part_f->bytes = mfile_seek(fd, 0, SEEK_END)) < 0)
274 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Failed to seek in %s",
282 if (dirp->max_bytes >= 0)
283 dirp->avail_bytes -= part_f->bytes;
287 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
289 yaz_log (YLOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
291 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
297 void mf_destroy(MFile_area ma)
315 meta_file *m = meta_f;
317 for (i = 0; i<m->no_files; i++)
319 xfree (m->files[i].path);
321 zebra_mutex_destroy (&meta_f->mutex);
322 meta_f = meta_f->next;
328 void mf_reset(MFile_area ma)
338 meta_file *m = meta_f;
341 for (i = 0; i<m->no_files; i++)
343 unlink (m->files[i].path);
344 xfree (m->files[i].path);
346 meta_f = meta_f->next;
354 * If !ma, Use MF_DEFAULT_AREA.
356 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
360 char tmp[FILENAME_MAX+1];
363 yaz_log(YLOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
364 wflag ? "RW" : "RDONLY");
366 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
367 if (!strcmp(name, mnew->name))
376 mnew = (meta_file *) xmalloc(sizeof(*mnew));
377 strcpy(mnew->name, name);
378 /* allocate one, empty file */
379 zebra_mutex_init (&mnew->mutex);
381 mnew->files[0].bytes = 0;
382 mnew->files[0].blocks = 0;
383 mnew->files[0].top = -1;
384 mnew->files[0].number = 0;
385 mnew->files[0].fd = -1;
386 mnew->unlink_flag = 0;
387 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
388 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
389 mnew->min_bytes_creat; dp = dp->next);
392 yaz_log (YLOG_FATAL, "Insufficient space for new mfile.");
395 mnew->files[0].dir = dp;
396 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
397 mnew->files[0].path = xstrdup(tmp);
399 mnew->next = ma->mfiles;
404 for (i = 0; i < mnew->no_files; i++)
406 if (mnew->files[i].bytes % block_size)
407 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
409 mnew->files[i].blocks = (int) (mnew->files[i].bytes / block_size);
413 mnew->blocksize = block_size;
414 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
419 for (i = 0; i < mnew->no_files; i++)
421 mnew->files[i].blocks = (int)(mnew->files[i].bytes / mnew->blocksize);
422 if (i == mnew->no_files - 1)
423 mnew->files[i].top = -1;
426 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
427 : (mnew->files[i].blocks - 1);
435 int mf_close(MFile mf)
439 yaz_log (YLOG_DEBUG, "mf_close(%s)", mf->name);
441 for (i = 0; i < mf->no_files; i++)
443 if (mf->files[i].fd >= 0)
446 fsync(mf->files[i].fd);
448 close(mf->files[i].fd);
449 mf->files[i].fd = -1;
452 unlink(mf->files[i].path);
459 * Read one block from a metafile. Interface mirrors bfile.
461 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
466 zebra_mutex_lock (&mf->mutex);
467 if ((rd = file_position(mf, no, offset)) < 0)
471 zebra_mutex_unlock (&mf->mutex);
476 yaz_log (YLOG_FATAL, "mf_read %s internal error", mf->name);
480 toread = nbytes ? nbytes : mf->blocksize;
481 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
483 yaz_log (YLOG_FATAL|YLOG_ERRNO, "mf_read: Read failed (%s)",
484 mf->files[mf->cur_file].path);
487 zebra_mutex_unlock (&mf->mutex);
497 int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
503 char tmp[FILENAME_MAX+1];
504 unsigned char dummych = '\xff';
506 zebra_mutex_lock (&mf->mutex);
507 if ((ps = file_position(mf, no, offset)) < 0)
509 yaz_log (YLOG_FATAL, "mf_write %s internal error (1)", mf->name);
512 /* file needs to grow */
513 while (ps >= mf->files[mf->cur_file].blocks)
515 mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
517 /* file overflow - allocate new file */
518 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
519 needed > mf->files[mf->cur_file].dir->avail_bytes)
522 if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
525 yaz_log (YLOG_DEBUG, "Capping off file %s at pos " ZINT_FORMAT,
526 mf->files[mf->cur_file].path, nblocks);
527 if ((ps = file_position(mf,
528 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
529 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
531 yaz_log (YLOG_FATAL, "mf_write %s internal error (2)",
535 yaz_log (YLOG_DEBUG, "ps = " ZINT_FORMAT, ps);
536 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
538 yaz_log (YLOG_ERRNO|YLOG_FATAL, "mf_write %s internal error (3)",
542 mf->files[mf->cur_file].blocks += nblocks;
543 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
544 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
548 yaz_log (YLOG_DEBUG, "Creating new file.");
549 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
550 dp->avail_bytes < needed; dp = dp->next);
553 yaz_log (YLOG_FATAL, "Cannot allocate more space for %s",
557 mf->files[mf->cur_file].top = (mf->cur_file ?
558 mf->files[mf->cur_file-1].top : -1) +
559 mf->files[mf->cur_file].blocks;
560 mf->files[++(mf->cur_file)].top = -1;
561 mf->files[mf->cur_file].dir = dp;
562 mf->files[mf->cur_file].number =
563 mf->files[mf->cur_file-1].number + 1;
564 mf->files[mf->cur_file].blocks = 0;
565 mf->files[mf->cur_file].bytes = 0;
566 mf->files[mf->cur_file].fd = -1;
567 sprintf(tmp, "%s/%s-" ZINT_FORMAT ".mf", dp->name, mf->name,
568 mf->files[mf->cur_file].number);
569 mf->files[mf->cur_file].path = xstrdup(tmp);
571 /* open new file and position at beginning */
572 if ((ps = file_position(mf, no, offset)) < 0)
574 yaz_log (YLOG_FATAL, "mf_write %s internal error (4)",
581 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
582 mf->files[mf->cur_file].blocks += nblocks;
583 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
584 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
585 mf->files[mf->cur_file].dir->avail_bytes -=
586 nblocks * mf->blocksize;
589 towrite = nbytes ? nbytes : mf->blocksize;
590 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
592 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Write failed for file %s part %d",
593 mf->name, mf->cur_file);
596 zebra_mutex_unlock (&mf->mutex);
601 * Destroy a metafile, unlinking component files. File must be open.
603 int mf_unlink(MFile mf)
610 for (i = 0; i<mf->no_files; i++)
611 unlink(mf->files[i].path);
617 * Unlink the file by name, rather than MFile-handle. File should be closed.
619 int mf_unlink_name(MFile_area ma, const char *name)