-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Add exclusive
flag to open() keywords (issue #33947)
#33949
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
base: master
Are you sure you want to change the base?
Conversation
Looks like Also needs a test. |
This needs some thought as to how it interacts with other boolean flag defaults. It seems like |
The manpage for |
That reads like |
Sorry, I should have included more of the manpage:
While the first choice seems like a nice shortcut, the second is more explicit in saying what it does. |
Could |
I'm not so sure that this API should be squeezed into the |
Assuming the set of imports below is the right place to reach in the meantime, I'd appreciate having the functionality somewhere (would never have patched myself it together without the link to Pidfile.jl in #33947): using Base: IOError, UV_EEXIST
using Base.Filesystem: JL_O_CREAT, JL_O_EXCL, JL_O_WRONLY, JL_O_RDWR
function tryopen_exclusive(f, path::String, mode::String="w"; modes::Integer = 0o444)
flag = if mode == "w+"
JL_O_RDWR
elseif mode == "w"
JL_O_WRONLY
else
throw(ArgumentError("`mode` can be \"w\" or \"w+\""))
end
try
io = Base.Filesystem.open(path, flag | JL_O_CREAT | JL_O_EXCL, modes)
try
f(io)
finally
close(io)
end
catch ex
(isa(ex, IOError) && ex.code == UV_EEXIST) || rethrow(ex)
return false
end
return true
end |
Fixes: #33947