improve the parsing of 'dir' output

This commit is contained in:
Robin Appelman 2012-12-16 03:02:35 +01:00
commit b02235c275
2 changed files with 52 additions and 6 deletions

View file

@ -28,17 +28,16 @@ class Dir extends Command {
protected function parseOutput($lines) { protected function parseOutput($lines) {
//last line is used space //last line is used space
array_pop($lines); array_pop($lines);
$regex = '/^\s*(.*?)\s\s\s\s+(?:([DHARS]*)\s+)?([0-9]+)\s+(.*)$/';
//2 spaces, filename, optional type, size, date
$content = array(); $content = array();
foreach ($lines as $line) { foreach ($lines as $line) {
$line = trim($line); if (preg_match($regex,$line,$matches)) {
if ($line) { list(,$name, $type, $size, $time)=$matches;
list($name, $meta) = explode(" ", $line, 2);
if ($name !== '.' and $name !== '..') { if ($name !== '.' and $name !== '..') {
list($type, $meta) = explode(" ", trim($meta), 2);
list($size, $time) = explode(" ", trim($meta), 2);
$content[$name] = array( $content[$name] = array(
'size' => intval(trim($size)), 'size' => intval(trim($size)),
'type' => ($type === 'D') ? 'dir' : 'file', 'type' => (strpos($type,'D')!==false) ? 'dir' : 'file',
'time' => strtotime($time) 'time' => strtotime($time)
); );
} }

View file

@ -74,4 +74,51 @@ class Test extends PHPUnit_Framework_TestCase {
$this->share->del($this->root . '/foo.txt'); $this->share->del($this->root . '/foo.txt');
$this->assertEquals(array(), $this->share->dir($this->root)); $this->assertEquals(array(), $this->share->dir($this->root));
} }
public function testEscaping() {
$names = array('simple', 'with spaces');
$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua';
$tmpFile1 = tempnam('/tmp', 'smb_test_');
file_put_contents($tmpFile1, $text);
foreach ($names as $name) {
$this->share->mkdir($this->root . '/' . $name);
$dir = $this->share->dir($this->root);
$this->assertArrayHasKey($name, $dir);
$this->assertEquals('dir', $dir[$name]['type']);
$this->assertEquals(array(), $this->share->dir($this->root . '/' . $name));
$this->share->put($tmpFile1, $this->root . '/' . $name . '/foo.txt');
$dir = $this->share->dir($this->root . '/' . $name);
$this->assertArrayHasKey('foo.txt', $dir);
$tmpFile2 = tempnam('/tmp', 'smb_test_');
$this->share->get($this->root . '/' . $name . '/foo.txt', $tmpFile2);
$this->assertEquals($text, file_get_contents($tmpFile2));
$this->share->rename($this->root . '/' . $name . '/foo.txt', $this->root . '/' . $name . '/bar.txt');
$dir = $this->share->dir($this->root . '/' . $name);
$this->assertArrayHasKey('bar.txt', $dir);
$this->assertEquals('file', $dir['bar.txt']['type']);
$this->share->del($this->root . '/' . $name . '/bar.txt');
$this->assertEquals(array(), $this->share->dir($this->root . '/' . $name));
$this->share->rmdir($this->root . '/' . $name);
$this->assertEquals(array(), $this->share->dir($this->root));
$this->share->put($tmpFile1, $this->root . '/' . $name);
$this->assertArrayHasKey($name, $this->share->dir($this->root));
$tmpFile2 = tempnam('/tmp', 'smb_test_');
$this->share->get($this->root . '/' . $name, $tmpFile2);
$this->assertEquals($text, file_get_contents($tmpFile2));
$this->share->del($this->root . '/' . $name);
$this->assertEquals(array(), $this->share->dir($this->root));
}
unlink($tmpFile1);
}
} }