This avoids duplicating the xputs call, which I assume was the sticking point with my previous suggestion.
Also remove the now-duplicated command name in the tests, and add a couple more tests. --- tests/file.test | 23 ++++++++++++++++------- toys/pending/file.c | 10 +++++----- 2 files changed, 21 insertions(+), 12 deletions(-)
From cc07ce9e954a41f0ddbcf0219aeab10efa06585c Mon Sep 17 00:00:00 2001 From: Elliott Hughes <[email protected]> Date: Thu, 3 Mar 2016 10:20:16 -0800 Subject: [PATCH] Fix the potential fd leak in file(1). This avoids duplicating the xputs call, which I assume was the sticking point with my previous suggestion. Also remove the now-duplicated command name in the tests, and add a couple more tests. --- tests/file.test | 23 ++++++++++++++++------- toys/pending/file.c | 10 +++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/file.test b/tests/file.test index 86ddd9e..3e586bc 100644 --- a/tests/file.test +++ b/tests/file.test @@ -11,11 +11,20 @@ echo "#! /usr/bin/env python" > env.python.script echo "Hello, world!" > ascii echo "cafebabe000000310000" | xxd -r -p > java.class -testing "file empty" "file empty" "empty: empty\n" "" "" -testing "file bash.script" "file bash.script" "bash.script: /bin/bash script\n" "" "" -testing "file bash.script with spaces" "file bash.script2" "bash.script2: /bin/bash script\n" "" "" -testing "file env python script" "file env.python.script" "env.python.script: python script\n" "" "" -testing "file ascii" "file ascii" "ascii: ASCII text\n" "" "" -testing "file java class" "file java.class" "java.class: Java class file, version 49.0\n" "" "" +echo "non-empty" > unreadable +touch unreadable.empty +chmod a-r unreadable unreadable.empty -rm empty bash.script bash.script2 env.python.script ascii java.class +testing "empty" "file empty" "empty: empty\n" "" "" +testing "bash.script" "file bash.script" "bash.script: /bin/bash script\n" "" "" +testing "bash.script with spaces" "file bash.script2" "bash.script2: /bin/bash script\n" "" "" +testing "env python script" "file env.python.script" "env.python.script: python script\n" "" "" +testing "ascii" "file ascii" "ascii: ASCII text\n" "" "" +testing "java class" "file java.class" "java.class: Java class file, version 49.0\n" "" "" + +testing "unreadable" "file unreadable" "unreadable: cannot open\n" "" "" +# Detecting empty unreadable files isn't mandatory, but is easily achieved. +testing "empty unreadable" "file unreadable.empty" "unreadable.empty: empty\n" "" "" + +rm empty bash.script bash.script2 env.python.script ascii java.class \ + unreadable unreadable.empty diff --git a/toys/pending/file.c b/toys/pending/file.c index fbff586..cce088f 100644 --- a/toys/pending/file.c +++ b/toys/pending/file.c @@ -247,16 +247,16 @@ void file_main(void) if (!lstat(name, &sb)) { if (S_ISFIFO(sb.st_mode)) what = "fifo"; else if (S_ISREG(sb.st_mode)) { - int fd = !strcmp(name, "-") ? 0 : open(name, O_RDONLY); + if (sb.st_size == 0) what = "empty"; + else { + int fd = !strcmp(name, "-") ? 0 : open(name, O_RDONLY); - if (fd!=-1) { - if (sb.st_size == 0) what = "empty"; - else { + if (fd!=-1) { do_regular_file(fd, name); + if (fd>0) close(fd); continue; } } - if (fd>0) close(fd); } else if (S_ISBLK(sb.st_mode)) what = "block special"; else if (S_ISCHR(sb.st_mode)) what = "character special"; else if (S_ISDIR(sb.st_mode)) what = "directory"; -- 2.7.0.rc3.207.g0ac5344
_______________________________________________ Toybox mailing list [email protected] http://lists.landley.net/listinfo.cgi/toybox-landley.net
