1 /* $Id: mfile.c,v 1.76 2007-12-17 14:12:09 heikki Exp $
2 Copyright (C) 1995-2007
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
23 #include <sys/types.h>
38 #include <zebra-lock.h>
39 #include <idzebra/util.h>
40 #include <yaz/yaz-util.h>
43 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
46 * If no definition is given, use current directory, unlimited.
48 char dirname[FILENAME_MAX+1];
49 mf_dir **dp = &ma->dirs, *dir = *dp;
56 int i = 0, fact = 1, multi;
59 while (*ad == ' ' || *ad == '\t')
63 if (!yaz_is_abspath(ad) && base)
65 strcpy(dirname, base);
71 if (*ad == ':' && strchr("+-0123456789", ad[1]))
80 yaz_log(YLOG_WARN, "Missing colon after path: %s", ad0);
85 yaz_log(YLOG_WARN, "Empty path: %s", ad0);
88 while (*ad == ' ' || *ad == '\t')
98 if (*ad < '0' || *ad > '9')
100 yaz_log(YLOG_FATAL, "Missing size after path: %s", ad0);
104 while (*ad >= '0' && *ad <= '9')
105 size = size*10 + (*ad++ - '0');
108 case 'B': case 'b': multi = 1; break;
109 case 'K': case 'k': multi = 1024; break;
110 case 'M': case 'm': multi = 1048576; break;
111 case 'G': case 'g': multi = 1073741824; break;
113 yaz_log(YLOG_FATAL, "Missing unit: %s", ad0);
116 yaz_log(YLOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
120 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
122 strcpy(dir->name, dirname);
123 dir->max_bytes = dir->avail_bytes = fact * size * multi;
129 /** \brief position within metafile (perform seek)
130 \param mf metafile handle
131 \param pos block position
132 \param offset offset within block
135 \retval -2 OK, but file does not created (read-only)
138 static zint file_position(MFile mf, zint pos, int offset)
141 int c = mf->cur_file;
143 if ((c > 0 && pos <= mf->files[c-1].top) ||
144 (c < mf->no_files -1 && pos > mf->files[c].top))
147 while (c + 1 < mf->no_files && mf->files[c].top < pos)
149 off += mf->files[c].blocks;
152 assert(c < mf->no_files);
155 off = c ? (mf->files[c-1].top + 1) : 0;
156 if (mf->files[c].fd < 0)
158 if ((mf->files[c].fd = open(mf->files[c].path,
160 (O_BINARY|O_RDWR|O_CREAT) :
161 (O_BINARY|O_RDONLY), 0666)) < 0)
163 if (!mf->wr && errno == ENOENT && off == 0)
165 /* we can't open it for reading. But not really an error */
168 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to open %s", mf->files[c].path);
173 if (mfile_seek(mf->files[c].fd, ps *(mfile_off_t) mf->blocksize + offset,
176 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
177 yaz_log(YLOG_WARN, "pos=" ZINT_FORMAT " off=" ZINT_FORMAT " blocksize=%d offset=%d",
178 pos, off, mf->blocksize, offset);
185 static int cmp_part_file(const void *p1, const void *p2)
187 zint d = ((part_file *)p1)->number - ((part_file *)p2)->number;
195 MFile_area mf_init(const char *name, const char *spec, const char *base,
196 int only_shadow_files)
198 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
201 part_file *part_f = 0;
205 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
207 yaz_log(YLOG_DEBUG, "mf_init(%s)", name);
208 strcpy(ma->name, name);
211 if (scan_areadef(ma, spec, base) < 0)
213 yaz_log(YLOG_WARN, "Failed to access description of '%s'", name);
217 /* look at each directory */
218 for (dirp = ma->dirs; dirp; dirp = dirp->next)
220 if (!(dd = opendir(dirp->name)))
222 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to open directory %s",
227 /* look at each file */
228 while ((dent = readdir(dd)))
230 int len = strlen(dent->d_name);
231 const char *cp = strrchr(dent->d_name, '-');
232 if (strchr(".-", *dent->d_name))
234 if (len < 5 || !cp || strcmp(dent->d_name + len - 3, ".mf"))
237 memcpy(metaname, dent->d_name, cp - dent->d_name);
238 metaname[ cp - dent->d_name] = '\0';
240 /* only files such as file-i-0.mf and file-i-b-0.mf, bug #739 */
241 if (only_shadow_files && cp[-2] != '-')
243 if (!only_shadow_files && cp[-2] == '-')
245 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
248 if (!strcmp(meta_f->name, metaname))
250 part_f = &meta_f->files[meta_f->no_files++];
257 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
258 zebra_mutex_init(&meta_f->mutex);
260 meta_f->next = ma->mfiles;
262 meta_f->cur_file = -1;
264 strcpy(meta_f->name, metaname);
265 part_f = &meta_f->files[0];
266 meta_f->no_files = 1;
268 part_f->number = number;
271 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
272 part_f->path = xstrdup(tmpnam);
274 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
276 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to access %s",
282 if ((part_f->bytes = mfile_seek(fd, 0, SEEK_END)) < 0)
284 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to seek in %s",
292 if (dirp->max_bytes >= 0)
293 dirp->avail_bytes -= part_f->bytes;
297 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
299 yaz_log(YLOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
301 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
307 void mf_destroy(MFile_area ma)
324 void mf_reset(MFile_area ma, int unlink_flag)
334 meta_file *m = meta_f;
336 meta_f = meta_f->next;
339 for (i = 0; i<m->no_files; i++)
342 unlink(m->files[i].path);
343 xfree(m->files[i].path);
345 zebra_mutex_destroy(&m->mutex);
351 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
355 char tmp[FILENAME_MAX+1];
358 yaz_log(YLOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
359 wflag ? "RW" : "RDONLY");
361 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
362 if (!strcmp(name, mnew->name))
366 yaz_log(YLOG_WARN, "metafile %s already open", name);
373 mnew = (meta_file *) xmalloc(sizeof(*mnew));
374 strcpy(mnew->name, name);
375 /* allocate one, empty file */
376 zebra_mutex_init(&mnew->mutex);
378 mnew->files[0].bytes = 0;
379 mnew->files[0].blocks = 0;
380 mnew->files[0].top = -1;
381 mnew->files[0].number = 0;
382 mnew->files[0].fd = -1;
383 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
384 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
385 mnew->min_bytes_creat; dp = dp->next);
388 yaz_log(YLOG_FATAL, "Insufficient space for file %s", name);
392 mnew->files[0].dir = dp;
393 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
394 mnew->files[0].path = xstrdup(tmp);
396 mnew->next = ma->mfiles;
401 for (i = 0; i < mnew->no_files; i++)
403 if (mnew->files[i].bytes % block_size)
404 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
406 mnew->files[i].blocks = (int) (mnew->files[i].bytes / block_size);
410 mnew->blocksize = block_size;
411 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
416 for (i = 0; i < mnew->no_files; i++)
418 mnew->files[i].blocks = (int)(mnew->files[i].bytes / mnew->blocksize);
419 if (i == mnew->no_files - 1)
420 mnew->files[i].top = -1;
423 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
424 : (mnew->files[i].blocks - 1);
429 int mf_close(MFile mf)
433 yaz_log(YLOG_DEBUG, "mf_close(%s)", mf->name);
435 for (i = 0; i < mf->no_files; i++)
437 if (mf->files[i].fd >= 0)
441 fsync(mf->files[i].fd);
443 close(mf->files[i].fd);
444 mf->files[i].fd = -1;
451 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
456 zebra_mutex_lock(&mf->mutex);
457 if ((rd = file_position(mf, no, offset)) < 0)
461 zebra_mutex_unlock(&mf->mutex);
466 yaz_log(YLOG_FATAL, "mf_read2 %s internal error", mf->name);
470 toread = nbytes ? nbytes : mf->blocksize;
471 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
473 yaz_log(YLOG_FATAL|YLOG_ERRNO, "mf_read2: Read failed (%s)",
474 mf->files[mf->cur_file].path);
477 zebra_mutex_unlock(&mf->mutex);
484 int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
491 char tmp[FILENAME_MAX+1];
492 unsigned char dummych = '\xff';
494 zebra_mutex_lock(&mf->mutex);
495 if ((ps = file_position(mf, no, offset)) < 0)
497 yaz_log(YLOG_FATAL, "mf_write: %s error (1)", mf->name);
501 /* file needs to grow */
502 while (ps >= mf->files[mf->cur_file].blocks)
504 mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
506 /* file overflow - allocate new file */
507 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
508 needed > mf->files[mf->cur_file].dir->avail_bytes)
511 if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
514 yaz_log(YLOG_DEBUG, "Capping off file %s at pos " ZINT_FORMAT,
515 mf->files[mf->cur_file].path, nblocks);
516 if ((ps = file_position(mf,
517 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
518 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
520 yaz_log(YLOG_FATAL, "mf_write: %s error (2)",
525 yaz_log(YLOG_DEBUG, "ps = " ZINT_FORMAT, ps);
526 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
528 yaz_log(YLOG_ERRNO|YLOG_FATAL, "mf_write: %s error (3)",
533 mf->files[mf->cur_file].blocks += nblocks;
534 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
535 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
539 yaz_log(YLOG_DEBUG, "Creating new file.");
540 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
541 dp->avail_bytes < needed; dp = dp->next);
544 yaz_log(YLOG_FATAL, "mf_write: %s error (4) no more space",
546 for (dp = mf->ma->dirs; dp ; dp = dp->next) {
547 yaz_log(YLOG_FATAL,"%s: max=" ZINT_FORMAT
548 " used=" ZINT_FORMAT " available=" ZINT_FORMAT,
549 dp->name, (zint)dp->max_bytes,
550 (zint)(dp->max_bytes - dp->avail_bytes), (zint)dp->avail_bytes );
552 yaz_log(YLOG_FATAL,"Adjust the limits in your zebra.cfg");
556 mf->files[mf->cur_file].top = (mf->cur_file ?
557 mf->files[mf->cur_file-1].top : -1) +
558 mf->files[mf->cur_file].blocks;
559 mf->files[++(mf->cur_file)].top = -1;
560 mf->files[mf->cur_file].dir = dp;
561 mf->files[mf->cur_file].number =
562 mf->files[mf->cur_file-1].number + 1;
563 mf->files[mf->cur_file].blocks = 0;
564 mf->files[mf->cur_file].bytes = 0;
565 mf->files[mf->cur_file].fd = -1;
566 sprintf(tmp, "%s/%s-" ZINT_FORMAT ".mf", dp->name, mf->name,
567 mf->files[mf->cur_file].number);
568 mf->files[mf->cur_file].path = xstrdup(tmp);
570 /* open new file and position at beginning */
571 if ((ps = file_position(mf, no, offset)) < 0)
573 yaz_log(YLOG_FATAL, "mf_write: %s error (5)", mf->name);
580 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
581 mf->files[mf->cur_file].blocks += nblocks;
582 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
583 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
584 mf->files[mf->cur_file].dir->avail_bytes -=
585 nblocks * mf->blocksize;
588 towrite = nbytes ? nbytes : mf->blocksize;
589 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
591 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Write failed for file %s part %d",
592 mf->name, mf->cur_file);
596 zebra_mutex_unlock(&mf->mutex);
600 /** \brief metafile area statistics
601 \param ma metafile area handle
602 \param no area number (0=first, 1=second, ..)
603 \param directory holds directory upon completion (if non-NULL)
604 \param used_bytes holds used bytes upon completion (if non-NULL)
605 \param max_bytes holds max size bytes upon completion (if non-NULL)
606 \retval 0 area number does not exist
607 \retval 1 area number exists (and directory,used_bytes,.. are set)
609 int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
610 double *used_bytes, double *max_bytes)
613 mf_dir *d = ma->dirs;
614 for (i = 0; d && i<no; i++, d = d->next)
619 *directory = d->name;
622 /* possible loss of data. But it's just statistics and lies */
623 *max_bytes = (double) d->max_bytes;
627 /* possible loss of data. But it's just statistics and lies */
628 *used_bytes = (double) (d->max_bytes - d->avail_bytes);
635 * indent-tabs-mode: nil
637 * vim: shiftwidth=4 tabstop=8 expandtab