2 * Copyright (C) 1994-2001, Index Data
6 * Revision 1.17 2001-03-29 14:07:14 adam
7 * Fixed nasty bug for fileUpdate.
9 * Revision 1.16 1999/05/26 07:49:13 adam
12 * Revision 1.15 1999/02/02 14:50:51 adam
13 * Updated WIN32 code specific sections. Changed header.
15 * Revision 1.14 1998/01/12 15:04:07 adam
16 * The test option (-s) only uses read-lock (and not write lock).
18 * Revision 1.13 1997/09/09 13:38:06 adam
19 * Partial port to WIN95/NT.
21 * Revision 1.12 1996/11/08 11:10:13 adam
22 * Buffers used during file match got bigger.
23 * Compressed ISAM support everywhere.
24 * Bug fixes regarding masking characters in queries.
25 * Redesigned Regexp-2 queries.
27 * Revision 1.11 1996/10/29 14:06:47 adam
28 * Include zebrautl.h instead of alexutil.h.
30 * Revision 1.10 1996/06/04 10:18:58 adam
31 * Minor changes - removed include of ctype.h.
33 * Revision 1.9 1996/04/23 12:39:07 adam
34 * Bug fix: In function dirs_del dict_delete is used to remove a file
35 * rather than a bogus dict_insert.
37 * Revision 1.8 1996/04/12 07:02:21 adam
38 * File update of single files.
40 * Revision 1.7 1996/03/21 14:50:09 adam
41 * File update uses modify-time instead of change-time.
43 * Revision 1.6 1996/02/02 13:44:43 adam
44 * The public dictionary functions simply use char instead of Dict_char
45 * to represent search strings. Dict_char is used internally only.
47 * Revision 1.5 1996/01/17 14:54:44 adam
48 * Function dirs_rmdir uses dict_delete.
50 * Revision 1.4 1995/11/30 08:34:27 adam
51 * Started work on commit facility.
52 * Changed a few malloc/free to xmalloc/xfree.
54 * Revision 1.3 1995/11/20 16:59:45 adam
55 * New update method: the 'old' keys are saved for each records.
57 * Revision 1.2 1995/11/20 11:56:23 adam
58 * Work on new traversal.
60 * Revision 1.1 1995/11/17 15:54:42 adam
61 * Started work on virtual directory structure.
71 #define DIRS_MAX_PATH 1024
79 struct dirs_entry *entries;
80 char nextpath[DIRS_MAX_PATH];
81 char prefix[DIRS_MAX_PATH];
83 struct dirs_entry *last_entry;
86 static int dirs_client_proc (char *name, const char *info, int pos,
89 struct dirs_info *ci = (struct dirs_info *) client;
90 struct dirs_entry *entry;
92 if (memcmp (name, ci->prefix, ci->prelen))
99 if (ci->no_cur == ci->no_max)
103 entry = ci->entries + ci->no_cur;
104 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
106 strcpy (entry->path, name + ci->prelen);
107 entry->kind = dirs_file;
108 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
109 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
110 sizeof(entry->mtime));
113 else if (info[0] == sizeof(entry->mtime))
115 strcpy (entry->path, name + ci->prelen);
116 entry->kind = dirs_dir;
117 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
123 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
126 int before = 0, after;
128 logf (LOG_DEBUG, "dirs_open %s", rep);
129 p = (struct dirs_info *) xmalloc (sizeof (*p));
132 strcpy (p->prefix, rep);
133 p->prelen = strlen(p->prefix);
134 strcpy (p->nextpath, rep);
135 p->no_read = p->no_cur = 0;
136 after = p->no_max = 100;
137 p->entries = (struct dirs_entry *)
138 xmalloc (sizeof(*p->entries) * (p->no_max));
139 logf (LOG_DEBUG, "dirs_open first scan");
140 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
144 struct dirs_info *dirs_fopen (Dict dict, const char *path)
147 struct dirs_entry *entry;
150 p = (struct dirs_info *) xmalloc (sizeof(*p));
153 p->entries = (struct dirs_entry *) xmalloc (sizeof(*p->entries));
159 info = dict_lookup (dict, path);
160 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
162 strcpy (entry->path, path);
163 entry->kind = dirs_file;
164 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
165 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
166 sizeof(entry->mtime));
172 struct dirs_entry *dirs_read (struct dirs_info *p)
174 int before = 0, after = p->no_max+1;
176 if (p->no_read < p->no_cur)
178 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
179 (p->entries + p->no_read)->path);
180 return p->last_entry = p->entries + (p->no_read++);
182 if (p->no_cur < p->no_max)
183 return p->last_entry = NULL;
185 logf (LOG_DEBUG, "dirs_read rescan");
186 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
188 if (p->no_read <= p->no_cur)
189 return p->last_entry = p->entries;
190 return p->last_entry = NULL;
193 struct dirs_entry *dirs_last (struct dirs_info *p)
195 return p->last_entry;
198 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
200 char path[DIRS_MAX_PATH];
202 sprintf (path, "%s%s", p->prefix, src);
203 logf (LOG_DEBUG, "dirs_mkdir %s", path);
205 dict_insert (p->dict, path, sizeof(mtime), &mtime);
208 void dirs_rmdir (struct dirs_info *p, const char *src)
210 char path[DIRS_MAX_PATH];
212 sprintf (path, "%s%s", p->prefix, src);
213 logf (LOG_DEBUG, "dirs_rmdir %s", path);
215 dict_delete (p->dict, path);
218 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
220 char path[DIRS_MAX_PATH];
223 sprintf (path, "%s%s", p->prefix, src);
224 logf (LOG_DEBUG, "dirs_add %s", path);
225 memcpy (info, &sysno, sizeof(sysno));
226 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
228 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
231 void dirs_del (struct dirs_info *p, const char *src)
233 char path[DIRS_MAX_PATH];
235 sprintf (path, "%s%s", p->prefix, src);
236 logf (LOG_DEBUG, "dirs_del %s", path);
238 dict_delete (p->dict, path);
241 void dirs_free (struct dirs_info **pp)
243 struct dirs_info *p = *pp;