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