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