1 /* $Id: dirs.c,v 1.24 2006-05-10 08:13:20 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
32 #define DIRS_MAX_PATH 1024
40 struct dirs_entry *entries;
41 char nextpath[DIRS_MAX_PATH];
42 char prefix[DIRS_MAX_PATH];
44 struct dirs_entry *last_entry;
48 static int dirs_client_proc (char *name, const char *info, int pos,
51 struct dirs_info *ci = (struct dirs_info *) client;
52 struct dirs_entry *entry;
54 if (memcmp (name, ci->prefix, ci->prelen))
61 assert (ci->no_cur < ci->no_max);
62 entry = ci->entries + ci->no_cur;
63 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
65 strcpy (entry->path, name + ci->prelen);
66 entry->kind = dirs_file;
67 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
68 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
69 sizeof(entry->mtime));
72 else if (info[0] == sizeof(entry->mtime))
74 strcpy (entry->path, name + ci->prelen);
75 entry->kind = dirs_dir;
76 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
82 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
85 int before = 0, after;
87 yaz_log (YLOG_DEBUG, "dirs_open %s", rep);
88 p = (struct dirs_info *) xmalloc (sizeof (*p));
91 strcpy (p->prefix, rep);
92 p->prelen = strlen(p->prefix);
93 strcpy (p->nextpath, rep);
94 p->nextpath_deleted = 0;
95 p->no_read = p->no_cur = 0;
96 after = p->no_max = 100;
97 p->entries = (struct dirs_entry *)
98 xmalloc (sizeof(*p->entries) * (p->no_max));
99 yaz_log (YLOG_DEBUG, "dirs_open first scan");
100 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
104 struct dirs_info *dirs_fopen (Dict dict, const char *path, int rw)
107 struct dirs_entry *entry;
110 p = (struct dirs_info *) xmalloc (sizeof(*p));
114 p->entries = (struct dirs_entry *) xmalloc (sizeof(*p->entries));
120 info = dict_lookup (dict, path);
121 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
123 strcpy (entry->path, path);
124 entry->kind = dirs_file;
125 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
126 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
127 sizeof(entry->mtime));
133 struct dirs_entry *dirs_read (struct dirs_info *p)
135 int before = 0, after = p->no_max+1;
137 if (p->no_read < p->no_cur)
139 yaz_log (YLOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
140 (p->entries + p->no_read)->path);
141 return p->last_entry = p->entries + (p->no_read++);
143 if (p->no_cur < p->no_max)
144 return p->last_entry = NULL;
145 if (p->nextpath_deleted)
153 after = p->no_max + 1;
156 p->nextpath_deleted = 0;
157 yaz_log (YLOG_DEBUG, "dirs_read rescan %s", p->nextpath);
158 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
159 if (p->no_read <= p->no_cur)
160 return p->last_entry = p->entries;
161 return p->last_entry = NULL;
164 struct dirs_entry *dirs_last (struct dirs_info *p)
166 return p->last_entry;
169 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
171 char path[DIRS_MAX_PATH];
173 sprintf (path, "%s%s", p->prefix, src);
174 yaz_log (YLOG_DEBUG, "dirs_mkdir %s", path);
176 dict_insert (p->dict, path, sizeof(mtime), &mtime);
179 void dirs_rmdir (struct dirs_info *p, const char *src)
181 char path[DIRS_MAX_PATH];
183 sprintf (path, "%s%s", p->prefix, src);
184 yaz_log (YLOG_DEBUG, "dirs_rmdir %s", path);
186 dict_delete (p->dict, path);
189 void dirs_add (struct dirs_info *p, const char *src, SYSNO sysno, time_t mtime)
191 char path[DIRS_MAX_PATH];
194 sprintf (path, "%s%s", p->prefix, src);
195 yaz_log (YLOG_DEBUG, "dirs_add %s", path);
196 memcpy (info, &sysno, sizeof(sysno));
197 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
199 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
202 void dirs_del (struct dirs_info *p, const char *src)
204 char path[DIRS_MAX_PATH];
206 sprintf (path, "%s%s", p->prefix, src);
207 yaz_log (YLOG_DEBUG, "dirs_del %s", path);
210 if (!strcmp(path, p->nextpath))
211 p->nextpath_deleted = 1;
212 dict_delete (p->dict, path);
216 void dirs_free (struct dirs_info **pp)
218 struct dirs_info *p = *pp;
228 * indent-tabs-mode: nil
230 * vim: shiftwidth=4 tabstop=8 expandtab