diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-21 23:40:20 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-10-21 23:40:20 +0000 |
commit | 05e73015aac1bc64cd889062328598c224f190de (patch) | |
tree | a6d0fa74d59e67d1570c54157f5ebcc47b4522d0 | |
parent | 2f36b989836df0f58dada461a7d7da608182cc02 (diff) | |
download | busybox-w32-05e73015aac1bc64cd889062328598c224f190de.tar.gz busybox-w32-05e73015aac1bc64cd889062328598c224f190de.tar.bz2 busybox-w32-05e73015aac1bc64cd889062328598c224f190de.zip |
cp: add support for -s, -l. Fix free(nonmalloc) bug.
Add doc on POSIX's rules on -i and -f (insane!).
ln: make "ln dangling_symlink new_link" work.
git-svn-id: svn://busybox.net/trunk/busybox@16419 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | coreutils/cp.c | 72 | ||||
-rw-r--r-- | coreutils/libcoreutils/cp_mv_stat.c | 4 | ||||
-rw-r--r-- | coreutils/ln.c | 43 | ||||
-rw-r--r-- | coreutils/mv.c | 13 | ||||
-rw-r--r-- | include/libbb.h | 5 | ||||
-rw-r--r-- | libbb/copy_file.c | 210 |
6 files changed, 188 insertions, 159 deletions
diff --git a/coreutils/cp.c b/coreutils/cp.c index fabfe58e0..47ad85ecf 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c | |||
@@ -7,8 +7,6 @@ | |||
7 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. | 7 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
8 | */ | 8 | */ |
9 | 9 | ||
10 | /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */ | ||
11 | /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */ | ||
12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ | 10 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ |
13 | 11 | ||
14 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) | 12 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
@@ -16,15 +14,6 @@ | |||
16 | * Size reduction. | 14 | * Size reduction. |
17 | */ | 15 | */ |
18 | 16 | ||
19 | #include <sys/types.h> | ||
20 | #include <sys/stat.h> | ||
21 | #include <unistd.h> | ||
22 | #include <fcntl.h> | ||
23 | #include <utime.h> | ||
24 | #include <errno.h> | ||
25 | #include <dirent.h> | ||
26 | #include <stdlib.h> | ||
27 | #include <assert.h> | ||
28 | #include "busybox.h" | 17 | #include "busybox.h" |
29 | #include "libcoreutils/coreutils.h" | 18 | #include "libcoreutils/coreutils.h" |
30 | 19 | ||
@@ -38,31 +27,26 @@ int cp_main(int argc, char **argv) | |||
38 | int d_flags; | 27 | int d_flags; |
39 | int flags; | 28 | int flags; |
40 | int status = 0; | 29 | int status = 0; |
30 | enum { | ||
31 | OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1), | ||
32 | OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)), | ||
33 | OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1), | ||
34 | OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), | ||
35 | OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3), | ||
36 | }; | ||
41 | 37 | ||
42 | flags = getopt32(argc, argv, "pdRfiarPHL"); | 38 | // Soft- and hardlinking don't mix |
43 | 39 | // -P and -d are the same (-P is POSIX, -d is GNU) | |
44 | if (flags & 32) { | 40 | // -r and -R are the same |
45 | flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE); | 41 | // -a = -pdR |
46 | } | 42 | opt_complementary = "?:l--s:s--l:Pd:rR:apdR"; |
47 | if (flags & 64) { | 43 | flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL"); |
48 | /* Make -r a synonym for -R, | ||
49 | * -r was marked as obsolete in SUSv3, but is included for compatibility | ||
50 | */ | ||
51 | flags |= FILEUTILS_RECUR; | ||
52 | } | ||
53 | if (flags & 128) { | ||
54 | /* Make -P a synonym for -d, | ||
55 | * -d is the GNU option while -P is the POSIX 2003 option | ||
56 | */ | ||
57 | flags |= FILEUTILS_DEREFERENCE; | ||
58 | } | ||
59 | /* Default behavior of cp is to dereference, so we don't have to do | 44 | /* Default behavior of cp is to dereference, so we don't have to do |
60 | * anything special when we are given -L. | 45 | * anything special when we are given -L. |
61 | * The behavior of -H is *almost* like -L, but not quite, so let's | 46 | * The behavior of -H is *almost* like -L, but not quite, so let's |
62 | * just ignore it too for fun. | 47 | * just ignore it too for fun. |
63 | if (flags & 256 || flags & 512) { | 48 | if (flags & OPT_L) ... |
64 | ; | 49 | if (flags & OPT_H) ... // deref command-line params only |
65 | } | ||
66 | */ | 50 | */ |
67 | 51 | ||
68 | flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ | 52 | flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ |
@@ -78,33 +62,31 @@ int cp_main(int argc, char **argv) | |||
78 | if (optind + 2 == argc) { | 62 | if (optind + 2 == argc) { |
79 | s_flags = cp_mv_stat2(*argv, &source_stat, | 63 | s_flags = cp_mv_stat2(*argv, &source_stat, |
80 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); | 64 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
81 | if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) { | 65 | if (s_flags < 0) |
82 | exit(EXIT_FAILURE); | 66 | return EXIT_FAILURE; |
83 | } | 67 | d_flags = cp_mv_stat(last, &dest_stat); |
68 | if (d_flags < 0) | ||
69 | return EXIT_FAILURE; | ||
70 | |||
84 | /* ...if neither is a directory or... */ | 71 | /* ...if neither is a directory or... */ |
85 | if ( !((s_flags | d_flags) & 2) || | 72 | if ( !((s_flags | d_flags) & 2) || |
86 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ | 73 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
87 | /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */ | 74 | ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) |
88 | /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */ | ||
89 | ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags) | ||
90 | ) { | 75 | ) { |
91 | /* ...do a simple copy. */ | 76 | /* ...do a simple copy. */ |
92 | dest = last; | 77 | dest = xstrdup(last); |
93 | goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */ | 78 | goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */ |
94 | } | 79 | } |
95 | } | 80 | } |
96 | 81 | ||
97 | do { | 82 | do { |
98 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); | 83 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
99 | DO_COPY: | 84 | DO_COPY: |
100 | if (copy_file(*argv, dest, flags) < 0) { | 85 | if (copy_file(*argv, dest, flags) < 0) { |
101 | status = 1; | 86 | status = 1; |
102 | } | 87 | } |
103 | if (*++argv == last) { | 88 | free((void*)dest); |
104 | break; | 89 | } while (*++argv != last); |
105 | } | ||
106 | free((void *) dest); | ||
107 | } while (1); | ||
108 | 90 | ||
109 | exit(status); | 91 | return status; |
110 | } | 92 | } |
diff --git a/coreutils/libcoreutils/cp_mv_stat.c b/coreutils/libcoreutils/cp_mv_stat.c index d401bcc75..0849ebc6c 100644 --- a/coreutils/libcoreutils/cp_mv_stat.c +++ b/coreutils/libcoreutils/cp_mv_stat.c | |||
@@ -20,8 +20,6 @@ | |||
20 | * | 20 | * |
21 | */ | 21 | */ |
22 | 22 | ||
23 | #include <errno.h> | ||
24 | #include <sys/stat.h> | ||
25 | #include "libbb.h" | 23 | #include "libbb.h" |
26 | #include "coreutils.h" | 24 | #include "coreutils.h" |
27 | 25 | ||
@@ -29,7 +27,7 @@ int cp_mv_stat2(const char *fn, struct stat *fn_stat, stat_func sf) | |||
29 | { | 27 | { |
30 | if (sf(fn, fn_stat) < 0) { | 28 | if (sf(fn, fn_stat) < 0) { |
31 | if (errno != ENOENT) { | 29 | if (errno != ENOENT) { |
32 | bb_perror_msg("unable to stat `%s'", fn); | 30 | bb_perror_msg("unable to stat '%s'", fn); |
33 | return -1; | 31 | return -1; |
34 | } | 32 | } |
35 | return 0; | 33 | return 0; |
diff --git a/coreutils/ln.c b/coreutils/ln.c index cd6e470be..231a3bf03 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c | |||
@@ -49,36 +49,39 @@ int ln_main(int argc, char **argv) | |||
49 | src = last; | 49 | src = last; |
50 | 50 | ||
51 | if (is_directory(src, | 51 | if (is_directory(src, |
52 | (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, | 52 | (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, |
53 | NULL)) { | 53 | NULL)) { |
54 | src_name = xstrdup(*argv); | 54 | src_name = xstrdup(*argv); |
55 | src = concat_path_file(src, bb_get_last_path_component(src_name)); | 55 | src = concat_path_file(src, bb_get_last_path_component(src_name)); |
56 | free(src_name); | 56 | free(src_name); |
57 | src_name = src; | 57 | src_name = src; |
58 | } | 58 | } |
59 | if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { | 59 | if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { |
60 | bb_perror_msg("%s", *argv); | 60 | // coreutils: "ln dangling_symlink new_hardlink" works |
61 | status = EXIT_FAILURE; | 61 | if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) { |
62 | free(src_name); | 62 | bb_perror_msg("%s", *argv); |
63 | continue; | 63 | status = EXIT_FAILURE; |
64 | free(src_name); | ||
65 | continue; | ||
66 | } | ||
64 | } | 67 | } |
65 | 68 | ||
66 | if (flag & LN_BACKUP) { | 69 | if (flag & LN_BACKUP) { |
67 | char *backup; | 70 | char *backup; |
68 | backup = xasprintf("%s%s", src, suffix); | 71 | backup = xasprintf("%s%s", src, suffix); |
69 | if (rename(src, backup) < 0 && errno != ENOENT) { | 72 | if (rename(src, backup) < 0 && errno != ENOENT) { |
70 | bb_perror_msg("%s", src); | 73 | bb_perror_msg("%s", src); |
71 | status = EXIT_FAILURE; | 74 | status = EXIT_FAILURE; |
72 | free(backup); | ||
73 | continue; | ||
74 | } | ||
75 | free(backup); | 75 | free(backup); |
76 | /* | 76 | continue; |
77 | * When the source and dest are both hard links to the same | 77 | } |
78 | * inode, a rename may succeed even though nothing happened. | 78 | free(backup); |
79 | * Therefore, always unlink(). | 79 | /* |
80 | */ | 80 | * When the source and dest are both hard links to the same |
81 | unlink(src); | 81 | * inode, a rename may succeed even though nothing happened. |
82 | * Therefore, always unlink(). | ||
83 | */ | ||
84 | unlink(src); | ||
82 | } else if (flag & LN_FORCE) { | 85 | } else if (flag & LN_FORCE) { |
83 | unlink(src); | 86 | unlink(src); |
84 | } | 87 | } |
diff --git a/coreutils/mv.c b/coreutils/mv.c index 770b42417..52ddd9fb7 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
@@ -57,7 +57,8 @@ int mv_main(int argc, char **argv) | |||
57 | argv += optind; | 57 | argv += optind; |
58 | 58 | ||
59 | if (optind + 2 == argc) { | 59 | if (optind + 2 == argc) { |
60 | if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) { | 60 | dest_exists = cp_mv_stat(last, &dest_stat); |
61 | if (dest_exists < 0) { | ||
61 | return 1; | 62 | return 1; |
62 | } | 63 | } |
63 | 64 | ||
@@ -69,8 +70,8 @@ int mv_main(int argc, char **argv) | |||
69 | 70 | ||
70 | do { | 71 | do { |
71 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); | 72 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); |
72 | 73 | dest_exists = cp_mv_stat(dest, &dest_stat); | |
73 | if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) { | 74 | if (dest_exists < 0) { |
74 | goto RET_1; | 75 | goto RET_1; |
75 | } | 76 | } |
76 | 77 | ||
@@ -79,7 +80,7 @@ DO_MOVE: | |||
79 | if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) && | 80 | if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) && |
80 | ((access(dest, W_OK) < 0 && isatty(0)) || | 81 | ((access(dest, W_OK) < 0 && isatty(0)) || |
81 | (flags & OPT_FILEUTILS_INTERACTIVE))) { | 82 | (flags & OPT_FILEUTILS_INTERACTIVE))) { |
82 | if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) { | 83 | if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) { |
83 | goto RET_1; /* Ouch! fprintf failed! */ | 84 | goto RET_1; /* Ouch! fprintf failed! */ |
84 | } | 85 | } |
85 | if (!bb_ask_confirmation()) { | 86 | if (!bb_ask_confirmation()) { |
@@ -92,7 +93,7 @@ DO_MOVE: | |||
92 | 93 | ||
93 | if (errno != EXDEV || | 94 | if (errno != EXDEV || |
94 | (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) { | 95 | (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) { |
95 | bb_perror_msg("unable to rename `%s'", *argv); | 96 | bb_perror_msg("cannot rename '%s'", *argv); |
96 | } else { | 97 | } else { |
97 | if (dest_exists) { | 98 | if (dest_exists) { |
98 | if (dest_exists == 3) { | 99 | if (dest_exists == 3) { |
@@ -107,7 +108,7 @@ DO_MOVE: | |||
107 | } | 108 | } |
108 | } | 109 | } |
109 | if (unlink(dest) < 0) { | 110 | if (unlink(dest) < 0) { |
110 | bb_perror_msg("cannot remove `%s'", dest); | 111 | bb_perror_msg("cannot remove '%s'", dest); |
111 | goto RET_1; | 112 | goto RET_1; |
112 | } | 113 | } |
113 | } | 114 | } |
diff --git a/include/libbb.h b/include/libbb.h index b1ddbb0c0..3fa49728f 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -473,8 +473,11 @@ enum { /* DO NOT CHANGE THESE VALUES! cp.c depends on them. */ | |||
473 | FILEUTILS_DEREFERENCE = 2, | 473 | FILEUTILS_DEREFERENCE = 2, |
474 | FILEUTILS_RECUR = 4, | 474 | FILEUTILS_RECUR = 4, |
475 | FILEUTILS_FORCE = 8, | 475 | FILEUTILS_FORCE = 8, |
476 | FILEUTILS_INTERACTIVE = 16 | 476 | FILEUTILS_INTERACTIVE = 0x10, |
477 | FILEUTILS_MAKE_HARDLINK = 0x20, | ||
478 | FILEUTILS_MAKE_SOFTLINK = 0x40, | ||
477 | }; | 479 | }; |
480 | #define FILEUTILS_CP_OPTSTR "pdRfils" | ||
478 | 481 | ||
479 | extern const char *applet_name; | 482 | extern const char *applet_name; |
480 | 483 | ||
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index bd9c9f7a2..0135831fe 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c | |||
@@ -10,31 +10,53 @@ | |||
10 | 10 | ||
11 | #include "libbb.h" | 11 | #include "libbb.h" |
12 | 12 | ||
13 | static int retry_overwrite(const char *dest, int flags) | ||
14 | { | ||
15 | if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) { | ||
16 | fprintf(stderr, "'%s' exists\n", dest); | ||
17 | return -1; | ||
18 | } | ||
19 | if (flags & FILEUTILS_INTERACTIVE) { | ||
20 | fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest); | ||
21 | if (!bb_ask_confirmation()) | ||
22 | return 0; // not allowed to overwrite | ||
23 | } | ||
24 | if (unlink(dest) < 0) { | ||
25 | bb_perror_msg("cannot remove '%s'", dest); | ||
26 | return -1; // error | ||
27 | } | ||
28 | return 1; // ok (to try again) | ||
29 | } | ||
30 | |||
13 | int copy_file(const char *source, const char *dest, int flags) | 31 | int copy_file(const char *source, const char *dest, int flags) |
14 | { | 32 | { |
15 | struct stat source_stat; | 33 | struct stat source_stat; |
16 | struct stat dest_stat; | 34 | struct stat dest_stat; |
17 | int dest_exists = 0; | ||
18 | int status = 0; | 35 | int status = 0; |
36 | signed char dest_exists = 0; | ||
37 | signed char ovr; | ||
19 | 38 | ||
20 | if ((!(flags & FILEUTILS_DEREFERENCE) && | 39 | #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE) |
21 | lstat(source, &source_stat) < 0) || | 40 | |
22 | ((flags & FILEUTILS_DEREFERENCE) && | 41 | if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) { |
23 | stat(source, &source_stat) < 0)) { | 42 | // This may be a dangling symlink. |
24 | bb_perror_msg("%s", source); | 43 | // Making [sym]links to dangling symlinks works, so... |
44 | if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) | ||
45 | goto make_links; | ||
46 | bb_perror_msg("cannot stat '%s'", source); | ||
25 | return -1; | 47 | return -1; |
26 | } | 48 | } |
27 | 49 | ||
28 | if (lstat(dest, &dest_stat) < 0) { | 50 | if (lstat(dest, &dest_stat) < 0) { |
29 | if (errno != ENOENT) { | 51 | if (errno != ENOENT) { |
30 | bb_perror_msg("unable to stat `%s'", dest); | 52 | bb_perror_msg("cannot stat '%s'", dest); |
31 | return -1; | 53 | return -1; |
32 | } | 54 | } |
33 | } else { | 55 | } else { |
34 | if (source_stat.st_dev == dest_stat.st_dev && | 56 | if (source_stat.st_dev == dest_stat.st_dev |
35 | source_stat.st_ino == dest_stat.st_ino) | 57 | && source_stat.st_ino == dest_stat.st_ino |
36 | { | 58 | ) { |
37 | bb_error_msg("`%s' and `%s' are the same file", source, dest); | 59 | bb_error_msg("'%s' and '%s' are the same file", source, dest); |
38 | return -1; | 60 | return -1; |
39 | } | 61 | } |
40 | dest_exists = 1; | 62 | dest_exists = 1; |
@@ -46,14 +68,14 @@ int copy_file(const char *source, const char *dest, int flags) | |||
46 | mode_t saved_umask = 0; | 68 | mode_t saved_umask = 0; |
47 | 69 | ||
48 | if (!(flags & FILEUTILS_RECUR)) { | 70 | if (!(flags & FILEUTILS_RECUR)) { |
49 | bb_error_msg("%s: omitting directory", source); | 71 | bb_error_msg("omitting directory '%s'", source); |
50 | return -1; | 72 | return -1; |
51 | } | 73 | } |
52 | 74 | ||
53 | /* Create DEST. */ | 75 | /* Create DEST. */ |
54 | if (dest_exists) { | 76 | if (dest_exists) { |
55 | if (!S_ISDIR(dest_stat.st_mode)) { | 77 | if (!S_ISDIR(dest_stat.st_mode)) { |
56 | bb_error_msg("`%s' is not a directory", dest); | 78 | bb_error_msg("target '%s' is not a directory", dest); |
57 | return -1; | 79 | return -1; |
58 | } | 80 | } |
59 | } else { | 81 | } else { |
@@ -67,7 +89,7 @@ int copy_file(const char *source, const char *dest, int flags) | |||
67 | 89 | ||
68 | if (mkdir(dest, mode) < 0) { | 90 | if (mkdir(dest, mode) < 0) { |
69 | umask(saved_umask); | 91 | umask(saved_umask); |
70 | bb_perror_msg("cannot create directory `%s'", dest); | 92 | bb_perror_msg("cannot create directory '%s'", dest); |
71 | return -1; | 93 | return -1; |
72 | } | 94 | } |
73 | 95 | ||
@@ -75,7 +97,8 @@ int copy_file(const char *source, const char *dest, int flags) | |||
75 | } | 97 | } |
76 | 98 | ||
77 | /* Recursively copy files in SOURCE. */ | 99 | /* Recursively copy files in SOURCE. */ |
78 | if ((dp = opendir(source)) == NULL) { | 100 | dp = opendir(source); |
101 | if (dp == NULL) { | ||
79 | status = -1; | 102 | status = -1; |
80 | goto preserve_status; | 103 | goto preserve_status; |
81 | } | 104 | } |
@@ -84,7 +107,7 @@ int copy_file(const char *source, const char *dest, int flags) | |||
84 | char *new_source, *new_dest; | 107 | char *new_source, *new_dest; |
85 | 108 | ||
86 | new_source = concat_subpath_file(source, d->d_name); | 109 | new_source = concat_subpath_file(source, d->d_name); |
87 | if(new_source == NULL) | 110 | if (new_source == NULL) |
88 | continue; | 111 | continue; |
89 | new_dest = concat_path_file(dest, d->d_name); | 112 | new_dest = concat_path_file(dest, d->d_name); |
90 | if (copy_file(new_source, new_dest, flags) < 0) | 113 | if (copy_file(new_source, new_dest, flags) < 0) |
@@ -92,103 +115,119 @@ int copy_file(const char *source, const char *dest, int flags) | |||
92 | free(new_source); | 115 | free(new_source); |
93 | free(new_dest); | 116 | free(new_dest); |
94 | } | 117 | } |
95 | /* closedir have only EBADF error, but "dp" not changes */ | ||
96 | closedir(dp); | 118 | closedir(dp); |
97 | 119 | ||
98 | if (!dest_exists && | 120 | if (!dest_exists |
99 | chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { | 121 | && chmod(dest, source_stat.st_mode & ~saved_umask) < 0 |
100 | bb_perror_msg("unable to change permissions of `%s'", dest); | 122 | ) { |
123 | bb_perror_msg("cannot change permissions of '%s'", dest); | ||
101 | status = -1; | 124 | status = -1; |
102 | } | 125 | } |
103 | } else if (S_ISREG(source_stat.st_mode) || | 126 | |
104 | (S_ISLNK(source_stat.st_mode) && (flags & FILEUTILS_DEREFERENCE))) | 127 | } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { |
105 | { | 128 | int (*lf)(const char *oldpath, const char *newpath); |
129 | make_links: | ||
130 | // Hmm... maybe | ||
131 | // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ? | ||
132 | // (but realpath returns NULL on dangling symlinks...) | ||
133 | lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link; | ||
134 | if (lf(source, dest) < 0) { | ||
135 | ovr = retry_overwrite(dest, flags); | ||
136 | if (ovr <= 0) | ||
137 | return ovr; | ||
138 | if (lf(source, dest) < 0) { | ||
139 | bb_perror_msg("cannot create link '%s'", dest); | ||
140 | return -1; | ||
141 | } | ||
142 | } | ||
143 | return 0; | ||
144 | |||
145 | } else if (S_ISREG(source_stat.st_mode) | ||
146 | // Huh? DEREF uses stat, which never returns links IIRC... | ||
147 | || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) | ||
148 | ) { | ||
106 | int src_fd; | 149 | int src_fd; |
107 | int dst_fd; | 150 | int dst_fd; |
108 | if (ENABLE_FEATURE_PRESERVE_HARDLINKS) { | 151 | if (ENABLE_FEATURE_PRESERVE_HARDLINKS) { |
109 | char *link_name; | 152 | char *link_name; |
110 | 153 | ||
111 | if (!(flags & FILEUTILS_DEREFERENCE) && | 154 | if (!FLAGS_DEREF |
112 | is_in_ino_dev_hashtable(&source_stat, &link_name)) { | 155 | && is_in_ino_dev_hashtable(&source_stat, &link_name) |
156 | ) { | ||
113 | if (link(link_name, dest) < 0) { | 157 | if (link(link_name, dest) < 0) { |
114 | bb_perror_msg("unable to link `%s'", dest); | 158 | ovr = retry_overwrite(dest, flags); |
115 | return -1; | 159 | if (ovr <= 0) |
160 | return ovr; | ||
161 | if (link(link_name, dest) < 0) { | ||
162 | bb_perror_msg("cannot create link '%s'", dest); | ||
163 | return -1; | ||
164 | } | ||
116 | } | 165 | } |
117 | |||
118 | return 0; | 166 | return 0; |
119 | } | 167 | } |
168 | // TODO: probably is_in_.. and add_to_... | ||
169 | // can be combined: find_or_add_... | ||
120 | add_to_ino_dev_hashtable(&source_stat, dest); | 170 | add_to_ino_dev_hashtable(&source_stat, dest); |
121 | } | 171 | } |
172 | |||
122 | src_fd = open(source, O_RDONLY); | 173 | src_fd = open(source, O_RDONLY); |
123 | if (src_fd == -1) { | 174 | if (src_fd == -1) { |
124 | bb_perror_msg("unable to open `%s'", source); | 175 | bb_perror_msg("cannot open '%s'", source); |
125 | return(-1); | 176 | return -1; |
126 | } | 177 | } |
127 | 178 | ||
128 | if (dest_exists) { | 179 | // POSIX: if exists and -i, ask (w/o -i assume yes). |
129 | if (flags & FILEUTILS_INTERACTIVE) { | 180 | // Then open w/o EXCL. |
130 | fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); | 181 | // If open still fails and -f, try unlink, then try open again. |
131 | if (!bb_ask_confirmation()) { | 182 | // Result: a mess: |
132 | close (src_fd); | 183 | // If dest is a softlink, we overwrite softlink's destination! |
133 | return 0; | 184 | // (or fail, if it points to dir/nonexistent location/etc). |
134 | } | 185 | // This is strange, but POSIX-correct. |
135 | } | 186 | // coreutils cp has --remove-destination to override this... |
136 | 187 | dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE) | |
137 | dst_fd = open(dest, O_WRONLY|O_TRUNC); | 188 | ? O_WRONLY|O_CREAT|O_TRUNC|O_EXCL |
138 | if (dst_fd == -1) { | 189 | : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); |
139 | if (!(flags & FILEUTILS_FORCE)) { | 190 | if (dst_fd == -1) { |
140 | bb_perror_msg("unable to open `%s'", dest); | 191 | // We would not do POSIX insanity. -i asks, |
141 | close(src_fd); | 192 | // then _unlinks_ the offender. Presto. |
142 | return -1; | 193 | // Or else we will end up having 3 open()s! |
143 | } | 194 | ovr = retry_overwrite(dest, flags); |
144 | 195 | if (ovr <= 0) { | |
145 | if (unlink(dest) < 0) { | 196 | close(src_fd); |
146 | bb_perror_msg("unable to remove `%s'", dest); | 197 | return ovr; |
147 | close(src_fd); | ||
148 | return -1; | ||
149 | } | ||
150 | |||
151 | goto dest_removed; | ||
152 | } | 198 | } |
153 | } else { | 199 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); |
154 | dest_removed: | ||
155 | dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode); | ||
156 | if (dst_fd == -1) { | 200 | if (dst_fd == -1) { |
157 | bb_perror_msg("unable to open `%s'", dest); | 201 | bb_perror_msg("cannot open '%s'", dest); |
158 | close(src_fd); | 202 | close(src_fd); |
159 | return(-1); | 203 | return -1; |
160 | } | 204 | } |
161 | } | 205 | } |
162 | 206 | ||
163 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) | 207 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
164 | status = -1; | 208 | status = -1; |
165 | |||
166 | if (close(dst_fd) < 0) { | 209 | if (close(dst_fd) < 0) { |
167 | bb_perror_msg("unable to close `%s'", dest); | 210 | bb_perror_msg("cannot close '%s'", dest); |
168 | status = -1; | 211 | status = -1; |
169 | } | 212 | } |
170 | |||
171 | if (close(src_fd) < 0) { | 213 | if (close(src_fd) < 0) { |
172 | bb_perror_msg("unable to close `%s'", source); | 214 | bb_perror_msg("cannot close '%s'", source); |
173 | status = -1; | 215 | status = -1; |
174 | } | 216 | } |
175 | } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || | ||
176 | S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) || | ||
177 | S_ISLNK(source_stat.st_mode)) { | ||
178 | 217 | ||
218 | } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) | ||
219 | || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) | ||
220 | || S_ISLNK(source_stat.st_mode) | ||
221 | ) { | ||
222 | // We are lazy here, a bit lax with races... | ||
179 | if (dest_exists) { | 223 | if (dest_exists) { |
180 | if((flags & FILEUTILS_FORCE) == 0) { | 224 | ovr = retry_overwrite(dest, flags); |
181 | fprintf(stderr, "`%s' exists\n", dest); | 225 | if (ovr <= 0) |
182 | return -1; | 226 | return ovr; |
183 | } | ||
184 | if(unlink(dest) < 0) { | ||
185 | bb_perror_msg("unable to remove `%s'", dest); | ||
186 | return -1; | ||
187 | } | ||
188 | } | 227 | } |
189 | if (S_ISFIFO(source_stat.st_mode)) { | 228 | if (S_ISFIFO(source_stat.st_mode)) { |
190 | if (mkfifo(dest, source_stat.st_mode) < 0) { | 229 | if (mkfifo(dest, source_stat.st_mode) < 0) { |
191 | bb_perror_msg("cannot create fifo `%s'", dest); | 230 | bb_perror_msg("cannot create fifo '%s'", dest); |
192 | return -1; | 231 | return -1; |
193 | } | 232 | } |
194 | } else if (S_ISLNK(source_stat.st_mode)) { | 233 | } else if (S_ISLNK(source_stat.st_mode)) { |
@@ -196,20 +235,21 @@ dest_removed: | |||
196 | 235 | ||
197 | lpath = xreadlink(source); | 236 | lpath = xreadlink(source); |
198 | if (symlink(lpath, dest) < 0) { | 237 | if (symlink(lpath, dest) < 0) { |
199 | bb_perror_msg("cannot create symlink `%s'", dest); | 238 | bb_perror_msg("cannot create symlink '%s'", dest); |
239 | free(lpath); | ||
200 | return -1; | 240 | return -1; |
201 | } | 241 | } |
202 | free(lpath); | 242 | free(lpath); |
203 | 243 | ||
204 | if (flags & FILEUTILS_PRESERVE_STATUS) | 244 | if (flags & FILEUTILS_PRESERVE_STATUS) |
205 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) | 245 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) |
206 | bb_perror_msg("unable to preserve ownership of `%s'", dest); | 246 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
207 | 247 | ||
208 | return 0; | 248 | return 0; |
209 | 249 | ||
210 | } else { | 250 | } else { |
211 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { | 251 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
212 | bb_perror_msg("unable to create `%s'", dest); | 252 | bb_perror_msg("cannot create '%s'", dest); |
213 | return -1; | 253 | return -1; |
214 | } | 254 | } |
215 | } | 255 | } |
@@ -218,22 +258,24 @@ dest_removed: | |||
218 | return -1; | 258 | return -1; |
219 | } | 259 | } |
220 | 260 | ||
221 | preserve_status: | 261 | preserve_status: |
222 | 262 | ||
223 | if (flags & FILEUTILS_PRESERVE_STATUS) { | 263 | if (flags & FILEUTILS_PRESERVE_STATUS |
264 | /* Cannot happen: */ | ||
265 | /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ | ||
266 | ) { | ||
224 | struct utimbuf times; | 267 | struct utimbuf times; |
225 | char *msg="unable to preserve %s of `%s'"; | ||
226 | 268 | ||
227 | times.actime = source_stat.st_atime; | 269 | times.actime = source_stat.st_atime; |
228 | times.modtime = source_stat.st_mtime; | 270 | times.modtime = source_stat.st_mtime; |
229 | if (utime(dest, ×) < 0) | 271 | if (utime(dest, ×) < 0) |
230 | bb_perror_msg(msg, "times", dest); | 272 | bb_perror_msg("cannot preserve %s of '%s'", "times", dest); |
231 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { | 273 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
232 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); | 274 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
233 | bb_perror_msg(msg, "ownership", dest); | 275 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
234 | } | 276 | } |
235 | if (chmod(dest, source_stat.st_mode) < 0) | 277 | if (chmod(dest, source_stat.st_mode) < 0) |
236 | bb_perror_msg(msg, "permissions", dest); | 278 | bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); |
237 | } | 279 | } |
238 | 280 | ||
239 | return status; | 281 | return status; |