2007.
[idzebra-moved-to-github.git] / index / update_file.c
1 /* $Id: update_file.c,v 1.5 2007-01-15 15:10:17 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <sys/types.h>
26 #ifdef WIN32
27 #include <io.h>
28 #define S_ISREG(x) (x & _S_IFREG)
29 #define S_ISDIR(x) (x & _S_IFDIR)
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <direntz.h>
35 #include <fcntl.h>
36 #include <time.h>
37
38 #include "index.h"
39
40 #if 0
41 static int dump_file_dict_func(char *name, const char *info, int pos,
42                                 void *client)
43 {
44     yaz_log(YLOG_LOG, "%s", name);
45     return 0;
46 }
47 static void dump_file_dict(Dict dict)
48 {
49     int before = 10;
50     int after = 1000;
51     char term[1000];
52     
53     strcpy(term, "0");
54     dict_scan (dict, term, &before, &after, 0, dump_file_dict_func);
55 }
56 #endif
57
58 static int repComp (const char *a, const char *b, size_t len)
59 {
60     if (!len)
61         return 0;
62     return memcmp (a, b, len);
63 }
64
65 static void fileDelete_r(ZebraHandle zh,
66                          struct dirs_info *di, struct dirs_entry *dst,
67                          const char *base, char *src)
68 {
69     char tmppath[1024];
70     size_t src_len = strlen (src);
71
72     while (dst && !repComp (dst->path, src, src_len+1))
73     {
74         switch (dst->kind)
75         {
76         case dirs_file:
77             sprintf (tmppath, "%s%s", base, dst->path);
78             zebra_extract_file (zh, &dst->sysno, tmppath, 1);
79              
80             strcpy (tmppath, dst->path);
81             dst = dirs_read (di); 
82             dirs_del (di, tmppath);
83             break;
84         case dirs_dir:
85             strcpy (tmppath, dst->path);
86             dst = dirs_read (di);
87             dirs_rmdir (di, tmppath);
88             break;
89         default:
90             dst = dirs_read (di);
91         }
92     }
93 }
94
95 static void file_update_r(ZebraHandle zh,
96                           struct dirs_info *di, struct dirs_entry *dst,
97                           const char *base, char *src, 
98                           int level)
99 {
100     struct dir_entry *e_src;
101     int i_src = 0;
102     static char tmppath[1024];
103     size_t src_len = strlen (src);
104
105     sprintf (tmppath, "%s%s", base, src);
106     e_src = dir_open (tmppath, zh->path_reg, zh->m_follow_links);
107     yaz_log(YLOG_LOG, "dir %s", tmppath);
108
109 #if 0
110     if (!dst || repComp (dst->path, src, src_len))
111 #else
112     if (!dst || strcmp (dst->path, src))
113 #endif
114     {
115         if (!e_src)
116             return;
117
118         if (src_len && src[src_len-1] != '/')
119         {
120             src[src_len] = '/';
121             src[++src_len] = '\0';
122         }
123         dirs_mkdir (di, src, 0);
124         if (dst && repComp (dst->path, src, src_len))
125             dst = NULL;
126     }
127     else if (!e_src)
128     {
129         strcpy (src, dst->path);
130         fileDelete_r(zh, di, dst, base, src);
131         return;
132     }
133     else
134     {
135         if (src_len && src[src_len-1] != '/')
136         {
137             src[src_len] = '/';
138             src[++src_len] = '\0';
139         }
140         dst = dirs_read (di); 
141     }
142     dir_sort (e_src);
143
144     while (1)
145     {
146         int sd;
147
148         if (dst && !repComp (dst->path, src, src_len))
149         {
150             if (e_src[i_src].name)
151             {
152                 yaz_log(YLOG_DEBUG, "dst=%s src=%s", dst->path + src_len,
153                       e_src[i_src].name);
154                 sd = strcmp (dst->path + src_len, e_src[i_src].name);
155             }
156             else
157                 sd = -1;
158         }
159         else if (e_src[i_src].name)
160             sd = 1;
161         else
162             break;
163         yaz_log(YLOG_DEBUG, "trav sd=%d", sd);
164
165         if (sd == 0)
166         {
167             strcpy (src + src_len, e_src[i_src].name);
168             sprintf (tmppath, "%s%s", base, src);
169             
170             switch (e_src[i_src].kind)
171             {
172             case dirs_file:
173                 if (e_src[i_src].mtime > dst->mtime)
174                 {
175                     if (zebra_extract_file (zh, &dst->sysno, tmppath, 0) == ZEBRA_OK)
176                     {
177                         dirs_add (di, src, dst->sysno, e_src[i_src].mtime);
178                     }
179                     yaz_log(YLOG_DEBUG, "old: %s", ctime (&dst->mtime));
180                     yaz_log(YLOG_DEBUG, "new: %s", ctime (&e_src[i_src].mtime));
181                 }
182                 dst = dirs_read (di);
183                 break;
184             case dirs_dir:
185                 file_update_r(zh, di, dst, base, src, level+1);
186                 dst = dirs_last (di);
187                 yaz_log(YLOG_DEBUG, "last is %s", dst ? dst->path : "null");
188                 break;
189             default:
190                 dst = dirs_read (di); 
191             }
192             i_src++;
193         }
194         else if (sd > 0)
195         {
196             zint sysno = 0;
197             strcpy (src + src_len, e_src[i_src].name);
198             sprintf (tmppath, "%s%s", base, src);
199
200             switch (e_src[i_src].kind)
201             {
202             case dirs_file:
203                 if (zebra_extract_file (zh, &sysno, tmppath, 0) == ZEBRA_OK)
204                     dirs_add (di, src, sysno, e_src[i_src].mtime);            
205                 break;
206             case dirs_dir:
207                 file_update_r(zh, di, dst, base, src, level+1);
208                 if (dst)
209                     dst = dirs_last (di);
210                 break;
211             }
212             i_src++;
213         }
214         else  /* sd < 0 */
215         {
216             strcpy (src, dst->path);
217             sprintf (tmppath, "%s%s", base, dst->path);
218
219             switch (dst->kind)
220             {
221             case dirs_file:
222                 zebra_extract_file (zh, &dst->sysno, tmppath, 1);
223                 dirs_del (di, dst->path);
224                 dst = dirs_read (di);
225                 break;
226             case dirs_dir:
227                 fileDelete_r(zh, di, dst, base, src);
228                 dst = dirs_last (di);
229             }
230         }
231     }
232     dir_free (&e_src);
233 }
234
235 static void file_update_top(ZebraHandle zh, Dict dict, const char *path)
236 {
237     struct dirs_info *di;
238     struct stat sbuf;
239     char src[1024];
240     char dst[1024];
241     int src_len, ret;
242
243     assert (path);
244
245     if (zh->path_reg && !yaz_is_abspath(path))
246     {
247         strcpy (src, zh->path_reg);
248         strcat (src, "/");
249     }
250     else
251         *src = '\0';
252     strcat (src, path);
253     ret = zebra_file_stat (src, &sbuf, zh->m_follow_links);
254
255     strcpy (src, path);
256     src_len = strlen (src);
257
258     if (ret == -1)
259     {
260         yaz_log(YLOG_WARN|YLOG_ERRNO, "Cannot access path %s", src);
261     } 
262     else if (S_ISREG(sbuf.st_mode))
263     {
264         struct dirs_entry *e_dst;
265         di = dirs_fopen (dict, src, zh->m_flag_rw);
266
267         e_dst = dirs_read(di);
268         if (e_dst)
269         {
270             if (sbuf.st_mtime > e_dst->mtime)
271                 if (zebra_extract_file (zh, &e_dst->sysno, src, 0) == ZEBRA_OK)
272                     dirs_add (di, src, e_dst->sysno, sbuf.st_mtime);
273         }
274         else
275         {
276             zint sysno = 0;
277             if (zebra_extract_file (zh, &sysno, src, 0) == ZEBRA_OK)
278                  dirs_add (di, src, sysno, sbuf.st_mtime);
279         }
280         dirs_free(&di);
281     }
282     else if (S_ISDIR(sbuf.st_mode))
283     {
284         if (src_len && src[src_len-1] != '/')
285         {
286             src[src_len] = '/';
287             src[++src_len] = '\0';
288         }
289         di = dirs_open (dict, src, zh->m_flag_rw);
290         *dst = '\0';
291         file_update_r(zh, di, dirs_read (di), src, dst, 0);
292         dirs_free (&di);
293     }
294     else
295     {
296         yaz_log(YLOG_WARN, "Skipping path %s", src);
297     }
298 }
299
300 static ZEBRA_RES zebra_open_fmatch(ZebraHandle zh, Dict *dictp)
301 {
302     char fmatch_fname[1024];
303     int ord;
304
305     ord = zebraExplain_get_database_ord(zh->reg->zei);
306     sprintf(fmatch_fname, FMATCH_DICT, ord);
307     if (!(*dictp = dict_open_res (zh->reg->bfs, fmatch_fname, 50,
308                                 zh->m_flag_rw, 0, zh->res)))
309     {
310         yaz_log(YLOG_FATAL, "dict_open fail of %s", fmatch_fname);
311         return ZEBRA_FAIL;
312     }
313     return ZEBRA_OK;
314 }
315
316 ZEBRA_RES zebra_remove_file_match(ZebraHandle zh)
317 {
318     Dict dict;
319     
320     if (zebra_open_fmatch(zh, &dict) != ZEBRA_OK)
321         return ZEBRA_FAIL;
322
323     dict_clean(dict);
324     dict_close(dict);
325
326     return ZEBRA_OK;
327 }
328
329 ZEBRA_RES zebra_update_file_match(ZebraHandle zh, const char *path)
330 {
331     Dict dict;
332
333     if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
334     {
335         if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0], 0))
336             return ZEBRA_FAIL;
337     }
338     if (zebra_open_fmatch(zh, &dict) != ZEBRA_OK)
339         return ZEBRA_FAIL;
340     
341     if (!strcmp(path, "") || !strcmp(path, "-"))
342     {
343         char src[1024];
344         while (scanf ("%s", src) == 1)
345             file_update_top(zh, dict, src);
346     }
347     else
348         file_update_top(zh, dict, path);
349 #if 0
350     dump_file_dict(dict);
351 #endif
352     dict_close (dict);
353     return ZEBRA_OK;
354 }
355
356
357 /*
358  * Local variables:
359  * c-basic-offset: 4
360  * indent-tabs-mode: nil
361  * End:
362  * vim: shiftwidth=4 tabstop=8 expandtab
363  */
364