Hello

While using this module on Linux I encountered a problem in the function _imagecache_mkdir($dir) (File: imagecache.module, Line 429). Using an absolut path, the leading slash is not written. So basically the first run of the foreach starts with an empty string. It's not a dir and not a file and fails at mkdir() at line 448.

Blame me, but maybe it happens only on my machine?

Anyway, I'd be happy if somone could have a look at it.

Following is my version of that function which works for me:

function _imagecache_mkdir($pathname) {
    is_dir(dirname($pathname)) || _imagecache_mkdir(dirname($pathname), 0775);
    return is_dir($pathname) || @mkdir($pathname, 0775);
}

Cheers

Nexo

Comments

pete-drupal’s picture

Assigned: Unassigned » pete-drupal

I can reproduce your problem in my environment

  • Drupal 5.7
  • PHP 5.2.0-8+etch11
  • Zend Engine v2.2.0
  • Debian Linux 4.x
  • MySQL database 5.0.32

and

  • ImageCache 5.x-2.0

The watchdog contained the error message:

"Could not create destination: %dir halted at: %path"

where

%dir was some absolute path (starting with '/')
%path was empty!

I solved it by modifying the function _imagecache_mkdir in file imagecache.module as follows:

/**
 * A recursive mkdir. Implements a Unix 'mkdir --parents --mode=755'
 */
function _imagecache_mkdir($dir) {
  $initial = substr($dir, 0) ? '/' : '';
  
  $folders = explode('/', $dir);

  foreach ($folders as $folder) {    

    // A leading '/' in $dir causes an empty $folder. This happens when $dir is an absolute path.
    // This is fact also the case in an otherwise valid path such as "/some//valid/path///name" which
    // translates into "<empty>/some/<empty>/valid/path/<empty>/<empty>/name". 
    // The following check skips all empty folders.
    if($folder == '') {
      continue; 
    }
    
    $dirs[] = $folder;

    $path =  $initial . implode('/', $dirs);
    if (is_dir($path)) {
      continue;
    }
    if (is_file($path)) {
      watchdog('imagecache', t('file exists where we would like a directory: %path', array('%path' => $path)), WATCHDOG_ERROR);
      return false;
    } 
    if (!@mkdir($path)) {
      watchdog('imagecache', t('Could not create destination: %dir halted at: %path', array('%dir' => $dir, '%path' => $path)), WATCHDOG_ERROR);
      return false;
    }
    if (!@chmod($path, 0775)) {
      watchdog('imagecache', t('Could not set permissons on created directory: %dir halted at: %path', array('%dir' => $dir, '%path' => $path)), WATCHDOG_ERROR);
      return false;
    } 
  }
  return true;
}

I would have created and attached a patch file, but forgot to check-in the original version of the module in my repository.

Note: according to http://www.php.net/mkdir, PHP5 is the first version that can create directories recursively while applying a 'chmod'

bool mkdir  ( string $pathname  [, int $mode  [, bool $recursive  [, resource $context  ]]] )

So, I guess the main reason why the function _imagecache_mkdir is iterating is to preserve PHP4 compatibility and possibly to output specific conditions to the watchdog?

pete-drupal’s picture

Assigned: pete-drupal » Unassigned
Status: Active » Needs review
StatusFileSize
new1.07 KB

Tested successfully

dopry’s picture

Status: Needs review » Fixed

Totally not concerned about php compat... I totally switched to mkdir... /me is so happy... thanks for reminding me pete. I was orignally trying to maintain php compat, but I dropped it... tnx. test the next dev snapshot for me, or CVS(DRUPAL-5--2) if you can.

pete-drupal’s picture

Version: 5.x-2.0 » 5.x-2.x-dev

Here is how I tested:

  1. I removed the directory containing the imagecache module and replaced it with the 5.x-2.x-dev snapshot (2008-May-20)
  2. I truncated all database cache* tables
  3. I removed all directories and files under /files/imagecache
  4. I accessed my gallery webpages. All pictures and directories are recreated under /files/imagecache as needed and displayed correctly.

Conclusion: Successfully tested. Feel free to close the issue.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.