summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2020-06-11 20:51:48 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2020-07-05 16:17:12 +0200
commita5f73a6dcd5296abb76c50615cb7c1ddfb227708 (patch)
tree2e83a44a771c1c3734b2e49f38e2c3540c2a9d18 /lib
parente9fb40763b6de1aaf889ba07529eed68f20f55c3 (diff)
downloadbarebox-a5f73a6dcd5296abb76c50615cb7c1ddfb227708.tar.gz
barebox-a5f73a6dcd5296abb76c50615cb7c1ddfb227708.tar.xz
libfile: copy_file: explicitly truncate to final size
When possible truncate the destination file to the final size explicitly. This allows for example ramfs to put the resulting file contiguously into memory. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/libfile.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/lib/libfile.c b/lib/libfile.c
index 9de938b2d3..863b6833a5 100644
--- a/lib/libfile.c
+++ b/lib/libfile.c
@@ -353,10 +353,6 @@ int copy_file(const char *src, const char *dst, int verbose)
goto out;
}
- /* Set O_TRUNC only if file exist and is a regular file */
- if (!s && S_ISREG(dststat.st_mode))
- mode |= O_TRUNC;
-
dstfd = open(dst, mode);
if (dstfd < 0) {
printf("could not open %s: %s\n", dst, errno_str());
@@ -364,12 +360,22 @@ int copy_file(const char *src, const char *dst, int verbose)
goto out;
}
+ ret = ftruncate(dstfd, 0);
+ if (ret)
+ goto out;
+
ret = stat(src, &srcstat);
if (ret)
goto out;
- if (srcstat.st_size != FILESIZE_MAX)
+ if (srcstat.st_size != FILESIZE_MAX) {
discard_range(dstfd, srcstat.st_size, 0);
+ if (S_ISREG(dststat.st_mode)) {
+ ret = ftruncate(dstfd, srcstat.st_size);
+ if (ret)
+ goto out;
+ }
+ }
if (verbose)
init_progression_bar(srcstat.st_size);