Ejemplo n.º 1
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];
 }
Ejemplo n.º 2
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;
         }
     }
 }