The directory of the shadow table file can be specified by the new
[idzebra-moved-to-github.git] / bfile / bfile.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: bfile.c,v $
7  * Revision 1.20  1996-03-26 15:59:04  adam
8  * The directory of the shadow table file can be specified by the new
9  * bf_lockDir call.
10  *
11  * Revision 1.19  1996/02/05  12:28:58  adam
12  * Removed a LOG_LOG message.
13  *
14  * Revision 1.18  1996/01/02  08:59:06  quinn
15  * Changed "commit" setting to "shadow".
16  *
17  * Revision 1.17  1995/12/11  09:03:51  adam
18  * New function: cf_unlink.
19  * New member of commit file head: state (0) deleted, (1) hash file.
20  *
21  * Revision 1.16  1995/12/08  16:21:13  adam
22  * Work on commit/update.
23  *
24  * Revision 1.15  1995/12/01  16:24:28  adam
25  * Commit files use separate meta file area.
26  *
27  * Revision 1.14  1995/12/01  11:37:21  adam
28  * Cached/commit files implemented as meta-files.
29  *
30  * Revision 1.13  1995/11/30  17:00:49  adam
31  * Several bug fixes. Commit system runs now.
32  *
33  * Revision 1.12  1995/11/30  08:33:10  adam
34  * Started work on commit facility.
35  *
36  * Revision 1.11  1995/09/04  12:33:21  adam
37  * Various cleanup. YAZ util used instead.
38  *
39  * Revision 1.10  1994/08/25  10:15:54  quinn
40  * Trivial
41  *
42  * Revision 1.9  1994/08/24  08:45:48  quinn
43  * Using mfile.
44  *
45  * Revision 1.8  1994/08/23  15:03:34  quinn
46  * *** empty log message ***
47  *
48  * Revision 1.7  1994/08/23  14:25:45  quinn
49  * Added O_CREAT because some geek wanted it. Sheesh.
50  *
51  * Revision 1.6  1994/08/23  14:21:38  quinn
52  * Fixed call to log
53  *
54  * Revision 1.5  1994/08/18  08:10:08  quinn
55  * Minimal changes
56  *
57  * Revision 1.4  1994/08/17  14:27:32  quinn
58  * last mods
59  *
60  * Revision 1.2  1994/08/17  14:09:32  quinn
61  * Compiles cleanly (still only dummy).
62  *
63  * Revision 1.1  1994/08/17  13:55:08  quinn
64  * New blocksystem. dummy only
65  *
66  */
67
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <assert.h>
72 #include <unistd.h>
73
74 #include <alexutil.h>
75 #include <bfile.h>
76 #include "cfile.h"
77
78 static MFile_area commit_area = NULL;
79 static char *commit_lockDir = NULL;
80
81 static FILE *open_cache (const char *flags)
82 {
83     char cacheFilename[1024];
84     FILE *file;
85
86     sprintf (cacheFilename, "%scache", commit_lockDir ? commit_lockDir : "");
87     file = fopen (cacheFilename, flags);
88     return file;
89 }
90
91 static void unlink_cache (void)
92 {
93     char cacheFilename[1024];
94
95     sprintf (cacheFilename, "%scache", commit_lockDir ? commit_lockDir : "");
96     unlink (cacheFilename);
97 }
98
99 void bf_lockDir (const char *lockDir)
100 {
101     size_t len;
102     
103     xfree (commit_lockDir);
104
105     if (lockDir == NULL)
106         lockDir = "";
107     len = strlen(lockDir);
108     commit_lockDir = xmalloc (len+2);
109     strcpy (commit_lockDir, lockDir);
110     
111     if (len > 0 && commit_lockDir[len-1] != '/')
112         strcpy (commit_lockDir + len, "/");
113 }
114
115 void bf_cache (int enableFlag)
116 {
117     if (enableFlag)
118     {
119         if (!commit_area)
120             if (res_get (common_resource, "shadow"))
121             {
122                 commit_area = mf_init ("shadow");
123             }
124             else
125             {
126                 logf (LOG_FATAL, "Shadow area must be defined if commit"
127                       "is to be enabled");
128                 exit (1);
129             }
130     }
131     else
132         commit_area = NULL;
133 }
134
135 int bf_close (BFile bf)
136 {
137     if (bf->cf)
138         cf_close (bf->cf);
139     mf_close (bf->mf);
140     xfree (bf);
141     return 0;
142 }
143
144 BFile bf_open (const char *name, int block_size, int wflag)
145 {
146     BFile tmp = xmalloc(sizeof(BFile_struct));
147
148     if (commit_area)
149     {
150         int first_time;
151
152         tmp->mf = mf_open (0, name, block_size, 0);
153         tmp->cf = cf_open (tmp->mf, commit_area, name, block_size,
154                            wflag, &first_time);
155         if (first_time)
156         {
157             FILE *outf;
158
159             outf = open_cache ("a");
160             if (!outf)
161             {
162                 logf (LOG_FATAL|LOG_ERRNO, "open %scache",
163                       commit_lockDir ? commit_lockDir : "");
164                 exit (1);
165             }
166             fprintf (outf, "%s %d\n", name, block_size);
167             fclose (outf);
168         }
169     }
170     else
171     {
172         tmp->mf = mf_open(0, name, block_size, wflag);
173         tmp->cf = NULL;
174     }
175     if (!tmp->mf)
176     {
177         logf (LOG_FATAL, "mf_open failed for %s", name);
178         xfree (tmp);
179         return 0;
180     }
181     return(tmp);
182 }
183
184 int bf_read (BFile bf, int no, int offset, int num, void *buf)
185 {
186     int r;
187
188     if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
189         return r;
190     return mf_read (bf->mf, no, offset, num, buf);
191 }
192
193 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
194 {
195     if (bf->cf)
196         return cf_write (bf->cf, no, offset, num, buf);
197     return mf_write (bf->mf, no, offset, num, buf);
198 }
199
200 int bf_commitExists (void)
201 {
202     FILE *inf;
203
204     inf = open_cache ("r");
205     if (inf)
206     {
207         fclose (inf);
208         return 1;
209     }
210     return 0;
211 }
212
213 void bf_commitExec (void)
214 {
215     FILE *inf;
216     int block_size;
217     char path[256];
218     MFile mf;
219     CFile cf;
220     int first_time;
221
222     assert (commit_area);
223     if (!(inf = open_cache ("r")))
224     {
225         logf (LOG_LOG, "No commit file");
226         return ;
227     }
228     while (fscanf (inf, "%s %d", path, &block_size) == 2)
229     {
230         mf = mf_open (0, path, block_size, 1);
231         cf = cf_open (mf, commit_area, path, block_size, 0, &first_time);
232
233         cf_commit (cf);
234
235         cf_close (cf);
236         mf_close (mf);
237     }
238     fclose (inf);
239 }
240
241 void bf_commitClean (void)
242 {
243     FILE *inf;
244     int block_size;
245     char path[256];
246     MFile mf;
247     CFile cf;
248     int mustDisable = 0;
249     int firstTime;
250
251     if (!commit_area)
252     {
253         bf_cache (1);
254         mustDisable = 1;
255     }
256
257     if (!(inf = open_cache ("r")))
258         return ;
259     while (fscanf (inf, "%s %d", path, &block_size) == 2)
260     {
261         mf = mf_open (0, path, block_size, 0);
262         cf = cf_open (mf, commit_area, path, block_size, 1, &firstTime);
263         cf_unlink (cf);
264         cf_close (cf);
265         mf_close (mf);
266     }
267     fclose (inf);
268     unlink_cache ();
269     if (mustDisable)
270         bf_cache (0);
271 }