/** * Recursively scans a directory to calculate the total filesize * * Locks should be set by the caller with `set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS );` * * @return array $directory_sizes An array of directory paths => filesize sum of all files in directory */ public function recursive_filesize_scanner() { // Use the cached array directory sizes if available $directory_sizes = get_transient('hmbkp_directory_filesizes'); // If we do have it in cache then let's use it and also clear the lock if (is_array($directory_sizes)) { delete_transient('hmbkp_directory_filesizes_running'); return $directory_sizes; } // If we don't have it cached then we'll need to re-calculate $files = $this->backup->get_files(); foreach ($files as $file) { if ($file->isReadable()) { $directory_sizes[wp_normalize_path($file->getRealpath())] = $file->getSize(); } else { $directory_sizes[wp_normalize_path($file->getRealpath())] = 0; } } set_transient('hmbkp_directory_filesizes', $directory_sizes, DAY_IN_SECONDS); delete_transient('hmbkp_directory_filesizes_running'); return $directory_sizes; }
/** * Recursively scans a directory to calculate the total filesize * * Locks should be set by the caller with `set_transient( 'hmbkp_directory_filesizes_running', true, HOUR_IN_SECONDS );` * * @return array $directory_sizes An array of directory paths => filesize sum of all files in directory */ public function recursive_filesize_scanner() { // Use the cached array directory sizes if available $directory_sizes = get_transient('hmbkp_directory_filesizes'); // If we do have it in cache then let's use it and also clear the lock if (is_array($directory_sizes)) { delete_transient('hmbkp_directory_filesizes_running'); return $directory_sizes; } $files = $this->backup->get_files(); foreach ($files as $file) { if ($file->isReadable()) { $directory_sizes[Backup::conform_dir($file->getRealpath())] = $file->getSize(); } else { $directory_sizes[Backup::conform_dir($file->getRealpath())] = 0; } } // This will be the total size of the included folders MINUS default excludes. $directory_sizes[$this->backup->get_root()] = array_sum($directory_sizes); set_transient('hmbkp_directory_filesizes', $directory_sizes, DAY_IN_SECONDS); delete_transient('hmbkp_directory_filesizes_running'); return $directory_sizes; }