1 /* $Id: bfile.c,v 1.35 2002-08-02 19:26:55 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
39 struct BFiles_struct {
40 MFile_area commit_area;
41 MFile_area_struct *register_area;
46 BFiles bfs_create (const char *spec, const char *base)
48 BFiles bfs = (BFiles) xmalloc (sizeof(*bfs));
49 bfs->commit_area = NULL;
53 bfs->base = xstrdup (base);
54 bfs->register_area = mf_init("register", spec, base);
55 if (!bfs->register_area)
63 void bfs_destroy (BFiles bfs)
67 xfree (bfs->cache_fname);
69 mf_destroy (bfs->commit_area);
70 mf_destroy (bfs->register_area);
74 static FILE *open_cache (BFiles bfs, const char *flags)
78 file = fopen (bfs->cache_fname, flags);
82 static void unlink_cache (BFiles bfs)
84 unlink (bfs->cache_fname);
87 void bf_cache (BFiles bfs, const char *spec)
91 yaz_log (LOG_LOG, "enabling cache spec=%s", spec);
92 if (!bfs->commit_area)
93 bfs->commit_area = mf_init ("shadow", spec, bfs->base);
96 bfs->cache_fname = xmalloc (strlen(bfs->commit_area->dirs->name)+
98 strcpy (bfs->cache_fname, bfs->commit_area->dirs->name);
99 strcat (bfs->cache_fname, "/cache");
100 yaz_log (LOG_LOG, "cache_fname = %s", bfs->cache_fname);
104 bfs->commit_area = NULL;
107 int bf_close (BFile bf)
109 zebra_lock_rdwr_destroy (&bf->rdwr_lock);
117 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
119 BFile tmp = (BFile) xmalloc(sizeof(BFile_struct));
121 if (bfs->commit_area)
125 tmp->mf = mf_open (bfs->register_area, name, block_size, 0);
126 tmp->cf = cf_open (tmp->mf, bfs->commit_area, name, block_size,
132 outf = open_cache (bfs, "ab");
135 logf (LOG_FATAL|LOG_ERRNO, "open %s", bfs->cache_fname);
138 fprintf (outf, "%s %d\n", name, block_size);
144 tmp->mf = mf_open(bfs->register_area, name, block_size, wflag);
149 logf (LOG_FATAL, "mf_open failed for %s", name);
153 zebra_lock_rdwr_init (&tmp->rdwr_lock);
157 int bf_read (BFile bf, int no, int offset, int nbytes, void *buf)
161 zebra_lock_rdwr_rlock (&bf->rdwr_lock);
164 if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
165 r = mf_read (bf->mf, no, offset, nbytes, buf);
168 r = mf_read (bf->mf, no, offset, nbytes, buf);
169 zebra_lock_rdwr_runlock (&bf->rdwr_lock);
173 int bf_write (BFile bf, int no, int offset, int nbytes, const void *buf)
176 zebra_lock_rdwr_wlock (&bf->rdwr_lock);
178 r = cf_write (bf->cf, no, offset, nbytes, buf);
180 r = mf_write (bf->mf, no, offset, nbytes, buf);
181 zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
185 int bf_commitExists (BFiles bfs)
189 inf = open_cache (bfs, "rb");
198 void bf_reset (BFiles bfs)
202 mf_reset (bfs->commit_area);
203 mf_reset (bfs->register_area);
206 void bf_commitExec (BFiles bfs)
215 assert (bfs->commit_area);
216 if (!(inf = open_cache (bfs, "rb")))
218 logf (LOG_LOG, "No commit file");
221 while (fscanf (inf, "%s %d", path, &block_size) == 2)
223 mf = mf_open (bfs->register_area, path, block_size, 1);
224 cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
234 void bf_commitClean (BFiles bfs, const char *spec)
244 if (!bfs->commit_area)
246 bf_cache (bfs, spec);
250 if (!(inf = open_cache (bfs, "rb")))
252 while (fscanf (inf, "%s %d", path, &block_size) == 2)
254 mf = mf_open (bfs->register_area, path, block_size, 0);
255 cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);