1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #define DIRS_MAX_PATH 1024
37 struct dirs_entry *entries;
38 char nextpath[DIRS_MAX_PATH];
39 char prefix[DIRS_MAX_PATH];
41 struct dirs_entry *last_entry;
45 static int dirs_client_proc(char *name, const char *info, int pos,
48 struct dirs_info *ci = (struct dirs_info *) client;
49 struct dirs_entry *entry;
51 if (memcmp(name, ci->prefix, ci->prelen))
58 assert(ci->no_cur < ci->no_max);
59 entry = ci->entries + ci->no_cur;
60 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
62 strcpy(entry->path, name + ci->prelen);
63 entry->kind = dirs_file;
64 memcpy(&entry->sysno, info+1, sizeof(entry->sysno));
65 memcpy(&entry->mtime, info+1+sizeof(entry->sysno),
66 sizeof(entry->mtime));
69 else if (info[0] == sizeof(entry->mtime))
71 strcpy(entry->path, name + ci->prelen);
72 entry->kind = dirs_dir;
73 memcpy(&entry->mtime, info+1, sizeof(entry->mtime));
79 struct dirs_info *dirs_open(Dict dict, const char *rep, int rw)
82 int before = 0, after;
84 yaz_log(YLOG_DEBUG, "dirs_open %s", rep);
85 p = (struct dirs_info *) xmalloc(sizeof(*p));
88 strcpy(p->prefix, rep);
89 p->prelen = strlen(p->prefix);
90 strcpy(p->nextpath, rep);
91 p->nextpath_deleted = 0;
92 p->no_read = p->no_cur = 0;
93 after = p->no_max = 100;
94 p->entries = (struct dirs_entry *)
95 xmalloc(sizeof(*p->entries) * (p->no_max));
96 yaz_log(YLOG_DEBUG, "dirs_open first scan");
97 dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
101 struct dirs_info *dirs_fopen(Dict dict, const char *path, int rw)
104 struct dirs_entry *entry;
107 p = (struct dirs_info *) xmalloc(sizeof(*p));
111 p->entries = (struct dirs_entry *) xmalloc(sizeof(*p->entries));
117 info = dict_lookup(dict, path);
118 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
120 strcpy(entry->path, path);
121 entry->kind = dirs_file;
122 memcpy(&entry->sysno, info+1, sizeof(entry->sysno));
123 memcpy(&entry->mtime, info+1+sizeof(entry->sysno),
124 sizeof(entry->mtime));
130 struct dirs_entry *dirs_read(struct dirs_info *p)
132 int before = 0, after = p->no_max+1;
134 if (p->no_read < p->no_cur)
136 yaz_log(YLOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
137 (p->entries + p->no_read)->path);
138 return p->last_entry = p->entries + (p->no_read++);
140 if (p->no_cur < p->no_max)
141 return p->last_entry = NULL;
142 if (p->nextpath_deleted)
150 after = p->no_max + 1;
153 p->nextpath_deleted = 0;
154 yaz_log(YLOG_DEBUG, "dirs_read rescan %s", p->nextpath);
155 dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
156 if (p->no_read <= p->no_cur)
157 return p->last_entry = p->entries;
158 return p->last_entry = NULL;
161 struct dirs_entry *dirs_last(struct dirs_info *p)
163 return p->last_entry;
166 void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime)
168 char path[DIRS_MAX_PATH];
170 sprintf(path, "%s%s", p->prefix, src);
171 yaz_log(YLOG_DEBUG, "dirs_mkdir %s", path);
173 dict_insert(p->dict, path, sizeof(mtime), &mtime);
176 void dirs_rmdir(struct dirs_info *p, const char *src)
178 char path[DIRS_MAX_PATH];
180 sprintf(path, "%s%s", p->prefix, src);
181 yaz_log(YLOG_DEBUG, "dirs_rmdir %s", path);
183 dict_delete(p->dict, path);
186 void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime)
188 char path[DIRS_MAX_PATH];
191 sprintf(path, "%s%s", p->prefix, src);
192 yaz_log(YLOG_DEBUG, "dirs_add %s", path);
193 memcpy(info, &sysno, sizeof(sysno));
194 memcpy(info+sizeof(sysno), &mtime, sizeof(mtime));
196 dict_insert(p->dict, path, sizeof(sysno)+sizeof(mtime), info);
199 void dirs_del(struct dirs_info *p, const char *src)
201 char path[DIRS_MAX_PATH];
203 sprintf(path, "%s%s", p->prefix, src);
204 yaz_log(YLOG_DEBUG, "dirs_del %s", path);
207 if (!strcmp(path, p->nextpath))
208 p->nextpath_deleted = 1;
209 dict_delete(p->dict, path);
213 void dirs_free(struct dirs_info **pp)
215 struct dirs_info *p = *pp;
225 * indent-tabs-mode: nil
227 * vim: shiftwidth=4 tabstop=8 expandtab