[PATCH 04/16] tools: Avoid leaving extra data at the end of copied files

Simon Glass sjg at chromium.org
Fri Nov 12 20:28:05 CET 2021


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 at chromium.org>
---

 tools/fit_common.c | 2 +-
 tools/fit_common.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/fit_common.c b/tools/fit_common.c
index c161f23b0f7..d13e5ebf1ae 100644
--- a/tools/fit_common.c
+++ b/tools/fit_common.c
@@ -126,7 +126,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 a76c4001c59..55f3f6acd4e 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
-- 
2.34.0.rc1.387.gb447b232ab-goog



More information about the U-Boot mailing list