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