1 /* $Id: dirs.c,v 1.18 2002-08-02 19:26:55 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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;
47 static int dirs_client_proc (char *name, const char *info, int pos,
50 struct dirs_info *ci = (struct dirs_info *) client;
51 struct dirs_entry *entry;
53 if (memcmp (name, ci->prefix, ci->prelen))
60 if (ci->no_cur == ci->no_max)
64 entry = ci->entries + ci->no_cur;
65 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
67 strcpy (entry->path, name + ci->prelen);
68 entry->kind = dirs_file;
69 memcpy (&entry->sysno, info+1, sizeof(entry->sysno));
70 memcpy (&entry->mtime, info+1+sizeof(entry->sysno),
71 sizeof(entry->mtime));
74 else if (info[0] == sizeof(entry->mtime))
76 strcpy (entry->path, name + ci->prelen);
77 entry->kind = dirs_dir;
78 memcpy (&entry->mtime, info+1, sizeof(entry->mtime));
84 struct dirs_info *dirs_open (Dict dict, const char *rep, int rw)
87 int before = 0, after;
89 logf (LOG_DEBUG, "dirs_open %s", rep);
90 p = (struct dirs_info *) xmalloc (sizeof (*p));
93 strcpy (p->prefix, rep);
94 p->prelen = strlen(p->prefix);
95 strcpy (p->nextpath, rep);
96 p->no_read = p->no_cur = 0;
97 after = p->no_max = 100;
98 p->entries = (struct dirs_entry *)
99 xmalloc (sizeof(*p->entries) * (p->no_max));
100 logf (LOG_DEBUG, "dirs_open first scan");
101 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
105 struct dirs_info *dirs_fopen (Dict dict, const char *path)
108 struct dirs_entry *entry;
111 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 logf (LOG_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;
146 logf (LOG_DEBUG, "dirs_read rescan");
147 dict_scan (p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
149 if (p->no_read <= p->no_cur)
150 return p->last_entry = p->entries;
151 return p->last_entry = NULL;
154 struct dirs_entry *dirs_last (struct dirs_info *p)
156 return p->last_entry;
159 void dirs_mkdir (struct dirs_info *p, const char *src, time_t mtime)
161 char path[DIRS_MAX_PATH];
163 sprintf (path, "%s%s", p->prefix, src);
164 logf (LOG_DEBUG, "dirs_mkdir %s", path);
166 dict_insert (p->dict, path, sizeof(mtime), &mtime);
169 void dirs_rmdir (struct dirs_info *p, const char *src)
171 char path[DIRS_MAX_PATH];
173 sprintf (path, "%s%s", p->prefix, src);
174 logf (LOG_DEBUG, "dirs_rmdir %s", path);
176 dict_delete (p->dict, path);
179 void dirs_add (struct dirs_info *p, const char *src, int sysno, time_t mtime)
181 char path[DIRS_MAX_PATH];
184 sprintf (path, "%s%s", p->prefix, src);
185 logf (LOG_DEBUG, "dirs_add %s", path);
186 memcpy (info, &sysno, sizeof(sysno));
187 memcpy (info+sizeof(sysno), &mtime, sizeof(mtime));
189 dict_insert (p->dict, path, sizeof(sysno)+sizeof(mtime), info);
192 void dirs_del (struct dirs_info *p, const char *src)
194 char path[DIRS_MAX_PATH];
196 sprintf (path, "%s%s", p->prefix, src);
197 logf (LOG_DEBUG, "dirs_del %s", path);
199 dict_delete (p->dict, path);
202 void dirs_free (struct dirs_info **pp)
204 struct dirs_info *p = *pp;