2015-03-13 11:30:13 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorFilesManagementCatWorkflow
|
|
|
|
extends PhabricatorFilesManagementWorkflow {
|
|
|
|
|
|
|
|
protected function didConstruct() {
|
|
|
|
$this
|
|
|
|
->setName('cat')
|
2015-05-22 17:27:56 +10:00
|
|
|
->setSynopsis(pht('Print the contents of a file.'))
|
2015-03-13 11:30:13 -07:00
|
|
|
->setArguments(
|
|
|
|
array(
|
2015-03-14 08:28:59 -07:00
|
|
|
array(
|
|
|
|
'name' => 'begin',
|
|
|
|
'param' => 'bytes',
|
|
|
|
'help' => pht('Begin printing at a specific offset.'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'end',
|
|
|
|
'param' => 'bytes',
|
|
|
|
'help' => pht('End printing at a specific offset.'),
|
|
|
|
),
|
2017-04-05 09:04:44 -07:00
|
|
|
array(
|
|
|
|
'name' => 'salvage',
|
|
|
|
'help' => pht(
|
|
|
|
'DANGEROUS. Attempt to salvage file content even if the '.
|
|
|
|
'integrity check fails. If an adversary has tampered with '.
|
2017-10-09 10:48:01 -07:00
|
|
|
'the file, the content may be unsafe.'),
|
2017-04-05 09:04:44 -07:00
|
|
|
),
|
2015-03-13 11:30:13 -07:00
|
|
|
array(
|
|
|
|
'name' => 'names',
|
|
|
|
'wildcard' => true,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(PhutilArgumentParser $args) {
|
|
|
|
$console = PhutilConsole::getConsole();
|
|
|
|
|
|
|
|
$names = $args->getArg('names');
|
|
|
|
if (count($names) > 1) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
2015-05-22 17:27:56 +10:00
|
|
|
pht('Specify exactly one file to print, like "%s".', 'F123'));
|
2015-03-13 11:30:13 -07:00
|
|
|
} else if (!$names) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
2015-05-22 17:27:56 +10:00
|
|
|
pht('Specify a file to print, like "%s".', 'F123'));
|
2015-03-13 11:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$file = head($this->loadFilesWithNames($names));
|
|
|
|
|
2015-03-14 08:28:59 -07:00
|
|
|
$begin = $args->getArg('begin');
|
|
|
|
$end = $args->getArg('end');
|
|
|
|
|
2017-04-05 09:04:44 -07:00
|
|
|
$file->makeEphemeral();
|
|
|
|
|
|
|
|
// If we're running in "salvage" mode, wipe out any integrity hash which
|
|
|
|
// may be present. This makes us read file data without performing an
|
|
|
|
// integrity check.
|
|
|
|
$salvage = $args->getArg('salvage');
|
|
|
|
if ($salvage) {
|
|
|
|
$file->setIntegrityHash(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$iterator = $file->getFileDataIterator($begin, $end);
|
|
|
|
foreach ($iterator as $data) {
|
|
|
|
echo $data;
|
|
|
|
}
|
|
|
|
} catch (PhabricatorFileIntegrityException $ex) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
|
|
|
'File data integrity check failed. Use "--salvage" to bypass '.
|
|
|
|
'integrity checks. This flag is dangerous, use it at your own '.
|
|
|
|
'risk. Underlying error: %s',
|
|
|
|
$ex->getMessage()));
|
2015-03-14 08:28:59 -07:00
|
|
|
}
|
2015-03-13 11:30:13 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|