1 /* $Id: bfile.c,v 1.38 2004-12-08 12:23:08 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
34 #include <idzebra/bfile.h>
41 Zebra_lock_rdwr rdwr_lock;
42 struct CFile_struct *cf;
45 struct BFiles_struct {
46 MFile_area commit_area;
47 MFile_area_struct *register_area;
52 BFiles bfs_create (const char *spec, const char *base)
54 BFiles bfs = (BFiles) xmalloc (sizeof(*bfs));
55 bfs->commit_area = NULL;
59 bfs->base = xstrdup (base);
60 bfs->register_area = mf_init("register", spec, base);
61 if (!bfs->register_area)
69 void bfs_destroy (BFiles bfs)
73 xfree (bfs->cache_fname);
75 mf_destroy (bfs->commit_area);
76 mf_destroy (bfs->register_area);
80 static FILE *open_cache (BFiles bfs, const char *flags)
84 file = fopen (bfs->cache_fname, flags);
88 static void unlink_cache (BFiles bfs)
90 unlink (bfs->cache_fname);
93 void bf_cache (BFiles bfs, const char *spec)
97 yaz_log (YLOG_LOG, "enabling cache spec=%s", spec);
98 if (!bfs->commit_area)
99 bfs->commit_area = mf_init ("shadow", spec, bfs->base);
100 if (bfs->commit_area)
102 bfs->cache_fname = xmalloc (strlen(bfs->commit_area->dirs->name)+
104 strcpy (bfs->cache_fname, bfs->commit_area->dirs->name);
105 strcat (bfs->cache_fname, "/cache");
106 yaz_log (YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
110 bfs->commit_area = NULL;
113 int bf_close (BFile bf)
115 zebra_lock_rdwr_destroy (&bf->rdwr_lock);
123 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
125 BFile tmp = (BFile) xmalloc(sizeof(struct BFile_struct));
127 if (bfs->commit_area)
131 tmp->mf = mf_open (bfs->register_area, name, block_size, 0);
132 tmp->cf = cf_open (tmp->mf, bfs->commit_area, name, block_size,
138 outf = open_cache (bfs, "ab");
141 yaz_log (YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
144 fprintf (outf, "%s %d\n", name, block_size);
150 tmp->mf = mf_open(bfs->register_area, name, block_size, wflag);
155 yaz_log (YLOG_FATAL, "mf_open failed for %s", name);
159 zebra_lock_rdwr_init (&tmp->rdwr_lock);
163 int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
167 zebra_lock_rdwr_rlock (&bf->rdwr_lock);
170 if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
171 r = mf_read (bf->mf, no, offset, nbytes, buf);
174 r = mf_read (bf->mf, no, offset, nbytes, buf);
175 zebra_lock_rdwr_runlock (&bf->rdwr_lock);
179 int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
182 zebra_lock_rdwr_wlock (&bf->rdwr_lock);
184 r = cf_write (bf->cf, no, offset, nbytes, buf);
186 r = mf_write (bf->mf, no, offset, nbytes, buf);
187 zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
191 int bf_commitExists (BFiles bfs)
195 inf = open_cache (bfs, "rb");
204 void bf_reset (BFiles bfs)
208 mf_reset (bfs->commit_area);
209 mf_reset (bfs->register_area);
212 void bf_commitExec (BFiles bfs)
221 assert (bfs->commit_area);
222 if (!(inf = open_cache (bfs, "rb")))
224 yaz_log (YLOG_LOG, "No commit file");
227 while (fscanf (inf, "%s %d", path, &block_size) == 2)
229 mf = mf_open (bfs->register_area, path, block_size, 1);
230 cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
240 void bf_commitClean (BFiles bfs, const char *spec)
250 if (!bfs->commit_area)
252 bf_cache (bfs, spec);
256 if (!(inf = open_cache (bfs, "rb")))
258 while (fscanf (inf, "%s %d", path, &block_size) == 2)
260 mf = mf_open (bfs->register_area, path, block_size, 0);
261 cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);