use stat instead of attributes

This commit is contained in:
Robin Appelman 2023-10-19 16:33:59 +02:00
commit 89082b2b29
3 changed files with 16 additions and 28 deletions

View file

@ -9,6 +9,7 @@ namespace Icewind\SMB\Native;
use Icewind\SMB\ACL; use Icewind\SMB\ACL;
use Icewind\SMB\Exception\Exception; use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\NotFoundException;
use Icewind\SMB\IFileInfo; use Icewind\SMB\IFileInfo;
class NativeFileInfo implements IFileInfo { class NativeFileInfo implements IFileInfo {
@ -18,8 +19,8 @@ class NativeFileInfo implements IFileInfo {
protected $name; protected $name;
/** @var NativeShare */ /** @var NativeShare */
protected $share; protected $share;
/** @var array{"mode": int, "size": int, "write_time": int}|null */ /** @var array{"mode": int, "size": int, "mtime": int}|null */
protected $attributeCache = null; protected $statCache = null;
public function __construct(NativeShare $share, string $path, string $name) { public function __construct(NativeShare $share, string $path, string $name) {
$this->share = $share; $this->share = $share;
@ -36,33 +37,13 @@ class NativeFileInfo implements IFileInfo {
} }
/** /**
* @return array{"mode": int, "size": int, "write_time": int} * @return array{"mode": int, "size": int, "mtime": int}
*/ */
protected function stat(): array { protected function stat(): array {
if (is_null($this->attributeCache)) { if (is_null($this->statCache)) {
$rawAttributes = explode(',', $this->share->getAttribute($this->path, 'system.dos_attr.*')); $this->statCache = $this->share->rawStat($this->path);
$attributes = [];
foreach ($rawAttributes as $rawAttribute) {
list($name, $value) = explode(':', $rawAttribute);
$name = strtolower($name);
if ($name == 'mode') {
$attributes[$name] = (int)hexdec(substr($value, 2));
} else {
$attributes[$name] = (int)$value;
} }
} return $this->statCache;
if (!isset($attributes['mode'])) {
throw new Exception("Invalid attribute response");
}
if (!isset($attributes['size'])) {
throw new Exception("Invalid attribute response");
}
if (!isset($attributes['write_time'])) {
throw new Exception("Invalid attribute response");
}
$this->attributeCache = $attributes;
}
return $this->attributeCache;
} }
public function getSize(): int { public function getSize(): int {
@ -72,7 +53,7 @@ class NativeFileInfo implements IFileInfo {
public function getMTime(): int { public function getMTime(): int {
$stat = $this->stat(); $stat = $this->stat();
return $stat['write_time']; return $stat['mtime'];
} }
/** /**

View file

@ -116,6 +116,13 @@ class NativeShare extends AbstractShare {
return $info; return $info;
} }
/**
* @return array{"mode": int, "size": int, "mtime": int}
*/
public function rawStat(string $path): array {
return $this->getState()->stat($this->buildUrl($path));
}
/** /**
* Multibyte unicode safe version of basename() * Multibyte unicode safe version of basename()
* *

View file

@ -131,7 +131,7 @@ abstract class NativeStream implements File {
*/ */
public function stream_stat() { public function stream_stat() {
try { try {
return $this->state->stat($this->url); return $this->state->fstat($this->handle, $this->url);
} catch (Exception $e) { } catch (Exception $e) {
return false; return false;
} }