1 /* $Id: mfile.c,v 1.67 2006-08-14 10:40:05 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 * TODO: The size estimates in init may not be accurate due to
28 * only partially written final blocks.
31 #include <sys/types.h>
46 #include <zebra-lock.h>
47 #include <idzebra/util.h>
48 #include <yaz/yaz-util.h>
51 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
54 * If no definition is given, use current directory, unlimited.
56 char dirname[FILENAME_MAX+1];
57 mf_dir **dp = &ma->dirs, *dir = *dp;
64 int i = 0, fact = 1, multi;
67 while (*ad == ' ' || *ad == '\t')
71 if (!yaz_is_abspath(ad) && base)
73 strcpy (dirname, base);
79 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
88 yaz_log (YLOG_WARN, "Missing colon after path: %s", ad0);
93 yaz_log (YLOG_WARN, "Empty path: %s", ad0);
96 while (*ad == ' ' || *ad == '\t')
106 if (*ad < '0' || *ad > '9')
108 yaz_log (YLOG_FATAL, "Missing size after path: %s", ad0);
112 while (*ad >= '0' && *ad <= '9')
113 size = size*10 + (*ad++ - '0');
116 case 'B': case 'b': multi = 1; break;
117 case 'K': case 'k': multi = 1024; break;
118 case 'M': case 'm': multi = 1048576; break;
119 case 'G': case 'g': multi = 1073741824; break;
121 yaz_log (YLOG_FATAL, "Missing unit: %s", ad0);
124 yaz_log (YLOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
128 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
130 strcpy(dir->name, dirname);
131 dir->max_bytes = dir->avail_bytes = fact * size * multi;
137 static zint file_position(MFile mf, zint pos, int offset)
140 int c = mf->cur_file;
142 if ((c > 0 && pos <= mf->files[c-1].top) ||
143 (c < mf->no_files -1 && pos > mf->files[c].top))
146 while (c + 1 < mf->no_files && mf->files[c].top < pos)
148 off += mf->files[c].blocks;
151 assert(c < mf->no_files);
154 off = c ? (mf->files[c-1].top + 1) : 0;
155 if (mf->files[c].fd < 0)
157 if ((mf->files[c].fd = open(mf->files[c].path,
159 (O_BINARY|O_RDWR|O_CREAT) :
160 (O_BINARY|O_RDONLY), 0666)) < 0)
162 if (!mf->wr && errno == ENOENT && off == 0)
164 yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to open %s", mf->files[c].path);
169 if (mfile_seek(mf->files[c].fd, ps * (mfile_off_t) mf->blocksize + offset,
172 yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
173 yaz_log(YLOG_WARN, "pos=" ZINT_FORMAT " off=" ZINT_FORMAT " blocksize=%d offset=%d",
174 pos, off, mf->blocksize, offset);
181 static int cmp_part_file(const void *p1, const void *p2)
183 zint d = ((part_file *)p1)->number - ((part_file *)p2)->number;
192 * Create a new area, cotaining metafiles in directories.
193 * Find the part-files in each directory, and inventory the existing metafiles.
195 MFile_area mf_init(const char *name, const char *spec, const char *base)
197 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
200 part_file *part_f = 0;
204 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
206 yaz_log (YLOG_DEBUG, "mf_init(%s)", name);
207 strcpy(ma->name, name);
210 if (scan_areadef(ma, spec, base) < 0)
212 yaz_log (YLOG_WARN, "Failed to access description of '%s'", name);
215 /* look at each directory */
216 for (dirp = ma->dirs; dirp; dirp = dirp->next)
218 if (!(dd = opendir(dirp->name)))
220 yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to open directory %s",
224 /* look at each file */
225 while ((dent = readdir(dd)))
227 int len = strlen(dent->d_name);
228 const char *cp = strrchr (dent->d_name, '-');
229 if (strchr (".-", *dent->d_name))
231 if (len < 5 || !cp || strcmp (dent->d_name + len - 3, ".mf"))
234 memcpy (metaname, dent->d_name, cp - dent->d_name);
235 metaname[ cp - dent->d_name] = '\0';
237 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
240 if (!strcmp(meta_f->name, metaname))
242 part_f = &meta_f->files[meta_f->no_files++];
249 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
250 zebra_mutex_init (&meta_f->mutex);
252 meta_f->next = ma->mfiles;
254 meta_f->cur_file = -1;
255 meta_f->unlink_flag = 0;
257 strcpy(meta_f->name, metaname);
258 part_f = &meta_f->files[0];
259 meta_f->no_files = 1;
261 part_f->number = number;
264 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
265 part_f->path = xstrdup(tmpnam);
267 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
269 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Failed to access %s",
273 if ((part_f->bytes = mfile_seek(fd, 0, SEEK_END)) < 0)
275 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Failed to seek in %s",
283 if (dirp->max_bytes >= 0)
284 dirp->avail_bytes -= part_f->bytes;
288 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
290 yaz_log (YLOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
292 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
298 void mf_destroy(MFile_area ma)
316 meta_file *m = meta_f;
318 for (i = 0; i<m->no_files; i++)
320 xfree (m->files[i].path);
322 zebra_mutex_destroy (&meta_f->mutex);
323 meta_f = meta_f->next;
329 void mf_reset(MFile_area ma)
339 meta_file *m = meta_f;
342 for (i = 0; i<m->no_files; i++)
344 unlink (m->files[i].path);
345 xfree (m->files[i].path);
347 meta_f = meta_f->next;
355 * If !ma, Use MF_DEFAULT_AREA.
357 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
361 char tmp[FILENAME_MAX+1];
364 yaz_log(YLOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
365 wflag ? "RW" : "RDONLY");
367 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
368 if (!strcmp(name, mnew->name))
377 mnew = (meta_file *) xmalloc(sizeof(*mnew));
378 strcpy(mnew->name, name);
379 /* allocate one, empty file */
380 zebra_mutex_init (&mnew->mutex);
382 mnew->files[0].bytes = 0;
383 mnew->files[0].blocks = 0;
384 mnew->files[0].top = -1;
385 mnew->files[0].number = 0;
386 mnew->files[0].fd = -1;
387 mnew->unlink_flag = 0;
388 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
389 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
390 mnew->min_bytes_creat; dp = dp->next);
393 yaz_log (YLOG_FATAL, "Insufficient space for new mfile.");
396 mnew->files[0].dir = dp;
397 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
398 mnew->files[0].path = xstrdup(tmp);
400 mnew->next = ma->mfiles;
405 for (i = 0; i < mnew->no_files; i++)
407 if (mnew->files[i].bytes % block_size)
408 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
410 mnew->files[i].blocks = (int) (mnew->files[i].bytes / block_size);
414 mnew->blocksize = block_size;
415 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
420 for (i = 0; i < mnew->no_files; i++)
422 mnew->files[i].blocks = (int)(mnew->files[i].bytes / mnew->blocksize);
423 if (i == mnew->no_files - 1)
424 mnew->files[i].top = -1;
427 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
428 : (mnew->files[i].blocks - 1);
436 int mf_close(MFile mf)
440 yaz_log (YLOG_DEBUG, "mf_close(%s) unlink=%d", mf->name, mf->unlink_flag);
442 for (i = 0; i < mf->no_files; i++)
444 if (mf->files[i].fd >= 0)
447 fsync(mf->files[i].fd);
449 close(mf->files[i].fd);
450 mf->files[i].fd = -1;
453 unlink(mf->files[i].path);
460 * Read one block from a metafile. Interface mirrors bfile.
462 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
467 zebra_mutex_lock (&mf->mutex);
468 if ((rd = file_position(mf, no, offset)) < 0)
472 zebra_mutex_unlock (&mf->mutex);
477 yaz_log (YLOG_FATAL, "mf_read %s internal error", mf->name);
481 toread = nbytes ? nbytes : mf->blocksize;
482 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
484 yaz_log (YLOG_FATAL|YLOG_ERRNO, "mf_read: Read failed (%s)",
485 mf->files[mf->cur_file].path);
488 zebra_mutex_unlock (&mf->mutex);
498 int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
504 char tmp[FILENAME_MAX+1];
505 unsigned char dummych = '\xff';
507 zebra_mutex_lock (&mf->mutex);
508 if ((ps = file_position(mf, no, offset)) < 0)
510 yaz_log (YLOG_FATAL, "mf_write %s internal error (1)", mf->name);
513 /* file needs to grow */
514 while (ps >= mf->files[mf->cur_file].blocks)
516 mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
518 /* file overflow - allocate new file */
519 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
520 needed > mf->files[mf->cur_file].dir->avail_bytes)
523 if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
526 yaz_log (YLOG_DEBUG, "Capping off file %s at pos " ZINT_FORMAT,
527 mf->files[mf->cur_file].path, nblocks);
528 if ((ps = file_position(mf,
529 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
530 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
532 yaz_log (YLOG_FATAL, "mf_write %s internal error (2)",
536 yaz_log (YLOG_DEBUG, "ps = " ZINT_FORMAT, ps);
537 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
539 yaz_log (YLOG_ERRNO|YLOG_FATAL, "mf_write %s internal error (3)",
543 mf->files[mf->cur_file].blocks += nblocks;
544 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
545 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
549 yaz_log (YLOG_DEBUG, "Creating new file.");
550 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
551 dp->avail_bytes < needed; dp = dp->next);
554 yaz_log (YLOG_FATAL, "Cannot allocate more space for %s",
558 mf->files[mf->cur_file].top = (mf->cur_file ?
559 mf->files[mf->cur_file-1].top : -1) +
560 mf->files[mf->cur_file].blocks;
561 mf->files[++(mf->cur_file)].top = -1;
562 mf->files[mf->cur_file].dir = dp;
563 mf->files[mf->cur_file].number =
564 mf->files[mf->cur_file-1].number + 1;
565 mf->files[mf->cur_file].blocks = 0;
566 mf->files[mf->cur_file].bytes = 0;
567 mf->files[mf->cur_file].fd = -1;
568 sprintf(tmp, "%s/%s-" ZINT_FORMAT ".mf", dp->name, mf->name,
569 mf->files[mf->cur_file].number);
570 mf->files[mf->cur_file].path = xstrdup(tmp);
572 /* open new file and position at beginning */
573 if ((ps = file_position(mf, no, offset)) < 0)
575 yaz_log (YLOG_FATAL, "mf_write %s internal error (4)",
582 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
583 mf->files[mf->cur_file].blocks += nblocks;
584 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
585 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
586 mf->files[mf->cur_file].dir->avail_bytes -=
587 nblocks * mf->blocksize;
590 towrite = nbytes ? nbytes : mf->blocksize;
591 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
593 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Write failed for file %s part %d",
594 mf->name, mf->cur_file);
597 zebra_mutex_unlock (&mf->mutex);
602 * Destroy a metafile, unlinking component files. File must be open.
604 int mf_unlink(MFile mf)
613 for (i = 0; i<mf->no_files; i++)
614 unlink(mf->files[i].path);
619 int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
620 double *used_bytes, double *max_bytes)
623 mf_dir *d = ma->dirs;
624 for (i = 0; d && i<no; i++, d = d->next)
629 *directory = d->name;
632 /* possible loss of data. But it's just statistics and lies */
633 *max_bytes = (double) d->max_bytes;
637 /* possible loss of data. But it's just statistics and lies */
638 *used_bytes = (double) (d->max_bytes - d->avail_bytes);
645 * indent-tabs-mode: nil
647 * vim: shiftwidth=4 tabstop=8 expandtab