Stream: helpdesk (published)

Topic: Check if (optionally existant) path is writable


view this post on Zulip Timothy (Nov 14 2022 at 09:14):

I just want to check if I can write to a file at a certain path. Unfortunately, this seems surprisingly hard :half_frown:.

Any suggestions?

view this post on Zulip jar (Nov 14 2022 at 09:43):

EAFP? In other words, don't check, just try writing and see if it fails.

view this post on Zulip Gunnar Farnebäck (Nov 14 2022 at 09:46):

uperm gives the permissions for the owner. Both /tmp and /root are (typically) owned by root and root has full permissions for them.

view this post on Zulip Timothy (Nov 14 2022 at 09:53):

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.

view this post on Zulip jar (Nov 14 2022 at 09:54):

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

view this post on Zulip Timothy (Nov 14 2022 at 10:22):

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.

view this post on Zulip Gunnar Farnebäck (Nov 14 2022 at 12:47):

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.

view this post on Zulip Timothy (Nov 14 2022 at 13:43):

I must say, for a simple check this is surprisingly complicated…

view this post on Zulip Sukera (Nov 14 2022 at 13:45):

it's not a simple check, being filesystem and OS specific

view this post on Zulip Gunnar Farnebäck (Nov 14 2022 at 14:58):

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.

view this post on Zulip Sukera (Nov 14 2022 at 15:07):

oh yes, NFS in particular will make a mess of things, simply due to NFS being a mess

view this post on Zulip Sukera (Nov 14 2022 at 15:08):

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? :)

view this post on Zulip Sukera (Nov 14 2022 at 15:09):

(talking about NFS specifically, not SMB or anything like that, which has its own set of things)


Last updated: Nov 22 2024 at 04:41 UTC