-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there some reason the 'FileTimes' and 'set_times' API is File only? #123883
Comments
On unix directories are considered files and can be opened. They mere are not regular files. Which is why So |
Then perhaps it's not on the file open? My use is/was: I would open a directory, read times from the source, and then do a FYI the All in all, does it seem a little goofy to open a file to change its metadata? Even if this is what actually happens under the hood? |
Check with |
Seems as if the issue is the file open:
|
man 2 open:
So open it as readable. This works on the playground: use std::fs::FileTimes;
use std::time::SystemTime;
use std::fs::File;
fn main() {
let home = std::env::home_dir().unwrap();
let home = File::open(home).unwrap();
home.set_times(FileTimes::new().set_modified(SystemTime::now())).unwrap();
} |
Excuse me. This blows up when the "source" path is opened as a file. And I have really no idea where in my program I would be doing that. I'm going to have to dig into where this happens, but my best guess is that this is happening under the hood, in the standard library, because one doesn't need to open the file to read its metadata/mtime/atime? |
I'm confused what you're doing and where it's failing. A reproducer would help. Stacktraces might also provide some context (strace may be able to provide those). |
That's fair. See: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c8ffc679c3beae232aeeae5a2d69da0a I'm struggling to see what the issue might be. |
Remove that part, open it as read-only as I said earlier. Then you can set the metadata. |
Aw, didn't understand that was what you were saying earlier. This appears to work, and I very much appreciate your help. I will close the issue, but this does seem like confusing semantics. In order to write file metadata, the file must be open read only? That just seems wrong, at the very least, so counterintuitive there should be a note describing this use case at |
There are two steps. Opening the file, setting the metadata. And in this case opening a directory has additional preconditions. There is a syscall to do it without the opening step, but the standard library currently doesn't expose that as a free function. |
Agreed, and I still think that's wildly counterintuitive. See the I'm supposing I simply copied the doc comment's code. And then it failed when I used the same with a directory. And perhaps I looked again and again at the doc comment and couldn't understand why it was failing. You're saying "Hey go look at I'd be pleased to file another issue, if you think that is best. |
This is due to the standard library being close to the system API which provides no backtraces or line information because those would come at extra costs, which can be prohibitive in cases where errors are frequent (such as trying to open lots of files that might not exist). Which is why I suggested using strace.
Setting file times is rare, setting file times on directories is even rarer. So this wasn't considered so far. That said, updating the documentation is a good idea.
If you want to propose a new API you can file an API Change Proposal here: https://github.com/rust-lang/libs-team/issues |
I don't see any discussion of this in the feature: #98245
Of course, it is possible the change the
mtime
andatime
on a directory. However, this fails re: the current FileTimes API because one can't open a directory as one would a file.Shouldn't the FileTimes API be available for all paths?
Tasks
The text was updated successfully, but these errors were encountered: