Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * Retourne la base de données pour le point de montage du chemin donné.
  *
  * @param string $path
  *
  * @return SQLite3
  */
 public static function getDb($path)
 {
     if (array_key_exists($path, self::$dbs)) {
         $mountPath = $path;
     } else {
         $mountPath = Mount::getMountPath($path);
     }
     if (!array_key_exists($mountPath, self::$dbs)) {
         $dbPath = Mount::getTrashmanFolderPath($mountPath, true) . "/trashman.db";
         $shouldBeCreated = !file_exists($dbPath);
         $db = new SQLite3($dbPath);
         $db->busyTimeout(1000);
         if ($shouldBeCreated) {
             $db->exec("CREATE TABLE Paths (path TEXT, mount TEXT, priority INTEGER)");
             $db->exec("CREATE INDEX PathsI ON Paths (mount, priority)");
             $db->exec("CREATE UNIQUE INDEX PathsI2 ON Paths (path)");
         }
         self::$dbs[$mountPath] = $db;
     }
     return self::$dbs[$mountPath];
 }
Exemplo n.º 3
0
 /**
  * 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;
         }
     }
 }