1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <yaz/xmalloc.h>
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 register_area;
60 BFiles bfs_create (const char *spec, const char *base)
62 BFiles bfs = (BFiles) xmalloc(sizeof(*bfs));
67 bfs->base = xstrdup(base);
68 bfs->register_area = mf_init("register", spec, base, 0);
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)
99 unlink(bfs->cache_fname);
102 ZEBRA_RES bf_cache(BFiles bfs, const char *spec)
106 yaz_log(YLOG_LOG, "enabling shadow spec=%s", spec);
107 if (!bfs->commit_area)
108 bfs->commit_area = mf_init("shadow", spec, bfs->base, 1);
109 if (bfs->commit_area)
111 bfs->cache_fname = xmalloc(strlen(bfs->commit_area->dirs->name)+
113 strcpy(bfs->cache_fname, bfs->commit_area->dirs->name);
114 strcat(bfs->cache_fname, "/cache");
115 yaz_log(YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
119 yaz_log(YLOG_WARN, "shadow could not be enabled");
124 bfs->commit_area = 0;
128 int bf_close2(BFile bf)
131 zebra_lock_rdwr_destroy(&bf->rdwr_lock);
134 if (cf_close(bf->cf))
139 if (mf_close(bf->mf))
142 xfree(bf->alloc_buf);
148 void bf_close(BFile bf)
152 zebra_exit("bf_close");
157 #define HEADER_SIZE 256
159 BFile bf_xopen(BFiles bfs, const char *name, int block_size, int wrflag,
160 const char *magic, int *read_version,
161 const char **more_info)
168 BFile bf = bf_open(bfs, name, block_size, wrflag);
172 /* HEADER_SIZE is considered enough for our header */
173 if (bf->block_size < HEADER_SIZE)
174 bf->alloc_buf_size = HEADER_SIZE;
176 bf->alloc_buf_size = bf->block_size;
178 hbuf = bf->alloc_buf = xmalloc(bf->alloc_buf_size);
180 /* fill-in default values */
182 bf->root_block = bf->last_block = HEADER_SIZE / bf->block_size + 1;
183 bf->magic = xstrdup(magic);
185 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
188 bf->header_dirty = 1;
191 while(hbuf[pos * bf->block_size + i] != '\0')
193 if (i == bf->block_size)
194 { /* end of block at pos reached .. */
195 if (bf->alloc_buf_size < (pos+1) * bf->block_size)
197 /* not room for all in alloc_buf_size */
198 yaz_log(YLOG_WARN, "bad header for %s (3)", magic);
202 /* read next block in header */
204 if (!bf_read(bf, pos, 0, 0, hbuf + pos * bf->block_size))
206 yaz_log(YLOG_WARN, "missing header block %s (4)", magic);
210 i = 0; /* pos within block is reset */
215 if (sscanf(hbuf, "%39s %d " ZINT_FORMAT " " ZINT_FORMAT "%n",
216 read_magic, read_version, &bf->last_block,
217 &bf->free_list, &l) < 4 && l) /* if %n is counted, it's 5 */
219 yaz_log(YLOG_WARN, "bad header for %s (1)", magic);
223 if (strcmp(read_magic, magic))
225 yaz_log(YLOG_WARN, "bad header for %s (2)", magic);
232 *more_info = hbuf + l;
236 int bf_xclose(BFile bf, int version, const char *more_info)
238 if (bf->header_dirty)
241 assert(bf->alloc_buf);
243 sprintf(bf->alloc_buf, "%s %d " ZINT_FORMAT " " ZINT_FORMAT " ",
244 bf->magic, version, bf->last_block, bf->free_list);
246 strcat(bf->alloc_buf, more_info);
249 bf_write(bf, pos, 0, 0, bf->alloc_buf + pos * bf->block_size);
251 if (pos * bf->block_size > strlen(bf->alloc_buf))
255 return bf_close2(bf);
258 BFile bf_open(BFiles bfs, const char *name, int block_size, int wflag)
260 BFile bf = (BFile) xmalloc(sizeof(*bf));
264 bf->block_size = block_size;
265 bf->header_dirty = 0;
268 zebra_lock_rdwr_init(&bf->rdwr_lock);
270 if (bfs->commit_area)
274 bf->mf = mf_open(bfs->register_area, name, block_size, 0);
275 bf->cf = cf_open(bf->mf, bfs->commit_area, name, block_size,
279 yaz_log(YLOG_FATAL, "cf_open failed for %s", name);
287 outf = open_cache(bfs, "ab");
290 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
294 fprintf(outf, "%s %d\n", name, block_size);
297 yaz_log(YLOG_FATAL|YLOG_ERRNO, "fclose %s", bfs->cache_fname);
305 bf->mf = mf_open(bfs->register_area, name, block_size, wflag);
309 yaz_log(YLOG_FATAL, "mf_open failed for %s", name);
316 int bf_read(BFile bf, zint no, int offset, int nbytes, void *buf)
318 int ret = bf_read2(bf, no, offset, nbytes, buf);
322 zebra_exit("bf_read");
327 int bf_read2(BFile bf, zint no, int offset, int nbytes, void *buf)
331 zebra_lock_rdwr_rlock(&bf->rdwr_lock);
334 if ((ret = cf_read(bf->cf, no, offset, nbytes, buf)) == 0)
335 ret = mf_read(bf->mf, no, offset, nbytes, buf);
338 ret = mf_read(bf->mf, no, offset, nbytes, buf);
339 zebra_lock_rdwr_runlock(&bf->rdwr_lock);
343 int bf_write(BFile bf, zint no, int offset, int nbytes, const void *buf)
345 int ret = bf_write2(bf, no, offset, nbytes, buf);
349 zebra_exit("bf_write");
354 int bf_write2(BFile bf, zint no, int offset, int nbytes, const void *buf)
357 zebra_lock_rdwr_wlock(&bf->rdwr_lock);
359 r = cf_write(bf->cf, no, offset, nbytes, buf);
361 r = mf_write(bf->mf, no, offset, nbytes, buf);
362 zebra_lock_rdwr_wunlock(&bf->rdwr_lock);
366 int bf_commitExists(BFiles bfs)
370 inf = open_cache(bfs, "rb");
379 void bf_reset(BFiles bfs)
383 mf_reset(bfs->commit_area, 1);
384 mf_reset(bfs->register_area, 1);
388 int bf_commitExec(BFiles bfs)
398 assert(bfs->commit_area);
399 if (!(inf = open_cache(bfs, "rb")))
401 yaz_log(YLOG_LOG, "No commit file");
404 while (fscanf(inf, "%s %d", path, &block_size) == 2)
406 mf = mf_open(bfs->register_area, path, block_size, 1);
412 cf = cf_open(mf, bfs->commit_area, path, block_size, 0, &first_time);
432 void bf_commitClean(BFiles bfs, const char *spec)
436 if (!bfs->commit_area)
442 mf_reset(bfs->commit_area, 1);
449 int bf_alloc(BFile bf, int no, zint *blocks)
452 assert(bf->alloc_buf);
453 bf->header_dirty = 1;
454 for (i = 0; i < no; i++)
457 blocks[i] = bf->last_block++;
461 const char *cp = buf;
462 memset(buf, '\0', sizeof(buf));
464 blocks[i] = bf->free_list;
465 if (bf_read(bf, bf->free_list, 0, sizeof(buf), buf) != 1)
467 yaz_log(YLOG_WARN, "Bad freelist entry " ZINT_FORMAT,
471 zebra_zint_decode(&cp, &bf->free_list);
477 int bf_free(BFile bf, int no, const zint *blocks)
480 assert(bf->alloc_buf);
481 bf->header_dirty = 1;
482 for (i = 0; i < no; i++)
486 memset(buf, '\0', sizeof(buf));
487 zebra_zint_encode(&cp, bf->free_list);
488 bf->free_list = blocks[i];
489 bf_write(bf, bf->free_list, 0, sizeof(buf), buf);
494 int bfs_register_directory_stat(BFiles bfs, int no, const char **directory,
495 double *used_bytes, double *max_bytes)
497 return mf_area_directory_stat(bfs->register_area, no, directory,
498 used_bytes, max_bytes);
502 int bfs_shadow_directory_stat(BFiles bfs, int no, const char **directory,
503 double *used_bytes, double *max_bytes)
505 if (!bfs->commit_area)
507 return mf_area_directory_stat(bfs->commit_area, no, directory,
508 used_bytes, max_bytes);
513 * c-file-style: "Stroustrup"
514 * indent-tabs-mode: nil
516 * vim: shiftwidth=4 tabstop=8 expandtab