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

Denys Vlasenko vda.linux at googlemail.com
Mon May 25 13:17:03 UTC 2015


commit: http://git.busybox.net/busybox/commit/?id=fc3e40ee8102c8b17fc98fbc93417bed4d878723
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

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

function                                             old     new   delta
truncate_main                                        161     221     +60

Signed-off-by: Ari Sundholm <ari at tuxera.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/truncate.c |   20 +++++++++++++++-----
 1 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/coreutils/truncate.c b/coreutils/truncate.c
index 0e36dab..e5fa656 100644
--- a/coreutils/truncate.c
+++ b/coreutils/truncate.c
@@ -64,12 +64,22 @@ int truncate_main(int argc UNUSED_PARAM, char **argv)
 
 	argv += optind;
 	while (*argv) {
-		int fd = xopen(*argv, flags);
-		if (ftruncate(fd, size) == -1) {
-			bb_perror_msg("%s: ftruncate", *argv);
-			ret = EXIT_FAILURE;
+		int fd = open(*argv, flags);
+		if (fd < 0) {
+			if (errno != ENOENT || !(opts & OPT_NOCREATE)) {
+				bb_perror_msg("%s: open", *argv);
+				ret = EXIT_FAILURE;
+			}
+			/* else: ENOENT && OPT_NOCREATE:
+			 * do not report error, exitcode is also 0.
+			 */
+		} else {
+			if (ftruncate(fd, size) == -1) {
+				bb_perror_msg("%s: truncate", *argv);
+				ret = EXIT_FAILURE;
+			}
+			xclose(fd);
 		}
-		xclose(fd);
 		++argv;
 	}
 


More information about the busybox-cvs mailing list