Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $texts = scandir('/tmp/xcoderemove');
- foreach($texts as $k => $v){
- if( substr($v, -4) != '.txt' ){ unset($texts[$k]); }
- }
- function is_dir_empty($dir) {
- if (!is_readable($dir)) return NULL;
- $handle = opendir($dir);
- while (false !== ($entry = readdir($handle))) {
- if ($entry != "." && $entry != ".." && $entry != ".DS_Store") {
- closedir($handle);
- return FALSE;
- }
- }
- closedir($handle);
- return TRUE;
- }
- $i = 1;
- while( true ){
- echo PHP_EOL.'** PASS ' . $i . '...'.PHP_EOL.PHP_EOL;
- $orphans = array(
- 'files' => array(),
- 'folders' => array(),
- );
- // parse all text files and build a list of orphaned files/folders
- foreach($texts as $f){
- $file = "/tmp/xcoderemove/".$f;
- $lines = file($file);
- foreach($lines as $line){
- $line = trim($line);
- // each line is like ./System/Library/Extensions/RemoteVirtualInterface.kext/Contents/MacOS/RemoteVirtualInterface
- // we need to translate this and step through it one by one until we are at /
- $line = preg_replace('/^\.\//', '/', $line);
- while ($line != '/' && $line != ''){
- // check if the path component exists, and is a file
- if( is_file( $line ) ){
- $orphans['files'][$line] = TRUE;
- }
- // check if the path component exists, and is an empty folder
- elseif( is_dir( $line ) && is_dir_empty( $line ) ){
- $orphans['folders'][$line] = TRUE;
- }
- // prepare for the next iteration by removing the last path component
- $line = preg_replace('/\/[^\/]+$/', '', $line);
- }
- }
- }
- // end the loop if there's no more orphans
- if( count($orphans['files']) == 0 && count($orphans['folders']) == 0 ){
- echo PHP_EOL.'** ALL DONE!'.PHP_EOL;
- break;
- }
- // delete all orphan Framework Headers folders (these folders are safe to delete as they were created by the command line tools installer)
- echo '***** REMOVING ORPHAN FRAMEWORK HEADERS FOLDERS *****' . PHP_EOL;
- foreach( $orphans['folders'] as $folder => $bleh ){
- // if the folder ends with /Headers, or has /Headers/ in its path,
- if( preg_match( '/^.+?\/Frameworks\/.+?\/Headers(?:$|\/)/', $folder ) ){
- echo ' => ' . $folder . PHP_EOL;
- if( !@rmdir( $folder ) ){
- // remove .DS_Store if it exists in the folder and is the cause of the failure
- $subcontents = scandir( $folder );
- if( count($subcontents) == 3 && in_array( '.DS_Store', $subcontents ) ){ // ., .. and .DS_Store
- unlink( $folder.'/.DS_Store' ); // remove .DS_Store
- }
- // try again 1 more time:
- if( !@rmdir( $folder ) ){
- echo ' ==> FAILED TO REMOVE. NOT EMPTY?'.PHP_EOL;
- }
- }
- unset( $orphans['folders'][$folder] );
- }
- }
- // delete other folders. I verified that it's all bullshit too. safe to delete.
- echo '***** REMOVING OTHER ORPHAN FOLDERS *****' . PHP_EOL;
- foreach( $orphans['folders'] as $folder => $bleh ){
- echo ' => ' . $folder . PHP_EOL;
- if( !@rmdir( $folder ) ){
- // remove .DS_Store if it exists in the folder and is the cause of the failure
- $subcontents = scandir( $folder );
- if( count($subcontents) == 3 && in_array( '.DS_Store', $subcontents ) ){ // ., .. and .DS_Store
- unlink( $folder.'/.DS_Store' ); // remove .DS_Store
- }
- // try again 1 more time:
- if( !@rmdir( $folder ) ){
- echo ' ==> FAILED TO REMOVE. NOT EMPTY?'.PHP_EOL;
- }
- }
- unset( $orphans['folders'][$folder] );
- }
- echo '***** ANY UNHANDLED ORPHANS: *****' . PHP_EOL;
- print_r($orphans);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement