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