2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.28 1996-11-01 08:58:44 adam
8 * Interface to isamc system now includes update and delete.
10 * Revision 1.27 1996/10/29 14:06:56 adam
11 * Include zebrautl.h instead of alexutil.h.
13 * Revision 1.26 1996/06/04 10:19:01 adam
14 * Minor changes - removed include of ctype.h.
16 * Revision 1.25 1996/05/01 13:46:37 adam
17 * First work on multiple records in one file.
18 * New option, -offset, to the "unread" command in the filter module.
20 * Revision 1.24 1996/04/26 10:00:23 adam
21 * Added option -V to zebraidx to display version information.
22 * Removed stupid warnings from file update.
24 * Revision 1.23 1996/04/12 07:02:25 adam
25 * File update of single files.
27 * Revision 1.22 1996/04/09 06:50:50 adam
28 * Bug fix: bad reference in function fileUpdateR.
30 * Revision 1.21 1996/03/22 15:34:18 quinn
33 * Revision 1.20 1996/03/21 14:50:10 adam
34 * File update uses modify-time instead of change-time.
36 * Revision 1.19 1996/03/20 16:16:55 quinn
37 * Added diagnostic output
39 * Revision 1.18 1996/03/19 12:43:27 adam
40 * Bug fix: File update traversal didn't handle trailing slashes correctly.
41 * Bug fix: Update of sub directory groups wasn't handled correctly.
43 * Revision 1.17 1996/02/12 18:45:17 adam
44 * Changed naming of some functions.
46 * Revision 1.16 1996/02/05 12:30:02 adam
47 * Logging reduced a bit.
48 * The remaining running time is estimated during register merge.
50 * Revision 1.15 1995/12/07 17:38:48 adam
51 * Work locking mechanisms for concurrent updates/commit.
53 * Revision 1.14 1995/12/06 12:41:26 adam
54 * New command 'stat' for the index program.
55 * Filenames can be read from stdin by specifying '-'.
56 * Bug fix/enhancement of the transformation from terms to regular
57 * expressons in the search engine.
59 * Revision 1.13 1995/11/28 09:09:46 adam
60 * Zebra config renamed.
61 * Use setting 'recordId' to identify record now.
62 * Bug fix in recindex.c: rec_release_blocks was invokeded even
63 * though the blocks were already released.
64 * File traversal properly deletes records when needed.
66 * Revision 1.12 1995/11/24 11:31:37 adam
67 * Commands add & del read filenames from stdin if source directory is
69 * Match criteria supports 'constant' strings.
71 * Revision 1.11 1995/11/22 17:19:19 adam
72 * Record management uses the bfile system.
74 * Revision 1.10 1995/11/21 15:01:16 adam
75 * New general match criteria implemented.
76 * New feature: document groups.
78 * Revision 1.9 1995/11/21 09:20:32 adam
79 * Yet more work on record match.
81 * Revision 1.8 1995/11/20 16:59:46 adam
82 * New update method: the 'old' keys are saved for each records.
84 * Revision 1.7 1995/11/20 11:56:28 adam
85 * Work on new traversal.
87 * Revision 1.6 1995/11/17 15:54:42 adam
88 * Started work on virtual directory structure.
90 * Revision 1.5 1995/10/17 18:02:09 adam
91 * New feature: databases. Implemented as prefix to words in dictionary.
93 * Revision 1.4 1995/09/28 09:19:46 adam
94 * xfree/xmalloc used everywhere.
95 * Extract/retrieve method seems to work for text records.
97 * Revision 1.3 1995/09/06 16:11:18 adam
98 * Option: only one word key per file.
100 * Revision 1.2 1995/09/04 12:33:43 adam
101 * Various cleanup. YAZ util used instead.
103 * Revision 1.1 1995/09/01 14:06:36 adam
104 * Split of work into more files.
111 #include <sys/stat.h>
112 #include <sys/types.h>
118 static int repComp (const char *a, const char *b, size_t len)
122 return memcmp (a, b, len);
125 static void repositoryExtractR (int deleteFlag, char *rep,
126 struct recordGroup *rGroup)
130 size_t rep_len = strlen (rep);
135 logf (LOG_LOG, "Dir: %s", rep);
136 if (rep[rep_len-1] != '/')
140 for (i=0; e[i].name; i++)
142 strcpy (rep +rep_len+1, e[i].name);
146 fileExtract (NULL, rep, rGroup, deleteFlag);
149 repositoryExtractR (deleteFlag, rep, rGroup);
157 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
158 const char *base, char *src,
159 struct recordGroup *rGroup)
162 size_t src_len = strlen (src);
164 while (dst && !repComp (dst->path, src, src_len+1))
169 sprintf (tmppath, "%s%s", base, dst->path);
170 fileExtract (&dst->sysno, tmppath, rGroup, 1);
172 strcpy (tmppath, dst->path);
173 dst = dirs_read (di);
174 dirs_del (di, tmppath);
177 strcpy (tmppath, dst->path);
178 dst = dirs_read (di);
179 dirs_rmdir (di, tmppath);
182 dst = dirs_read (di);
187 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
188 const char *base, char *src,
189 struct recordGroup *rGroup)
191 struct dir_entry *e_src;
193 static char tmppath[1024];
194 size_t src_len = strlen (src);
196 sprintf (tmppath, "%s%s", base, src);
197 e_src = dir_open (tmppath);
198 logf (LOG_LOG, "Dir: %s", tmppath);
201 if (!dst || repComp (dst->path, src, src_len))
203 if (!dst || strcmp (dst->path, src))
209 if (src_len && src[src_len-1] != '/')
212 src[++src_len] = '\0';
214 dirs_mkdir (di, src, 0);
215 if (dst && repComp (dst->path, src, src_len))
220 strcpy (src, dst->path);
221 fileDeleteR (di, dst, base, src, rGroup);
226 if (src_len && src[src_len-1] != '/')
229 src[++src_len] = '\0';
231 dst = dirs_read (di);
239 if (dst && !repComp (dst->path, src, src_len))
241 if (e_src[i_src].name)
243 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
245 sd = strcmp (dst->path + src_len, e_src[i_src].name);
250 else if (e_src[i_src].name)
254 logf (LOG_DEBUG, "trav sd=%d", sd);
257 strcpy (src + src_len, e_src[i_src].name);
258 sprintf (tmppath, "%s%s", base, src);
260 switch (e_src[i_src].kind)
263 if (e_src[i_src].mtime > dst->mtime)
265 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
267 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
269 logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
270 logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
272 dst = dirs_read (di);
275 fileUpdateR (di, dst, base, src, rGroup);
276 dst = dirs_last (di);
277 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
280 dst = dirs_read (di);
287 strcpy (src + src_len, e_src[i_src].name);
288 sprintf (tmppath, "%s%s", base, src);
290 switch (e_src[i_src].kind)
293 if (fileExtract (&sysno, tmppath, rGroup, 0))
294 dirs_add (di, src, sysno, e_src[i_src].mtime);
297 fileUpdateR (di, dst, base, src, rGroup);
299 dst = dirs_last (di);
306 strcpy (src, dst->path);
307 sprintf (tmppath, "%s%s", base, dst->path);
312 fileExtract (&dst->sysno, tmppath, rGroup, 1);
313 dirs_del (di, dst->path);
314 dst = dirs_read (di);
317 fileDeleteR (di, dst, base, src, rGroup);
318 dst = dirs_last (di);
325 static void groupRes (struct recordGroup *rGroup)
330 if (!rGroup->groupName || !*rGroup->groupName)
333 sprintf (gPrefix, "%s.", rGroup->groupName);
335 sprintf (resStr, "%srecordId", gPrefix);
336 rGroup->recordId = res_get (common_resource, resStr);
339 void repositoryShow (struct recordGroup *rGroup)
343 struct dirs_entry *dst;
345 struct dirs_info *di;
347 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
349 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
353 assert (rGroup->path);
354 strcpy (src, rGroup->path);
355 src_len = strlen (src);
357 if (src_len && src[src_len-1] != '/')
360 src[++src_len] = '\0';
363 di = dirs_open (dict, src);
365 while ( (dst = dirs_read (di)) )
366 logf (LOG_LOG, "%s", dst->path);
371 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
374 struct dirs_info *di;
382 src_len = strlen (src);
385 if (S_ISREG(sbuf.st_mode))
387 struct dirs_entry *e_dst;
388 di = dirs_fopen (dict, src);
390 e_dst = dirs_read (di);
393 if (sbuf.st_mtime > e_dst->mtime)
394 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
395 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
400 if (fileExtract (&sysno, src, rGroup, 0))
401 dirs_add (di, src, sysno, sbuf.st_mtime);
405 else if (S_ISDIR(sbuf.st_mode))
407 if (src_len && src[src_len-1] != '/')
410 src[++src_len] = '\0';
412 di = dirs_open (dict, src);
414 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
419 logf (LOG_WARN, "Cannot handle file %s", src);
424 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
434 if (S_ISREG(sbuf.st_mode))
435 fileExtract (NULL, src, rGroup, deleteFlag);
436 else if (S_ISDIR(sbuf.st_mode))
437 repositoryExtractR (deleteFlag, src, rGroup);
439 logf (LOG_WARN, "Cannot handle file %s", src);
442 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
444 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
448 while (scanf ("%s", src) == 1)
449 repositoryExtract (deleteFlag, rGroup, src);
452 repositoryExtract (deleteFlag, rGroup, rGroup->path);
455 void repositoryUpdate (struct recordGroup *rGroup)
458 assert (rGroup->path);
459 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
462 if (!(dict = dict_open (FMATCH_DICT, 50, 1)))
464 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
467 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
470 while (scanf ("%s", src) == 1)
471 fileUpdate (dict, rGroup, src);
474 fileUpdate (dict, rGroup, rGroup->path);
478 repositoryExtractG (0, rGroup);
481 void repositoryDelete (struct recordGroup *rGroup)
483 repositoryExtractG (1, rGroup);