Path: blob/master/src/applications/files/management/PhabricatorFilesManagementCatWorkflow.php
13423 views
<?php12final class PhabricatorFilesManagementCatWorkflow3extends PhabricatorFilesManagementWorkflow {45protected function didConstruct() {6$this7->setName('cat')8->setSynopsis(pht('Print the contents of a file.'))9->setArguments(10array(11array(12'name' => 'begin',13'param' => 'bytes',14'help' => pht('Begin printing at a specific offset.'),15),16array(17'name' => 'end',18'param' => 'bytes',19'help' => pht('End printing at a specific offset.'),20),21array(22'name' => 'salvage',23'help' => pht(24'DANGEROUS. Attempt to salvage file content even if the '.25'integrity check fails. If an adversary has tampered with '.26'the file, the content may be unsafe.'),27),28array(29'name' => 'names',30'wildcard' => true,31),32));33}3435public function execute(PhutilArgumentParser $args) {36$console = PhutilConsole::getConsole();3738$names = $args->getArg('names');39if (count($names) > 1) {40throw new PhutilArgumentUsageException(41pht('Specify exactly one file to print, like "%s".', 'F123'));42} else if (!$names) {43throw new PhutilArgumentUsageException(44pht('Specify a file to print, like "%s".', 'F123'));45}4647$file = head($this->loadFilesWithNames($names));4849$begin = $args->getArg('begin');50$end = $args->getArg('end');5152$file->makeEphemeral();5354// If we're running in "salvage" mode, wipe out any integrity hash which55// may be present. This makes us read file data without performing an56// integrity check.57$salvage = $args->getArg('salvage');58if ($salvage) {59$file->setIntegrityHash(null);60}6162try {63$iterator = $file->getFileDataIterator($begin, $end);64foreach ($iterator as $data) {65echo $data;66}67} catch (PhabricatorFileIntegrityException $ex) {68throw new PhutilArgumentUsageException(69pht(70'File data integrity check failed. Use "--salvage" to bypass '.71'integrity checks. This flag is dangerous, use it at your own '.72'risk. Underlying error: %s',73$ex->getMessage()));74}7576return 0;77}7879}808182