From: Simon Glass <sjg@chromium.org>
Date: Fri, 12 Nov 2021 19:28:05 +0000 (-0700)
Subject: tools: Avoid leaving extra data at the end of copied files
X-Git-Tag: v2025.01-rc5-pxa1908~1562^2~8
X-Git-Url: http://git.dujemihanovic.xyz/%22mailto:Murray.Jensen%40csiro.au/static/%7B%7B%20%24style.RelPermalink%20%7D%7D?a=commitdiff_plain;h=7ae46c35793bcc034f1300d8b79e3fd7e506537c;p=u-boot.git

tools: Avoid leaving extra data at the end of copied files

The copyfile() implementation has strange behaviour if the destination
file already exists. Update it to ensure that any existing data in the
destination file is dropped.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

diff --git a/tools/fit_common.c b/tools/fit_common.c
index 4370de2f61..5ea43f5fec 100644
--- a/tools/fit_common.c
+++ b/tools/fit_common.c
@@ -134,7 +134,7 @@ int copyfile(const char *src, const char *dst)
 		goto out;
 	}
 
-	fd_dst = open(dst, O_WRONLY | O_CREAT, 0666);
+	fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
 	if (fd_dst < 0) {
 		printf("Can't open file %s (%s)\n", dst, strerror(errno));
 		goto out;
diff --git a/tools/fit_common.h b/tools/fit_common.h
index 872d8afa17..c600dc2ba4 100644
--- a/tools/fit_common.h
+++ b/tools/fit_common.h
@@ -44,6 +44,8 @@ int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
  *
  * This uses read()/write() to copy file @src to file @dst
  *
+ * If @dst exists, it is overwritten and truncated to the correct size.
+ *
  * @src: Filename to read from
  * @dst: Filename to write to
  * @return 0 if OK, -1 on error