Skip to content

Commit

Permalink
add dry-run option to files_external:notify
Browse files Browse the repository at this point in the history
and add some additional logging

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Oct 14, 2020
1 parent a41b273 commit c1e354a
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions apps/files_external/lib/Command/Notify.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ protected function configure() {
'',
InputOption::VALUE_NONE,
'Disable self check on startup'
)->addOption(
'dry-run',
'',
InputOption::VALUE_NONE,
'Don\'t make any changes, only log detected changes'
);
parent::configure();
}
Expand Down Expand Up @@ -181,7 +186,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$verbose = $input->getOption('verbose');
$dryRun = $input->getOption('dry-run');
$verbose = $input->getOption('verbose') || $dryRun;

$path = trim($input->getOption('path'), '/');
$notifyHandler = $storage->notify($path);
Expand All @@ -190,14 +196,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->selfTest($storage, $notifyHandler, $verbose, $output);
}

$notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output) {
$notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output, $dryRun) {
if ($verbose) {
$this->logUpdate($change, $output);
}
if ($change instanceof IRenameChange) {
$this->markParentAsOutdated($mount->getId(), $change->getTargetPath(), $output);
$this->markParentAsOutdated($mount->getId(), $change->getTargetPath(), $output, $verbose, $dryRun);
}
$this->markParentAsOutdated($mount->getId(), $change->getPath(), $output);
$this->markParentAsOutdated($mount->getId(), $change->getPath(), $output, $verbose, $dryRun);
});
return 0;
}
Expand All @@ -207,29 +213,45 @@ private function createStorage(StorageConfig $mount) {
return new $class($mount->getBackendOptions());
}

private function markParentAsOutdated($mountId, $path, OutputInterface $output) {
private function markParentAsOutdated($mountId, $path, OutputInterface $output, bool $verbose, bool $dryRun) {
$parent = ltrim(dirname($path), '/');
if ($parent === '.') {
$parent = '';
}

try {
$storageIds = $this->getStorageIds($mountId);
$storages = $this->getStorageIds($mountId, $parent);
} catch (DriverException $ex) {
$this->logger->logException($ex, ['message' => 'Error while trying to find correct storage ids.', 'level' => ILogger::WARN]);
$this->connection = $this->reconnectToDatabase($this->connection, $output);
$output->writeln('<info>Needed to reconnect to the database</info>');
$storageIds = $this->getStorageIds($mountId);
$storages = $this->getStorageIds($mountId, $path);
}
if (count($storageIds) === 0) {
throw new StorageNotAvailableException('No storages found by mount ID ' . $mountId);
if (count($storages) === 0) {
return;
}

$users = array_map(function(array $storage) {
return $storage['user_id'];
},$storages);

if ($verbose) {
$output->writeln(" marking '$parent' as outdated for " . implode(', ', $users));
}
$storageIds = array_map('intval', $storageIds);

$result = $this->updateParent($storageIds, $parent);
if ($result === 0) {
//TODO: Find existing parent further up the tree in the database and register that folder instead.
$this->logger->info('Failed updating parent for "' . $path . '" while trying to register change. It may not exist in the filecache.');
$storageIds = array_map(function(array $storage) {
return intval($storage['storage_id']);
},$storages);
$storageIds = array_values(array_unique($storageIds));

if ($dryRun) {
$output->writeln(" dry-run: skipping database write");
} else {
$result = $this->updateParent($storageIds, $parent);
if ($result === 0) {
//TODO: Find existing parent further up the tree in the database and register that folder instead.
$this->logger->info('Failed updating parent for "' . $path . '" while trying to register change. It may not exist in the filecache.');
}
}
}

Expand Down Expand Up @@ -259,18 +281,17 @@ private function logUpdate(IChange $change, OutputInterface $output) {
$output->writeln($text);
}

/**
* @param int $mountId
* @return array
*/
private function getStorageIds($mountId) {
private function getStorageIds(int $mountId, string $path): array {
$pathHash = md5(trim(\OC_Util::normalizeUnicode($path), '/'));
$qb = $this->connection->getQueryBuilder();
return $qb
->select('storage_id')
->from('mounts')
->select('storage_id', 'user_id')
->from('mounts', 'm')
->innerJoin('m', 'filecache', 'f', $qb->expr()->eq('m.storage_id', 'f.storage'))
->where($qb->expr()->eq('mount_id', $qb->createNamedParameter($mountId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('path_hash', $qb->createNamedParameter($pathHash, IQueryBuilder::PARAM_STR)))
->execute()
->fetchAll(\PDO::FETCH_COLUMN);
->fetchAll();
}

/**
Expand Down

0 comments on commit c1e354a

Please sign in to comment.