SHARE
TWEET

Xcode Remover

a guest Aug 17th, 2013 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. $texts = scandir('/tmp/xcoderemove');
  4. foreach($texts as $k => $v){
  5.         if( substr($v, -4) != '.txt' ){ unset($texts[$k]); }
  6. }
  7.  
  8. function is_dir_empty($dir) {
  9.   if (!is_readable($dir)) return NULL;
  10.   $handle = opendir($dir);
  11.   while (false !== ($entry = readdir($handle))) {
  12.     if ($entry != "." && $entry != ".." && $entry != ".DS_Store") {
  13.       closedir($handle);
  14.       return FALSE;
  15.     }
  16.   }
  17.   closedir($handle);
  18.   return TRUE;
  19. }
  20.  
  21. $i = 1;
  22. while( true ){
  23.         echo PHP_EOL.'** PASS ' . $i . '...'.PHP_EOL.PHP_EOL;
  24.  
  25.         $orphans = array(
  26.         'files' => array(),
  27.         'folders' => array(),
  28.         );
  29.        
  30.         // parse all text files and build a list of orphaned files/folders
  31.         foreach($texts as $f){
  32.                 $file = "/tmp/xcoderemove/".$f;
  33.                 $lines = file($file);
  34.                 foreach($lines as $line){
  35.                         $line = trim($line);
  36.                         // each line is like ./System/Library/Extensions/RemoteVirtualInterface.kext/Contents/MacOS/RemoteVirtualInterface
  37.                         // we need to translate this and step through it one by one until we are at /
  38.                         $line = preg_replace('/^\.\//', '/', $line);
  39.                         while ($line != '/' && $line != ''){
  40.                                 // check if the path component exists, and is a file
  41.                                 if( is_file( $line ) ){
  42.                                         $orphans['files'][$line] = TRUE;
  43.                                 }
  44.                                 // check if the path component exists, and is an empty folder
  45.                                 elseif( is_dir( $line ) && is_dir_empty( $line ) ){
  46.                                         $orphans['folders'][$line] = TRUE;
  47.                                 }
  48.                                
  49.                                 // prepare for the next iteration by removing the last path component
  50.                                 $line = preg_replace('/\/[^\/]+$/', '', $line);
  51.                         }
  52.                 }
  53.         }
  54.        
  55.         // end the loop if there's no more orphans
  56.         if( count($orphans['files']) == 0 && count($orphans['folders']) == 0 ){
  57.                 echo PHP_EOL.'** ALL DONE!'.PHP_EOL;
  58.                 break;
  59.         }
  60.        
  61.         // delete all orphan Framework Headers folders (these folders are safe to delete as they were created by the command line tools installer)
  62.         echo '***** REMOVING ORPHAN FRAMEWORK HEADERS FOLDERS *****' . PHP_EOL;
  63.         foreach( $orphans['folders'] as $folder => $bleh ){
  64.                 // if the folder ends with /Headers, or has /Headers/ in its path,
  65.                 if( preg_match( '/^.+?\/Frameworks\/.+?\/Headers(?:$|\/)/', $folder ) ){
  66.                         echo ' => ' . $folder . PHP_EOL;
  67.                         if( !@rmdir( $folder ) ){
  68.                                 // remove .DS_Store if it exists in the folder and is the cause of the failure
  69.                                 $subcontents = scandir( $folder );
  70.                                 if( count($subcontents) == 3 && in_array( '.DS_Store', $subcontents ) ){ // ., .. and .DS_Store
  71.                                         unlink( $folder.'/.DS_Store' ); // remove .DS_Store
  72.                                 }
  73.                                 // try again 1 more time:
  74.                                 if( !@rmdir( $folder ) ){
  75.                                         echo ' ==> FAILED TO REMOVE. NOT EMPTY?'.PHP_EOL;
  76.                                 }
  77.                         }
  78.                         unset( $orphans['folders'][$folder] );
  79.                 }
  80.         }
  81.        
  82.         // delete other folders. I verified that it's all bullshit too. safe to delete.
  83.         echo '***** REMOVING OTHER ORPHAN FOLDERS *****' . PHP_EOL;
  84.         foreach( $orphans['folders'] as $folder => $bleh ){
  85.                 echo ' => ' . $folder . PHP_EOL;
  86.                 if( !@rmdir( $folder ) ){
  87.                         // remove .DS_Store if it exists in the folder and is the cause of the failure
  88.                         $subcontents = scandir( $folder );
  89.                         if( count($subcontents) == 3 && in_array( '.DS_Store', $subcontents ) ){ // ., .. and .DS_Store
  90.                                 unlink( $folder.'/.DS_Store' ); // remove .DS_Store
  91.                         }
  92.                         // try again 1 more time:
  93.                         if( !@rmdir( $folder ) ){
  94.                                 echo ' ==> FAILED TO REMOVE. NOT EMPTY?'.PHP_EOL;
  95.                         }
  96.                 }
  97.                 unset( $orphans['folders'][$folder] );
  98.         }
  99.        
  100.         echo '***** ANY UNHANDLED ORPHANS: *****' . PHP_EOL;
  101.         print_r($orphans);
  102. }
  103.  
  104. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top