You are here

public function S3fsStream::rmdir in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::rmdir()
  2. 8.2 src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::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 PhpStreamWrapperInterface::rmdir

See also

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

File

src/StreamWrapper/S3fsStream.php, line 1010

Class

S3fsStream
Defines a Drupal s3 (s3://) stream wrapper class.

Namespace

Drupal\s3fs\StreamWrapper

Code

public function rmdir($uri, $options) {
  if (!$this
    ->isDir($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.
  $base_path = rtrim($uri, '/');
  $slash_path = $base_path . '/';

  // Check if the folder is empty.
  $query = \Drupal::database()
    ->select('s3fs_file', 's');
  $query
    ->fields('s')
    ->condition('uri', $query
    ->escapeLike($slash_path) . '%', 'LIKE');
  $file_count = $query
    ->countQuery()
    ->execute()
    ->fetchField();

  // If the folder is empty at time of query it is eligible for deletion.
  if ($file_count == 0) {

    // Suppress race triggerError('Subfolder is not empty').
    if (@parent::rmdir($this
      ->convertUriToKeyedPath($uri), $options)) {
      $this
        ->deleteCache($uri);
      clearstatcache(TRUE, $uri);
      return TRUE;
    }
  }

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