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