Skip to content

Commit

Permalink
fix: DAV storage should return false on stat() if connection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
DeepDiver1975 committed Jul 7, 2023
1 parent b13fbf6 commit d13f2c4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -64,7 +64,8 @@
"composer/semver": "^3.3",
"ext-json": "*",
"sabre/vobject": "^4.5",
"dg/composer-cleaner": "^2.2"
"dg/composer-cleaner": "^2.2",
"ext-curl": "*"
},
"extra": {
"bamarni-bin": {
Expand Down
42 changes: 28 additions & 14 deletions lib/private/Files/Storage/DAV.php
Expand Up @@ -45,6 +45,7 @@
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
use OCP\Util;
use Sabre\DAV\Exception as SabreException;
use Sabre\DAV\Xml\Property\ResourceType;
use Sabre\HTTP\Client;
use Sabre\HTTP\ClientHttpException;
Expand All @@ -69,8 +70,6 @@ class DAV extends Common {
protected $secure;
/** @var string */
protected $root;
/** @var string */
protected $certPath;
/** @var bool */
protected $ready;
/** @var Client */
Expand Down Expand Up @@ -158,7 +157,6 @@ public function getId() {
return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
}

/** {@inheritdoc} */
public function createBaseUri() {
$baseUri = 'http';
if ($this->secure) {
Expand Down Expand Up @@ -243,10 +241,12 @@ public function opendir($path) {
* If not, request it from the server then store to cache.
*
* @param string $path path to propfind
*
* @param bool $strictNotFoundCheck
* @return array|boolean propfind response or false if the entry was not found
*
* @throws ClientHttpException
* @throws StorageInvalidException
* @throws StorageNotAvailableException
* @throws SabreException
*/
protected function propfind($path, $strictNotFoundCheck=false) {
$path = $this->cleanPath($path);
Expand Down Expand Up @@ -291,10 +291,10 @@ protected function propfind($path, $strictNotFoundCheck=false) {
$this->convertException($e, $path);
}
}
} else {
$response = $cachedResponse;
return $response;
}
return $response;

return $cachedResponse;
}

/** {@inheritdoc} */
Expand Down Expand Up @@ -421,6 +421,9 @@ public function fopen($path, $mode) {

/**
* @param string $tmpFile
* @throws StorageInvalidException
* @throws StorageNotAvailableException
* @throws SabreException
*/
public function writeBack($tmpFile) {
if (isset(self::$tempFiles[$tmpFile])) {
Expand Down Expand Up @@ -512,14 +515,17 @@ public function file_put_contents($path, $data) {
/**
* @param string $path
* @param string $target
* @throws StorageInvalidException
* @throws StorageNotAvailableException
* @throws SabreException
*/
protected function uploadFile($path, $target) {
$this->init();

// invalidate
$target = $this->cleanPath($target);
$this->statCache->remove($target);
$source = \fopen($path, 'r');
$source = \fopen($path, 'rb');

$this->removeCachedFile($target);
try {
Expand Down Expand Up @@ -599,21 +605,25 @@ public function copy($path1, $path2) {
return false;
}

/** {@inheritdoc} */
/**
* @throws StorageInvalidException
* @throws StorageNotAvailableException
* @throws SabreException
*/
public function stat($path) {
try {
$response = $this->propfind($path);
if ($response === false) {
return [];
return false;
}
return [
'mtime' => \strtotime($response['{DAV:}getlastmodified']),
'size' => (int)isset($response['{DAV:}getcontentlength']) ? $response['{DAV:}getcontentlength'] : 0,
];
} catch (\Exception $e) {
$this->convertException($e, $path);
return false;
}
return [];
}

/** {@inheritdoc} */
Expand Down Expand Up @@ -674,6 +684,7 @@ private function encodePath($path) {
* @return bool
* @throws StorageInvalidException
* @throws StorageNotAvailableException
* @throws SabreException
*/
private function simpleResponse($method, $path, $body, $expected) {
$path = $this->cleanPath($path);
Expand Down Expand Up @@ -786,8 +797,10 @@ protected function parsePermissions($permissionsString) {
*
* @param string $path
* @param int $time
* @throws \OCP\Files\StorageNotAvailableException
* @return bool
* @throws StorageInvalidException
* @throws \OCP\Files\StorageNotAvailableException
* @throws SabreException
*/
public function hasUpdated($path, $time) {
$this->init();
Expand Down Expand Up @@ -851,6 +864,7 @@ public function hasUpdated($path, $time) {
* @throws StorageInvalidException if the storage is invalid, for example
* when the authentication expired or is invalid
* @throws StorageNotAvailableException if the storage is not available,
* @throws SabreException
* which might be temporary
*/
private function convertException(Exception $e, $path = '') {
Expand All @@ -871,7 +885,7 @@ private function convertException(Exception $e, $path = '') {
} elseif (($e instanceof StorageNotAvailableException)
|| ($e instanceof StorageInvalidException)
|| (
$e instanceof \Sabre\DAV\Exception
$e instanceof SabreException
)) {
// rethrow
throw $e;
Expand Down

0 comments on commit d13f2c4

Please sign in to comment.