Skip to content

Commit

Permalink
Implement -ot
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Sep 21, 2024
1 parent 71eba68 commit 71c369d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/elements/expr/error
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
../../../test/test_compound.bash
25 changes: 11 additions & 14 deletions src/utils/file_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions test/test_compound.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]]')
Expand Down

0 comments on commit 71c369d

Please sign in to comment.