-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathScan.php
146 lines (124 loc) Β· 4.14 KB
/
Scan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\GroupFolders\Command;
use OC\Files\ObjectStore\ObjectStoreScanner;
use OCP\Constants;
use OCP\Files\Cache\IScanner;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class Scan extends FolderCommand {
protected function configure(): void {
$this
->setName('groupfolders:scan')
->setDescription('Scan a Team folder for outside changes')
->addArgument(
'folder_id',
InputArgument::OPTIONAL,
'Id of the Team folder to scan.'
)->addOption(
'all',
null,
InputOption::VALUE_NONE,
'Scan all the Team folders.'
)
->addOption(
'path',
'p',
InputOption::VALUE_REQUIRED,
'Limit rescan to this path, eg. --path="/shared/media/Music".'
)->addOption(
'shallow',
null,
InputOption::VALUE_NONE,
'Do not scan folders recursively.'
);
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$folderId = $input->getArgument('folder_id');
$all = $input->getOption('all');
if ($folderId === null && !$all) {
$output->writeln('Either a Team folder id or --all needs to be provided');
return -1;
}
if ($folderId !== null && $all) {
$output->writeln('Specifying a Team folder id and --all are mutually exclusive');
return -1;
}
if ($all) {
$folders = $this->folderManager->getAllFolders();
} else {
$folder = $this->getFolder($input, $output);
if ($folder === null) {
return -1;
}
$folders = [$folder['id'] => $folder];
}
$inputPath = $input->getOption('path');
if ($inputPath !== null) {
if (!is_string($inputPath)) {
$output->writeln('<error><path> option has to be a string</error>');
return -3;
}
$inputPath = '/' . trim($inputPath, '/');
} else {
$inputPath = '';
}
$recursive = !$input->getOption('shallow');
$stats = [];
foreach ($folders as $folder) {
$folderId = $folder['id'];
$statsRow = [$folderId, 0, 0, 0];
$mount = $this->mountProvider->getMount($folder['id'], '/' . $folder['mount_point'], Constants::PERMISSION_ALL, $folder['quota']);
/** @var IScanner&\OC\Hooks\BasicEmitter $scanner */
$scanner = $mount->getStorage()->getScanner();
$output->writeln("Scanning Team folder with id\t<info>{$folder['id']}</info>", OutputInterface::VERBOSITY_VERBOSE);
if ($scanner instanceof ObjectStoreScanner) {
$output->writeln('Scanning Team folders using an object store as primary storage is not supported.');
return -1;
}
$scanner->listen(\OC\Files\Cache\Scanner::class, 'scanFile', function (string $path) use ($output, &$statsRow): void {
$output->writeln("\tFile\t<info>/$path</info>", OutputInterface::VERBOSITY_VERBOSE);
$statsRow[2]++;
// abortIfInterrupted doesn't exist in nc14
if (method_exists($this, 'abortIfInterrupted')) {
$this->abortIfInterrupted();
}
});
$scanner->listen(\OC\Files\Cache\Scanner::class, 'scanFolder', function (string $path) use ($output, &$statsRow): void {
$output->writeln("\tFolder\t<info>/$path</info>", OutputInterface::VERBOSITY_VERBOSE);
$statsRow[1]++;
// abortIfInterrupted doesn't exist in nc14
if (method_exists($this, 'abortIfInterrupted')) {
$this->abortIfInterrupted();
}
});
$start = microtime(true);
$scanner->setUseTransactions(false);
$scanner->scan($inputPath, $recursive);
$end = microtime(true);
$statsRow[3] = date('H:i:s', (int)($end - $start));
$output->writeln('', OutputInterface::VERBOSITY_VERBOSE);
$stats[] = $statsRow;
}
$headers = [
'Folder Id', 'Folders', 'Files', 'Elapsed time'
];
$this->showSummary($headers, $stats, $output);
return 0;
}
protected function showSummary(array $headers, array $rows, OutputInterface $output): void {
$table = new Table($output);
$table
->setHeaders($headers)
->setRows($rows);
$table->render();
}
}