2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.33 1998-01-12 15:04:08 adam
8 * The test option (-s) only uses read-lock (and not write lock).
10 * Revision 1.32 1997/09/25 14:56:51 adam
11 * Windows NT interface code to the stat call.
13 * Revision 1.31 1997/09/17 12:19:17 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.30 1997/09/09 13:38:09 adam
18 * Partial port to WIN95/NT.
20 * Revision 1.29 1997/02/12 20:39:47 adam
21 * Implemented options -f <n> that limits the log to the first <n>
23 * Changed some log messages also.
25 * Revision 1.28 1996/11/01 08:58:44 adam
26 * Interface to isamc system now includes update and delete.
28 * Revision 1.27 1996/10/29 14:06:56 adam
29 * Include zebrautl.h instead of alexutil.h.
31 * Revision 1.26 1996/06/04 10:19:01 adam
32 * Minor changes - removed include of ctype.h.
34 * Revision 1.25 1996/05/01 13:46:37 adam
35 * First work on multiple records in one file.
36 * New option, -offset, to the "unread" command in the filter module.
38 * Revision 1.24 1996/04/26 10:00:23 adam
39 * Added option -V to zebraidx to display version information.
40 * Removed stupid warnings from file update.
42 * Revision 1.23 1996/04/12 07:02:25 adam
43 * File update of single files.
45 * Revision 1.22 1996/04/09 06:50:50 adam
46 * Bug fix: bad reference in function fileUpdateR.
48 * Revision 1.21 1996/03/22 15:34:18 quinn
51 * Revision 1.20 1996/03/21 14:50:10 adam
52 * File update uses modify-time instead of change-time.
54 * Revision 1.19 1996/03/20 16:16:55 quinn
55 * Added diagnostic output
57 * Revision 1.18 1996/03/19 12:43:27 adam
58 * Bug fix: File update traversal didn't handle trailing slashes correctly.
59 * Bug fix: Update of sub directory groups wasn't handled correctly.
61 * Revision 1.17 1996/02/12 18:45:17 adam
62 * Changed naming of some functions.
64 * Revision 1.16 1996/02/05 12:30:02 adam
65 * Logging reduced a bit.
66 * The remaining running time is estimated during register merge.
68 * Revision 1.15 1995/12/07 17:38:48 adam
69 * Work locking mechanisms for concurrent updates/commit.
71 * Revision 1.14 1995/12/06 12:41:26 adam
72 * New command 'stat' for the index program.
73 * Filenames can be read from stdin by specifying '-'.
74 * Bug fix/enhancement of the transformation from terms to regular
75 * expressons in the search engine.
77 * Revision 1.13 1995/11/28 09:09:46 adam
78 * Zebra config renamed.
79 * Use setting 'recordId' to identify record now.
80 * Bug fix in recindex.c: rec_release_blocks was invokeded even
81 * though the blocks were already released.
82 * File traversal properly deletes records when needed.
84 * Revision 1.12 1995/11/24 11:31:37 adam
85 * Commands add & del read filenames from stdin if source directory is
87 * Match criteria supports 'constant' strings.
89 * Revision 1.11 1995/11/22 17:19:19 adam
90 * Record management uses the bfile system.
92 * Revision 1.10 1995/11/21 15:01:16 adam
93 * New general match criteria implemented.
94 * New feature: document groups.
96 * Revision 1.9 1995/11/21 09:20:32 adam
97 * Yet more work on record match.
99 * Revision 1.8 1995/11/20 16:59:46 adam
100 * New update method: the 'old' keys are saved for each records.
102 * Revision 1.7 1995/11/20 11:56:28 adam
103 * Work on new traversal.
105 * Revision 1.6 1995/11/17 15:54:42 adam
106 * Started work on virtual directory structure.
108 * Revision 1.5 1995/10/17 18:02:09 adam
109 * New feature: databases. Implemented as prefix to words in dictionary.
111 * Revision 1.4 1995/09/28 09:19:46 adam
112 * xfree/xmalloc used everywhere.
113 * Extract/retrieve method seems to work for text records.
115 * Revision 1.3 1995/09/06 16:11:18 adam
116 * Option: only one word key per file.
118 * Revision 1.2 1995/09/04 12:33:43 adam
119 * Various cleanup. YAZ util used instead.
121 * Revision 1.1 1995/09/01 14:06:36 adam
122 * Split of work into more files.
129 #include <sys/types.h>
130 #include <sys/stat.h>
133 #define S_ISREG(x) (x & _S_IFREG)
134 #define S_ISDIR(x) (x & _S_IFDIR)
144 static int repComp (const char *a, const char *b, size_t len)
148 return memcmp (a, b, len);
151 static void repositoryExtractR (int deleteFlag, char *rep,
152 struct recordGroup *rGroup)
156 size_t rep_len = strlen (rep);
161 logf (LOG_LOG, "dir %s", rep);
162 if (rep[rep_len-1] != '/')
166 for (i=0; e[i].name; i++)
168 strcpy (rep +rep_len+1, e[i].name);
172 fileExtract (NULL, rep, rGroup, deleteFlag);
175 repositoryExtractR (deleteFlag, rep, rGroup);
183 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
184 const char *base, char *src,
185 struct recordGroup *rGroup)
188 size_t src_len = strlen (src);
190 while (dst && !repComp (dst->path, src, src_len+1))
195 sprintf (tmppath, "%s%s", base, dst->path);
196 fileExtract (&dst->sysno, tmppath, rGroup, 1);
198 strcpy (tmppath, dst->path);
199 dst = dirs_read (di);
200 dirs_del (di, tmppath);
203 strcpy (tmppath, dst->path);
204 dst = dirs_read (di);
205 dirs_rmdir (di, tmppath);
208 dst = dirs_read (di);
213 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
214 const char *base, char *src,
215 struct recordGroup *rGroup)
217 struct dir_entry *e_src;
219 static char tmppath[1024];
220 size_t src_len = strlen (src);
222 sprintf (tmppath, "%s%s", base, src);
223 e_src = dir_open (tmppath);
224 logf (LOG_LOG, "dir %s", tmppath);
227 if (!dst || repComp (dst->path, src, src_len))
229 if (!dst || strcmp (dst->path, src))
235 if (src_len && src[src_len-1] != '/')
238 src[++src_len] = '\0';
240 dirs_mkdir (di, src, 0);
241 if (dst && repComp (dst->path, src, src_len))
246 strcpy (src, dst->path);
247 fileDeleteR (di, dst, base, src, rGroup);
252 if (src_len && src[src_len-1] != '/')
255 src[++src_len] = '\0';
257 dst = dirs_read (di);
265 if (dst && !repComp (dst->path, src, src_len))
267 if (e_src[i_src].name)
269 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
271 sd = strcmp (dst->path + src_len, e_src[i_src].name);
276 else if (e_src[i_src].name)
280 logf (LOG_DEBUG, "trav sd=%d", sd);
283 strcpy (src + src_len, e_src[i_src].name);
284 sprintf (tmppath, "%s%s", base, src);
286 switch (e_src[i_src].kind)
289 if (e_src[i_src].mtime > dst->mtime)
291 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
293 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
295 logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
296 logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
298 dst = dirs_read (di);
301 fileUpdateR (di, dst, base, src, rGroup);
302 dst = dirs_last (di);
303 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
306 dst = dirs_read (di);
313 strcpy (src + src_len, e_src[i_src].name);
314 sprintf (tmppath, "%s%s", base, src);
316 switch (e_src[i_src].kind)
319 if (fileExtract (&sysno, tmppath, rGroup, 0))
320 dirs_add (di, src, sysno, e_src[i_src].mtime);
323 fileUpdateR (di, dst, base, src, rGroup);
325 dst = dirs_last (di);
332 strcpy (src, dst->path);
333 sprintf (tmppath, "%s%s", base, dst->path);
338 fileExtract (&dst->sysno, tmppath, rGroup, 1);
339 dirs_del (di, dst->path);
340 dst = dirs_read (di);
343 fileDeleteR (di, dst, base, src, rGroup);
344 dst = dirs_last (di);
351 static void groupRes (struct recordGroup *rGroup)
356 if (!rGroup->groupName || !*rGroup->groupName)
359 sprintf (gPrefix, "%s.", rGroup->groupName);
361 sprintf (resStr, "%srecordId", gPrefix);
362 rGroup->recordId = res_get (common_resource, resStr);
365 void repositoryShow (struct recordGroup *rGroup)
369 struct dirs_entry *dst;
371 struct dirs_info *di;
373 if (!(dict = dict_open (rGroup->bfs, FMATCH_DICT, 50, 0)))
375 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
379 assert (rGroup->path);
380 strcpy (src, rGroup->path);
381 src_len = strlen (src);
383 if (src_len && src[src_len-1] != '/')
386 src[++src_len] = '\0';
389 di = dirs_open (dict, src, rGroup->flagRw);
391 while ( (dst = dirs_read (di)) )
392 logf (LOG_LOG, "%s", dst->path);
397 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
400 struct dirs_info *di;
408 src_len = strlen (src);
411 if (S_ISREG(sbuf.st_mode))
413 struct dirs_entry *e_dst;
414 di = dirs_fopen (dict, src);
416 e_dst = dirs_read (di);
419 if (sbuf.st_mtime > e_dst->mtime)
420 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
421 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
426 if (fileExtract (&sysno, src, rGroup, 0))
427 dirs_add (di, src, sysno, sbuf.st_mtime);
431 else if (S_ISDIR(sbuf.st_mode))
433 if (src_len && src[src_len-1] != '/')
436 src[++src_len] = '\0';
438 di = dirs_open (dict, src, rGroup->flagRw);
440 fileUpdateR (di, dirs_read (di), src, dst, rGroup);
445 logf (LOG_WARN, "Ignoring path %s", src);
450 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
460 if (S_ISREG(sbuf.st_mode))
461 fileExtract (NULL, src, rGroup, deleteFlag);
462 else if (S_ISDIR(sbuf.st_mode))
463 repositoryExtractR (deleteFlag, src, rGroup);
465 logf (LOG_WARN, "Ignoring path %s", src);
468 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
470 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
474 while (scanf ("%s", src) == 1)
475 repositoryExtract (deleteFlag, rGroup, src);
478 repositoryExtract (deleteFlag, rGroup, rGroup->path);
481 void repositoryUpdate (struct recordGroup *rGroup)
484 assert (rGroup->path);
485 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
488 if (!(dict = dict_open (rGroup->bfs, FMATCH_DICT, 50, rGroup->flagRw)))
490 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
493 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
496 while (scanf ("%s", src) == 1)
497 fileUpdate (dict, rGroup, src);
500 fileUpdate (dict, rGroup, rGroup->path);
504 repositoryExtractG (0, rGroup);
507 void repositoryDelete (struct recordGroup *rGroup)
509 repositoryExtractG (1, rGroup);