Fix for device nodes. (dann frazier) (Debian #129135)
[rsync/rsync.git] / flist.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
736a6a29 4 Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
c627d613
AT
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
736a6a29
MP
21/** @file flist.c
22 * Generate and receive file lists
23 *
24 * @todo Get rid of the string_area optimization. Efficiently
25 * allocating blocks is the responsibility of the system's malloc
26 * library, not of rsync.
27 *
172875cf
MP
28 * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html
29 *
736a6a29 30 **/
c627d613
AT
31
32#include "rsync.h"
33
a800434a
AT
34extern struct stats stats;
35
c627d613
AT
36extern int verbose;
37extern int am_server;
38extern int always_checksum;
c627d613
AT
39
40extern int cvs_exclude;
41
a06d19e3
AT
42extern int recurse;
43
c627d613
AT
44extern int one_file_system;
45extern int make_backups;
46extern int preserve_links;
dc5ddbcc 47extern int preserve_hard_links;
c627d613
AT
48extern int preserve_perms;
49extern int preserve_devices;
50extern int preserve_uid;
51extern int preserve_gid;
52extern int preserve_times;
6574b4f7 53extern int relative_paths;
82306bf6 54extern int copy_links;
b5313607 55extern int copy_unsafe_links;
f6c34742 56extern int remote_version;
6ba9279f 57extern int io_error;
cb13abfe 58extern int sanitize_paths;
c627d613 59
6902ed17
MP
60extern int read_batch;
61extern int write_batch;
62
b5313607
DD
63static char topsrcname[MAXPATHLEN];
64
2b6b4d53 65static struct exclude_struct **local_exclude_list;
c627d613 66
3d382777
AT
67static struct file_struct null_file;
68
0199b05f
AT
69static void clean_flist(struct file_list *flist, int strip_root);
70
58225000
MP
71
72static int show_build_progress_p(void)
73{
74 extern int do_progress;
ebed4c3a 75
58225000
MP
76 return do_progress && verbose && recurse && !am_server;
77}
78
79/**
80 * True if we're local, etc, and should emit progress emssages.
81 **/
82static void emit_build_progress(const struct file_list *flist)
83{
ebed4c3a 84 rprintf(FINFO, " %d files...\r", flist->count);
58225000
MP
85}
86
87
88static void finish_build_progress(const struct file_list *flist)
89{
90 if (verbose && recurse && !am_server) {
91 /* This overwrites the progress line, if any. */
92 rprintf(FINFO, RSYNC_NAME ": %d files to consider.\n",
93 flist->count);
94 }
95}
96
97
86943126
MP
98void show_flist_stats(void)
99{
100 /* Nothing yet */
101}
102
103
5c66303a 104static struct string_area *string_area_new(int size)
3d382777
AT
105{
106 struct string_area *a;
107
ebed4c3a
MP
108 if (size <= 0)
109 size = ARENA_SIZE;
3d382777 110 a = malloc(sizeof(*a));
ebed4c3a
MP
111 if (!a)
112 out_of_memory("string_area_new");
3d382777 113 a->current = a->base = malloc(size);
ebed4c3a
MP
114 if (!a->current)
115 out_of_memory("string_area_new buffer");
3d382777 116 a->end = a->base + size;
5c66303a 117 a->next = NULL;
3d382777
AT
118
119 return a;
120}
121
5c66303a 122static void string_area_free(struct string_area *a)
3d382777
AT
123{
124 struct string_area *next;
125
ebed4c3a 126 for (; a; a = next) {
3d382777
AT
127 next = a->next;
128 free(a->base);
129 }
130}
131
5c66303a 132static char *string_area_malloc(struct string_area **ap, int size)
3d382777
AT
133{
134 char *p;
135 struct string_area *a;
136
137 /* does the request fit into the current space? */
138 a = *ap;
139 if (a->current + size >= a->end) {
140 /* no; get space, move new string_area to front of the list */
141 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
142 a->next = *ap;
143 *ap = a;
144 }
145
146 /* have space; do the "allocation." */
147 p = a->current;
148 a->current += size;
149 return p;
150}
151
5c66303a 152static char *string_area_strdup(struct string_area **ap, const char *src)
3d382777 153{
ebed4c3a 154 char *dest = string_area_malloc(ap, strlen(src) + 1);
3d382777
AT
155 return strcpy(dest, src);
156}
f7632fc6
AT
157
158static void list_file_entry(struct file_struct *f)
159{
740819ef 160 char perms[11];
f7632fc6 161
7212be92
DD
162 if (!f->basename)
163 /* this can happen if duplicate names were removed */
164 return;
165
740819ef
MP
166 permstring(perms, f->mode);
167
f7632fc6 168 if (preserve_links && S_ISLNK(f->mode)) {
ebed4c3a
MP
169 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
170 perms,
171 (double) f->length, timestring(f->modtime),
f7632fc6
AT
172 f_name(f), f->link);
173 } else {
ebed4c3a
MP
174 rprintf(FINFO, "%s %11.0f %s %s\n",
175 perms,
176 (double) f->length, timestring(f->modtime),
177 f_name(f));
f7632fc6
AT
178 }
179}
180
181
ebed4c3a 182int readlink_stat(const char *Path, STRUCT_STAT * Buffer, char *Linkbuf)
b5313607
DD
183{
184#if SUPPORT_LINKS
185 if (copy_links) {
186 return do_stat(Path, Buffer);
187 }
188 if (do_lstat(Path, Buffer) == -1) {
189 return -1;
190 }
191 if (S_ISLNK(Buffer->st_mode)) {
ab94af5c 192 int l;
ebed4c3a
MP
193 if ((l =
194 readlink((char *) Path, Linkbuf,
195 MAXPATHLEN - 1)) == -1) {
b5313607
DD
196 return -1;
197 }
198 Linkbuf[l] = 0;
199 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
ebed4c3a 200 unsafe_symlink(Linkbuf, topsrcname)) {
b5313607
DD
201 return do_stat(Path, Buffer);
202 }
203 }
204 return 0;
205#else
206 return do_stat(Path, Buffer);
207#endif
208}
209
ebed4c3a 210int link_stat(const char *Path, STRUCT_STAT * Buffer)
82306bf6
AT
211{
212#if SUPPORT_LINKS
ebed4c3a
MP
213 if (copy_links) {
214 return do_stat(Path, Buffer);
215 } else {
216 return do_lstat(Path, Buffer);
217 }
82306bf6 218#else
ebed4c3a 219 return do_stat(Path, Buffer);
82306bf6
AT
220#endif
221}
222
c627d613
AT
223/*
224 This function is used to check if a file should be included/excluded
225 from the list of files based on its name and type etc
226 */
ebed4c3a 227static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
c627d613 228{
76e26e10
DD
229 extern int delete_excluded;
230
231 /* f is set to -1 when calculating deletion file list */
232 if ((f == -1) && delete_excluded) {
233 return 0;
234 }
ebed4c3a 235 if (check_exclude(fname, local_exclude_list, st)) {
76e26e10
DD
236 return 1;
237 }
238 return 0;
c627d613
AT
239}
240
241/* used by the one_file_system code */
242static dev_t filesystem_dev;
243
244static void set_filesystem(char *fname)
245{
ebed4c3a
MP
246 STRUCT_STAT st;
247 if (link_stat(fname, &st) != 0)
248 return;
249 filesystem_dev = st.st_dev;
c627d613
AT
250}
251
252
b280a1f4
AT
253static int to_wire_mode(mode_t mode)
254{
efe3037c 255 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
b280a1f4
AT
256 return (mode & ~(_S_IFMT)) | 0120000;
257 }
ebed4c3a 258 return (int) mode;
b280a1f4
AT
259}
260
261static mode_t from_wire_mode(int mode)
262{
efe3037c
AT
263 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
264 return (mode & ~(_S_IFMT)) | _S_IFLNK;
b280a1f4 265 }
ebed4c3a 266 return (mode_t) mode;
b280a1f4
AT
267}
268
269
ebed4c3a 270static void send_directory(int f, struct file_list *flist, char *dir);
c627d613 271
3a6a366f 272static char *flist_dir;
c627d613 273
3ec4dd97 274
d9d6bc52
MP
275/**
276 * Make sure @p flist is big enough to hold at least @p flist->count
277 * entries.
278 **/
279static void flist_expand(struct file_list *flist)
280{
281 if (flist->count >= flist->malloced) {
2e7d1994
MP
282 size_t new_bytes;
283 void *new_ptr;
284
d9d6bc52
MP
285 if (flist->malloced < 1000)
286 flist->malloced += 1000;
287 else
288 flist->malloced *= 2;
2e7d1994
MP
289
290 new_bytes = sizeof(flist->files[0]) * flist->malloced;
291
292 new_ptr = realloc(flist->files, new_bytes);
293
1d5a1da9 294 if (verbose >= 2) {
2e7d1994
MP
295 rprintf(FINFO, RSYNC_NAME ": expand file_list to %.0f bytes, did%s move\n",
296 (double) new_bytes,
297 (new_ptr == flist->files) ? " not" : "");
298 }
299
300 flist->files = (struct file_struct **) new_ptr;
301
d9d6bc52
MP
302 if (!flist->files)
303 out_of_memory("flist_expand");
304 }
305}
306
307
ebed4c3a
MP
308static void send_file_entry(struct file_struct *file, int f,
309 unsigned base_flags)
c627d613 310{
72914a60
AT
311 unsigned char flags;
312 static time_t last_time;
313 static mode_t last_mode;
2119a4c4 314 static DEV64_T last_rdev;
72914a60
AT
315 static uid_t last_uid;
316 static gid_t last_gid;
317 static char lastname[MAXPATHLEN];
318 char *fname;
ebed4c3a 319 int l1, l2;
72914a60 320
ebed4c3a
MP
321 if (f == -1)
322 return;
72914a60
AT
323
324 if (!file) {
ebed4c3a 325 write_byte(f, 0);
72914a60
AT
326 return;
327 }
328
329 fname = f_name(file);
330
331 flags = base_flags;
332
ebed4c3a
MP
333 if (file->mode == last_mode)
334 flags |= SAME_MODE;
335 if (file->rdev == last_rdev)
336 flags |= SAME_RDEV;
337 if (file->uid == last_uid)
338 flags |= SAME_UID;
339 if (file->gid == last_gid)
340 flags |= SAME_GID;
341 if (file->modtime == last_time)
342 flags |= SAME_TIME;
343
344 for (l1 = 0;
345 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
346 l1++);
72914a60
AT
347 l2 = strlen(fname) - l1;
348
ebed4c3a
MP
349 if (l1 > 0)
350 flags |= SAME_NAME;
351 if (l2 > 255)
352 flags |= LONG_NAME;
72914a60
AT
353
354 /* we must make sure we don't send a zero flags byte or the other
355 end will terminate the flist transfer */
ebed4c3a
MP
356 if (flags == 0 && !S_ISDIR(file->mode))
357 flags |= FLAG_DELETE;
358 if (flags == 0)
359 flags |= LONG_NAME;
72914a60 360
ebed4c3a 361 write_byte(f, flags);
72914a60 362 if (flags & SAME_NAME)
ebed4c3a 363 write_byte(f, l1);
72914a60 364 if (flags & LONG_NAME)
ebed4c3a 365 write_int(f, l2);
72914a60 366 else
ebed4c3a
MP
367 write_byte(f, l2);
368 write_buf(f, fname + l1, l2);
72914a60 369
ebed4c3a 370 write_longint(f, file->length);
72914a60 371 if (!(flags & SAME_TIME))
ebed4c3a 372 write_int(f, (int) file->modtime);
72914a60 373 if (!(flags & SAME_MODE))
ebed4c3a 374 write_int(f, to_wire_mode(file->mode));
72914a60
AT
375 if (preserve_uid && !(flags & SAME_UID)) {
376 add_uid(file->uid);
ebed4c3a 377 write_int(f, (int) file->uid);
72914a60
AT
378 }
379 if (preserve_gid && !(flags & SAME_GID)) {
380 add_gid(file->gid);
ebed4c3a 381 write_int(f, (int) file->gid);
72914a60 382 }
ebed4c3a
MP
383 if (preserve_devices && IS_DEVICE(file->mode)
384 && !(flags & SAME_RDEV))
385 write_int(f, (int) file->rdev);
c627d613
AT
386
387#if SUPPORT_LINKS
72914a60 388 if (preserve_links && S_ISLNK(file->mode)) {
ebed4c3a
MP
389 write_int(f, strlen(file->link));
390 write_buf(f, file->link, strlen(file->link));
72914a60 391 }
c627d613
AT
392#endif
393
dc5ddbcc 394#if SUPPORT_HARD_LINKS
72914a60 395 if (preserve_hard_links && S_ISREG(file->mode)) {
736a6a29
MP
396 if (remote_version < 26) {
397 /* 32-bit dev_t and ino_t */
ebed4c3a
MP
398 write_int(f, (int) file->dev);
399 write_int(f, (int) file->inode);
736a6a29
MP
400 } else {
401 /* 64-bit dev_t and ino_t */
6abd193f
MP
402 write_longint(f, file->dev);
403 write_longint(f, file->inode);
736a6a29 404 }
72914a60 405 }
dc5ddbcc
AT
406#endif
407
72914a60 408 if (always_checksum) {
f855a7d0 409 if (remote_version < 21) {
ebed4c3a 410 write_buf(f, file->sum, 2);
f855a7d0 411 } else {
ebed4c3a 412 write_buf(f, file->sum, MD4_SUM_LENGTH);
f855a7d0 413 }
ebed4c3a 414 }
182dca5c 415
72914a60
AT
416 last_mode = file->mode;
417 last_rdev = file->rdev;
418 last_uid = file->uid;
419 last_gid = file->gid;
420 last_time = file->modtime;
4fe159a8 421
ebed4c3a
MP
422 strlcpy(lastname, fname, MAXPATHLEN);
423 lastname[MAXPATHLEN - 1] = 0;
182dca5c
AT
424}
425
426
427
3333ffbd 428static void receive_file_entry(struct file_struct **fptr,
ebed4c3a 429 unsigned flags, int f)
182dca5c 430{
72914a60
AT
431 static time_t last_time;
432 static mode_t last_mode;
2119a4c4 433 static DEV64_T last_rdev;
72914a60
AT
434 static uid_t last_uid;
435 static gid_t last_gid;
436 static char lastname[MAXPATHLEN];
437 char thisname[MAXPATHLEN];
ebed4c3a 438 unsigned int l1 = 0, l2 = 0;
72914a60
AT
439 char *p;
440 struct file_struct *file;
441
442 if (flags & SAME_NAME)
443 l1 = read_byte(f);
ebed4c3a 444
72914a60
AT
445 if (flags & LONG_NAME)
446 l2 = read_int(f);
447 else
448 l2 = read_byte(f);
449
ebed4c3a
MP
450 file = (struct file_struct *) malloc(sizeof(*file));
451 if (!file)
452 out_of_memory("receive_file_entry");
453 memset((char *) file, 0, sizeof(*file));
72914a60
AT
454 (*fptr) = file;
455
ebed4c3a
MP
456 if (l2 >= MAXPATHLEN - l1) {
457 rprintf(FERROR,
458 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
d0fd26aa
AT
459 flags, l1, l2, lastname);
460 overflow("receive_file_entry");
461 }
72914a60 462
ebed4c3a
MP
463 strlcpy(thisname, lastname, l1 + 1);
464 read_sbuf(f, &thisname[l1], l2);
465 thisname[l1 + l2] = 0;
72914a60 466
ebed4c3a
MP
467 strlcpy(lastname, thisname, MAXPATHLEN);
468 lastname[MAXPATHLEN - 1] = 0;
72914a60
AT
469
470 clean_fname(thisname);
471
cb13abfe
DD
472 if (sanitize_paths) {
473 sanitize_path(thisname, NULL);
474 }
475
ebed4c3a 476 if ((p = strrchr(thisname, '/'))) {
72914a60
AT
477 static char *lastdir;
478 *p = 0;
ebed4c3a 479 if (lastdir && strcmp(thisname, lastdir) == 0) {
72914a60
AT
480 file->dirname = lastdir;
481 } else {
482 file->dirname = strdup(thisname);
483 lastdir = file->dirname;
484 }
ebed4c3a 485 file->basename = strdup(p + 1);
72914a60
AT
486 } else {
487 file->dirname = NULL;
488 file->basename = strdup(thisname);
489 }
490
ebed4c3a
MP
491 if (!file->basename)
492 out_of_memory("receive_file_entry 1");
72914a60
AT
493
494
495 file->flags = flags;
496 file->length = read_longint(f);
ebed4c3a
MP
497 file->modtime =
498 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
499 file->mode =
500 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
72914a60 501 if (preserve_uid)
ebed4c3a
MP
502 file->uid =
503 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
72914a60 504 if (preserve_gid)
ebed4c3a
MP
505 file->gid =
506 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
72914a60 507 if (preserve_devices && IS_DEVICE(file->mode))
ebed4c3a
MP
508 file->rdev =
509 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
72914a60
AT
510
511 if (preserve_links && S_ISLNK(file->mode)) {
512 int l = read_int(f);
9dd891bb 513 if (l < 0) {
ebed4c3a 514 rprintf(FERROR, "overflow: l=%d\n", l);
9dd891bb
MP
515 overflow("receive_file_entry");
516 }
ebed4c3a
MP
517 file->link = (char *) malloc(l + 1);
518 if (!file->link)
519 out_of_memory("receive_file_entry 2");
520 read_sbuf(f, file->link, l);
cb13abfe
DD
521 if (sanitize_paths) {
522 sanitize_path(file->link, file->dirname);
523 }
72914a60 524 }
dc5ddbcc 525#if SUPPORT_HARD_LINKS
72914a60 526 if (preserve_hard_links && S_ISREG(file->mode)) {
736a6a29
MP
527 if (remote_version < 26) {
528 file->dev = read_int(f);
529 file->inode = read_int(f);
530 } else {
531 file->dev = read_longint(f);
532 file->inode = read_longint(f);
533 }
72914a60 534 }
dc5ddbcc 535#endif
ebed4c3a 536
72914a60 537 if (always_checksum) {
ebed4c3a
MP
538 file->sum = (char *) malloc(MD4_SUM_LENGTH);
539 if (!file->sum)
540 out_of_memory("md4 sum");
f855a7d0 541 if (remote_version < 21) {
ebed4c3a 542 read_buf(f, file->sum, 2);
f855a7d0 543 } else {
ebed4c3a 544 read_buf(f, file->sum, MD4_SUM_LENGTH);
f855a7d0 545 }
72914a60 546 }
ebed4c3a 547
72914a60
AT
548 last_mode = file->mode;
549 last_rdev = file->rdev;
550 last_uid = file->uid;
551 last_gid = file->gid;
552 last_time = file->modtime;
553
554 if (!preserve_perms) {
555 extern int orig_umask;
556 /* set an appropriate set of permissions based on original
557 permissions and umask. This emulates what GNU cp does */
558 file->mode &= ~orig_umask;
559 }
c627d613
AT
560}
561
562
0c5f37d9
AT
563/* determine if a file in a different filesstem should be skipped
564 when one_file_system is set. We bascally only want to include
565 the mount points - but they can be hard to find! */
ebed4c3a 566static int skip_filesystem(char *fname, STRUCT_STAT * st)
0c5f37d9 567{
bcacc18b 568 STRUCT_STAT st2;
0c5f37d9
AT
569 char *p = strrchr(fname, '/');
570
571 /* skip all but directories */
ebed4c3a
MP
572 if (!S_ISDIR(st->st_mode))
573 return 1;
0c5f37d9
AT
574
575 /* if its not a subdirectory then allow */
ebed4c3a
MP
576 if (!p)
577 return 0;
0c5f37d9
AT
578
579 *p = 0;
580 if (link_stat(fname, &st2)) {
581 *p = '/';
582 return 0;
583 }
584 *p = '/';
ebed4c3a 585
0c5f37d9
AT
586 return (st2.st_dev != filesystem_dev);
587}
4fe159a8 588
3d382777 589#define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
87a819ed
MP
590/* IRIX cc cares that the operands to the ternary have the same type. */
591#define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
3d382777 592
66203a98 593/* create a file_struct for a named file */
ac1a0994
AT
594struct file_struct *make_file(int f, char *fname, struct string_area **ap,
595 int noexcludes)
c627d613 596{
3ec4dd97 597 struct file_struct *file;
bcacc18b 598 STRUCT_STAT st;
3ec4dd97
AT
599 char sum[SUM_LENGTH];
600 char *p;
601 char cleaned_name[MAXPATHLEN];
b5313607 602 char linkbuf[MAXPATHLEN];
ac1a0994 603 extern int module_id;
3ec4dd97 604
37f9805d 605 strlcpy(cleaned_name, fname, MAXPATHLEN);
ebed4c3a 606 cleaned_name[MAXPATHLEN - 1] = 0;
3ec4dd97 607 clean_fname(cleaned_name);
cb13abfe
DD
608 if (sanitize_paths) {
609 sanitize_path(cleaned_name, NULL);
610 }
3ec4dd97
AT
611 fname = cleaned_name;
612
ebed4c3a 613 memset(sum, 0, SUM_LENGTH);
3ec4dd97 614
ebed4c3a 615 if (readlink_stat(fname, &st, linkbuf) != 0) {
76e26e10
DD
616 int save_errno = errno;
617 if ((errno == ENOENT) && copy_links && !noexcludes) {
618 /* symlink pointing nowhere, see if excluded */
ebed4c3a
MP
619 memset((char *) &st, 0, sizeof(st));
620 if (check_exclude_file(f, fname, &st)) {
76e26e10
DD
621 /* file is excluded anyway, ignore silently */
622 return NULL;
623 }
624 }
6ba9279f 625 io_error = 1;
ebed4c3a
MP
626 rprintf(FERROR, "readlink %s: %s\n",
627 fname, strerror(save_errno));
3ec4dd97
AT
628 return NULL;
629 }
c627d613 630
ac1a0994 631 /* we use noexcludes from backup.c */
ebed4c3a
MP
632 if (noexcludes)
633 goto skip_excludes;
ac1a0994 634
3ec4dd97 635 if (S_ISDIR(st.st_mode) && !recurse) {
ebed4c3a 636 rprintf(FINFO, "skipping directory %s\n", fname);
3ec4dd97
AT
637 return NULL;
638 }
ebed4c3a 639
0c5f37d9
AT
640 if (one_file_system && st.st_dev != filesystem_dev) {
641 if (skip_filesystem(fname, &st))
642 return NULL;
643 }
ebed4c3a
MP
644
645 if (check_exclude_file(f, fname, &st))
76e26e10
DD
646 return NULL;
647
ac1a0994 648
ebed4c3a 649 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
ac1a0994
AT
650 return NULL;
651
ebed4c3a 652 skip_excludes:
ac1a0994 653
3ec4dd97 654 if (verbose > 2)
ebed4c3a
MP
655 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
656
657 file = (struct file_struct *) malloc(sizeof(*file));
658 if (!file)
659 out_of_memory("make_file");
660 memset((char *) file, 0, sizeof(*file));
3ec4dd97 661
ebed4c3a 662 if ((p = strrchr(fname, '/'))) {
3ec4dd97
AT
663 static char *lastdir;
664 *p = 0;
ebed4c3a 665 if (lastdir && strcmp(fname, lastdir) == 0) {
3ec4dd97
AT
666 file->dirname = lastdir;
667 } else {
a20aa42a 668 file->dirname = strdup(fname);
3ec4dd97
AT
669 lastdir = file->dirname;
670 }
ebed4c3a 671 file->basename = STRDUP(ap, p + 1);
3ec4dd97
AT
672 *p = '/';
673 } else {
674 file->dirname = NULL;
3d382777 675 file->basename = STRDUP(ap, fname);
3ec4dd97 676 }
c627d613 677
3ec4dd97
AT
678 file->modtime = st.st_mtime;
679 file->length = st.st_size;
680 file->mode = st.st_mode;
681 file->uid = st.st_uid;
682 file->gid = st.st_gid;
683 file->dev = st.st_dev;
684 file->inode = st.st_ino;
c627d613 685#ifdef HAVE_ST_RDEV
3ec4dd97 686 file->rdev = st.st_rdev;
c627d613
AT
687#endif
688
689#if SUPPORT_LINKS
3ec4dd97 690 if (S_ISLNK(st.st_mode)) {
3d382777 691 file->link = STRDUP(ap, linkbuf);
3ec4dd97 692 }
c627d613
AT
693#endif
694
1250f24e 695 if (always_checksum) {
ebed4c3a
MP
696 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
697 if (!file->sum)
698 out_of_memory("md4 sum");
1250f24e
AT
699 /* drat. we have to provide a null checksum for non-regular
700 files in order to be compatible with earlier versions
701 of rsync */
702 if (S_ISREG(st.st_mode)) {
ebed4c3a 703 file_checksum(fname, file->sum, st.st_size);
1250f24e
AT
704 } else {
705 memset(file->sum, 0, MD4_SUM_LENGTH);
706 }
ebed4c3a 707 }
c627d613 708
3ec4dd97
AT
709 if (flist_dir) {
710 static char *lastdir;
ebed4c3a 711 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
3ec4dd97
AT
712 file->basedir = lastdir;
713 } else {
a20aa42a 714 file->basedir = strdup(flist_dir);
3ec4dd97
AT
715 lastdir = file->basedir;
716 }
717 } else {
718 file->basedir = NULL;
719 }
c627d613 720
3ec4dd97 721 if (!S_ISDIR(st.st_mode))
a800434a 722 stats.total_size += st.st_size;
c627d613 723
3ec4dd97 724 return file;
c627d613
AT
725}
726
727
728
ebed4c3a
MP
729void send_file_name(int f, struct file_list *flist, char *fname,
730 int recursive, unsigned base_flags)
c627d613 731{
ebed4c3a
MP
732 struct file_struct *file;
733
734 file = make_file(f, fname, &flist->string_area, 0);
735
736 if (!file)
737 return;
738
739 if (show_build_progress_p() & !(flist->count % 100))
740 emit_build_progress(flist);
741
d9d6bc52 742 flist_expand(flist);
ebed4c3a
MP
743
744 if (write_batch) /* dw */
745 file->flags = FLAG_DELETE;
746
747 if (strcmp(file->basename, "")) {
748 flist->files[flist->count++] = file;
749 send_file_entry(file, f, base_flags);
750 }
751
752 if (S_ISDIR(file->mode) && recursive) {
753 struct exclude_struct **last_exclude_list =
754 local_exclude_list;
755 send_directory(f, flist, f_name(file));
756 local_exclude_list = last_exclude_list;
757 return;
758 }
c627d613
AT
759}
760
761
762
ebed4c3a 763static void send_directory(int f, struct file_list *flist, char *dir)
c627d613 764{
3ec4dd97
AT
765 DIR *d;
766 struct dirent *di;
767 char fname[MAXPATHLEN];
768 int l;
769 char *p;
770
771 d = opendir(dir);
772 if (!d) {
6ba9279f 773 io_error = 1;
ebed4c3a 774 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
3ec4dd97
AT
775 return;
776 }
c627d613 777
ebed4c3a 778 strlcpy(fname, dir, MAXPATHLEN);
3ec4dd97 779 l = strlen(fname);
ebed4c3a
MP
780 if (fname[l - 1] != '/') {
781 if (l == MAXPATHLEN - 1) {
6ba9279f 782 io_error = 1;
ebed4c3a
MP
783 rprintf(FERROR,
784 "skipping long-named directory %s\n",
785 fname);
3ec4dd97
AT
786 closedir(d);
787 return;
788 }
ebed4c3a 789 strlcat(fname, "/", MAXPATHLEN);
3ec4dd97
AT
790 l++;
791 }
792 p = fname + strlen(fname);
c627d613 793
3d913675
AT
794 local_exclude_list = NULL;
795
3ec4dd97 796 if (cvs_exclude) {
ebed4c3a
MP
797 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
798 strcpy(p, ".cvsignore");
799 local_exclude_list =
800 make_exclude_list(fname, NULL, 0, 0);
3ec4dd97 801 } else {
6ba9279f 802 io_error = 1;
ebed4c3a
MP
803 rprintf(FINFO,
804 "cannot cvs-exclude in long-named directory %s\n",
805 fname);
3ec4dd97 806 }
ebed4c3a
MP
807 }
808
809 for (di = readdir(d); di; di = readdir(d)) {
d6e6ecbd 810 char *dname = d_name(di);
ebed4c3a 811 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
3ec4dd97 812 continue;
ebed4c3a
MP
813 strlcpy(p, dname, MAXPATHLEN - l);
814 send_file_name(f, flist, fname, recurse, 0);
3ec4dd97 815 }
c627d613 816
3d913675
AT
817 if (local_exclude_list) {
818 add_exclude_list("!", &local_exclude_list, 0);
819 }
820
3ec4dd97 821 closedir(d);
c627d613
AT
822}
823
824
58225000
MP
825/*
826 *
827 * I *think* f==-1 means that the list should just be built in memory
828 * and not transmitted. But who can tell? -- mbp
829 */
ebed4c3a 830struct file_list *send_file_list(int f, int argc, char *argv[])
c627d613 831{
ebed4c3a 832 int i, l;
bcacc18b 833 STRUCT_STAT st;
ebed4c3a
MP
834 char *p, *dir, *olddir;
835 char lastpath[MAXPATHLEN] = "";
649d65ed 836 struct file_list *flist;
a800434a 837 int64 start_write;
649d65ed
AT
838
839 if (verbose && recurse && !am_server && f != -1) {
58225000 840 rprintf(FINFO, RSYNC_NAME ": building file list...\n");
ebed4c3a
MP
841 if (verbose > 1)
842 rprintf(FINFO, "\n");
9486289c 843 rflush(FINFO);
649d65ed 844 }
c627d613 845
a800434a
AT
846 start_write = stats.total_written;
847
3d382777 848 flist = flist_new();
c627d613 849
d6dead6b
AT
850 if (f != -1) {
851 io_start_buffering(f);
852 }
853
ebed4c3a 854 for (i = 0; i < argc; i++) {
b5313607 855 char *fname = topsrcname;
c627d613 856
ebed4c3a 857 strlcpy(fname, argv[i], MAXPATHLEN);
c627d613 858
649d65ed 859 l = strlen(fname);
ebed4c3a 860 if (l != 1 && fname[l - 1] == '/') {
53f821f1
DD
861 if ((l == 2) && (fname[0] == '.')) {
862 /* Turn ./ into just . rather than ./.
ebed4c3a
MP
863 This was put in to avoid a problem with
864 rsync -aR --delete from ./
865 The send_file_name() below of ./ was
866 mysteriously preventing deletes */
53f821f1
DD
867 fname[1] = 0;
868 } else {
ebed4c3a 869 strlcat(fname, ".", MAXPATHLEN);
53f821f1 870 }
649d65ed 871 }
c627d613 872
ebed4c3a 873 if (link_stat(fname, &st) != 0) {
f76933b1 874 if (f != -1) {
ebed4c3a
MP
875 io_error = 1;
876 rprintf(FERROR, "link_stat %s : %s\n",
877 fname, strerror(errno));
f76933b1 878 }
649d65ed
AT
879 continue;
880 }
c627d613 881
649d65ed 882 if (S_ISDIR(st.st_mode) && !recurse) {
ebed4c3a 883 rprintf(FINFO, "skipping directory %s\n", fname);
649d65ed
AT
884 continue;
885 }
c627d613 886
649d65ed 887 dir = NULL;
2bca43f6 888 olddir = NULL;
649d65ed
AT
889
890 if (!relative_paths) {
ebed4c3a 891 p = strrchr(fname, '/');
649d65ed
AT
892 if (p) {
893 *p = 0;
ebed4c3a 894 if (p == fname)
649d65ed
AT
895 dir = "/";
896 else
ebed4c3a
MP
897 dir = fname;
898 fname = p + 1;
649d65ed 899 }
ebed4c3a 900 } else if (f != -1 && (p = strrchr(fname, '/'))) {
649d65ed
AT
901 /* this ensures we send the intermediate directories,
902 thus getting their permissions right */
903 *p = 0;
ebed4c3a 904 if (strcmp(lastpath, fname)) {
37f9805d 905 strlcpy(lastpath, fname, sizeof(lastpath));
649d65ed 906 *p = '/';
ebed4c3a
MP
907 for (p = fname + 1; (p = strchr(p, '/'));
908 p++) {
6c82f74b 909 int copy_links_saved = copy_links;
245fbb51 910 int recurse_saved = recurse;
649d65ed 911 *p = 0;
b5313607 912 copy_links = copy_unsafe_links;
245fbb51
DD
913 /* set recurse to 1 to prevent make_file
914 from ignoring directory, but still
915 turn off the recursive parameter to
916 send_file_name */
917 recurse = 1;
ebed4c3a
MP
918 send_file_name(f, flist, fname, 0,
919 0);
6c82f74b 920 copy_links = copy_links_saved;
245fbb51 921 recurse = recurse_saved;
649d65ed
AT
922 *p = '/';
923 }
924 } else {
925 *p = '/';
926 }
927 }
ebed4c3a 928
649d65ed
AT
929 if (!*fname)
930 fname = ".";
ebed4c3a 931
649d65ed 932 if (dir && *dir) {
2bca43f6 933 olddir = push_dir(dir, 1);
5243c216
AT
934
935 if (!olddir) {
ebed4c3a
MP
936 io_error = 1;
937 rprintf(FERROR, "push_dir %s : %s\n",
938 dir, strerror(errno));
649d65ed
AT
939 continue;
940 }
5243c216 941
649d65ed 942 flist_dir = dir;
2bca43f6 943 }
ebed4c3a 944
2bca43f6
DD
945 if (one_file_system)
946 set_filesystem(fname);
947
ebed4c3a 948 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
2bca43f6
DD
949
950 if (olddir != NULL) {
649d65ed 951 flist_dir = NULL;
5243c216 952 if (pop_dir(olddir) != 0) {
ebed4c3a
MP
953 rprintf(FERROR, "pop_dir %s : %s\n",
954 dir, strerror(errno));
65417579 955 exit_cleanup(RERR_FILESELECT);
649d65ed 956 }
649d65ed 957 }
649d65ed 958 }
dc5ddbcc 959
b5313607
DD
960 topsrcname[0] = '\0';
961
649d65ed 962 if (f != -1) {
ebed4c3a 963 send_file_entry(NULL, f, 0);
649d65ed 964 }
c627d613 965
58225000 966 finish_build_progress(flist);
ebed4c3a 967
0199b05f 968 clean_flist(flist, 0);
ebed4c3a 969
649d65ed 970 /* now send the uid/gid list. This was introduced in protocol
ebed4c3a 971 version 15 */
649d65ed
AT
972 if (f != -1 && remote_version >= 15) {
973 send_uid_list(f);
974 }
f6c34742 975
6ba9279f
AT
976 /* if protocol version is >= 17 then send the io_error flag */
977 if (f != -1 && remote_version >= 17) {
cda2ae84 978 extern int module_id;
ebed4c3a 979 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
6ba9279f
AT
980 }
981
d6dead6b
AT
982 if (f != -1) {
983 io_end_buffering(f);
a800434a
AT
984 stats.flist_size = stats.total_written - start_write;
985 stats.num_files = flist->count;
ebed4c3a
MP
986 if (write_batch) /* dw */
987 write_batch_flist_info(flist->count, flist->files);
d6dead6b
AT
988 }
989
17faa41c 990 if (verbose > 2)
ebed4c3a 991 rprintf(FINFO, "send_file_list done\n");
17faa41c 992
649d65ed 993 return flist;
c627d613
AT
994}
995
996
997struct file_list *recv_file_list(int f)
998{
ebed4c3a
MP
999 struct file_list *flist;
1000 unsigned char flags;
1001 int64 start_read;
1002 extern int list_only;
c627d613 1003
ebed4c3a
MP
1004 if (verbose && recurse && !am_server) {
1005 rprintf(FINFO, "receiving file list ... ");
1006 rflush(FINFO);
1007 }
c627d613 1008
ebed4c3a 1009 start_read = stats.total_read;
a800434a 1010
ebed4c3a
MP
1011 flist = (struct file_list *) malloc(sizeof(flist[0]));
1012 if (!flist)
1013 goto oom;
c627d613 1014
ebed4c3a
MP
1015 flist->count = 0;
1016 flist->malloced = 1000;
1017 flist->files =
1018 (struct file_struct **) malloc(sizeof(flist->files[0]) *
1019 flist->malloced);
1020 if (!flist->files)
1021 goto oom;
c627d613
AT
1022
1023
ebed4c3a 1024 for (flags = read_byte(f); flags; flags = read_byte(f)) {
5d2c5c4c
MP
1025 int i = flist->count;
1026
d9d6bc52 1027 flist_expand(flist);
c627d613 1028
ebed4c3a 1029 receive_file_entry(&flist->files[i], flags, f);
c627d613 1030
ebed4c3a
MP
1031 if (S_ISREG(flist->files[i]->mode))
1032 stats.total_size += flist->files[i]->length;
c627d613 1033
ebed4c3a 1034 flist->count++;
c627d613 1035
ebed4c3a
MP
1036 if (verbose > 2)
1037 rprintf(FINFO, "recv_file_name(%s)\n",
1038 f_name(flist->files[i]));
1039 }
c627d613
AT
1040
1041
ebed4c3a
MP
1042 if (verbose > 2)
1043 rprintf(FINFO, "received %d names\n", flist->count);
c627d613 1044
ebed4c3a 1045 clean_flist(flist, relative_paths);
c627d613 1046
ebed4c3a
MP
1047 if (verbose && recurse && !am_server) {
1048 rprintf(FINFO, "done\n");
1049 }
a06d19e3 1050
ebed4c3a
MP
1051 /* now recv the uid/gid list. This was introduced in protocol version 15 */
1052 if (f != -1 && remote_version >= 15) {
1053 recv_uid_list(f, flist);
1054 }
f6c34742 1055
ebed4c3a
MP
1056 /* if protocol version is >= 17 then recv the io_error flag */
1057 if (f != -1 && remote_version >= 17 && !read_batch) { /* dw-added readbatch */
1058 extern int module_id;
1059 extern int ignore_errors;
1060 if (lp_ignore_errors(module_id) || ignore_errors) {
1061 read_int(f);
1062 } else {
1063 io_error |= read_int(f);
1064 }
1065 }
6ba9279f 1066
ebed4c3a
MP
1067 if (list_only) {
1068 int i;
1069 for (i = 0; i < flist->count; i++) {
1070 list_file_entry(flist->files[i]);
1071 }
1072 }
f7632fc6
AT
1073
1074
ebed4c3a
MP
1075 if (verbose > 2)
1076 rprintf(FINFO, "recv_file_list done\n");
17faa41c 1077
ebed4c3a
MP
1078 stats.flist_size = stats.total_read - start_read;
1079 stats.num_files = flist->count;
a800434a 1080
ebed4c3a 1081 return flist;
c627d613 1082
ebed4c3a
MP
1083 oom:
1084 out_of_memory("recv_file_list");
1085 return NULL; /* not reached */
c627d613
AT
1086}
1087
1088
e03dfae5
MP
1089/*
1090 * XXX: This is currently the hottest function while building the file
1091 * list, because building f_name()s every time is expensive.
1092 **/
ebed4c3a 1093int file_compare(struct file_struct **f1, struct file_struct **f2)
c627d613 1094{
ebed4c3a
MP
1095 if (!(*f1)->basename && !(*f2)->basename)
1096 return 0;
1097 if (!(*f1)->basename)
1098 return -1;
1099 if (!(*f2)->basename)
1100 return 1;
3ec4dd97 1101 if ((*f1)->dirname == (*f2)->dirname)
aa9b77a5 1102 return u_strcmp((*f1)->basename, (*f2)->basename);
ebed4c3a 1103 return u_strcmp(f_name(*f1), f_name(*f2));
c627d613
AT
1104}
1105
1106
ebed4c3a 1107int flist_find(struct file_list *flist, struct file_struct *f)
c627d613 1108{
ebed4c3a 1109 int low = 0, high = flist->count - 1;
d966ee25 1110
ebed4c3a
MP
1111 if (flist->count <= 0)
1112 return -1;
d966ee25
AT
1113
1114 while (low != high) {
ebed4c3a
MP
1115 int mid = (low + high) / 2;
1116 int ret =
1117 file_compare(&flist->files[flist_up(flist, mid)], &f);
1118 if (ret == 0)
1119 return flist_up(flist, mid);
d966ee25 1120 if (ret > 0) {
ebed4c3a 1121 high = mid;
d966ee25 1122 } else {
ebed4c3a 1123 low = mid + 1;
d966ee25
AT
1124 }
1125 }
1126
ebed4c3a
MP
1127 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
1128 return flist_up(flist, low);
d966ee25 1129 return -1;
c627d613
AT
1130}
1131
1132
3ec4dd97
AT
1133/*
1134 * free up one file
1135 */
66203a98 1136void free_file(struct file_struct *file)
c627d613 1137{
ebed4c3a
MP
1138 if (!file)
1139 return;
1140 if (file->basename)
1141 free(file->basename);
1142 if (file->link)
1143 free(file->link);
1144 if (file->sum)
1145 free(file->sum);
3d382777 1146 *file = null_file;
3ec4dd97 1147}
c627d613 1148
c627d613 1149
3d382777
AT
1150/*
1151 * allocate a new file list
1152 */
1153struct file_list *flist_new()
1154{
1155 struct file_list *flist;
1156
ebed4c3a
MP
1157 flist = (struct file_list *) malloc(sizeof(flist[0]));
1158 if (!flist)
1159 out_of_memory("send_file_list");
3d382777 1160
ebed4c3a 1161 flist->count = 0;
d9d6bc52
MP
1162 flist->malloced = 0;
1163 flist->files = NULL;
1164
3d382777
AT
1165#if ARENA_SIZE > 0
1166 flist->string_area = string_area_new(0);
1167#else
5c66303a 1168 flist->string_area = NULL;
3d382777
AT
1169#endif
1170 return flist;
1171}
ebed4c3a 1172
3ec4dd97
AT
1173/*
1174 * free up all elements in a flist
1175 */
1176void flist_free(struct file_list *flist)
1177{
1178 int i;
ebed4c3a 1179 for (i = 1; i < flist->count; i++) {
3d382777
AT
1180 if (!flist->string_area)
1181 free_file(flist->files[i]);
649d65ed 1182 free(flist->files[i]);
ebed4c3a 1183 }
d9d6bc52
MP
1184 /* FIXME: I don't think we generally need to blank the flist
1185 * since it's about to be freed. This will just cause more
1186 * memory traffic. If you want a freed-memory debugger, you
1187 * know where to get it. */
ebed4c3a
MP
1188 memset((char *) flist->files, 0,
1189 sizeof(flist->files[0]) * flist->count);
3ec4dd97 1190 free(flist->files);
3d382777
AT
1191 if (flist->string_area)
1192 string_area_free(flist->string_area);
ebed4c3a 1193 memset((char *) flist, 0, sizeof(*flist));
3ec4dd97 1194 free(flist);
c627d613
AT
1195}
1196
1197
1198/*
1199 * This routine ensures we don't have any duplicate names in our file list.
1200 * duplicate names can cause corruption because of the pipelining
1201 */
0199b05f 1202static void clean_flist(struct file_list *flist, int strip_root)
c627d613 1203{
3ec4dd97 1204 int i;
c627d613 1205
ebed4c3a 1206 if (!flist || flist->count == 0)
3ec4dd97 1207 return;
3ec4dd97 1208
ebed4c3a
MP
1209 qsort(flist->files, flist->count,
1210 sizeof(flist->files[0]), (int (*)()) file_compare);
1211
1212 for (i = 1; i < flist->count; i++) {
3ec4dd97 1213 if (flist->files[i]->basename &&
ebed4c3a 1214 flist->files[i - 1]->basename &&
3ec4dd97 1215 strcmp(f_name(flist->files[i]),
ebed4c3a 1216 f_name(flist->files[i - 1])) == 0) {
3ec4dd97 1217 if (verbose > 1 && !am_server)
ebed4c3a
MP
1218 rprintf(FINFO,
1219 "removing duplicate name %s from file list %d\n",
1220 f_name(flist->files[i - 1]),
1221 i - 1);
3d382777
AT
1222 /* it's not great that the flist knows the semantics of the
1223 * file memory usage, but i'd rather not add a flag byte
1224 * to that struct. XXX can i use a bit in the flags field? */
1225 if (flist->string_area)
1226 flist->files[i][0] = null_file;
1227 else
1228 free_file(flist->files[i]);
ebed4c3a 1229 }
3ec4dd97 1230 }
0199b05f 1231
736a6a29
MP
1232 /* FIXME: There is a bug here when filenames are repeated more
1233 * than once, because we don't handle freed files when doing
1234 * the comparison. */
1235
0199b05f
AT
1236 if (strip_root) {
1237 /* we need to strip off the root directory in the case
1238 of relative paths, but this must be done _after_
1239 the sorting phase */
ebed4c3a 1240 for (i = 0; i < flist->count; i++) {
0199b05f
AT
1241 if (flist->files[i]->dirname &&
1242 flist->files[i]->dirname[0] == '/') {
1243 memmove(&flist->files[i]->dirname[0],
1244 &flist->files[i]->dirname[1],
1245 strlen(flist->files[i]->dirname));
1246 }
ebed4c3a
MP
1247
1248 if (flist->files[i]->dirname &&
0199b05f
AT
1249 !flist->files[i]->dirname[0]) {
1250 flist->files[i]->dirname = NULL;
1251 }
1252 }
1253 }
1254
1255
ebed4c3a
MP
1256 if (verbose <= 3)
1257 return;
0199b05f 1258
ebed4c3a
MP
1259 for (i = 0; i < flist->count; i++) {
1260 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
1261 (int) getpid(), i,
74e708d8
AT
1262 NS(flist->files[i]->dirname),
1263 NS(flist->files[i]->basename),
08a740ff 1264 (int) flist->files[i]->mode,
ebed4c3a 1265 (double) flist->files[i]->length);
0199b05f 1266 }
3ec4dd97
AT
1267}
1268
1269
1270/*
1271 * return the full filename of a flist entry
e03dfae5
MP
1272 *
1273 * This function is too expensive at the moment, because it copies
1274 * strings when often we only want to compare them. In any case,
1275 * using strlcat is silly because it will walk the string repeatedly.
3ec4dd97
AT
1276 */
1277char *f_name(struct file_struct *f)
1278{
1279 static char names[10][MAXPATHLEN];
1280 static int n;
1281 char *p = names[n];
1282
ebed4c3a
MP
1283 if (!f || !f->basename)
1284 return NULL;
3ec4dd97 1285
ebed4c3a 1286 n = (n + 1) % 10;
3ec4dd97
AT
1287
1288 if (f->dirname) {
e03dfae5
MP
1289 int off;
1290
1291 off = strlcpy(p, f->dirname, MAXPATHLEN);
ebed4c3a
MP
1292 off += strlcpy(p + off, "/", MAXPATHLEN - off);
1293 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
3ec4dd97 1294 } else {
37f9805d 1295 strlcpy(p, f->basename, MAXPATHLEN);
3ec4dd97
AT
1296 }
1297
1298 return p;
c627d613 1299}