Several bug fixes. Commit system runs now.
[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.13  1995-11-30 17:00:49  adam
8  * Several bug fixes. Commit system runs now.
9  *
10  * Revision 1.12  1995/11/30  08:33:10  adam
11  * Started work on commit facility.
12  *
13  * Revision 1.11  1995/09/04  12:33:21  adam
14  * Various cleanup. YAZ util used instead.
15  *
16  * Revision 1.10  1994/08/25  10:15:54  quinn
17  * Trivial
18  *
19  * Revision 1.9  1994/08/24  08:45:48  quinn
20  * Using mfile.
21  *
22  * Revision 1.8  1994/08/23  15:03:34  quinn
23  * *** empty log message ***
24  *
25  * Revision 1.7  1994/08/23  14:25:45  quinn
26  * Added O_CREAT because some geek wanted it. Sheesh.
27  *
28  * Revision 1.6  1994/08/23  14:21:38  quinn
29  * Fixed call to log
30  *
31  * Revision 1.5  1994/08/18  08:10:08  quinn
32  * Minimal changes
33  *
34  * Revision 1.4  1994/08/17  14:27:32  quinn
35  * last mods
36  *
37  * Revision 1.2  1994/08/17  14:09:32  quinn
38  * Compiles cleanly (still only dummy).
39  *
40  * Revision 1.1  1994/08/17  13:55:08  quinn
41  * New blocksystem. dummy only
42  *
43  */
44
45 #include <stdio.h>
46 #include <sys/types.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <stdlib.h>
50
51 #include <alexutil.h>
52 #include <bfile.h>
53 #include "cfile.h"
54
55 static const char *cache_name = NULL;
56
57 void bf_cache (const char *name)
58 {
59     cache_name = name;
60 }
61
62 int bf_close (BFile bf)
63 {
64     if (bf->cf)
65         cf_close (bf->cf);
66     mf_close (bf->mf);
67     xfree (bf);
68     return 0;
69 }
70
71 BFile bf_open (const char *name, int block_size, int wflag)
72 {
73     BFile tmp = xmalloc(sizeof(BFile_struct));
74
75     if (cache_name)
76     {
77         FILE *outf;
78         int first_time;
79
80         logf (LOG_LOG, "cf,mf_open %s, cache_name=%s", name, cache_name);
81         tmp->mf = mf_open(0, name, block_size, 0);
82         tmp->cf = cf_open(tmp->mf, cache_name, name, block_size, wflag,
83                           &first_time);
84
85         if (first_time)
86         {
87             outf = fopen (cache_name, "a");
88             fprintf (outf, "%s %d\n", name, block_size);
89             fclose (outf);
90         }
91     }
92     else
93     {
94         tmp->mf = mf_open(0, name, block_size, wflag);
95         tmp->cf = NULL;
96     }
97     if (!tmp->mf)
98     {
99         logf (LOG_FATAL, "mf_open failed for %s", name);
100         xfree (tmp);
101         return 0;
102     }
103     return(tmp);
104 }
105
106 int bf_read (BFile bf, int no, int offset, int num, void *buf)
107 {
108     int r;
109
110     if (bf->cf && (r=cf_read (bf->cf, no, offset, num, buf)) != -1)
111         return r;
112     return mf_read (bf->mf, no, offset, num, buf);
113 }
114
115 int bf_write (BFile bf, int no, int offset, int num, const void *buf)
116 {
117     if (bf->cf)
118         return cf_write (bf->cf, no, offset, num, buf);
119     return mf_write (bf->mf, no, offset, num, buf);
120 }
121
122 void bf_commit (const char *name)
123 {
124     FILE *inf;
125     int block_size;
126     char path[256];
127     MFile mf;
128     CFile cf;
129     int first_time;
130
131     if (!(inf = fopen (name, "r")))
132     {
133         logf (LOG_FATAL|LOG_ERRNO, "cannot open commit %s", name);
134         exit (1);
135     }
136     while (fscanf (inf, "%s %d", path, &block_size) == 2)
137     {
138         mf = mf_open(0, path, block_size, 1);
139         cf = cf_open(mf, name, path, block_size, 0, &first_time);
140
141         cf_commit (cf);
142
143         cf_close (cf);
144         mf_close (mf);
145     }
146     fclose (inf);
147 }