diff options
author | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-12-11 16:43:48 +0000 |
---|---|---|
committer | kraai <kraai@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-12-11 16:43:48 +0000 |
commit | b39867739edc8910b6f708133e0074ba44943d20 (patch) | |
tree | a526d078b760f343038f2f67eda043e3e9198613 | |
parent | 8332f12a43362c5006de9a7e34612cfa09666aa3 (diff) | |
download | busybox-w32-b39867739edc8910b6f708133e0074ba44943d20.tar.gz busybox-w32-b39867739edc8910b6f708133e0074ba44943d20.tar.bz2 busybox-w32-b39867739edc8910b6f708133e0074ba44943d20.zip |
Open the source before creating the destination.
git-svn-id: svn://busybox.net/trunk/busybox@3882 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | libbb/copy_file.c | 19 | ||||
-rw-r--r-- | testsuite/cp/cp-does-not-copy-unreadable-file | 4 |
2 files changed, 15 insertions, 8 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index a80e30b50..29778f2a4 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c | |||
@@ -132,21 +132,30 @@ int copy_file(const char *source, const char *dest, int flags) | |||
132 | } else if (S_ISREG(source_stat.st_mode)) { | 132 | } else if (S_ISREG(source_stat.st_mode)) { |
133 | FILE *sfp, *dfp=NULL; | 133 | FILE *sfp, *dfp=NULL; |
134 | 134 | ||
135 | if ((sfp = fopen(source, "r")) == NULL) { | ||
136 | perror_msg("unable to open `%s'", source); | ||
137 | return -1; | ||
138 | } | ||
139 | |||
135 | if (dest_exists) { | 140 | if (dest_exists) { |
136 | if (flags & FILEUTILS_INTERACTIVE) { | 141 | if (flags & FILEUTILS_INTERACTIVE) { |
137 | fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); | 142 | fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); |
138 | if (!ask_confirmation()) | 143 | if (!ask_confirmation()) { |
144 | fclose (sfp); | ||
139 | return 0; | 145 | return 0; |
146 | } | ||
140 | } | 147 | } |
141 | 148 | ||
142 | if ((dfp = fopen(dest, "w")) == NULL) { | 149 | if ((dfp = fopen(dest, "w")) == NULL) { |
143 | if (!(flags & FILEUTILS_FORCE)) { | 150 | if (!(flags & FILEUTILS_FORCE)) { |
144 | perror_msg("unable to open `%s'", dest); | 151 | perror_msg("unable to open `%s'", dest); |
152 | fclose (sfp); | ||
145 | return -1; | 153 | return -1; |
146 | } | 154 | } |
147 | 155 | ||
148 | if (unlink(dest) < 0) { | 156 | if (unlink(dest) < 0) { |
149 | perror_msg("unable to remove `%s'", dest); | 157 | perror_msg("unable to remove `%s'", dest); |
158 | fclose (sfp); | ||
150 | return -1; | 159 | return -1; |
151 | } | 160 | } |
152 | 161 | ||
@@ -162,17 +171,11 @@ int copy_file(const char *source, const char *dest, int flags) | |||
162 | if (fd >= 0) | 171 | if (fd >= 0) |
163 | close(fd); | 172 | close(fd); |
164 | perror_msg("unable to open `%s'", dest); | 173 | perror_msg("unable to open `%s'", dest); |
174 | fclose (sfp); | ||
165 | return -1; | 175 | return -1; |
166 | } | 176 | } |
167 | } | 177 | } |
168 | 178 | ||
169 | if ((sfp = fopen(source, "r")) == NULL) { | ||
170 | fclose(dfp); | ||
171 | perror_msg("unable to open `%s'", source); | ||
172 | status = -1; | ||
173 | goto end; | ||
174 | } | ||
175 | |||
176 | if (copy_file_chunk(sfp, dfp, -1) < 0) | 179 | if (copy_file_chunk(sfp, dfp, -1) < 0) |
177 | status = -1; | 180 | status = -1; |
178 | 181 | ||
diff --git a/testsuite/cp/cp-does-not-copy-unreadable-file b/testsuite/cp/cp-does-not-copy-unreadable-file new file mode 100644 index 000000000..68c576727 --- /dev/null +++ b/testsuite/cp/cp-does-not-copy-unreadable-file | |||
@@ -0,0 +1,4 @@ | |||
1 | touch foo | ||
2 | chmod a-r foo | ||
3 | busybox cp foo bar | ||
4 | test ! -f bar | ||