Fix echo commands for Solaris systems.
[rsync/rsync.git] / flist.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/* generate and receive file lists */
21
22#include "rsync.h"
23
24extern int verbose;
25extern int am_server;
26extern int always_checksum;
27extern off_t total_size;
28
29extern int cvs_exclude;
30
31extern int one_file_system;
32extern int make_backups;
33extern int preserve_links;
34extern int preserve_perms;
35extern int preserve_devices;
36extern int preserve_uid;
37extern int preserve_gid;
38extern int preserve_times;
39
40static char **local_exclude_list = NULL;
41
42static void clean_fname(char *name);
43
44
45/*
46 This function is used to check if a file should be included/excluded
47 from the list of files based on its name and type etc
48 */
49static int match_file_name(char *fname,struct stat *st)
50{
51 if (check_exclude(fname,local_exclude_list)) {
52 if (verbose > 2)
53 fprintf(stderr,"excluding file %s\n",fname);
54 return 0;
55 }
56 return 1;
57}
58
59/* used by the one_file_system code */
60static dev_t filesystem_dev;
61
62static void set_filesystem(char *fname)
63{
64 struct stat st;
65 if (lstat(fname,&st) != 0) return;
66 filesystem_dev = st.st_dev;
67}
68
69
70static void send_directory(int f,struct file_list *flist,char *dir);
71
72static char *flist_dir = NULL;
73
182dca5c
AT
74#define FILE_VALID 1
75#define SAME_MODE (1<<1)
76#define SAME_DEV (1<<2)
77#define SAME_UID (1<<3)
78#define SAME_GID (1<<4)
79#define SAME_DIR (1<<5)
80
c627d613
AT
81static void send_file_entry(struct file_struct *file,int f)
82{
182dca5c
AT
83 unsigned char flags;
84 static mode_t last_mode=0;
85 static dev_t last_dev=0;
86 static uid_t last_uid=0;
87 static gid_t last_gid=0;
88 static char lastdir[MAXPATHLEN]="";
89 char *p=NULL;
90
c627d613
AT
91 if (f == -1) return;
92
182dca5c
AT
93 if (!file) {
94 write_byte(f,0);
95 return;
96 }
97
98 flags = FILE_VALID;
99
100 if (file->mode == last_mode) flags |= SAME_MODE;
101 if (file->dev == last_dev) flags |= SAME_DEV;
102 if (file->uid == last_uid) flags |= SAME_UID;
103 if (file->gid == last_gid) flags |= SAME_GID;
104
105 if (strncmp(file->name,lastdir,strlen(lastdir)) == 0) {
106 flags |= SAME_DIR;
107 p = file->name + strlen(lastdir);
108 } else {
109 p = file->name;
110 }
111
112 write_byte(f,flags);
9f3541e6
AT
113 if (flags & SAME_DIR)
114 write_byte(f,strlen(p));
115 else
116 write_int(f,strlen(p));
182dca5c 117 write_buf(f,p,strlen(p));
c627d613
AT
118 write_int(f,(int)file->modtime);
119 write_int(f,(int)file->length);
182dca5c
AT
120 if (!(flags & SAME_MODE))
121 write_int(f,(int)file->mode);
122 if (preserve_uid && !(flags & SAME_UID))
c627d613 123 write_int(f,(int)file->uid);
182dca5c 124 if (preserve_gid && !(flags & SAME_GID))
c627d613 125 write_int(f,(int)file->gid);
182dca5c 126 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_DEV))
c627d613 127 write_int(f,(int)file->dev);
c627d613
AT
128
129#if SUPPORT_LINKS
130 if (preserve_links && S_ISLNK(file->mode)) {
131 write_int(f,strlen(file->link));
132 write_buf(f,file->link,strlen(file->link));
133 }
134#endif
135
136 if (always_checksum) {
137 write_buf(f,file->sum,SUM_LENGTH);
138 }
182dca5c
AT
139
140 last_mode = file->mode;
141 last_dev = file->dev;
142 last_uid = file->uid;
143 last_gid = file->gid;
144 p = strrchr(file->name,'/');
145 if (p) {
146 int l = (int)(p - file->name) + 1;
147 strncpy(lastdir,file->name,l);
148 lastdir[l] = 0;
149 } else {
150 strcpy(lastdir,"");
151 }
152}
153
154
155
156static void receive_file_entry(struct file_struct *file,
157 unsigned char flags,int f)
158{
159 static mode_t last_mode=0;
160 static dev_t last_dev=0;
161 static uid_t last_uid=0;
162 static gid_t last_gid=0;
163 static char lastdir[MAXPATHLEN]="";
164 char *p=NULL;
165 int l1,l2;
166
9f3541e6
AT
167 if (flags & SAME_DIR) {
168 l1 = read_byte(f);
182dca5c 169 l2 = strlen(lastdir);
9f3541e6
AT
170 } else {
171 l1 = read_int(f);
182dca5c 172 l2 = 0;
9f3541e6 173 }
182dca5c
AT
174
175 file->name = (char *)malloc(l1+l2+1);
176 if (!file->name) out_of_memory("receive_file_entry");
177
178 strncpy(file->name,lastdir,l2);
179 read_buf(f,file->name+l2,l1);
180 file->name[l1+l2] = 0;
181
182 file->modtime = (time_t)read_int(f);
183 file->length = (off_t)read_int(f);
184 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
185 if (preserve_uid)
186 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
187 if (preserve_gid)
188 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
189 if (preserve_devices && IS_DEVICE(file->mode))
190 file->dev = (flags & SAME_DEV) ? last_dev : (dev_t)read_int(f);
191
192#if SUPPORT_LINKS
193 if (preserve_links && S_ISLNK(file->mode)) {
194 int l = read_int(f);
195 file->link = (char *)malloc(l+1);
196 if (!file->link) out_of_memory("receive_file_entry");
197 read_buf(f,file->link,l);
198 file->link[l] = 0;
199 }
200#endif
201
202 if (always_checksum)
203 read_buf(f,file->sum,SUM_LENGTH);
204
205 last_mode = file->mode;
206 last_dev = file->dev;
207 last_uid = file->uid;
208 last_gid = file->gid;
209 p = strrchr(file->name,'/');
210 if (p) {
211 int l = (int)(p - file->name) + 1;
212 strncpy(lastdir,file->name,l);
213 lastdir[l] = 0;
214 } else {
215 strcpy(lastdir,"");
216 }
c627d613
AT
217}
218
219
220static struct file_struct *make_file(int recurse,char *fname)
221{
222 static struct file_struct file;
223 struct stat st;
224 char sum[SUM_LENGTH];
225
226 bzero(sum,SUM_LENGTH);
227
228 if (lstat(fname,&st) != 0) {
229 fprintf(stderr,"%s: %s\n",
230 fname,strerror(errno));
231 return NULL;
232 }
233
234 if (S_ISDIR(st.st_mode) && !recurse) {
235 fprintf(stderr,"skipping directory %s\n",fname);
236 return NULL;
237 }
238
239 if (one_file_system && st.st_dev != filesystem_dev)
240 return NULL;
241
242 if (!match_file_name(fname,&st))
243 return NULL;
244
245 if (verbose > 2)
246 fprintf(stderr,"make_file(%s)\n",fname);
247
248 file.name = strdup(fname);
249 file.modtime = st.st_mtime;
250 file.length = st.st_size;
251 file.mode = st.st_mode;
252 file.uid = st.st_uid;
253 file.gid = st.st_gid;
254#ifdef HAVE_ST_RDEV
255 file.dev = st.st_rdev;
256#endif
257
258#if SUPPORT_LINKS
259 if (S_ISLNK(st.st_mode)) {
260 int l;
261 char lnk[MAXPATHLEN];
262 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
263 fprintf(stderr,"readlink %s : %s\n",fname,strerror(errno));
264 return NULL;
265 }
266 lnk[l] = 0;
267 file.link = strdup(lnk);
268 }
269#endif
270
271 if (always_checksum && S_ISREG(st.st_mode)) {
272 file_checksum(fname,file.sum,st.st_size);
273 }
274
275 if (flist_dir)
276 file.dir = strdup(flist_dir);
277 else
278 file.dir = NULL;
279
280 if (!S_ISDIR(st.st_mode))
281 total_size += st.st_size;
282
283 return &file;
284}
285
286
287
288static void send_file_name(int f,struct file_list *flist,
289 int recurse,char *fname)
290{
291 struct file_struct *file;
292
293 file = make_file(recurse,fname);
294
295 if (!file) return;
296
297 if (flist->count >= flist->malloced) {
298 flist->malloced += 100;
299 flist->files = (struct file_struct *)realloc(flist->files,
300 sizeof(flist->files[0])*
301 flist->malloced);
302 if (!flist->files)
303 out_of_memory("send_file_name");
304 }
305
306 if (strcmp(file->name,".") && strcmp(file->name,"/")) {
307 flist->files[flist->count++] = *file;
308 send_file_entry(file,f);
309 }
310
311 if (S_ISDIR(file->mode) && recurse) {
312 char **last_exclude_list = local_exclude_list;
313 send_directory(f,flist,file->name);
314 local_exclude_list = last_exclude_list;
315 return;
316 }
317}
318
319
320
321static void send_directory(int f,struct file_list *flist,char *dir)
322{
323 DIR *d;
324 struct dirent *di;
325 char fname[MAXPATHLEN];
326 int l;
327 char *p;
328
329 d = opendir(dir);
330 if (!d) {
331 fprintf(stderr,"%s: %s\n",
332 dir,strerror(errno));
333 return;
334 }
335
336 strcpy(fname,dir);
337 l = strlen(fname);
338 if (fname[l-1] != '/')
339 strcat(fname,"/");
340 p = fname + strlen(fname);
341
342 if (cvs_exclude) {
343 strcpy(p,".cvsignore");
344 local_exclude_list = make_exclude_list(fname,NULL,0);
345 }
346
347 for (di=readdir(d); di; di=readdir(d)) {
348 if (strcmp(di->d_name,".")==0 ||
349 strcmp(di->d_name,"..")==0)
350 continue;
351 strcpy(p,di->d_name);
352 send_file_name(f,flist,1,fname);
353 }
354
355 closedir(d);
356}
357
358
359
360struct file_list *send_file_list(int f,int recurse,int argc,char *argv[])
361{
362 int i,l;
363 struct stat st;
364 char *p,*dir;
365 char dbuf[MAXPATHLEN];
366 struct file_list *flist;
367
368 if (verbose && recurse) {
369 fprintf(am_server?stderr:stdout,"building file list ... ");
370 fflush(am_server?stderr:stdout);
371 }
372
373 flist = (struct file_list *)malloc(sizeof(flist[0]));
374 if (!flist) out_of_memory("send_file_list");
375
376 flist->count=0;
377 flist->malloced = 100;
378 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
379 flist->malloced);
380 if (!flist->files) out_of_memory("send_file_list");
381
382 for (i=0;i<argc;i++) {
383 char fname2[MAXPATHLEN];
384 char *fname = fname2;
385
386 strcpy(fname,argv[i]);
387
388 l = strlen(fname);
389 if (l != 1 && fname[l-1] == '/') {
390 strcat(fname,".");
391 }
392
393 if (lstat(fname,&st) != 0) {
394 fprintf(stderr,"%s : %s\n",fname,strerror(errno));
395 continue;
396 }
397
398 if (S_ISDIR(st.st_mode) && !recurse) {
399 fprintf(stderr,"skipping directory %s\n",fname);
400 continue;
401 }
402
403 dir = NULL;
404 p = strrchr(fname,'/');
405 if (p) {
406 *p = 0;
407 dir = fname;
408 fname = p+1;
409 }
410 if (!*fname)
411 fname = ".";
412
413 if (dir && *dir) {
414 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
415 fprintf(stderr,"getwd : %s\n",strerror(errno));
416 exit(1);
417 }
418 if (chdir(dir) != 0) {
419 fprintf(stderr,"chdir %s : %s\n",dir,strerror(errno));
420 continue;
421 }
422 flist_dir = dir;
423 if (one_file_system)
424 set_filesystem(fname);
425 send_file_name(f,flist,recurse,fname);
426 flist_dir = NULL;
427 if (chdir(dbuf) != 0) {
428 fprintf(stderr,"chdir %s : %s\n",dbuf,strerror(errno));
429 exit(1);
430 }
431 continue;
432 }
433
434 if (one_file_system)
435 set_filesystem(fname);
436 send_file_name(f,flist,recurse,fname);
437 }
438
439 if (f != -1) {
182dca5c 440 send_file_entry(NULL,f);
c627d613
AT
441 write_flush(f);
442 }
443
444 clean_flist(flist);
445
446 if (verbose && recurse)
447 fprintf(am_server?stderr:stdout,"done\n");
448
449 return flist;
450}
451
452
453struct file_list *recv_file_list(int f)
454{
c627d613 455 struct file_list *flist;
182dca5c 456 unsigned char flags;
c627d613
AT
457
458 if (verbose > 2)
459 fprintf(stderr,"recv_file_list starting\n");
460
461 flist = (struct file_list *)malloc(sizeof(flist[0]));
462 if (!flist)
463 goto oom;
464
465 flist->count=0;
466 flist->malloced=100;
467 flist->files = (struct file_struct *)malloc(sizeof(flist->files[0])*
468 flist->malloced);
469 if (!flist->files)
470 goto oom;
471
472
182dca5c 473 for (flags=read_byte(f); flags; flags=read_byte(f)) {
c627d613
AT
474 int i = flist->count;
475
476 if (i >= flist->malloced) {
477 flist->malloced += 100;
478 flist->files =(struct file_struct *)realloc(flist->files,
479 sizeof(flist->files[0])*
480 flist->malloced);
481 if (!flist->files)
482 goto oom;
483 }
484
182dca5c 485 receive_file_entry(&flist->files[i],flags,f);
c627d613
AT
486
487 if (S_ISREG(flist->files[i].mode))
488 total_size += flist->files[i].length;
489
490 flist->count++;
491
492 if (verbose > 2)
493 fprintf(stderr,"recv_file_name(%s)\n",flist->files[i].name);
494 }
495
496
497 if (verbose > 2)
498 fprintf(stderr,"received %d names\n",flist->count);
499
500 clean_flist(flist);
501
502 return flist;
503
504oom:
505 out_of_memory("recv_file_list");
506 return NULL; /* not reached */
507}
508
509
510static int flist_compare(struct file_struct *f1,struct file_struct *f2)
511{
512 if (!f1->name && !f2->name) return 0;
513 if (!f1->name) return -1;
514 if (!f2->name) return 1;
515 return strcmp(f1->name,f2->name);
516}
517
518
519int flist_find(struct file_list *flist,struct file_struct *f)
520{
521 int low=0,high=flist->count;
522
523 while (low != high) {
524 int mid = (low+high)/2;
525 int ret = flist_compare(&flist->files[mid],f);
526 if (ret == 0) return mid;
527 if (ret > 0)
528 high=mid;
529 else
530 low=mid+1;
531 }
532 if (flist_compare(&flist->files[low],f) == 0)
533 return low;
534 return -1;
535}
536
537
538static void clean_fname(char *name)
539{
540 char *p;
541 int l;
542 int modified = 1;
543
544 if (!name) return;
545
546 while (modified) {
547 modified = 0;
548
549 if ((p=strstr(name,"/./"))) {
550 modified = 1;
551 while (*p) {
552 p[0] = p[2];
553 p++;
554 }
555 }
556
557 if ((p=strstr(name,"//"))) {
558 modified = 1;
559 while (*p) {
560 p[0] = p[1];
561 p++;
562 }
563 }
564
565 if (strncmp(p=name,"./",2) == 0) {
566 modified = 1;
567 while (*p) {
568 p[0] = p[2];
569 p++;
570 }
571 }
572
573 l = strlen(p=name);
574 if (l > 1 && p[l-1] == '/') {
575 modified = 1;
576 p[l-1] = 0;
577 }
578 }
579}
580
581
582/*
583 * This routine ensures we don't have any duplicate names in our file list.
584 * duplicate names can cause corruption because of the pipelining
585 */
586void clean_flist(struct file_list *flist)
587{
588 int i;
589
590 if (!flist || flist->count == 0)
591 return;
592
593 for (i=0;i<flist->count;i++) {
594 clean_fname(flist->files[i].name);
595 }
596
597 qsort(flist->files,flist->count,
598 sizeof(flist->files[0]),
599 (int (*)())flist_compare);
600
601 for (i=1;i<flist->count;i++) {
602 if (flist->files[i].name &&
603 strcmp(flist->files[i].name,flist->files[i-1].name) == 0) {
604 if (verbose > 1 && !am_server)
605 fprintf(stderr,"removing duplicate name %s from file list\n",
606 flist->files[i].name);
607 free(flist->files[i-1].name);
608 flist->files[i-1].name = NULL;
609 }
610 }
611}
612