2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1996-04-23 12:39:07 adam
8 * Bug fix: In function dirs_del dict_delete is used to remove a file
9 * rather than a bogus dict_insert.
11 * Revision 1.8 1996/04/12 07:02:21 adam
12 * File update of single files.
14 * Revision 1.7 1996/03/21 14:50:09 adam
15 * File update uses modify-time instead of change-time.
17 * Revision 1.6 1996/02/02 13:44:43 adam
18 * The public dictionary functions simply use char instead of Dict_char
19 * to represent search strings. Dict_char is used internally only.
21 * Revision 1.5 1996/01/17 14:54:44 adam
22 * Function dirs_rmdir uses dict_delete.
24 * Revision 1.4 1995/11/30 08:34:27 adam
25 * Started work on commit facility.
26 * Changed a few malloc/free to xmalloc/xfree.
28 * Revision 1.3 1995/11/20 16:59:45 adam
29 * New update method: the 'old' keys are saved for each records.
31 * Revision 1.2 1995/11/20 11:56:23 adam
32 * Work on new traversal.
34 * Revision 1.1 1995/11/17 15:54:42 adam
35 * Started work on virtual directory structure.
51 struct dirs_entry *entries;
55 struct dirs_entry *last_entry;
58 static int dirs_client_proc (char *name, const char *info, int pos,
61 struct dirs_info *ci = client;
62 struct dirs_entry *entry;
64 if (memcmp (name, ci->prefix, ci->prelen))
71 if (ci->no_cur == ci->no_max)
75 entry = ci->entries + ci->no_cur;
76 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
78 strcpy (entry->path, name + ci->prelen);
79 entry->kind = dirs_file;
80 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
81 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
82 sizeof(entry->mtime));
85 else if (info[0] == sizeof(entry->mtime))
87 strcpy (entry->path, name + ci->prelen);
88 entry->kind = dirs_dir;
89 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
95 struct dirs_info *dirs_open (Dict dict, const char *rep)
98 int before = 0, after;
100 logf (LOG_DEBUG, "dirs_open %s", rep);
101 p = xmalloc (sizeof (*p));
103 strcpy (p->prefix, rep);
104 p->prelen = strlen(p->prefix);
105 strcpy (p->nextpath, rep);
106 p->no_read = p->no_cur = 0;
107 after = p->no_max = 400;
108 p->entries = xmalloc (sizeof(*p->entries) * (p->no_max));
109 logf (LOG_DEBUG, "dirs_open first scan");
110 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
114 struct dirs_info *dirs_fopen (Dict dict, const char *path)
117 struct dirs_entry *entry;
120 p = xmalloc (sizeof(*p));
123 p->entries = xmalloc (sizeof(*p->entries));
129 info = dict_lookup (dict, path);
130 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
132 strcpy (entry->path, path);
133 entry->kind = dirs_file;
134 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
135 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
136 sizeof(entry->mtime));
142 struct dirs_entry *dirs_read (struct dirs_info *p)
144 int before = 0, after = p->no_max+1;
146 if (p->no_read < p->no_cur)
148 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
149 (p->entries + p->no_read)->path);
150 return p->last_entry = p->entries + (p->no_read++);
152 if (p->no_cur < p->no_max)
153 return p->last_entry = NULL;
155 logf (LOG_DEBUG, "dirs_read rescan");
156 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
158 if (p->no_read < p->no_cur)
159 return p->last_entry = p->entries;
160 return p->last_entry = NULL;
163 struct dirs_entry *dirs_last (struct dirs_info *p)
165 return p->last_entry;
168 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
172 sprintf (path, "%s%s", p->prefix, src);
173 logf (LOG_DEBUG, "dirs_mkdir %s", path);
174 dict_insert (p->dict, path, sizeof(mtime), &mtime);
177 void dirs_rmdir (struct dirs_info *p, const char *src)
181 sprintf (path, "%s%s", p->prefix, src);
182 logf (LOG_DEBUG, "dirs_rmdir %s", path);
183 dict_delete (p->dict, path);
186 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
191 sprintf (path, "%s%s", p->prefix, src);
192 logf (LOG_DEBUG, "dirs_add %s", path);
193 memcpy (info, &sysno, sizeof(sysno));
194 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
195 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
198 void dirs_del (struct dirs_info *p, const char *src)
202 sprintf (path, "%s%s", p->prefix, src);
203 logf (LOG_DEBUG, "dirs_del %s", path);
204 dict_delete (p->dict, path);
207 void dirs_free (struct dirs_info **pp)
209 struct dirs_info *p = *pp;