xfree/xmalloc used everywhere.
[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.4  1995-09-28 09:19:46  adam
8  * xfree/xmalloc used everywhere.
9  * Extract/retrieve method seems to work for text records.
10  *
11  * Revision 1.3  1995/09/06  16:11:18  adam
12  * Option: only one word key per file.
13  *
14  * Revision 1.2  1995/09/04  12:33:43  adam
15  * Various cleanup. YAZ util used instead.
16  *
17  * Revision 1.1  1995/09/01  14:06:36  adam
18  * Split of work into more files.
19  *
20  */
21 #include <stdio.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #include <ctype.h>
29
30 #include <alexutil.h>
31 #include "index.h"
32
33 static void repository_extract_r (int cmd, char *rep)
34 {
35     struct dir_entry *e;
36     int i;
37     struct stat fs;
38     size_t rep_len = strlen (rep);
39
40     e = dir_open (rep);
41     if (!e)
42         return;
43     if (rep[rep_len-1] != '/')
44         rep[rep_len] = '/';
45     else
46         --rep_len;
47     for (i=0; e[i].name; i++)
48     {
49         strcpy (rep +rep_len+1, e[i].name);
50         stat (rep, &fs);
51         switch (fs.st_mode & S_IFMT)
52         {
53         case S_IFREG:
54             file_extract (cmd, rep, rep);
55             break;
56         case S_IFDIR:
57             repository_extract_r (cmd, rep);
58             break;
59         }
60     }
61     dir_free (&e);
62 }
63
64 void copy_file (const char *dst, const char *src)
65 {
66     int d_fd = open (dst, O_WRONLY|O_CREAT, 0666);
67     int s_fd = open (src, O_RDONLY);
68     char *buf;
69     size_t i, r, w;
70
71     if (d_fd == -1)
72     {
73         logf (LOG_FATAL|LOG_ERRNO, "Cannot create %s", dst);
74         exit (1);
75     }
76     if (s_fd == -1)
77     {
78         logf (LOG_FATAL|LOG_ERRNO, "Cannot open %s", src);
79         exit (1);
80     }
81     buf = xmalloc (4096);
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     xfree (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