1 /* $Id: bfile.c,v 1.36 2004-08-04 08:35:22 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
40 struct BFiles_struct {
41 MFile_area commit_area;
42 MFile_area_struct *register_area;
47 BFiles bfs_create (const char *spec, const char *base)
49 BFiles bfs = (BFiles) xmalloc (sizeof(*bfs));
50 bfs->commit_area = NULL;
54 bfs->base = xstrdup (base);
55 bfs->register_area = mf_init("register", spec, base);
56 if (!bfs->register_area)
64 void bfs_destroy (BFiles bfs)
68 xfree (bfs->cache_fname);
70 mf_destroy (bfs->commit_area);
71 mf_destroy (bfs->register_area);
75 static FILE *open_cache (BFiles bfs, const char *flags)
79 file = fopen (bfs->cache_fname, flags);
83 static void unlink_cache (BFiles bfs)
85 unlink (bfs->cache_fname);
88 void bf_cache (BFiles bfs, const char *spec)
92 yaz_log (LOG_LOG, "enabling cache spec=%s", spec);
93 if (!bfs->commit_area)
94 bfs->commit_area = mf_init ("shadow", spec, bfs->base);
97 bfs->cache_fname = xmalloc (strlen(bfs->commit_area->dirs->name)+
99 strcpy (bfs->cache_fname, bfs->commit_area->dirs->name);
100 strcat (bfs->cache_fname, "/cache");
101 yaz_log (LOG_LOG, "cache_fname = %s", bfs->cache_fname);
105 bfs->commit_area = NULL;
108 int bf_close (BFile bf)
110 zebra_lock_rdwr_destroy (&bf->rdwr_lock);
118 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
120 BFile tmp = (BFile) xmalloc(sizeof(BFile_struct));
122 if (bfs->commit_area)
126 tmp->mf = mf_open (bfs->register_area, name, block_size, 0);
127 tmp->cf = cf_open (tmp->mf, bfs->commit_area, name, block_size,
133 outf = open_cache (bfs, "ab");
136 logf (LOG_FATAL|LOG_ERRNO, "open %s", bfs->cache_fname);
139 fprintf (outf, "%s %d\n", name, block_size);
145 tmp->mf = mf_open(bfs->register_area, name, block_size, wflag);
150 logf (LOG_FATAL, "mf_open failed for %s", name);
154 zebra_lock_rdwr_init (&tmp->rdwr_lock);
158 int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
162 zebra_lock_rdwr_rlock (&bf->rdwr_lock);
165 if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
166 r = mf_read (bf->mf, no, offset, nbytes, buf);
169 r = mf_read (bf->mf, no, offset, nbytes, buf);
170 zebra_lock_rdwr_runlock (&bf->rdwr_lock);
174 int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
177 zebra_lock_rdwr_wlock (&bf->rdwr_lock);
179 r = cf_write (bf->cf, no, offset, nbytes, buf);
181 r = mf_write (bf->mf, no, offset, nbytes, buf);
182 zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
186 int bf_commitExists (BFiles bfs)
190 inf = open_cache (bfs, "rb");
199 void bf_reset (BFiles bfs)
203 mf_reset (bfs->commit_area);
204 mf_reset (bfs->register_area);
207 void bf_commitExec (BFiles bfs)
216 assert (bfs->commit_area);
217 if (!(inf = open_cache (bfs, "rb")))
219 logf (LOG_LOG, "No commit file");
222 while (fscanf (inf, "%s %d", path, &block_size) == 2)
224 mf = mf_open (bfs->register_area, path, block_size, 1);
225 cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
235 void bf_commitClean (BFiles bfs, const char *spec)
245 if (!bfs->commit_area)
247 bf_cache (bfs, spec);
251 if (!(inf = open_cache (bfs, "rb")))
253 while (fscanf (inf, "%s %d", path, &block_size) == 2)
255 mf = mf_open (bfs->register_area, path, block_size, 0);
256 cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);