Commit 52c17c6d authored by webchick's avatar webchick

#373502 by drewish and sun: Add function to delete unmanaged files recurisevely (with tests).

parent 6afc39c2
...@@ -782,6 +782,7 @@ function file_delete($file, $force = FALSE) { ...@@ -782,6 +782,7 @@ function file_delete($file, $force = FALSE) {
* error. * error.
* *
* @see file_delete() * @see file_delete()
* @see file_unmanaged_delete_recursive()
*/ */
function file_unmanaged_delete($path) { function file_unmanaged_delete($path) {
if (is_dir($path)) { if (is_dir($path)) {
...@@ -797,10 +798,47 @@ function file_unmanaged_delete($path) { ...@@ -797,10 +798,47 @@ function file_unmanaged_delete($path) {
watchdog('file', 'The file %path was not deleted, because it does not exist.', array('%path' => $path), WATCHDOG_NOTICE); watchdog('file', 'The file %path was not deleted, because it does not exist.', array('%path' => $path), WATCHDOG_NOTICE);
return TRUE; return TRUE;
} }
// Catch all for everything else: sockets, symbolic links, etc. // We cannot handle anything other than files and directories. Log an error
// for everything else (sockets, symbolic links, etc).
watchdog('file', 'The file %path is not of a recognized type so it was not deleted.', array('%path' => $path), WATCHDOG_ERROR);
return FALSE; return FALSE;
} }
/**
* Recursively delete all files and directories in the specified filepath.
*
* If the specified path is a directory then the function will call itself
* recursively to process the contents. Once the contents have been removed the
* directory will also be removed.
*
* If the specified path is a file then it will be passed to
* file_unmanaged_delete().
*
* Note that this only deletes visible files with write permission.
*
* @param $path
* A string containing a file or directory path.
* @return
* TRUE for success or path does not exist, or FALSE in the event of an
* error.
*
* @see file_unmanaged_delete()
*/
function file_unmanaged_delete_recursive($path) {
if (is_dir($path)) {
$dir = dir($path);
while (($entry = $dir->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
file_unmanaged_delete_recursive($entry_path);
}
return rmdir($path);
}
return file_unmanaged_delete($path);
}
/** /**
* Determine total disk space used by a single user or the whole filesystem. * Determine total disk space used by a single user or the whole filesystem.
* *
......
...@@ -902,7 +902,7 @@ protected function tearDown() { ...@@ -902,7 +902,7 @@ protected function tearDown() {
global $db_prefix, $user; global $db_prefix, $user;
if (preg_match('/simpletest\d+/', $db_prefix)) { if (preg_match('/simpletest\d+/', $db_prefix)) {
// Delete temporary files directory and reset files directory path. // Delete temporary files directory and reset files directory path.
simpletest_clean_temporary_directory(file_directory_path()); file_unmanaged_delete_recursive(file_directory_path());
variable_set('file_directory_path', $this->originalFileDirectory); variable_set('file_directory_path', $this->originalFileDirectory);
// Remove all prefixed tables (all the tables in the schema). // Remove all prefixed tables (all the tables in the schema).
...@@ -1999,7 +1999,7 @@ protected function assertResponse($code, $message = '') { ...@@ -1999,7 +1999,7 @@ protected function assertResponse($code, $message = '') {
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code; $match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser')); return $this->assertTrue($match, $message ? $message : t('HTTP response expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
} }
/** /**
* TODO write documentation. * TODO write documentation.
* @param $type * @param $type
...@@ -2017,13 +2017,13 @@ protected function drupalCreateField($type, $field_name = NULL, $settings = arra ...@@ -2017,13 +2017,13 @@ protected function drupalCreateField($type, $field_name = NULL, $settings = arra
); );
$field_definition += $settings; $field_definition += $settings;
field_create_field($field_definition); field_create_field($field_definition);
$field = field_read_field($field_name); $field = field_read_field($field_name);
$this->assertTrue($field, t('Created field @field_name of type @type.', array('@field_name' => $field_name, '@type' => $type))); $this->assertTrue($field, t('Created field @field_name of type @type.', array('@field_name' => $field_name, '@type' => $type)));
return $field; return $field;
} }
/** /**
* TODO write documentation. * TODO write documentation.
* @param $field_name * @param $field_name
...@@ -2046,10 +2046,10 @@ protected function drupalCreateFieldInstance($field_name, $widget_type, $formatt ...@@ -2046,10 +2046,10 @@ protected function drupalCreateFieldInstance($field_name, $widget_type, $formatt
), ),
); );
field_create_instance($instance_definition); field_create_instance($instance_definition);
$instance = field_read_instance($field_name, $bundle); $instance = field_read_instance($field_name, $bundle);
$this->assertTrue($instance, t('Created instance of field @field_name on bundle @bundle.', array('@field_name' => $field_name, '@bundle' => $bundle))); $this->assertTrue($instance, t('Created instance of field @field_name on bundle @bundle.', array('@field_name' => $field_name, '@bundle' => $bundle)));
return $instance; return $instance;
} }
} }
......
...@@ -550,7 +550,7 @@ function simpletest_clean_temporary_directories() { ...@@ -550,7 +550,7 @@ function simpletest_clean_temporary_directories() {
foreach ($files as $file) { foreach ($files as $file) {
$path = file_directory_path() . '/' . $file; $path = file_directory_path() . '/' . $file;
if (is_dir($path) && preg_match('/^simpletest\d+/', $file)) { if (is_dir($path) && preg_match('/^simpletest\d+/', $file)) {
simpletest_clean_temporary_directory($path); file_unmanaged_delete_recursive($path);
$count++; $count++;
} }
} }
...@@ -563,27 +563,6 @@ function simpletest_clean_temporary_directories() { ...@@ -563,27 +563,6 @@ function simpletest_clean_temporary_directories() {
} }
} }
/**
* Remove all files from specified directory and then remove directory.
*
* @param string $path Directory path.
*/
function simpletest_clean_temporary_directory($path) {
$files = scandir($path);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$file_path = "$path/$file";
if (is_dir($file_path)) {
simpletest_clean_temporary_directory($file_path);
}
else {
file_unmanaged_delete($file_path);
}
}
}
rmdir($path);
}
/** /**
* Clear the test results tables. * Clear the test results tables.
*/ */
......
...@@ -849,6 +849,83 @@ class FileUnmanagedDeleteTest extends FileTestCase { ...@@ -849,6 +849,83 @@ class FileUnmanagedDeleteTest extends FileTestCase {
} }
/**
* Deletion related tests.
*/
class FileUnmanagedDeleteRecursiveTest extends FileTestCase {
function getInfo() {
return array(
'name' => t('Unmanaged recursive file delete'),
'description' => t('Tests the unmanaged file delete recursive function.'),
'group' => t('File'),
);
}
/**
* Delete a normal file.
*/
function testSingleFile() {
// Create a file for testing
$filepath = file_directory_path() . '/' . $this->randomName();
file_put_contents($filepath, '');
// Delete the file.
$this->assertTrue(file_unmanaged_delete_recursive($filepath), t('Function reported success.'));
$this->assertFalse(file_exists($filepath), t('Test file has been deleted.'));
}
/**
* Try deleting an empty directory.
*/
function testEmptyDirectory() {
// A directory to operate on.
$directory = $this->createDirectory();
// Delete the directory.
$this->assertTrue(file_unmanaged_delete_recursive($directory), t('Function reported success.'));
$this->assertFalse(file_exists($directory), t('Directory has been deleted.'));
}
/**
* Try deleting a directory with some files.
*/
function testDirectory() {
// A directory to operate on.
$directory = $this->createDirectory();
$filepathA = $directory . '/A';
$filepathB = $directory . '/B';
file_put_contents($filepathA, '');
file_put_contents($filepathB, '');
// Delete the directory.
$this->assertTrue(file_unmanaged_delete_recursive($directory), t('Function reported success.'));
$this->assertFalse(file_exists($filepathA), t('Test file A has been deleted.'));
$this->assertFalse(file_exists($filepathB), t('Test file B has been deleted.'));
$this->assertFalse(file_exists($directory), t('Directory has been deleted.'));
}
/**
* Try deleting subdirectories with some files.
*/
function testSubDirectory() {
// A directory to operate on.
$directory = $this->createDirectory();
$subdirectory = $this->createDirectory($directory . '/sub');
$filepathA = $directory . '/A';
$filepathB = $subdirectory . '/B';
file_put_contents($filepathA, '');
file_put_contents($filepathB, '');
// Delete the directory.
$this->assertTrue(file_unmanaged_delete_recursive($directory), t('Function reported success.'));
$this->assertFalse(file_exists($filepathA), t('Test file A has been deleted.'));
$this->assertFalse(file_exists($filepathB), t('Test file B has been deleted.'));
$this->assertFalse(file_exists($subdirectory), t('Subdirectory has been deleted.'));
$this->assertFalse(file_exists($directory), t('Directory has been deleted.'));
}
}
/** /**
* Unmanaged move related tests. * Unmanaged move related tests.
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment