b4eec12e685cbdaf6f05540c8dd292e8ad5721d0
[idzebra-moved-to-github.git] / index / update_path.c
1 /* $Id: update_path.c,v 1.1 2006-05-30 13:21:16 adam Exp $
2    Copyright (C) 1995-2006
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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 static void repositoryExtractR(ZebraHandle zh, int deleteFlag, char *rep,
41                                int level)
42 {
43     struct dir_entry *e;
44     int i;
45     size_t rep_len = strlen (rep);
46
47     e = dir_open (rep, zh->path_reg, zh->m_follow_links);
48     if (!e)
49         return;
50     yaz_log (YLOG_LOG, "dir %s", rep);
51     if (rep[rep_len-1] != '/')
52         rep[rep_len] = '/';
53     else
54         --rep_len;
55     
56     for (i=0; e[i].name; i++)
57     {
58         char *ecp;
59         strcpy (rep +rep_len+1, e[i].name);
60         if ((ecp = strrchr (e[i].name, '/')))
61             *ecp = '\0';
62
63         switch (e[i].kind)
64         {
65         case dirs_file:
66             zebra_extract_file (zh, NULL, rep, deleteFlag);
67             break;
68         case dirs_dir:
69             repositoryExtractR (zh, deleteFlag, rep, level+1);
70             break;
71         }
72     }
73     dir_free (&e);
74
75 }
76
77 void repositoryShow(ZebraHandle zh, const char *path)
78 {
79     char src[1024];
80     int src_len;
81     struct dirs_entry *dst;
82     Dict dict;
83     struct dirs_info *di;
84
85     if (!(dict = dict_open_res (zh->reg->bfs, FMATCH_DICT, 50, 0, 0, zh->res)))
86     {
87         yaz_log (YLOG_FATAL, "dict_open fail of %s", FMATCH_DICT);
88         return;
89     }
90     
91     strncpy(src, path, sizeof(src)-1);
92     src[sizeof(src)-1]='\0';
93     src_len = strlen (src);
94     
95     if (src_len && src[src_len-1] != '/')
96     {
97         src[src_len] = '/';
98         src[++src_len] = '\0';
99     }
100     
101     di = dirs_open (dict, src, zh->m_flag_rw);
102     
103     while ( (dst = dirs_read (di)) )
104         yaz_log (YLOG_LOG, "%s", dst->path);
105     dirs_free (&di);
106     dict_close (dict);
107 }
108
109 static void repositoryExtract(ZebraHandle zh,
110                               int deleteFlag, const char *path)
111 {
112     struct stat sbuf;
113     char src[1024];
114     int ret;
115
116     assert (path);
117
118     if (zh->path_reg && !yaz_is_abspath(path))
119     {
120         strcpy (src, zh->path_reg);
121         strcat (src, "/");
122     }
123     else
124         *src = '\0';
125     strcat (src, path);
126     ret = zebra_file_stat (src, &sbuf, zh->m_follow_links);
127
128     strcpy (src, path);
129
130     if (ret == -1)
131         yaz_log (YLOG_WARN|YLOG_ERRNO, "Cannot access path %s", src);
132     else if (S_ISREG(sbuf.st_mode))
133         zebra_extract_file (zh, NULL, src, deleteFlag);
134     else if (S_ISDIR(sbuf.st_mode))
135         repositoryExtractR (zh, deleteFlag, src, 0);
136     else
137         yaz_log (YLOG_WARN, "Skipping path %s", src);
138 }
139
140 static void repositoryExtractG(ZebraHandle zh, const char *path, 
141                                int deleteFlag)
142 {
143     if (!strcmp(path, "") || !strcmp(path, "-"))
144     {
145         char src[1024];
146         
147         while (scanf("%1020s", src) == 1)
148             repositoryExtract(zh, deleteFlag, src);
149     }
150     else
151         repositoryExtract(zh, deleteFlag, path);
152 }
153
154 ZEBRA_RES zebra_update_from_path(ZebraHandle zh, const char *path)
155 {
156     assert (path);
157     repositoryExtractG(zh, path, 0);
158     return ZEBRA_OK;
159 }
160
161 ZEBRA_RES zebra_delete_from_path(ZebraHandle zh, const char *path)
162 {
163     assert (path);
164     repositoryExtractG(zh, path, 1);
165     return ZEBRA_OK;
166 }
167
168 /*
169  * Local variables:
170  * c-basic-offset: 4
171  * indent-tabs-mode: nil
172  * End:
173  * vim: shiftwidth=4 tabstop=8 expandtab
174  */
175