2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-11-20 16:59:45 adam
8 * New update method: the 'old' keys are saved for each records.
10 * Revision 1.2 1995/11/20 11:56:23 adam
11 * Work on new traversal.
13 * Revision 1.1 1995/11/17 15:54:42 adam
14 * Started work on virtual directory structure.
30 struct dirs_entry *entries;
34 struct dirs_entry *last_entry;
37 static int dirs_client_proc (Dict_char *name, const char *info, int pos,
40 struct dirs_info *ci = client;
41 struct dirs_entry *entry;
43 if (memcmp (name, ci->prefix, ci->prelen))
50 if (ci->no_cur == ci->no_max)
54 entry = ci->entries + ci->no_cur;
55 if (info[0] == sizeof(entry->sysno)+sizeof(entry->ctime))
57 strcpy (entry->path, name + ci->prelen);
58 entry->kind = dirs_file;
59 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
60 memcpy (&entry->ctime, info+1+sizeof(entry->sysno),
61 sizeof(entry->ctime));
64 else if (info[0] == sizeof(entry->ctime))
66 strcpy (entry->path, name + ci->prelen);
67 entry->kind = dirs_dir;
68 memcpy (&entry->ctime, info+1, sizeof(entry->ctime));
74 struct dirs_info *dirs_open (Dict dict, const char *rep)
77 int before = 0, after;
79 logf (LOG_DEBUG, "dirs_open %s", rep);
80 if (!(p = malloc (sizeof (*p))))
82 logf (LOG_FATAL|LOG_ERRNO, "malloc");
86 strcpy (p->prefix, rep);
87 p->prelen = strlen(p->prefix);
88 strcpy (p->nextpath, rep);
89 p->no_read = p->no_cur = 0;
90 after = p->no_max = 400;
91 if (!(p->entries = malloc (sizeof(*p->entries) * (p->no_max))))
93 logf (LOG_FATAL|LOG_ERRNO, "malloc");
96 logf (LOG_DEBUG, "dirs_open first scan");
97 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
101 struct dirs_entry *dirs_read (struct dirs_info *p)
103 int before = 0, after = p->no_max+1;
105 if (p->no_read < p->no_cur)
107 logf (LOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
108 (p->entries + p->no_read)->path);
109 return p->last_entry = p->entries + (p->no_read++);
111 if (p->no_cur < p->no_max)
112 return p->last_entry = NULL;
114 logf (LOG_DEBUG, "dirs_read rescan");
115 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
117 if (p->no_read < p->no_cur)
118 return p->last_entry = p->entries;
119 return p->last_entry = NULL;
122 struct dirs_entry *dirs_last (struct dirs_info *p)
124 return p->last_entry;
127 void dirs_mkdir (struct dirs_info *p, const char *src, int ctime)
131 sprintf (path, "%s%s", p->prefix, src);
132 logf (LOG_DEBUG, "dirs_mkdir %s", path);
133 dict_insert (p->dict, path, sizeof(ctime), &ctime);
136 void dirs_rmdir (struct dirs_info *p, const char *src)
141 sprintf (path, "%s%s", p->prefix, src);
142 logf (LOG_DEBUG, "dirs_rmdir %s", path);
144 dict_insert (p->dict, path, 1, info);
147 void dirs_add (struct dirs_info *p, const char *src, int sysno, int ctime)
152 sprintf (path, "%s%s", p->prefix, src);
153 logf (LOG_DEBUG, "dirs_add %s", path);
154 memcpy (info, &sysno, sizeof(sysno));
155 memcpy (info+sizeof(sysno), &ctime, sizeof(ctime));
156 dict_insert (p->dict, path, sizeof(sysno)+sizeof(ctime), info);
159 void dirs_del (struct dirs_info *p, const char *src)
164 sprintf (path, "%s%s", p->prefix, src);
165 logf (LOG_DEBUG, "dirs_del %s", path);
167 dict_insert (p->dict, path, 1, info);
170 void dirs_free (struct dirs_info **pp)
172 struct dirs_info *p = *pp;