Changed "commit" setting to "shadow".
[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.18  1996-01-02 08:59:06  quinn
8  * Changed "commit" setting to "shadow".
9  *
10  * Revision 1.17  1995/12/11  09:03:51  adam
11  * New function: cf_unlink.
12  * New member of commit file head: state (0) deleted, (1) hash file.
13  *
14  * Revision 1.16  1995/12/08  16:21:13  adam
15  * Work on commit/update.
16  *
17  * Revision 1.15  1995/12/01  16:24:28  adam
18  * Commit files use separate meta file area.
19  *
20  * Revision 1.14  1995/12/01  11:37:21  adam
21  * Cached/commit files implemented as meta-files.
22  *
23  * Revision 1.13  1995/11/30  17:00:49  adam
24  * Several bug fixes. Commit system runs now.
25  *
26  * Revision 1.12  1995/11/30  08:33:10  adam
27  * Started work on commit facility.
28  *
29  * Revision 1.11  1995/09/04  12:33:21  adam
30  * Various cleanup. YAZ util used instead.
31  *
32  * Revision 1.10  1994/08/25  10:15:54  quinn
33  * Trivial
34  *
35  * Revision 1.9  1994/08/24  08:45:48  quinn
36  * Using mfile.
37  *
38  * Revision 1.8  1994/08/23  15:03:34  quinn
39  * *** empty log message ***
40  *
41  * Revision 1.7  1994/08/23  14:25:45  quinn
42  * Added O_CREAT because some geek wanted it. Sheesh.
43  *
44  * Revision 1.6  1994/08/23  14:21:38  quinn
45  * Fixed call to log
46  *
47  * Revision 1.5  1994/08/18  08:10:08  quinn
48  * Minimal changes
49  *
50  * Revision 1.4  1994/08/17  14:27:32  quinn
51  * last mods
52  *
53  * Revision 1.2  1994/08/17  14:09:32  quinn
54  * Compiles cleanly (still only dummy).
55  *
56  * Revision 1.1  1994/08/17  13:55:08  quinn
57  * New blocksystem. dummy only
58  *
59  */
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <assert.h>
64 #include <unistd.h>
65
66 #include <alexutil.h>
67 #include <bfile.h>
68 #include "cfile.h"
69
70 static MFile_area commit_area = NULL;
71
72 void bf_cache (int enableFlag)
73 {
74     if (enableFlag)
75     {
76         if (!commit_area)
77             if (res_get (common_resource, "shadow"))
78                 commit_area = mf_init ("shadow");
79             else
80             {
81                 logf (LOG_FATAL, "Shadow area must be defined if commit"
82                       "is to be enabled");
83                 exit (1);
84             }
85     }
86     else
87         commit_area = NULL;
88 }
89
90 int bf_close (BFile bf)
91 {
92     if (bf->cf)
93         cf_close (bf->cf);
94     mf_close (bf->mf);
95     xfree (bf);
96     return 0;
97 }
98
99 BFile bf_open (const char *name, int block_size, int wflag)
100 {
101     BFile tmp = xmalloc(sizeof(BFile_struct));
102
103     if (commit_area)
104     {
105         FILE *outf;
106         int first_time;
107
108         logf (LOG_LOG, "cf,mf_open %s", name);
109         
110         tmp->mf = mf_open (0, name, block_size, 0);
111         tmp->cf = cf_open (tmp->mf, commit_area, name, block_size,
112                            wflag, &first_time);
113         if (first_time)
114         {
115             outf = fopen ("cache", "a");
116             fprintf (outf, "%s %d\n", name, block_size);
117             fclose (outf);
118         }
119     }
120     else
121     {
122         tmp->mf = mf_open(0, name, block_size, wflag);
123         tmp->cf = NULL;
124     }
125     if (!tmp->mf)
126     {
127         logf (LOG_FATAL, "mf_open failed for %s", name);
128         xfree (tmp);
129         return 0;
130     }
131     return(tmp);
132 }
133
134 int bf_read (BFile bf, int no, int offset, int num, void *buf)
135 {
136     int r;
137
138     if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
139         return r;
140     return mf_read (bf->mf, no, offset, num, buf);
141 }
142
143 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
144 {
145     if (bf->cf)
146         return cf_write (bf->cf, no, offset, num, buf);
147     return mf_write (bf->mf, no, offset, num, buf);
148 }
149
150 int bf_commitExists (void)
151 {
152     FILE *inf;
153
154     inf = fopen ("cache", "r");
155     if (inf)
156     {
157         fclose (inf);
158         return 1;
159     }
160     return 0;
161 }
162
163 void bf_commitExec (void)
164 {
165     FILE *inf;
166     int block_size;
167     char path[256];
168     MFile mf;
169     CFile cf;
170     int first_time;
171
172     assert (commit_area);
173     if (!(inf = fopen ("cache", "r")))
174     {
175         logf (LOG_LOG, "No commit file");
176         return ;
177     }
178     while (fscanf (inf, "%s %d", path, &block_size) == 2)
179     {
180         mf = mf_open (0, path, block_size, 1);
181         cf = cf_open (mf, commit_area, path, block_size, 0, &first_time);
182
183         cf_commit (cf);
184
185         cf_close (cf);
186         mf_close (mf);
187     }
188     fclose (inf);
189 }
190
191 void bf_commitClean (void)
192 {
193     FILE *inf;
194     int block_size;
195     char path[256];
196     MFile mf;
197     CFile cf;
198     int mustDisable = 0;
199     int firstTime;
200
201     if (!commit_area)
202     {
203         bf_cache (1);
204         mustDisable = 1;
205     }
206
207     if (!(inf = fopen ("cache", "r")))
208         return ;
209     while (fscanf (inf, "%s %d", path, &block_size) == 2)
210     {
211         mf = mf_open (0, path, block_size, 0);
212         cf = cf_open (mf, commit_area, path, block_size, 1, &firstTime);
213         cf_unlink (cf);
214         cf_close (cf);
215         mf_close (mf);
216     }
217     fclose (inf);
218     unlink ("cache");
219     if (mustDisable)
220         bf_cache (0);
221 }