You are here

public function S3fsStreamWrapper::rmdir in S3 File System 7

Same name and namespace in other branches
  1. 7.3 S3fsStreamWrapper.inc \S3fsStreamWrapper::rmdir()
  2. 7.2 S3fsStreamWrapper.inc \S3fsStreamWrapper::rmdir()

Support for rmdir().

Parameters

string $uri: A string containing the URI to the folder to delete.

int $options: A bit mask of STREAM_REPORT_ERRORS.

Return value

bool TRUE if folder is successfully removed. FALSE if $uri isn't a folder, or the folder is not empty.

Overrides StreamWrapperInterface::rmdir

See also

http://php.net/manual/en/streamwrapper.rmdir.php

File

./S3fsStreamWrapper.inc, line 905
Drupal stream wrapper implementation for S3 File System.

Class

S3fsStreamWrapper
The stream wrapper class.

Code

public function rmdir($uri, $options) {
  $this
    ->_assert_constructor_called();
  $this
    ->_debug("rmdir({$uri}, {$options}) called.");
  if (!$this
    ->_uri_is_dir($uri)) {
    return FALSE;
  }

  // We need a version of the URI with no / (folders are cached with no /),
  // and a version with the /, in case it's an object in S3, and to
  // differentiate it from files with this folder's name as a substring.
  // e.g. rmdir('s3://foo/bar') should ignore s3://foo/barbell.jpg.
  $bare_uri = rtrim($uri, '/');
  $slash_uri = $bare_uri . '/';

  // Check if the folder is empty.
  $files = db_select('s3fs_file', 's')
    ->fields('s')
    ->condition('uri', db_like($slash_uri) . '%', 'LIKE')
    ->execute()
    ->fetchAll(PDO::FETCH_ASSOC);

  // If the folder is empty, it's eligible for deletion.
  if (empty($files)) {
    $result = $this
      ->_delete_cache($bare_uri);
    clearstatcache(TRUE, $bare_uri);

    // Also delete the object from S3, if it's there.
    $params = $this
      ->_get_params($slash_uri);
    try {
      if ($this->s3
        ->doesObjectExist($params['Bucket'], $params['Key'])) {
        $this->s3
          ->deleteObject($params);
      }
    } catch (\Exception $e) {
      $this
        ->_debug($e
        ->getMessage());
      return $this
        ->_trigger_error($e
        ->getMessage());
    }
    return (bool) $result;
  }

  // The folder is non-empty.
  return FALSE;
}