2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1996-03-20 16:16:55 quinn
8 * Added diagnostic output
10 * Revision 1.18 1996/03/19 12:43:27 adam
11 * Bug fix: File update traversal didn't handle trailing slashes correctly.
12 * Bug fix: Update of sub directory groups wasn't handled correctly.
14 * Revision 1.17 1996/02/12 18:45:17 adam
15 * Changed naming of some functions.
17 * Revision 1.16 1996/02/05 12:30:02 adam
18 * Logging reduced a bit.
19 * The remaining running time is estimated during register merge.
21 * Revision 1.15 1995/12/07 17:38:48 adam
22 * Work locking mechanisms for concurrent updates/commit.
24 * Revision 1.14 1995/12/06 12:41:26 adam
25 * New command 'stat' for the index program.
26 * Filenames can be read from stdin by specifying '-'.
27 * Bug fix/enhancement of the transformation from terms to regular
28 * expressons in the search engine.
30 * Revision 1.13 1995/11/28 09:09:46 adam
31 * Zebra config renamed.
32 * Use setting 'recordId' to identify record now.
33 * Bug fix in recindex.c: rec_release_blocks was invokeded even
34 * though the blocks were already released.
35 * File traversal properly deletes records when needed.
37 * Revision 1.12 1995/11/24 11:31:37 adam
38 * Commands add & del read filenames from stdin if source directory is
40 * Match criteria supports 'constant' strings.
42 * Revision 1.11 1995/11/22 17:19:19 adam
43 * Record management uses the bfile system.
45 * Revision 1.10 1995/11/21 15:01:16 adam
46 * New general match criteria implemented.
47 * New feature: document groups.
49 * Revision 1.9 1995/11/21 09:20:32 adam
50 * Yet more work on record match.
52 * Revision 1.8 1995/11/20 16:59:46 adam
53 * New update method: the 'old' keys are saved for each records.
55 * Revision 1.7 1995/11/20 11:56:28 adam
56 * Work on new traversal.
58 * Revision 1.6 1995/11/17 15:54:42 adam
59 * Started work on virtual directory structure.
61 * Revision 1.5 1995/10/17 18:02:09 adam
62 * New feature: databases. Implemented as prefix to words in dictionary.
64 * Revision 1.4 1995/09/28 09:19:46 adam
65 * xfree/xmalloc used everywhere.
66 * Extract/retrieve method seems to work for text records.
68 * Revision 1.3 1995/09/06 16:11:18 adam
69 * Option: only one word key per file.
71 * Revision 1.2 1995/09/04 12:33:43 adam
72 * Various cleanup. YAZ util used instead.
74 * Revision 1.1 1995/09/01 14:06:36 adam
75 * Split of work into more files.
83 #include <sys/types.h>
91 static int repComp (const char *a, const char *b, size_t len)
95 return memcmp (a, b, len);
98 static void repositoryExtractR (int deleteFlag, char *rep,
99 struct recordGroup *rGroup)
103 size_t rep_len = strlen (rep);
108 logf (LOG_LOG, "Dir: %s", rep);
109 if (rep[rep_len-1] != '/')
113 for (i=0; e[i].name; i++)
115 strcpy (rep +rep_len+1, e[i].name);
119 fileExtract (NULL, rep, rGroup, deleteFlag);
122 repositoryExtractR (deleteFlag, rep, rGroup);
130 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
134 logf (LOG_LOG, "stdinExtractR");
135 while (scanf ("%s", tmppath) == 1)
136 fileExtract (NULL, tmppath, rGroup, deleteFlag);
139 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
140 const char *base, char *src,
141 struct recordGroup *rGroup)
144 size_t src_len = strlen (src);
146 while (dst && !repComp (dst->path, src, src_len+1))
151 sprintf (tmppath, "%s%s", base, dst->path);
152 fileExtract (&dst->sysno, tmppath, rGroup, 1);
154 strcpy (tmppath, dst->path);
155 dst = dirs_read (di);
156 dirs_del (di, tmppath);
159 strcpy (tmppath, dst->path);
160 dst = dirs_read (di);
161 dirs_rmdir (di, tmppath);
164 dst = dirs_read (di);
169 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
170 const char *base, char *src,
171 struct recordGroup *rGroup)
173 struct dir_entry *e_src;
175 static char tmppath[1024];
176 size_t src_len = strlen (src);
178 sprintf (tmppath, "%s%s", base, src);
179 e_src = dir_open (tmppath);
180 logf (LOG_LOG, "Dir: %s", tmppath);
183 if (!dst || repComp (dst->path, src, src_len))
185 if (!dst || strcmp (dst->path, src))
191 if (src_len && src[src_len-1] != '/')
194 src[++src_len] = '\0';
196 dirs_mkdir (di, src, 0);
203 strcpy (src, dst->path);
204 fileDeleteR (di, dst, base, src, rGroup);
209 if (src_len && src[src_len-1] != '/')
212 src[++src_len] = '\0';
214 dst = dirs_read (di);
222 if (dst && !repComp (dst->path, src, src_len))
224 if (e_src[i_src].name)
226 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
228 sd = strcmp (dst->path + src_len, e_src[i_src].name);
233 else if (e_src[i_src].name)
237 logf (LOG_DEBUG, "trav sd=%d", sd);
240 strcpy (src + src_len, e_src[i_src].name);
241 sprintf (tmppath, "%s%s", base, src);
243 switch (e_src[i_src].kind)
246 if (e_src[i_src].ctime > dst->ctime)
248 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
250 dirs_add (di, src, dst->sysno, e_src[i_src].ctime);
252 logf (LOG_LOG, "old: %s", ctime (&dst->ctime));
253 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].ctime));
255 dst = dirs_read (di);
258 fileUpdateR (di, dst, base, src, rGroup);
259 dst = dirs_last (di);
260 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
263 dst = dirs_read (di);
270 strcpy (src + src_len, e_src[i_src].name);
271 sprintf (tmppath, "%s%s", base, src);
273 switch (e_src[i_src].kind)
276 if (fileExtract (&sysno, tmppath, rGroup, 0))
277 dirs_add (di, src, sysno, e_src[i_src].ctime);
280 fileUpdateR (di, dst, base, src, rGroup);
282 dst = dirs_last (di);
289 strcpy (src, dst->path);
290 sprintf (tmppath, "%s%s", base, dst->path);
295 fileExtract (&dst->sysno, tmppath, rGroup, 1);
296 dirs_del (di, dst->path);
297 dst = dirs_read (di);
300 fileDeleteR (di, dst, base, src, rGroup);
301 dst = dirs_last (di);
308 static void groupRes (struct recordGroup *rGroup)
313 if (!rGroup->groupName || !*rGroup->groupName)
316 sprintf (gPrefix, "%s.", rGroup->groupName);
318 sprintf (resStr, "%srecordId", gPrefix);
319 rGroup->recordId = res_get (common_resource, resStr);
323 void repositoryShow (struct recordGroup *rGroup)
327 struct dirs_entry *dst;
329 struct dirs_info *di;
331 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
333 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
337 assert (rGroup->path);
338 strcpy (src, rGroup->path);
339 src_len = strlen (src);
341 if (src_len && src[src_len-1] != '/')
344 src[++src_len] = '\0';
347 di = dirs_open (dict, src);
349 while ( (dst = dirs_read (di)) )
350 logf (LOG_LOG, "%s", dst->path);
355 void repositoryUpdate (struct recordGroup *rGroup)
362 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
365 struct dirs_info *di;
367 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
369 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
372 assert (rGroup->path);
374 strcpy (src, rGroup->path);
375 src_len = strlen (src);
377 if (src_len && src[src_len-1] != '/')
380 src[++src_len] = '\0';
383 di = dirs_open (dict, src);
386 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
392 strcpy (src, rGroup->path);
393 if (*src == '\0' || !strcmp (src, "-"))
394 stdinExtractR (0, rGroup);
396 repositoryExtractR (0, src, rGroup);
400 void repositoryDelete (struct recordGroup *rGroup)
404 assert (rGroup->path);
406 strcpy (src, rGroup->path);
407 if (*src == '\0' || !strcmp(src, "-"))
408 stdinExtractR (1, rGroup);
410 repositoryExtractR (1, src, rGroup);