Skip to content

Commit

Permalink
Multiple targets
Browse files Browse the repository at this point in the history
Add multiple targets feature to scan several directories at a time
  • Loading branch information
vincepare committed Dec 12, 2015
1 parent ab306d4 commit 242bdfb
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ DirScan is bundled with a text reporter, but you can customize its output by cre
Download dirscan.phar, make it executable (chmod +x dirscan.phar) and rename it if you want. On Linux, store it to `/usr/local/bin` to make it available everywhere :

```
wget -O dirscan https://github.com/vincepare/DirScan/releases/download/1.1.1/dirscan.phar
wget -O dirscan https://github.com/vincepare/DirScan/releases/download/1.2.0/dirscan.phar
chmod +x dirscan
sudo mv dirscan /usr/local/bin/dirscan
```
Expand All @@ -27,7 +27,7 @@ Tested on PHP 5.3, 5.4, 5.5 & 5.6. There is also a [legacy release](https://raw.
### Usage
```
Usage :
dirscan [OPTIONS] TARGET
dirscan [OPTIONS] TARGET...
Options :
--help, -h This help message
Expand Down
14 changes: 8 additions & 6 deletions src/CliReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,11 @@ public function __construct($settings)
/**
* Print report header
*
* @param string $target Target directory
* @param array $targets List of target directories
* @param array $argv Command line arguments
*/
public function header($target, $argv)
public function header($targets, $argv)
{
$targetStat = lstat($target);
$targetStat['realpath'] = realpath($target);
$header = !empty($this->format) ? $this->getRowFormatHeader($this->format) : $this->getRowHeader();
echo "date: ".date('r (U)')."\n";
echo "getenv(TZ): ".getenv('TZ')."\n";
Expand All @@ -86,10 +84,14 @@ public function header($target, $argv)
echo "php version: ".phpversion()."\n";
echo "uname: ".php_uname()."\n";
echo "cwd: ".getcwd()."\n";
echo "target: ".$target." (realpath: ".$targetStat['realpath'].")\n";
echo "start device: ".$targetStat['dev']."\n";
echo "settings: ".json_encode($this->settings)."\n";
echo "argv: ".json_encode($argv)."\n";
echo "target".(count($targets) > 1 ? "s" : "").": "."\n";
foreach ($targets as $key => $target) {
$targetStat = lstat($target);
$targetStat['realpath'] = realpath($target);
echo sprintf(" - %s (realpath: %s, device: %s)\n", $target, $targetStat['realpath'], $targetStat['dev']);
}
echo "=====================================\n";
echo implode("\t", $header)."\n";
}
Expand Down
43 changes: 33 additions & 10 deletions src/dirscan
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* @copyright © 2015 Vincent Paré
* @license http://opensource.org/licenses/Apache-2.0
* @package DirScan
* @version 1.1.1 (2015-12-11)
* @version 1.2.0 (2015-12-12)
*/

ini_set('log_errors', 0);
ini_set('display_errors', 'stderr');

const DIRSCAN_VERSION = '1.1.1';
const DIRSCAN_VERSION = '1.2.0';

require __DIR__.'/DirScan.php';
require __DIR__.'/Reporter.php';
Expand All @@ -25,7 +25,7 @@ use Vincepare\DirScan\CliReporter;
// Help message
$usage = <<<EOT
Usage :
dirscan [OPTIONS] TARGET
dirscan [OPTIONS] TARGET...
Options :
--help, -h This help message
Expand Down Expand Up @@ -58,12 +58,23 @@ $settings = array(
'same-device' => isset($options['same-device']) ? true : false,
'format' => isset($options['format']) ? $options['format'] : null,
);
$target = isset($argv[1]) ? end($argv) : null;

// Get targets
$targets = array();
foreach ($argv as $key => $val) {
// Ignore first argument (script) and options
if ($key == 0 || preg_match('#^--?\w+#', $val)) {
continue;
}
$targets[] = $val;
}

// Make target an absolute path to get phar working
$absPattern = PHP_OS === 'WINNT' ? '#^[A-Z]:\\\\#i' : '#^/#';
if ($target !== null && !preg_match($absPattern, $target)) {
$target = rtrim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$target;
foreach ($targets as $key => $target) {
if ($target !== null && !preg_match($absPattern, $target)) {
$targets[$key] = rtrim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$target;
}
}

// Format help
Expand All @@ -88,14 +99,26 @@ if ($settings['version']) {
}

// Target check
if (!is_dir($target)) {
fwrite(STDERR, "TARGET".(!empty($target) ? ' ('.$target.') ' : ' ')."is not a directory".PHP_EOL);
if (empty($targets)) {
fwrite(STDERR, "No target".PHP_EOL);
echo $usage;
die(2);
}
$badTarget = 0;
foreach ($targets as $key => $target) {
if (!is_dir($target)) {
fwrite(STDERR, "TARGET".(!empty($target) ? ' ('.$target.') ' : ' ')."is not a directory".PHP_EOL);
$badTarget++;
}
}
if ($badTarget > 0) {
die(1);
}

// Scan
$reporter = new CliReporter($settings);
$reporter->header($target, $argv);
$reporter->header($targets, $argv);
$scanner = new DirScan($settings, $reporter);
$scanner->scan($target);
foreach ($targets as $key => $target) {
$scanner->scan($target);
}
56 changes: 40 additions & 16 deletions src/legacy/dirscan
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
* @copyright © 2015 Vincent Paré
* @license http://opensource.org/licenses/Apache-2.0
* @package DirScan
* @version 1.1.1-legacy (2015-12-11)
* @version 1.2.0-legacy (2015-12-12)
*/

ini_set('log_errors', 0);
ini_set('display_errors', 'stderr');

define('DIRSCAN_VERSION', '1.1.1-legacy');
define('DIRSCAN_VERSION', '1.2.0-legacy');

// Disabling realpath cache on php <= 5.2
ini_set('realpath_cache_size', '0');
Expand All @@ -23,7 +23,7 @@ ini_set('realpath_cache_ttl', '0');
// Help message
$usage = <<<EOT
Usage :
dirscan [OPTIONS] TARGET
dirscan [OPTIONS] TARGET...
Options :
--help, -h This help message
Expand Down Expand Up @@ -74,12 +74,22 @@ foreach ($argv as $val) {
}
}

$target = isset($argv[1]) ? end($argv) : null;
// Get targets
$targets = array();
foreach ($argv as $key => $val) {
// Ignore first argument (script) and options
if ($key == 0 || preg_match('#^--?\w+#', $val)) {
continue;
}
$targets[] = $val;
}

// Make target an absolute path to get phar working
$absPattern = PHP_OS === 'WINNT' ? '#^[A-Z]:\\\\#i' : '#^/#';
if ($target !== null && !preg_match($absPattern, $target)) {
$target = rtrim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$target;
foreach ($targets as $key => $target) {
if ($target !== null && !preg_match($absPattern, $target)) {
$targets[$key] = rtrim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$target;
}
}

// Format help
Expand All @@ -104,17 +114,29 @@ if ($settings['version']) {
}

// Target check
if (!is_dir($target)) {
fwrite(STDERR, "TARGET".(!empty($target) ? ' ('.$target.') ' : ' ')."is not a directory".PHP_EOL);
if (empty($targets)) {
fwrite(STDERR, "No target".PHP_EOL);
echo $usage;
die(2);
}
$badTarget = 0;
foreach ($targets as $key => $target) {
if (!is_dir($target)) {
fwrite(STDERR, "TARGET".(!empty($target) ? ' ('.$target.') ' : ' ')."is not a directory".PHP_EOL);
$badTarget++;
}
}
if ($badTarget > 0) {
die(1);
}

// Scan
$reporter = new CliReporter($settings);
$reporter->header($target, $argv);
$reporter->header($targets, $argv);
$scanner = new DirScan($settings, $reporter);
$scanner->scan($target);
foreach ($targets as $key => $target) {
$scanner->scan($target);
}

/**
* File system scanning class
Expand Down Expand Up @@ -385,13 +407,11 @@ class CliReporter extends Reporter
/**
* Print report header
*
* @param string $target Target directory
* @param array $targets List of target directories
* @param array $argv Command line arguments
*/
public function header($target, $argv)
public function header($targets, $argv)
{
$targetStat = lstat($target);
$targetStat['realpath'] = realpath($target);
$header = !empty($this->format) ? $this->getRowFormatHeader($this->format) : $this->getRowHeader();
echo "date: ".date('r (U)')."\n";
echo "getenv(TZ): ".getenv('TZ')."\n";
Expand All @@ -402,10 +422,14 @@ class CliReporter extends Reporter
echo "php version: ".phpversion()."\n";
echo "uname: ".php_uname()."\n";
echo "cwd: ".getcwd()."\n";
echo "target: ".$target." (realpath: ".$targetStat['realpath'].")\n";
echo "start device: ".$targetStat['dev']."\n";
echo "settings: ".json_encode($this->settings)."\n";
echo "argv: ".json_encode($argv)."\n";
echo "target".(count($targets) > 1 ? "s" : "").": "."\n";
foreach ($targets as $key => $target) {
$targetStat = lstat($target);
$targetStat['realpath'] = realpath($target);
echo sprintf(" - %s (realpath: %s, device: %s)\n", $target, $targetStat['realpath'], $targetStat['dev']);
}
echo "=====================================\n";
echo implode("\t", $header)."\n";
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Setup test environment
$wd = getcwd();
chdir(__DIR__);
if (!is_executable('setup.sh') && !chmod('setup.sh', 0555)) {
if (!is_executable('setup.sh') && !chmod('setup.sh', 0755)) {
trigger_error("Please make setup.sh executable", E_USER_ERROR);
}
exec('./setup.sh', $output, $status);
Expand Down

0 comments on commit 242bdfb

Please sign in to comment.