/** * Retourne le chemin vers le dossier de stockage de Trashman, pour un montage. * @param string $mountPath * @return string */ public static function getTrashmanFolderPath($mountPath, $create = false) { $path = preg_replace('~/+~', '/', $mountPath . '/.trashman'); if (!is_writeable($path)) { $trashmanHomePath = getenv('HOME') . '/.trashman'; $path = $trashmanHomePath . "/" . Utils::shortHash($mountPath); $configFile = getenv('HOME') . "/.trashman.yml"; $forcedTrashmanFolders = array(); if (file_exists($configFile)) { $config = Yaml::parse($configFile); if (array_key_exists('forcedTrashmanFolders', $config)) { $forcedTrashmanFolders = $config['forcedTrashmanFolders']; if (array_key_exists($mountPath, $forcedTrashmanFolders)) { $path = $forcedTrashmanFolders[$mountPath]; } } } // On vérifie que le dossier soit bien sur le même point de montage. // Sinon, on ne peut rien faire. if (Mount::getMountPath($path) !== Mount::getMountPath($mountPath)) { throw new Exception('"' . $path . '" et "' . $mountPath . '" sont sur des systèmes de fichiers différents.'); } } if ($create && !file_exists($path)) { mkdir($path, 0777, true); } return $path; }
/** * Déplace ou marque des fichiers pour suppression ultérieure. * * @param InputInterface $input * @param OutputInterface $output */ public function trash(InputInterface $input, OutputInterface $output) { $dryRun = $this->in->getOption('dry-run'); $priority = $this->in->getOption('priority'); if (!is_numeric($priority)) { throw new Exception("Priorité invalide : " . $priority); } $priority = intval($priority); foreach ($input->getArgument('paths') as $path) { if (preg_match("~^\\.{1,2}\$~", $path)) { continue; } if (!file_exists($path)) { Logger::error('Le chemin "' . $path . '" n\'existe pas.'); continue; } $path = realpath($path); Logger::log($path); $mountPath = Mount::getMountPath($path); Logger::debug("Mountpoint pour " . $path . " : " . $mountPath); if ($priority === 0) { // Suppression immédiate. On déplace le fichier dans le dossier dédié. try { $toDeleteFolderPath = Mount::getTrashmanFolderPath($mountPath, true) . "/toDelete"; } catch (Exception $e) { // Impossible de traiter ce chemin. Logger::error($e->getMessage()); continue; } if (!file_exists($toDeleteFolderPath)) { mkdir($toDeleteFolderPath, 0777, true); } $destName = Utils::shortHash($path . microtime(true)); $destPath = $toDeleteFolderPath . '/' . $destName; if (!$dryRun) { rename($path, $destPath); } Logger::log($path . ' -> ' . $destPath); continue; } // Suppression délayée. try { if (!$dryRun) { Database::insert($path, $mountPath, $priority); } } catch (Exception $e) { // Impossible de traiter ce chemin. Logger::error($e->getMessage()); continue; } } }