mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
improve the parsing of 'dir' output
This commit is contained in:
parent
e76f9974d1
commit
b02235c275
2 changed files with 52 additions and 6 deletions
|
|
@ -28,17 +28,16 @@ class Dir extends Command {
|
|||
protected function parseOutput($lines) {
|
||||
//last line is used space
|
||||
array_pop($lines);
|
||||
$regex = '/^\s*(.*?)\s\s\s\s+(?:([DHARS]*)\s+)?([0-9]+)\s+(.*)$/';
|
||||
//2 spaces, filename, optional type, size, date
|
||||
$content = array();
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if ($line) {
|
||||
list($name, $meta) = explode(" ", $line, 2);
|
||||
if (preg_match($regex,$line,$matches)) {
|
||||
list(,$name, $type, $size, $time)=$matches;
|
||||
if ($name !== '.' and $name !== '..') {
|
||||
list($type, $meta) = explode(" ", trim($meta), 2);
|
||||
list($size, $time) = explode(" ", trim($meta), 2);
|
||||
$content[$name] = array(
|
||||
'size' => intval(trim($size)),
|
||||
'type' => ($type === 'D') ? 'dir' : 'file',
|
||||
'type' => (strpos($type,'D')!==false) ? 'dir' : 'file',
|
||||
'time' => strtotime($time)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,4 +74,51 @@ class Test extends PHPUnit_Framework_TestCase {
|
|||
$this->share->del($this->root . '/foo.txt');
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue