Open the source before creating the destination.

This commit is contained in:
Matt Kraai 2001-12-11 16:43:48 +00:00
parent 741f40b58e
commit 14b7c5d12b
2 changed files with 15 additions and 8 deletions

View File

@ -132,21 +132,30 @@ int copy_file(const char *source, const char *dest, int flags)
} else if (S_ISREG(source_stat.st_mode)) { } else if (S_ISREG(source_stat.st_mode)) {
FILE *sfp, *dfp=NULL; FILE *sfp, *dfp=NULL;
if ((sfp = fopen(source, "r")) == NULL) {
perror_msg("unable to open `%s'", source);
return -1;
}
if (dest_exists) { if (dest_exists) {
if (flags & FILEUTILS_INTERACTIVE) { if (flags & FILEUTILS_INTERACTIVE) {
fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
if (!ask_confirmation()) if (!ask_confirmation()) {
fclose (sfp);
return 0; return 0;
} }
}
if ((dfp = fopen(dest, "w")) == NULL) { if ((dfp = fopen(dest, "w")) == NULL) {
if (!(flags & FILEUTILS_FORCE)) { if (!(flags & FILEUTILS_FORCE)) {
perror_msg("unable to open `%s'", dest); perror_msg("unable to open `%s'", dest);
fclose (sfp);
return -1; return -1;
} }
if (unlink(dest) < 0) { if (unlink(dest) < 0) {
perror_msg("unable to remove `%s'", dest); perror_msg("unable to remove `%s'", dest);
fclose (sfp);
return -1; return -1;
} }
@ -162,17 +171,11 @@ int copy_file(const char *source, const char *dest, int flags)
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
perror_msg("unable to open `%s'", dest); perror_msg("unable to open `%s'", dest);
fclose (sfp);
return -1; return -1;
} }
} }
if ((sfp = fopen(source, "r")) == NULL) {
fclose(dfp);
perror_msg("unable to open `%s'", source);
status = -1;
goto end;
}
if (copy_file_chunk(sfp, dfp, -1) < 0) if (copy_file_chunk(sfp, dfp, -1) < 0)
status = -1; status = -1;

View File

@ -0,0 +1,4 @@
touch foo
chmod a-r foo
busybox cp foo bar
test ! -f bar