2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1999-02-02 14:50:51 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.14 1998/01/12 15:04:07 adam
11 * The test option (-s) only uses read-lock (and not write lock).
13 * Revision 1.13 1997/09/09 13:38:06 adam
14 * Partial port to WIN95/NT.
16 * Revision 1.12 1996/11/08 11:10:13 adam
17 * Buffers used during file match got bigger.
18 * Compressed ISAM support everywhere.
19 * Bug fixes regarding masking characters in queries.
20 * Redesigned Regexp-2 queries.
22 * Revision 1.11 1996/10/29 14:06:47 adam
23 * Include zebrautl.h instead of alexutil.h.
25 * Revision 1.10 1996/06/04 10:18:58 adam
26 * Minor changes - removed include of ctype.h.
28 * Revision 1.9 1996/04/23 12:39:07 adam
29 * Bug fix: In function dirs_del dict_delete is used to remove a file
30 * rather than a bogus dict_insert.
32 * Revision 1.8 1996/04/12 07:02:21 adam
33 * File update of single files.
35 * Revision 1.7 1996/03/21 14:50:09 adam
36 * File update uses modify-time instead of change-time.
38 * Revision 1.6 1996/02/02 13:44:43 adam
39 * The public dictionary functions simply use char instead of Dict_char
40 * to represent search strings. Dict_char is used internally only.
42 * Revision 1.5 1996/01/17 14:54:44 adam
43 * Function dirs_rmdir uses dict_delete.
45 * Revision 1.4 1995/11/30 08:34:27 adam
46 * Started work on commit facility.
47 * Changed a few malloc/free to xmalloc/xfree.
49 * Revision 1.3 1995/11/20 16:59:45 adam
50 * New update method: the 'old' keys are saved for each records.
52 * Revision 1.2 1995/11/20 11:56:23 adam
53 * Work on new traversal.
55 * Revision 1.1 1995/11/17 15:54:42 adam
56 * Started work on virtual directory structure.
66 #define DIRS_MAX_PATH 1024
74 struct dirs_entry *entries;
75 char nextpath[DIRS_MAX_PATH];
76 char prefix[DIRS_MAX_PATH];
78 struct dirs_entry *last_entry;
81 static int dirs_client_proc (char *name, const char *info, int pos,
84 struct dirs_info *ci = client;
85 struct dirs_entry *entry;
87 if (memcmp (name, ci->prefix, ci->prelen))
94 if (ci->no_cur == ci->no_max)
98 entry = ci->entries + ci->no_cur;
99 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
101 strcpy (entry->path, name + ci->prelen);
102 entry->kind = dirs_file;
103 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
104 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
105 sizeof(entry->mtime));
108 else if (info[0] == sizeof(entry->mtime))
110 strcpy (entry->path, name + ci->prelen);
111 entry->kind = dirs_dir;
112 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
118 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
121 int before = 0, after;
123 logf (LOG_DEBUG, "dirs_open %s", rep);
124 p = xmalloc (sizeof (*p));
127 strcpy (p->prefix, rep);
128 p->prelen = strlen(p->prefix);
129 strcpy (p->nextpath, rep);
130 p->no_read = p->no_cur = 0;
131 after = p->no_max = 100;
132 p->entries = xmalloc (sizeof(*p->entries) * (p->no_max));
133 logf (LOG_DEBUG, "dirs_open first scan");
134 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
138 struct dirs_info *dirs_fopen (Dict dict, const char *path)
141 struct dirs_entry *entry;
144 p = xmalloc (sizeof(*p));
147 p->entries = xmalloc (sizeof(*p->entries));
153 info = dict_lookup (dict, path);
154 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
156 strcpy (entry->path, path);
157 entry->kind = dirs_file;
158 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
159 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
160 sizeof(entry->mtime));
166 struct dirs_entry *dirs_read (struct dirs_info *p)
168 int before = 0, after = p->no_max+1;
170 if (p->no_read < p->no_cur)
172 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
173 (p->entries + p->no_read)->path);
174 return p->last_entry = p->entries + (p->no_read++);
176 if (p->no_cur < p->no_max)
177 return p->last_entry = NULL;
179 logf (LOG_DEBUG, "dirs_read rescan");
180 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
182 if (p->no_read < p->no_cur)
183 return p->last_entry = p->entries;
184 return p->last_entry = NULL;
187 struct dirs_entry *dirs_last (struct dirs_info *p)
189 return p->last_entry;
192 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
194 char path[DIRS_MAX_PATH];
196 sprintf (path, "%s%s", p->prefix, src);
197 logf (LOG_DEBUG, "dirs_mkdir %s", path);
199 dict_insert (p->dict, path, sizeof(mtime), &mtime);
202 void dirs_rmdir (struct dirs_info *p, const char *src)
204 char path[DIRS_MAX_PATH];
206 sprintf (path, "%s%s", p->prefix, src);
207 logf (LOG_DEBUG, "dirs_rmdir %s", path);
209 dict_delete (p->dict, path);
212 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
214 char path[DIRS_MAX_PATH];
217 sprintf (path, "%s%s", p->prefix, src);
218 logf (LOG_DEBUG, "dirs_add %s", path);
219 memcpy (info, &sysno, sizeof(sysno));
220 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
222 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
225 void dirs_del (struct dirs_info *p, const char *src)
227 char path[DIRS_MAX_PATH];
229 sprintf (path, "%s%s", p->prefix, src);
230 logf (LOG_DEBUG, "dirs_del %s", path);
232 dict_delete (p->dict, path);
235 void dirs_free (struct dirs_info **pp)
237 struct dirs_info *p = *pp;