1 /* $Id: bfile.c,v 1.54 2007-01-15 15:10:13 adam 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
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 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, 0);
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 if (bfs->cache_fname)
102 unlink(bfs->cache_fname);
105 ZEBRA_RES bf_cache(BFiles bfs, const char *spec)
109 yaz_log(YLOG_LOG, "enabling shadow spec=%s", spec);
110 if (!bfs->commit_area)
111 bfs->commit_area = mf_init("shadow", spec, bfs->base, 1);
112 if (bfs->commit_area)
114 bfs->cache_fname = xmalloc(strlen(bfs->commit_area->dirs->name)+
116 strcpy(bfs->cache_fname, bfs->commit_area->dirs->name);
117 strcat(bfs->cache_fname, "/cache");
118 yaz_log(YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
122 yaz_log(YLOG_WARN, "shadow could not be enabled");
127 bfs->commit_area = 0;
131 int bf_close2(BFile bf)
134 zebra_lock_rdwr_destroy(&bf->rdwr_lock);
137 if (cf_close(bf->cf))
142 if (mf_close(bf->mf))
145 xfree(bf->alloc_buf);
151 void bf_close(BFile bf)
155 zebra_exit("bf_close");
160 #define HEADER_SIZE 256
162 BFile bf_xopen(BFiles bfs, const char *name, int block_size, int wrflag,
163 const char *magic, int *read_version,
164 const char **more_info)
171 BFile bf = bf_open(bfs, name, block_size, wrflag);
175 /* HEADER_SIZE is considered enough for our header */
176 if (bf->block_size < HEADER_SIZE)
177 bf->alloc_buf_size = HEADER_SIZE;
179 bf->alloc_buf_size = bf->block_size;
181 hbuf = bf->alloc_buf = xmalloc(bf->alloc_buf_size);
183 /* fill-in default values */
185 bf->root_block = bf->last_block = HEADER_SIZE / bf->block_size + 1;
186 bf->magic = xstrdup(magic);
188 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
191 bf->header_dirty = 1;
194 while(hbuf[pos * bf->block_size + i] != '\0')
196 if (i == bf->block_size)
197 { /* end of block at pos reached .. */
198 if (bf->alloc_buf_size < (pos+1) * bf->block_size)
200 /* not room for all in alloc_buf_size */
201 yaz_log(YLOG_WARN, "bad header for %s (3)", magic);
205 /* read next block in header */
207 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
209 yaz_log(YLOG_WARN, "missing header block %s (4)", magic);
213 i = 0; /* pos within block is reset */
218 if (sscanf(hbuf, "%39s %d " ZINT_FORMAT " " ZINT_FORMAT "%n",
219 read_magic, read_version, &bf->last_block,
220 &bf->free_list, &l) < 4 && l) /* if %n is counted, it's 5 */
222 yaz_log(YLOG_WARN, "bad header for %s (1)", magic);
226 if (strcmp(read_magic, magic))
228 yaz_log(YLOG_WARN, "bad header for %s (2)", magic);
235 *more_info = hbuf + l;
239 int bf_xclose(BFile bf, int version, const char *more_info)
241 if (bf->header_dirty)
244 assert(bf->alloc_buf);
246 sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ",
247 bf->magic, version, bf->last_block, bf->free_list);
249 strcat(bf->alloc_buf, more_info);
252 bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
254 if (pos * bf->block_size > strlen(bf->alloc_buf))
258 return bf_close2(bf);
261 BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
263 BFile bf = (BFile) xmalloc(sizeof(*bf));
267 bf->block_size = block_size;
268 bf->header_dirty = 0;
271 zebra_lock_rdwr_init(&bf->rdwr_lock);
273 if (bfs->commit_area)
277 bf->mf = mf_open(bfs->register_area, name, block_size, 0);
278 bf->cf = cf_open(bf->mf, bfs->commit_area, name, block_size,
282 yaz_log(YLOG_FATAL, "cf_open failed for %s", name);
290 outf = open_cache(bfs, "ab");
293 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
297 fprintf(outf, "%s %d\n", name, block_size);
300 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fclose %s", bfs->cache_fname);
308 bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
312 yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
319 int bf_read(BFile bf, zint no, int offset, int nbytes, void *buf)
321 int ret = bf_read2(bf, no, offset, nbytes, buf);
325 zebra_exit("bf_read");
330 int bf_read2(BFile bf, zint no, int offset, int nbytes, void *buf)
334 zebra_lock_rdwr_rlock(&bf->rdwr_lock);
337 if ((ret = cf_read(bf->cf, no, offset, nbytes, buf)) == 0)
338 ret = mf_read(bf->mf, no, offset, nbytes, buf);
341 ret = mf_read(bf->mf, no, offset, nbytes, buf);
342 zebra_lock_rdwr_runlock(&bf->rdwr_lock);
346 int bf_write(BFile bf, zint no, int offset, int nbytes, const void *buf)
348 int ret = bf_write2(bf, no, offset, nbytes, buf);
352 zebra_exit("bf_write");
357 int bf_write2(BFile bf, zint no, int offset, int nbytes, const void *buf)
360 zebra_lock_rdwr_wlock(&bf->rdwr_lock);
362 r = cf_write(bf->cf, no, offset, nbytes, buf);
364 r = mf_write(bf->mf, no, offset, nbytes, buf);
365 zebra_lock_rdwr_wunlock(&bf->rdwr_lock);
369 int bf_commitExists(BFiles bfs)
373 inf = open_cache(bfs, "rb");
382 void bf_reset(BFiles bfs)
386 mf_reset(bfs->commit_area, 1);
387 mf_reset(bfs->register_area, 1);
391 int bf_commitExec(BFiles bfs)
401 assert(bfs->commit_area);
402 if (!(inf = open_cache(bfs, "rb")))
404 yaz_log(YLOG_LOG, "No commit file");
407 while (fscanf(inf, "%s %d", path, &block_size) == 2)
409 mf = mf_open(bfs->register_area, path, block_size, 1);
415 cf = cf_open(mf, bfs->commit_area, path, block_size, 0, &first_time);
435 void bf_commitClean(BFiles bfs, const char *spec)
439 if (!bfs->commit_area)
445 mf_reset(bfs->commit_area, 1);
452 int bf_alloc(BFile bf, int no, zint *blocks)
455 assert(bf->alloc_buf);
456 bf->header_dirty = 1;
457 for (i = 0; i < no; i++)
460 blocks[i] = bf->last_block++;
464 const char *cp = buf;
465 memset(buf, '\0', sizeof(buf));
467 blocks[i] = bf->free_list;
468 if (bf_read(bf, bf->free_list, 0, sizeof(buf), buf) != 1)
470 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
474 zebra_zint_decode(&cp, &bf->free_list);
480 int bf_free(BFile bf, int no, const zint *blocks)
483 assert(bf->alloc_buf);
484 bf->header_dirty = 1;
485 for (i = 0; i < no; i++)
489 memset(buf, '\0', sizeof(buf));
490 zebra_zint_encode(&cp, bf->free_list);
491 bf->free_list = blocks[i];
492 bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
497 int bfs_register_directory_stat(BFiles bfs, int no, const char **directory,
498 double *used_bytes, double *max_bytes)
500 return mf_area_directory_stat(bfs->register_area, no, directory,
501 used_bytes, max_bytes);
505 int bfs_shadow_directory_stat(BFiles bfs, int no, const char **directory,
506 double *used_bytes, double *max_bytes)
508 if (!bfs->commit_area)
510 return mf_area_directory_stat(bfs->commit_area, no, directory,
511 used_bytes, max_bytes);
516 * indent-tabs-mode: nil
518 * vim: shiftwidth=4 tabstop=8 expandtab