Skip to content

Commit cf26569

Browse files
committed
add command to repair broken filesystem trees
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent b9287f9 commit cf26569

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

apps/files/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<command>OCA\Files\Command\DeleteOrphanedFiles</command>
3535
<command>OCA\Files\Command\TransferOwnership</command>
3636
<command>OCA\Files\Command\ScanAppData</command>
37+
<command>OCA\Files\Command\RepairTree</command>
3738
</commands>
3839

3940
<activity>

apps/files/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'OCA\\Files\\Collaboration\\Resources\\Listener' => $baseDir . '/../lib/Collaboration/Resources/Listener.php',
2828
'OCA\\Files\\Collaboration\\Resources\\ResourceProvider' => $baseDir . '/../lib/Collaboration/Resources/ResourceProvider.php',
2929
'OCA\\Files\\Command\\DeleteOrphanedFiles' => $baseDir . '/../lib/Command/DeleteOrphanedFiles.php',
30+
'OCA\\Files\\Command\\RepairTree' => $baseDir . '/../lib/Command/RepairTree.php',
3031
'OCA\\Files\\Command\\Scan' => $baseDir . '/../lib/Command/Scan.php',
3132
'OCA\\Files\\Command\\ScanAppData' => $baseDir . '/../lib/Command/ScanAppData.php',
3233
'OCA\\Files\\Command\\TransferOwnership' => $baseDir . '/../lib/Command/TransferOwnership.php',

apps/files/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ComposerStaticInitFiles
4242
'OCA\\Files\\Collaboration\\Resources\\Listener' => __DIR__ . '/..' . '/../lib/Collaboration/Resources/Listener.php',
4343
'OCA\\Files\\Collaboration\\Resources\\ResourceProvider' => __DIR__ . '/..' . '/../lib/Collaboration/Resources/ResourceProvider.php',
4444
'OCA\\Files\\Command\\DeleteOrphanedFiles' => __DIR__ . '/..' . '/../lib/Command/DeleteOrphanedFiles.php',
45+
'OCA\\Files\\Command\\RepairTree' => __DIR__ . '/..' . '/../lib/Command/RepairTree.php',
4546
'OCA\\Files\\Command\\Scan' => __DIR__ . '/..' . '/../lib/Command/Scan.php',
4647
'OCA\\Files\\Command\\ScanAppData' => __DIR__ . '/..' . '/../lib/Command/ScanAppData.php',
4748
'OCA\\Files\\Command\\TransferOwnership' => __DIR__ . '/..' . '/../lib/Command/TransferOwnership.php',
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2021 Robin Appelman <robin@icewind.nl>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\Files\Command;
25+
26+
use OCP\IDBConnection;
27+
use Symfony\Component\Console\Command\Command;
28+
use Symfony\Component\Console\Input\InputInterface;
29+
use Symfony\Component\Console\Output\OutputInterface;
30+
31+
class RepairTree extends Command {
32+
public const CHUNK_SIZE = 200;
33+
34+
/**
35+
* @var IDBConnection
36+
*/
37+
protected $connection;
38+
39+
public function __construct(IDBConnection $connection) {
40+
$this->connection = $connection;
41+
parent::__construct();
42+
}
43+
44+
protected function configure() {
45+
$this
46+
->setName('files:repair-tree')
47+
->setDescription('Try and repair malformed filesystem tree structures')
48+
->addOption('dry-run');
49+
}
50+
51+
public function execute(InputInterface $input, OutputInterface $output): int {
52+
$rows = $this->findBrokenTreeBits();
53+
$fix = !$input->getOption('dry-run');
54+
55+
$output->writeln("Found " . count($rows) . " file entries with an invalid path");
56+
57+
if ($fix) {
58+
$this->connection->beginTransaction();
59+
}
60+
61+
$query = $this->connection->getQueryBuilder();
62+
$query->update('filecache')
63+
->set('path', $query->createParameter('path'))
64+
->set('path_hash', $query->func()->md5($query->createParameter('path')))
65+
->where($query->expr()->eq('fileid', $query->createParameter('fileid')));
66+
67+
foreach ($rows as $row) {
68+
$output->writeln("Path of file ${row['fileid']} is ${row['path']} but should be ${row['parent_path']}/${row['name']} based on it's parent", OutputInterface::VERBOSITY_VERBOSE);
69+
70+
if ($fix) {
71+
$query->setParameters([
72+
'fileid' => $row['fileid'],
73+
'path' => $row['parent_path'] . '/' . $row['name'],
74+
]);
75+
$query->execute();
76+
}
77+
}
78+
79+
if ($fix) {
80+
$this->connection->commit();
81+
}
82+
83+
return 0;
84+
}
85+
86+
private function findBrokenTreeBits(): array {
87+
$query = $this->connection->getQueryBuilder();
88+
89+
$query->select('f.fileid', 'f.path', 'f.parent', 'f.name')
90+
->selectAlias('p.path', 'parent_path')
91+
->from('filecache', 'f')
92+
->innerJoin('f', 'filecache', 'p', $query->expr()->eq('f.parent', 'p.fileid'))
93+
->where($query->expr()->orX(
94+
$query->expr()->andX(
95+
$query->expr()->neq('p.path_hash', $query->createNamedParameter(md5(''))),
96+
$query->expr()->neq('f.path', $query->func()->concat('p.path', $query->func()->concat($query->createNamedParameter('/'), 'f.name')))
97+
),
98+
$query->expr()->andX(
99+
$query->expr()->eq('p.path_hash', $query->createNamedParameter(md5(''))),
100+
$query->expr()->neq('f.path', 'f.name')
101+
),
102+
$query->expr()->neq('f.storage', 'p.storage')
103+
));
104+
105+
return $query->execute()->fetchAll();
106+
}
107+
}

0 commit comments

Comments
 (0)