Commit | Line | Data |
---|---|---|
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 | /* | |
21 | Utilities used in rsync | |
22 | ||
23 | tridge, June 1996 | |
24 | */ | |
25 | #include "rsync.h" | |
26 | ||
a5343e76 AT |
27 | /**************************************************************************** |
28 | Set a fd into nonblocking mode. Uses POSIX O_NONBLOCK if available, | |
29 | else | |
30 | if SYSV use O_NDELAY | |
31 | if BSD use FNDELAY | |
32 | ****************************************************************************/ | |
33 | int set_nonblocking(int fd) | |
94481d91 | 34 | { |
a5343e76 AT |
35 | int val; |
36 | #ifdef O_NONBLOCK | |
37 | #define FLAG_TO_SET O_NONBLOCK | |
38 | #else | |
39 | #ifdef SYSV | |
40 | #define FLAG_TO_SET O_NDELAY | |
41 | #else /* BSD */ | |
42 | #define FLAG_TO_SET FNDELAY | |
43 | #endif | |
44 | #endif | |
45 | ||
46 | if((val = fcntl(fd, F_GETFL, 0)) == -1) | |
47 | return -1; | |
48 | val |= FLAG_TO_SET; | |
49 | return fcntl( fd, F_SETFL, val); | |
50 | #undef FLAG_TO_SET | |
94481d91 AT |
51 | } |
52 | ||
d9bea2dd | 53 | |
c627d613 AT |
54 | /* this is taken from CVS */ |
55 | int piped_child(char **command,int *f_in,int *f_out) | |
56 | { | |
57 | int pid; | |
58 | int to_child_pipe[2]; | |
59 | int from_child_pipe[2]; | |
60 | ||
61 | if (pipe(to_child_pipe) < 0 || | |
62 | pipe(from_child_pipe) < 0) { | |
9486289c | 63 | rprintf(FERROR,"pipe: %s\n",strerror(errno)); |
34ccb63e | 64 | exit_cleanup(1); |
c627d613 AT |
65 | } |
66 | ||
67 | ||
3ba62a83 | 68 | pid = do_fork(); |
c627d613 | 69 | if (pid < 0) { |
9486289c | 70 | rprintf(FERROR,"fork: %s\n",strerror(errno)); |
34ccb63e | 71 | exit_cleanup(1); |
c627d613 AT |
72 | } |
73 | ||
74 | if (pid == 0) | |
75 | { | |
6574b4f7 | 76 | extern int orig_umask; |
c627d613 AT |
77 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || |
78 | close(to_child_pipe[1]) < 0 || | |
79 | close(from_child_pipe[0]) < 0 || | |
80 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { | |
9486289c | 81 | rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); |
34ccb63e | 82 | exit_cleanup(1); |
c627d613 | 83 | } |
773f2bd4 AT |
84 | if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); |
85 | if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); | |
6574b4f7 | 86 | umask(orig_umask); |
c627d613 | 87 | execvp(command[0], command); |
9486289c | 88 | rprintf(FERROR,"Failed to exec %s : %s\n", |
c627d613 | 89 | command[0],strerror(errno)); |
34ccb63e | 90 | exit_cleanup(1); |
c627d613 AT |
91 | } |
92 | ||
93 | if (close(from_child_pipe[1]) < 0 || | |
94 | close(to_child_pipe[0]) < 0) { | |
9486289c | 95 | rprintf(FERROR,"Failed to close : %s\n",strerror(errno)); |
34ccb63e | 96 | exit_cleanup(1); |
c627d613 AT |
97 | } |
98 | ||
99 | *f_in = from_child_pipe[0]; | |
100 | *f_out = to_child_pipe[1]; | |
3eb38818 AT |
101 | |
102 | set_nonblocking(*f_in); | |
103 | set_nonblocking(*f_out); | |
c627d613 AT |
104 | |
105 | return pid; | |
106 | } | |
107 | ||
366345fe AT |
108 | int local_child(int argc, char **argv,int *f_in,int *f_out) |
109 | { | |
110 | int pid; | |
111 | int to_child_pipe[2]; | |
112 | int from_child_pipe[2]; | |
113 | ||
114 | if (pipe(to_child_pipe) < 0 || | |
115 | pipe(from_child_pipe) < 0) { | |
9486289c | 116 | rprintf(FERROR,"pipe: %s\n",strerror(errno)); |
366345fe AT |
117 | exit_cleanup(1); |
118 | } | |
119 | ||
120 | ||
121 | pid = do_fork(); | |
122 | if (pid < 0) { | |
9486289c | 123 | rprintf(FERROR,"fork: %s\n",strerror(errno)); |
366345fe AT |
124 | exit_cleanup(1); |
125 | } | |
126 | ||
127 | if (pid == 0) { | |
128 | extern int am_sender; | |
129 | extern int am_server; | |
130 | ||
131 | am_sender = !am_sender; | |
132 | am_server = 1; | |
133 | ||
134 | if (dup2(to_child_pipe[0], STDIN_FILENO) < 0 || | |
135 | close(to_child_pipe[1]) < 0 || | |
136 | close(from_child_pipe[0]) < 0 || | |
137 | dup2(from_child_pipe[1], STDOUT_FILENO) < 0) { | |
9486289c | 138 | rprintf(FERROR,"Failed to dup/close : %s\n",strerror(errno)); |
366345fe AT |
139 | exit_cleanup(1); |
140 | } | |
141 | if (to_child_pipe[0] != STDIN_FILENO) close(to_child_pipe[0]); | |
142 | if (from_child_pipe[1] != STDOUT_FILENO) close(from_child_pipe[1]); | |
9486289c | 143 | start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv); |
366345fe AT |
144 | } |
145 | ||
146 | if (close(from_child_pipe[1]) < 0 || | |
147 | close(to_child_pipe[0]) < 0) { | |
9486289c | 148 | rprintf(FERROR,"Failed to close : %s\n",strerror(errno)); |
366345fe AT |
149 | exit_cleanup(1); |
150 | } | |
151 | ||
152 | *f_in = from_child_pipe[0]; | |
153 | *f_out = to_child_pipe[1]; | |
154 | ||
155 | return pid; | |
156 | } | |
157 | ||
158 | ||
c627d613 AT |
159 | |
160 | void out_of_memory(char *str) | |
161 | { | |
9486289c | 162 | rprintf(FERROR,"ERROR: out of memory in %s\n",str); |
575f2fca AT |
163 | exit_cleanup(1); |
164 | } | |
165 | ||
166 | void overflow(char *str) | |
167 | { | |
9486289c | 168 | rprintf(FERROR,"ERROR: buffer overflow in %s\n",str); |
34ccb63e | 169 | exit_cleanup(1); |
c627d613 AT |
170 | } |
171 | ||
172 | ||
c627d613 AT |
173 | |
174 | int set_modtime(char *fname,time_t modtime) | |
175 | { | |
31e12522 AT |
176 | extern int dry_run; |
177 | if (dry_run) return 0; | |
178 | { | |
1e9f155a | 179 | #ifdef HAVE_UTIMBUF |
31e12522 AT |
180 | struct utimbuf tbuf; |
181 | tbuf.actime = time(NULL); | |
182 | tbuf.modtime = modtime; | |
183 | return utime(fname,&tbuf); | |
c627d613 | 184 | #elif defined(HAVE_UTIME) |
31e12522 AT |
185 | time_t t[2]; |
186 | t[0] = time(NULL); | |
187 | t[1] = modtime; | |
188 | return utime(fname,t); | |
c627d613 | 189 | #else |
31e12522 AT |
190 | struct timeval t[2]; |
191 | t[0].tv_sec = time(NULL); | |
192 | t[0].tv_usec = 0; | |
193 | t[1].tv_sec = modtime; | |
194 | t[1].tv_usec = 0; | |
195 | return utimes(fname,t); | |
c627d613 | 196 | #endif |
31e12522 | 197 | } |
c627d613 | 198 | } |
94481d91 | 199 | |
720b47f2 | 200 | |
6574b4f7 AT |
201 | /**************************************************************************** |
202 | create any necessary directories in fname. Unfortunately we don't know | |
203 | what perms to give the directory when this is called so we need to rely | |
204 | on the umask | |
205 | ****************************************************************************/ | |
206 | int create_directory_path(char *fname) | |
207 | { | |
208 | extern int orig_umask; | |
209 | char *p; | |
210 | ||
211 | while (*fname == '/') fname++; | |
212 | while (strncmp(fname,"./",2)==0) fname += 2; | |
213 | ||
214 | p = fname; | |
215 | while ((p=strchr(p,'/'))) { | |
216 | *p = 0; | |
1b2d733a | 217 | do_mkdir(fname,0777 & ~orig_umask); |
6574b4f7 AT |
218 | *p = '/'; |
219 | p++; | |
220 | } | |
221 | return 0; | |
222 | } | |
950ab32d AT |
223 | |
224 | ||
225 | /* Write LEN bytes at PTR to descriptor DESC, retrying if interrupted. | |
226 | Return LEN upon success, write's (negative) error code otherwise. | |
227 | ||
228 | derived from GNU C's cccp.c. | |
229 | */ | |
a5343e76 | 230 | static int full_write(int desc, char *ptr, int len) |
950ab32d AT |
231 | { |
232 | int total_written; | |
233 | ||
234 | total_written = 0; | |
235 | while (len > 0) { | |
236 | int written = write (desc, ptr, len); | |
237 | if (written < 0) { | |
238 | #ifdef EINTR | |
239 | if (errno == EINTR) | |
240 | continue; | |
241 | #endif | |
242 | return written; | |
243 | } | |
244 | total_written += written; | |
245 | ptr += written; | |
246 | len -= written; | |
247 | } | |
248 | return total_written; | |
249 | } | |
250 | ||
251 | /* Read LEN bytes at PTR from descriptor DESC, retrying if interrupted. | |
252 | Return the actual number of bytes read, zero for EOF, or negative | |
253 | for an error. | |
254 | ||
255 | derived from GNU C's cccp.c. */ | |
6e4fb64e | 256 | static int safe_read(int desc, char *ptr, int len) |
950ab32d AT |
257 | { |
258 | int n_chars; | |
259 | ||
260 | if (len <= 0) | |
261 | return len; | |
262 | ||
263 | #ifdef EINTR | |
264 | do { | |
265 | n_chars = read(desc, ptr, len); | |
266 | } while (n_chars < 0 && errno == EINTR); | |
267 | #else | |
268 | n_chars = read(desc, ptr, len); | |
269 | #endif | |
270 | ||
271 | return n_chars; | |
272 | } | |
273 | ||
274 | ||
275 | /* copy a file - this is used in conjunction with the --temp-dir option */ | |
276 | int copy_file(char *source, char *dest, mode_t mode) | |
277 | { | |
278 | int ifd; | |
279 | int ofd; | |
280 | char buf[1024 * 8]; | |
281 | int len; /* Number of bytes read into `buf'. */ | |
282 | ||
283 | ifd = open(source, O_RDONLY); | |
284 | if (ifd == -1) { | |
9486289c | 285 | rprintf(FERROR,"open %s: %s\n", |
950ab32d AT |
286 | source,strerror(errno)); |
287 | return -1; | |
288 | } | |
289 | ||
31e12522 | 290 | if (do_unlink(dest) && errno != ENOENT) { |
9486289c | 291 | rprintf(FERROR,"unlink %s: %s\n", |
950ab32d AT |
292 | dest,strerror(errno)); |
293 | return -1; | |
294 | } | |
295 | ||
31e12522 | 296 | ofd = do_open(dest, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, mode); |
c46ded46 | 297 | if (ofd == -1) { |
9486289c | 298 | rprintf(FERROR,"open %s: %s\n", |
950ab32d AT |
299 | dest,strerror(errno)); |
300 | close(ifd); | |
301 | return -1; | |
302 | } | |
303 | ||
304 | while ((len = safe_read(ifd, buf, sizeof(buf))) > 0) { | |
305 | if (full_write(ofd, buf, len) < 0) { | |
9486289c | 306 | rprintf(FERROR,"write %s: %s\n", |
950ab32d AT |
307 | dest,strerror(errno)); |
308 | close(ifd); | |
309 | close(ofd); | |
310 | return -1; | |
311 | } | |
312 | } | |
313 | ||
314 | close(ifd); | |
315 | close(ofd); | |
316 | ||
317 | if (len < 0) { | |
9486289c | 318 | rprintf(FERROR,"read %s: %s\n", |
950ab32d AT |
319 | source,strerror(errno)); |
320 | return -1; | |
321 | } | |
322 | ||
323 | return 0; | |
324 | } | |
feaa89c4 AT |
325 | |
326 | /* sleep for a while via select */ | |
327 | void u_sleep(int usec) | |
328 | { | |
329 | struct timeval tv; | |
330 | ||
331 | tv.tv_sec = 0; | |
332 | tv.tv_usec = usec; | |
333 | select(0, NULL, NULL, NULL, &tv); | |
334 | } | |
3ba62a83 AT |
335 | |
336 | ||
337 | static pid_t all_pids[10]; | |
338 | static int num_pids; | |
339 | ||
340 | /* fork and record the pid of the child */ | |
341 | pid_t do_fork(void) | |
342 | { | |
343 | pid_t newpid = fork(); | |
344 | ||
345 | if (newpid) { | |
346 | all_pids[num_pids++] = newpid; | |
347 | } | |
348 | return newpid; | |
349 | } | |
350 | ||
351 | /* kill all children */ | |
352 | void kill_all(int sig) | |
353 | { | |
354 | int i; | |
355 | for (i=0;i<num_pids;i++) { | |
356 | if (all_pids[i] != getpid()) | |
357 | kill(all_pids[i], sig); | |
358 | } | |
359 | } | |
9486289c | 360 | |
7a6421fa AT |
361 | /* like strncpy but does not 0 fill the buffer and always null |
362 | terminates (thus it can use maxlen+1 space in d) */ | |
363 | void strlcpy(char *d, char *s, int maxlen) | |
364 | { | |
365 | int len = strlen(s); | |
366 | if (len > maxlen) len = maxlen; | |
367 | memcpy(d, s, len); | |
368 | d[len] = 0; | |
369 | } | |
8ef4ffd6 | 370 | |
e42c9458 AT |
371 | /* like strncat but does not 0 fill the buffer and always null |
372 | terminates (thus it can use maxlen+1 space in d) */ | |
373 | void strlcat(char *d, char *s, int maxlen) | |
374 | { | |
375 | int len1 = strlen(d); | |
376 | int len2 = strlen(s); | |
377 | if (len1+len2 > maxlen) { | |
378 | len2 = maxlen-len1; | |
379 | } | |
380 | if (len2 > 0) { | |
381 | memcpy(d+len1, s, len2); | |
382 | d[len1+len2] = 0; | |
383 | } | |
384 | } | |
385 | ||
8ef4ffd6 AT |
386 | /* turn a user name into a uid */ |
387 | int name_to_uid(char *name, uid_t *uid) | |
388 | { | |
389 | struct passwd *pass; | |
390 | if (!name || !*name) return 0; | |
391 | pass = getpwnam(name); | |
392 | if (pass) { | |
393 | *uid = pass->pw_uid; | |
394 | return 1; | |
395 | } | |
396 | return 0; | |
397 | } | |
398 | ||
399 | /* turn a group name into a gid */ | |
400 | int name_to_gid(char *name, gid_t *gid) | |
401 | { | |
402 | struct group *grp; | |
403 | if (!name || !*name) return 0; | |
404 | grp = getgrnam(name); | |
405 | if (grp) { | |
406 | *gid = grp->gr_gid; | |
407 | return 1; | |
408 | } | |
409 | return 0; | |
410 | } | |
411 | ||
ff8b29b8 | 412 | |
31593dd6 AT |
413 | /* lock a byte range in a open file */ |
414 | int lock_range(int fd, int offset, int len) | |
0c515f17 | 415 | { |
31593dd6 | 416 | struct flock lock; |
0c515f17 | 417 | |
31593dd6 AT |
418 | lock.l_type = F_WRLCK; |
419 | lock.l_whence = SEEK_SET; | |
420 | lock.l_start = offset; | |
421 | lock.l_len = len; | |
422 | lock.l_pid = 0; | |
423 | ||
424 | return fcntl(fd,F_SETLK,&lock) == 0; | |
0c515f17 | 425 | } |
874895d5 AT |
426 | |
427 | ||
087bf010 | 428 | static void glob_expand_one(char *s, char **argv, int *argc, int maxargs) |
874895d5 | 429 | { |
932be9aa | 430 | #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H)) |
e42c9458 | 431 | if (!*s) s = "."; |
087bf010 | 432 | argv[*argc] = strdup(s); |
874895d5 AT |
433 | (*argc)++; |
434 | return; | |
435 | #else | |
436 | glob_t globbuf; | |
437 | int i; | |
438 | ||
e42c9458 AT |
439 | if (!*s) s = "."; |
440 | ||
087bf010 AT |
441 | argv[*argc] = strdup(s); |
442 | ||
874895d5 AT |
443 | memset(&globbuf, 0, sizeof(globbuf)); |
444 | glob(argv[*argc], 0, NULL, &globbuf); | |
445 | if (globbuf.gl_pathc == 0) { | |
446 | (*argc)++; | |
447 | globfree(&globbuf); | |
448 | return; | |
449 | } | |
450 | for (i=0; i<(maxargs - (*argc)) && i<globbuf.gl_pathc;i++) { | |
451 | if (i == 0) free(argv[*argc]); | |
452 | argv[(*argc) + i] = strdup(globbuf.gl_pathv[i]); | |
453 | if (!argv[(*argc) + i]) out_of_memory("glob_expand"); | |
454 | } | |
455 | globfree(&globbuf); | |
456 | (*argc) += i; | |
457 | #endif | |
458 | } | |
5a96ee05 | 459 | |
ba5e128d | 460 | void glob_expand(char *base1, char **argv, int *argc, int maxargs) |
087bf010 AT |
461 | { |
462 | char *s = argv[*argc]; | |
463 | char *p, *q; | |
ba5e128d | 464 | char *base = base1; |
087bf010 AT |
465 | |
466 | if (!s || !*s) return; | |
467 | ||
e42c9458 AT |
468 | if (strncmp(s, base, strlen(base)) == 0) { |
469 | s += strlen(base); | |
470 | } | |
471 | ||
087bf010 AT |
472 | s = strdup(s); |
473 | if (!s) out_of_memory("glob_expand"); | |
474 | ||
ba5e128d AT |
475 | base = (char *)malloc(strlen(base1)+3); |
476 | if (!base) out_of_memory("glob_expand"); | |
477 | ||
478 | sprintf(base," %s/", base1); | |
479 | ||
087bf010 AT |
480 | q = s; |
481 | while ((p = strstr(q,base)) && ((*argc) < maxargs)) { | |
ba5e128d AT |
482 | /* split it at this point */ |
483 | *p = 0; | |
484 | glob_expand_one(q, argv, argc, maxargs); | |
485 | q = p+strlen(base); | |
087bf010 AT |
486 | } |
487 | ||
488 | if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs); | |
489 | ||
490 | free(s); | |
ba5e128d | 491 | free(base); |
087bf010 | 492 | } |
5a96ee05 AT |
493 | |
494 | /******************************************************************* | |
495 | convert a string to lower case | |
496 | ********************************************************************/ | |
497 | void strlower(char *s) | |
498 | { | |
499 | while (*s) { | |
500 | if (isupper(*s)) *s = tolower(*s); | |
501 | s++; | |
502 | } | |
503 | } | |
e42c9458 AT |
504 | |
505 | /* this is like vsnprintf but the 'n' limit does not include | |
506 | the terminating null. So if you have a 1024 byte buffer then | |
507 | pass 1023 for n */ | |
508 | int vslprintf(char *str, int n, const char *format, va_list ap) | |
509 | { | |
e42c9458 AT |
510 | int ret = vsnprintf(str, n, format, ap); |
511 | if (ret > n || ret < 0) { | |
512 | str[n] = 0; | |
513 | return -1; | |
514 | } | |
515 | str[ret] = 0; | |
516 | return ret; | |
e42c9458 AT |
517 | } |
518 | ||
519 | ||
520 | /* like snprintf but always null terminates */ | |
521 | int slprintf(char *str, int n, char *format, ...) | |
522 | { | |
523 | va_list ap; | |
524 | int ret; | |
525 | ||
526 | va_start(ap, format); | |
527 | ret = vslprintf(str,n,format,ap); | |
528 | va_end(ap); | |
529 | return ret; | |
530 | } | |
8d9dc9f9 | 531 | |
fe8c0a98 AT |
532 | |
533 | void *Realloc(void *p, int size) | |
534 | { | |
535 | if (!p) return (void *)malloc(size); | |
536 | return (void *)realloc(p, size); | |
537 | } | |
5243c216 AT |
538 | |
539 | ||
540 | void clean_fname(char *name) | |
541 | { | |
542 | char *p; | |
543 | int l; | |
544 | int modified = 1; | |
545 | ||
546 | if (!name) return; | |
547 | ||
548 | while (modified) { | |
549 | modified = 0; | |
550 | ||
551 | if ((p=strstr(name,"/./"))) { | |
552 | modified = 1; | |
553 | while (*p) { | |
554 | p[0] = p[2]; | |
555 | p++; | |
556 | } | |
557 | } | |
558 | ||
559 | if ((p=strstr(name,"//"))) { | |
560 | modified = 1; | |
561 | while (*p) { | |
562 | p[0] = p[1]; | |
563 | p++; | |
564 | } | |
565 | } | |
566 | ||
567 | if (strncmp(p=name,"./",2) == 0) { | |
568 | modified = 1; | |
569 | do { | |
570 | p[0] = p[2]; | |
571 | } while (*p++); | |
572 | } | |
573 | ||
574 | l = strlen(p=name); | |
575 | if (l > 1 && p[l-1] == '/') { | |
576 | modified = 1; | |
577 | p[l-1] = 0; | |
578 | } | |
579 | } | |
580 | } | |
581 | ||
582 | ||
583 | static char curr_dir[MAXPATHLEN]; | |
584 | ||
585 | /* like chdir() but can be reversed with pop_dir() if save is set. It | |
586 | is also much faster as it remembers where we have been */ | |
587 | char *push_dir(char *dir, int save) | |
588 | { | |
589 | char *ret = curr_dir; | |
590 | static int initialised; | |
591 | ||
592 | if (!initialised) { | |
593 | initialised = 1; | |
594 | getcwd(curr_dir, sizeof(curr_dir)-1); | |
595 | } | |
596 | ||
597 | if (chdir(dir)) return NULL; | |
598 | ||
599 | if (save) { | |
600 | ret = strdup(curr_dir); | |
601 | } | |
602 | ||
603 | if (*dir == '/') { | |
604 | strlcpy(curr_dir, dir, sizeof(curr_dir)-1); | |
605 | } else { | |
606 | strlcat(curr_dir,"/", sizeof(curr_dir)-1); | |
607 | strlcat(curr_dir,dir, sizeof(curr_dir)-1); | |
608 | } | |
609 | ||
610 | clean_fname(curr_dir); | |
611 | ||
612 | return ret; | |
613 | } | |
614 | ||
615 | /* reverse a push_dir call */ | |
616 | int pop_dir(char *dir) | |
617 | { | |
618 | int ret; | |
619 | ||
620 | ret = chdir(dir); | |
621 | if (ret) { | |
622 | free(dir); | |
623 | return ret; | |
624 | } | |
625 | ||
626 | strlcpy(curr_dir, dir, sizeof(curr_dir)-1); | |
627 | ||
628 | free(dir); | |
629 | ||
630 | return 0; | |
631 | } | |
aa9b77a5 AT |
632 | |
633 | /* we need to supply our own strcmp function for file list comparisons | |
634 | to ensure that signed/unsigned usage is consistent between machines. */ | |
635 | int u_strcmp(const char *cs1, const char *cs2) | |
636 | { | |
637 | const uchar *s1 = (uchar *)cs1; | |
638 | const uchar *s2 = (uchar *)cs2; | |
639 | ||
640 | while (*s1 && *s2 && (*s1 == *s2)) { | |
641 | s1++; s2++; | |
642 | } | |
643 | ||
644 | return (int)*s1 - (int)*s2; | |
645 | } | |
eb86d661 | 646 | |
43b06eea | 647 | static OFF_T last_ofs; |
eb86d661 AT |
648 | |
649 | void end_progress(void) | |
650 | { | |
651 | extern int do_progress, am_server; | |
652 | ||
653 | if (do_progress && !am_server) { | |
654 | rprintf(FINFO,"\n"); | |
655 | } | |
43b06eea | 656 | last_ofs = 0; |
eb86d661 AT |
657 | } |
658 | ||
659 | void show_progress(OFF_T ofs, OFF_T size) | |
660 | { | |
661 | extern int do_progress, am_server; | |
662 | ||
663 | if (do_progress && !am_server) { | |
43b06eea AT |
664 | if (ofs > last_ofs + 1000) { |
665 | int pct = (int)((100.0*ofs)/size); | |
eb86d661 | 666 | rprintf(FINFO,"%.0f (%d%%)\r", (double)ofs, pct); |
43b06eea | 667 | last_ofs = ofs; |
eb86d661 AT |
668 | } |
669 | } | |
670 | } | |
4b957c22 AT |
671 | |
672 | /* determine if a symlink points outside the current directory tree */ | |
673 | int unsafe_symlink(char *dest, char *src) | |
674 | { | |
675 | char *tok; | |
676 | int depth = 0; | |
677 | ||
678 | /* all absolute and null symlinks are unsafe */ | |
679 | if (!dest || !(*dest) || (*dest == '/')) return 1; | |
680 | ||
681 | src = strdup(src); | |
682 | if (!src) out_of_memory("unsafe_symlink"); | |
683 | ||
684 | /* find out what our safety margin is */ | |
685 | for (tok=strtok(src,"/"); tok; tok=strtok(NULL,"/")) { | |
686 | if (strcmp(tok,"..") == 0) { | |
687 | depth=0; | |
688 | } else if (strcmp(tok,".") == 0) { | |
689 | /* nothing */ | |
690 | } else { | |
691 | depth++; | |
692 | } | |
693 | } | |
694 | free(src); | |
695 | ||
696 | /* drop by one to account for the filename portion */ | |
697 | depth--; | |
698 | ||
699 | dest = strdup(dest); | |
700 | if (!dest) out_of_memory("unsafe_symlink"); | |
701 | ||
702 | for (tok=strtok(dest,"/"); tok; tok=strtok(NULL,"/")) { | |
703 | if (strcmp(tok,"..") == 0) { | |
704 | depth--; | |
705 | } else if (strcmp(tok,".") == 0) { | |
706 | /* nothing */ | |
707 | } else { | |
708 | depth++; | |
709 | } | |
710 | /* if at any point we go outside the current directory then | |
711 | stop - it is unsafe */ | |
712 | if (depth < 0) break; | |
713 | } | |
714 | ||
715 | free(dest); | |
716 | return (depth < 0); | |
717 | } | |
375a4556 DD |
718 | |
719 | /* | |
720 | * Make path appear as if a chroot had occurred: | |
721 | * 1. remove leading "/" (or replace with "." if at end) | |
722 | * 2. remove leading ".." components | |
723 | * 3. delete any other "<dir>/.." (recursively) | |
724 | * Return a malloc'ed copy. | |
725 | * Contributed by Dave Dykstra <dwd@bell-labs.com> | |
726 | */ | |
727 | ||
728 | char *sanitize_path(char *p) | |
729 | { | |
730 | char *copy, *copyp; | |
731 | ||
732 | copy = (char *) malloc(strlen(p)+1); | |
733 | copyp = copy; | |
734 | while (*p != '\0') { | |
735 | if ((*p == '/') && (copyp == copy)) { | |
736 | /* remove leading slash */ | |
737 | p++; | |
738 | } | |
739 | else if ((*p == '.') && (*(p+1) == '.') && | |
740 | ((*(p+2) == '/') || (*(p+2) == '\0'))) { | |
741 | /* remove .. followed by slash or end */ | |
742 | p += 2; | |
743 | if (copyp != copy) { | |
744 | /* backup the copy one level */ | |
745 | while ((--copyp != copy) && (*copyp == '/')) | |
746 | /* skip trailing slashes */ | |
747 | ; | |
748 | while ((copyp != copy) && (*copyp != '/')) | |
749 | /* skip back through slash */ | |
750 | copyp--; | |
751 | } | |
752 | } else { | |
753 | /* copy one component */ | |
754 | while (1) { | |
755 | *copyp++ = *p++; | |
756 | if ((*p == '\0') || (*(p-1) == '/')) | |
757 | break; | |
758 | } | |
759 | } | |
760 | } | |
761 | *copyp = '\0'; | |
762 | return(copy); | |
763 | } | |
764 |