background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

There is nothing more frustrating than not having permissions set correctly on a server. I recently tried to commit a bunch of files to subversion and received the following error:

svn: Can't create directory 'sites/default/files/some/path/.svn': Permission denied

This usually indicates your user does not have permission to alter the .svn folders to execute the subversion commit command. The failed command will leave your subversion status with a tilde (~):

$ svn stat
~      sites/default/files/some/path
~      sites/default/files/some/other/path

You'll first need to reset permissions and ownership:

# change directories, you don't want to reset every file permission
$ cd sites/default/files

# use a find command and "-exec" switch to reset ownership and permissions
# NOTE: the group, owner, and permissions will vary on every server configuration
$ find . -exec chown Eric.apache {} \; -exec chmod -R ug+rw {} \;

Now, you can revert the files to remove the tilde (~) status:

$ svn stat
~      sites/default/files/some/path
~      sites/default/files/some/other/path
$ svn revert "sites/default/files/some/path"
$ svn revert "sites/default/files/some/other/path"
$ svn stat
?      sites/default/files/some/path
?      sites/default/files/some/other/path

At this point, you should be able to re-add the files to subversion and commit.

Recently, I was working with the file_get_contents() and file_put_contents() functions to read in a file, change its contents, and write it to the file system. Although the is_writable() function returned TRUE, my file_put_contents() command was returning FALSE. This lead me to try a fopen command with the "w" switch (which opens a file for writing and truncates it), and still no luck. Although the file had group permissions to read and write on the file system, the file was owned by a non-apache user. In certain PHP configurations (safe mode, etc) this will not work.

I then created a hook_requirements() function to test for the proper file permissions for my custom module. When I deployed my code to the test server, I got a white screen, which usually signifies a PHP fatal error. Upon review of the apache vhost error log, I saw the function I used "posix_getpwuid" was not defined.

In the end I decided to create a helper function that returns the owner of a file:

<?php
function _MYMODULE_get_file_owner($file) {
   
 
// if posix library is installed:
 
if (function_exists('posix_getpwuid')) {
   
$owner = fileowner($file);
   
$ownerInfo = posix_getpwuid($owner);
    if (
$ownerInfo['name']) return $ownerInfo['name'];
  }

 
// try using ls & awk
 
$command = "ls -l $file | awk '{print $3}'";
 
$result = exec($command);
  if (
$result) return $result;
   
  return
false;

}
?>

NOTE: the "stat" shell function could also be used:

<?php
$command
= "stat -c %U $file";
?>

Eric.London's picture

If you've ever gotten the following error, you might need to reset the file permissions and ownership on all of your project files, including the hidden subversion files (located in the .svn folders). This can occur if you've ever executed a subversion command as root, which I try to avoid doing.

svn: Can't open file 'PATH/TO/YOUR/FILES/.svn/lock': Permission denied

The following commands can be used to reset the permissions and ownership for all the files in your directory. NOTE: only execute these commands if you feel comfortable with the shell and know what the file permissions should be set to for your files.

# go to the path of your project
cd /PATH/TO/MY/PROJECT

# reset ownership
# NOTE: replace apache.staff with your user and group
sudo find . -exec chown apache.staff {} \;

# reset permissions
# NOTE: replace 2770 with your file permissions
sudo find . -exec chmod 2770 {} \;

# Now you can run the cleanup command to repair your .svn folders
svn cleanup

Syndicate content