You are here

public function S3fsStreamWrapper::rmdir in S3 File System 7.2

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

Support for rmdir().

Parameters

string $uri: 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 989
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 $uri with no / because folders are cached with no /.
  // We also need one with the /, because it might be a file in S3 that
  // ends with /. In addition, we must differentiate against 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 . '/';

  // If the folder is empty, it's eligible for deletion.
  $file_count = db_select('s3fs_file', 's')
    ->fields('s')
    ->condition('uri', db_like($slash_uri) . '%', 'LIKE')
    ->execute()
    ->rowCount();
  if ($file_count === 0) {
    $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;
}