Skip to content

Commit

Permalink
use stat instead of attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Oct 19, 2023
1 parent e0e86b1 commit bc54b74
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
33 changes: 7 additions & 26 deletions src/Native/NativeFileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Icewind\SMB\ACL;
use Icewind\SMB\Exception\Exception;
use Icewind\SMB\Exception\NotFoundException;
use Icewind\SMB\IFileInfo;

class NativeFileInfo implements IFileInfo {
Expand All @@ -19,7 +20,7 @@ class NativeFileInfo implements IFileInfo {
/** @var NativeShare */
protected $share;
/** @var array{"mode": int, "size": int, "write_time": int}|null */
protected $attributeCache = null;
protected $statCache = null;

public function __construct(NativeShare $share, string $path, string $name) {
$this->share = $share;
Expand All @@ -36,33 +37,13 @@ public function getName(): string {
}

/**
* @return array{"mode": int, "size": int, "write_time": int}
* @return array{"mode": int, "size": int, "mtime": int}
*/
protected function stat(): array {
if (is_null($this->attributeCache)) {
$rawAttributes = explode(',', $this->share->getAttribute($this->path, 'system.dos_attr.*'));
$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;
}
}
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;
if (is_null($this->statCache)) {
$this->statCache = $this->share->rawStat($this->path);

Check failure on line 44 in src/Native/NativeFileInfo.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Property Icewind\SMB\Native\NativeFileInfo::$statCache (array('mode' => int, 'size' => int, 'write_time' => int)|null) does not accept array('mode' => int, 'size' => int, 'mtime' => int).

Check failure on line 44 in src/Native/NativeFileInfo.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis

Property Icewind\SMB\Native\NativeFileInfo::$statCache (array('mode' => int, 'size' => int, 'write_time' => int)|null) does not accept array('mode' => int, 'size' => int, 'mtime' => int).
}
return $this->attributeCache;
return $this->statCache;
}

public function getSize(): int {
Expand All @@ -72,7 +53,7 @@ public function getSize(): int {

public function getMTime(): int {
$stat = $this->stat();
return $stat['write_time'];
return $stat['mtime'];
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Native/NativeShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ public function stat(string $path): IFileInfo {
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()
*
Expand Down
2 changes: 1 addition & 1 deletion src/Native/NativeStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function stream_seek($offset, $whence = SEEK_SET) {
*/
public function stream_stat() {
try {
return $this->state->stat($this->url);
return $this->state->fstat($this->handle, $this->url);
} catch (Exception $e) {
return false;
}
Expand Down

0 comments on commit bc54b74

Please sign in to comment.