1 /* $Id: mfile.c,v 1.69 2006-10-10 10:19:28 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;
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)
314 void mf_reset(MFile_area ma, int unlink_flag)
324 meta_file *m = meta_f;
326 meta_f = meta_f->next;
329 for (i = 0; i<m->no_files; i++)
332 unlink (m->files[i].path);
333 xfree (m->files[i].path);
335 zebra_mutex_destroy (&m->mutex);
344 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
348 char tmp[FILENAME_MAX+1];
351 yaz_log(YLOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
352 wflag ? "RW" : "RDONLY");
354 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
355 if (!strcmp(name, mnew->name))
364 mnew = (meta_file *) xmalloc(sizeof(*mnew));
365 strcpy(mnew->name, name);
366 /* allocate one, empty file */
367 zebra_mutex_init (&mnew->mutex);
369 mnew->files[0].bytes = 0;
370 mnew->files[0].blocks = 0;
371 mnew->files[0].top = -1;
372 mnew->files[0].number = 0;
373 mnew->files[0].fd = -1;
374 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
375 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
376 mnew->min_bytes_creat; dp = dp->next);
379 yaz_log (YLOG_FATAL, "Insufficient space for new mfile.");
382 mnew->files[0].dir = dp;
383 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
384 mnew->files[0].path = xstrdup(tmp);
386 mnew->next = ma->mfiles;
391 for (i = 0; i < mnew->no_files; i++)
393 if (mnew->files[i].bytes % block_size)
394 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
396 mnew->files[i].blocks = (int) (mnew->files[i].bytes / block_size);
400 mnew->blocksize = block_size;
401 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
406 for (i = 0; i < mnew->no_files; i++)
408 mnew->files[i].blocks = (int)(mnew->files[i].bytes / mnew->blocksize);
409 if (i == mnew->no_files - 1)
410 mnew->files[i].top = -1;
413 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
414 : (mnew->files[i].blocks - 1);
422 int mf_close(MFile mf)
426 yaz_log (YLOG_DEBUG, "mf_close(%s)", mf->name);
428 for (i = 0; i < mf->no_files; i++)
430 if (mf->files[i].fd >= 0)
433 fsync(mf->files[i].fd);
435 close(mf->files[i].fd);
436 mf->files[i].fd = -1;
444 * Read one block from a metafile. Interface mirrors bfile.
446 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
451 zebra_mutex_lock (&mf->mutex);
452 if ((rd = file_position(mf, no, offset)) < 0)
456 zebra_mutex_unlock (&mf->mutex);
461 yaz_log (YLOG_FATAL, "mf_read %s internal error", mf->name);
465 toread = nbytes ? nbytes : mf->blocksize;
466 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
468 yaz_log (YLOG_FATAL|YLOG_ERRNO, "mf_read: Read failed (%s)",
469 mf->files[mf->cur_file].path);
472 zebra_mutex_unlock (&mf->mutex);
482 int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
488 char tmp[FILENAME_MAX+1];
489 unsigned char dummych = '\xff';
491 zebra_mutex_lock (&mf->mutex);
492 if ((ps = file_position(mf, no, offset)) < 0)
494 yaz_log (YLOG_FATAL, "mf_write %s internal error (1)", mf->name);
497 /* file needs to grow */
498 while (ps >= mf->files[mf->cur_file].blocks)
500 mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
502 /* file overflow - allocate new file */
503 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
504 needed > mf->files[mf->cur_file].dir->avail_bytes)
507 if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
510 yaz_log (YLOG_DEBUG, "Capping off file %s at pos " ZINT_FORMAT,
511 mf->files[mf->cur_file].path, nblocks);
512 if ((ps = file_position(mf,
513 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
514 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
516 yaz_log (YLOG_FATAL, "mf_write %s internal error (2)",
520 yaz_log (YLOG_DEBUG, "ps = " ZINT_FORMAT, ps);
521 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
523 yaz_log (YLOG_ERRNO|YLOG_FATAL, "mf_write %s internal error (3)",
527 mf->files[mf->cur_file].blocks += nblocks;
528 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
529 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
533 yaz_log (YLOG_DEBUG, "Creating new file.");
534 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
535 dp->avail_bytes < needed; dp = dp->next);
538 yaz_log (YLOG_FATAL, "Cannot allocate more space for %s",
542 mf->files[mf->cur_file].top = (mf->cur_file ?
543 mf->files[mf->cur_file-1].top : -1) +
544 mf->files[mf->cur_file].blocks;
545 mf->files[++(mf->cur_file)].top = -1;
546 mf->files[mf->cur_file].dir = dp;
547 mf->files[mf->cur_file].number =
548 mf->files[mf->cur_file-1].number + 1;
549 mf->files[mf->cur_file].blocks = 0;
550 mf->files[mf->cur_file].bytes = 0;
551 mf->files[mf->cur_file].fd = -1;
552 sprintf(tmp, "%s/%s-" ZINT_FORMAT ".mf", dp->name, mf->name,
553 mf->files[mf->cur_file].number);
554 mf->files[mf->cur_file].path = xstrdup(tmp);
556 /* open new file and position at beginning */
557 if ((ps = file_position(mf, no, offset)) < 0)
559 yaz_log (YLOG_FATAL, "mf_write %s internal error (4)",
566 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
567 mf->files[mf->cur_file].blocks += nblocks;
568 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
569 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
570 mf->files[mf->cur_file].dir->avail_bytes -=
571 nblocks * mf->blocksize;
574 towrite = nbytes ? nbytes : mf->blocksize;
575 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
577 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Write failed for file %s part %d",
578 mf->name, mf->cur_file);
581 zebra_mutex_unlock (&mf->mutex);
585 int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
586 double *used_bytes, double *max_bytes)
589 mf_dir *d = ma->dirs;
590 for (i = 0; d && i<no; i++, d = d->next)
595 *directory = d->name;
598 /* possible loss of data. But it's just statistics and lies */
599 *max_bytes = (double) d->max_bytes;
603 /* possible loss of data. But it's just statistics and lies */
604 *used_bytes = (double) (d->max_bytes - d->avail_bytes);
611 * indent-tabs-mode: nil
613 * vim: shiftwidth=4 tabstop=8 expandtab