-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
file: allow open/2 to work on directories #2212
Conversation
Thanks for the PR! I think it looks great aside from making it possible to open directories without the |
Hi In this case doesn’t the Windows system call return an error, like on Unix when you use the Or do you mean I should check and return |
Sorry about the confusing wording. The problem is that this PR makes it possible to open a directory without specifying the I'd like the behavior to be consistent across platforms, and I think it would be best to disallow opening directories without the |
This behaviour is not specified anywhere, is it? |
@jhogberg, it makes sense to be consistent; I should be able to work on it tomorrow. Do you have a preference regarding adding a new commit to the PR or rebasing to keep a single commit? @llelf, I’m not sure if it’s standardized, but it’s documented on a number of articles, e.g. https://lwn.net/Articles/457667/ and http://blog.httrack.com/blog/2013/11/15/everything-you-always-wanted-to-know-about-fsync/ Also, a descriptor-based API (e.g. fstat vs stay) is also useful to prevent some rare but possible race conditions on file access. I intend to work on allowing some functions in the |
We'd prefer rebasing in this case as it's just a fixup of a commit in the same PR, but feel free to keep them separate until it's time to merge it.
I believe it will be difficult to make this both consistent and portable. We've gone through great pains to make sure all file functions behave the same way on all platforms, often down to simulating the same error codes, and I can't see a nice way to make (for example) Working around these issues isn't impossible, but we've been toying with the idea of adding a new module ( |
I added a new commit to make it easier for you to review what's changed and then I'll rebase if you think it's ready. Regarding the fd-based APIs I was thinking of just allowing some already existing functions to also work over I/O devices, such as fstat, and indeed I saw the compatibility workarounds that you mentioned, particularly the get_drive_number() function (I think a version that works on handles could be written with GetFinalPathNameByHandleW, if I understood the docs correctly), and is_executable_file() which just checks the file extension (i.e. .exe, .bat, etc) -- I don't know if there's a way around for that one. At first I tried to implement these patches as separate module but I'd like to write NIFs that could operate on the file_descriptor record that the file module returns, and I'm not sure it's possible to share NIF resources across different modules like that. I ended up having to copy a lot of code from the file module NIFs, so I decided to give up this idea and go directly for the PR instead. My plan was indeed to have the |
Great, I think it looks good. It just occurred to me that not all platforms have (It can also be hidden behind various feature-test macros but those are often platform dependent. I can look into that later on.)
Fudging it with I don't think anyone cares in this specific case, but we're bound to collide with other annoying corner cases in other functions.
The idea is to use the same NIF but expose different interfaces to it. There will be some duplication of code but most of it can be shared.
We don't do platform-specific modules, what I have in mind is to The current idea is that the module should only support raw files without fancy options like We haven't done much beyond toying with the idea however, and I'm not opposed to adding descriptor-based functions to the |
John,
I've added a commit to do that. I used There's a bit of repetition in side the |
I don't mind the repetition, it looks good to me aside from needing to close the file before returning the error. |
e30f9ad
to
eab46e5
Compare
I added the close calls (also on windows) and took the liberty to rebase. |
Thanks, it looks good to me! We're a bit too close to include it in the OTP 22 release, so I'll add it too our nightly tests soon after that. |
Awesome, thanks! |
eab46e5
to
d65aeae
Compare
@jhogberg It took me a few weeks, but I finally managed to get a windows VM working for development, and I found a bug in the handling of the file open modes. The directory mode flag would never be handled in the original code, which I fixed by reordering the if/else if sequence. |
OTP 22's out so I've added this to our nightly tests. While looking through the PR again I noticed I'd missed an unrelated change in the |
This is useful mainly to ensure that a new file has been persisted to disk by calling file:sync/1 or file:datasync/1 on file's parent directory.
d65aeae
to
bd3f4f0
Compare
Done. I added |
Merged, thanks again for the PR! |
Thank you! |
This is useful mainly to ensure that a new file has been persisted to disk by calling file:sync/1 or file:datasync/1 on file's parent directory.
Note: I haven't been able to test on Windows, though I believe the changes are correct according to the docs for CreateFileW.