2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.35 1999-02-02 14:51:09 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.34 1998/06/08 14:43:14 adam
11 * Added suport for EXPLAIN Proxy servers - added settings databasePath
12 * and explainDatabase to facilitate this. Increased maximum number
13 * of databases and attributes in one register.
15 * Revision 1.33 1998/01/12 15:04:08 adam
16 * The test option (-s) only uses read-lock (and not write lock).
18 * Revision 1.32 1997/09/25 14:56:51 adam
19 * Windows NT interface code to the stat call.
21 * Revision 1.31 1997/09/17 12:19:17 adam
22 * Zebra version corresponds to YAZ version 1.4.
23 * Changed Zebra server so that it doesn't depend on global common_resource.
25 * Revision 1.30 1997/09/09 13:38:09 adam
26 * Partial port to WIN95/NT.
28 * Revision 1.29 1997/02/12 20:39:47 adam
29 * Implemented options -f <n> that limits the log to the first <n>
31 * Changed some log messages also.
33 * Revision 1.28 1996/11/01 08:58:44 adam
34 * Interface to isamc system now includes update and delete.
36 * Revision 1.27 1996/10/29 14:06:56 adam
37 * Include zebrautl.h instead of alexutil.h.
39 * Revision 1.26 1996/06/04 10:19:01 adam
40 * Minor changes - removed include of ctype.h.
42 * Revision 1.25 1996/05/01 13:46:37 adam
43 * First work on multiple records in one file.
44 * New option, -offset, to the "unread" command in the filter module.
46 * Revision 1.24 1996/04/26 10:00:23 adam
47 * Added option -V to zebraidx to display version information.
48 * Removed stupid warnings from file update.
50 * Revision 1.23 1996/04/12 07:02:25 adam
51 * File update of single files.
53 * Revision 1.22 1996/04/09 06:50:50 adam
54 * Bug fix: bad reference in function fileUpdateR.
56 * Revision 1.21 1996/03/22 15:34:18 quinn
59 * Revision 1.20 1996/03/21 14:50:10 adam
60 * File update uses modify-time instead of change-time.
62 * Revision 1.19 1996/03/20 16:16:55 quinn
63 * Added diagnostic output
65 * Revision 1.18 1996/03/19 12:43:27 adam
66 * Bug fix: File update traversal didn't handle trailing slashes correctly.
67 * Bug fix: Update of sub directory groups wasn't handled correctly.
69 * Revision 1.17 1996/02/12 18:45:17 adam
70 * Changed naming of some functions.
72 * Revision 1.16 1996/02/05 12:30:02 adam
73 * Logging reduced a bit.
74 * The remaining running time is estimated during register merge.
76 * Revision 1.15 1995/12/07 17:38:48 adam
77 * Work locking mechanisms for concurrent updates/commit.
79 * Revision 1.14 1995/12/06 12:41:26 adam
80 * New command 'stat' for the index program.
81 * Filenames can be read from stdin by specifying '-'.
82 * Bug fix/enhancement of the transformation from terms to regular
83 * expressons in the search engine.
85 * Revision 1.13 1995/11/28 09:09:46 adam
86 * Zebra config renamed.
87 * Use setting 'recordId' to identify record now.
88 * Bug fix in recindex.c: rec_release_blocks was invokeded even
89 * though the blocks were already released.
90 * File traversal properly deletes records when needed.
92 * Revision 1.12 1995/11/24 11:31:37 adam
93 * Commands add & del read filenames from stdin if source directory is
95 * Match criteria supports 'constant' strings.
97 * Revision 1.11 1995/11/22 17:19:19 adam
98 * Record management uses the bfile system.
100 * Revision 1.10 1995/11/21 15:01:16 adam
101 * New general match criteria implemented.
102 * New feature: document groups.
104 * Revision 1.9 1995/11/21 09:20:32 adam
105 * Yet more work on record match.
107 * Revision 1.8 1995/11/20 16:59:46 adam
108 * New update method: the 'old' keys are saved for each records.
110 * Revision 1.7 1995/11/20 11:56:28 adam
111 * Work on new traversal.
113 * Revision 1.6 1995/11/17 15:54:42 adam
114 * Started work on virtual directory structure.
116 * Revision 1.5 1995/10/17 18:02:09 adam
117 * New feature: databases. Implemented as prefix to words in dictionary.
119 * Revision 1.4 1995/09/28 09:19:46 adam
120 * xfree/xmalloc used everywhere.
121 * Extract/retrieve method seems to work for text records.
123 * Revision 1.3 1995/09/06 16:11:18 adam
124 * Option: only one word key per file.
126 * Revision 1.2 1995/09/04 12:33:43 adam
127 * Various cleanup. YAZ util used instead.
129 * Revision 1.1 1995/09/01 14:06:36 adam
130 * Split of work into more files.
137 #include <sys/types.h>
138 #include <sys/stat.h>
141 #define S_ISREG(x) (x & _S_IFREG)
142 #define S_ISDIR(x) (x & _S_IFDIR)
152 static int repComp (const char *a, const char *b, size_t len)
156 return memcmp (a, b, len);
159 static void repositoryExtractR (int deleteFlag, char *rep,
160 struct recordGroup *rGroup,
165 size_t rep_len = strlen (rep);
170 logf (LOG_LOG, "dir %s", rep);
171 if (rep[rep_len-1] != '/')
176 for (i=0; e[i].name; i++)
179 strcpy (rep +rep_len+1, e[i].name);
180 if ((ecp = strrchr (e[i].name, '/')))
182 if (level == 0 && rGroup->databaseNamePath)
183 rGroup->databaseName = e[i].name;
188 fileExtract (NULL, rep, rGroup, deleteFlag);
191 repositoryExtractR (deleteFlag, rep, rGroup, level+1);
199 static void fileDeleteR (struct dirs_info *di, struct dirs_entry *dst,
200 const char *base, char *src,
201 struct recordGroup *rGroup)
204 size_t src_len = strlen (src);
206 while (dst && !repComp (dst->path, src, src_len+1))
211 sprintf (tmppath, "%s%s", base, dst->path);
212 fileExtract (&dst->sysno, tmppath, rGroup, 1);
214 strcpy (tmppath, dst->path);
215 dst = dirs_read (di);
216 dirs_del (di, tmppath);
219 strcpy (tmppath, dst->path);
220 dst = dirs_read (di);
221 dirs_rmdir (di, tmppath);
224 dst = dirs_read (di);
229 static void fileUpdateR (struct dirs_info *di, struct dirs_entry *dst,
230 const char *base, char *src,
231 struct recordGroup *rGroup,
234 struct dir_entry *e_src;
236 static char tmppath[1024];
237 size_t src_len = strlen (src);
239 sprintf (tmppath, "%s%s", base, src);
240 e_src = dir_open (tmppath);
241 logf (LOG_LOG, "dir %s", tmppath);
244 if (!dst || repComp (dst->path, src, src_len))
246 if (!dst || strcmp (dst->path, src))
252 if (src_len && src[src_len-1] != '/')
255 src[++src_len] = '\0';
257 dirs_mkdir (di, src, 0);
258 if (dst && repComp (dst->path, src, src_len))
263 strcpy (src, dst->path);
264 fileDeleteR (di, dst, base, src, rGroup);
269 if (src_len && src[src_len-1] != '/')
272 src[++src_len] = '\0';
274 dst = dirs_read (di);
282 if (dst && !repComp (dst->path, src, src_len))
284 if (e_src[i_src].name)
286 logf (LOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
288 sd = strcmp (dst->path + src_len, e_src[i_src].name);
293 else if (e_src[i_src].name)
297 logf (LOG_DEBUG, "trav sd=%d", sd);
299 if (level == 0 && rGroup->databaseNamePath)
300 rGroup->databaseName = e_src[i_src].name;
303 strcpy (src + src_len, e_src[i_src].name);
304 sprintf (tmppath, "%s%s", base, src);
306 switch (e_src[i_src].kind)
309 if (e_src[i_src].mtime > dst->mtime)
311 if (fileExtract (&dst->sysno, tmppath, rGroup, 0))
313 dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
315 logf (LOG_DEBUG, "old: %s", ctime (&dst->mtime));
316 logf (LOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
318 dst = dirs_read (di);
321 fileUpdateR (di, dst, base, src, rGroup, level+1);
322 dst = dirs_last (di);
323 logf (LOG_DEBUG, "last is %s", dst ? dst->path : "null");
326 dst = dirs_read (di);
333 strcpy (src + src_len, e_src[i_src].name);
334 sprintf (tmppath, "%s%s", base, src);
336 switch (e_src[i_src].kind)
339 if (fileExtract (&sysno, tmppath, rGroup, 0))
340 dirs_add (di, src, sysno, e_src[i_src].mtime);
343 fileUpdateR (di, dst, base, src, rGroup, level+1);
345 dst = dirs_last (di);
352 strcpy (src, dst->path);
353 sprintf (tmppath, "%s%s", base, dst->path);
358 fileExtract (&dst->sysno, tmppath, rGroup, 1);
359 dirs_del (di, dst->path);
360 dst = dirs_read (di);
363 fileDeleteR (di, dst, base, src, rGroup);
364 dst = dirs_last (di);
371 static void groupRes (struct recordGroup *rGroup)
376 if (!rGroup->groupName || !*rGroup->groupName)
379 sprintf (gPrefix, "%s.", rGroup->groupName);
381 sprintf (resStr, "%srecordId", gPrefix);
382 rGroup->recordId = res_get (common_resource, resStr);
383 sprintf (resStr, "%sdatabasePath", gPrefix);
384 rGroup->databaseNamePath =
385 atoi (res_get_def (common_resource, resStr, "0"));
388 void repositoryShow (struct recordGroup *rGroup)
392 struct dirs_entry *dst;
394 struct dirs_info *di;
396 if (!(dict = dict_open (rGroup->bfs, FMATCH_DICT, 50, 0)))
398 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
402 assert (rGroup->path);
403 strcpy (src, rGroup->path);
404 src_len = strlen (src);
406 if (src_len && src[src_len-1] != '/')
409 src[++src_len] = '\0';
412 di = dirs_open (dict, src, rGroup->flagRw);
414 while ( (dst = dirs_read (di)) )
415 logf (LOG_LOG, "%s", dst->path);
420 static void fileUpdate (Dict dict, struct recordGroup *rGroup,
423 struct dirs_info *di;
431 src_len = strlen (src);
434 if (S_ISREG(sbuf.st_mode))
436 struct dirs_entry *e_dst;
437 di = dirs_fopen (dict, src);
439 e_dst = dirs_read (di);
442 if (sbuf.st_mtime > e_dst->mtime)
443 if (fileExtract (&e_dst->sysno, src, rGroup, 0))
444 dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
449 if (fileExtract (&sysno, src, rGroup, 0))
450 dirs_add (di, src, sysno, sbuf.st_mtime);
454 else if (S_ISDIR(sbuf.st_mode))
456 if (src_len && src[src_len-1] != '/')
459 src[++src_len] = '\0';
461 di = dirs_open (dict, src, rGroup->flagRw);
463 fileUpdateR (di, dirs_read (di), src, dst, rGroup, 0);
468 logf (LOG_WARN, "Ignoring path %s", src);
473 static void repositoryExtract (int deleteFlag, struct recordGroup *rGroup,
483 if (S_ISREG(sbuf.st_mode))
484 fileExtract (NULL, src, rGroup, deleteFlag);
485 else if (S_ISDIR(sbuf.st_mode))
486 repositoryExtractR (deleteFlag, src, rGroup, 0);
488 logf (LOG_WARN, "Ignoring path %s", src);
491 static void repositoryExtractG (int deleteFlag, struct recordGroup *rGroup)
493 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
497 while (scanf ("%s", src) == 1)
498 repositoryExtract (deleteFlag, rGroup, src);
501 repositoryExtract (deleteFlag, rGroup, rGroup->path);
504 void repositoryUpdate (struct recordGroup *rGroup)
507 assert (rGroup->path);
508 if (rGroup->recordId && !strcmp (rGroup->recordId, "file"))
511 if (!(dict = dict_open (rGroup->bfs, FMATCH_DICT, 50, rGroup->flagRw)))
513 logf (LOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
516 if (*rGroup->path == '\0' || !strcmp(rGroup->path, "-"))
519 while (scanf ("%s", src) == 1)
520 fileUpdate (dict, rGroup, src);
523 fileUpdate (dict, rGroup, rGroup->path);
527 repositoryExtractG (0, rGroup);
530 void repositoryDelete (struct recordGroup *rGroup)
532 repositoryExtractG (1, rGroup);