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 /libbb | |
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
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/copy_file.c | 210 |
1 files changed, 126 insertions, 84 deletions
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; |