2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1996-04-12 07:02:21 adam
8 * File update of single files.
10 * Revision 1.7 1996/03/21 14:50:09 adam
11 * File update uses modify-time instead of change-time.
13 * Revision 1.6 1996/02/02 13:44:43 adam
14 * The public dictionary functions simply use char instead of Dict_char
15 * to represent search strings. Dict_char is used internally only.
17 * Revision 1.5 1996/01/17 14:54:44 adam
18 * Function dirs_rmdir uses dict_delete.
20 * Revision 1.4 1995/11/30 08:34:27 adam
21 * Started work on commit facility.
22 * Changed a few malloc/free to xmalloc/xfree.
24 * Revision 1.3 1995/11/20 16:59:45 adam
25 * New update method: the 'old' keys are saved for each records.
27 * Revision 1.2 1995/11/20 11:56:23 adam
28 * Work on new traversal.
30 * Revision 1.1 1995/11/17 15:54:42 adam
31 * Started work on virtual directory structure.
47 struct dirs_entry *entries;
51 struct dirs_entry *last_entry;
54 static int dirs_client_proc (char *name, const char *info, int pos,
57 struct dirs_info *ci = client;
58 struct dirs_entry *entry;
60 if (memcmp (name, ci->prefix, ci->prelen))
67 if (ci->no_cur == ci->no_max)
71 entry = ci->entries + ci->no_cur;
72 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
74 strcpy (entry->path, name + ci->prelen);
75 entry->kind = dirs_file;
76 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
77 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
78 sizeof(entry->mtime));
81 else if (info[0] == sizeof(entry->mtime))
83 strcpy (entry->path, name + ci->prelen);
84 entry->kind = dirs_dir;
85 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
91 struct dirs_info *dirs_open (Dict dict, const char *rep)
94 int before = 0, after;
96 logf (LOG_DEBUG, "dirs_open %s", rep);
97 p = xmalloc (sizeof (*p));
99 strcpy (p->prefix, rep);
100 p->prelen = strlen(p->prefix);
101 strcpy (p->nextpath, rep);
102 p->no_read = p->no_cur = 0;
103 after = p->no_max = 400;
104 p->entries = xmalloc (sizeof(*p->entries) * (p->no_max));
105 logf (LOG_DEBUG, "dirs_open first scan");
106 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
110 struct dirs_info *dirs_fopen (Dict dict, const char *path)
113 struct dirs_entry *entry;
116 p = xmalloc (sizeof(*p));
119 p->entries = xmalloc (sizeof(*p->entries));
125 info = dict_lookup (dict, path);
126 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
128 strcpy (entry->path, path);
129 entry->kind = dirs_file;
130 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
131 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
132 sizeof(entry->mtime));
138 struct dirs_entry *dirs_read (struct dirs_info *p)
140 int before = 0, after = p->no_max+1;
142 if (p->no_read < p->no_cur)
144 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
145 (p->entries + p->no_read)->path);
146 return p->last_entry = p->entries + (p->no_read++);
148 if (p->no_cur < p->no_max)
149 return p->last_entry = NULL;
151 logf (LOG_DEBUG, "dirs_read rescan");
152 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
154 if (p->no_read < p->no_cur)
155 return p->last_entry = p->entries;
156 return p->last_entry = NULL;
159 struct dirs_entry *dirs_last (struct dirs_info *p)
161 return p->last_entry;
164 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
168 sprintf (path, "%s%s", p->prefix, src);
169 logf (LOG_DEBUG, "dirs_mkdir %s", path);
170 dict_insert (p->dict, path, sizeof(mtime), &mtime);
173 void dirs_rmdir (struct dirs_info *p, const char *src)
177 sprintf (path, "%s%s", p->prefix, src);
178 logf (LOG_DEBUG, "dirs_rmdir %s", path);
179 dict_delete (p->dict, path);
182 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
187 sprintf (path, "%s%s", p->prefix, src);
188 logf (LOG_DEBUG, "dirs_add %s", path);
189 memcpy (info, &sysno, sizeof(sysno));
190 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
191 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
194 void dirs_del (struct dirs_info *p, const char *src)
199 sprintf (path, "%s%s", p->prefix, src);
200 logf (LOG_DEBUG, "dirs_del %s", path);
202 dict_insert (p->dict, path, 1, info);
205 void dirs_free (struct dirs_info **pp)
207 struct dirs_info *p = *pp;