Skip to content

builtin test: Implement -ot, -nt, -ef extensions - #9058

Merged
floam merged 4 commits into
fish-shell:masterfrom
floam:test_time
Jul 16, 2022
Merged

floam merged 4 commits into
fish-shell:masterfrom
floam:test_time

Conversation

@floam

@floam floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member

-ot and -nt are common beyond-POSIX extensions that test(1) utilities implement, comparing the modification time of two files, as was proposed by myself for fish in #3589 back in 2016. -ef is the other one we never got around to: it checks if two files are effectively identical, usually implemented by verifying they share inode and ctime.

test if file1 is newer than file2:

test file1 -nt file2

test if file1 is older than file2:

test file1 -ot file2

test if file1 and file2 are the same file:

test file1 -ef file2

Description

test cases still need to be added and documentation needs to be updated. There might need to be accommodation for struct stat platform idiosyncrasies.

image

Fixes #3589

@floam floam changed the title builtin test: Implement -ot, -nt extensions builtin test: Implement -ot, -nt, -ef extensions Jul 8, 2022
@floam
floam force-pushed the test_time branch 3 times, most recently from dc4d746 to e79ed7e Compare July 8, 2022 05:39
@floam

floam commented Jul 8, 2022

Copy link
Copy Markdown
Member Author

There might need to be accommodation for struct stat platform idiosyncrasies.

I've changed to using file_id_t stuff instead of stating anything myself, since we already have a pretty exhaustive method by which we try to determine file equality and we we're cooking with gas for modification times on presumably all the platforms already.

@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

A strange thing:

touch -m -t 197001010000.00 epoch
touch -m -t 190212112045.40 old
touch -m -t 190212112045.39 oldest
touch -m -t 203801080314.07 newest
$ for x in (ls); echo -n test $x -nt NONEXIST=; test $x -nt NONEXIST && echo true || echo false; end
test epoch -nt NONEXIST=true
test newest -nt NONEXIST=true
test old -nt NONEXIST=false
test oldest -nt NONEXIST=false

edit: Ah. Dates earlier than 1970 have a negative time_t, however https://github.com/fish-shell/fish-shell/blob/master/src/wutil.h#L144-L151 was defaulting to a -1 magic number: INT_MIN fixes that.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

The implementation here looks okay at first glance.

However, because of our issues with test, I'm wary of adding more stuff to it. We constantly need to tell people about quoting problems etc, and so making them more likely to use it doesn't seem like a great idea.

So I'd add this to path filter instead:

# Instead of `test file1 -nt file2`
path filter --newer-than=file2 file1 # and many more files

# Instead of `test file1 -ot file2`
path filter --older-than=file2 file2

which can then be combined with other filters - path filter -rf --newer-than=.timestamp * to print all readable files in the current directory that are newer than the file ".timestamp".

@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

I think binary primaries are much less likely to be a thing that causes a surprise if someone accidentally passes an empty unquoted argument through a $variable; it'll error out for test -nt foo or test foo -nt: you'd need to actually do it twice to hit test -nt. I don't really see this as anything but an overdue enhancement. We don't implement [[, ways we can improve test without hurting prior use cases are pretty tangible.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

Sure, the binary operators are less of a problem. But they are still awkward to use - if you do test $first -nt $second, what can happen is:

  1. $first or $second is empty, so this is an error
  2. $first or $second are multiple elements, which would most likely be an error (or could trigger arbitrary conditions)

And even more awkward things like $second is empty and $first is -n, but that's admittedly a theoretical problem.

we can improve test without hurting prior use cases is pretty tangible.

This still sends people to use test when we really want to deemphasize that. And anything we add to test makes it harder to move away from it.

path is also nicer to use with multiple files, nicer to use with multiple conditions and nicer to integrate into a pipeline.

I don't even see an argument for compatibility here, because it's not posix to begin with.

These also aren't very important operators - we've never needed them, and we've not heard many people ask for them in the six years since the feature request was opened. So it's okay to spell them longer than the rather cryptic -nt.

(note we have needed "is file older than a week", but that's #9057)

@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

I like path - the path filter suggestion with dates is interesting. I wasn't suggesting an either or thing, I can see that suggestion particularly useful when trying to kind of whittle down a list of files, the UI is amenable to being able to do stuff like "is it older than x days" or "list the files from 2011 to 2014". That's not how you use test. It's more taking on find.

Multiple elements are going to be a wrench in a lot of situations - so are empty unquoted variables. These are errors, obviously, but it's tricky to do the right thing and also not be surprising.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

the path filter suggestion with dates is interesting

Note: For now, I am suggesting comparing against a file. That path filter --newer-than=.timestamp had a file named ".timestamp". That's what you'd use if you e.g. wanted to write a build system in fish.

Using dates (or ages like "7 weeks" or "3 minutes") is possible but would require parsing.

For now I am suggesting you take this, and move it to path filter instead of test.

That's not how you use test. It's more taking on find.

You can use it like test:

path is --newer-than=file2 file1

Just returns the $status, same as test file1 -nt file2 would. If you implement it for path filter you get path is for free (because it's quite literally a shortcut for path filter --quiet).

This is a tad longer, but clearer, and if you have more complicated conditions it gets shorter:

# $file is a readable file that's newer than timestampfile
test -f "$file" -a -r "$file" -a "$file" -nt timestampfile

# vs

path is -rf --newer-than=timestampfile $file

(the quoting for test there is absolutely necessary because you need to use unary operators like -r)

Multiple elements are going to be a wrench in a lot of situations - so are empty unquoted variables. These are errors, obviously, but it's tricky to do the right thing and also not be surprising.

With path, they aren't really:

path filter -rf --newer-than=../timestampfile *

Look ma, no for-loops! If the current directory has no files? No files will be printed. No error here, and I wouldn't call it surprising - which of no files is newer than timestampfile? Well, none.

@floam

floam commented Jul 8, 2022

Copy link
Copy Markdown
Member Author

If you have multiple elements in the variable and you're not aware you're doing this, or expecting behavior that isn't how it works, you're going to be surprised in some fashion or another.

floam@MBP ~/f/f/src (test_time)> set accidentally_two / /dev/null
floam@MBP ~/f/f/src (test_time)> test -d $accidentally_two 
test: unexpected argument at index 3: '/dev/null'
-d / /dev/null
     ^
floam@MBP ~/f/f/src (test_time) [1]> path is --type=dir $accidentally_two 
floam@MBP ~/f/f/src (test_time)> 

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

If you have multiple elements in the variable and you're not aware you're doing this, or expecting behavior that isn't how it works, you're going to be surprised in some fashion or another.

path is here returned true because at least one of the arguments you gave it was a directory.

test blew up instead.

That's not the same.

If you give path is -d no further argument, it will be false because you've given it no directory.

If you give test -d no further argument, it will be true because -d is a non-empty string.

That's not the same.

And sure, -nt is a binary operator and doesn't have that latter problem, but it would often be combined with -d or -f or -r, which have. See my example above.

We really want to get away from test. It is bad. Adding more features to it makes it worse, not better, because it doesn't solve the problems it has while giving people more reasons/opportunities to use it.

@faho

faho commented Jul 8, 2022 •

Copy link
Copy Markdown
Member

test having issues isn't a new idea. I'm not trying to suddenly spring this on you - see #6124, which starts with the words

test sucks (#1337, #5186, #5947, #6114). Plus I always have to look up the manpage because I can never remember what the options do. Its one saving grace is its compliance with POSIX, so rather than enhancing it beyond that, can we do better?

We now have something better for paths, so I'm simply asking that we enhance that.

@floam
floam marked this pull request as ready for review July 8, 2022 12:41
@floam
floam requested a review from faho July 8, 2022 12:45
@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

The code changes are really quite minimal once squashed and I feel pretty confident that it was implemented well, #3589 has been open for… 6 years. It's a simple enhancement and the UI doesn't suck.

faho
faho previously requested changes Jul 8, 2022

@faho faho left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since my review was specifically requested: My opinion is that this should be added to path instead.

The code here seems fine, implementation-wise (tho I'm a bit miffed at the reformatting of the test completions to use complete without -c), but I simply believe this is the wrong place to place the feature.

My opinion is that it should be changed.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

I feel pretty good that it was implemented well, #3589 has been open for… 6 years.

Yes, and notably that was before path was added, so we can now easily do better.

It's a simple enhancement and the UI doesn't suck.

Let me quote #6124 again:

test sucks

Is this feature the worst? No, in terms of what test can do it's okay.

Except

I always have to look up the manpage because I can never remember what the options do

Because test foo -nt bar is still fairly cryptic. It has a mnemonic ("newer than"), but now there is "-gt", "-lt", "-nt" and "-ot".

But we can now do better than test, and that can help us get away from test.

complete -c test -k -f -s t -d "FD is a terminal"
complete -c test -k -s u -r -d "File set-user-ID bit is set"
complete -c test -k -s w -r -d "File is writable"
complete -c test -k -s x -r -d "File is executable"

@floam floam Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-k is here because these short options were better organized logically in this file than alphabetizing.

@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

I'd suggest that this is actually simpler to use and understand than adapting my approach to the path filter modality or the proposed path mtime thing which isn't very well fleshed out yet, where it'd be more likely to race, and be a fair number of further operations required to accomplish the comparison. It's not some bike shed I thought up: most test utilities implement -nt and -ot.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

proposed path mtime thing which isn't very well fleshed out yet

path mtime is a different thing. It just gives you the mtime as data to do with what you want.

The main usecase is what #9057 already covers - figure out if something is older than a week without resorting to date and stat, which is rather unstandardized.

It's stat -c '%Y' (or stat -f %m on macOS), and it's math (date +%s) - (stat -c %Y) with the --relative option.

It's a building block that you can then do things with.

This is comparing files directly. It's not really easily usable to answer that same question because you would have to have a file that's a week old (meaning you would have to resort to touch, and create a file, just to do a comparison with). But if you already have two files? Sure.

I'm not saying both can't exist.

adapting my approach to the path filter modality

I don't understand what you mean by that.

In terms of user interface, it's quite literally just

path filter --newer-than=comparefile <files-you-want-to-compare-against-it>

And in terms of code, you'd have to add new options --newer-than and --older-than that take options, then you would have to get the stat buf from those files and compare timestamps in filter_path.

It's probably about the same amount of code as this.

It's not some bike shed I thought up: most test utilities implement -nt and -ot.

That means that the comparison operation is interesting, not that it needs to be in test.

Both bash and zsh also implement an alternative to test/[.

@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

test -f "$file" -a -r "$file" -a "$file" -nt timestampfile

This is basically a really bad case scenario, doing a check if something is a file, and also it is readable, and then finally newer than a second file which would imply it exists anyhow - we rarely find it necessary to chain up stuff with a bunch of -a's or multiple test commands - the only thing you didn't do to really make it a more hideous example is insert some escaped parenthesis. What you proffered here as an example though, however bad, it's not like some kind of perl enigma. I think we're kind of talking past each other to score points.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

This is basically a really bad case scenario, doing a check if something is a file, and also it is readable, and then finally newer than a second file which would imply it exists anyhow

Yes, all of these imply existence. You don't need -e here anymore. The -f is there to clarify it's a file, not a directory, and the -r is to make sure it's readable.

But we use this. We do chain operations, and "is a readable file" is a common one. See

It's common because "readable file" means it's usable. You can't do cat $foo if $foo isn't a readable file.

@floam

floam commented Jul 8, 2022 •

Copy link
Copy Markdown
Member Author

If you look through our codebase, you are going to see most of the date-time file path stuff we figure out is along the lines of set -l age (math (date +%s) - (stat ...)). This particular PR of mine doesn't make that specific use case extremely easier (although sometimes there will be some specific files you can check against to figure out it's a decent time to flush the cache..).. neither does a path filter --newer-than that takes a path argument on your new builtin: I think it'd be cool if path or something had a way to do streamline that unaddressed part more easily. The part where we want to ask if a file is an hour/week/month old. You need not worry about this PR cannibalizing much of anything.

@faho

faho commented Jul 8, 2022

Copy link
Copy Markdown
Member

most of the date-time file path stuff we need to laboriously figure out is along the lines of set -l age (math (date +%s) - (stat ...)).

Yes, and that's path mtime -R. See #9057, which already replaces those uses:

- set -l age (math (date +%s) - (stat -c '%Y' $cache_file))
+ set -l age (path mtime -R -- $cache_file)

I called that out above.

You need not worry about this PR cannibalizing much of anything.

I am not. I explicitly said that I'm okay with comparing being a separate thing.

I just would prefer to have it in path instead of test.

@krobelus

krobelus commented Jul 9, 2022

Copy link
Copy Markdown
Contributor

Looks like a nice addition.
I think having this functionality in both test and path is justified because many people already know test.
Though it means they might not learn the path one.

Comment thread src/wutil.h Resolved
Comment thread src/wutil.cpp Resolved
Comment thread src/builtins/test.cpp Resolved
@floam
floam requested review from faho, krobelus and ridiculousfish July 9, 2022 18:12
@zanchey

zanchey commented Jul 10, 2022

Copy link
Copy Markdown
Member

I think having this functionality in both test and path is justified because many people already know test.

I agree

@faho faho mentioned this pull request Jul 10, 2022
2 of 3 tasks
@floam

floam commented Jul 10, 2022

Copy link
Copy Markdown
Member Author

I'm squashing down the commit history here a bit.

@floam
floam force-pushed the test_time branch 3 times, most recently from 429fa23 to 40ffbb6 Compare July 10, 2022 17:09
@faho

faho commented Jul 10, 2022

Copy link
Copy Markdown
Member

Okay, test and path it is then, it appears I have been outvoted.

Comment thread src/wutil.h
@@ -145,10 +145,15 @@ struct file_id_t {
dev_t device{static_cast<dev_t>(-1LL)};
ino_t inode{static_cast<ino_t>(-1LL)};
uint64_t size{static_cast<uint64_t>(-1LL)};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please don't @ me in commit messages because then I get notifications when any fork pushes that commit

I'm squashing down the commit history here a bit.

yeah I think that's a good idea, it will make the change easier to review.
Especially since some changes cancel each outer out

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dang I hate it when that happens, sorry about that.

The twists and turns of how this PR was finally implemented are squashed away now.

Comment thread src/wutil.h Outdated
tv_nsec's meaningful values are REALLY [0, 999999999]
this nanosecond component we'll initialize at 0. */
time_t change_seconds{std::numeric_limits<time_t>::min()};
long change_nanoseconds{0};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry what I meant is that

  • if we use long, then std::numeric_limits<long>::min() is the obvious "oldest" value
  • if we use unsigned long, then 0 would be more readable than std::numeric_limits<unsigned long>::min()

long 0 is a weird in-between state (how come it's better than the original -1).
I guess it makes no practical difference, but it would improve the clarity of the change to make it the actual oldest file that we can represent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using std::numeric_limits<time_t>::min() for the time_t - that's a valid "really old" timestamp.
The nsec part is how many additional nanoseconds there are - it's defined between 0 and 1000000. Does INT_MIN make sense there?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how many additional nanoseconds there are - it's defined between 0 and 1000000

I see, then I'd stick to -1 since that's already older than any valid time. 0 is fine too IMO

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are left at -1 again now.

@floam
floam force-pushed the test_time branch 3 times, most recently from a254bf4 to eb15c0b Compare July 10, 2022 20:05
@floam
floam requested a review from krobelus July 10, 2022 20:08

@krobelus krobelus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

floam added 4 commits July 10, 2022 14:39
These are non-POSIX extensions other test(1) utilities implement,
which compares the modification time of two files as proposed for
fish in fish-shell#3589: testing if one file is newer than another file.

-ef is a common extension to test(1) which checks if two paths refer
to the same file, by comparing the dev and inode numbers.
also complete filenames in binary cases

e.g. test path -ef path
Explain test(1)'s -ef, -nt, -ot features.
Some sanity checks for -ot, -nt, -ef

Try negative mtime values too, there was interesting behavior
there during development.
@floam
floam dismissed faho’s stale review July 16, 2022 19:39

Dismissed

@floam
floam removed the request for review from faho July 16, 2022 19:39
@floam
floam merged commit 77f6afa into fish-shell:master Jul 16, 2022
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 17, 2023
@faho faho added this to the fish 3.6.0 milestone Dec 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend 'test' with non-POSIX file time comparison extensions

4 participants