Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/management/PhabricatorFilesManagementCatWorkflow.php
13423 views
1
<?php
2
3
final class PhabricatorFilesManagementCatWorkflow
4
extends PhabricatorFilesManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('cat')
9
->setSynopsis(pht('Print the contents of a file.'))
10
->setArguments(
11
array(
12
array(
13
'name' => 'begin',
14
'param' => 'bytes',
15
'help' => pht('Begin printing at a specific offset.'),
16
),
17
array(
18
'name' => 'end',
19
'param' => 'bytes',
20
'help' => pht('End printing at a specific offset.'),
21
),
22
array(
23
'name' => 'salvage',
24
'help' => pht(
25
'DANGEROUS. Attempt to salvage file content even if the '.
26
'integrity check fails. If an adversary has tampered with '.
27
'the file, the content may be unsafe.'),
28
),
29
array(
30
'name' => 'names',
31
'wildcard' => true,
32
),
33
));
34
}
35
36
public function execute(PhutilArgumentParser $args) {
37
$console = PhutilConsole::getConsole();
38
39
$names = $args->getArg('names');
40
if (count($names) > 1) {
41
throw new PhutilArgumentUsageException(
42
pht('Specify exactly one file to print, like "%s".', 'F123'));
43
} else if (!$names) {
44
throw new PhutilArgumentUsageException(
45
pht('Specify a file to print, like "%s".', 'F123'));
46
}
47
48
$file = head($this->loadFilesWithNames($names));
49
50
$begin = $args->getArg('begin');
51
$end = $args->getArg('end');
52
53
$file->makeEphemeral();
54
55
// If we're running in "salvage" mode, wipe out any integrity hash which
56
// may be present. This makes us read file data without performing an
57
// integrity check.
58
$salvage = $args->getArg('salvage');
59
if ($salvage) {
60
$file->setIntegrityHash(null);
61
}
62
63
try {
64
$iterator = $file->getFileDataIterator($begin, $end);
65
foreach ($iterator as $data) {
66
echo $data;
67
}
68
} catch (PhabricatorFileIntegrityException $ex) {
69
throw new PhutilArgumentUsageException(
70
pht(
71
'File data integrity check failed. Use "--salvage" to bypass '.
72
'integrity checks. This flag is dangerous, use it at your own '.
73
'risk. Underlying error: %s',
74
$ex->getMessage()));
75
}
76
77
return 0;
78
}
79
80
}
81
82