Option: only one word key per file.
[idzebra-moved-to-github.git] / index / trav.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: trav.c,v $
7  * Revision 1.3  1995-09-06 16:11:18  adam
8  * Option: only one word key per file.
9  *
10  * Revision 1.2  1995/09/04  12:33:43  adam
11  * Various cleanup. YAZ util used instead.
12  *
13  * Revision 1.1  1995/09/01  14:06:36  adam
14  * Split of work into more files.
15  *
16  */
17 #include <stdio.h>
18 #include <assert.h>
19 #include <unistd.h>
20 #include <dirent.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25
26 #include <alexutil.h>
27 #include "index.h"
28
29 static void repository_extract_r (int cmd, char *rep)
30 {
31     struct dir_entry *e;
32     int i;
33     struct stat fs;
34     size_t rep_len = strlen (rep);
35
36     e = dir_open (rep);
37     if (!e)
38         return;
39     if (rep[rep_len-1] != '/')
40         rep[rep_len] = '/';
41     else
42         --rep_len;
43     for (i=0; e[i].name; i++)
44     {
45         strcpy (rep +rep_len+1, e[i].name);
46         stat (rep, &fs);
47         switch (fs.st_mode & S_IFMT)
48         {
49         case S_IFREG:
50             file_extract (cmd, rep, rep);
51             break;
52         case S_IFDIR:
53             repository_extract_r (cmd, rep);
54             break;
55         }
56     }
57     dir_free (&e);
58 }
59
60 void copy_file (const char *dst, const char *src)
61 {
62     int d_fd = open (dst, O_WRONLY|O_CREAT, 0666);
63     int s_fd = open (src, O_RDONLY);
64     char *buf;
65     size_t i, r, w;
66
67     if (d_fd == -1)
68     {
69         logf (LOG_FATAL|LOG_ERRNO, "Cannot create %s", dst);
70         exit (1);
71     }
72     if (s_fd == -1)
73     {
74         logf (LOG_FATAL|LOG_ERRNO, "Cannot open %s", src);
75         exit (1);
76     }
77     if (!(buf = malloc (4096)))
78     {
79         logf (LOG_FATAL|LOG_ERRNO, "malloc");
80         exit (1);
81     }
82     while ((r=read (s_fd, buf, 4096))>0)
83         for (w = 0; w < r; w += i)
84         {
85             i = write (d_fd, buf + w, r - w);
86             if (i == -1)
87             {
88                 logf (LOG_FATAL|LOG_ERRNO, "write");
89                 exit (1);
90             }
91         }
92     if (r)
93     {
94         logf (LOG_FATAL|LOG_ERRNO, "read");
95         exit (1);
96     }
97     free (buf);
98     close (d_fd);
99     close (s_fd);
100 }
101
102 void del_file (const char *dst)
103 {
104     unlink (dst);
105 }
106
107 void del_dir (const char *dst)
108 {
109     logf (LOG_DEBUG, "rmdir of %s", dst);
110     if (rmdir (dst) == -1)
111         logf (LOG_ERRNO|LOG_WARN, "rmdir");
112 }
113
114 void repository_update_r (int cmd, char *dst, char *src);
115
116 void repository_add_tree (int cmd, char *dst, char *src)
117 {
118     mkdir (dst, 0755);
119     repository_update_r (cmd, dst, src);
120 }
121
122 void repository_del_tree (int cmd, char *dst, char *src)
123 {
124     size_t dst_len = strlen (dst);
125     size_t src_len = strlen (src);
126     struct dir_entry *e_dst;
127     int i_dst = 0;
128     struct stat fs_dst;
129
130     e_dst = dir_open (dst);
131
132     dir_sort (e_dst);
133
134     if (src[src_len-1] != '/')
135         src[src_len] = '/';
136     else
137         --src_len;
138     if (dst[dst_len-1] != '/')
139         dst[dst_len] = '/';
140     else
141         --dst_len;
142     while (e_dst[i_dst].name)
143     {
144         strcpy (dst +dst_len+1, e_dst[i_dst].name);
145         strcpy (src +src_len+1, e_dst[i_dst].name);
146         
147         stat (dst, &fs_dst);
148         switch (fs_dst.st_mode & S_IFMT)
149         {
150         case S_IFREG:
151             file_extract ('d', dst, dst);
152             del_file (dst);
153             break;
154         case S_IFDIR:
155             repository_del_tree (cmd, dst, src);
156             break;
157         }
158         i_dst++;
159     }
160     dir_free (&e_dst);
161     if (dst_len > 0)
162     {
163         dst[dst_len] = '\0';
164         del_dir (dst);
165     }
166 }
167
168 void repository_update_r (int cmd, char *dst, char *src)
169 {
170     struct dir_entry *e_dst, *e_src;
171     int i_dst = 0, i_src = 0;
172     struct stat fs_dst, fs_src;
173     size_t dst_len = strlen (dst);
174     size_t src_len = strlen (src);
175
176     e_dst = dir_open (dst);
177     e_src = dir_open (src);
178
179     if (!e_dst && !e_src)
180         return;
181     if (!e_dst)
182     {
183         dir_free (&e_src);
184         repository_add_tree (cmd, dst, src);
185         return;
186     }
187     else if (!e_src)
188     {
189         dir_free (&e_dst);
190         repository_del_tree (cmd, dst, src);
191         return;
192     }
193
194     dir_sort (e_src);
195     dir_sort (e_dst);
196
197     if (src[src_len-1] != '/')
198         src[src_len] = '/';
199     else
200         --src_len;
201     if (dst[dst_len-1] != '/')
202         dst[dst_len] = '/';
203     else
204         --dst_len;
205     while (e_dst[i_dst].name || e_src[i_src].name)
206     {
207         int sd;
208
209         if (e_dst[i_dst].name && e_src[i_src].name)
210             sd = strcmp (e_dst[i_dst].name, e_src[i_src].name);
211         else if (e_src[i_src].name)
212             sd = 1;
213         else
214             sd = -1;
215                 
216         if (sd == 0)
217         {
218             strcpy (dst +dst_len+1, e_dst[i_dst].name);
219             strcpy (src +src_len+1, e_src[i_src].name);
220             
221             /* check type, date, length */
222
223             stat (dst, &fs_dst);
224             stat (src, &fs_src);
225                 
226             switch (fs_dst.st_mode & S_IFMT)
227             {
228             case S_IFREG:
229                 if (fs_src.st_ctime > fs_dst.st_ctime)
230                 {
231                     file_extract ('d', dst, dst);
232                     file_extract ('a', src, dst);
233                     copy_file (dst, src);
234                 }
235                 break;
236             case S_IFDIR:
237                 repository_update_r (cmd, dst, src);
238                 break;
239             }
240             i_src++;
241             i_dst++;
242         }
243         else if (sd > 0)
244         {
245             strcpy (dst +dst_len+1, e_src[i_src].name);
246             strcpy (src +src_len+1, e_src[i_src].name);
247             
248             stat (src, &fs_src);
249             switch (fs_src.st_mode & S_IFMT)
250             {
251             case S_IFREG:
252                 file_extract ('a', src, dst);
253                 copy_file (dst, src);
254                 break;
255             case S_IFDIR:
256                 repository_add_tree (cmd, dst, src);
257                 break;
258             }
259             i_src++;
260         }
261         else 
262         {
263             strcpy (dst +dst_len+1, e_dst[i_dst].name);
264             strcpy (src +src_len+1, e_dst[i_dst].name);
265             
266             stat (dst, &fs_dst);
267             switch (fs_dst.st_mode & S_IFMT)
268             {
269             case S_IFREG:
270                 file_extract ('d', dst, dst);
271                 del_file (dst);
272                 break;
273             case S_IFDIR:
274                 repository_del_tree (cmd, dst, src);
275                 break;
276             }
277             i_dst++;
278         }
279     }
280     dir_free (&e_dst);
281     dir_free (&e_src);
282 }
283
284 void repository (int cmd, const char *rep, const char *base_path)
285 {
286     char rep_tmp1[2048];
287     char rep_tmp2[2048];
288
289     strcpy (rep_tmp1, rep);
290     if (base_path)
291     {
292         strcpy (rep_tmp2, base_path);
293         repository_update_r (cmd, rep_tmp2, rep_tmp1);
294     }
295     else
296         repository_extract_r (cmd, rep_tmp1);
297 }
298