Skip to content

Commit

Permalink
Implement -nt
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Sep 21, 2024
1 parent d97a743 commit 71eba68
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/elements/expr/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl ConditionalExpr {
Err(e) => return Err(e),
};

let result = file_check::metadata_comp(&right, &left, op);
let result = file_check::metadata_comp(&left, &right, op);
stack.push( Elem::Ans(result) );
Ok(())
}
Expand Down
7 changes: 6 additions & 1 deletion src/utils/file_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ pub fn metadata_comp(left: &str, right: &str, tp: &str) -> bool {
};
let right_meta = match fs::metadata(right) {
Ok(m) => m,
_ => return false,
_ => return tp == "-nt",
};

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
},
_ => false,
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/error
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
8 changes: 8 additions & 0 deletions test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
../../../test/test_compound.bash
23 changes: 23 additions & 0 deletions test/test_compound.bash
Original file line number Diff line number Diff line change
Expand Up @@ -727,18 +727,41 @@ rm -f /tmp/$$*

res=$($com -c 'touch /tmp/$$ ; ln /tmp/$$ /tmp/$$x; [[ /tmp/$$ -ef /tmp/$$x ]]')
[ "$?" = "0" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ /tmp/$$ -ef /tmp/$$x ]]')
[ "$?" = "1" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ ! /tmp/$$ -ef /tmp/$$x ]]')
[ "$?" = "0" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ ! ( /tmp/$$ -ef /tmp/$$x ) ]]')
[ "$?" = "0" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c '[[ /etc/passwd -ef /tmp/aaaaa ]]')
[ "$?" = "1" ] || err $LINENO

res=$($com -c '[[ /etc/aaaaaa -ef /etc/passwd ]]')
[ "$?" = "1" ] || err $LINENO

res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ /tmp/$$x -nt /tmp/$$ ]]')
[ "$?" = "0" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c 'touch /tmp/$$ ; touch /tmp/$$x; [[ /tmp/$$ -nt /tmp/$$x ]]')
[ "$?" = "1" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c 'touch /tmp/$$ ; [[ /tmp/$$ -nt /tmp/$$ ]]')
[ "$?" = "1" ] || err $LINENO
rm -f /tmp/$$*

res=$($com -c '[[ /etc/passwd -nt /tmp/aaaaaaaaa ]]')
[ "$?" = "0" ] || err $LINENO

# and or

res=$($com -c '[[ -a /etc/passwd && -a /etc/passwd ]]')
Expand Down

0 comments on commit 71eba68

Please sign in to comment.