The test option (-s) only uses read-lock (and not write lock).
[idzebra-moved-to-github.git] / index / dirs.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dirs.c,v $
7  * Revision 1.14  1998-01-12 15:04:07  adam
8  * The test option (-s) only uses read-lock (and not write lock).
9  *
10  * Revision 1.13  1997/09/09 13:38:06  adam
11  * Partial port to WIN95/NT.
12  *
13  * Revision 1.12  1996/11/08 11:10:13  adam
14  * Buffers used during file match got bigger.
15  * Compressed ISAM support everywhere.
16  * Bug fixes regarding masking characters in queries.
17  * Redesigned Regexp-2 queries.
18  *
19  * Revision 1.11  1996/10/29 14:06:47  adam
20  * Include zebrautl.h instead of alexutil.h.
21  *
22  * Revision 1.10  1996/06/04 10:18:58  adam
23  * Minor changes - removed include of ctype.h.
24  *
25  * Revision 1.9  1996/04/23  12:39:07  adam
26  * Bug fix: In function dirs_del dict_delete is used to remove a file
27  * rather than a bogus dict_insert.
28  *
29  * Revision 1.8  1996/04/12  07:02:21  adam
30  * File update of single files.
31  *
32  * Revision 1.7  1996/03/21 14:50:09  adam
33  * File update uses modify-time instead of change-time.
34  *
35  * Revision 1.6  1996/02/02  13:44:43  adam
36  * The public dictionary functions simply use char instead of Dict_char
37  * to represent search strings. Dict_char is used internally only.
38  *
39  * Revision 1.5  1996/01/17  14:54:44  adam
40  * Function dirs_rmdir uses dict_delete.
41  *
42  * Revision 1.4  1995/11/30  08:34:27  adam
43  * Started work on commit facility.
44  * Changed a few malloc/free to xmalloc/xfree.
45  *
46  * Revision 1.3  1995/11/20  16:59:45  adam
47  * New update method: the 'old' keys are saved for each records.
48  *
49  * Revision 1.2  1995/11/20  11:56:23  adam
50  * Work on new traversal.
51  *
52  * Revision 1.1  1995/11/17  15:54:42  adam
53  * Started work on virtual directory structure.
54  */
55 #include <stdio.h>
56 #include <string.h>
57 #include <assert.h>
58 #include <errno.h>
59 #include <fcntl.h>
60
61 #include "index.h"
62
63 #define DIRS_MAX_PATH 1024
64
65 struct dirs_info {
66     Dict dict;
67     int rw;
68     int no_read;
69     int no_cur;
70     int no_max;
71     struct dirs_entry *entries;
72     char nextpath[DIRS_MAX_PATH];
73     char prefix[DIRS_MAX_PATH];
74     int prelen;
75     struct dirs_entry *last_entry;
76 };
77
78 static int dirs_client_proc (char *name, const char *info, int pos,
79                              void *client)
80 {
81     struct dirs_info *ci = client;
82     struct dirs_entry *entry;
83
84     if (memcmp (name, ci->prefix, ci->prelen))
85         return 1;
86     if (ci->no_cur < 0)
87     {
88         ci->no_cur = 0;
89         return 0;
90     }
91     if (ci->no_cur == ci->no_max)
92     {
93         assert (0);
94     }
95     entry = ci->entries + ci->no_cur;
96     if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
97     {
98         strcpy (entry->path, name + ci->prelen); 
99         entry->kind = dirs_file;
100         memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
101         memcpy (&entry->mtime, info+1+sizeof(entry->sysno), 
102                 sizeof(entry->mtime));
103         ci->no_cur++;
104     } 
105     else if (info[0] == sizeof(entry->mtime))
106     {
107         strcpy (entry->path, name + ci->prelen);
108         entry->kind = dirs_dir;
109         memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
110         ci->no_cur++;
111     }
112     return 0;
113 }
114
115 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
116 {
117     struct dirs_info *p;
118     int before = 0, after;
119
120     logf (LOG_DEBUG, "dirs_open %s", rep);
121     p = xmalloc (sizeof (*p));
122     p->dict = dict;
123     p->rw = rw;
124     strcpy (p->prefix, rep);
125     p->prelen = strlen(p->prefix);
126     strcpy (p->nextpath, rep);
127     p->no_read = p->no_cur = 0;
128     after = p->no_max = 100;
129     p->entries = xmalloc (sizeof(*p->entries) * (p->no_max));
130     logf (LOG_DEBUG, "dirs_open first scan");
131     dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
132     return p;
133 }
134
135 struct dirs_info *dirs_fopen (Dict dict, const char *path)
136 {
137     struct dirs_info *p;
138     struct dirs_entry *entry;
139     char *info;
140
141     p = xmalloc (sizeof(*p));
142     p->dict = dict;
143     *p->prefix = '\0';
144     p->entries = xmalloc (sizeof(*p->entries));
145     p->no_read = 0;
146     p->no_cur = 0;
147     p->no_max = 2;
148
149     entry = p->entries;
150     info = dict_lookup (dict, path);
151     if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
152     {
153         strcpy (entry->path, path); 
154         entry->kind = dirs_file;
155         memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
156         memcpy (&entry->mtime, info+1+sizeof(entry->sysno), 
157                 sizeof(entry->mtime));
158         p->no_cur++;
159     }
160     return p;
161 }
162
163 struct dirs_entry *dirs_read (struct dirs_info *p)
164 {
165     int before = 0, after = p->no_max+1;
166
167     if (p->no_read < p->no_cur)
168     {
169         logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
170               (p->entries + p->no_read)->path);
171         return p->last_entry = p->entries + (p->no_read++);
172     }
173     if (p->no_cur < p->no_max)
174         return p->last_entry = NULL;
175     p->no_cur = -1;
176     logf (LOG_DEBUG, "dirs_read rescan");
177     dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
178     p->no_read = 1;
179     if (p->no_read < p->no_cur)
180         return p->last_entry = p->entries;
181     return p->last_entry = NULL;
182 }
183
184 struct dirs_entry *dirs_last (struct dirs_info *p)
185 {
186     return p->last_entry;
187 }
188
189 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
190 {
191     char path[DIRS_MAX_PATH];
192
193     sprintf (path, "%s%s", p->prefix, src);
194     logf (LOG_DEBUG, "dirs_mkdir %s", path);
195     if (p->rw)
196         dict_insert (p->dict, path, sizeof(mtime), &mtime);
197 }
198
199 void dirs_rmdir (struct dirs_info *p, const char *src)
200 {
201     char path[DIRS_MAX_PATH];
202
203     sprintf (path, "%s%s", p->prefix, src);
204     logf (LOG_DEBUG, "dirs_rmdir %s", path);
205     if (p->rw)
206         dict_delete (p->dict, path);
207 }
208
209 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
210 {
211     char path[DIRS_MAX_PATH];
212     char info[16];
213
214     sprintf (path, "%s%s", p->prefix, src);
215     logf (LOG_DEBUG, "dirs_add %s", path);
216     memcpy (info, &sysno, sizeof(sysno));
217     memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
218     if (p->rw)
219         dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
220 }
221
222 void dirs_del (struct dirs_info *p, const char *src)
223 {
224     char path[DIRS_MAX_PATH];
225
226     sprintf (path, "%s%s", p->prefix, src);
227     logf (LOG_DEBUG, "dirs_del %s", path);
228     if (p->rw)
229         dict_delete (p->dict, path);
230 }
231
232 void dirs_free (struct dirs_info **pp)
233 {
234     struct dirs_info *p = *pp;
235
236     xfree (p->entries);
237     xfree (p);
238     *pp = NULL;
239 }
240