Page MenuHomePhabricator

D18982.diff
No OneTemporary

D18982.diff

diff --git a/src/filesystem/FileFinder.php b/src/filesystem/FileFinder.php
--- a/src/filesystem/FileFinder.php
+++ b/src/filesystem/FileFinder.php
@@ -22,6 +22,7 @@
private $paths = array();
private $name = array();
private $suffix = array();
+ private $nameGlobs = array();
private $type;
private $generateChecksums = false;
private $followSymlinks;
@@ -98,6 +99,11 @@
return $this->generateChecksums;
}
+ public function withNameGlob($pattern) {
+ $this->nameGlobs[] = $pattern;
+ return $this;
+ }
+
/**
* @task config
* @param string Either "php", "shell", or the empty string.
@@ -126,6 +132,23 @@
}
}
+ if ($this->nameGlobs) {
+ $name = basename($file);
+
+ $matches = false;
+ foreach ($this->nameGlobs as $glob) {
+ $glob = addcslashes($glob, '\\');
+ if (fnmatch($glob, $name)) {
+ $matches = true;
+ break;
+ }
+ }
+
+ if (!$matches) {
+ return false;
+ }
+ }
+
if ($this->suffix) {
$matches = false;
foreach ($this->suffix as $suffix) {
@@ -261,6 +284,10 @@
$command[] = $this->generateList('path', $this->paths);
}
+ if ($this->nameGlobs) {
+ $command[] = $this->generateList('name', $this->nameGlobs);
+ }
+
$command[] = '-print0';
array_unshift($args, implode(' ', $command));
@@ -312,12 +339,12 @@
private function generateList(
$flag,
array $items,
- $mode = 'path') {
+ $mode = 'glob') {
foreach ($items as $key => $item) {
- // If the mode is not "path" mode, we're going to escape glob characters
+ // If the mode is not "glob" mode, we're going to escape glob characters
// in the pattern. Otherwise, we escape only backslashes.
- if ($mode === 'path') {
+ if ($mode === 'glob') {
$item = addcslashes($item, '\\');
} else {
$item = addcslashes($item, '\\*?');
diff --git a/src/filesystem/__tests__/FileFinderTestCase.php b/src/filesystem/__tests__/FileFinderTestCase.php
--- a/src/filesystem/__tests__/FileFinderTestCase.php
+++ b/src/filesystem/__tests__/FileFinderTestCase.php
@@ -184,6 +184,26 @@
array(
'backslash\\.\\*',
));
+
+ $this->assertFinder(
+ pht('Glob Magic, With Globs'),
+ $this->newFinder($tmp_dir)
+ ->withType('f')
+ ->withNameGlob('star-*'),
+ array(
+ 'star-*.*',
+ 'star-*.txt',
+ ));
+
+ $this->assertFinder(
+ pht('Glob Magic, With Globs + Suffix'),
+ $this->newFinder($tmp_dir)
+ ->withType('f')
+ ->withNameGlob('star-*')
+ ->withSuffix('txt'),
+ array(
+ 'star-*.txt',
+ ));
}
private function assertFinder($label, FileFinder $finder, $expect) {

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 20, 8:19 PM (6 h, 45 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7633460
Default Alt Text
D18982.diff (2 KB)

Event Timeline