2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1995-12-06 12:41:26 adam
8 * New command 'stat' for the index program.
9 * Filenames can be read from stdin by specifying '-'.
10 * Bug fix/enhancement of the transformation from terms to regular
11 * expressons in the search engine.
13 * Revision 1.13 1995/11/28 09:09:46 adam
14 * Zebra config renamed.
15 * Use setting 'recordId' to identify record now.
16 * Bug fix in recindex.c: rec_release_blocks was invokeded even
17 * though the blocks were already released.
18 * File traversal properly deletes records when needed.
20 * Revision 1.12 1995/11/24 11:31:37 adam
21 * Commands add & del read filenames from stdin if source directory is
23 * Match criteria supports 'constant' strings.
25 * Revision 1.11 1995/11/22 17:19:19 adam
26 * Record management uses the bfile system.
28 * Revision 1.10 1995/11/21 15:01:16 adam
29 * New general match criteria implemented.
30 * New feature: document groups.
32 * Revision 1.9 1995/11/21 09:20:32 adam
33 * Yet more work on record match.
35 * Revision 1.8 1995/11/20 16:59:46 adam
36 * New update method: the 'old' keys are saved for each records.
38 * Revision 1.7 1995/11/20 11:56:28 adam
39 * Work on new traversal.
41 * Revision 1.6 1995/11/17 15:54:42 adam
42 * Started work on virtual directory structure.
44 * Revision 1.5 1995/10/17 18:02:09 adam
45 * New feature: databases. Implemented as prefix to words in dictionary.
47 * Revision 1.4 1995/09/28 09:19:46 adam
48 * xfree/xmalloc used everywhere.
49 * Extract/retrieve method seems to work for text records.
51 * Revision 1.3 1995/09/06 16:11:18 adam
52 * Option: only one word key per file.
54 * Revision 1.2 1995/09/04 12:33:43 adam
55 * Various cleanup. YAZ util used instead.
57 * Revision 1.1 1995/09/01 14:06:36 adam
58 * Split of work into more files.
66 #include <sys/types.h>
73 static int repComp (const char *a, const char *b, size_t len)
77 return memcmp (a, b, len);
80 static void repositoryExtractR (int deleteFlag, char *rep,
81 struct recordGroup *rGroup)
85 size_t rep_len = strlen (rep);
90 if (rep[rep_len-1] != '/')
94 for (i=0; e[i].name; i++)
96 strcpy (rep +rep_len+1, e[i].name);
100 fileExtract (NULL, rep, rGroup, deleteFlag);
103 repositoryExtractR (deleteFlag, rep, rGroup);
111 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
115 logf (LOG_LOG, "stdinExtractR");
116 while (scanf ("%s", tmppath) == 1)
117 fileExtract (NULL, tmppath, rGroup, deleteFlag);
120 static void repositoryDeleteR (struct dirs_info *di, struct dirs_entry *dst,
121 const char *base, char *src,
122 struct recordGroup *rGroup)
125 size_t src_len = strlen (src);
127 while (dst && !repComp (dst->path, src, src_len+1))
132 sprintf (tmppath, "%s%s", base, dst->path);
133 fileExtract (&dst->sysno, tmppath, rGroup, 1);
135 strcpy (tmppath, dst->path);
136 dst = dirs_read (di);
137 dirs_del (di, tmppath);
140 strcpy (tmppath, dst->path);
141 dst = dirs_read (di);
142 dirs_rmdir (di, tmppath);
145 dst = dirs_read (di);
150 static void repositoryUpdateR (struct dirs_info *di, struct dirs_entry *dst,
151 const char *base, char *src,
152 struct recordGroup *rGroup)
154 struct dir_entry *e_src;
156 static char tmppath[256];
157 size_t src_len = strlen (src);
159 sprintf (tmppath, "%s%s", base, src);
160 e_src = dir_open (tmppath);
163 if (!dst || repComp (dst->path, src, src_len))
165 if (!dst || strcmp (dst->path, src))
170 if (src_len && src[src_len-1] == '/')
174 src[src_len+1] = '\0';
175 dirs_mkdir (di, src, 0);
180 strcpy (src, dst->path);
181 repositoryDeleteR (di, dst, base, src, rGroup);
186 if (src_len && src[src_len-1] == '/')
190 src[src_len+1] = '\0';
191 dst = dirs_read (di);
199 if (dst && !repComp (dst->path, src, src_len+1))
201 if (e_src[i_src].name)
203 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len+1,
205 sd = strcmp (dst->path + src_len+1, e_src[i_src].name);
210 else if (e_src[i_src].name)
214 logf (LOG_DEBUG, "trav sd=%d", sd);
217 strcpy (src + src_len+1, e_src[i_src].name);
218 sprintf (tmppath, "%s%s", base, src);
220 switch (e_src[i_src].kind)
223 if (e_src[i_src].ctime > dst->ctime)
225 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
227 logf (LOG_LOG, "dirs_add");
228 dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
231 dst = dirs_read (di);
234 repositoryUpdateR (di, dst, base, src, rGroup);
235 dst = dirs_last (di);
236 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
239 dst = dirs_read (di);
246 strcpy (src + src_len+1, e_src[i_src].name);
247 sprintf (tmppath, "%s%s", base, src);
249 switch (e_src[i_src].kind)
252 if (fileExtract (&sysno, tmppath, rGroup, 0))
253 dirs_add (di, src, sysno, e_src[i_src].ctime);
256 repositoryUpdateR (di, dst, base, src, rGroup);
258 dst = dirs_last (di);
265 strcpy (src, dst->path);
266 sprintf (tmppath, "%s%s", base, dst->path);
271 fileExtract (&dst->sysno, tmppath, rGroup, 1);
272 dirs_del (di, dst->path);
273 dst = dirs_read (di);
276 repositoryDeleteR (di, dst, base, src, rGroup);
277 dst = dirs_last (di);
284 static void groupRes (struct recordGroup *rGroup)
289 if (!rGroup->groupName || !*rGroup->groupName)
292 sprintf (gPrefix, "%s.", rGroup->groupName);
294 sprintf (resStr, "%srecordId", gPrefix);
295 rGroup->recordId = res_get (common_resource, resStr);
298 void repositoryUpdate (struct recordGroup *rGroup)
303 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
306 struct dirs_info *di;
308 dict = dict_open ("repdict", 40, 1);
310 assert (rGroup->path);
311 di = dirs_open (dict, rGroup->path);
313 repositoryUpdateR (di, dirs_read (di), rGroup->path, src, rGroup);
319 strcpy (src, rGroup->path);
320 if (*src == '\0' || !strcmp (src, "-"))
321 stdinExtractR (0, rGroup);
323 repositoryExtractR (0, src, rGroup);
327 void repositoryDelete (struct recordGroup *rGroup)
331 assert (rGroup->path);
333 strcpy (src, rGroup->path);
334 if (*src == '\0' || !strcmp(src, "-"))
335 stdinExtractR (1, rGroup);
337 repositoryExtractR (1, src, rGroup);