IDZEBRA 2.2.8
dirs.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20
21#if HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdio.h>
25#include <string.h>
26#include <assert.h>
27#include <errno.h>
28#include <fcntl.h>
29
30#include <yaz/snprintf.h>
31#include "index.h"
32
33#define DIRS_MAX_PATH 1024
34
48
49static int dirs_client_proc(char *name, const char *info, int pos,
50 void *client)
51{
52 struct dirs_info *ci = (struct dirs_info *) client;
53 struct dirs_entry *entry;
54
55 if (memcmp(name, ci->prefix, ci->prelen))
56 return 1;
57 if (ci->no_cur < 0)
58 {
59 ci->no_cur = 0;
60 return 0;
61 }
62 assert(ci->no_cur < ci->no_max);
63 entry = ci->entries + ci->no_cur;
64 if (info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
65 {
66 strcpy(entry->path, name + ci->prelen);
67 entry->kind = dirs_file;
68 memcpy(&entry->sysno, info+1, sizeof(entry->sysno));
69 memcpy(&entry->mtime, info+1+sizeof(entry->sysno),
70 sizeof(entry->mtime));
71 ci->no_cur++;
72 }
73 else if (info[0] == sizeof(entry->mtime))
74 {
75 strcpy(entry->path, name + ci->prelen);
76 entry->kind = dirs_dir;
77 memcpy(&entry->mtime, info+1, sizeof(entry->mtime));
78 ci->no_cur++;
79 }
80 return 0;
81}
82
83struct dirs_info *dirs_open(Dict dict, const char *rep, int rw)
84{
85 struct dirs_info *p;
86 int before = 0, after;
87
88 yaz_log(YLOG_DEBUG, "dirs_open %s", rep);
89 p = (struct dirs_info *) xmalloc(sizeof(*p));
90 p->dict = dict;
91 p->rw = rw;
92 strcpy(p->prefix, rep);
93 p->prelen = strlen(p->prefix);
94 strcpy(p->nextpath, rep);
95 p->nextpath_deleted = 0;
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 yaz_log(YLOG_DEBUG, "dirs_open first scan");
101 dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
102 return p;
103}
104
105struct dirs_info *dirs_fopen(Dict dict, const char *path, int rw)
106{
107 struct dirs_info *p;
108 struct dirs_entry *entry;
109 char *info;
110
111 p = (struct dirs_info *) xmalloc(sizeof(*p));
112 p->dict = dict;
113 p->rw = rw;
114 *p->prefix = '\0';
115 p->entries = (struct dirs_entry *) xmalloc(sizeof(*p->entries));
116 p->no_read = 0;
117 p->no_cur = 0;
118 p->no_max = 2;
119
120 entry = p->entries;
121 info = dict_lookup(dict, path);
122 if (info && info[0] == sizeof(entry->sysno)+sizeof(entry->mtime))
123 {
124 strcpy(entry->path, path);
125 entry->kind = dirs_file;
126 memcpy(&entry->sysno, info+1, sizeof(entry->sysno));
127 memcpy(&entry->mtime, info+1+sizeof(entry->sysno),
128 sizeof(entry->mtime));
129 p->no_cur++;
130 }
131 return p;
132}
133
135{
136 int before = 0, after = p->no_max+1;
137
138 if (p->no_read < p->no_cur)
139 {
140 yaz_log(YLOG_DEBUG, "dirs_read %d. returns %s", p->no_read,
141 (p->entries + p->no_read)->path);
142 return p->last_entry = p->entries + (p->no_read++);
143 }
144 if (p->no_cur < p->no_max)
145 return p->last_entry = NULL;
146 if (p->nextpath_deleted)
147 {
148 p->no_cur = 0;
149 after = p->no_max;
150 }
151 else
152 {
153 p->no_cur = -1;
154 after = p->no_max + 1;
155 }
156 p->no_read = 1;
157 p->nextpath_deleted = 0;
158 yaz_log(YLOG_DEBUG, "dirs_read rescan %s", p->nextpath);
159 dict_scan(p->dict, p->nextpath, &before, &after, p, dirs_client_proc);
160 if (p->no_read <= p->no_cur)
161 return p->last_entry = p->entries;
162 return p->last_entry = NULL;
163}
164
166{
167 return p->last_entry;
168}
169
170void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime)
171{
172 char path[DIRS_MAX_PATH];
173
174 yaz_snprintf(path, sizeof(path), "%s%s", p->prefix, src);
175 yaz_log(YLOG_DEBUG, "dirs_mkdir %s", path);
176 if (p->rw)
177 dict_insert(p->dict, path, sizeof(mtime), &mtime);
178}
179
180void dirs_rmdir(struct dirs_info *p, const char *src)
181{
182 char path[DIRS_MAX_PATH];
183
184 yaz_snprintf(path, sizeof(path), "%s%s", p->prefix, src);
185 yaz_log(YLOG_DEBUG, "dirs_rmdir %s", path);
186 if (p->rw)
187 dict_delete(p->dict, path);
188}
189
190void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime)
191{
192 char path[DIRS_MAX_PATH];
193 char info[16];
194
195 yaz_snprintf(path, sizeof(path), "%s%s", p->prefix, src);
196 yaz_log(YLOG_DEBUG, "dirs_add %s", path);
197 memcpy(info, &sysno, sizeof(sysno));
198 memcpy(info+sizeof(sysno), &mtime, sizeof(mtime));
199 if (p->rw)
200 dict_insert(p->dict, path, sizeof(sysno)+sizeof(mtime), info);
201}
202
203void dirs_del(struct dirs_info *p, const char *src)
204{
205 char path[DIRS_MAX_PATH];
206
207 yaz_snprintf(path, sizeof(path), "%s%s", p->prefix, src);
208 yaz_log(YLOG_DEBUG, "dirs_del %s", path);
209 if (p->rw)
210 {
211 if (!strcmp(path, p->nextpath))
212 p->nextpath_deleted = 1;
213 dict_delete(p->dict, path);
214 }
215}
216
217void dirs_free(struct dirs_info **pp)
218{
219 struct dirs_info *p = *pp;
220
221 xfree(p->entries);
222 xfree(p);
223 *pp = NULL;
224}
225
226/*
227 * Local variables:
228 * c-basic-offset: 4
229 * c-file-style: "Stroustrup"
230 * indent-tabs-mode: nil
231 * End:
232 * vim: shiftwidth=4 tabstop=8 expandtab
233 */
234
int dict_delete(Dict dict, const char *p)
deletes item from dictionary
Definition delete.c:260
int dict_insert(Dict dict, const char *p, int userlen, void *userinfo)
insert item into dictionary
Definition insert.c:439
char * dict_lookup(Dict dict, const char *p)
lookup item in dictionary
Definition lookup.c:100
int dict_scan(Dict dict, char *str, int *before, int *after, void *client, int(*f)(char *name, const char *info, int pos, void *client))
dictionary scan
Definition scan.c:242
static Dict dict
Definition dicttest.c:35
void dirs_mkdir(struct dirs_info *p, const char *src, time_t mtime)
Definition dirs.c:170
static int dirs_client_proc(char *name, const char *info, int pos, void *client)
Definition dirs.c:49
void dirs_rmdir(struct dirs_info *p, const char *src)
Definition dirs.c:180
void dirs_add(struct dirs_info *p, const char *src, zint sysno, time_t mtime)
Definition dirs.c:190
#define DIRS_MAX_PATH
Definition dirs.c:33
struct dirs_info * dirs_fopen(Dict dict, const char *path, int rw)
Definition dirs.c:105
void dirs_free(struct dirs_info **pp)
Definition dirs.c:217
struct dirs_info * dirs_open(Dict dict, const char *rep, int rw)
Definition dirs.c:83
struct dirs_entry * dirs_read(struct dirs_info *p)
Definition dirs.c:134
struct dirs_entry * dirs_last(struct dirs_info *p)
Definition dirs.c:165
void dirs_del(struct dirs_info *p, const char *src)
Definition dirs.c:203
@ dirs_file
Definition index.h:55
@ dirs_dir
Definition index.h:55
Definition index.h:63
zint sysno
Definition index.h:66
enum dirsKind kind
Definition index.h:64
char path[256]
Definition index.h:65
time_t mtime
Definition index.h:67
char nextpath[DIRS_MAX_PATH]
Definition dirs.c:42
int no_max
Definition dirs.c:40
char prefix[DIRS_MAX_PATH]
Definition dirs.c:43
int rw
Definition dirs.c:37
struct dirs_entry * last_entry
Definition dirs.c:45
Dict dict
Definition dirs.c:36
struct dirs_entry * entries
Definition dirs.c:41
int nextpath_deleted
Definition dirs.c:46
int no_read
Definition dirs.c:38
int no_cur
Definition dirs.c:39
int prelen
Definition dirs.c:44
long zint
Zebra integer.
Definition util.h:66