diff options
| author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-12-20 04:38:01 +0000 |
|---|---|---|
| committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-12-20 04:38:01 +0000 |
| commit | f62ea20affa0876756463f909d2d549d44736a26 (patch) | |
| tree | 07405469c301d355c574cb5c9acb00895aa69af4 /libbb | |
| parent | 266c1f5eff52fa211774733c92566102016dd1b6 (diff) | |
| download | busybox-w32-f62ea20affa0876756463f909d2d549d44736a26.tar.gz busybox-w32-f62ea20affa0876756463f909d2d549d44736a26.tar.bz2 busybox-w32-f62ea20affa0876756463f909d2d549d44736a26.zip | |
Use low level file descriptors to match bb_copyfd_eof
Diffstat (limited to 'libbb')
| -rw-r--r-- | libbb/copy_file.c | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index c9d239f9a..ffdb8242b 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c | |||
| @@ -123,7 +123,8 @@ int copy_file(const char *source, const char *dest, int flags) | |||
| 123 | status = -1; | 123 | status = -1; |
| 124 | } | 124 | } |
| 125 | } else if (S_ISREG(source_stat.st_mode)) { | 125 | } else if (S_ISREG(source_stat.st_mode)) { |
| 126 | FILE *sfp, *dfp=NULL; | 126 | int src_fd; |
| 127 | int dst_fd; | ||
| 127 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS | 128 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
| 128 | char *link_name; | 129 | char *link_name; |
| 129 | 130 | ||
| @@ -137,30 +138,32 @@ int copy_file(const char *source, const char *dest, int flags) | |||
| 137 | return 0; | 138 | return 0; |
| 138 | } | 139 | } |
| 139 | #endif | 140 | #endif |
| 140 | 141 | src_fd = open(source, O_RDONLY); | |
| 141 | if ((sfp = bb_wfopen(source, "r")) == NULL) { | 142 | if (src_fd == -1) { |
| 142 | return -1; | 143 | bb_perror_msg("unable to open `%s'", source); |
| 144 | return(-1); | ||
| 143 | } | 145 | } |
| 144 | 146 | ||
| 145 | if (dest_exists) { | 147 | if (dest_exists) { |
| 146 | if (flags & FILEUTILS_INTERACTIVE) { | 148 | if (flags & FILEUTILS_INTERACTIVE) { |
| 147 | fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest); | 149 | bb_error_msg("overwrite `%s'? ", dest); |
| 148 | if (!bb_ask_confirmation()) { | 150 | if (!bb_ask_confirmation()) { |
| 149 | fclose (sfp); | 151 | close (src_fd); |
| 150 | return 0; | 152 | return 0; |
| 151 | } | 153 | } |
| 152 | } | 154 | } |
| 153 | 155 | ||
| 154 | if ((dfp = fopen(dest, "w")) == NULL) { | 156 | dst_fd = open(dest, O_WRONLY); |
| 157 | if (dst_fd == -1) { | ||
| 155 | if (!(flags & FILEUTILS_FORCE)) { | 158 | if (!(flags & FILEUTILS_FORCE)) { |
| 156 | bb_perror_msg("unable to open `%s'", dest); | 159 | bb_perror_msg("unable to open `%s'", dest); |
| 157 | fclose (sfp); | 160 | close(src_fd); |
| 158 | return -1; | 161 | return -1; |
| 159 | } | 162 | } |
| 160 | 163 | ||
| 161 | if (unlink(dest) < 0) { | 164 | if (unlink(dest) < 0) { |
| 162 | bb_perror_msg("unable to remove `%s'", dest); | 165 | bb_perror_msg("unable to remove `%s'", dest); |
| 163 | fclose (sfp); | 166 | close(src_fd); |
| 164 | return -1; | 167 | return -1; |
| 165 | } | 168 | } |
| 166 | 169 | ||
| @@ -169,27 +172,23 @@ int copy_file(const char *source, const char *dest, int flags) | |||
| 169 | } | 172 | } |
| 170 | 173 | ||
| 171 | if (!dest_exists) { | 174 | if (!dest_exists) { |
| 172 | int fd; | 175 | dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode); |
| 173 | 176 | if (dst_fd == -1) { | |
| 174 | if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || | ||
| 175 | (dfp = fdopen(fd, "w")) == NULL) { | ||
| 176 | if (fd >= 0) | ||
| 177 | close(fd); | ||
| 178 | bb_perror_msg("unable to open `%s'", dest); | 177 | bb_perror_msg("unable to open `%s'", dest); |
| 179 | fclose (sfp); | 178 | close(src_fd); |
| 180 | return -1; | 179 | return(-1); |
| 181 | } | 180 | } |
| 182 | } | 181 | } |
| 183 | 182 | ||
| 184 | if (bb_copyfd_eof(fileno(sfp), fileno(dfp)) == -1) | 183 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
| 185 | status = -1; | 184 | status = -1; |
| 186 | 185 | ||
| 187 | if (fclose(dfp) < 0) { | 186 | if (close(dst_fd) < 0) { |
| 188 | bb_perror_msg("unable to close `%s'", dest); | 187 | bb_perror_msg("unable to close `%s'", dest); |
| 189 | status = -1; | 188 | status = -1; |
| 190 | } | 189 | } |
| 191 | 190 | ||
| 192 | if (fclose(sfp) < 0) { | 191 | if (close(src_fd) < 0) { |
| 193 | bb_perror_msg("unable to close `%s'", source); | 192 | bb_perror_msg("unable to close `%s'", source); |
| 194 | status = -1; | 193 | status = -1; |
| 195 | } | 194 | } |
