Update apps/files_external/lib/sftp.php
Added a little better exception handling, hopefully making it a little more robust
This commit is contained in:
parent
3375253e1d
commit
00bfcd94ec
1 changed files with 111 additions and 71 deletions
|
@ -52,12 +52,15 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
}
|
||||
|
||||
private function host_keys_path() {
|
||||
try {
|
||||
$storage_view = \OCP\Files::getStorage('files_external');
|
||||
if ($storage_view) {
|
||||
return \OCP\Config::getSystemValue('datadirectory') .
|
||||
$storage_view->getAbsolutePath('') .
|
||||
'ssh_host_keys';
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -76,6 +79,7 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
}
|
||||
|
||||
private function read_host_keys() {
|
||||
try {
|
||||
$key_path = $this->host_keys_path();
|
||||
if (file_exists($key_path)) {
|
||||
$hosts = array();
|
||||
|
@ -92,20 +96,30 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
return array_combine($hosts, $keys);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
public function mkdir($path) {
|
||||
try {
|
||||
return $this->client->mkdir($this->abs_path($path));
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function rmdir($path) {
|
||||
try {
|
||||
return $this->client->delete($this->abs_path($path), true);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function opendir($path) {
|
||||
$list = $this->client->nlist($this->abs_path($path));
|
||||
try {
|
||||
$list = $this->client->nlist($this->abs_path($path));
|
||||
|
||||
$id = md5('sftp:' . $path);
|
||||
OC_FakeDirStream::$dirs[$id] = array();
|
||||
foreach($list as $file) {
|
||||
|
@ -120,9 +134,12 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
}
|
||||
|
||||
public function filetype($path) {
|
||||
try {
|
||||
$stat = $this->client->stat($this->abs_path($path));
|
||||
if ($stat['type'] == NET_SFTP_TYPE_REGULAR) return 'file';
|
||||
if ($stat['type'] == NET_SFTP_TYPE_DIRECTORY) return 'dir';
|
||||
} catch (Exeption $e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -135,14 +152,23 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
}
|
||||
|
||||
public function file_exists($path) {
|
||||
try {
|
||||
return $this->client->stat($this->abs_path($path)) === false ? false : true;
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function unlink($path) {
|
||||
try {
|
||||
return $this->client->delete($this->abs_path($path), true);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function fopen($path, $mode) {
|
||||
try {
|
||||
$abs_path = $this->abs_path($path);
|
||||
switch($mode) {
|
||||
case 'r':
|
||||
|
@ -182,6 +208,8 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
self::$tempFiles[$tmpFile]=$abs_path;
|
||||
return fopen('close://'.$tmpFile, $mode);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -198,9 +226,13 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
}
|
||||
|
||||
public function touch($path, $mtime=null) {
|
||||
try {
|
||||
if (!$this->file_exists($path)) {
|
||||
$this->client->put($this->abs_path($path), '');
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getFile($path, $target) {
|
||||
|
@ -212,16 +244,24 @@ class OC_Filestorage_SFTP extends OC_Filestorage_Common {
|
|||
}
|
||||
|
||||
public function rename($source, $target) {
|
||||
try {
|
||||
return $this->client->rename($this->abs_path($source), $this->abs_path($target));
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function stat($path) {
|
||||
try {
|
||||
$stat = $this->client->stat($this->abs_path($path));
|
||||
|
||||
$mtime = $stat ? $stat['mtime'] : -1;
|
||||
$size = $stat ? $stat['size'] : 0;
|
||||
|
||||
return array('mtime' => $mtime, 'size' => $size, 'ctime' => -1);
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
Loading…
Reference in a new issue