2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1996-04-12 07:02:25 adam
8 * File update of single files.
10 * Revision 1.22 1996/04/09 06:50:50 adam
11 * Bug fix: bad reference in function fileUpdateR.
13 * Revision 1.21 1996/03/22 15:34:18 quinn
16 * Revision 1.20 1996/03/21 14:50:10 adam
17 * File update uses modify-time instead of change-time.
19 * Revision 1.19 1996/03/20 16:16:55 quinn
20 * Added diagnostic output
22 * Revision 1.18 1996/03/19 12:43:27 adam
23 * Bug fix: File update traversal didn't handle trailing slashes correctly.
24 * Bug fix: Update of sub directory groups wasn't handled correctly.
26 * Revision 1.17 1996/02/12 18:45:17 adam
27 * Changed naming of some functions.
29 * Revision 1.16 1996/02/05 12:30:02 adam
30 * Logging reduced a bit.
31 * The remaining running time is estimated during register merge.
33 * Revision 1.15 1995/12/07 17:38:48 adam
34 * Work locking mechanisms for concurrent updates/commit.
36 * Revision 1.14 1995/12/06 12:41:26 adam
37 * New command 'stat' for the index program.
38 * Filenames can be read from stdin by specifying '-'.
39 * Bug fix/enhancement of the transformation from terms to regular
40 * expressons in the search engine.
42 * Revision 1.13 1995/11/28 09:09:46 adam
43 * Zebra config renamed.
44 * Use setting 'recordId' to identify record now.
45 * Bug fix in recindex.c: rec_release_blocks was invokeded even
46 * though the blocks were already released.
47 * File traversal properly deletes records when needed.
49 * Revision 1.12 1995/11/24 11:31:37 adam
50 * Commands add & del read filenames from stdin if source directory is
52 * Match criteria supports 'constant' strings.
54 * Revision 1.11 1995/11/22 17:19:19 adam
55 * Record management uses the bfile system.
57 * Revision 1.10 1995/11/21 15:01:16 adam
58 * New general match criteria implemented.
59 * New feature: document groups.
61 * Revision 1.9 1995/11/21 09:20:32 adam
62 * Yet more work on record match.
64 * Revision 1.8 1995/11/20 16:59:46 adam
65 * New update method: the 'old' keys are saved for each records.
67 * Revision 1.7 1995/11/20 11:56:28 adam
68 * Work on new traversal.
70 * Revision 1.6 1995/11/17 15:54:42 adam
71 * Started work on virtual directory structure.
73 * Revision 1.5 1995/10/17 18:02:09 adam
74 * New feature: databases. Implemented as prefix to words in dictionary.
76 * Revision 1.4 1995/09/28 09:19:46 adam
77 * xfree/xmalloc used everywhere.
78 * Extract/retrieve method seems to work for text records.
80 * Revision 1.3 1995/09/06 16:11:18 adam
81 * Option: only one word key per file.
83 * Revision 1.2 1995/09/04 12:33:43 adam
84 * Various cleanup. YAZ util used instead.
86 * Revision 1.1 1995/09/01 14:06:36 adam
87 * Split of work into more files.
95 #include <sys/types.h>
100 #include <alexutil.h>
103 static int repComp (const char *a, const char *b, size_t len)
107 return memcmp (a, b, len);
110 static void repositoryExtractR (int deleteFlag, char *rep,
111 struct recordGroup *rGroup)
115 size_t rep_len = strlen (rep);
120 logf (LOG_LOG, "Dir: %s", rep);
121 if (rep[rep_len-1] != '/')
125 for (i=0; e[i].name; i++)
127 strcpy (rep +rep_len+1, e[i].name);
131 fileExtract (NULL, rep, rGroup, deleteFlag);
134 repositoryExtractR (deleteFlag, rep, rGroup);
142 static void stdinExtractR (int deleteFlag, struct recordGroup *rGroup)
146 logf (LOG_LOG, "stdinExtractR");
147 while (scanf ("%s", tmppath) == 1)
148 fileExtract (NULL, tmppath, rGroup, deleteFlag);
151 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
152 const char *base, char *src,
153 struct recordGroup *rGroup)
156 size_t src_len = strlen (src);
158 while (dst && !repComp (dst->path, src, src_len+1))
163 sprintf (tmppath, "%s%s", base, dst->path);
164 fileExtract (&dst->sysno, tmppath, rGroup, 1);
166 strcpy (tmppath, dst->path);
167 dst = dirs_read (di);
168 dirs_del (di, tmppath);
171 strcpy (tmppath, dst->path);
172 dst = dirs_read (di);
173 dirs_rmdir (di, tmppath);
176 dst = dirs_read (di);
181 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
182 const char *base, char *src,
183 struct recordGroup *rGroup)
185 struct dir_entry *e_src;
187 static char tmppath[1024];
188 size_t src_len = strlen (src);
190 sprintf (tmppath, "%s%s", base, src);
191 e_src = dir_open (tmppath);
192 logf (LOG_LOG, "Dir: %s", tmppath);
195 if (!dst || repComp (dst->path, src, src_len))
197 if (!dst || strcmp (dst->path, src))
203 if (src_len && src[src_len-1] != '/')
206 src[++src_len] = '\0';
208 dirs_mkdir (di, src, 0);
209 if (dst && repComp (dst->path, src, src_len))
214 strcpy (src, dst->path);
215 fileDeleteR (di, dst, base, src, rGroup);
220 if (src_len && src[src_len-1] != '/')
223 src[++src_len] = '\0';
225 dst = dirs_read (di);
233 if (dst && !repComp (dst->path, src, src_len))
235 if (e_src[i_src].name)
237 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
239 sd = strcmp (dst->path + src_len, e_src[i_src].name);
244 else if (e_src[i_src].name)
248 logf (LOG_DEBUG, "trav sd=%d", sd);
251 strcpy (src + src_len, e_src[i_src].name);
252 sprintf (tmppath, "%s%s", base, src);
254 switch (e_src[i_src].kind)
257 if (e_src[i_src].mtime > dst->mtime)
259 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
261 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
263 logf (LOG_LOG, "old: %s", ctime (&dst->mtime));
264 logf (LOG_LOG, "new: %s", ctime (&e_src[i_src].mtime));
266 dst = dirs_read (di);
269 fileUpdateR (di, dst, base, src, rGroup);
270 dst = dirs_last (di);
271 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
274 dst = dirs_read (di);
281 strcpy (src + src_len, e_src[i_src].name);
282 sprintf (tmppath, "%s%s", base, src);
284 switch (e_src[i_src].kind)
287 if (fileExtract (&sysno, tmppath, rGroup, 0))
288 dirs_add (di, src, sysno, e_src[i_src].mtime);
291 fileUpdateR (di, dst, base, src, rGroup);
293 dst = dirs_last (di);
300 strcpy (src, dst->path);
301 sprintf (tmppath, "%s%s", base, dst->path);
306 fileExtract (&dst->sysno, tmppath, rGroup, 1);
307 dirs_del (di, dst->path);
308 dst = dirs_read (di);
311 fileDeleteR (di, dst, base, src, rGroup);
312 dst = dirs_last (di);
319 static void groupRes (struct recordGroup *rGroup)
324 if (!rGroup->groupName || !*rGroup->groupName)
327 sprintf (gPrefix, "%s.", rGroup->groupName);
329 sprintf (resStr, "%srecordId", gPrefix);
330 rGroup->recordId = res_get (common_resource, resStr);
334 void repositoryShow (struct recordGroup *rGroup)
338 struct dirs_entry *dst;
340 struct dirs_info *di;
342 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
344 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
348 assert (rGroup->path);
349 strcpy (src, rGroup->path);
350 src_len = strlen (src);
352 if (src_len && src[src_len-1] != '/')
355 src[++src_len] = '\0';
358 di = dirs_open (dict, src);
360 while ( (dst = dirs_read (di)) )
361 logf (LOG_LOG, "%s", dst->path);
366 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
369 struct dirs_info *di;
377 src_len = strlen (src);
380 if (S_ISREG(sbuf.st_mode))
382 struct dirs_entry *e_dst;
383 di = dirs_fopen (dict, src);
385 logf (LOG_WARN, "Handle file %s", src);
386 e_dst = dirs_read (di);
389 logf (LOG_WARN, "Update Handle file %s", src);
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 logf (LOG_WARN, "New Handle file %s", src);
398 if (fileExtract (&sysno, src, rGroup, 0))
399 dirs_add (di, src, sysno, sbuf.st_mtime);
403 else if (S_ISDIR(sbuf.st_mode))
405 if (src_len && src[src_len-1] != '/')
408 src[++src_len] = '\0';
410 di = dirs_open (dict, src);
412 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
417 logf (LOG_WARN, "Cannot handle file %s", src);
421 void repositoryUpdate (struct recordGroup *rGroup)
424 assert (rGroup->path);
425 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
428 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
430 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
433 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
436 while (scanf ("%s", src) == 1)
437 fileUpdate (dict, rGroup, src);
440 fileUpdate (dict, rGroup, rGroup->path);
447 strcpy (src, rGroup->path);
448 if (*src == '\0' || !strcmp (src, "-"))
449 stdinExtractR (0, rGroup);
451 repositoryExtractR (0, src, rGroup);
455 void repositoryDelete (struct recordGroup *rGroup)
459 assert (rGroup->path);
461 strcpy (src, rGroup->path);
462 if (*src == '\0' || !strcmp(src, "-"))
463 stdinExtractR (1, rGroup);
465 repositoryExtractR (1, src, rGroup);