1 /* $Id: bfile.c,v 1.51 2006-11-08 22:08:27 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
35 #include <yaz/xmalloc.h>
36 #include <idzebra/util.h>
37 #include <idzebra/bfile.h>
44 Zebra_lock_rdwr rdwr_lock;
45 struct CFile_struct *cf;
56 struct BFiles_struct {
57 MFile_area commit_area;
58 MFile_area_struct *register_area;
63 BFiles bfs_create (const char *spec, const char *base)
65 BFiles bfs = (BFiles) xmalloc(sizeof(*bfs));
70 bfs->base = xstrdup(base);
71 bfs->register_area = mf_init("register", spec, base);
72 if (!bfs->register_area)
80 void bfs_destroy(BFiles bfs)
84 xfree(bfs->cache_fname);
86 mf_destroy(bfs->commit_area);
87 mf_destroy(bfs->register_area);
91 static FILE *open_cache(BFiles bfs, const char *flags)
95 file = fopen(bfs->cache_fname, flags);
99 static void unlink_cache(BFiles bfs)
101 unlink(bfs->cache_fname);
104 ZEBRA_RES bf_cache(BFiles bfs, const char *spec)
108 yaz_log(YLOG_LOG, "enabling shadow spec=%s", spec);
109 if (!bfs->commit_area)
110 bfs->commit_area = mf_init("shadow", spec, bfs->base);
111 if (bfs->commit_area)
113 bfs->cache_fname = xmalloc(strlen(bfs->commit_area->dirs->name)+
115 strcpy(bfs->cache_fname, bfs->commit_area->dirs->name);
116 strcat(bfs->cache_fname, "/cache");
117 yaz_log(YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
121 yaz_log(YLOG_WARN, "shadow could not be enabled");
126 bfs->commit_area = 0;
130 int bf_close(BFile bf)
132 zebra_lock_rdwr_destroy(&bf->rdwr_lock);
137 xfree(bf->alloc_buf);
143 #define HEADER_SIZE 256
145 BFile bf_xopen(BFiles bfs, const char *name, int block_size, int wrflag,
146 const char *magic, int *read_version,
147 const char **more_info)
154 BFile bf = bf_open(bfs, name, block_size, wrflag);
158 /* HEADER_SIZE is considered enough for our header */
159 if (bf->block_size < HEADER_SIZE)
160 bf->alloc_buf_size = HEADER_SIZE;
162 bf->alloc_buf_size = bf->block_size;
164 hbuf = bf->alloc_buf = xmalloc(bf->alloc_buf_size);
166 /* fill-in default values */
168 bf->root_block = bf->last_block = HEADER_SIZE / bf->block_size + 1;
169 bf->magic = xstrdup(magic);
171 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
174 bf->header_dirty = 1;
177 while(hbuf[pos * bf->block_size + i] != '\0')
179 if (i == bf->block_size)
180 { /* end of block at pos reached .. */
181 if (bf->alloc_buf_size < (pos+1) * bf->block_size)
183 /* not room for all in alloc_buf_size */
184 yaz_log(YLOG_WARN, "bad header for %s (3)", magic);
188 /* read next block in header */
190 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
192 yaz_log(YLOG_WARN, "missing header block %s (4)", magic);
196 i = 0; /* pos within block is reset */
201 if (sscanf(hbuf, "%39s %d " ZINT_FORMAT " " ZINT_FORMAT "%n",
202 read_magic, read_version, &bf->last_block,
203 &bf->free_list, &l) < 4 && l) /* if %n is counted, it's 5 */
205 yaz_log(YLOG_WARN, "bad header for %s (1)", magic);
209 if (strcmp(read_magic, magic))
211 yaz_log(YLOG_WARN, "bad header for %s (2)", magic);
218 *more_info = hbuf + l;
222 int bf_xclose(BFile bf, int version, const char *more_info)
224 if (bf->header_dirty)
227 assert(bf->alloc_buf);
229 sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ",
230 bf->magic, version, bf->last_block, bf->free_list);
232 strcat(bf->alloc_buf, more_info);
235 bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
237 if (pos * bf->block_size > strlen(bf->alloc_buf))
244 BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
246 BFile bf = (BFile) xmalloc(sizeof(*bf));
250 bf->block_size = block_size;
251 bf->header_dirty = 0;
254 zebra_lock_rdwr_init(&bf->rdwr_lock);
256 if (bfs->commit_area)
260 bf->mf = mf_open(bfs->register_area, name, block_size, 0);
261 bf->cf = cf_open(bf->mf, bfs->commit_area, name, block_size,
265 yaz_log(YLOG_FATAL, "cf_open failed for %s", name);
273 outf = open_cache(bfs, "ab");
276 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
280 fprintf(outf, "%s %d\n", name, block_size);
283 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fclose %s", bfs->cache_fname);
291 bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
295 yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
302 int bf_read(BFile bf, zint no, int offset, int nbytes, void *buf)
304 int ret = bf_read2(bf, no, offset, nbytes, buf);
313 int bf_read2(BFile bf, zint no, int offset, int nbytes, void *buf)
317 zebra_lock_rdwr_rlock(&bf->rdwr_lock);
320 if ((ret = cf_read(bf->cf, no, offset, nbytes, buf)) == 0)
321 ret = mf_read(bf->mf, no, offset, nbytes, buf);
324 ret = mf_read(bf->mf, no, offset, nbytes, buf);
325 zebra_lock_rdwr_runlock(&bf->rdwr_lock);
329 int bf_write(BFile bf, zint no, int offset, int nbytes, const void *buf)
331 int ret = bf_write2(bf, no, offset, nbytes, buf);
340 int bf_write2(BFile bf, zint no, int offset, int nbytes, const void *buf)
343 zebra_lock_rdwr_wlock(&bf->rdwr_lock);
345 r = cf_write(bf->cf, no, offset, nbytes, buf);
347 r = mf_write(bf->mf, no, offset, nbytes, buf);
348 zebra_lock_rdwr_wunlock(&bf->rdwr_lock);
352 int bf_commitExists(BFiles bfs)
356 inf = open_cache(bfs, "rb");
365 void bf_reset(BFiles bfs)
369 mf_reset(bfs->commit_area, 1);
370 mf_reset(bfs->register_area, 1);
374 void bf_commitExec(BFiles bfs)
383 assert(bfs->commit_area);
384 if (!(inf = open_cache(bfs, "rb")))
386 yaz_log(YLOG_LOG, "No commit file");
389 while (fscanf(inf, "%s %d", path, &block_size) == 2)
391 mf = mf_open(bfs->register_area, path, block_size, 1);
392 cf = cf_open(mf, bfs->commit_area, path, block_size, 0, &first_time);
402 void bf_commitClean(BFiles bfs, const char *spec)
406 if (!bfs->commit_area)
412 mf_reset(bfs->commit_area, 1);
419 int bf_alloc(BFile bf, int no, zint *blocks)
422 assert(bf->alloc_buf);
423 bf->header_dirty = 1;
424 for (i = 0; i < no; i++)
427 blocks[i] = bf->last_block++;
431 const char *cp = buf;
432 memset(buf, '\0', sizeof(buf));
434 blocks[i] = bf->free_list;
435 if (!bf_read(bf, bf->free_list, 0, sizeof(buf), buf))
437 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
441 zebra_zint_decode(&cp, &bf->free_list);
447 int bf_free(BFile bf, int no, const zint *blocks)
450 assert(bf->alloc_buf);
451 bf->header_dirty = 1;
452 for (i = 0; i < no; i++)
456 memset(buf, '\0', sizeof(buf));
457 zebra_zint_encode(&cp, bf->free_list);
458 bf->free_list = blocks[i];
459 bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
464 int bfs_register_directory_stat(BFiles bfs, int no, const char **directory,
465 double *used_bytes, double *max_bytes)
467 return mf_area_directory_stat(bfs->register_area, no, directory,
468 used_bytes, max_bytes);
472 int bfs_shadow_directory_stat(BFiles bfs, int no, const char **directory,
473 double *used_bytes, double *max_bytes)
475 if (!bfs->commit_area)
477 return mf_area_directory_stat(bfs->commit_area, no, directory,
478 used_bytes, max_bytes);
483 * indent-tabs-mode: nil
485 * vim: shiftwidth=4 tabstop=8 expandtab