Repository navigation
builtin test: Implement -ot, -nt, -ef extensions - #9058
Conversation
dc4d746 to
e79ed7e
Compare
I've changed to using |
|
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=falseedit: 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. |
|
The implementation here looks okay at first glance. However, because of our issues with So I'd add this to # 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 file2which can then be combined with other filters - |
|
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 |
|
Sure, the binary operators are less of a problem. But they are still awkward to use - if you do
And even more awkward things like $second is empty and $first is
This still sends people to use
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 (note we have needed "is file older than a week", but that's #9057) |
|
I like 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. |
Note: For now, I am suggesting comparing against a file. That 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
You can use it like path is --newer-than=file2 file1Just returns the $status, same as 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
With 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. |
|
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)> |
That's not the same. If you give If you give That's not the same. And sure, 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. |
|
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
We now have something better for paths, so I'm simply asking that we enhance that. |
|
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
left a comment
There was a problem hiding this comment.
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.
Yes, and notably that was before
Let me quote #6124 again:
Is this feature the worst? No, in terms of what Except
Because But we can now do better than |
| 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" |
There was a problem hiding this comment.
-k is here because these short options were better organized logically in this file than alphabetizing.
|
I'd suggest that this is actually simpler to use and understand than adapting my approach to the |
The main usecase is what #9057 already covers - figure out if something is older than a week without resorting to It's 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 I'm not saying both can't exist.
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 It's probably about the same amount of code as this.
That means that the comparison operation is interesting, not that it needs to be in Both bash and zsh also implement an alternative to |
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 |
Yes, all of these imply existence. You don't need 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 |
|
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 |
Yes, and that's - set -l age (math (date +%s) - (stat -c '%Y' $cache_file))
+ set -l age (path mtime -R -- $cache_file)I called that out above.
I am not. I explicitly said that I'm okay with comparing being a separate thing. I just would prefer to have it in |
|
Looks like a nice addition. |
I agree |
|
I'm squashing down the commit history here a bit. |
429fa23 to
40ffbb6
Compare
|
Okay, |
| @@ -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)}; | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| 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}; |
There was a problem hiding this comment.
sorry what I meant is that
- if we use
long, thenstd::numeric_limits<long>::min()is the obvious "oldest" value - if we use
unsigned long, then 0 would be more readable thanstd::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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Those are left at -1 again now.
a254bf4 to
eb15c0b
Compare
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.
-otand-ntare 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.-efis 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
file1is newer thanfile2:test if
file1is older thanfile2:test if
file1andfile2are the same file:Description
test cases still need to be added and documentation needs to be updated.
There might need to be accommodation forstruct statplatform idiosyncrasies.Fixes #3589