Completed
Push — stable13 ( 2d5d8b...1a016f )
by Roeland
17:00
created
lib/private/Files/Mount/ObjectHomeMountProvider.php 3 patches
Indentation   +99 added lines, -99 removed lines patch added patch discarded remove patch
@@ -32,106 +32,106 @@
 block discarded – undo
32 32
  * Mount provider for object store home storages
33 33
  */
34 34
 class ObjectHomeMountProvider implements IHomeMountProvider {
35
-	/**
36
-	 * @var IConfig
37
-	 */
38
-	private $config;
39
-
40
-	/**
41
-	 * ObjectStoreHomeMountProvider constructor.
42
-	 *
43
-	 * @param IConfig $config
44
-	 */
45
-	public function __construct(IConfig $config) {
46
-		$this->config = $config;
47
-	}
48
-
49
-	/**
50
-	 * Get the cache mount for a user
51
-	 *
52
-	 * @param IUser $user
53
-	 * @param IStorageFactory $loader
54
-	 * @return \OCP\Files\Mount\IMountPoint
55
-	 */
56
-	public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
57
-
58
-		$config = $this->getMultiBucketObjectStoreConfig($user);
59
-		if ($config === null) {
60
-			$config = $this->getSingleBucketObjectStoreConfig($user);
61
-		}
62
-
63
-		if ($config === null) {
64
-			return null;
65
-		}
66
-
67
-		return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader);
68
-	}
69
-
70
-	/**
71
-	 * @param IUser $user
72
-	 * @return array|null
73
-	 */
74
-	private function getSingleBucketObjectStoreConfig(IUser $user) {
75
-		$config = $this->config->getSystemValue('objectstore');
76
-		if (!is_array($config)) {
77
-			return null;
78
-		}
79
-
80
-		// sanity checks
81
-		if (empty($config['class'])) {
82
-			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
83
-		}
84
-		if (!isset($config['arguments'])) {
85
-			$config['arguments'] = [];
86
-		}
87
-		$config['arguments']['user'] = $user;
88
-		// instantiate object store implementation
89
-		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
90
-
91
-		return $config;
92
-	}
93
-
94
-	/**
95
-	 * @param IUser $user
96
-	 * @return array|null
97
-	 */
98
-	private function getMultiBucketObjectStoreConfig(IUser $user) {
99
-		$config = $this->config->getSystemValue('objectstore_multibucket');
100
-		if (!is_array($config)) {
101
-			return null;
102
-		}
103
-
104
-		// sanity checks
105
-		if (empty($config['class'])) {
106
-			\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
107
-		}
108
-		if (!isset($config['arguments'])) {
109
-			$config['arguments'] = [];
110
-		}
111
-		$config['arguments']['user'] = $user;
112
-
113
-		$bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
114
-
115
-		if ($bucket === null) {
116
-			/*
35
+    /**
36
+     * @var IConfig
37
+     */
38
+    private $config;
39
+
40
+    /**
41
+     * ObjectStoreHomeMountProvider constructor.
42
+     *
43
+     * @param IConfig $config
44
+     */
45
+    public function __construct(IConfig $config) {
46
+        $this->config = $config;
47
+    }
48
+
49
+    /**
50
+     * Get the cache mount for a user
51
+     *
52
+     * @param IUser $user
53
+     * @param IStorageFactory $loader
54
+     * @return \OCP\Files\Mount\IMountPoint
55
+     */
56
+    public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
57
+
58
+        $config = $this->getMultiBucketObjectStoreConfig($user);
59
+        if ($config === null) {
60
+            $config = $this->getSingleBucketObjectStoreConfig($user);
61
+        }
62
+
63
+        if ($config === null) {
64
+            return null;
65
+        }
66
+
67
+        return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader);
68
+    }
69
+
70
+    /**
71
+     * @param IUser $user
72
+     * @return array|null
73
+     */
74
+    private function getSingleBucketObjectStoreConfig(IUser $user) {
75
+        $config = $this->config->getSystemValue('objectstore');
76
+        if (!is_array($config)) {
77
+            return null;
78
+        }
79
+
80
+        // sanity checks
81
+        if (empty($config['class'])) {
82
+            \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
83
+        }
84
+        if (!isset($config['arguments'])) {
85
+            $config['arguments'] = [];
86
+        }
87
+        $config['arguments']['user'] = $user;
88
+        // instantiate object store implementation
89
+        $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
90
+
91
+        return $config;
92
+    }
93
+
94
+    /**
95
+     * @param IUser $user
96
+     * @return array|null
97
+     */
98
+    private function getMultiBucketObjectStoreConfig(IUser $user) {
99
+        $config = $this->config->getSystemValue('objectstore_multibucket');
100
+        if (!is_array($config)) {
101
+            return null;
102
+        }
103
+
104
+        // sanity checks
105
+        if (empty($config['class'])) {
106
+            \OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
107
+        }
108
+        if (!isset($config['arguments'])) {
109
+            $config['arguments'] = [];
110
+        }
111
+        $config['arguments']['user'] = $user;
112
+
113
+        $bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
114
+
115
+        if ($bucket === null) {
116
+            /*
117 117
 			 * Use any provided bucket argument as prefix
118 118
 			 * and add the mapping from username => bucket
119 119
 			 */
120
-			if (!isset($config['arguments']['bucket'])) {
121
-				$config['arguments']['bucket'] = '';
122
-			}
123
-			$mapper = new \OC\Files\ObjectStore\Mapper($user);
124
-			$numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
125
-			$config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
126
-
127
-			$this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
128
-		} else {
129
-			$config['arguments']['bucket'] = $bucket;
130
-		}
131
-
132
-		// instantiate object store implementation
133
-		$config['arguments']['objectstore'] = new $config['class']($config['arguments']);
134
-
135
-		return $config;
136
-	}
120
+            if (!isset($config['arguments']['bucket'])) {
121
+                $config['arguments']['bucket'] = '';
122
+            }
123
+            $mapper = new \OC\Files\ObjectStore\Mapper($user);
124
+            $numBuckets = isset($config['arguments']['num_buckets']) ? $config['arguments']['num_buckets'] : 64;
125
+            $config['arguments']['bucket'] .= $mapper->getBucket($numBuckets);
126
+
127
+            $this->config->setUserValue($user->getUID(), 'homeobjectstore', 'bucket', $config['arguments']['bucket']);
128
+        } else {
129
+            $config['arguments']['bucket'] = $bucket;
130
+        }
131
+
132
+        // instantiate object store implementation
133
+        $config['arguments']['objectstore'] = new $config['class']($config['arguments']);
134
+
135
+        return $config;
136
+    }
137 137
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -64,7 +64,7 @@
 block discarded – undo
64 64
 			return null;
65 65
 		}
66 66
 
67
-		return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/' . $user->getUID(), $config['arguments'], $loader);
67
+		return new MountPoint('\OC\Files\ObjectStore\HomeObjectStoreStorage', '/'.$user->getUID(), $config['arguments'], $loader);
68 68
 	}
69 69
 
70 70
 	/**
Please login to merge, or discard this patch.
Doc Comments   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
 	 *
52 52
 	 * @param IUser $user
53 53
 	 * @param IStorageFactory $loader
54
-	 * @return \OCP\Files\Mount\IMountPoint
54
+	 * @return null|MountPoint
55 55
 	 */
56 56
 	public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
57 57
 
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
 
70 70
 	/**
71 71
 	 * @param IUser $user
72
-	 * @return array|null
72
+	 * @return IStorageFactory|null
73 73
 	 */
74 74
 	private function getSingleBucketObjectStoreConfig(IUser $user) {
75 75
 		$config = $this->config->getSystemValue('objectstore');
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 
94 94
 	/**
95 95
 	 * @param IUser $user
96
-	 * @return array|null
96
+	 * @return IStorageFactory|null
97 97
 	 */
98 98
 	private function getMultiBucketObjectStoreConfig(IUser $user) {
99 99
 		$config = $this->config->getSystemValue('objectstore_multibucket');
Please login to merge, or discard this patch.
lib/private/Files/Mount/MountPoint.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
 		} else {
106 106
 			// Update old classes to new namespace
107 107
 			if (strpos($storage, 'OC_Filestorage_') !== false) {
108
-				$storage = '\OC\Files\Storage\\' . substr($storage, 15);
108
+				$storage = '\OC\Files\Storage\\'.substr($storage, 15);
109 109
 			}
110 110
 			$this->class = $storage;
111 111
 			$this->arguments = $arguments;
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 				return;
158 158
 			}
159 159
 		} else {
160
-			\OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', \OCP\Util::ERROR);
160
+			\OCP\Util::writeLog('core', 'storage backend '.$this->class.' not found', \OCP\Util::ERROR);
161 161
 			$this->invalidStorage = true;
162 162
 			return;
163 163
 		}
@@ -207,13 +207,13 @@  discard block
 block discarded – undo
207 207
 	 */
208 208
 	public function getInternalPath($path) {
209 209
 		$path = Filesystem::normalizePath($path, true, false, true);
210
-		if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
210
+		if ($this->mountPoint === $path or $this->mountPoint.'/' === $path) {
211 211
 			$internalPath = '';
212 212
 		} else {
213 213
 			$internalPath = substr($path, strlen($this->mountPoint));
214 214
 		}
215 215
 		// substr returns false instead of an empty string, we always want a string
216
-		return (string)$internalPath;
216
+		return (string) $internalPath;
217 217
 	}
218 218
 
219 219
 	/**
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
 	 */
267 267
 	public function getStorageRootId() {
268 268
 		if (is_null($this->rootId)) {
269
-			$this->rootId = (int)$this->getStorage()->getCache()->getId('');
269
+			$this->rootId = (int) $this->getStorage()->getCache()->getId('');
270 270
 		}
271 271
 		return $this->rootId;
272 272
 	}
Please login to merge, or discard this patch.
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -35,247 +35,247 @@
 block discarded – undo
35 35
 use OCP\Files\Mount\IMountPoint;
36 36
 
37 37
 class MountPoint implements IMountPoint {
38
-	/**
39
-	 * @var \OC\Files\Storage\Storage $storage
40
-	 */
41
-	protected $storage = null;
42
-	protected $class;
43
-	protected $storageId;
44
-	protected $rootId = null;
38
+    /**
39
+     * @var \OC\Files\Storage\Storage $storage
40
+     */
41
+    protected $storage = null;
42
+    protected $class;
43
+    protected $storageId;
44
+    protected $rootId = null;
45 45
 
46
-	/**
47
-	 * Configuration options for the storage backend
48
-	 *
49
-	 * @var array
50
-	 */
51
-	protected $arguments = array();
52
-	protected $mountPoint;
46
+    /**
47
+     * Configuration options for the storage backend
48
+     *
49
+     * @var array
50
+     */
51
+    protected $arguments = array();
52
+    protected $mountPoint;
53 53
 
54
-	/**
55
-	 * Mount specific options
56
-	 *
57
-	 * @var array
58
-	 */
59
-	protected $mountOptions = array();
54
+    /**
55
+     * Mount specific options
56
+     *
57
+     * @var array
58
+     */
59
+    protected $mountOptions = array();
60 60
 
61
-	/**
62
-	 * @var \OC\Files\Storage\StorageFactory $loader
63
-	 */
64
-	private $loader;
61
+    /**
62
+     * @var \OC\Files\Storage\StorageFactory $loader
63
+     */
64
+    private $loader;
65 65
 
66
-	/**
67
-	 * Specified whether the storage is invalid after failing to
68
-	 * instantiate it.
69
-	 *
70
-	 * @var bool
71
-	 */
72
-	private $invalidStorage = false;
66
+    /**
67
+     * Specified whether the storage is invalid after failing to
68
+     * instantiate it.
69
+     *
70
+     * @var bool
71
+     */
72
+    private $invalidStorage = false;
73 73
 
74
-	/** @var int|null */
75
-	protected $mountId;
74
+    /** @var int|null */
75
+    protected $mountId;
76 76
 
77
-	/**
78
-	 * @param string|\OC\Files\Storage\Storage $storage
79
-	 * @param string $mountpoint
80
-	 * @param array $arguments (optional) configuration for the storage backend
81
-	 * @param \OCP\Files\Storage\IStorageFactory $loader
82
-	 * @param array $mountOptions mount specific options
83
-	 * @param int|null $mountId
84
-	 * @throws \Exception
85
-	 */
86
-	public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) {
87
-		if (is_null($arguments)) {
88
-			$arguments = array();
89
-		}
90
-		if (is_null($loader)) {
91
-			$this->loader = new StorageFactory();
92
-		} else {
93
-			$this->loader = $loader;
94
-		}
77
+    /**
78
+     * @param string|\OC\Files\Storage\Storage $storage
79
+     * @param string $mountpoint
80
+     * @param array $arguments (optional) configuration for the storage backend
81
+     * @param \OCP\Files\Storage\IStorageFactory $loader
82
+     * @param array $mountOptions mount specific options
83
+     * @param int|null $mountId
84
+     * @throws \Exception
85
+     */
86
+    public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) {
87
+        if (is_null($arguments)) {
88
+            $arguments = array();
89
+        }
90
+        if (is_null($loader)) {
91
+            $this->loader = new StorageFactory();
92
+        } else {
93
+            $this->loader = $loader;
94
+        }
95 95
 
96
-		if (!is_null($mountOptions)) {
97
-			$this->mountOptions = $mountOptions;
98
-		}
96
+        if (!is_null($mountOptions)) {
97
+            $this->mountOptions = $mountOptions;
98
+        }
99 99
 
100
-		$mountpoint = $this->formatPath($mountpoint);
101
-		$this->mountPoint = $mountpoint;
102
-		if ($storage instanceof Storage) {
103
-			$this->class = get_class($storage);
104
-			$this->storage = $this->loader->wrap($this, $storage);
105
-		} else {
106
-			// Update old classes to new namespace
107
-			if (strpos($storage, 'OC_Filestorage_') !== false) {
108
-				$storage = '\OC\Files\Storage\\' . substr($storage, 15);
109
-			}
110
-			$this->class = $storage;
111
-			$this->arguments = $arguments;
112
-		}
113
-		$this->mountId = $mountId;
114
-	}
100
+        $mountpoint = $this->formatPath($mountpoint);
101
+        $this->mountPoint = $mountpoint;
102
+        if ($storage instanceof Storage) {
103
+            $this->class = get_class($storage);
104
+            $this->storage = $this->loader->wrap($this, $storage);
105
+        } else {
106
+            // Update old classes to new namespace
107
+            if (strpos($storage, 'OC_Filestorage_') !== false) {
108
+                $storage = '\OC\Files\Storage\\' . substr($storage, 15);
109
+            }
110
+            $this->class = $storage;
111
+            $this->arguments = $arguments;
112
+        }
113
+        $this->mountId = $mountId;
114
+    }
115 115
 
116
-	/**
117
-	 * get complete path to the mount point, relative to data/
118
-	 *
119
-	 * @return string
120
-	 */
121
-	public function getMountPoint() {
122
-		return $this->mountPoint;
123
-	}
116
+    /**
117
+     * get complete path to the mount point, relative to data/
118
+     *
119
+     * @return string
120
+     */
121
+    public function getMountPoint() {
122
+        return $this->mountPoint;
123
+    }
124 124
 
125
-	/**
126
-	 * Sets the mount point path, relative to data/
127
-	 *
128
-	 * @param string $mountPoint new mount point
129
-	 */
130
-	public function setMountPoint($mountPoint) {
131
-		$this->mountPoint = $this->formatPath($mountPoint);
132
-	}
125
+    /**
126
+     * Sets the mount point path, relative to data/
127
+     *
128
+     * @param string $mountPoint new mount point
129
+     */
130
+    public function setMountPoint($mountPoint) {
131
+        $this->mountPoint = $this->formatPath($mountPoint);
132
+    }
133 133
 
134
-	/**
135
-	 * create the storage that is mounted
136
-	 */
137
-	private function createStorage() {
138
-		if ($this->invalidStorage) {
139
-			return;
140
-		}
134
+    /**
135
+     * create the storage that is mounted
136
+     */
137
+    private function createStorage() {
138
+        if ($this->invalidStorage) {
139
+            return;
140
+        }
141 141
 
142
-		if (class_exists($this->class)) {
143
-			try {
144
-				$class = $this->class;
145
-				// prevent recursion by setting the storage before applying wrappers
146
-				$this->storage = new $class($this->arguments);
147
-				$this->storage = $this->loader->wrap($this, $this->storage);
148
-			} catch (\Exception $exception) {
149
-				$this->storage = null;
150
-				$this->invalidStorage = true;
151
-				if ($this->mountPoint === '/') {
152
-					// the root storage could not be initialized, show the user!
153
-					throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
154
-				} else {
155
-					\OCP\Util::writeLog('core', $exception->getMessage(), \OCP\Util::ERROR);
156
-				}
157
-				return;
158
-			}
159
-		} else {
160
-			\OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', \OCP\Util::ERROR);
161
-			$this->invalidStorage = true;
162
-			return;
163
-		}
164
-	}
142
+        if (class_exists($this->class)) {
143
+            try {
144
+                $class = $this->class;
145
+                // prevent recursion by setting the storage before applying wrappers
146
+                $this->storage = new $class($this->arguments);
147
+                $this->storage = $this->loader->wrap($this, $this->storage);
148
+            } catch (\Exception $exception) {
149
+                $this->storage = null;
150
+                $this->invalidStorage = true;
151
+                if ($this->mountPoint === '/') {
152
+                    // the root storage could not be initialized, show the user!
153
+                    throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception);
154
+                } else {
155
+                    \OCP\Util::writeLog('core', $exception->getMessage(), \OCP\Util::ERROR);
156
+                }
157
+                return;
158
+            }
159
+        } else {
160
+            \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', \OCP\Util::ERROR);
161
+            $this->invalidStorage = true;
162
+            return;
163
+        }
164
+    }
165 165
 
166
-	/**
167
-	 * @return \OC\Files\Storage\Storage
168
-	 */
169
-	public function getStorage() {
170
-		if (is_null($this->storage)) {
171
-			$this->createStorage();
172
-		}
173
-		return $this->storage;
174
-	}
166
+    /**
167
+     * @return \OC\Files\Storage\Storage
168
+     */
169
+    public function getStorage() {
170
+        if (is_null($this->storage)) {
171
+            $this->createStorage();
172
+        }
173
+        return $this->storage;
174
+    }
175 175
 
176
-	/**
177
-	 * @return string
178
-	 */
179
-	public function getStorageId() {
180
-		if (!$this->storageId) {
181
-			if (is_null($this->storage)) {
182
-				$storage = $this->createStorage(); //FIXME: start using exceptions
183
-				if (is_null($storage)) {
184
-					return null;
185
-				}
176
+    /**
177
+     * @return string
178
+     */
179
+    public function getStorageId() {
180
+        if (!$this->storageId) {
181
+            if (is_null($this->storage)) {
182
+                $storage = $this->createStorage(); //FIXME: start using exceptions
183
+                if (is_null($storage)) {
184
+                    return null;
185
+                }
186 186
 
187
-				$this->storage = $storage;
188
-			}
189
-			$this->storageId = $this->storage->getId();
190
-			if (strlen($this->storageId) > 64) {
191
-				$this->storageId = md5($this->storageId);
192
-			}
193
-		}
194
-		return $this->storageId;
195
-	}
187
+                $this->storage = $storage;
188
+            }
189
+            $this->storageId = $this->storage->getId();
190
+            if (strlen($this->storageId) > 64) {
191
+                $this->storageId = md5($this->storageId);
192
+            }
193
+        }
194
+        return $this->storageId;
195
+    }
196 196
 
197
-	/**
198
-	 * @return int
199
-	 */
200
-	public function getNumericStorageId() {
201
-		return $this->getStorage()->getStorageCache()->getNumericId();
202
-	}
197
+    /**
198
+     * @return int
199
+     */
200
+    public function getNumericStorageId() {
201
+        return $this->getStorage()->getStorageCache()->getNumericId();
202
+    }
203 203
 
204
-	/**
205
-	 * @param string $path
206
-	 * @return string
207
-	 */
208
-	public function getInternalPath($path) {
209
-		$path = Filesystem::normalizePath($path, true, false, true);
210
-		if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
211
-			$internalPath = '';
212
-		} else {
213
-			$internalPath = substr($path, strlen($this->mountPoint));
214
-		}
215
-		// substr returns false instead of an empty string, we always want a string
216
-		return (string)$internalPath;
217
-	}
204
+    /**
205
+     * @param string $path
206
+     * @return string
207
+     */
208
+    public function getInternalPath($path) {
209
+        $path = Filesystem::normalizePath($path, true, false, true);
210
+        if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) {
211
+            $internalPath = '';
212
+        } else {
213
+            $internalPath = substr($path, strlen($this->mountPoint));
214
+        }
215
+        // substr returns false instead of an empty string, we always want a string
216
+        return (string)$internalPath;
217
+    }
218 218
 
219
-	/**
220
-	 * @param string $path
221
-	 * @return string
222
-	 */
223
-	private function formatPath($path) {
224
-		$path = Filesystem::normalizePath($path);
225
-		if (strlen($path) > 1) {
226
-			$path .= '/';
227
-		}
228
-		return $path;
229
-	}
219
+    /**
220
+     * @param string $path
221
+     * @return string
222
+     */
223
+    private function formatPath($path) {
224
+        $path = Filesystem::normalizePath($path);
225
+        if (strlen($path) > 1) {
226
+            $path .= '/';
227
+        }
228
+        return $path;
229
+    }
230 230
 
231
-	/**
232
-	 * @param callable $wrapper
233
-	 */
234
-	public function wrapStorage($wrapper) {
235
-		$storage = $this->getStorage();
236
-		// storage can be null if it couldn't be initialized
237
-		if ($storage != null) {
238
-			$this->storage = $wrapper($this->mountPoint, $storage, $this);
239
-		}
240
-	}
231
+    /**
232
+     * @param callable $wrapper
233
+     */
234
+    public function wrapStorage($wrapper) {
235
+        $storage = $this->getStorage();
236
+        // storage can be null if it couldn't be initialized
237
+        if ($storage != null) {
238
+            $this->storage = $wrapper($this->mountPoint, $storage, $this);
239
+        }
240
+    }
241 241
 
242
-	/**
243
-	 * Get a mount option
244
-	 *
245
-	 * @param string $name Name of the mount option to get
246
-	 * @param mixed $default Default value for the mount option
247
-	 * @return mixed
248
-	 */
249
-	public function getOption($name, $default) {
250
-		return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default;
251
-	}
242
+    /**
243
+     * Get a mount option
244
+     *
245
+     * @param string $name Name of the mount option to get
246
+     * @param mixed $default Default value for the mount option
247
+     * @return mixed
248
+     */
249
+    public function getOption($name, $default) {
250
+        return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default;
251
+    }
252 252
 
253
-	/**
254
-	 * Get all options for the mount
255
-	 *
256
-	 * @return array
257
-	 */
258
-	public function getOptions() {
259
-		return $this->mountOptions;
260
-	}
253
+    /**
254
+     * Get all options for the mount
255
+     *
256
+     * @return array
257
+     */
258
+    public function getOptions() {
259
+        return $this->mountOptions;
260
+    }
261 261
 
262
-	/**
263
-	 * Get the file id of the root of the storage
264
-	 *
265
-	 * @return int
266
-	 */
267
-	public function getStorageRootId() {
268
-		if (is_null($this->rootId)) {
269
-			$this->rootId = (int)$this->getStorage()->getCache()->getId('');
270
-		}
271
-		return $this->rootId;
272
-	}
262
+    /**
263
+     * Get the file id of the root of the storage
264
+     *
265
+     * @return int
266
+     */
267
+    public function getStorageRootId() {
268
+        if (is_null($this->rootId)) {
269
+            $this->rootId = (int)$this->getStorage()->getCache()->getId('');
270
+        }
271
+        return $this->rootId;
272
+    }
273 273
 
274
-	public function getMountId() {
275
-		return $this->mountId;
276
-	}
274
+    public function getMountId() {
275
+        return $this->mountId;
276
+    }
277 277
 
278
-	public function getMountType() {
279
-		return '';
280
-	}
278
+    public function getMountType() {
279
+        return '';
280
+    }
281 281
 }
Please login to merge, or discard this patch.
lib/private/Files/Mount/LocalHomeMountProvider.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -30,15 +30,15 @@
 block discarded – undo
30 30
  * Mount provider for regular posix home folders
31 31
  */
32 32
 class LocalHomeMountProvider implements IHomeMountProvider {
33
-	/**
34
-	 * Get the cache mount for a user
35
-	 *
36
-	 * @param IUser $user
37
-	 * @param IStorageFactory $loader
38
-	 * @return \OCP\Files\Mount\IMountPoint[]
39
-	 */
40
-	public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
41
-		$arguments = ['user' => $user];
42
-		return new MountPoint('\OC\Files\Storage\Home', '/' . $user->getUID(), $arguments, $loader);
43
-	}
33
+    /**
34
+     * Get the cache mount for a user
35
+     *
36
+     * @param IUser $user
37
+     * @param IStorageFactory $loader
38
+     * @return \OCP\Files\Mount\IMountPoint[]
39
+     */
40
+    public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
41
+        $arguments = ['user' => $user];
42
+        return new MountPoint('\OC\Files\Storage\Home', '/' . $user->getUID(), $arguments, $loader);
43
+    }
44 44
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -39,6 +39,6 @@
 block discarded – undo
39 39
 	 */
40 40
 	public function getHomeMountForUser(IUser $user, IStorageFactory $loader) {
41 41
 		$arguments = ['user' => $user];
42
-		return new MountPoint('\OC\Files\Storage\Home', '/' . $user->getUID(), $arguments, $loader);
42
+		return new MountPoint('\OC\Files\Storage\Home', '/'.$user->getUID(), $arguments, $loader);
43 43
 	}
44 44
 }
Please login to merge, or discard this patch.
lib/private/Files/Mount/MoveableMount.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -27,19 +27,19 @@
 block discarded – undo
27 27
  * Defines the mount point to be (re)moved by the user
28 28
  */
29 29
 interface MoveableMount {
30
-	/**
31
-	 * Move the mount point to $target
32
-	 *
33
-	 * @param string $target the target mount point
34
-	 * @return bool
35
-	 */
36
-	public function moveMount($target);
30
+    /**
31
+     * Move the mount point to $target
32
+     *
33
+     * @param string $target the target mount point
34
+     * @return bool
35
+     */
36
+    public function moveMount($target);
37 37
 
38
-	/**
39
-	 * Remove the mount points
40
-	 *
41
-	 * @return mixed
42
-	 * @return bool
43
-	 */
44
-	public function removeMount();
38
+    /**
39
+     * Remove the mount points
40
+     *
41
+     * @return mixed
42
+     * @return bool
43
+     */
44
+    public function removeMount();
45 45
 }
Please login to merge, or discard this patch.
lib/private/Files/Mount/Manager.php 2 patches
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -30,137 +30,137 @@
 block discarded – undo
30 30
 use OCP\Files\Mount\IMountPoint;
31 31
 
32 32
 class Manager implements IMountManager {
33
-	/**
34
-	 * @var MountPoint[]
35
-	 */
36
-	private $mounts = array();
33
+    /**
34
+     * @var MountPoint[]
35
+     */
36
+    private $mounts = array();
37 37
 
38
-	/**
39
-	 * @param IMountPoint $mount
40
-	 */
41
-	public function addMount(IMountPoint $mount) {
42
-		$this->mounts[$mount->getMountPoint()] = $mount;
43
-	}
38
+    /**
39
+     * @param IMountPoint $mount
40
+     */
41
+    public function addMount(IMountPoint $mount) {
42
+        $this->mounts[$mount->getMountPoint()] = $mount;
43
+    }
44 44
 
45
-	/**
46
-	 * @param string $mountPoint
47
-	 */
48
-	public function removeMount($mountPoint) {
49
-		$mountPoint = Filesystem::normalizePath($mountPoint);
50
-		if (strlen($mountPoint) > 1) {
51
-			$mountPoint .= '/';
52
-		}
53
-		unset($this->mounts[$mountPoint]);
54
-	}
45
+    /**
46
+     * @param string $mountPoint
47
+     */
48
+    public function removeMount($mountPoint) {
49
+        $mountPoint = Filesystem::normalizePath($mountPoint);
50
+        if (strlen($mountPoint) > 1) {
51
+            $mountPoint .= '/';
52
+        }
53
+        unset($this->mounts[$mountPoint]);
54
+    }
55 55
 
56
-	/**
57
-	 * @param string $mountPoint
58
-	 * @param string $target
59
-	 */
60
-	public function moveMount($mountPoint, $target){
61
-		$this->mounts[$target] = $this->mounts[$mountPoint];
62
-		unset($this->mounts[$mountPoint]);
63
-	}
56
+    /**
57
+     * @param string $mountPoint
58
+     * @param string $target
59
+     */
60
+    public function moveMount($mountPoint, $target){
61
+        $this->mounts[$target] = $this->mounts[$mountPoint];
62
+        unset($this->mounts[$mountPoint]);
63
+    }
64 64
 
65
-	/**
66
-	 * Find the mount for $path
67
-	 *
68
-	 * @param string $path
69
-	 * @return MountPoint
70
-	 */
71
-	public function find($path) {
72
-		\OC_Util::setupFS();
73
-		$path = $this->formatPath($path);
74
-		if (isset($this->mounts[$path])) {
75
-			return $this->mounts[$path];
76
-		}
65
+    /**
66
+     * Find the mount for $path
67
+     *
68
+     * @param string $path
69
+     * @return MountPoint
70
+     */
71
+    public function find($path) {
72
+        \OC_Util::setupFS();
73
+        $path = $this->formatPath($path);
74
+        if (isset($this->mounts[$path])) {
75
+            return $this->mounts[$path];
76
+        }
77 77
 
78
-		\OC_Hook::emit('OC_Filesystem', 'get_mountpoint', array('path' => $path));
79
-		$foundMountPoint = '';
80
-		$mountPoints = array_keys($this->mounts);
81
-		foreach ($mountPoints as $mountpoint) {
82
-			if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) {
83
-				$foundMountPoint = $mountpoint;
84
-			}
85
-		}
86
-		if (isset($this->mounts[$foundMountPoint])) {
87
-			return $this->mounts[$foundMountPoint];
88
-		} else {
89
-			return null;
90
-		}
91
-	}
78
+        \OC_Hook::emit('OC_Filesystem', 'get_mountpoint', array('path' => $path));
79
+        $foundMountPoint = '';
80
+        $mountPoints = array_keys($this->mounts);
81
+        foreach ($mountPoints as $mountpoint) {
82
+            if (strpos($path, $mountpoint) === 0 and strlen($mountpoint) > strlen($foundMountPoint)) {
83
+                $foundMountPoint = $mountpoint;
84
+            }
85
+        }
86
+        if (isset($this->mounts[$foundMountPoint])) {
87
+            return $this->mounts[$foundMountPoint];
88
+        } else {
89
+            return null;
90
+        }
91
+    }
92 92
 
93
-	/**
94
-	 * Find all mounts in $path
95
-	 *
96
-	 * @param string $path
97
-	 * @return MountPoint[]
98
-	 */
99
-	public function findIn($path) {
100
-		\OC_Util::setupFS();
101
-		$path = $this->formatPath($path);
102
-		$result = array();
103
-		$pathLength = strlen($path);
104
-		$mountPoints = array_keys($this->mounts);
105
-		foreach ($mountPoints as $mountPoint) {
106
-			if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) {
107
-				$result[] = $this->mounts[$mountPoint];
108
-			}
109
-		}
110
-		return $result;
111
-	}
93
+    /**
94
+     * Find all mounts in $path
95
+     *
96
+     * @param string $path
97
+     * @return MountPoint[]
98
+     */
99
+    public function findIn($path) {
100
+        \OC_Util::setupFS();
101
+        $path = $this->formatPath($path);
102
+        $result = array();
103
+        $pathLength = strlen($path);
104
+        $mountPoints = array_keys($this->mounts);
105
+        foreach ($mountPoints as $mountPoint) {
106
+            if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) {
107
+                $result[] = $this->mounts[$mountPoint];
108
+            }
109
+        }
110
+        return $result;
111
+    }
112 112
 
113
-	public function clear() {
114
-		$this->mounts = array();
115
-	}
113
+    public function clear() {
114
+        $this->mounts = array();
115
+    }
116 116
 
117
-	/**
118
-	 * Find mounts by storage id
119
-	 *
120
-	 * @param string $id
121
-	 * @return MountPoint[]
122
-	 */
123
-	public function findByStorageId($id) {
124
-		\OC_Util::setupFS();
125
-		if (strlen($id) > 64) {
126
-			$id = md5($id);
127
-		}
128
-		$result = array();
129
-		foreach ($this->mounts as $mount) {
130
-			if ($mount->getStorageId() === $id) {
131
-				$result[] = $mount;
132
-			}
133
-		}
134
-		return $result;
135
-	}
117
+    /**
118
+     * Find mounts by storage id
119
+     *
120
+     * @param string $id
121
+     * @return MountPoint[]
122
+     */
123
+    public function findByStorageId($id) {
124
+        \OC_Util::setupFS();
125
+        if (strlen($id) > 64) {
126
+            $id = md5($id);
127
+        }
128
+        $result = array();
129
+        foreach ($this->mounts as $mount) {
130
+            if ($mount->getStorageId() === $id) {
131
+                $result[] = $mount;
132
+            }
133
+        }
134
+        return $result;
135
+    }
136 136
 
137
-	/**
138
-	 * @return MountPoint[]
139
-	 */
140
-	public function getAll() {
141
-		return $this->mounts;
142
-	}
137
+    /**
138
+     * @return MountPoint[]
139
+     */
140
+    public function getAll() {
141
+        return $this->mounts;
142
+    }
143 143
 
144
-	/**
145
-	 * Find mounts by numeric storage id
146
-	 *
147
-	 * @param int $id
148
-	 * @return MountPoint[]
149
-	 */
150
-	public function findByNumericId($id) {
151
-		$storageId = \OC\Files\Cache\Storage::getStorageId($id);
152
-		return $this->findByStorageId($storageId);
153
-	}
144
+    /**
145
+     * Find mounts by numeric storage id
146
+     *
147
+     * @param int $id
148
+     * @return MountPoint[]
149
+     */
150
+    public function findByNumericId($id) {
151
+        $storageId = \OC\Files\Cache\Storage::getStorageId($id);
152
+        return $this->findByStorageId($storageId);
153
+    }
154 154
 
155
-	/**
156
-	 * @param string $path
157
-	 * @return string
158
-	 */
159
-	private function formatPath($path) {
160
-		$path = Filesystem::normalizePath($path);
161
-		if (strlen($path) > 1) {
162
-			$path .= '/';
163
-		}
164
-		return $path;
165
-	}
155
+    /**
156
+     * @param string $path
157
+     * @return string
158
+     */
159
+    private function formatPath($path) {
160
+        $path = Filesystem::normalizePath($path);
161
+        if (strlen($path) > 1) {
162
+            $path .= '/';
163
+        }
164
+        return $path;
165
+    }
166 166
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -57,7 +57,7 @@
 block discarded – undo
57 57
 	 * @param string $mountPoint
58 58
 	 * @param string $target
59 59
 	 */
60
-	public function moveMount($mountPoint, $target){
60
+	public function moveMount($mountPoint, $target) {
61 61
 		$this->mounts[$target] = $this->mounts[$mountPoint];
62 62
 		unset($this->mounts[$mountPoint]);
63 63
 	}
Please login to merge, or discard this patch.
lib/private/Files/AppData/Factory.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -27,24 +27,24 @@
 block discarded – undo
27 27
 
28 28
 class Factory {
29 29
 
30
-	/** @var IRootFolder */
31
-	private $rootFolder;
30
+    /** @var IRootFolder */
31
+    private $rootFolder;
32 32
 
33
-	/** @var SystemConfig */
34
-	private $config;
33
+    /** @var SystemConfig */
34
+    private $config;
35 35
 
36
-	public function __construct(IRootFolder $rootFolder,
37
-								SystemConfig $systemConfig) {
36
+    public function __construct(IRootFolder $rootFolder,
37
+                                SystemConfig $systemConfig) {
38 38
 
39
-		$this->rootFolder = $rootFolder;
40
-		$this->config = $systemConfig;
41
-	}
39
+        $this->rootFolder = $rootFolder;
40
+        $this->config = $systemConfig;
41
+    }
42 42
 
43
-	/**
44
-	 * @param string $appId
45
-	 * @return AppData
46
-	 */
47
-	public function get($appId) {
48
-		return new AppData($this->rootFolder, $this->config, $appId);
49
-	}
43
+    /**
44
+     * @param string $appId
45
+     * @return AppData
46
+     */
47
+    public function get($appId) {
48
+        return new AppData($this->rootFolder, $this->config, $appId);
49
+    }
50 50
 }
Please login to merge, or discard this patch.
lib/private/Files/AppData/AppData.php 2 patches
Indentation   +94 added lines, -94 removed lines patch added patch discarded remove patch
@@ -34,98 +34,98 @@
 block discarded – undo
34 34
 
35 35
 class AppData implements IAppData {
36 36
 
37
-	/** @var IRootFolder */
38
-	private $rootFolder;
39
-
40
-	/** @var SystemConfig */
41
-	private $config;
42
-
43
-	/** @var string */
44
-	private $appId;
45
-
46
-	/** @var Folder */
47
-	private $folder;
48
-
49
-	/**
50
-	 * AppData constructor.
51
-	 *
52
-	 * @param IRootFolder $rootFolder
53
-	 * @param SystemConfig $systemConfig
54
-	 * @param string $appId
55
-	 */
56
-	public function __construct(IRootFolder $rootFolder,
57
-								SystemConfig $systemConfig,
58
-								$appId) {
59
-
60
-		$this->rootFolder = $rootFolder;
61
-		$this->config = $systemConfig;
62
-		$this->appId = $appId;
63
-	}
64
-
65
-	/**
66
-	 * @return Folder
67
-	 * @throws \RuntimeException
68
-	 */
69
-	private function getAppDataFolder() {
70
-		if ($this->folder === null) {
71
-			$instanceId = $this->config->getValue('instanceid', null);
72
-			if ($instanceId === null) {
73
-				throw new \RuntimeException('no instance id!');
74
-			}
75
-
76
-			$name = 'appdata_' . $instanceId;
77
-
78
-			try {
79
-				$appDataFolder = $this->rootFolder->get($name);
80
-			} catch (NotFoundException $e) {
81
-				try {
82
-					$appDataFolder = $this->rootFolder->newFolder($name);
83
-				} catch (NotPermittedException $e) {
84
-					throw new \RuntimeException('Could not get appdata folder');
85
-				}
86
-			}
87
-
88
-			try {
89
-				$appDataFolder = $appDataFolder->get($this->appId);
90
-			} catch (NotFoundException $e) {
91
-				try {
92
-					$appDataFolder = $appDataFolder->newFolder($this->appId);
93
-				} catch (NotPermittedException $e) {
94
-					throw new \RuntimeException('Could not get appdata folder for ' . $this->appId);
95
-				}
96
-			}
97
-
98
-			$this->folder = $appDataFolder;
99
-		}
100
-
101
-		return $this->folder;
102
-	}
103
-
104
-	public function getFolder($name) {
105
-		$node = $this->getAppDataFolder()->get($name);
106
-
107
-		/** @var Folder $node */
108
-		return new SimpleFolder($node);
109
-	}
110
-
111
-	public function newFolder($name) {
112
-		$folder = $this->getAppDataFolder()->newFolder($name);
113
-
114
-		return new SimpleFolder($folder);
115
-	}
116
-
117
-	public function getDirectoryListing() {
118
-		$listing = $this->getAppDataFolder()->getDirectoryListing();
119
-
120
-		$fileListing = array_map(function(Node $folder) {
121
-			if ($folder instanceof Folder) {
122
-				return new SimpleFolder($folder);
123
-			}
124
-			return null;
125
-		}, $listing);
126
-
127
-		$fileListing = array_filter($fileListing);
128
-
129
-		return array_values($fileListing);
130
-	}
37
+    /** @var IRootFolder */
38
+    private $rootFolder;
39
+
40
+    /** @var SystemConfig */
41
+    private $config;
42
+
43
+    /** @var string */
44
+    private $appId;
45
+
46
+    /** @var Folder */
47
+    private $folder;
48
+
49
+    /**
50
+     * AppData constructor.
51
+     *
52
+     * @param IRootFolder $rootFolder
53
+     * @param SystemConfig $systemConfig
54
+     * @param string $appId
55
+     */
56
+    public function __construct(IRootFolder $rootFolder,
57
+                                SystemConfig $systemConfig,
58
+                                $appId) {
59
+
60
+        $this->rootFolder = $rootFolder;
61
+        $this->config = $systemConfig;
62
+        $this->appId = $appId;
63
+    }
64
+
65
+    /**
66
+     * @return Folder
67
+     * @throws \RuntimeException
68
+     */
69
+    private function getAppDataFolder() {
70
+        if ($this->folder === null) {
71
+            $instanceId = $this->config->getValue('instanceid', null);
72
+            if ($instanceId === null) {
73
+                throw new \RuntimeException('no instance id!');
74
+            }
75
+
76
+            $name = 'appdata_' . $instanceId;
77
+
78
+            try {
79
+                $appDataFolder = $this->rootFolder->get($name);
80
+            } catch (NotFoundException $e) {
81
+                try {
82
+                    $appDataFolder = $this->rootFolder->newFolder($name);
83
+                } catch (NotPermittedException $e) {
84
+                    throw new \RuntimeException('Could not get appdata folder');
85
+                }
86
+            }
87
+
88
+            try {
89
+                $appDataFolder = $appDataFolder->get($this->appId);
90
+            } catch (NotFoundException $e) {
91
+                try {
92
+                    $appDataFolder = $appDataFolder->newFolder($this->appId);
93
+                } catch (NotPermittedException $e) {
94
+                    throw new \RuntimeException('Could not get appdata folder for ' . $this->appId);
95
+                }
96
+            }
97
+
98
+            $this->folder = $appDataFolder;
99
+        }
100
+
101
+        return $this->folder;
102
+    }
103
+
104
+    public function getFolder($name) {
105
+        $node = $this->getAppDataFolder()->get($name);
106
+
107
+        /** @var Folder $node */
108
+        return new SimpleFolder($node);
109
+    }
110
+
111
+    public function newFolder($name) {
112
+        $folder = $this->getAppDataFolder()->newFolder($name);
113
+
114
+        return new SimpleFolder($folder);
115
+    }
116
+
117
+    public function getDirectoryListing() {
118
+        $listing = $this->getAppDataFolder()->getDirectoryListing();
119
+
120
+        $fileListing = array_map(function(Node $folder) {
121
+            if ($folder instanceof Folder) {
122
+                return new SimpleFolder($folder);
123
+            }
124
+            return null;
125
+        }, $listing);
126
+
127
+        $fileListing = array_filter($fileListing);
128
+
129
+        return array_values($fileListing);
130
+    }
131 131
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
 				throw new \RuntimeException('no instance id!');
74 74
 			}
75 75
 
76
-			$name = 'appdata_' . $instanceId;
76
+			$name = 'appdata_'.$instanceId;
77 77
 
78 78
 			try {
79 79
 				$appDataFolder = $this->rootFolder->get($name);
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
 				try {
92 92
 					$appDataFolder = $appDataFolder->newFolder($this->appId);
93 93
 				} catch (NotPermittedException $e) {
94
-					throw new \RuntimeException('Could not get appdata folder for ' . $this->appId);
94
+					throw new \RuntimeException('Could not get appdata folder for '.$this->appId);
95 95
 				}
96 96
 			}
97 97
 
Please login to merge, or discard this patch.
lib/private/Files/Stream/Encryption.php 2 patches
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 								$unencryptedSize,
157 157
 								$headerSize,
158 158
 								$signed,
159
-								$wrapper =  'OC\Files\Stream\Encryption') {
159
+								$wrapper = 'OC\Files\Stream\Encryption') {
160 160
 
161 161
 		$context = stream_context_create(array(
162 162
 			'ocencryption' => array(
@@ -195,9 +195,9 @@  discard block
 block discarded – undo
195 195
 		try {
196 196
 			stream_wrapper_register($protocol, $class);
197 197
 			if (@rewinddir($source) === false) {
198
-				$wrapped = fopen($protocol . '://', $mode, false, $context);
198
+				$wrapped = fopen($protocol.'://', $mode, false, $context);
199 199
 			} else {
200
-				$wrapped = opendir($protocol . '://', $context);
200
+				$wrapped = opendir($protocol.'://', $context);
201 201
 			}
202 202
 		} catch (\BadMethodCallException $e) {
203 203
 			stream_wrapper_unregister($protocol);
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
 			if (array_key_exists($property, $context)) {
222 222
 				$this->{$property} = $context[$property];
223 223
 			} else {
224
-				throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
224
+				throw new \BadMethodCallException('Invalid context, "'.$property.'" options not set');
225 225
 			}
226 226
 		}
227 227
 		return $context;
@@ -320,7 +320,7 @@  discard block
 block discarded – undo
320 320
 
321 321
 			// for seekable streams the pointer is moved back to the beginning of the encrypted block
322 322
 			// flush will start writing there when the position moves to another block
323
-			$positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
323
+			$positionInFile = (int) floor($this->position / $this->unencryptedBlockSize) *
324 324
 				$this->util->getBlockSize() + $this->headerSize;
325 325
 			$resultFseek = $this->parentStreamSeek($positionInFile);
326 326
 
@@ -337,14 +337,14 @@  discard block
 block discarded – undo
337 337
 				// update position and liberate $data
338 338
 				if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
339 339
 					$this->cache = substr($this->cache, 0, $blockPosition)
340
-						. $data . substr($this->cache, $blockPosition + $remainingLength);
340
+						. $data.substr($this->cache, $blockPosition + $remainingLength);
341 341
 					$this->position += $remainingLength;
342 342
 					$length += $remainingLength;
343 343
 					$data = '';
344 344
 					// if $data doesn't fit the current block, the fill the current block and reiterate
345 345
 					// after the block is filled, it is flushed and $data is updatedxxx
346 346
 				} else {
347
-					$this->cache = substr($this->cache, 0, $blockPosition) .
347
+					$this->cache = substr($this->cache, 0, $blockPosition).
348 348
 						substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
349 349
 					$this->flush();
350 350
 					$this->position += ($this->unencryptedBlockSize - $blockPosition);
@@ -402,10 +402,10 @@  discard block
 block discarded – undo
402 402
 
403 403
 	public function stream_close() {
404 404
 		$this->flush('end');
405
-		$position = (int)floor($this->position/$this->unencryptedBlockSize);
406
-		$remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
405
+		$position = (int) floor($this->position / $this->unencryptedBlockSize);
406
+		$remainingData = $this->encryptionModule->end($this->fullPath, $position.'end');
407 407
 		if ($this->readOnly === false) {
408
-			if(!empty($remainingData)) {
408
+			if (!empty($remainingData)) {
409 409
 				parent::stream_write($remainingData);
410 410
 			}
411 411
 			$this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
@@ -424,16 +424,16 @@  discard block
 block discarded – undo
424 424
 			// automatically attempted when the file is written to disk -
425 425
 			// we are handling that separately here and we don't want to
426 426
 			// get into an infinite loop
427
-			$position = (int)floor($this->position/$this->unencryptedBlockSize);
428
-			$encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
427
+			$position = (int) floor($this->position / $this->unencryptedBlockSize);
428
+			$encrypted = $this->encryptionModule->encrypt($this->cache, $position.$positionPrefix);
429 429
 			$bytesWritten = parent::stream_write($encrypted);
430 430
 			$this->writeFlag = false;
431 431
 			// Check whether the write concerns the last block
432 432
 			// If so then update the encrypted filesize
433 433
 			// Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
434 434
 			// We recalculate the encrypted filesize as we do not know the context of calling flush()
435
-			$completeBlocksInFile=(int)floor($this->unencryptedSize/$this->unencryptedBlockSize);
436
-			if ($completeBlocksInFile === (int)floor($this->position/$this->unencryptedBlockSize)) {
435
+			$completeBlocksInFile = (int) floor($this->unencryptedSize / $this->unencryptedBlockSize);
436
+			if ($completeBlocksInFile === (int) floor($this->position / $this->unencryptedBlockSize)) {
437 437
 				$this->size = $this->util->getBlockSize() * $completeBlocksInFile;
438 438
 				$this->size += $bytesWritten;
439 439
 				$this->size += $this->headerSize;
@@ -452,9 +452,9 @@  discard block
 block discarded – undo
452 452
 		if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
453 453
 			// Get the data from the file handle
454 454
 			$data = parent::stream_read($this->util->getBlockSize());
455
-			$position = (int)floor($this->position/$this->unencryptedBlockSize);
456
-			$numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
457
-			if($numberOfChunks === $position) {
455
+			$position = (int) floor($this->position / $this->unencryptedBlockSize);
456
+			$numberOfChunks = (int) ($this->unencryptedSize / $this->unencryptedBlockSize);
457
+			if ($numberOfChunks === $position) {
458 458
 				$position .= 'end';
459 459
 			}
460 460
 			$this->cache = $this->encryptionModule->decrypt($data, $position);
Please login to merge, or discard this patch.
Indentation   +483 added lines, -483 removed lines patch added patch discarded remove patch
@@ -33,488 +33,488 @@
 block discarded – undo
33 33
 
34 34
 class Encryption extends Wrapper {
35 35
 
36
-	/** @var \OC\Encryption\Util */
37
-	protected $util;
38
-
39
-	/** @var \OC\Encryption\File */
40
-	protected $file;
41
-
42
-	/** @var \OCP\Encryption\IEncryptionModule */
43
-	protected $encryptionModule;
44
-
45
-	/** @var \OC\Files\Storage\Storage */
46
-	protected $storage;
47
-
48
-	/** @var \OC\Files\Storage\Wrapper\Encryption */
49
-	protected $encryptionStorage;
50
-
51
-	/** @var string */
52
-	protected $internalPath;
53
-
54
-	/** @var string */
55
-	protected $cache;
56
-
57
-	/** @var integer */
58
-	protected $size;
59
-
60
-	/** @var integer */
61
-	protected $position;
62
-
63
-	/** @var integer */
64
-	protected $unencryptedSize;
65
-
66
-	/** @var integer */
67
-	protected $headerSize;
68
-
69
-	/** @var integer */
70
-	protected $unencryptedBlockSize;
71
-
72
-	/** @var array */
73
-	protected $header;
74
-
75
-	/** @var string */
76
-	protected $fullPath;
77
-
78
-	/** @var  bool */
79
-	protected $signed;
80
-
81
-	/**
82
-	 * header data returned by the encryption module, will be written to the file
83
-	 * in case of a write operation
84
-	 *
85
-	 * @var array
86
-	 */
87
-	protected $newHeader;
88
-
89
-	/**
90
-	 * user who perform the read/write operation null for public access
91
-	 *
92
-	 * @var string
93
-	 */
94
-	protected $uid;
95
-
96
-	/** @var bool */
97
-	protected $readOnly;
98
-
99
-	/** @var bool */
100
-	protected $writeFlag;
101
-
102
-	/** @var array */
103
-	protected $expectedContextProperties;
104
-
105
-	/** @var bool */
106
-	protected $fileUpdated;
107
-
108
-	public function __construct() {
109
-		$this->expectedContextProperties = array(
110
-			'source',
111
-			'storage',
112
-			'internalPath',
113
-			'fullPath',
114
-			'encryptionModule',
115
-			'header',
116
-			'uid',
117
-			'file',
118
-			'util',
119
-			'size',
120
-			'unencryptedSize',
121
-			'encryptionStorage',
122
-			'headerSize',
123
-			'signed'
124
-		);
125
-	}
126
-
127
-
128
-	/**
129
-	 * Wraps a stream with the provided callbacks
130
-	 *
131
-	 * @param resource $source
132
-	 * @param string $internalPath relative to mount point
133
-	 * @param string $fullPath relative to data/
134
-	 * @param array $header
135
-	 * @param string $uid
136
-	 * @param \OCP\Encryption\IEncryptionModule $encryptionModule
137
-	 * @param \OC\Files\Storage\Storage $storage
138
-	 * @param \OC\Files\Storage\Wrapper\Encryption $encStorage
139
-	 * @param \OC\Encryption\Util $util
140
-	 * @param \OC\Encryption\File $file
141
-	 * @param string $mode
142
-	 * @param int $size
143
-	 * @param int $unencryptedSize
144
-	 * @param int $headerSize
145
-	 * @param bool $signed
146
-	 * @param string $wrapper stream wrapper class
147
-	 * @return resource
148
-	 *
149
-	 * @throws \BadMethodCallException
150
-	 */
151
-	public static function wrap($source, $internalPath, $fullPath, array $header,
152
-								$uid,
153
-								\OCP\Encryption\IEncryptionModule $encryptionModule,
154
-								\OC\Files\Storage\Storage $storage,
155
-								\OC\Files\Storage\Wrapper\Encryption $encStorage,
156
-								\OC\Encryption\Util $util,
157
-								 \OC\Encryption\File $file,
158
-								$mode,
159
-								$size,
160
-								$unencryptedSize,
161
-								$headerSize,
162
-								$signed,
163
-								$wrapper =  'OC\Files\Stream\Encryption') {
164
-
165
-		$context = stream_context_create(array(
166
-			'ocencryption' => array(
167
-				'source' => $source,
168
-				'storage' => $storage,
169
-				'internalPath' => $internalPath,
170
-				'fullPath' => $fullPath,
171
-				'encryptionModule' => $encryptionModule,
172
-				'header' => $header,
173
-				'uid' => $uid,
174
-				'util' => $util,
175
-				'file' => $file,
176
-				'size' => $size,
177
-				'unencryptedSize' => $unencryptedSize,
178
-				'encryptionStorage' => $encStorage,
179
-				'headerSize' => $headerSize,
180
-				'signed' => $signed
181
-			)
182
-		));
183
-
184
-		return self::wrapSource($source, $context, 'ocencryption', $wrapper, $mode);
185
-	}
186
-
187
-	/**
188
-	 * add stream wrapper
189
-	 *
190
-	 * @param resource $source
191
-	 * @param string $mode
192
-	 * @param resource $context
193
-	 * @param string $protocol
194
-	 * @param string $class
195
-	 * @return resource
196
-	 * @throws \BadMethodCallException
197
-	 */
198
-	protected static function wrapSource($source, $context, $protocol, $class, $mode = 'r+') {
199
-		try {
200
-			stream_wrapper_register($protocol, $class);
201
-			if (@rewinddir($source) === false) {
202
-				$wrapped = fopen($protocol . '://', $mode, false, $context);
203
-			} else {
204
-				$wrapped = opendir($protocol . '://', $context);
205
-			}
206
-		} catch (\BadMethodCallException $e) {
207
-			stream_wrapper_unregister($protocol);
208
-			throw $e;
209
-		}
210
-		stream_wrapper_unregister($protocol);
211
-		return $wrapped;
212
-	}
213
-
214
-	/**
215
-	 * Load the source from the stream context and return the context options
216
-	 *
217
-	 * @param string $name
218
-	 * @return array
219
-	 * @throws \BadMethodCallException
220
-	 */
221
-	protected function loadContext($name) {
222
-		$context = parent::loadContext($name);
223
-
224
-		foreach ($this->expectedContextProperties as $property) {
225
-			if (array_key_exists($property, $context)) {
226
-				$this->{$property} = $context[$property];
227
-			} else {
228
-				throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
229
-			}
230
-		}
231
-		return $context;
232
-
233
-	}
234
-
235
-	public function stream_open($path, $mode, $options, &$opened_path) {
236
-		$this->loadContext('ocencryption');
237
-
238
-		$this->position = 0;
239
-		$this->cache = '';
240
-		$this->writeFlag = false;
241
-		$this->fileUpdated = false;
242
-		$this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed);
243
-
244
-		if (
245
-			$mode === 'w'
246
-			|| $mode === 'w+'
247
-			|| $mode === 'wb'
248
-			|| $mode === 'wb+'
249
-			|| $mode === 'r+'
250
-			|| $mode === 'rb+'
251
-		) {
252
-			$this->readOnly = false;
253
-		} else {
254
-			$this->readOnly = true;
255
-		}
256
-
257
-		$sharePath = $this->fullPath;
258
-		if (!$this->storage->file_exists($this->internalPath)) {
259
-			$sharePath = dirname($sharePath);
260
-		}
261
-
262
-		$accessList = [];
263
-		if ($this->encryptionModule->needDetailedAccessList()) {
264
-			$accessList = $this->file->getAccessList($sharePath);
265
-		}
266
-		$this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList);
267
-
268
-		if (
269
-			$mode === 'w'
270
-			|| $mode === 'w+'
271
-			|| $mode === 'wb'
272
-			|| $mode === 'wb+'
273
-		) {
274
-			// We're writing a new file so start write counter with 0 bytes
275
-			$this->unencryptedSize = 0;
276
-			$this->writeHeader();
277
-			$this->headerSize = $this->util->getHeaderSize();
278
-			$this->size = $this->headerSize;
279
-		} else {
280
-			$this->skipHeader();
281
-		}
282
-
283
-		return true;
284
-
285
-	}
286
-
287
-	public function stream_eof() {
288
-		return $this->position >= $this->unencryptedSize;
289
-	}
290
-
291
-	public function stream_read($count) {
292
-
293
-		$result = '';
294
-
295
-		$count = min($count, $this->unencryptedSize - $this->position);
296
-		while ($count > 0) {
297
-			$remainingLength = $count;
298
-			// update the cache of the current block
299
-			$this->readCache();
300
-			// determine the relative position in the current block
301
-			$blockPosition = ($this->position % $this->unencryptedBlockSize);
302
-			// if entire read inside current block then only position needs to be updated
303
-			if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
304
-				$result .= substr($this->cache, $blockPosition, $remainingLength);
305
-				$this->position += $remainingLength;
306
-				$count = 0;
307
-				// otherwise remainder of current block is fetched, the block is flushed and the position updated
308
-			} else {
309
-				$result .= substr($this->cache, $blockPosition);
310
-				$this->flush();
311
-				$this->position += ($this->unencryptedBlockSize - $blockPosition);
312
-				$count -= ($this->unencryptedBlockSize - $blockPosition);
313
-			}
314
-		}
315
-		return $result;
316
-
317
-	}
318
-
319
-	public function stream_write($data) {
320
-		$length = 0;
321
-		// loop over $data to fit it in 6126 sized unencrypted blocks
322
-		while (isset($data[0])) {
323
-			$remainingLength = strlen($data);
324
-
325
-			// set the cache to the current 6126 block
326
-			$this->readCache();
327
-
328
-			// for seekable streams the pointer is moved back to the beginning of the encrypted block
329
-			// flush will start writing there when the position moves to another block
330
-			$positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
331
-				$this->util->getBlockSize() + $this->headerSize;
332
-			$resultFseek = $this->parentStreamSeek($positionInFile);
333
-
334
-			// only allow writes on seekable streams, or at the end of the encrypted stream
335
-			if (!($this->readOnly) && ($resultFseek || $positionInFile === $this->size)) {
336
-
337
-				// switch the writeFlag so flush() will write the block
338
-				$this->writeFlag = true;
339
-				$this->fileUpdated = true;
340
-
341
-				// determine the relative position in the current block
342
-				$blockPosition = ($this->position % $this->unencryptedBlockSize);
343
-				// check if $data fits in current block
344
-				// if so, overwrite existing data (if any)
345
-				// update position and liberate $data
346
-				if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
347
-					$this->cache = substr($this->cache, 0, $blockPosition)
348
-						. $data . substr($this->cache, $blockPosition + $remainingLength);
349
-					$this->position += $remainingLength;
350
-					$length += $remainingLength;
351
-					$data = '';
352
-					// if $data doesn't fit the current block, the fill the current block and reiterate
353
-					// after the block is filled, it is flushed and $data is updatedxxx
354
-				} else {
355
-					$this->cache = substr($this->cache, 0, $blockPosition) .
356
-						substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
357
-					$this->flush();
358
-					$this->position += ($this->unencryptedBlockSize - $blockPosition);
359
-					$length += ($this->unencryptedBlockSize - $blockPosition);
360
-					$data = substr($data, $this->unencryptedBlockSize - $blockPosition);
361
-				}
362
-			} else {
363
-				$data = '';
364
-			}
365
-			$this->unencryptedSize = max($this->unencryptedSize, $this->position);
366
-		}
367
-		return $length;
368
-	}
369
-
370
-	public function stream_tell() {
371
-		return $this->position;
372
-	}
373
-
374
-	public function stream_seek($offset, $whence = SEEK_SET) {
375
-
376
-		$return = false;
377
-
378
-		switch ($whence) {
379
-			case SEEK_SET:
380
-				$newPosition = $offset;
381
-				break;
382
-			case SEEK_CUR:
383
-				$newPosition = $this->position + $offset;
384
-				break;
385
-			case SEEK_END:
386
-				$newPosition = $this->unencryptedSize + $offset;
387
-				break;
388
-			default:
389
-				return $return;
390
-		}
391
-
392
-		if ($newPosition > $this->unencryptedSize || $newPosition < 0) {
393
-			return $return;
394
-		}
395
-
396
-		$newFilePosition = floor($newPosition / $this->unencryptedBlockSize)
397
-			* $this->util->getBlockSize() + $this->headerSize;
398
-
399
-		$oldFilePosition = parent::stream_tell();
400
-		if ($this->parentStreamSeek($newFilePosition)) {
401
-			$this->parentStreamSeek($oldFilePosition);
402
-			$this->flush();
403
-			$this->parentStreamSeek($newFilePosition);
404
-			$this->position = $newPosition;
405
-			$return = true;
406
-		}
407
-		return $return;
408
-
409
-	}
410
-
411
-	public function stream_close() {
412
-		$this->flush('end');
413
-		$position = (int)floor($this->position/$this->unencryptedBlockSize);
414
-		$remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
415
-		if ($this->readOnly === false) {
416
-			if(!empty($remainingData)) {
417
-				parent::stream_write($remainingData);
418
-			}
419
-			$this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
420
-		}
421
-		$result = parent::stream_close();
422
-
423
-		if ($this->fileUpdated) {
424
-			$cache = $this->storage->getCache();
425
-			$cacheEntry = $cache->get($this->internalPath);
426
-			if ($cacheEntry) {
427
-				$version = $cacheEntry['encryptedVersion'] + 1;
428
-				$cache->update($cacheEntry->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
429
-			}
430
-		}
431
-
432
-		return $result;
433
-	}
434
-
435
-	/**
436
-	 * write block to file
437
-	 * @param string $positionPrefix
438
-	 */
439
-	protected function flush($positionPrefix = '') {
440
-		// write to disk only when writeFlag was set to 1
441
-		if ($this->writeFlag) {
442
-			// Disable the file proxies so that encryption is not
443
-			// automatically attempted when the file is written to disk -
444
-			// we are handling that separately here and we don't want to
445
-			// get into an infinite loop
446
-			$position = (int)floor($this->position/$this->unencryptedBlockSize);
447
-			$encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
448
-			$bytesWritten = parent::stream_write($encrypted);
449
-			$this->writeFlag = false;
450
-			// Check whether the write concerns the last block
451
-			// If so then update the encrypted filesize
452
-			// Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
453
-			// We recalculate the encrypted filesize as we do not know the context of calling flush()
454
-			$completeBlocksInFile=(int)floor($this->unencryptedSize/$this->unencryptedBlockSize);
455
-			if ($completeBlocksInFile === (int)floor($this->position/$this->unencryptedBlockSize)) {
456
-				$this->size = $this->util->getBlockSize() * $completeBlocksInFile;
457
-				$this->size += $bytesWritten;
458
-				$this->size += $this->headerSize;
459
-			}
460
-		}
461
-		// always empty the cache (otherwise readCache() will not fill it with the new block)
462
-		$this->cache = '';
463
-	}
464
-
465
-	/**
466
-	 * read block to file
467
-	 */
468
-	protected function readCache() {
469
-		// cache should always be empty string when this function is called
470
-		// don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
471
-		if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
472
-			// Get the data from the file handle
473
-			$data = parent::stream_read($this->util->getBlockSize());
474
-			$position = (int)floor($this->position/$this->unencryptedBlockSize);
475
-			$numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
476
-			if($numberOfChunks === $position) {
477
-				$position .= 'end';
478
-			}
479
-			$this->cache = $this->encryptionModule->decrypt($data, $position);
480
-		}
481
-	}
482
-
483
-	/**
484
-	 * write header at beginning of encrypted file
485
-	 *
486
-	 * @return integer
487
-	 * @throws EncryptionHeaderKeyExistsException if header key is already in use
488
-	 */
489
-	protected function writeHeader() {
490
-		$header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
491
-		return parent::stream_write($header);
492
-	}
493
-
494
-	/**
495
-	 * read first block to skip the header
496
-	 */
497
-	protected function skipHeader() {
498
-		parent::stream_read($this->headerSize);
499
-	}
500
-
501
-	/**
502
-	 * call stream_seek() from parent class
503
-	 *
504
-	 * @param integer $position
505
-	 * @return bool
506
-	 */
507
-	protected function parentStreamSeek($position) {
508
-		return parent::stream_seek($position);
509
-	}
510
-
511
-	/**
512
-	 * @param string $path
513
-	 * @param array $options
514
-	 * @return bool
515
-	 */
516
-	public function dir_opendir($path, $options) {
517
-		return false;
518
-	}
36
+    /** @var \OC\Encryption\Util */
37
+    protected $util;
38
+
39
+    /** @var \OC\Encryption\File */
40
+    protected $file;
41
+
42
+    /** @var \OCP\Encryption\IEncryptionModule */
43
+    protected $encryptionModule;
44
+
45
+    /** @var \OC\Files\Storage\Storage */
46
+    protected $storage;
47
+
48
+    /** @var \OC\Files\Storage\Wrapper\Encryption */
49
+    protected $encryptionStorage;
50
+
51
+    /** @var string */
52
+    protected $internalPath;
53
+
54
+    /** @var string */
55
+    protected $cache;
56
+
57
+    /** @var integer */
58
+    protected $size;
59
+
60
+    /** @var integer */
61
+    protected $position;
62
+
63
+    /** @var integer */
64
+    protected $unencryptedSize;
65
+
66
+    /** @var integer */
67
+    protected $headerSize;
68
+
69
+    /** @var integer */
70
+    protected $unencryptedBlockSize;
71
+
72
+    /** @var array */
73
+    protected $header;
74
+
75
+    /** @var string */
76
+    protected $fullPath;
77
+
78
+    /** @var  bool */
79
+    protected $signed;
80
+
81
+    /**
82
+     * header data returned by the encryption module, will be written to the file
83
+     * in case of a write operation
84
+     *
85
+     * @var array
86
+     */
87
+    protected $newHeader;
88
+
89
+    /**
90
+     * user who perform the read/write operation null for public access
91
+     *
92
+     * @var string
93
+     */
94
+    protected $uid;
95
+
96
+    /** @var bool */
97
+    protected $readOnly;
98
+
99
+    /** @var bool */
100
+    protected $writeFlag;
101
+
102
+    /** @var array */
103
+    protected $expectedContextProperties;
104
+
105
+    /** @var bool */
106
+    protected $fileUpdated;
107
+
108
+    public function __construct() {
109
+        $this->expectedContextProperties = array(
110
+            'source',
111
+            'storage',
112
+            'internalPath',
113
+            'fullPath',
114
+            'encryptionModule',
115
+            'header',
116
+            'uid',
117
+            'file',
118
+            'util',
119
+            'size',
120
+            'unencryptedSize',
121
+            'encryptionStorage',
122
+            'headerSize',
123
+            'signed'
124
+        );
125
+    }
126
+
127
+
128
+    /**
129
+     * Wraps a stream with the provided callbacks
130
+     *
131
+     * @param resource $source
132
+     * @param string $internalPath relative to mount point
133
+     * @param string $fullPath relative to data/
134
+     * @param array $header
135
+     * @param string $uid
136
+     * @param \OCP\Encryption\IEncryptionModule $encryptionModule
137
+     * @param \OC\Files\Storage\Storage $storage
138
+     * @param \OC\Files\Storage\Wrapper\Encryption $encStorage
139
+     * @param \OC\Encryption\Util $util
140
+     * @param \OC\Encryption\File $file
141
+     * @param string $mode
142
+     * @param int $size
143
+     * @param int $unencryptedSize
144
+     * @param int $headerSize
145
+     * @param bool $signed
146
+     * @param string $wrapper stream wrapper class
147
+     * @return resource
148
+     *
149
+     * @throws \BadMethodCallException
150
+     */
151
+    public static function wrap($source, $internalPath, $fullPath, array $header,
152
+                                $uid,
153
+                                \OCP\Encryption\IEncryptionModule $encryptionModule,
154
+                                \OC\Files\Storage\Storage $storage,
155
+                                \OC\Files\Storage\Wrapper\Encryption $encStorage,
156
+                                \OC\Encryption\Util $util,
157
+                                    \OC\Encryption\File $file,
158
+                                $mode,
159
+                                $size,
160
+                                $unencryptedSize,
161
+                                $headerSize,
162
+                                $signed,
163
+                                $wrapper =  'OC\Files\Stream\Encryption') {
164
+
165
+        $context = stream_context_create(array(
166
+            'ocencryption' => array(
167
+                'source' => $source,
168
+                'storage' => $storage,
169
+                'internalPath' => $internalPath,
170
+                'fullPath' => $fullPath,
171
+                'encryptionModule' => $encryptionModule,
172
+                'header' => $header,
173
+                'uid' => $uid,
174
+                'util' => $util,
175
+                'file' => $file,
176
+                'size' => $size,
177
+                'unencryptedSize' => $unencryptedSize,
178
+                'encryptionStorage' => $encStorage,
179
+                'headerSize' => $headerSize,
180
+                'signed' => $signed
181
+            )
182
+        ));
183
+
184
+        return self::wrapSource($source, $context, 'ocencryption', $wrapper, $mode);
185
+    }
186
+
187
+    /**
188
+     * add stream wrapper
189
+     *
190
+     * @param resource $source
191
+     * @param string $mode
192
+     * @param resource $context
193
+     * @param string $protocol
194
+     * @param string $class
195
+     * @return resource
196
+     * @throws \BadMethodCallException
197
+     */
198
+    protected static function wrapSource($source, $context, $protocol, $class, $mode = 'r+') {
199
+        try {
200
+            stream_wrapper_register($protocol, $class);
201
+            if (@rewinddir($source) === false) {
202
+                $wrapped = fopen($protocol . '://', $mode, false, $context);
203
+            } else {
204
+                $wrapped = opendir($protocol . '://', $context);
205
+            }
206
+        } catch (\BadMethodCallException $e) {
207
+            stream_wrapper_unregister($protocol);
208
+            throw $e;
209
+        }
210
+        stream_wrapper_unregister($protocol);
211
+        return $wrapped;
212
+    }
213
+
214
+    /**
215
+     * Load the source from the stream context and return the context options
216
+     *
217
+     * @param string $name
218
+     * @return array
219
+     * @throws \BadMethodCallException
220
+     */
221
+    protected function loadContext($name) {
222
+        $context = parent::loadContext($name);
223
+
224
+        foreach ($this->expectedContextProperties as $property) {
225
+            if (array_key_exists($property, $context)) {
226
+                $this->{$property} = $context[$property];
227
+            } else {
228
+                throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
229
+            }
230
+        }
231
+        return $context;
232
+
233
+    }
234
+
235
+    public function stream_open($path, $mode, $options, &$opened_path) {
236
+        $this->loadContext('ocencryption');
237
+
238
+        $this->position = 0;
239
+        $this->cache = '';
240
+        $this->writeFlag = false;
241
+        $this->fileUpdated = false;
242
+        $this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed);
243
+
244
+        if (
245
+            $mode === 'w'
246
+            || $mode === 'w+'
247
+            || $mode === 'wb'
248
+            || $mode === 'wb+'
249
+            || $mode === 'r+'
250
+            || $mode === 'rb+'
251
+        ) {
252
+            $this->readOnly = false;
253
+        } else {
254
+            $this->readOnly = true;
255
+        }
256
+
257
+        $sharePath = $this->fullPath;
258
+        if (!$this->storage->file_exists($this->internalPath)) {
259
+            $sharePath = dirname($sharePath);
260
+        }
261
+
262
+        $accessList = [];
263
+        if ($this->encryptionModule->needDetailedAccessList()) {
264
+            $accessList = $this->file->getAccessList($sharePath);
265
+        }
266
+        $this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList);
267
+
268
+        if (
269
+            $mode === 'w'
270
+            || $mode === 'w+'
271
+            || $mode === 'wb'
272
+            || $mode === 'wb+'
273
+        ) {
274
+            // We're writing a new file so start write counter with 0 bytes
275
+            $this->unencryptedSize = 0;
276
+            $this->writeHeader();
277
+            $this->headerSize = $this->util->getHeaderSize();
278
+            $this->size = $this->headerSize;
279
+        } else {
280
+            $this->skipHeader();
281
+        }
282
+
283
+        return true;
284
+
285
+    }
286
+
287
+    public function stream_eof() {
288
+        return $this->position >= $this->unencryptedSize;
289
+    }
290
+
291
+    public function stream_read($count) {
292
+
293
+        $result = '';
294
+
295
+        $count = min($count, $this->unencryptedSize - $this->position);
296
+        while ($count > 0) {
297
+            $remainingLength = $count;
298
+            // update the cache of the current block
299
+            $this->readCache();
300
+            // determine the relative position in the current block
301
+            $blockPosition = ($this->position % $this->unencryptedBlockSize);
302
+            // if entire read inside current block then only position needs to be updated
303
+            if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
304
+                $result .= substr($this->cache, $blockPosition, $remainingLength);
305
+                $this->position += $remainingLength;
306
+                $count = 0;
307
+                // otherwise remainder of current block is fetched, the block is flushed and the position updated
308
+            } else {
309
+                $result .= substr($this->cache, $blockPosition);
310
+                $this->flush();
311
+                $this->position += ($this->unencryptedBlockSize - $blockPosition);
312
+                $count -= ($this->unencryptedBlockSize - $blockPosition);
313
+            }
314
+        }
315
+        return $result;
316
+
317
+    }
318
+
319
+    public function stream_write($data) {
320
+        $length = 0;
321
+        // loop over $data to fit it in 6126 sized unencrypted blocks
322
+        while (isset($data[0])) {
323
+            $remainingLength = strlen($data);
324
+
325
+            // set the cache to the current 6126 block
326
+            $this->readCache();
327
+
328
+            // for seekable streams the pointer is moved back to the beginning of the encrypted block
329
+            // flush will start writing there when the position moves to another block
330
+            $positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
331
+                $this->util->getBlockSize() + $this->headerSize;
332
+            $resultFseek = $this->parentStreamSeek($positionInFile);
333
+
334
+            // only allow writes on seekable streams, or at the end of the encrypted stream
335
+            if (!($this->readOnly) && ($resultFseek || $positionInFile === $this->size)) {
336
+
337
+                // switch the writeFlag so flush() will write the block
338
+                $this->writeFlag = true;
339
+                $this->fileUpdated = true;
340
+
341
+                // determine the relative position in the current block
342
+                $blockPosition = ($this->position % $this->unencryptedBlockSize);
343
+                // check if $data fits in current block
344
+                // if so, overwrite existing data (if any)
345
+                // update position and liberate $data
346
+                if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
347
+                    $this->cache = substr($this->cache, 0, $blockPosition)
348
+                        . $data . substr($this->cache, $blockPosition + $remainingLength);
349
+                    $this->position += $remainingLength;
350
+                    $length += $remainingLength;
351
+                    $data = '';
352
+                    // if $data doesn't fit the current block, the fill the current block and reiterate
353
+                    // after the block is filled, it is flushed and $data is updatedxxx
354
+                } else {
355
+                    $this->cache = substr($this->cache, 0, $blockPosition) .
356
+                        substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
357
+                    $this->flush();
358
+                    $this->position += ($this->unencryptedBlockSize - $blockPosition);
359
+                    $length += ($this->unencryptedBlockSize - $blockPosition);
360
+                    $data = substr($data, $this->unencryptedBlockSize - $blockPosition);
361
+                }
362
+            } else {
363
+                $data = '';
364
+            }
365
+            $this->unencryptedSize = max($this->unencryptedSize, $this->position);
366
+        }
367
+        return $length;
368
+    }
369
+
370
+    public function stream_tell() {
371
+        return $this->position;
372
+    }
373
+
374
+    public function stream_seek($offset, $whence = SEEK_SET) {
375
+
376
+        $return = false;
377
+
378
+        switch ($whence) {
379
+            case SEEK_SET:
380
+                $newPosition = $offset;
381
+                break;
382
+            case SEEK_CUR:
383
+                $newPosition = $this->position + $offset;
384
+                break;
385
+            case SEEK_END:
386
+                $newPosition = $this->unencryptedSize + $offset;
387
+                break;
388
+            default:
389
+                return $return;
390
+        }
391
+
392
+        if ($newPosition > $this->unencryptedSize || $newPosition < 0) {
393
+            return $return;
394
+        }
395
+
396
+        $newFilePosition = floor($newPosition / $this->unencryptedBlockSize)
397
+            * $this->util->getBlockSize() + $this->headerSize;
398
+
399
+        $oldFilePosition = parent::stream_tell();
400
+        if ($this->parentStreamSeek($newFilePosition)) {
401
+            $this->parentStreamSeek($oldFilePosition);
402
+            $this->flush();
403
+            $this->parentStreamSeek($newFilePosition);
404
+            $this->position = $newPosition;
405
+            $return = true;
406
+        }
407
+        return $return;
408
+
409
+    }
410
+
411
+    public function stream_close() {
412
+        $this->flush('end');
413
+        $position = (int)floor($this->position/$this->unencryptedBlockSize);
414
+        $remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
415
+        if ($this->readOnly === false) {
416
+            if(!empty($remainingData)) {
417
+                parent::stream_write($remainingData);
418
+            }
419
+            $this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
420
+        }
421
+        $result = parent::stream_close();
422
+
423
+        if ($this->fileUpdated) {
424
+            $cache = $this->storage->getCache();
425
+            $cacheEntry = $cache->get($this->internalPath);
426
+            if ($cacheEntry) {
427
+                $version = $cacheEntry['encryptedVersion'] + 1;
428
+                $cache->update($cacheEntry->getId(), ['encrypted' => $version, 'encryptedVersion' => $version]);
429
+            }
430
+        }
431
+
432
+        return $result;
433
+    }
434
+
435
+    /**
436
+     * write block to file
437
+     * @param string $positionPrefix
438
+     */
439
+    protected function flush($positionPrefix = '') {
440
+        // write to disk only when writeFlag was set to 1
441
+        if ($this->writeFlag) {
442
+            // Disable the file proxies so that encryption is not
443
+            // automatically attempted when the file is written to disk -
444
+            // we are handling that separately here and we don't want to
445
+            // get into an infinite loop
446
+            $position = (int)floor($this->position/$this->unencryptedBlockSize);
447
+            $encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
448
+            $bytesWritten = parent::stream_write($encrypted);
449
+            $this->writeFlag = false;
450
+            // Check whether the write concerns the last block
451
+            // If so then update the encrypted filesize
452
+            // Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
453
+            // We recalculate the encrypted filesize as we do not know the context of calling flush()
454
+            $completeBlocksInFile=(int)floor($this->unencryptedSize/$this->unencryptedBlockSize);
455
+            if ($completeBlocksInFile === (int)floor($this->position/$this->unencryptedBlockSize)) {
456
+                $this->size = $this->util->getBlockSize() * $completeBlocksInFile;
457
+                $this->size += $bytesWritten;
458
+                $this->size += $this->headerSize;
459
+            }
460
+        }
461
+        // always empty the cache (otherwise readCache() will not fill it with the new block)
462
+        $this->cache = '';
463
+    }
464
+
465
+    /**
466
+     * read block to file
467
+     */
468
+    protected function readCache() {
469
+        // cache should always be empty string when this function is called
470
+        // don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
471
+        if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
472
+            // Get the data from the file handle
473
+            $data = parent::stream_read($this->util->getBlockSize());
474
+            $position = (int)floor($this->position/$this->unencryptedBlockSize);
475
+            $numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
476
+            if($numberOfChunks === $position) {
477
+                $position .= 'end';
478
+            }
479
+            $this->cache = $this->encryptionModule->decrypt($data, $position);
480
+        }
481
+    }
482
+
483
+    /**
484
+     * write header at beginning of encrypted file
485
+     *
486
+     * @return integer
487
+     * @throws EncryptionHeaderKeyExistsException if header key is already in use
488
+     */
489
+    protected function writeHeader() {
490
+        $header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
491
+        return parent::stream_write($header);
492
+    }
493
+
494
+    /**
495
+     * read first block to skip the header
496
+     */
497
+    protected function skipHeader() {
498
+        parent::stream_read($this->headerSize);
499
+    }
500
+
501
+    /**
502
+     * call stream_seek() from parent class
503
+     *
504
+     * @param integer $position
505
+     * @return bool
506
+     */
507
+    protected function parentStreamSeek($position) {
508
+        return parent::stream_seek($position);
509
+    }
510
+
511
+    /**
512
+     * @param string $path
513
+     * @param array $options
514
+     * @return bool
515
+     */
516
+    public function dir_opendir($path, $options) {
517
+        return false;
518
+    }
519 519
 
520 520
 }
Please login to merge, or discard this patch.
lib/private/Files/Stream/Quota.php 3 patches
Indentation   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -33,71 +33,71 @@
 block discarded – undo
33 33
  * usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
34 34
  */
35 35
 class Quota extends Wrapper {
36
-	/**
37
-	 * @var int $limit
38
-	 */
39
-	private $limit;
36
+    /**
37
+     * @var int $limit
38
+     */
39
+    private $limit;
40 40
 
41
-	/**
42
-	 * @param resource $stream
43
-	 * @param int $limit
44
-	 * @return resource
45
-	 */
46
-	static public function wrap($stream, $limit) {
47
-		$context = stream_context_create(array(
48
-			'quota' => array(
49
-				'source' => $stream,
50
-				'limit' => $limit
51
-			)
52
-		));
53
-		return Wrapper::wrapSource($stream, $context, 'quota', self::class);
54
-	}
41
+    /**
42
+     * @param resource $stream
43
+     * @param int $limit
44
+     * @return resource
45
+     */
46
+    static public function wrap($stream, $limit) {
47
+        $context = stream_context_create(array(
48
+            'quota' => array(
49
+                'source' => $stream,
50
+                'limit' => $limit
51
+            )
52
+        ));
53
+        return Wrapper::wrapSource($stream, $context, 'quota', self::class);
54
+    }
55 55
 
56
-	public function stream_open($path, $mode, $options, &$opened_path) {
57
-		$context = $this->loadContext('quota');
58
-		$this->source = $context['source'];
59
-		$this->limit = $context['limit'];
56
+    public function stream_open($path, $mode, $options, &$opened_path) {
57
+        $context = $this->loadContext('quota');
58
+        $this->source = $context['source'];
59
+        $this->limit = $context['limit'];
60 60
 
61
-		return true;
62
-	}
61
+        return true;
62
+    }
63 63
 
64
-	public function dir_opendir($path, $options) {
65
-		return false;
66
-	}
64
+    public function dir_opendir($path, $options) {
65
+        return false;
66
+    }
67 67
 
68
-	public function stream_seek($offset, $whence = SEEK_SET) {
69
-		if ($whence === SEEK_END){
70
-			// go to the end to find out last position's offset
71
-			$oldOffset = $this->stream_tell();
72
-			if (fseek($this->source, 0, $whence) !== 0){
73
-				return false;
74
-			}
75
-			$whence = SEEK_SET;
76
-			$offset = $this->stream_tell() + $offset;
77
-			$this->limit += $oldOffset - $offset;
78
-		}
79
-		else if ($whence === SEEK_SET) {
80
-			$this->limit += $this->stream_tell() - $offset;
81
-		} else {
82
-			$this->limit -= $offset;
83
-		}
84
-		// this wrapper needs to return "true" for success.
85
-		// the fseek call itself returns 0 on succeess
86
-		return fseek($this->source, $offset, $whence) === 0;
87
-	}
68
+    public function stream_seek($offset, $whence = SEEK_SET) {
69
+        if ($whence === SEEK_END){
70
+            // go to the end to find out last position's offset
71
+            $oldOffset = $this->stream_tell();
72
+            if (fseek($this->source, 0, $whence) !== 0){
73
+                return false;
74
+            }
75
+            $whence = SEEK_SET;
76
+            $offset = $this->stream_tell() + $offset;
77
+            $this->limit += $oldOffset - $offset;
78
+        }
79
+        else if ($whence === SEEK_SET) {
80
+            $this->limit += $this->stream_tell() - $offset;
81
+        } else {
82
+            $this->limit -= $offset;
83
+        }
84
+        // this wrapper needs to return "true" for success.
85
+        // the fseek call itself returns 0 on succeess
86
+        return fseek($this->source, $offset, $whence) === 0;
87
+    }
88 88
 
89
-	public function stream_read($count) {
90
-		$this->limit -= $count;
91
-		return fread($this->source, $count);
92
-	}
89
+    public function stream_read($count) {
90
+        $this->limit -= $count;
91
+        return fread($this->source, $count);
92
+    }
93 93
 
94
-	public function stream_write($data) {
95
-		$size = strlen($data);
96
-		if ($size > $this->limit) {
97
-			$data = substr($data, 0, $this->limit);
98
-			$size = $this->limit;
99
-		}
100
-		$this->limit -= $size;
101
-		return fwrite($this->source, $data);
102
-	}
94
+    public function stream_write($data) {
95
+        $size = strlen($data);
96
+        if ($size > $this->limit) {
97
+            $data = substr($data, 0, $this->limit);
98
+            $size = $this->limit;
99
+        }
100
+        $this->limit -= $size;
101
+        return fwrite($this->source, $data);
102
+    }
103 103
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -66,10 +66,10 @@
 block discarded – undo
66 66
 	}
67 67
 
68 68
 	public function stream_seek($offset, $whence = SEEK_SET) {
69
-		if ($whence === SEEK_END){
69
+		if ($whence === SEEK_END) {
70 70
 			// go to the end to find out last position's offset
71 71
 			$oldOffset = $this->stream_tell();
72
-			if (fseek($this->source, 0, $whence) !== 0){
72
+			if (fseek($this->source, 0, $whence) !== 0) {
73 73
 				return false;
74 74
 			}
75 75
 			$whence = SEEK_SET;
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -75,8 +75,7 @@
 block discarded – undo
75 75
 			$whence = SEEK_SET;
76 76
 			$offset = $this->stream_tell() + $offset;
77 77
 			$this->limit += $oldOffset - $offset;
78
-		}
79
-		else if ($whence === SEEK_SET) {
78
+		} else if ($whence === SEEK_SET) {
80 79
 			$this->limit += $this->stream_tell() - $offset;
81 80
 		} else {
82 81
 			$this->limit -= $offset;
Please login to merge, or discard this patch.