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