I just want to check if I can write to a file at a certain path. Unfortunately, this seems surprisingly hard :half_frown:.
uperm
seems bugy, uperm("/tmp")
and uperm("/root")
both give 0x07
(!!)ccall(:access, Cint, (Cstring, Cint), path, 2)
(+ checking directory if file does not exist) seems to work, but I'm not sure how portable this isAny suggestions?
EAFP? In other words, don't check, just try writing and see if it fails.
uperm
gives the permissions for the owner. Both /tmp
and /root
are (typically) owned by root and root has full permissions for them.
jar said:
EAFP? In other words, don't check, just try writing and see if it fails.
That wouldn't be too bad, but I don't actually want to write to the path at this stage, just check if I could.
But it might change later between when you check and when you use it https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
That's a reasonable concern, but not a problem here.
I just need to check if I could write a file without actually trying to do so.
Your best bet is probably to ccall
to :jl_fs_access
, which wraps uv_fs_access
, which is libuv's attempt to make access
portable. But I somehow suspect that actually trying to open a file for writing is more robust.
I must say, for a simple check this is surprisingly complicated…
it's not a simple check, being filesystem and OS specific
In my experience even the OS won't always know the answer. The permissions may look fine but then it turns out that the filesystem was mounted in some odd way or if it's a network file system that the server has its own ideas about what it wants to allow.
oh yes, NFS in particular will make a mess of things, simply due to NFS being a mess
did you know that if you're locally root, you can pretend to be root on an NFS system and read all files mounted there? :)
(talking about NFS specifically, not SMB or anything like that, which has its own set of things)
Last updated: Dec 28 2024 at 04:38 UTC