Example #1
0
 /**
  * Supprime le contenu du dossier, dans l'ordre alphabétique, jusqu'à ce que le nombre d'octets donné soit atteint.
  * @param string $path
  * @param string $amount
  */
 private function doDelete($path, $amount)
 {
     if (@lstat($path) === false) {
         // Chemin inexistant.
         Logger::debug("Chemin inexistant : {$path}");
         Database::deletePath($path);
         return $amount;
     }
     $dryRun = $this->in->getOption('dry-run');
     if (Utils::bcmax('0', $amount) === '0') {
         // Plus rien à supprimer, on arrête.
         return $amount;
     }
     Logger::log(Utils::humanFilesize($amount) . " " . $path);
     if (is_dir($path)) {
         // On scanne tous les sous-dossiers & fichiers.
         Logger::debug("{$path} est un dossier.");
         $scan = scandir($path, SCANDIR_SORT_ASCENDING);
         foreach ($scan as $subPath) {
             if (preg_match('~^\\.{1,2}$~', $subPath)) {
                 // Raccourcis "." et ".." => ignorés.
                 continue;
             }
             $amount = $this->doDelete($path . '/' . $subPath, $amount);
         }
         // Si le dossier est vide après suppression, on le supprime.
         $scan = scandir($path, SCANDIR_SORT_ASCENDING);
         if (count($scan) === 2) {
             // Dossier vide, on le supprime.
             if (!$dryRun) {
                 Logger::debug("Suppression du dossier vide : {$path}");
                 rmdir($path);
                 Database::deletePath($path);
             }
             return $amount;
         }
     } elseif (is_file($path) || is_link($path)) {
         $amount = bcsub($amount, trim(shell_exec("stat -c%s " . escapeshellarg($path))));
         if (!$dryRun) {
             Logger::debug("Suppression du fichier : {$path}");
             unlink($path);
             Database::deletePath($path);
         }
     } else {
         Logger::error("Type de fichier non géré : " . $path);
         if (!$dryRun) {
             Database::deletePath($path);
         }
     }
     return $amount;
 }