2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.29 1997-02-12 20:39:47 adam
8 * Implemented options -f <n> that limits the log to the first <n>
10 * Changed some log messages also.
12 * Revision 1.28 1996/11/01 08:58:44 adam
13 * Interface to isamc system now includes update and delete.
15 * Revision 1.27 1996/10/29 14:06:56 adam
16 * Include zebrautl.h instead of alexutil.h.
18 * Revision 1.26 1996/06/04 10:19:01 adam
19 * Minor changes - removed include of ctype.h.
21 * Revision 1.25 1996/05/01 13:46:37 adam
22 * First work on multiple records in one file.
23 * New option, -offset, to the "unread" command in the filter module.
25 * Revision 1.24 1996/04/26 10:00:23 adam
26 * Added option -V to zebraidx to display version information.
27 * Removed stupid warnings from file update.
29 * Revision 1.23 1996/04/12 07:02:25 adam
30 * File update of single files.
32 * Revision 1.22 1996/04/09 06:50:50 adam
33 * Bug fix: bad reference in function fileUpdateR.
35 * Revision 1.21 1996/03/22 15:34:18 quinn
38 * Revision 1.20 1996/03/21 14:50:10 adam
39 * File update uses modify-time instead of change-time.
41 * Revision 1.19 1996/03/20 16:16:55 quinn
42 * Added diagnostic output
44 * Revision 1.18 1996/03/19 12:43:27 adam
45 * Bug fix: File update traversal didn't handle trailing slashes correctly.
46 * Bug fix: Update of sub directory groups wasn't handled correctly.
48 * Revision 1.17 1996/02/12 18:45:17 adam
49 * Changed naming of some functions.
51 * Revision 1.16 1996/02/05 12:30:02 adam
52 * Logging reduced a bit.
53 * The remaining running time is estimated during register merge.
55 * Revision 1.15 1995/12/07 17:38:48 adam
56 * Work locking mechanisms for concurrent updates/commit.
58 * Revision 1.14 1995/12/06 12:41:26 adam
59 * New command 'stat' for the index program.
60 * Filenames can be read from stdin by specifying '-'.
61 * Bug fix/enhancement of the transformation from terms to regular
62 * expressons in the search engine.
64 * Revision 1.13 1995/11/28 09:09:46 adam
65 * Zebra config renamed.
66 * Use setting 'recordId' to identify record now.
67 * Bug fix in recindex.c: rec_release_blocks was invokeded even
68 * though the blocks were already released.
69 * File traversal properly deletes records when needed.
71 * Revision 1.12 1995/11/24 11:31:37 adam
72 * Commands add & del read filenames from stdin if source directory is
74 * Match criteria supports 'constant' strings.
76 * Revision 1.11 1995/11/22 17:19:19 adam
77 * Record management uses the bfile system.
79 * Revision 1.10 1995/11/21 15:01:16 adam
80 * New general match criteria implemented.
81 * New feature: document groups.
83 * Revision 1.9 1995/11/21 09:20:32 adam
84 * Yet more work on record match.
86 * Revision 1.8 1995/11/20 16:59:46 adam
87 * New update method: the 'old' keys are saved for each records.
89 * Revision 1.7 1995/11/20 11:56:28 adam
90 * Work on new traversal.
92 * Revision 1.6 1995/11/17 15:54:42 adam
93 * Started work on virtual directory structure.
95 * Revision 1.5 1995/10/17 18:02:09 adam
96 * New feature: databases. Implemented as prefix to words in dictionary.
98 * Revision 1.4 1995/09/28 09:19:46 adam
99 * xfree/xmalloc used everywhere.
100 * Extract/retrieve method seems to work for text records.
102 * Revision 1.3 1995/09/06 16:11:18 adam
103 * Option: only one word key per file.
105 * Revision 1.2 1995/09/04 12:33:43 adam
106 * Various cleanup. YAZ util used instead.
108 * Revision 1.1 1995/09/01 14:06:36 adam
109 * Split of work into more files.
116 #include <sys/stat.h>
117 #include <sys/types.h>
123 static int repComp (const char *a, const char *b, size_t len)
127 return memcmp (a, b, len);
130 static void repositoryExtractR (int deleteFlag, char *rep,
131 struct recordGroup *rGroup)
135 size_t rep_len = strlen (rep);
140 logf (LOG_LOG, "dir %s", rep);
141 if (rep[rep_len-1] != '/')
145 for (i=0; e[i].name; i++)
147 strcpy (rep +rep_len+1, e[i].name);
151 fileExtract (NULL, rep, rGroup, deleteFlag);
154 repositoryExtractR (deleteFlag, rep, rGroup);
162 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
163 const char *base, char *src,
164 struct recordGroup *rGroup)
167 size_t src_len = strlen (src);
169 while (dst && !repComp (dst->path, src, src_len+1))
174 sprintf (tmppath, "%s%s", base, dst->path);
175 fileExtract (&dst->sysno, tmppath, rGroup, 1);
177 strcpy (tmppath, dst->path);
178 dst = dirs_read (di);
179 dirs_del (di, tmppath);
182 strcpy (tmppath, dst->path);
183 dst = dirs_read (di);
184 dirs_rmdir (di, tmppath);
187 dst = dirs_read (di);
192 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
193 const char *base, char *src,
194 struct recordGroup *rGroup)
196 struct dir_entry *e_src;
198 static char tmppath[1024];
199 size_t src_len = strlen (src);
201 sprintf (tmppath, "%s%s", base, src);
202 e_src = dir_open (tmppath);
203 logf (LOG_LOG, "dir %s", tmppath);
206 if (!dst || repComp (dst->path, src, src_len))
208 if (!dst || strcmp (dst->path, src))
214 if (src_len && src[src_len-1] != '/')
217 src[++src_len] = '\0';
219 dirs_mkdir (di, src, 0);
220 if (dst && repComp (dst->path, src, src_len))
225 strcpy (src, dst->path);
226 fileDeleteR (di, dst, base, src, rGroup);
231 if (src_len && src[src_len-1] != '/')
234 src[++src_len] = '\0';
236 dst = dirs_read (di);
244 if (dst && !repComp (dst->path, src, src_len))
246 if (e_src[i_src].name)
248 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
250 sd = strcmp (dst->path + src_len, e_src[i_src].name);
255 else if (e_src[i_src].name)
259 logf (LOG_DEBUG, "trav sd=%d", sd);
262 strcpy (src + src_len, e_src[i_src].name);
263 sprintf (tmppath, "%s%s", base, src);
265 switch (e_src[i_src].kind)
268 if (e_src[i_src].mtime > dst->mtime)
270 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
272 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
274 logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
275 logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
277 dst = dirs_read (di);
280 fileUpdateR (di, dst, base, src, rGroup);
281 dst = dirs_last (di);
282 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
285 dst = dirs_read (di);
292 strcpy (src + src_len, e_src[i_src].name);
293 sprintf (tmppath, "%s%s", base, src);
295 switch (e_src[i_src].kind)
298 if (fileExtract (&sysno, tmppath, rGroup, 0))
299 dirs_add (di, src, sysno, e_src[i_src].mtime);
302 fileUpdateR (di, dst, base, src, rGroup);
304 dst = dirs_last (di);
311 strcpy (src, dst->path);
312 sprintf (tmppath, "%s%s", base, dst->path);
317 fileExtract (&dst->sysno, tmppath, rGroup, 1);
318 dirs_del (di, dst->path);
319 dst = dirs_read (di);
322 fileDeleteR (di, dst, base, src, rGroup);
323 dst = dirs_last (di);
330 static void groupRes (struct recordGroup *rGroup)
335 if (!rGroup->groupName || !*rGroup->groupName)
338 sprintf (gPrefix, "%s.", rGroup->groupName);
340 sprintf (resStr, "%srecordId", gPrefix);
341 rGroup->recordId = res_get (common_resource, resStr);
344 void repositoryShow (struct recordGroup *rGroup)
348 struct dirs_entry *dst;
350 struct dirs_info *di;
352 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
354 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
358 assert (rGroup->path);
359 strcpy (src, rGroup->path);
360 src_len = strlen (src);
362 if (src_len && src[src_len-1] != '/')
365 src[++src_len] = '\0';
368 di = dirs_open (dict, src);
370 while ( (dst = dirs_read (di)) )
371 logf (LOG_LOG, "%s", dst->path);
376 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
379 struct dirs_info *di;
387 src_len = strlen (src);
390 if (S_ISREG(sbuf.st_mode))
392 struct dirs_entry *e_dst;
393 di = dirs_fopen (dict, src);
395 e_dst = dirs_read (di);
398 if (sbuf.st_mtime > e_dst->mtime)
399 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
400 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
405 if (fileExtract (&sysno, src, rGroup, 0))
406 dirs_add (di, src, sysno, sbuf.st_mtime);
410 else if (S_ISDIR(sbuf.st_mode))
412 if (src_len && src[src_len-1] != '/')
415 src[++src_len] = '\0';
417 di = dirs_open (dict, src);
419 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
424 logf (LOG_WARN, "Cannot handle file %s", src);
429 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
439 if (S_ISREG(sbuf.st_mode))
440 fileExtract (NULL, src, rGroup, deleteFlag);
441 else if (S_ISDIR(sbuf.st_mode))
442 repositoryExtractR (deleteFlag, src, rGroup);
444 logf (LOG_WARN, "Cannot handle file %s", src);
447 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
449 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
453 while (scanf ("%s", src) == 1)
454 repositoryExtract (deleteFlag, rGroup, src);
457 repositoryExtract (deleteFlag, rGroup, rGroup->path);
460 void repositoryUpdate (struct recordGroup *rGroup)
463 assert (rGroup->path);
464 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
467 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
469 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
472 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
475 while (scanf ("%s", src) == 1)
476 fileUpdate (dict, rGroup, src);
479 fileUpdate (dict, rGroup, rGroup->path);
483 repositoryExtractG (0, rGroup);
486 void repositoryDelete (struct recordGroup *rGroup)
488 repositoryExtractG (1, rGroup);