1 /* $Id: mfile.c,v 1.52 2003-04-05 12:32:34 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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>
49 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
52 * If no definition is given, use current directory, unlimited.
54 char dirname[FILENAME_MAX+1];
55 mf_dir **dp = &ma->dirs, *dir = *dp;
62 int i = 0, fact = 1, multi;
65 while (*ad == ' ' || *ad == '\t')
69 if (!yaz_is_abspath(ad) && base)
71 strcpy (dirname, base);
77 if (*ad == ':' && strchr ("+-0123456789", ad[1]))
86 logf (LOG_WARN, "Missing colon after path: %s", ad0);
91 logf (LOG_WARN, "Empty path: %s", ad0);
94 while (*ad == ' ' || *ad == '\t')
104 if (*ad < '0' || *ad > '9')
106 logf (LOG_FATAL, "Missing size after path: %s", ad0);
110 while (*ad >= '0' && *ad <= '9')
111 size = size*10 + (*ad++ - '0');
114 case 'B': case 'b': multi = 1; break;
115 case 'K': case 'k': multi = 1024; break;
116 case 'M': case 'm': multi = 1048576; break;
117 case 'G': case 'g': multi = 1073741824; break;
119 logf (LOG_FATAL, "Missing unit: %s", ad0);
122 logf (LOG_FATAL, "Illegal unit: %c in %s", *ad, ad0);
126 *dp = dir = (mf_dir *) xmalloc(sizeof(mf_dir));
128 strcpy(dir->name, dirname);
129 dir->max_bytes = dir->avail_bytes = fact * size * multi;
135 static int file_position(MFile mf, int pos, int offset)
137 int off = 0, c = mf->cur_file, ps;
139 if ((c > 0 && pos <= mf->files[c-1].top) ||
140 (c < mf->no_files -1 && pos > mf->files[c].top))
143 while (c + 1 < mf->no_files && mf->files[c].top < pos)
145 off += mf->files[c].blocks;
148 assert(c < mf->no_files);
151 off = c ? (mf->files[c-1].top + 1) : 0;
152 if (mf->files[c].fd < 0)
154 if ((mf->files[c].fd = open(mf->files[c].path,
156 (O_BINARY|O_RDWR|O_CREAT) :
157 (O_BINARY|O_RDONLY), 0666)) < 0)
159 if (!mf->wr && errno == ENOENT && off == 0)
161 logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
166 if (mfile_seek(mf->files[c].fd, ps * (mfile_off_t) mf->blocksize + offset,
169 logf (LOG_WARN|LOG_ERRNO, "Failed to seek in %s", mf->files[c].path);
170 logf(LOG_WARN, "pos=%d off=%d blocksize=%d offset=%d",
171 pos, off, mf->blocksize, offset);
178 static int cmp_part_file(const void *p1, const void *p2)
180 return ((part_file *)p1)->number - ((part_file *)p2)->number;
184 * Create a new area, cotaining metafiles in directories.
185 * Find the part-files in each directory, and inventory the existing metafiles.
187 MFile_area mf_init(const char *name, const char *spec, const char *base)
189 MFile_area ma = (MFile_area) xmalloc(sizeof(*ma));
192 part_file *part_f = 0;
196 char metaname[FILENAME_MAX+1], tmpnam[FILENAME_MAX+1];
198 logf (LOG_DEBUG, "mf_init(%s)", name);
199 strcpy(ma->name, name);
202 if (scan_areadef(ma, spec, base) < 0)
204 logf (LOG_WARN, "Failed to access description of '%s'", name);
207 /* look at each directory */
208 for (dirp = ma->dirs; dirp; dirp = dirp->next)
210 if (!(dd = opendir(dirp->name)))
212 logf (LOG_WARN|LOG_ERRNO, "Failed to open directory %s",
216 /* look at each file */
217 while ((dent = readdir(dd)))
219 int len = strlen(dent->d_name);
220 const char *cp = strrchr (dent->d_name, '-');
221 if (strchr (".-", *dent->d_name))
223 if (len < 5 || !cp || strcmp (dent->d_name + len - 3, ".mf"))
226 memcpy (metaname, dent->d_name, cp - dent->d_name);
227 metaname[ cp - dent->d_name] = '\0';
229 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
232 if (!strcmp(meta_f->name, metaname))
234 part_f = &meta_f->files[meta_f->no_files++];
241 meta_f = (meta_file *) xmalloc(sizeof(*meta_f));
242 zebra_mutex_init (&meta_f->mutex);
244 meta_f->next = ma->mfiles;
246 meta_f->cur_file = -1;
247 meta_f->unlink_flag = 0;
249 strcpy(meta_f->name, metaname);
250 part_f = &meta_f->files[0];
251 meta_f->no_files = 1;
253 part_f->number = number;
256 sprintf(tmpnam, "%s/%s", dirp->name, dent->d_name);
257 part_f->path = xstrdup(tmpnam);
259 if ((fd = open(part_f->path, O_BINARY|O_RDONLY)) < 0)
261 logf (LOG_FATAL|LOG_ERRNO, "Failed to access %s",
265 if ((part_f->bytes = mfile_seek(fd, 0, SEEK_END)) < 0)
267 logf (LOG_FATAL|LOG_ERRNO, "Failed to seek in %s",
275 if (dirp->max_bytes >= 0)
276 dirp->avail_bytes -= part_f->bytes;
280 for (meta_f = ma->mfiles; meta_f; meta_f = meta_f->next)
282 logf (LOG_DEBUG, "mf_init: %s consists of %d part(s)", meta_f->name,
284 qsort(meta_f->files, meta_f->no_files, sizeof(part_file),
290 void mf_destroy(MFile_area ma)
308 meta_file *m = meta_f;
310 for (i = 0; i<m->no_files; i++)
312 xfree (m->files[i].path);
314 zebra_mutex_destroy (&meta_f->mutex);
315 meta_f = meta_f->next;
321 void mf_reset(MFile_area ma)
331 meta_file *m = meta_f;
334 for (i = 0; i<m->no_files; i++)
336 unlink (m->files[i].path);
337 xfree (m->files[i].path);
339 meta_f = meta_f->next;
347 * If !ma, Use MF_DEFAULT_AREA.
349 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
353 char tmp[FILENAME_MAX+1];
356 logf(LOG_DEBUG, "mf_open(%s bs=%d, %s)", name, block_size,
357 wflag ? "RW" : "RDONLY");
359 for (mnew = ma->mfiles; mnew; mnew = mnew->next)
360 if (!strcmp(name, mnew->name))
369 mnew = (meta_file *) xmalloc(sizeof(*mnew));
370 strcpy(mnew->name, name);
371 /* allocate one, empty file */
372 zebra_mutex_init (&mnew->mutex);
374 mnew->files[0].bytes = 0;
375 mnew->files[0].blocks = 0;
376 mnew->files[0].top = -1;
377 mnew->files[0].number = 0;
378 mnew->files[0].fd = -1;
379 mnew->unlink_flag = 0;
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 logf (LOG_FATAL, "Insufficient space for new mfile.");
388 mnew->files[0].dir = dp;
389 sprintf(tmp, "%s/%s-%d.mf", dp->name, mnew->name, 0);
390 mnew->files[0].path = xstrdup(tmp);
392 mnew->next = ma->mfiles;
397 for (i = 0; i < mnew->no_files; i++)
399 if (mnew->files[i].bytes % block_size)
400 mnew->files[i].bytes += block_size - mnew->files[i].bytes %
402 mnew->files[i].blocks = (int) (mnew->files[i].bytes / block_size);
406 mnew->blocksize = block_size;
407 mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
412 for (i = 0; i < mnew->no_files; i++)
414 mnew->files[i].blocks = (int)(mnew->files[i].bytes / mnew->blocksize);
415 if (i == mnew->no_files - 1)
416 mnew->files[i].top = -1;
419 i ? (mnew->files[i-1].top + mnew->files[i].blocks)
420 : (mnew->files[i].blocks - 1);
428 int mf_close(MFile mf)
432 logf (LOG_DEBUG, "mf_close(%s)", mf->name);
434 for (i = 0; i < mf->no_files; i++)
436 if (mf->files[i].fd >= 0)
439 fsync(mf->files[i].fd);
441 close(mf->files[i].fd);
442 mf->files[i].fd = -1;
445 unlink(mf->files[i].path);
452 * Read one block from a metafile. Interface mirrors bfile.
454 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
458 zebra_mutex_lock (&mf->mutex);
459 if ((rd = file_position(mf, no, offset)) < 0)
463 zebra_mutex_unlock (&mf->mutex);
468 yaz_log (LOG_FATAL, "mf_read %s internal error", mf->name);
472 toread = nbytes ? nbytes : mf->blocksize;
473 if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
475 logf (LOG_FATAL|LOG_ERRNO, "mf_read: Read failed (%s)",
476 mf->files[mf->cur_file].path);
479 zebra_mutex_unlock (&mf->mutex);
489 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
491 int ps, nblocks, towrite;
493 char tmp[FILENAME_MAX+1];
494 unsigned char dummych = '\xff';
496 zebra_mutex_lock (&mf->mutex);
497 if ((ps = file_position(mf, no, offset)) < 0)
499 yaz_log (LOG_FATAL, "mf_write %s internal error (1)", mf->name);
502 /* file needs to grow */
503 while (ps >= mf->files[mf->cur_file].blocks)
505 mfile_off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
507 /* file overflow - allocate new file */
508 if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
509 needed > mf->files[mf->cur_file].dir->avail_bytes)
512 if ((nblocks = (int) (mf->files[mf->cur_file].dir->avail_bytes /
515 logf (LOG_DEBUG, "Capping off file %s at pos %d",
516 mf->files[mf->cur_file].path, nblocks);
517 if ((ps = file_position(mf,
518 (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
519 mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
521 yaz_log (LOG_FATAL, "mf_write %s internal error (2)",
525 logf (LOG_DEBUG, "ps = %d", ps);
526 if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
528 logf (LOG_ERRNO|LOG_FATAL, "mf_write %s internal error (3)",
532 mf->files[mf->cur_file].blocks += nblocks;
533 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
534 mf->files[mf->cur_file].dir->avail_bytes -= nblocks *
538 logf (LOG_DEBUG, "Creating new file.");
539 for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
540 dp->avail_bytes < needed; dp = dp->next);
543 logf (LOG_FATAL, "Cannot allocate more space for %s",
547 mf->files[mf->cur_file].top = (mf->cur_file ?
548 mf->files[mf->cur_file-1].top : -1) +
549 mf->files[mf->cur_file].blocks;
550 mf->files[++(mf->cur_file)].top = -1;
551 mf->files[mf->cur_file].dir = dp;
552 mf->files[mf->cur_file].number =
553 mf->files[mf->cur_file-1].number + 1;
554 mf->files[mf->cur_file].blocks = 0;
555 mf->files[mf->cur_file].bytes = 0;
556 mf->files[mf->cur_file].fd = -1;
557 sprintf(tmp, "%s/%s-%d.mf", dp->name, mf->name,
558 mf->files[mf->cur_file].number);
559 mf->files[mf->cur_file].path = xstrdup(tmp);
561 /* open new file and position at beginning */
562 if ((ps = file_position(mf, no, offset)) < 0)
564 yaz_log (LOG_FATAL, "mf_write %s internal error (4)",
571 nblocks = ps - mf->files[mf->cur_file].blocks + 1;
572 mf->files[mf->cur_file].blocks += nblocks;
573 mf->files[mf->cur_file].bytes += nblocks * mf->blocksize;
574 if (mf->files[mf->cur_file].dir->max_bytes >= 0)
575 mf->files[mf->cur_file].dir->avail_bytes -=
576 nblocks * mf->blocksize;
579 towrite = nbytes ? nbytes : mf->blocksize;
580 if (write(mf->files[mf->cur_file].fd, buf, towrite) < towrite)
582 logf (LOG_FATAL|LOG_ERRNO, "Write failed for file %s part %d",
583 mf->name, mf->cur_file);
586 zebra_mutex_unlock (&mf->mutex);
591 * Destroy a metafile, unlinking component files. File must be open.
593 int mf_unlink(MFile mf)
600 for (i = 0; i<mf->no_files; i++)
601 unlink(mf->files[i].path);
607 * Unlink the file by name, rather than MFile-handle. File should be closed.
609 int mf_unlink_name(MFile_area ma, const char *name)