2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1996-04-26 10:00:23 adam
8 * Added option -V to zebraidx to display version information.
9 * Removed stupid warnings from file update.
11 * Revision 1.23 1996/04/12 07:02:25 adam
12 * File update of single files.
14 * Revision 1.22 1996/04/09 06:50:50 adam
15 * Bug fix: bad reference in function fileUpdateR.
17 * Revision 1.21 1996/03/22 15:34:18 quinn
20 * Revision 1.20 1996/03/21 14:50:10 adam
21 * File update uses modify-time instead of change-time.
23 * Revision 1.19 1996/03/20 16:16:55 quinn
24 * Added diagnostic output
26 * Revision 1.18 1996/03/19 12:43:27 adam
27 * Bug fix: File update traversal didn't handle trailing slashes correctly.
28 * Bug fix: Update of sub directory groups wasn't handled correctly.
30 * Revision 1.17 1996/02/12 18:45:17 adam
31 * Changed naming of some functions.
33 * Revision 1.16 1996/02/05 12:30:02 adam
34 * Logging reduced a bit.
35 * The remaining running time is estimated during register merge.
37 * Revision 1.15 1995/12/07 17:38:48 adam
38 * Work locking mechanisms for concurrent updates/commit.
40 * Revision 1.14 1995/12/06 12:41:26 adam
41 * New command 'stat' for the index program.
42 * Filenames can be read from stdin by specifying '-'.
43 * Bug fix/enhancement of the transformation from terms to regular
44 * expressons in the search engine.
46 * Revision 1.13 1995/11/28 09:09:46 adam
47 * Zebra config renamed.
48 * Use setting 'recordId' to identify record now.
49 * Bug fix in recindex.c: rec_release_blocks was invokeded even
50 * though the blocks were already released.
51 * File traversal properly deletes records when needed.
53 * Revision 1.12 1995/11/24 11:31:37 adam
54 * Commands add & del read filenames from stdin if source directory is
56 * Match criteria supports 'constant' strings.
58 * Revision 1.11 1995/11/22 17:19:19 adam
59 * Record management uses the bfile system.
61 * Revision 1.10 1995/11/21 15:01:16 adam
62 * New general match criteria implemented.
63 * New feature: document groups.
65 * Revision 1.9 1995/11/21 09:20:32 adam
66 * Yet more work on record match.
68 * Revision 1.8 1995/11/20 16:59:46 adam
69 * New update method: the 'old' keys are saved for each records.
71 * Revision 1.7 1995/11/20 11:56:28 adam
72 * Work on new traversal.
74 * Revision 1.6 1995/11/17 15:54:42 adam
75 * Started work on virtual directory structure.
77 * Revision 1.5 1995/10/17 18:02:09 adam
78 * New feature: databases. Implemented as prefix to words in dictionary.
80 * Revision 1.4 1995/09/28 09:19:46 adam
81 * xfree/xmalloc used everywhere.
82 * Extract/retrieve method seems to work for text records.
84 * Revision 1.3 1995/09/06 16:11:18 adam
85 * Option: only one word key per file.
87 * Revision 1.2 1995/09/04 12:33:43 adam
88 * Various cleanup. YAZ util used instead.
90 * Revision 1.1 1995/09/01 14:06:36 adam
91 * Split of work into more files.
99 #include <sys/types.h>
104 #include <alexutil.h>
107 static int repComp (const char *a, const char *b, size_t len)
111 return memcmp (a, b, len);
114 static void repositoryExtractR (int deleteFlag, char *rep,
115 struct recordGroup *rGroup)
119 size_t rep_len = strlen (rep);
124 logf (LOG_LOG, "Dir: %s", rep);
125 if (rep[rep_len-1] != '/')
129 for (i=0; e[i].name; i++)
131 strcpy (rep +rep_len+1, e[i].name);
135 fileExtract (NULL, rep, rGroup, deleteFlag);
138 repositoryExtractR (deleteFlag, rep, rGroup);
146 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
150 logf (LOG_LOG, "stdinExtractR");
151 while (scanf ("%s", tmppath) == 1)
152 fileExtract (NULL, tmppath, rGroup, deleteFlag);
155 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
156 const char *base, char *src,
157 struct recordGroup *rGroup)
160 size_t src_len = strlen (src);
162 while (dst && !repComp (dst->path, src, src_len+1))
167 sprintf (tmppath, "%s%s", base, dst->path);
168 fileExtract (&dst->sysno, tmppath, rGroup, 1);
170 strcpy (tmppath, dst->path);
171 dst = dirs_read (di);
172 dirs_del (di, tmppath);
175 strcpy (tmppath, dst->path);
176 dst = dirs_read (di);
177 dirs_rmdir (di, tmppath);
180 dst = dirs_read (di);
185 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
186 const char *base, char *src,
187 struct recordGroup *rGroup)
189 struct dir_entry *e_src;
191 static char tmppath[1024];
192 size_t src_len = strlen (src);
194 sprintf (tmppath, "%s%s", base, src);
195 e_src = dir_open (tmppath);
196 logf (LOG_LOG, "Dir: %s", tmppath);
199 if (!dst || repComp (dst->path, src, src_len))
201 if (!dst || strcmp (dst->path, src))
207 if (src_len && src[src_len-1] != '/')
210 src[++src_len] = '\0';
212 dirs_mkdir (di, src, 0);
213 if (dst && repComp (dst->path, src, src_len))
218 strcpy (src, dst->path);
219 fileDeleteR (di, dst, base, src, rGroup);
224 if (src_len && src[src_len-1] != '/')
227 src[++src_len] = '\0';
229 dst = dirs_read (di);
237 if (dst && !repComp (dst->path, src, src_len))
239 if (e_src[i_src].name)
241 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
243 sd = strcmp (dst->path + src_len, e_src[i_src].name);
248 else if (e_src[i_src].name)
252 logf (LOG_DEBUG, "trav sd=%d", sd);
255 strcpy (src + src_len, e_src[i_src].name);
256 sprintf (tmppath, "%s%s", base, src);
258 switch (e_src[i_src].kind)
261 if (e_src[i_src].mtime > dst->mtime)
263 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
265 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
267 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
268 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
270 dst = dirs_read (di);
273 fileUpdateR (di, dst, base, src, rGroup);
274 dst = dirs_last (di);
275 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
278 dst = dirs_read (di);
285 strcpy (src + src_len, e_src[i_src].name);
286 sprintf (tmppath, "%s%s", base, src);
288 switch (e_src[i_src].kind)
291 if (fileExtract (&sysno, tmppath, rGroup, 0))
292 dirs_add (di, src, sysno, e_src[i_src].mtime);
295 fileUpdateR (di, dst, base, src, rGroup);
297 dst = dirs_last (di);
304 strcpy (src, dst->path);
305 sprintf (tmppath, "%s%s", base, dst->path);
310 fileExtract (&dst->sysno, tmppath, rGroup, 1);
311 dirs_del (di, dst->path);
312 dst = dirs_read (di);
315 fileDeleteR (di, dst, base, src, rGroup);
316 dst = dirs_last (di);
323 static void groupRes (struct recordGroup *rGroup)
328 if (!rGroup->groupName || !*rGroup->groupName)
331 sprintf (gPrefix, "%s.", rGroup->groupName);
333 sprintf (resStr, "%srecordId", gPrefix);
334 rGroup->recordId = res_get (common_resource, resStr);
338 void repositoryShow (struct recordGroup *rGroup)
342 struct dirs_entry *dst;
344 struct dirs_info *di;
346 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
348 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
352 assert (rGroup->path);
353 strcpy (src, rGroup->path);
354 src_len = strlen (src);
356 if (src_len && src[src_len-1] != '/')
359 src[++src_len] = '\0';
362 di = dirs_open (dict, src);
364 while ( (dst = dirs_read (di)) )
365 logf (LOG_LOG, "%s", dst->path);
370 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
373 struct dirs_info *di;
381 src_len = strlen (src);
384 if (S_ISREG(sbuf.st_mode))
386 struct dirs_entry *e_dst;
387 di = dirs_fopen (dict, src);
389 e_dst = dirs_read (di);
392 if (sbuf.st_mtime > e_dst->mtime)
393 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
394 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
399 if (fileExtract (&sysno, src, rGroup, 0))
400 dirs_add (di, src, sysno, sbuf.st_mtime);
404 else if (S_ISDIR(sbuf.st_mode))
406 if (src_len && src[src_len-1] != '/')
409 src[++src_len] = '\0';
411 di = dirs_open (dict, src);
413 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
418 logf (LOG_WARN, "Cannot handle file %s", src);
422 void repositoryUpdate (struct recordGroup *rGroup)
425 assert (rGroup->path);
426 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
429 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
431 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
434 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
437 while (scanf ("%s", src) == 1)
438 fileUpdate (dict, rGroup, src);
441 fileUpdate (dict, rGroup, rGroup->path);
448 strcpy (src, rGroup->path);
449 if (*src == '\0' || !strcmp (src, "-"))
450 stdinExtractR (0, rGroup);
452 repositoryExtractR (0, src, rGroup);
456 void repositoryDelete (struct recordGroup *rGroup)
460 assert (rGroup->path);
462 strcpy (src, rGroup->path);
463 if (*src == '\0' || !strcmp(src, "-"))
464 stdinExtractR (1, rGroup);
466 repositoryExtractR (1, src, rGroup);