From 71c369d5d09ca642dd94152c68a9dfb9f31931ed Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sat, 21 Sep 2024 18:26:51 +0900 Subject: [PATCH] Implement -ot --- src/elements/expr/error | 1 + src/utils/file_check.rs | 25 +++++++++++-------------- test/ok | 3 +++ test/test_compound.bash | 24 ++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 14 deletions(-) create mode 100644 src/elements/expr/error diff --git a/src/elements/expr/error b/src/elements/expr/error new file mode 100644 index 00000000..ddcd8cab --- /dev/null +++ b/src/elements/expr/error @@ -0,0 +1 @@ +../../../test/test_compound.bash diff --git a/src/utils/file_check.rs b/src/utils/file_check.rs index ed9a3117..3709c106 100644 --- a/src/utils/file_check.rs +++ b/src/utils/file_check.rs @@ -28,23 +28,20 @@ pub fn is_dir(name: &str) -> bool { } pub fn metadata_comp(left: &str, right: &str, tp: &str) -> bool { - let left_meta = match fs::metadata(left) { - Ok(m) => m, - _ => return false, - }; - let right_meta = match fs::metadata(right) { - Ok(m) => m, - _ => return tp == "-nt", + let (lmeta, rmeta) = match ( fs::metadata(left), fs::metadata(right) ) { + ( Ok(lm), Ok(rm) ) => (lm, rm), + ( Ok(_), Err(_) ) => return tp == "-nt", + ( Err(_), Ok(_) ) => return tp == "-ot", + ( Err(_), Err(_) ) => return false, }; match tp { - "-ef" => (left_meta.dev(), left_meta.ino()) - == (right_meta.dev(), right_meta.ino()), - "-nt" => { - let left_modified = left_meta.modified().unwrap(); - let right_modified = right_meta.modified().unwrap(); - left_modified > right_modified - }, + "-ef" => (lmeta.dev(), lmeta.ino()) + == (rmeta.dev(), rmeta.ino()), + "-nt" => lmeta.modified().unwrap() + > rmeta.modified().unwrap(), + "-ot" => lmeta.modified().unwrap() + < rmeta.modified().unwrap(), _ => false, } } diff --git a/test/ok b/test/ok index 8f6a98b7..9c562a8e 100644 --- a/test/ok +++ b/test/ok @@ -20,3 +20,6 @@ ../../../test/test_compound.bash ../../../test/test_compound.bash ../../../test/test_compound.bash +../../../test/test_compound.bash +../../../test/test_compound.bash +../../../test/test_compound.bash diff --git a/test/test_compound.bash b/test/test_compound.bash index ac74e53a..530be789 100755 --- a/test/test_compound.bash +++ b/test/test_compound.bash @@ -762,6 +762,30 @@ rm -f /tmp/$$* res=$($com -c '[[ /etc/passwd -nt /tmp/aaaaaaaaa ]]') [ "$?" = "0" ] || err $LINENO +res=$($com -c '[[ /etc/aaaaaaaaaa -nt /etc/bbbbbb ]]') +[ "$?" = "1" ] || err $LINENO + +res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ /tmp/$$x -ot /tmp/$$ ]]') +[ "$?" = "1" ] || err $LINENO +rm -f /tmp/$$* + +res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ /tmp/$$ -ot /tmp/$$x ]]') +[ "$?" = "0" ] || err $LINENO +rm -f /tmp/$$* + +res=$($com -c 'touch /tmp/$$ ; [[ /tmp/$$ -ot /tmp/$$ ]]') +[ "$?" = "1" ] || err $LINENO +rm -f /tmp/$$* + +res=$($com -c '[[ /etc/passwd -ot /tmp/aaaaaaaaa ]]') +[ "$?" = "1" ] || err $LINENO + +res=$($com -c '[[ /etc/aaaaaaaaaa -ot /etc/passwd ]]') +[ "$?" = "0" ] || err $LINENO + +res=$($com -c '[[ /etc/aaaaaaaaaa -ot /etc/bbbbbb ]]') +[ "$?" = "1" ] || err $LINENO + # and or res=$($com -c '[[ -a /etc/passwd && -a /etc/passwd ]]')