Changeset View
Changeset View
Standalone View
Standalone View
src/lint/linter/ArcanistChmodLinter.php
| Show All 35 Lines | final class ArcanistChmodLinter extends ArcanistLinter { | ||||
| protected function getDefaultMessageSeverity($code) { | protected function getDefaultMessageSeverity($code) { | ||||
| return ArcanistLintSeverity::SEVERITY_WARNING; | return ArcanistLintSeverity::SEVERITY_WARNING; | ||||
| } | } | ||||
| protected function shouldLintBinaryFiles() { | protected function shouldLintBinaryFiles() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| public function lintPath($path) { | protected function lintPath(ArcanistWorkingCopyPath $path) { | ||||
| $engine = $this->getEngine(); | if ($path->isExecutable()) { | ||||
| if ($path->isBinary()) { | |||||
| if (is_executable($engine->getFilePathOnDisk($path))) { | $mime = $path->getMimeType(); | ||||
epriestley: This is a good example of the new approach vs the old approach -- I think the new one is quite… | |||||
| if ($engine->isBinaryFile($path)) { | |||||
| $mime = Filesystem::getMimeType($engine->getFilePathOnDisk($path)); | |||||
| switch ($mime) { | switch ($mime) { | ||||
| // Archives | // Archives | ||||
| case 'application/jar': | case 'application/jar': | ||||
| case 'application/java-archive': | case 'application/java-archive': | ||||
| case 'application/x-bzip2': | case 'application/x-bzip2': | ||||
| case 'application/x-gzip': | case 'application/x-gzip': | ||||
| case 'application/x-rar-compressed': | case 'application/x-rar-compressed': | ||||
| Show All 39 Lines | if ($path->isExecutable()) { | ||||
| self::LINT_INVALID_EXECUTABLE, | self::LINT_INVALID_EXECUTABLE, | ||||
| pht("'%s' files should not be executable.", $mime)); | pht("'%s' files should not be executable.", $mime)); | ||||
| return; | return; | ||||
| default: | default: | ||||
| // Path is a binary file, which makes it a valid executable. | // Path is a binary file, which makes it a valid executable. | ||||
| return; | return; | ||||
| } | } | ||||
| } else if ($this->getShebang($path)) { | } else if ($this->hasShebang($path->getData())) { | ||||
| // Path contains a shebang, which makes it a valid executable. | // Path contains a shebang, which makes it a valid executable. | ||||
| return; | return; | ||||
| } else { | } else { | ||||
| $this->raiseLintAtPath( | $this->raiseLintAtPath( | ||||
| self::LINT_INVALID_EXECUTABLE, | self::LINT_INVALID_EXECUTABLE, | ||||
| pht( | pht( | ||||
| 'Executable files should either be binary or contain a shebang.')); | 'Executable files should either be binary or contain a shebang.')); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /** | private function hasShebang($data) { | ||||
| * Returns the path's shebang. | $lines = phutil_split_lines($data); | ||||
| * | if (!$lines) { | ||||
| * @param string | return false; | ||||
| * @return string|null | } | ||||
| */ | |||||
| private function getShebang($path) { | |||||
| $line = head(phutil_split_lines($this->getEngine()->loadData($path), true)); | |||||
| $matches = array(); | $line = head($lines); | ||||
| if (preg_match('/^#!(.*)$/', $line, $matches)) { | |||||
| return $matches[1]; | if (preg_match('/^#!(.*)$/', $line)) { | ||||
| } else { | return true; | ||||
| return null; | |||||
| } | } | ||||
| return false; | |||||
| } | } | ||||
| } | } | ||||
This is a good example of the new approach vs the old approach -- I think the new one is quite a bit cleaner and easier to deal with.