예제 #1
0
 /**
  * Return the single depth list of files and subdirectories in $directory ordered by total filesize
  *
  * Will schedule background threads to recursively calculate the filesize of subdirectories.
  * The total filesize of each directory and subdirectory is cached in a transient for 1 week.
  *
  * @param string $directory The directory to scan
  *
  * @return array returns an array of files ordered by filesize
  */
 public function list_directory_by_total_filesize($directory)
 {
     $files = $files_with_no_size = $empty_files = $files_with_size = $unreadable_files = array();
     if (!is_dir($directory)) {
         return $files;
     }
     $found = array();
     if (!empty($this->files)) {
         return $this->files;
     }
     $default_excludes = $this->backup->default_excludes();
     $finder = new Finder();
     $finder->ignoreDotFiles(false);
     $finder->ignoreUnreadableDirs();
     $finder->followLinks();
     $finder->depth('== 0');
     foreach ($default_excludes as $exclude) {
         $finder->notPath($exclude);
     }
     foreach ($finder->in($directory) as $entry) {
         $files[] = $entry;
         // Get the total filesize for each file and directory
         $filesize = $this->filesize($entry);
         if ($filesize) {
             // If there is already a file with exactly the same filesize then let's keep increasing the filesize of this one until we don't have a clash
             while (array_key_exists($filesize, $files_with_size)) {
                 $filesize++;
             }
             $files_with_size[$filesize] = $entry;
         } elseif (0 === $filesize) {
             $empty_files[] = $entry;
         } else {
             $files_with_no_size[] = $entry;
         }
     }
     // Add 0 byte files / directories to the bottom
     $files = $files_with_size + array_merge($empty_files, $unreadable_files);
     // Add directories that are still calculating to the top
     if ($files_with_no_size) {
         // We have to loop as merging or concatenating the array would re-flow the keys which we don't want because the filesize is stored in the key
         foreach ($files_with_no_size as $entry) {
             array_unshift($files, $entry);
         }
     }
     return $files;
 }