From 904fbe737ce58d1452bfce0002263ee5f7a88663 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Sun, 18 May 2014 16:57:08 -0700 Subject: [PATCH] Add an `ArcanistChmodLinter`. Summary: Fixes T3922. Add a linter which complains if files are executable when they shoudn't be. Test Plan: Tested by modifying the `.arclint` file of the rARC repository and running `arc lint --everything`. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: epriestley, Korvin Maniphest Tasks: T3922 Differential Revision: https://secure.phabricator.com/D9187 --- src/__phutil_library_map__.php | 2 + src/lint/linter/ArcanistChmodLinter.php | 80 +++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/lint/linter/ArcanistChmodLinter.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f60b447d..f9a006ab 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -32,6 +32,7 @@ phutil_register_library_map(array( 'ArcanistCSharpLinter' => 'lint/linter/ArcanistCSharpLinter.php', 'ArcanistCallConduitWorkflow' => 'workflow/ArcanistCallConduitWorkflow.php', 'ArcanistCapabilityNotSupportedException' => 'workflow/exception/ArcanistCapabilityNotSupportedException.php', + 'ArcanistChmodLinter' => 'lint/linter/ArcanistChmodLinter.php', 'ArcanistChooseInvalidRevisionException' => 'exception/ArcanistChooseInvalidRevisionException.php', 'ArcanistChooseNoRevisionsException' => 'exception/ArcanistChooseNoRevisionsException.php', 'ArcanistCloseRevisionWorkflow' => 'workflow/ArcanistCloseRevisionWorkflow.php', @@ -222,6 +223,7 @@ phutil_register_library_map(array( 'ArcanistCSharpLinter' => 'ArcanistLinter', 'ArcanistCallConduitWorkflow' => 'ArcanistBaseWorkflow', 'ArcanistCapabilityNotSupportedException' => 'Exception', + 'ArcanistChmodLinter' => 'ArcanistLinter', 'ArcanistChooseInvalidRevisionException' => 'Exception', 'ArcanistChooseNoRevisionsException' => 'Exception', 'ArcanistCloseRevisionWorkflow' => 'ArcanistBaseWorkflow', diff --git a/src/lint/linter/ArcanistChmodLinter.php b/src/lint/linter/ArcanistChmodLinter.php new file mode 100644 index 00000000..48ab8648 --- /dev/null +++ b/src/lint/linter/ArcanistChmodLinter.php @@ -0,0 +1,80 @@ + pht('Invalid Executable'), + ); + } + + public function getLintSeverityMap() { + return array( + self::LINT_INVALID_EXECUTABLE => ArcanistLintSeverity::SEVERITY_WARNING, + ); + } + + public function lintPath($path) { + if (is_executable($path)) { + if ($this->getEngine()->isBinaryFile($path)) { + // Path is a binary file, which makes it a valid executable. + return; + } else if ($this->getShebang($path)) { + // Path contains a shebang, which makes it a valid executable. + return; + } else { + $this->raiseLintAtPath( + self::LINT_INVALID_EXECUTABLE, + pht( + 'Executable files should either be binary or contain a shebang.')); + } + } + } + + /** + * Returns the path's shebang. + * + * @param string + * @return string|null + */ + private function getShebang($path) { + $line = head(phutil_split_lines($this->getEngine()->loadData($path), true)); + + $matches = array(); + if (preg_match('/^#!(.*)$/', $line, $matches)) { + return $matches[1]; + } else { + return null; + } + } + +} -- 2.43.0