2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.27 1996-10-29 14:06:56 adam
8 * Include zebrautl.h instead of alexutil.h.
10 * Revision 1.26 1996/06/04 10:19:01 adam
11 * Minor changes - removed include of ctype.h.
13 * Revision 1.25 1996/05/01 13:46:37 adam
14 * First work on multiple records in one file.
15 * New option, -offset, to the "unread" command in the filter module.
17 * Revision 1.24 1996/04/26 10:00:23 adam
18 * Added option -V to zebraidx to display version information.
19 * Removed stupid warnings from file update.
21 * Revision 1.23 1996/04/12 07:02:25 adam
22 * File update of single files.
24 * Revision 1.22 1996/04/09 06:50:50 adam
25 * Bug fix: bad reference in function fileUpdateR.
27 * Revision 1.21 1996/03/22 15:34:18 quinn
30 * Revision 1.20 1996/03/21 14:50:10 adam
31 * File update uses modify-time instead of change-time.
33 * Revision 1.19 1996/03/20 16:16:55 quinn
34 * Added diagnostic output
36 * Revision 1.18 1996/03/19 12:43:27 adam
37 * Bug fix: File update traversal didn't handle trailing slashes correctly.
38 * Bug fix: Update of sub directory groups wasn't handled correctly.
40 * Revision 1.17 1996/02/12 18:45:17 adam
41 * Changed naming of some functions.
43 * Revision 1.16 1996/02/05 12:30:02 adam
44 * Logging reduced a bit.
45 * The remaining running time is estimated during register merge.
47 * Revision 1.15 1995/12/07 17:38:48 adam
48 * Work locking mechanisms for concurrent updates/commit.
50 * Revision 1.14 1995/12/06 12:41:26 adam
51 * New command 'stat' for the index program.
52 * Filenames can be read from stdin by specifying '-'.
53 * Bug fix/enhancement of the transformation from terms to regular
54 * expressons in the search engine.
56 * Revision 1.13 1995/11/28 09:09:46 adam
57 * Zebra config renamed.
58 * Use setting 'recordId' to identify record now.
59 * Bug fix in recindex.c: rec_release_blocks was invokeded even
60 * though the blocks were already released.
61 * File traversal properly deletes records when needed.
63 * Revision 1.12 1995/11/24 11:31:37 adam
64 * Commands add & del read filenames from stdin if source directory is
66 * Match criteria supports 'constant' strings.
68 * Revision 1.11 1995/11/22 17:19:19 adam
69 * Record management uses the bfile system.
71 * Revision 1.10 1995/11/21 15:01:16 adam
72 * New general match criteria implemented.
73 * New feature: document groups.
75 * Revision 1.9 1995/11/21 09:20:32 adam
76 * Yet more work on record match.
78 * Revision 1.8 1995/11/20 16:59:46 adam
79 * New update method: the 'old' keys are saved for each records.
81 * Revision 1.7 1995/11/20 11:56:28 adam
82 * Work on new traversal.
84 * Revision 1.6 1995/11/17 15:54:42 adam
85 * Started work on virtual directory structure.
87 * Revision 1.5 1995/10/17 18:02:09 adam
88 * New feature: databases. Implemented as prefix to words in dictionary.
90 * Revision 1.4 1995/09/28 09:19:46 adam
91 * xfree/xmalloc used everywhere.
92 * Extract/retrieve method seems to work for text records.
94 * Revision 1.3 1995/09/06 16:11:18 adam
95 * Option: only one word key per file.
97 * Revision 1.2 1995/09/04 12:33:43 adam
98 * Various cleanup. YAZ util used instead.
100 * Revision 1.1 1995/09/01 14:06:36 adam
101 * Split of work into more files.
108 #include <sys/stat.h>
109 #include <sys/types.h>
115 static int repComp (const char *a, const char *b, size_t len)
119 return memcmp (a, b, len);
122 static void repositoryExtractR (int deleteFlag, char *rep,
123 struct recordGroup *rGroup)
127 size_t rep_len = strlen (rep);
132 logf (LOG_LOG, "Dir: %s", rep);
133 if (rep[rep_len-1] != '/')
137 for (i=0; e[i].name; i++)
139 strcpy (rep +rep_len+1, e[i].name);
143 fileExtract (NULL, rep, rGroup, deleteFlag);
146 repositoryExtractR (deleteFlag, rep, rGroup);
154 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
155 const char *base, char *src,
156 struct recordGroup *rGroup)
159 size_t src_len = strlen (src);
161 while (dst && !repComp (dst->path, src, src_len+1))
166 sprintf (tmppath, "%s%s", base, dst->path);
167 fileExtract (&dst->sysno, tmppath, rGroup, 1);
169 strcpy (tmppath, dst->path);
170 dst = dirs_read (di);
171 dirs_del (di, tmppath);
174 strcpy (tmppath, dst->path);
175 dst = dirs_read (di);
176 dirs_rmdir (di, tmppath);
179 dst = dirs_read (di);
184 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
185 const char *base, char *src,
186 struct recordGroup *rGroup)
188 struct dir_entry *e_src;
190 static char tmppath[1024];
191 size_t src_len = strlen (src);
193 sprintf (tmppath, "%s%s", base, src);
194 e_src = dir_open (tmppath);
195 logf (LOG_LOG, "Dir: %s", tmppath);
198 if (!dst || repComp (dst->path, src, src_len))
200 if (!dst || strcmp (dst->path, src))
206 if (src_len && src[src_len-1] != '/')
209 src[++src_len] = '\0';
211 dirs_mkdir (di, src, 0);
212 if (dst && repComp (dst->path, src, src_len))
217 strcpy (src, dst->path);
218 fileDeleteR (di, dst, base, src, rGroup);
223 if (src_len && src[src_len-1] != '/')
226 src[++src_len] = '\0';
228 dst = dirs_read (di);
236 if (dst && !repComp (dst->path, src, src_len))
238 if (e_src[i_src].name)
240 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
242 sd = strcmp (dst->path + src_len, e_src[i_src].name);
247 else if (e_src[i_src].name)
251 logf (LOG_DEBUG, "trav sd=%d", sd);
254 strcpy (src + src_len, e_src[i_src].name);
255 sprintf (tmppath, "%s%s", base, src);
257 switch (e_src[i_src].kind)
260 if (e_src[i_src].mtime > dst->mtime)
262 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
264 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
266 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
267 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
269 dst = dirs_read (di);
272 fileUpdateR (di, dst, base, src, rGroup);
273 dst = dirs_last (di);
274 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
277 dst = dirs_read (di);
284 strcpy (src + src_len, e_src[i_src].name);
285 sprintf (tmppath, "%s%s", base, src);
287 switch (e_src[i_src].kind)
290 if (fileExtract (&sysno, tmppath, rGroup, 0))
291 dirs_add (di, src, sysno, e_src[i_src].mtime);
294 fileUpdateR (di, dst, base, src, rGroup);
296 dst = dirs_last (di);
303 strcpy (src, dst->path);
304 sprintf (tmppath, "%s%s", base, dst->path);
309 fileExtract (&dst->sysno, tmppath, rGroup, 1);
310 dirs_del (di, dst->path);
311 dst = dirs_read (di);
314 fileDeleteR (di, dst, base, src, rGroup);
315 dst = dirs_last (di);
322 static void groupRes (struct recordGroup *rGroup)
327 if (!rGroup->groupName || !*rGroup->groupName)
330 sprintf (gPrefix, "%s.", rGroup->groupName);
332 sprintf (resStr, "%srecordId", gPrefix);
333 rGroup->recordId = res_get (common_resource, resStr);
336 void repositoryShow (struct recordGroup *rGroup)
340 struct dirs_entry *dst;
342 struct dirs_info *di;
344 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
346 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
350 assert (rGroup->path);
351 strcpy (src, rGroup->path);
352 src_len = strlen (src);
354 if (src_len && src[src_len-1] != '/')
357 src[++src_len] = '\0';
360 di = dirs_open (dict, src);
362 while ( (dst = dirs_read (di)) )
363 logf (LOG_LOG, "%s", dst->path);
368 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
371 struct dirs_info *di;
379 src_len = strlen (src);
382 if (S_ISREG(sbuf.st_mode))
384 struct dirs_entry *e_dst;
385 di = dirs_fopen (dict, src);
387 e_dst = dirs_read (di);
390 if (sbuf.st_mtime > e_dst->mtime)
391 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
392 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
397 if (fileExtract (&sysno, src, rGroup, 0))
398 dirs_add (di, src, sysno, sbuf.st_mtime);
402 else if (S_ISDIR(sbuf.st_mode))
404 if (src_len && src[src_len-1] != '/')
407 src[++src_len] = '\0';
409 di = dirs_open (dict, src);
411 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
416 logf (LOG_WARN, "Cannot handle file %s", src);
421 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
431 if (S_ISREG(sbuf.st_mode))
432 fileExtract (NULL, src, rGroup, deleteFlag);
433 else if (S_ISDIR(sbuf.st_mode))
434 repositoryExtractR (deleteFlag, src, rGroup);
436 logf (LOG_WARN, "Cannot handle file %s", src);
439 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
441 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
445 while (scanf ("%s", src) == 1)
446 repositoryExtract (deleteFlag, rGroup, src);
449 repositoryExtract (deleteFlag, rGroup, rGroup->path);
452 void repositoryUpdate (struct recordGroup *rGroup)
455 assert (rGroup->path);
456 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
459 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
461 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
464 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
467 while (scanf ("%s", src) == 1)
468 fileUpdate (dict, rGroup, src);
471 fileUpdate (dict, rGroup, rGroup->path);
475 repositoryExtractG (0, rGroup);
478 void repositoryDelete (struct recordGroup *rGroup)
480 repositoryExtractG (1, rGroup);