Skip to contentSkip to conversation

fix ext4_write_file can only write file once

PATCH Wavin 1 participant 1 message Feb 19, 2023 ext4 file system

lore
Share & tools
lore

Wavin

author
#0 lore raw

Hi!     I found an problem that the u-boot will delete my file first when I write an exists file on ext4 filesystem. This results in only one line being written when I want to write multiple lines, and cannot write more times. Although the method maybe betrickly, It's really resolve my problem.

Thanks & Best Regards!
Wavin Don

From 1bb07ab223ccb84f874f52222055e8f1bb2d9c54 Mon Sep 17 00:00:00 2001
From: Wavin Don <[email protected]>
Date: Sat, 18 Feb 2023 23:30:06 +0800
Subject: [PATCH] fix ext4_write_file can only write file once

---
 fs/ext4/ext4_write.c | 42 +++++++++++++++++++++++++++++++++++++++---
 1 file changed, 39 insertions(+), 3 deletions(-)
fs/ext4/ext4_write.c +39 -3
index f22af45d1b..d92065b697 100644
@@ -18,6 +18,11 @@
  * Copyright (C) 2003, 2004  Free Software Foundation, Inc.
  *
  * ext4write : Based on generic ext4 protocol.
+ *
+ * (C) Copyright 2023
+ * Wavin Don <[email protected]>
+ *
+ * ext4fs_write_append : Write file append rear of file.
  */
 
 
@@ -1037,17 +1042,48 @@ fail:
 	return -1;
 }
 
+int ext4fs_write_append(const char *fname, char *buffer,
+		 unsigned long sizebytes, int type)
+{
+	int ret;
+	loff_t size = sizebytes;
+	char *real_buffer;
+	if (ext4fs_exists(fname)) {
+		loff_t actread = 0;
+		ret = ext4fs_size(fname, &size);
+		if (ret) goto end;
+		real_buffer = zalloc(sizebytes + size);
+		ret = ext4_read_file(fname, real_buffer, 0, size, &actread);
+		if (ret) goto end;
+		if (actread != size) {
+			ret = -1;
+			goto end;
+		}
+		memcpy(real_buffer + size, buffer, sizebytes);
+		size = size + sizebytes;
+	} else {
+		real_buffer = buffer;
+	}
+	ret = ext4fs_write(fname, real_buffer, size, FILETYPE_REG);
+end:
+	free(real_buffer);
+	return ret;
+}
+
 int ext4_write_file(const char *filename, void *buf, loff_t offset,
 		    loff_t len, loff_t *actwrite)
 {
 	int ret;
 
-	if (offset != 0) {
-		printf("** Cannot support non-zero offset **\n");
+	if (offset == -1) {
+		ret = ext4fs_write_append(filename, buf, len, FILETYPE_REG);
+	} else if (offset == 0) {
+		ret = ext4fs_write(filename, buf, len, FILETYPE_REG);
+	} else {
+		printf("** Cannot support non-minus-one and non-zero offset **\n");
 		return -1;
 	}
 
-	ret = ext4fs_write(filename, buf, len, FILETYPE_REG);
 	if (ret) {
 		printf("** Error ext4fs_write() **\n");
 		goto fail;
Signature · 1 line
2.34.1