From: Alexander Shiyan <[email protected]>

Fix bug where processing multiple files with -s +SIZE option
would use incorrect sizes for files after the first one, because the
size variable was being overwritten.

Signed-off-by: Alexander Shiyan <[email protected]>
Link: 
https://lore.barebox.org/[email protected]
Signed-off-by: Sascha Hauer <[email protected]>
(cherry picked from commit 6981db707661f68a1aea4f0969360cb0af7fd530)
Signed-off-by: Ahmad Fatoum <[email protected]>
---
 commands/truncate.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/commands/truncate.c b/commands/truncate.c
index 5c6d74257da1..83661fbaf63b 100644
--- a/commands/truncate.c
+++ b/commands/truncate.c
@@ -59,23 +59,26 @@ static int do_truncate(int argc, char *argv[])
                        continue;
                }
 
+               off_t target_size = size;
+
                if (modify) {
                        struct stat st;
 
                        if (fstat(fd, &st) == -1) {
                                perror("fstat");
                                ret = 1;
-                               goto close;
+                               close(fd);
+                               continue;
                        }
 
-                       size = st.st_size + modify * size;
+                       target_size = st.st_size + size;
                }
 
-               if (ftruncate(fd, size) == -1) {
+               if (ftruncate(fd, target_size) == -1) {
                        perror("truncate");
                        ret = 1;
                }
-close:
+
                close(fd);
        }
 
-- 
2.47.3


Reply via email to