Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a directory is writable by a given UID?

We can test if a directory is writable by the uid of the current process:

if [ -w $directory ] ; then echo 'Eureka!' ; fi 

But can anyone suggest a way to test if a directory is writable by some other uid?

My scenario is that I am administering a MySQL Server instance, and I want to change the location of the slow-query log file temporarily. I can do this by executing a MySQL command SET GLOBAL slow_query_log_file='$new_log_filename' and then disable & enable query logging to make mysqld start using that file.

But I'd like my script to check that the uid of the mysqld process has permissions to create that new log file. So I'd like to do something like (pseudocode):

$ if [ -w-as-mysql-uid `basename $new_log_filename` ] ; then echo 'Eureka!' ; fi 

But of course that's an imaginary test predicate.

Clarification: I would like a solution that doesn't rely on su because I can't assume the user of my script has su privilege.

like image 602
Bill Karwin Avatar asked Dec 31 '12 16:12

Bill Karwin


People also ask

How can I tell if a directory is writeable?

PHP | is_writable() Function The name of the file is sent as a parameter to the is_writable() function and it returns True if the filename exists and is writable. Name of a directory can also be a parameter to the is_writable() function which allows checking whether the directory is writable or not.

How do I check if a directory is writable in Python?

access(path, os. W_OK) should do what you ask for. If you instead want to know whether you can write to the directory, open() a test file for writing (it shouldn't exist beforehand), catch and examine any IOError , and clean up the test file afterwards.

How do you check if a user can access a given file?

You can run test -r /path/to/file; echo "$?" to view the return code of the test command. Use test -w to test for write permission and test -x to test for execute permission.

Is used for checking if the file is writable or not?

How to check for a file and whether it is readable and writable. The bash shell test command has many more options as follows: -w FILE : FILE exists and write permission is granted. -x FILE : FILE exists and execute (or search) permission is granted.


1 Answers

Here's a long, roundabout way of checking.

USER=johndoe DIR=/path/to/somewhere  # Use -L to get information about the target of a symlink, # not the link itself, as pointed out in the comments INFO=( $(stat -L -c "%a %G %U" "$DIR") ) PERM=${INFO[0]} GROUP=${INFO[1]} OWNER=${INFO[2]}  ACCESS=no if (( ($PERM & 0002) != 0 )); then     # Everyone has write access     ACCESS=yes elif (( ($PERM & 0020) != 0 )); then     # Some group has write access.     # Is user in that group?     gs=( $(groups $USER) )     for g in "${gs[@]}"; do         if [[ $GROUP == $g ]]; then             ACCESS=yes             break         fi     done elif (( ($PERM & 0200) != 0 )); then     # The owner has write access.     # Does the user own the file?     [[ $USER == $OWNER ]] && ACCESS=yes fi 
like image 161
chepner Avatar answered Sep 25 '22 13:09

chepner