2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-11-28 09:09:46 adam
8 * Zebra config renamed.
9 * Use setting 'recordId' to identify record now.
10 * Bug fix in recindex.c: rec_release_blocks was invokeded even
11 * though the blocks were already released.
12 * File traversal properly deletes records when needed.
14 * Revision 1.12 1995/11/24 11:31:37 adam
15 * Commands add & del read filenames from stdin if source directory is
17 * Match criteria supports 'constant' strings.
19 * Revision 1.11 1995/11/22 17:19:19 adam
20 * Record management uses the bfile system.
22 * Revision 1.10 1995/11/21 15:01:16 adam
23 * New general match criteria implemented.
24 * New feature: document groups.
26 * Revision 1.9 1995/11/21 09:20:32 adam
27 * Yet more work on record match.
29 * Revision 1.8 1995/11/20 16:59:46 adam
30 * New update method: the 'old' keys are saved for each records.
32 * Revision 1.7 1995/11/20 11:56:28 adam
33 * Work on new traversal.
35 * Revision 1.6 1995/11/17 15:54:42 adam
36 * Started work on virtual directory structure.
38 * Revision 1.5 1995/10/17 18:02:09 adam
39 * New feature: databases. Implemented as prefix to words in dictionary.
41 * Revision 1.4 1995/09/28 09:19:46 adam
42 * xfree/xmalloc used everywhere.
43 * Extract/retrieve method seems to work for text records.
45 * Revision 1.3 1995/09/06 16:11:18 adam
46 * Option: only one word key per file.
48 * Revision 1.2 1995/09/04 12:33:43 adam
49 * Various cleanup. YAZ util used instead.
51 * Revision 1.1 1995/09/01 14:06:36 adam
52 * Split of work into more files.
60 #include <sys/types.h>
67 static int repComp (const char *a, const char *b, size_t len)
71 return memcmp (a, b, len);
74 static void repositoryExtractR (int deleteFlag, char *rep,
75 struct recordGroup *rGroup)
79 size_t rep_len = strlen (rep);
84 if (rep[rep_len-1] != '/')
88 for (i=0; e[i].name; i++)
90 strcpy (rep +rep_len+1, e[i].name);
94 fileExtract (NULL, rep, rGroup, deleteFlag);
97 repositoryExtractR (deleteFlag, rep, rGroup);
105 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
109 logf (LOG_LOG, "stdinExtractR");
110 while (scanf ("%s", tmppath) == 1)
111 fileExtract (NULL, tmppath, rGroup, deleteFlag);
114 static void repositoryDeleteR (struct dirs_info *di, struct dirs_entry *dst,
115 const char *base, char *src,
116 struct recordGroup *rGroup)
119 size_t src_len = strlen (src);
121 while (dst && !repComp (dst->path, src, src_len+1))
126 sprintf (tmppath, "%s%s", base, dst->path);
127 fileExtract (&dst->sysno, tmppath, rGroup, 1);
129 strcpy (tmppath, dst->path);
130 dst = dirs_read (di);
131 dirs_del (di, tmppath);
134 strcpy (tmppath, dst->path);
135 dst = dirs_read (di);
136 dirs_rmdir (di, tmppath);
139 dst = dirs_read (di);
144 static void repositoryUpdateR (struct dirs_info *di, struct dirs_entry *dst,
145 const char *base, char *src,
146 struct recordGroup *rGroup)
148 struct dir_entry *e_src;
150 static char tmppath[256];
151 size_t src_len = strlen (src);
153 sprintf (tmppath, "%s%s", base, src);
154 e_src = dir_open (tmppath);
157 if (!dst || repComp (dst->path, src, src_len))
159 if (!dst || strcmp (dst->path, src))
164 if (src_len && src[src_len-1] == '/')
168 src[src_len+1] = '\0';
169 dirs_mkdir (di, src, 0);
174 strcpy (src, dst->path);
175 repositoryDeleteR (di, dst, base, src, rGroup);
180 if (src_len && src[src_len-1] == '/')
184 src[src_len+1] = '\0';
185 dst = dirs_read (di);
193 if (dst && !repComp (dst->path, src, src_len+1))
195 if (e_src[i_src].name)
197 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len+1,
199 sd = strcmp (dst->path + src_len+1, e_src[i_src].name);
204 else if (e_src[i_src].name)
208 logf (LOG_DEBUG, "trav sd=%d", sd);
211 strcpy (src + src_len+1, e_src[i_src].name);
212 sprintf (tmppath, "%s%s", base, src);
214 switch (e_src[i_src].kind)
217 if (e_src[i_src].ctime > dst->ctime)
219 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
221 logf (LOG_LOG, "dirs_add");
222 dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
225 dst = dirs_read (di);
228 repositoryUpdateR (di, dst, base, src, rGroup);
229 dst = dirs_last (di);
230 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
233 dst = dirs_read (di);
240 strcpy (src + src_len+1, e_src[i_src].name);
241 sprintf (tmppath, "%s%s", base, src);
243 switch (e_src[i_src].kind)
246 if (fileExtract (&sysno, tmppath, rGroup, 0))
247 dirs_add (di, src, sysno, e_src[i_src].ctime);
250 repositoryUpdateR (di, dst, base, src, rGroup);
252 dst = dirs_last (di);
259 strcpy (src, dst->path);
260 sprintf (tmppath, "%s%s", base, dst->path);
265 fileExtract (&dst->sysno, tmppath, rGroup, 1);
266 dirs_del (di, dst->path);
267 dst = dirs_read (di);
270 repositoryDeleteR (di, dst, base, src, rGroup);
271 dst = dirs_last (di);
278 static void groupRes (struct recordGroup *rGroup)
283 if (!rGroup->groupName || !*rGroup->groupName)
286 sprintf (gPrefix, "%s.", rGroup->groupName);
288 sprintf (resStr, "%srecordId", gPrefix);
289 rGroup->recordId = res_get (common_resource, resStr);
292 void repositoryUpdate (struct recordGroup *rGroup)
297 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
300 struct dirs_info *di;
302 dict = dict_open ("repdict", 40, 1);
304 assert (rGroup->path);
305 di = dirs_open (dict, rGroup->path);
307 repositoryUpdateR (di, dirs_read (di), rGroup->path, src, rGroup);
313 strcpy (src, rGroup->path);
315 stdinExtractR (0, rGroup);
317 repositoryExtractR (0, src, rGroup);
321 void repositoryDelete (struct recordGroup *rGroup)
325 assert (rGroup->path);
327 strcpy (src, rGroup->path);
329 stdinExtractR (1, rGroup);
331 repositoryExtractR (1, src, rGroup);