3ba061e17a1be4250b5934e5d658ecc1b26258c0
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /* $Id: bfile.c,v 1.37 2004-11-19 10:26:53 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #ifdef WIN32
30 #include <io.h>
31 #else
32 #include <unistd.h>
33 #endif
34
35 #include <zebrautl.h>
36 #include <bfile.h>
37
38 #include "cfile.h"
39
40 struct BFiles_struct {
41     MFile_area commit_area;
42     MFile_area_struct *register_area;
43     char *base;
44     char *cache_fname;
45 };
46
47 BFiles bfs_create (const char *spec, const char *base)
48 {
49     BFiles bfs = (BFiles) xmalloc (sizeof(*bfs));
50     bfs->commit_area = NULL;
51     bfs->base = 0;
52     bfs->cache_fname = 0;
53     if (base)
54         bfs->base = xstrdup (base);
55     bfs->register_area = mf_init("register", spec, base);
56     if (!bfs->register_area)
57     {
58         bfs_destroy(bfs);
59         return 0;
60     }
61     return bfs;
62 }
63
64 void bfs_destroy (BFiles bfs)
65 {
66     if (!bfs)
67         return;
68     xfree (bfs->cache_fname);
69     xfree (bfs->base);
70     mf_destroy (bfs->commit_area);
71     mf_destroy (bfs->register_area);
72     xfree (bfs);
73 }
74
75 static FILE *open_cache (BFiles bfs, const char *flags)
76 {
77     FILE *file;
78
79     file = fopen (bfs->cache_fname, flags);
80     return file;
81 }
82
83 static void unlink_cache (BFiles bfs)
84 {
85     unlink (bfs->cache_fname);
86 }
87
88 void bf_cache (BFiles bfs, const char *spec)
89 {
90     if (spec)
91     {
92         yaz_log (YLOG_LOG, "enabling cache spec=%s", spec);
93         if (!bfs->commit_area)
94             bfs->commit_area = mf_init ("shadow", spec, bfs->base);
95         if (bfs->commit_area)
96         {
97             bfs->cache_fname = xmalloc (strlen(bfs->commit_area->dirs->name)+
98                                        8);
99             strcpy (bfs->cache_fname, bfs->commit_area->dirs->name);
100             strcat (bfs->cache_fname, "/cache");
101             yaz_log (YLOG_LOG, "cache_fname = %s", bfs->cache_fname);
102         }
103     }
104     else
105         bfs->commit_area = NULL;
106 }
107
108 int bf_close (BFile bf)
109 {
110     zebra_lock_rdwr_destroy (&bf->rdwr_lock);
111     if (bf->cf)
112         cf_close (bf->cf);
113     mf_close (bf->mf);
114     xfree (bf);
115     return 0;
116 }
117
118 BFile bf_open (BFiles bfs, const char *name, int block_size, int wflag)
119 {
120     BFile tmp = (BFile) xmalloc(sizeof(BFile_struct));
121
122     if (bfs->commit_area)
123     {
124         int first_time;
125
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,
128                            wflag, &first_time);
129         if (first_time)
130         {
131             FILE *outf;
132
133             outf = open_cache (bfs, "ab");
134             if (!outf)
135             {
136                 yaz_log (YLOG_FATAL|YLOG_ERRNO, "open %s", bfs->cache_fname);
137                 exit (1);
138             }
139             fprintf (outf, "%s %d\n", name, block_size);
140             fclose (outf);
141         }
142     }
143     else
144     {
145         tmp->mf = mf_open(bfs->register_area, name, block_size, wflag);
146         tmp->cf = NULL;
147     }
148     if (!tmp->mf)
149     {
150         yaz_log (YLOG_FATAL, "mf_open failed for %s", name);
151         xfree (tmp);
152         return 0;
153     }
154     zebra_lock_rdwr_init (&tmp->rdwr_lock);
155     return(tmp);
156 }
157
158 int bf_read (BFile bf, zint no, int offset, int nbytes, void *buf)
159 {
160     int r;
161
162     zebra_lock_rdwr_rlock (&bf->rdwr_lock);
163     if (bf->cf)
164     {
165         if ((r = cf_read (bf->cf, no, offset, nbytes, buf)) == -1)
166             r = mf_read (bf->mf, no, offset, nbytes, buf);
167     }
168     else 
169         r = mf_read (bf->mf, no, offset, nbytes, buf);
170     zebra_lock_rdwr_runlock (&bf->rdwr_lock);
171     return r;
172 }
173
174 int bf_write (BFile bf, zint no, int offset, int nbytes, const void *buf)
175 {
176     int r;
177     zebra_lock_rdwr_wlock (&bf->rdwr_lock);
178     if (bf->cf)
179         r = cf_write (bf->cf, no, offset, nbytes, buf);
180     else
181         r = mf_write (bf->mf, no, offset, nbytes, buf);
182     zebra_lock_rdwr_wunlock (&bf->rdwr_lock);
183     return r;
184 }
185
186 int bf_commitExists (BFiles bfs)
187 {
188     FILE *inf;
189
190     inf = open_cache (bfs, "rb");
191     if (inf)
192     {
193         fclose (inf);
194         return 1;
195     }
196     return 0;
197 }
198
199 void bf_reset (BFiles bfs)
200 {
201     if (!bfs)
202         return;
203     mf_reset (bfs->commit_area);
204     mf_reset (bfs->register_area);
205 }
206
207 void bf_commitExec (BFiles bfs)
208 {
209     FILE *inf;
210     int block_size;
211     char path[256];
212     MFile mf;
213     CFile cf;
214     int first_time;
215
216     assert (bfs->commit_area);
217     if (!(inf = open_cache (bfs, "rb")))
218     {
219         yaz_log (YLOG_LOG, "No commit file");
220         return ;
221     }
222     while (fscanf (inf, "%s %d", path, &block_size) == 2)
223     {
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);
226
227         cf_commit (cf);
228
229         cf_close (cf);
230         mf_close (mf);
231     }
232     fclose (inf);
233 }
234
235 void bf_commitClean (BFiles bfs, const char *spec)
236 {
237     FILE *inf;
238     int block_size;
239     char path[256];
240     MFile mf;
241     CFile cf;
242     int mustDisable = 0;
243     int firstTime;
244
245     if (!bfs->commit_area)
246     {
247         bf_cache (bfs, spec);
248         mustDisable = 1;
249     }
250
251     if (!(inf = open_cache (bfs, "rb")))
252         return ;
253     while (fscanf (inf, "%s %d", path, &block_size) == 2)
254     {
255         mf = mf_open (bfs->register_area, path, block_size, 0);
256         cf = cf_open (mf, bfs->commit_area, path, block_size, 1, &firstTime);
257         cf_unlink (cf);
258         cf_close (cf);
259         mf_close (mf);
260     }
261     fclose (inf);
262     unlink_cache (bfs);
263     if (mustDisable)
264         bf_cache (bfs, 0);
265 }