summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorVictor Dubiniuk <victor.dubiniuk@gmail.com>2016-12-13 20:45:48 +0300
committerLukas Reschke <lukas@statuscode.ch>2016-12-16 17:50:58 +0100
commit876754a5a55e076a56e98e4ed3fc7e82589e2afa (patch)
tree1a7319a5bf8f79fc83fc1ab81b54511e5d55d826 /lib
parentb0c1460a1d7d71cb752637b42154cd515c2b489e (diff)
downloadnextcloud-server-876754a5a55e076a56e98e4ed3fc7e82589e2afa.tar.gz
nextcloud-server-876754a5a55e076a56e98e4ed3fc7e82589e2afa.zip
Check return value for file_put_contents. Add return value to the commands
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'lib')
-rw-r--r--lib/private/IntegrityCheck/Checker.php38
-rw-r--r--lib/private/IntegrityCheck/Helpers/FileAccessHelper.php29
2 files changed, 55 insertions, 12 deletions
diff --git a/lib/private/IntegrityCheck/Checker.php b/lib/private/IntegrityCheck/Checker.php
index 102fe42a99d..a3d9d906d8d 100644
--- a/lib/private/IntegrityCheck/Checker.php
+++ b/lib/private/IntegrityCheck/Checker.php
@@ -271,16 +271,24 @@ class Checker {
public function writeAppSignature($path,
X509 $certificate,
RSA $privateKey) {
- if(!is_dir($path)) {
- throw new \Exception('Directory does not exist.');
- }
+ $appInfoDir = $path . '/appinfo';
+ $this->fileAccessHelper->assertDirectoryExists($path);
+ $this->fileAccessHelper->assertDirectoryExists($appInfoDir);
+
$iterator = $this->getFolderIterator($path);
$hashes = $this->generateHashes($iterator, $path);
$signature = $this->createSignatureData($hashes, $certificate, $privateKey);
- $this->fileAccessHelper->file_put_contents(
- $path . '/appinfo/signature.json',
+ try {
+ $this->fileAccessHelper->file_put_contents(
+ $appInfoDir . '/signature.json',
json_encode($signature, JSON_PRETTY_PRINT)
- );
+ );
+ } catch (\Exception $e){
+ if (!$this->fileAccessHelper->is_writeable($appInfoDir)){
+ throw new \Exception($appInfoDir . ' is not writable');
+ }
+ throw $e;
+ }
}
/**
@@ -289,17 +297,29 @@ class Checker {
* @param X509 $certificate
* @param RSA $rsa
* @param string $path
+ * @throws \Exception
*/
public function writeCoreSignature(X509 $certificate,
RSA $rsa,
$path) {
+ $coreDir = $path . '/core';
+ $this->fileAccessHelper->assertDirectoryExists($path);
+ $this->fileAccessHelper->assertDirectoryExists($coreDir);
+
$iterator = $this->getFolderIterator($path, $path);
$hashes = $this->generateHashes($iterator, $path);
$signatureData = $this->createSignatureData($hashes, $certificate, $rsa);
- $this->fileAccessHelper->file_put_contents(
- $path . '/core/signature.json',
+ try {
+ $this->fileAccessHelper->file_put_contents(
+ $coreDir . '/signature.json',
json_encode($signatureData, JSON_PRETTY_PRINT)
- );
+ );
+ } catch (\Exception $e){
+ if (!$this->fileAccessHelper->is_writeable($coreDir)){
+ throw new \Exception($coreDir . ' is not writable');
+ }
+ throw $e;
+ }
}
/**
diff --git a/lib/private/IntegrityCheck/Helpers/FileAccessHelper.php b/lib/private/IntegrityCheck/Helpers/FileAccessHelper.php
index 9e2b76ce11a..c3193457890 100644
--- a/lib/private/IntegrityCheck/Helpers/FileAccessHelper.php
+++ b/lib/private/IntegrityCheck/Helpers/FileAccessHelper.php
@@ -53,10 +53,33 @@ class FileAccessHelper {
* Wrapper around file_put_contents($filename, $data)
*
* @param string $filename
- * @param $data
- * @return int|false
+ * @param string $data
+ * @return int
+ * @throws \Exception
*/
public function file_put_contents($filename, $data) {
- return file_put_contents($filename, $data);
+ $bytesWritten = file_put_contents($filename, $data);
+ if ($bytesWritten === false || $bytesWritten !== strlen($data)){
+ throw new \Exception('Failed to write into ' . $filename);
+ }
+ return $bytesWritten;
+ }
+
+ /**
+ * @param string $path
+ * @return bool
+ */
+ public function is_writeable($path){
+ return is_writeable($path);
+ }
+
+ /**
+ * @param string $path
+ * @throws \Exception
+ */
+ public function assertDirectoryExists($path){
+ if (!is_dir($path)) {
+ throw new \Exception('Directory ' . $path . ' does not exist.');
+ }
}
}