2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1996-03-19 12:43:27 adam
8 * Bug fix: File update traversal didn't handle trailing slashes correctly.
9 * Bug fix: Update of sub directory groups wasn't handled correctly.
11 * Revision 1.17 1996/02/12 18:45:17 adam
12 * Changed naming of some functions.
14 * Revision 1.16 1996/02/05 12:30:02 adam
15 * Logging reduced a bit.
16 * The remaining running time is estimated during register merge.
18 * Revision 1.15 1995/12/07 17:38:48 adam
19 * Work locking mechanisms for concurrent updates/commit.
21 * Revision 1.14 1995/12/06 12:41:26 adam
22 * New command 'stat' for the index program.
23 * Filenames can be read from stdin by specifying '-'.
24 * Bug fix/enhancement of the transformation from terms to regular
25 * expressons in the search engine.
27 * Revision 1.13 1995/11/28 09:09:46 adam
28 * Zebra config renamed.
29 * Use setting 'recordId' to identify record now.
30 * Bug fix in recindex.c: rec_release_blocks was invokeded even
31 * though the blocks were already released.
32 * File traversal properly deletes records when needed.
34 * Revision 1.12 1995/11/24 11:31:37 adam
35 * Commands add & del read filenames from stdin if source directory is
37 * Match criteria supports 'constant' strings.
39 * Revision 1.11 1995/11/22 17:19:19 adam
40 * Record management uses the bfile system.
42 * Revision 1.10 1995/11/21 15:01:16 adam
43 * New general match criteria implemented.
44 * New feature: document groups.
46 * Revision 1.9 1995/11/21 09:20:32 adam
47 * Yet more work on record match.
49 * Revision 1.8 1995/11/20 16:59:46 adam
50 * New update method: the 'old' keys are saved for each records.
52 * Revision 1.7 1995/11/20 11:56:28 adam
53 * Work on new traversal.
55 * Revision 1.6 1995/11/17 15:54:42 adam
56 * Started work on virtual directory structure.
58 * Revision 1.5 1995/10/17 18:02:09 adam
59 * New feature: databases. Implemented as prefix to words in dictionary.
61 * Revision 1.4 1995/09/28 09:19:46 adam
62 * xfree/xmalloc used everywhere.
63 * Extract/retrieve method seems to work for text records.
65 * Revision 1.3 1995/09/06 16:11:18 adam
66 * Option: only one word key per file.
68 * Revision 1.2 1995/09/04 12:33:43 adam
69 * Various cleanup. YAZ util used instead.
71 * Revision 1.1 1995/09/01 14:06:36 adam
72 * Split of work into more files.
80 #include <sys/types.h>
87 static int repComp (const char *a, const char *b, size_t len)
91 return memcmp (a, b, len);
94 static void repositoryExtractR (int deleteFlag, char *rep,
95 struct recordGroup *rGroup)
99 size_t rep_len = strlen (rep);
104 logf (LOG_LOG, "Dir: %s", rep);
105 if (rep[rep_len-1] != '/')
109 for (i=0; e[i].name; i++)
111 strcpy (rep +rep_len+1, e[i].name);
115 fileExtract (NULL, rep, rGroup, deleteFlag);
118 repositoryExtractR (deleteFlag, rep, rGroup);
126 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
130 logf (LOG_LOG, "stdinExtractR");
131 while (scanf ("%s", tmppath) == 1)
132 fileExtract (NULL, tmppath, rGroup, deleteFlag);
135 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
136 const char *base, char *src,
137 struct recordGroup *rGroup)
140 size_t src_len = strlen (src);
142 while (dst && !repComp (dst->path, src, src_len+1))
147 sprintf (tmppath, "%s%s", base, dst->path);
148 fileExtract (&dst->sysno, tmppath, rGroup, 1);
150 strcpy (tmppath, dst->path);
151 dst = dirs_read (di);
152 dirs_del (di, tmppath);
155 strcpy (tmppath, dst->path);
156 dst = dirs_read (di);
157 dirs_rmdir (di, tmppath);
160 dst = dirs_read (di);
165 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
166 const char *base, char *src,
167 struct recordGroup *rGroup)
169 struct dir_entry *e_src;
171 static char tmppath[1024];
172 size_t src_len = strlen (src);
174 sprintf (tmppath, "%s%s", base, src);
175 e_src = dir_open (tmppath);
176 logf (LOG_LOG, "Dir: %s", tmppath);
179 if (!dst || repComp (dst->path, src, src_len))
181 if (!dst || strcmp (dst->path, src))
187 if (src_len && src[src_len-1] != '/')
190 src[++src_len] = '\0';
192 dirs_mkdir (di, src, 0);
199 strcpy (src, dst->path);
200 fileDeleteR (di, dst, base, src, rGroup);
205 if (src_len && src[src_len-1] != '/')
208 src[++src_len] = '\0';
210 dst = dirs_read (di);
218 if (dst && !repComp (dst->path, src, src_len))
220 if (e_src[i_src].name)
222 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
224 sd = strcmp (dst->path + src_len, e_src[i_src].name);
229 else if (e_src[i_src].name)
233 logf (LOG_DEBUG, "trav sd=%d", sd);
236 strcpy (src + src_len, e_src[i_src].name);
237 sprintf (tmppath, "%s%s", base, src);
239 switch (e_src[i_src].kind)
242 if (e_src[i_src].ctime > dst->ctime)
244 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
246 dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
249 dst = dirs_read (di);
252 fileUpdateR (di, dst, base, src, rGroup);
253 dst = dirs_last (di);
254 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
257 dst = dirs_read (di);
264 strcpy (src + src_len, e_src[i_src].name);
265 sprintf (tmppath, "%s%s", base, src);
267 switch (e_src[i_src].kind)
270 if (fileExtract (&sysno, tmppath, rGroup, 0))
271 dirs_add (di, src, sysno, e_src[i_src].ctime);
274 fileUpdateR (di, dst, base, src, rGroup);
276 dst = dirs_last (di);
283 strcpy (src, dst->path);
284 sprintf (tmppath, "%s%s", base, dst->path);
289 fileExtract (&dst->sysno, tmppath, rGroup, 1);
290 dirs_del (di, dst->path);
291 dst = dirs_read (di);
294 fileDeleteR (di, dst, base, src, rGroup);
295 dst = dirs_last (di);
302 static void groupRes (struct recordGroup *rGroup)
307 if (!rGroup->groupName || !*rGroup->groupName)
310 sprintf (gPrefix, "%s.", rGroup->groupName);
312 sprintf (resStr, "%srecordId", gPrefix);
313 rGroup->recordId = res_get (common_resource, resStr);
317 void repositoryShow (struct recordGroup *rGroup)
321 struct dirs_entry *dst;
323 struct dirs_info *di;
325 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
327 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
331 assert (rGroup->path);
332 strcpy (src, rGroup->path);
333 src_len = strlen (src);
335 if (src_len && src[src_len-1] != '/')
338 src[++src_len] = '\0';
341 di = dirs_open (dict, src);
343 while ( (dst = dirs_read (di)) )
344 logf (LOG_LOG, "%s", dst->path);
349 void repositoryUpdate (struct recordGroup *rGroup)
356 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
359 struct dirs_info *di;
361 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
363 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
366 assert (rGroup->path);
368 strcpy (src, rGroup->path);
369 src_len = strlen (src);
371 if (src_len && src[src_len-1] != '/')
374 src[++src_len] = '\0';
377 di = dirs_open (dict, src);
380 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
386 strcpy (src, rGroup->path);
387 if (*src == '\0' || !strcmp (src, "-"))
388 stdinExtractR (0, rGroup);
390 repositoryExtractR (0, src, rGroup);
394 void repositoryDelete (struct recordGroup *rGroup)
398 assert (rGroup->path);
400 strcpy (src, rGroup->path);
401 if (*src == '\0' || !strcmp(src, "-"))
402 stdinExtractR (1, rGroup);
404 repositoryExtractR (1, src, rGroup);