diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2020-08-18 10:05:58 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2020-08-18 11:23:17 +0200 |
commit | cdf3e7ed0d6624108a2a4722f721032d576e5516 (patch) | |
tree | 72e07f6c6f89862a6dc3a37806056157e132c728 /lib | |
parent | 9f0fe7745e458a388066e9a504b77fb5fe2295b2 (diff) | |
download | barebox-cdf3e7ed0d6624108a2a4722f721032d576e5516.tar.gz barebox-cdf3e7ed0d6624108a2a4722f721032d576e5516.tar.xz |
libfile: copy_file: Fix ftruncate called on device files
ftruncate may only be called on regular files. This became broken in
a5f73a6dcd. Revert to the previous behaviour and pass O_TRUNC to the
initial open() call instead of truncating it with ftruncate().
Also we can call ftruncate() to the final size also when the file
previously didn't exist.
Fixes: a5f73a6dcd ("libfile: copy_file: explicitly truncate to final size")
Reported-by: Bastian Krause <bst@pengutronix.de>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libfile.c | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/lib/libfile.c b/lib/libfile.c index 863b6833a5..20bb689a79 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -353,6 +353,10 @@ int copy_file(const char *src, const char *dst, int verbose) goto out; } + /* Set O_TRUNC only if file exists 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()); @@ -360,17 +364,13 @@ 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) { discard_range(dstfd, srcstat.st_size, 0); - if (S_ISREG(dststat.st_mode)) { + if (s || S_ISREG(dststat.st_mode)) { ret = ftruncate(dstfd, srcstat.st_size); if (ret) goto out; |