1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <sys/types.h>
35 #include <zebra-lock.h>
36 #include <idzebra/util.h>
37 #include <yaz/yaz-util.h>
40 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
43 * If no definition is given, use current directory, unlimited.
45 char dirname[FILENAME_MAX+1];
46 mf_dir **dp = &ma->dirs, *dir = *dp;
53 int i = 0, fact = 1, multi;
56 while (*ad == ' ' || *ad == '\t')
60 if (!yaz_is_abspath(ad) && base)
62 strcpy(dirname, base);
68 if (*ad == ':' && strchr("+-0123456789", ad[1]))
77 yaz_log(YLOG_WARN, "Missing colon after path: %s", ad0);
82 yaz_log(YLOG_WARN, "Empty path: %s", ad0);
85 while (*ad == ' ' || *ad == '\t')
95 if (*ad < '0' || *ad > '9')
97 yaz_log(YLOG_FATAL, "Missing size after path: %s", ad0);
101 while (*ad >= '0' && *ad <= '9')
102 size = size*10 + (*ad++ - '0');
105 case 'B': case 'b': multi = 1; break;
106 case 'K': case 'k': multi = 1024; break;
107 case 'M': case 'm': multi = 1048576; break;
108 case 'G': case 'g': multi = 1073741824; break;
110 yaz_log(YLOG_FATAL, "Missing unit: %s", ad0);
113 yaz_log(YLOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
117 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
119 strcpy(dir->name, dirname);
120 dir->max_bytes = dir->avail_bytes = fact * size * multi;
126 /** \brief position within metafile (perform seek)
127 \param mf metafile handle
128 \param pos block position
129 \param offset offset within block
132 \retval -2 OK, but file does not created (read-only)
135 static zint file_position(MFile mf, zint pos, int offset)
138 int c = mf->cur_file;
140 if ((c > 0 && pos <= mf->files[c-1].top) ||
141 (c < mf->no_files -1 && pos > mf->files[c].top))
144 while (c + 1 < mf->no_files && mf->files[c].top < pos)
146 off += mf->files[c].blocks;
149 assert(c < mf->no_files);
152 off = c ? (mf->files[c-1].top + 1) : 0;
153 if (mf->files[c].fd < 0)
155 if ((mf->files[c].fd = open(mf->files[c].path,
157 (O_BINARY|O_RDWR|O_CREAT) :
158 (O_BINARY|O_RDONLY), 0666)) < 0)
160 if (!mf->wr && errno == ENOENT && off == 0)
162 /* we can't open it for reading. But not really an error */
165 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to open %s", mf->files[c].path);
170 if (mfile_seek(mf->files[c].fd, ps *(mfile_off_t) mf->blocksize + offset,
173 yaz_log(YLOG_WARN|YLOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
174 yaz_log(YLOG_WARN, "pos=" ZINT_FORMAT " off=" ZINT_FORMAT " blocksize=%d offset=%d",
175 pos, off, mf->blocksize, offset);
182 static int cmp_part_file(const void *p1, const void *p2)
184 zint d = ((part_file *)p1)->number - ((part_file *)p2)->number;
192 MFile_area mf_init(const char *name, const char *spec, const char *base,
193 int only_shadow_files)
195 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
198 part_file *part_f = 0;
202 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
204 yaz_log(YLOG_DEBUG, "mf_init(%s)", name);
205 strcpy(ma->name, name);
208 if (scan_areadef(ma, spec, base) < 0)
210 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",
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 /* only files such as file-i-0.mf and file-i-b-0.mf, bug #739 */
238 if (only_shadow_files && cp[-2] != '-')
240 if (!only_shadow_files && cp[-2] == '-')
242 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
245 if (!strcmp(meta_f->name, metaname))
247 part_f = &meta_f->files[meta_f->no_files++];
254 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
255 zebra_mutex_init(&meta_f->mutex);
257 meta_f->next = ma->mfiles;
259 meta_f->cur_file = -1;
261 strcpy(meta_f->name, metaname);
262 part_f = &meta_f->files[0];
263 meta_f->no_files = 1;
265 part_f->number = number;
268 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
269 part_f->path = xstrdup(tmpnam);
271 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
273 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to access %s",
279 if ((part_f->bytes = mfile_seek(fd, 0, SEEK_END)) < 0)
281 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to seek in %s",
289 if (dirp->max_bytes >= 0)
290 dirp->avail_bytes -= part_f->bytes;
294 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
296 yaz_log(YLOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
298 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
304 void mf_destroy(MFile_area ma)
321 void mf_reset(MFile_area ma, int unlink_flag)
331 meta_file *m = meta_f;
333 meta_f = meta_f->next;
336 for (i = 0; i<m->no_files; i++)
339 unlink(m->files[i].path);
340 xfree(m->files[i].path);
342 zebra_mutex_destroy(&m->mutex);
348 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
352 char tmp[FILENAME_MAX+1];
355 yaz_log(YLOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
356 wflag ? "RW" : "RDONLY");
358 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
359 if (!strcmp(name, mnew->name))
363 yaz_log(YLOG_WARN, "metafile %s already open", name);
370 mnew = (meta_file *) xmalloc(sizeof(*mnew));
371 strcpy(mnew->name, name);
372 /* allocate one, empty file */
373 zebra_mutex_init(&mnew->mutex);
375 mnew->files[0].bytes = 0;
376 mnew->files[0].blocks = 0;
377 mnew->files[0].top = -1;
378 mnew->files[0].number = 0;
379 mnew->files[0].fd = -1;
380 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
381 for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
382 mnew->min_bytes_creat; dp = dp->next);
385 yaz_log(YLOG_FATAL, "Insufficient space for file %s", name);
389 mnew->files[0].dir = dp;
390 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
391 mnew->files[0].path = xstrdup(tmp);
393 mnew->next = ma->mfiles;
398 for (i = 0; i < mnew->no_files; i++)
400 if (mnew->files[i].bytes % block_size)
401 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
403 mnew->files[i].blocks = (int) (mnew->files[i].bytes / block_size);
407 mnew->blocksize = block_size;
408 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
413 for (i = 0; i < mnew->no_files; i++)
415 mnew->files[i].blocks = (int)(mnew->files[i].bytes / mnew->blocksize);
416 if (i == mnew->no_files - 1)
417 mnew->files[i].top = -1;
420 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
421 : (mnew->files[i].blocks - 1);
426 int mf_close(MFile mf)
430 yaz_log(YLOG_DEBUG, "mf_close(%s)", mf->name);
432 for (i = 0; i < mf->no_files; i++)
434 if (mf->files[i].fd >= 0)
438 fsync(mf->files[i].fd);
440 close(mf->files[i].fd);
441 mf->files[i].fd = -1;
448 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
453 zebra_mutex_lock(&mf->mutex);
454 if ((rd = file_position(mf, no, offset)) < 0)
458 zebra_mutex_unlock(&mf->mutex);
463 yaz_log(YLOG_FATAL, "mf_read2 %s internal error", mf->name);
467 toread = nbytes ? nbytes : mf->blocksize;
468 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
470 yaz_log(YLOG_FATAL|YLOG_ERRNO, "mf_read2: Read failed (%s)",
471 mf->files[mf->cur_file].path);
474 zebra_mutex_unlock(&mf->mutex);
481 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 error (1)", mf->name);
498 /* file needs to grow */
499 while (ps >= mf->files[mf->cur_file].blocks)
501 mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
503 /* file overflow - allocate new file */
504 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
505 needed > mf->files[mf->cur_file].dir->avail_bytes)
508 if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
511 yaz_log(YLOG_DEBUG, "Capping off file %s at pos " ZINT_FORMAT,
512 mf->files[mf->cur_file].path, nblocks);
513 if ((ps = file_position(mf,
514 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
515 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
517 yaz_log(YLOG_FATAL, "mf_write: %s error (2)",
522 yaz_log(YLOG_DEBUG, "ps = " ZINT_FORMAT, ps);
523 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
525 yaz_log(YLOG_ERRNO|YLOG_FATAL, "mf_write: %s error (3)",
530 mf->files[mf->cur_file].blocks += nblocks;
531 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
532 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
536 yaz_log(YLOG_DEBUG, "Creating new file.");
537 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
538 dp->avail_bytes < needed; dp = dp->next);
541 yaz_log(YLOG_FATAL, "mf_write: %s error (4) no more space",
543 for (dp = mf->ma->dirs; dp ; dp = dp->next) {
544 yaz_log(YLOG_FATAL,"%s: max=" ZINT_FORMAT
545 " used=" ZINT_FORMAT " available=" ZINT_FORMAT,
546 dp->name, (zint)dp->max_bytes,
547 (zint)(dp->max_bytes - dp->avail_bytes), (zint)dp->avail_bytes );
549 yaz_log(YLOG_FATAL,"Adjust the limits in your zebra.cfg");
553 mf->files[mf->cur_file].top = (mf->cur_file ?
554 mf->files[mf->cur_file-1].top : -1) +
555 mf->files[mf->cur_file].blocks;
556 mf->files[++(mf->cur_file)].top = -1;
557 mf->files[mf->cur_file].dir = dp;
558 mf->files[mf->cur_file].number =
559 mf->files[mf->cur_file-1].number + 1;
560 mf->files[mf->cur_file].blocks = 0;
561 mf->files[mf->cur_file].bytes = 0;
562 mf->files[mf->cur_file].fd = -1;
563 sprintf(tmp, "%s/%s-" ZINT_FORMAT ".mf", dp->name, mf->name,
564 mf->files[mf->cur_file].number);
565 mf->files[mf->cur_file].path = xstrdup(tmp);
567 /* open new file and position at beginning */
568 if ((ps = file_position(mf, no, offset)) < 0)
570 yaz_log(YLOG_FATAL, "mf_write: %s error (5)", mf->name);
577 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
578 mf->files[mf->cur_file].blocks += nblocks;
579 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
580 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
581 mf->files[mf->cur_file].dir->avail_bytes -=
582 nblocks * mf->blocksize;
585 towrite = nbytes ? nbytes : mf->blocksize;
586 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
588 yaz_log(YLOG_FATAL|YLOG_ERRNO, "Write failed for file %s part %d",
589 mf->name, mf->cur_file);
593 zebra_mutex_unlock(&mf->mutex);
597 /** \brief metafile area statistics
598 \param ma metafile area handle
599 \param no area number (0=first, 1=second, ..)
600 \param directory holds directory upon completion (if non-NULL)
601 \param used_bytes holds used bytes upon completion (if non-NULL)
602 \param max_bytes holds max size bytes upon completion (if non-NULL)
603 \retval 0 area number does not exist
604 \retval 1 area number exists (and directory,used_bytes,.. are set)
606 int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
607 double *used_bytes, double *max_bytes)
610 mf_dir *d = ma->dirs;
611 for (i = 0; d && i<no; i++, d = d->next)
616 *directory = d->name;
619 /* possible loss of data. But it's just statistics and lies */
620 *max_bytes = (double) d->max_bytes;
624 /* possible loss of data. But it's just statistics and lies */
625 *used_bytes = (double) (d->max_bytes - d->avail_bytes);
632 * c-file-style: "Stroustrup"
633 * indent-tabs-mode: nil
635 * vim: shiftwidth=4 tabstop=8 expandtab