-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
std.fs
is missing an "open any" API
#16738
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
Comments
To me the category "file" intuitively doesn't contain "directory", therefore option 2 of having a separate function Looking into |
The primary use case for var something = try std.fs.cwd().openAny("something", .{});
defer something.close();
const stat = try something.stat();
switch (stat.kind) {
.file => {},
.named_pipe => {},
// ...
} (a real world example of this sort of thing here) Plus, AFAIK |
I like (2) and I think that it could potentially even be called
As a related side note, I have decided to revert #12060. |
Good question. My initial thought would be This issue is also related: #12377 where some It's possible we may want to go for some sort of design where:
Usage would then look something like: var dir = try std.fs.cwd().openDir("blah", .{});
defer dir.close();
// `stat` is a wrapper around `dir.handle.stat()`, or maybe you'd have to call `dir.handle.stat()` directly
const stat = try dir.stat(); and then var handle = try std.fs.cwd().openAny("something", .{});
defer handle.close();
const other_stat = try handle.stat();
// maybe some way to go from Handle to File/Dir once the type is known? Note that Lines 2637 to 2643 in 4c97919
EDIT: See the comment after this one; I no longer think the design detailed in this comment makes sense with the 'non-dir' wrinkle: #16738 (comment) |
So it actually turns out that
In more practical terms, this means that As an example: > test.exe \\.\pipe\mypipe
opening NT-prefixed path '\??\pipe\mypipe' with filter: .file_only
attributes: 0x80
kind=fs.file.File.Kind.file Relevant test code (with debug prints in the std library functions): var output_file = try std.fs.cwd().openFile(output_path, .{ .mode = .read_write });
defer output_file.close();
const stat = try output_file.stat(); So |
And to me, who worked on UNIX kernels and did other systems programming on Posix systems for many years, intuitively (A) "file" is any object in the file system, something represented by a path, that has an inode and you can perform a But I also have the more common intuition of (B) a file as a random-access collection of bytes stored on disk, as opposed to a directory, symlink, socket, block or character device, etc. ... the sort of thing that on Posix systems is sometimes referred to as a "plain file". Which indicates that the term "file" is ambiguous. And it's really not possible to avoid the ambiguity when we have
Note that Given the existence of numerous instances of "File" referring to any kind of fs object, I think option 1 is preferable to But wait ... suppose you open a directory using BTW, Another possibility is to merge In any case, I think the |
Naming idea: |
@rohlem |
However it's split up on the inside, maybe a |
for this use case my go-to has been to |
Uh oh!
There was an error while loading. Please reload this page.
Right now, the status quo is:
std.fs.Dir.openFile
error.IsDir
if the path is a directory (this is due to.filter = .file_only
during thestd.os.windows.OpenFile
call)OpenFlags.isWrite()
is true, in which case it will returnerror.IsDir
)std.fs.Dir.openDir
error.NotDir
if the path is not a directoryThis means that there is currently no cross-platform
std.fs.Dir
API that allows opening any path (regardless of its type). Having an API that allows opening any path on all platforms is possible, though, and would be useful (see https://ziggit.dev/t/how-to-check-if-path-points-to-a-file-or-a-directory/).There are a few ways that this could be addressed:
filter: enum { file_only, any }
toFile.OpenFlags
and let the caller ofDir.openFile
choose which behavior they wantDir.openAny
function that can open any type, and makeDir.openFile
always returnerror.IsDir
for directory paths on all platformsopenFile
open any type on all platforms and therefore make that the 'cross-platform' behavior (i.e. don't even have a function that returnserror.IsDir
when opening a directory as read-only) [this is the option I prefer the least, since it means that you'd have to do a stat call afterwards if you wanted theerror.IsDir
behavior (which wouldn't be necessary on windows if OpenFile was just called with.file_only
)]Note that all of these options would effectively close #5732 (since in option 1 & 2, a
stat
call would be done on non-Windows in the file-only version to get the matchingerror.IsDir
behavior)The text was updated successfully, but these errors were encountered: