aboutsummaryrefslogtreecommitdiff
path: root/libbb/copy_file.c
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-12-11 16:43:48 +0000
committerMatt Kraai <kraai@debian.org>2001-12-11 16:43:48 +0000
commit14b7c5d12b9a492d148b744cf8300f47b53aafd0 (patch)
treea526d078b760f343038f2f67eda043e3e9198613 /libbb/copy_file.c
parent741f40b58edf3644c6bc8e6863ee9ad681b21562 (diff)
downloadbusybox-w32-14b7c5d12b9a492d148b744cf8300f47b53aafd0.tar.gz
busybox-w32-14b7c5d12b9a492d148b744cf8300f47b53aafd0.tar.bz2
busybox-w32-14b7c5d12b9a492d148b744cf8300f47b53aafd0.zip
Open the source before creating the destination.
Diffstat (limited to 'libbb/copy_file.c')
-rw-r--r--libbb/copy_file.c19
1 files changed, 11 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