1 /* $Id: bfile.c,v 1.40 2005-03-30 09:25:23 adam Exp $
2 Copyright (C) 1995-2005
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
33 #include <idzebra/util.h>
34 #include <idzebra/bfile.h>
41 Zebra_lock_rdwr rdwr_lock;
42 struct CFile_struct *cf;
53 struct BFiles_struct {
54 MFile_area commit_area;
55 MFile_area_struct *register_area;
60 BFiles bfs_create (const char *spec, const char *base)
62 BFiles bfs = (BFiles) xmalloc (sizeof(*bfs));
63 bfs->commit_area = NULL;
67 bfs->base = xstrdup (base);
68 bfs->register_area = mf_init("register", spec, base);
69 if (!bfs->register_area)
77 void bfs_destroy (BFiles bfs)
81 xfree (bfs->cache_fname);
83 mf_destroy (bfs->commit_area);
84 mf_destroy (bfs->register_area);
88 static FILE *open_cache (BFiles bfs, const char *flags)
92 file = fopen (bfs->cache_fname, flags);
96 static void unlink_cache (BFiles bfs)
98 unlink (bfs->cache_fname);
101 void bf_cache (BFiles bfs, const char *spec)
105 yaz_log (YLOG_LOG, "enabling cache spec=%s", spec);
106 if (!bfs->commit_area)
107 bfs->commit_area = mf_init ("shadow", spec, bfs->base);
108 if (bfs->commit_area)
110 bfs->cache_fname = xmalloc (strlen(bfs->commit_area->dirs->name)+
112 strcpy (bfs->cache_fname, bfs->commit_area->dirs->name);
113 strcat (bfs->cache_fname, "/cache");
114 yaz_log (YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
118 bfs->commit_area = NULL;
121 int bf_close (BFile bf)
123 zebra_lock_rdwr_destroy (&bf->rdwr_lock);
127 xfree(bf->alloc_buf);
133 #define HEADER_SIZE 256
135 BFile bf_xopen(BFiles bfs, const char *name, int block_size, int wrflag,
136 const char *magic, int *read_version,
137 const char **more_info)
144 BFile bf = bf_open(bfs, name, block_size, wrflag);
148 /* HEADER_SIZE is considered enough for our header */
149 if (bf->alloc_buf_size < HEADER_SIZE)
150 bf->alloc_buf_size = HEADER_SIZE;
152 bf->alloc_buf_size = bf->block_size;
154 hbuf = bf->alloc_buf = xmalloc(bf->alloc_buf_size);
156 /* fill-in default values */
158 bf->root_block = bf->last_block = HEADER_SIZE / bf->block_size + 1;
159 bf->magic = xstrdup(magic);
161 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
164 bf->header_dirty = 1;
167 while(hbuf[pos * bf->block_size + i] != '\0')
169 if (i == bf->block_size)
170 { /* end of block at pos reached .. */
171 if (bf->alloc_buf_size < (pos+1) * bf->block_size)
173 /* not room for all in alloc_buf_size */
174 yaz_log(YLOG_WARN, "bad header for %s (3)", magic);
178 /* read next block in header */
180 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
182 yaz_log(YLOG_WARN, "missing header block %s (4)", magic);
186 i = 0; /* pos within block is reset */
191 if (sscanf(hbuf, "%39s %d " ZINT_FORMAT " " ZINT_FORMAT "%n",
192 read_magic, read_version, &bf->last_block,
193 &bf->free_list, &l) < 4 && l) /* if %n is counted, it's 5 */
195 yaz_log(YLOG_WARN, "bad header for %s (1)", magic);
199 if (strcmp(read_magic, magic))
201 yaz_log(YLOG_WARN, "bad header for %s (2)", magic);
206 *more_info = hbuf + l;
210 int bf_xclose (BFile bf, int version, const char *more_info)
212 if (bf->header_dirty)
214 assert(bf->alloc_buf);
217 sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ",
218 bf->magic, version, bf->last_block, bf->free_list);
220 strcat(bf->alloc_buf, more_info);
223 bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
225 if (pos * bf->block_size > strlen(bf->alloc_buf))
232 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
234 BFile bf = (BFile) xmalloc(sizeof(struct BFile_struct));
238 bf->block_size = block_size;
239 bf->header_dirty = 0;
240 if (bfs->commit_area)
244 bf->mf = mf_open (bfs->register_area, name, block_size, 0);
245 bf->cf = cf_open (bf->mf, bfs->commit_area, name, block_size,
251 outf = open_cache(bfs, "ab");
254 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
257 fprintf(outf, "%s %d\n", name, block_size);
263 bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
268 yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
272 zebra_lock_rdwr_init(&bf->rdwr_lock);
276 int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
280 zebra_lock_rdwr_rlock (&bf->rdwr_lock);
283 if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
284 r = mf_read (bf->mf, no, offset, nbytes, buf);
287 r = mf_read (bf->mf, no, offset, nbytes, buf);
288 zebra_lock_rdwr_runlock (&bf->rdwr_lock);
292 int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
295 zebra_lock_rdwr_wlock (&bf->rdwr_lock);
297 r = cf_write (bf->cf, no, offset, nbytes, buf);
299 r = mf_write (bf->mf, no, offset, nbytes, buf);
300 zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
304 int bf_commitExists (BFiles bfs)
308 inf = open_cache (bfs, "rb");
317 void bf_reset (BFiles bfs)
321 mf_reset (bfs->commit_area);
322 mf_reset (bfs->register_area);
325 void bf_commitExec (BFiles bfs)
334 assert (bfs->commit_area);
335 if (!(inf = open_cache (bfs, "rb")))
337 yaz_log (YLOG_LOG, "No commit file");
340 while (fscanf (inf, "%s %d", path, &block_size) == 2)
342 mf = mf_open (bfs->register_area, path, block_size, 1);
343 cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
353 void bf_commitClean (BFiles bfs, const char *spec)
363 if (!bfs->commit_area)
365 bf_cache (bfs, spec);
369 if (!(inf = open_cache (bfs, "rb")))
371 while (fscanf (inf, "%s %d", path, &block_size) == 2)
373 mf = mf_open (bfs->register_area, path, block_size, 0);
374 cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);
385 int bf_alloc(BFile bf, int no, zint *blocks)
388 assert(bf->alloc_buf);
389 bf->header_dirty = 1;
390 for (i = 0; i < no; i++)
393 blocks[i] = bf->last_block++;
397 const char *cp = buf;
398 memset(buf, '\0', sizeof(buf));
400 blocks[i] = bf->free_list;
401 if (!bf_read(bf, bf->free_list, 0, sizeof(buf), buf))
403 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
407 zebra_zint_decode(&cp, &bf->free_list);
413 int bf_free(BFile bf, int no, const zint *blocks)
416 assert(bf->alloc_buf);
417 bf->header_dirty = 1;
418 for (i = 0; i < no; i++)
422 memset(buf, '\0', sizeof(buf));
423 zebra_zint_encode(&cp, bf->free_list);
424 bf->free_list = blocks[i];
425 bf_write(bf, bf->free_list, 0, sizeof(buf), buf);