+ BFile bf = (BFile) xmalloc(sizeof(struct BFile_struct));
+
+ bf->alloc_buf = 0;
+ bf->magic = 0;
+ bf->block_size = block_size;
+ bf->header_dirty = 0;
+ if (bfs->commit_area)
+ {
+ int first_time;
+
+ bf->mf = mf_open (bfs->register_area, name, block_size, 0);
+ bf->cf = cf_open (bf->mf, bfs->commit_area, name, block_size,
+ wflag, &first_time);
+ if (first_time)
+ {
+ FILE *outf;
+
+ outf = open_cache(bfs, "ab");
+ if (!outf)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
+ exit(1);
+ }
+ fprintf(outf, "%s %d\n", name, block_size);
+ fclose(outf);
+ }
+ }
+ else
+ {
+ bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
+ bf->cf = NULL;
+ }
+ if (!bf->mf)
+ {
+ yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
+ xfree(bf);
+ return 0;
+ }
+ zebra_lock_rdwr_init(&bf->rdwr_lock);
+ return bf;
+}
+
+int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
+{
+ int r;
+
+ zebra_lock_rdwr_rlock (&bf->rdwr_lock);
+ if (bf->cf)
+ {
+ if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
+ r = mf_read (bf->mf, no, offset, nbytes, buf);
+ }
+ else
+ r = mf_read (bf->mf, no, offset, nbytes, buf);
+ zebra_lock_rdwr_runlock (&bf->rdwr_lock);
+ return r;
+}
+
+int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
+{
+ int r;
+ zebra_lock_rdwr_wlock (&bf->rdwr_lock);
+ if (bf->cf)
+ r = cf_write (bf->cf, no, offset, nbytes, buf);
+ else
+ r = mf_write (bf->mf, no, offset, nbytes, buf);
+ zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
+ return r;
+}
+
+int bf_commitExists (BFiles bfs)
+{
+ FILE *inf;
+
+ inf = open_cache (bfs, "rb");
+ if (inf)
+ {
+ fclose (inf);
+ return 1;
+ }
+ return 0;
+}
+
+void bf_reset (BFiles bfs)
+{
+ if (!bfs)
+ return;
+ mf_reset (bfs->commit_area);
+ mf_reset (bfs->register_area);
+}
+
+void bf_commitExec (BFiles bfs)
+{
+ FILE *inf;
+ int block_size;
+ char path[256];
+ MFile mf;
+ CFile cf;
+ int first_time;
+
+ assert (bfs->commit_area);
+ if (!(inf = open_cache (bfs, "rb")))
+ {
+ yaz_log (YLOG_LOG, "No commit file");
+ return ;
+ }
+ while (fscanf (inf, "%s %d", path, &block_size) == 2)
+ {
+ mf = mf_open (bfs->register_area, path, block_size, 1);
+ cf = cf_open (mf, bfs->commit_area, path, block_size, 0, &first_time);
+
+ cf_commit (cf);
+
+ cf_close (cf);
+ mf_close (mf);
+ }
+ fclose (inf);
+}
+
+void bf_commitClean (BFiles bfs, const char *spec)
+{
+ FILE *inf;
+ int block_size;
+ char path[256];
+ MFile mf;
+ CFile cf;
+ int mustDisable = 0;
+ int firstTime;
+
+ if (!bfs->commit_area)
+ {
+ bf_cache (bfs, spec);
+ mustDisable = 1;
+ }
+
+ if (!(inf = open_cache (bfs, "rb")))
+ return ;
+ while (fscanf (inf, "%s %d", path, &block_size) == 2)
+ {
+ mf = mf_open (bfs->register_area, path, block_size, 0);
+ cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);
+ cf_unlink (cf);
+ cf_close (cf);
+ mf_close (mf);
+ }
+ fclose (inf);
+ unlink_cache (bfs);
+ if (mustDisable)
+ bf_cache (bfs, 0);
+}
+
+int bf_alloc(BFile bf, int no, zint *blocks)
+{
+ int i;
+ assert(bf->alloc_buf);
+ bf->header_dirty = 1;
+ for (i = 0; i < no; i++)
+ {
+ if (!bf->free_list)
+ blocks[i] = bf->last_block++;
+ else
+ {
+ char buf[16];
+ const char *cp = buf;
+ memset(buf, '\0', sizeof(buf));
+
+ blocks[i] = bf->free_list;
+ if (!bf_read(bf, bf->free_list, 0, sizeof(buf), buf))
+ {
+ yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
+ bf->free_list);
+ exit(1);
+ }
+ zebra_zint_decode(&cp, &bf->free_list);
+ }
+ }
+ return 0;
+}
+
+int bf_free(BFile bf, int no, const zint *blocks)
+{
+ int i;
+ assert(bf->alloc_buf);
+ bf->header_dirty = 1;
+ for (i = 0; i < no; i++)
+ {
+ char buf[16];
+ char *cp = buf;
+ memset(buf, '\0', sizeof(buf));
+ zebra_zint_encode(&cp, bf->free_list);
+ bf->free_list = blocks[i];
+ bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
+ }
+ return 0;