[PATCH] truncate: do not die when a file doesn't exist and no-create flag is on

Ari Sundholm ari at tuxera.com
Fri May 15 13:34:22 UTC 2015


From: Ari Sundholm <ari at tuxera.com>

Additionally, open(2) failures do not make the program die immediately.
This makes the behavior of the program match coreutils more closely.

bloat-o-meter:
function                                             old     new   delta
truncate_main                                        178     228     +50
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 50/0)               Total: 50 bytes
---
 coreutils/truncate.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/coreutils/truncate.c b/coreutils/truncate.c
index 0e36dab..55e44db 100644
--- a/coreutils/truncate.c
+++ b/coreutils/truncate.c
@@ -62,15 +62,21 @@ int truncate_main(int argc UNUSED_PARAM, char **argv)
 	// compatible at once...
 	size = XATOU_SFX(size_str, kMG_suffixes);
 
-	argv += optind;
-	while (*argv) {
-		int fd = xopen(*argv, flags);
+	for (argv += optind; *argv; ++argv) {
+		int fd = open(*argv, flags);
+		if (fd < 0) {
+			if (errno != ENOENT || !(opts & OPT_NOCREATE)) {
+				bb_perror_msg("%s: open", *argv);
+				ret = EXIT_FAILURE;
+			}
+			continue;
+		}
+
 		if (ftruncate(fd, size) == -1) {
 			bb_perror_msg("%s: ftruncate", *argv);
 			ret = EXIT_FAILURE;
 		}
 		xclose(fd);
-		++argv;
 	}
 
 	return ret;
-- 
1.9.1




More information about the busybox mailing list