2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1996-03-21 14:50:10 adam
8 * File update uses modify-time instead of change-time.
10 * Revision 1.19 1996/03/20 16:16:55 quinn
11 * Added diagnostic output
13 * Revision 1.18 1996/03/19 12:43:27 adam
14 * Bug fix: File update traversal didn't handle trailing slashes correctly.
15 * Bug fix: Update of sub directory groups wasn't handled correctly.
17 * Revision 1.17 1996/02/12 18:45:17 adam
18 * Changed naming of some functions.
20 * Revision 1.16 1996/02/05 12:30:02 adam
21 * Logging reduced a bit.
22 * The remaining running time is estimated during register merge.
24 * Revision 1.15 1995/12/07 17:38:48 adam
25 * Work locking mechanisms for concurrent updates/commit.
27 * Revision 1.14 1995/12/06 12:41:26 adam
28 * New command 'stat' for the index program.
29 * Filenames can be read from stdin by specifying '-'.
30 * Bug fix/enhancement of the transformation from terms to regular
31 * expressons in the search engine.
33 * Revision 1.13 1995/11/28 09:09:46 adam
34 * Zebra config renamed.
35 * Use setting 'recordId' to identify record now.
36 * Bug fix in recindex.c: rec_release_blocks was invokeded even
37 * though the blocks were already released.
38 * File traversal properly deletes records when needed.
40 * Revision 1.12 1995/11/24 11:31:37 adam
41 * Commands add & del read filenames from stdin if source directory is
43 * Match criteria supports 'constant' strings.
45 * Revision 1.11 1995/11/22 17:19:19 adam
46 * Record management uses the bfile system.
48 * Revision 1.10 1995/11/21 15:01:16 adam
49 * New general match criteria implemented.
50 * New feature: document groups.
52 * Revision 1.9 1995/11/21 09:20:32 adam
53 * Yet more work on record match.
55 * Revision 1.8 1995/11/20 16:59:46 adam
56 * New update method: the 'old' keys are saved for each records.
58 * Revision 1.7 1995/11/20 11:56:28 adam
59 * Work on new traversal.
61 * Revision 1.6 1995/11/17 15:54:42 adam
62 * Started work on virtual directory structure.
64 * Revision 1.5 1995/10/17 18:02:09 adam
65 * New feature: databases. Implemented as prefix to words in dictionary.
67 * Revision 1.4 1995/09/28 09:19:46 adam
68 * xfree/xmalloc used everywhere.
69 * Extract/retrieve method seems to work for text records.
71 * Revision 1.3 1995/09/06 16:11:18 adam
72 * Option: only one word key per file.
74 * Revision 1.2 1995/09/04 12:33:43 adam
75 * Various cleanup. YAZ util used instead.
77 * Revision 1.1 1995/09/01 14:06:36 adam
78 * Split of work into more files.
86 #include <sys/types.h>
94 static int repComp (const char *a, const char *b, size_t len)
98 return memcmp (a, b, len);
101 static void repositoryExtractR (int deleteFlag, char *rep,
102 struct recordGroup *rGroup)
106 size_t rep_len = strlen (rep);
111 logf (LOG_LOG, "Dir: %s", rep);
112 if (rep[rep_len-1] != '/')
116 for (i=0; e[i].name; i++)
118 strcpy (rep +rep_len+1, e[i].name);
122 fileExtract (NULL, rep, rGroup, deleteFlag);
125 repositoryExtractR (deleteFlag, rep, rGroup);
133 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
137 logf (LOG_LOG, "stdinExtractR");
138 while (scanf ("%s", tmppath) == 1)
139 fileExtract (NULL, tmppath, rGroup, deleteFlag);
142 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
143 const char *base, char *src,
144 struct recordGroup *rGroup)
147 size_t src_len = strlen (src);
149 while (dst && !repComp (dst->path, src, src_len+1))
154 sprintf (tmppath, "%s%s", base, dst->path);
155 fileExtract (&dst->sysno, tmppath, rGroup, 1);
157 strcpy (tmppath, dst->path);
158 dst = dirs_read (di);
159 dirs_del (di, tmppath);
162 strcpy (tmppath, dst->path);
163 dst = dirs_read (di);
164 dirs_rmdir (di, tmppath);
167 dst = dirs_read (di);
172 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
173 const char *base, char *src,
174 struct recordGroup *rGroup)
176 struct dir_entry *e_src;
178 static char tmppath[1024];
179 size_t src_len = strlen (src);
181 sprintf (tmppath, "%s%s", base, src);
182 e_src = dir_open (tmppath);
183 logf (LOG_LOG, "Dir: %s", tmppath);
186 if (!dst || repComp (dst->path, src, src_len))
188 if (!dst || strcmp (dst->path, src))
194 if (src_len && src[src_len-1] != '/')
197 src[++src_len] = '\0';
199 dirs_mkdir (di, src, 0);
200 if (repComp (dst->path, src, src_len))
205 strcpy (src, dst->path);
206 fileDeleteR (di, dst, base, src, rGroup);
211 if (src_len && src[src_len-1] != '/')
214 src[++src_len] = '\0';
216 dst = dirs_read (di);
224 if (dst && !repComp (dst->path, src, src_len))
226 if (e_src[i_src].name)
228 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
230 sd = strcmp (dst->path + src_len, e_src[i_src].name);
235 else if (e_src[i_src].name)
239 logf (LOG_DEBUG, "trav sd=%d", sd);
242 strcpy (src + src_len, e_src[i_src].name);
243 sprintf (tmppath, "%s%s", base, src);
245 switch (e_src[i_src].kind)
248 if (e_src[i_src].mtime > dst->mtime)
250 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
252 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
254 logf (LOG_LOG, "old: %s", ctime (&dst->ctime));
255 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].ctime));
257 dst = dirs_read (di);
260 fileUpdateR (di, dst, base, src, rGroup);
261 dst = dirs_last (di);
262 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
265 dst = dirs_read (di);
272 strcpy (src + src_len, e_src[i_src].name);
273 sprintf (tmppath, "%s%s", base, src);
275 switch (e_src[i_src].kind)
278 if (fileExtract (&sysno, tmppath, rGroup, 0))
279 dirs_add (di, src, sysno, e_src[i_src].mtime);
282 fileUpdateR (di, dst, base, src, rGroup);
284 dst = dirs_last (di);
291 strcpy (src, dst->path);
292 sprintf (tmppath, "%s%s", base, dst->path);
297 fileExtract (&dst->sysno, tmppath, rGroup, 1);
298 dirs_del (di, dst->path);
299 dst = dirs_read (di);
302 fileDeleteR (di, dst, base, src, rGroup);
303 dst = dirs_last (di);
310 static void groupRes (struct recordGroup *rGroup)
315 if (!rGroup->groupName || !*rGroup->groupName)
318 sprintf (gPrefix, "%s.", rGroup->groupName);
320 sprintf (resStr, "%srecordId", gPrefix);
321 rGroup->recordId = res_get (common_resource, resStr);
325 void repositoryShow (struct recordGroup *rGroup)
329 struct dirs_entry *dst;
331 struct dirs_info *di;
333 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
335 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
339 assert (rGroup->path);
340 strcpy (src, rGroup->path);
341 src_len = strlen (src);
343 if (src_len && src[src_len-1] != '/')
346 src[++src_len] = '\0';
349 di = dirs_open (dict, src);
351 while ( (dst = dirs_read (di)) )
352 logf (LOG_LOG, "%s", dst->path);
357 void repositoryUpdate (struct recordGroup *rGroup)
364 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
367 struct dirs_info *di;
369 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
371 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
374 assert (rGroup->path);
376 strcpy (src, rGroup->path);
377 src_len = strlen (src);
379 if (src_len && src[src_len-1] != '/')
382 src[++src_len] = '\0';
385 di = dirs_open (dict, src);
388 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
394 strcpy (src, rGroup->path);
395 if (*src == '\0' || !strcmp (src, "-"))
396 stdinExtractR (0, rGroup);
398 repositoryExtractR (0, src, rGroup);
402 void repositoryDelete (struct recordGroup *rGroup)
406 assert (rGroup->path);
408 strcpy (src, rGroup->path);
409 if (*src == '\0' || !strcmp(src, "-"))
410 stdinExtractR (1, rGroup);
412 repositoryExtractR (1, src, rGroup);