Completed
Push — stable13 ( 080267...628a04 )
by
unknown
32:35 queued 15:53
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   +468 added lines, -468 removed lines patch added patch discarded remove patch
@@ -32,473 +32,473 @@
 block discarded – undo
32 32
 
33 33
 class Encryption extends Wrapper {
34 34
 
35
-	/** @var \OC\Encryption\Util */
36
-	protected $util;
37
-
38
-	/** @var \OC\Encryption\File */
39
-	protected $file;
40
-
41
-	/** @var \OCP\Encryption\IEncryptionModule */
42
-	protected $encryptionModule;
43
-
44
-	/** @var \OC\Files\Storage\Storage */
45
-	protected $storage;
46
-
47
-	/** @var \OC\Files\Storage\Wrapper\Encryption */
48
-	protected $encryptionStorage;
49
-
50
-	/** @var string */
51
-	protected $internalPath;
52
-
53
-	/** @var string */
54
-	protected $cache;
55
-
56
-	/** @var integer */
57
-	protected $size;
58
-
59
-	/** @var integer */
60
-	protected $position;
61
-
62
-	/** @var integer */
63
-	protected $unencryptedSize;
64
-
65
-	/** @var integer */
66
-	protected $headerSize;
67
-
68
-	/** @var integer */
69
-	protected $unencryptedBlockSize;
70
-
71
-	/** @var array */
72
-	protected $header;
73
-
74
-	/** @var string */
75
-	protected $fullPath;
76
-
77
-	/** @var  bool */
78
-	protected $signed;
79
-
80
-	/**
81
-	 * header data returned by the encryption module, will be written to the file
82
-	 * in case of a write operation
83
-	 *
84
-	 * @var array
85
-	 */
86
-	protected $newHeader;
87
-
88
-	/**
89
-	 * user who perform the read/write operation null for public access
90
-	 *
91
-	 * @var string
92
-	 */
93
-	protected $uid;
94
-
95
-	/** @var bool */
96
-	protected $readOnly;
97
-
98
-	/** @var bool */
99
-	protected $writeFlag;
100
-
101
-	/** @var array */
102
-	protected $expectedContextProperties;
103
-
104
-	public function __construct() {
105
-		$this->expectedContextProperties = array(
106
-			'source',
107
-			'storage',
108
-			'internalPath',
109
-			'fullPath',
110
-			'encryptionModule',
111
-			'header',
112
-			'uid',
113
-			'file',
114
-			'util',
115
-			'size',
116
-			'unencryptedSize',
117
-			'encryptionStorage',
118
-			'headerSize',
119
-			'signed'
120
-		);
121
-	}
122
-
123
-
124
-	/**
125
-	 * Wraps a stream with the provided callbacks
126
-	 *
127
-	 * @param resource $source
128
-	 * @param string $internalPath relative to mount point
129
-	 * @param string $fullPath relative to data/
130
-	 * @param array $header
131
-	 * @param string $uid
132
-	 * @param \OCP\Encryption\IEncryptionModule $encryptionModule
133
-	 * @param \OC\Files\Storage\Storage $storage
134
-	 * @param \OC\Files\Storage\Wrapper\Encryption $encStorage
135
-	 * @param \OC\Encryption\Util $util
136
-	 * @param \OC\Encryption\File $file
137
-	 * @param string $mode
138
-	 * @param int $size
139
-	 * @param int $unencryptedSize
140
-	 * @param int $headerSize
141
-	 * @param bool $signed
142
-	 * @param string $wrapper stream wrapper class
143
-	 * @return resource
144
-	 *
145
-	 * @throws \BadMethodCallException
146
-	 */
147
-	public static function wrap($source, $internalPath, $fullPath, array $header,
148
-								$uid,
149
-								\OCP\Encryption\IEncryptionModule $encryptionModule,
150
-								\OC\Files\Storage\Storage $storage,
151
-								\OC\Files\Storage\Wrapper\Encryption $encStorage,
152
-								\OC\Encryption\Util $util,
153
-								 \OC\Encryption\File $file,
154
-								$mode,
155
-								$size,
156
-								$unencryptedSize,
157
-								$headerSize,
158
-								$signed,
159
-								$wrapper =  'OC\Files\Stream\Encryption') {
160
-
161
-		$context = stream_context_create(array(
162
-			'ocencryption' => array(
163
-				'source' => $source,
164
-				'storage' => $storage,
165
-				'internalPath' => $internalPath,
166
-				'fullPath' => $fullPath,
167
-				'encryptionModule' => $encryptionModule,
168
-				'header' => $header,
169
-				'uid' => $uid,
170
-				'util' => $util,
171
-				'file' => $file,
172
-				'size' => $size,
173
-				'unencryptedSize' => $unencryptedSize,
174
-				'encryptionStorage' => $encStorage,
175
-				'headerSize' => $headerSize,
176
-				'signed' => $signed
177
-			)
178
-		));
179
-
180
-		return self::wrapSource($source, $context, 'ocencryption', $wrapper, $mode);
181
-	}
182
-
183
-	/**
184
-	 * add stream wrapper
185
-	 *
186
-	 * @param resource $source
187
-	 * @param string $mode
188
-	 * @param resource $context
189
-	 * @param string $protocol
190
-	 * @param string $class
191
-	 * @return resource
192
-	 * @throws \BadMethodCallException
193
-	 */
194
-	protected static function wrapSource($source, $context, $protocol, $class, $mode = 'r+') {
195
-		try {
196
-			stream_wrapper_register($protocol, $class);
197
-			if (@rewinddir($source) === false) {
198
-				$wrapped = fopen($protocol . '://', $mode, false, $context);
199
-			} else {
200
-				$wrapped = opendir($protocol . '://', $context);
201
-			}
202
-		} catch (\BadMethodCallException $e) {
203
-			stream_wrapper_unregister($protocol);
204
-			throw $e;
205
-		}
206
-		stream_wrapper_unregister($protocol);
207
-		return $wrapped;
208
-	}
209
-
210
-	/**
211
-	 * Load the source from the stream context and return the context options
212
-	 *
213
-	 * @param string $name
214
-	 * @return array
215
-	 * @throws \BadMethodCallException
216
-	 */
217
-	protected function loadContext($name) {
218
-		$context = parent::loadContext($name);
219
-
220
-		foreach ($this->expectedContextProperties as $property) {
221
-			if (array_key_exists($property, $context)) {
222
-				$this->{$property} = $context[$property];
223
-			} else {
224
-				throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
225
-			}
226
-		}
227
-		return $context;
228
-
229
-	}
230
-
231
-	public function stream_open($path, $mode, $options, &$opened_path) {
232
-		$this->loadContext('ocencryption');
233
-
234
-		$this->position = 0;
235
-		$this->cache = '';
236
-		$this->writeFlag = false;
237
-		$this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed);
238
-
239
-		if (
240
-			$mode === 'w'
241
-			|| $mode === 'w+'
242
-			|| $mode === 'wb'
243
-			|| $mode === 'wb+'
244
-			|| $mode === 'r+'
245
-			|| $mode === 'rb+'
246
-		) {
247
-			$this->readOnly = false;
248
-		} else {
249
-			$this->readOnly = true;
250
-		}
251
-
252
-		$sharePath = $this->fullPath;
253
-		if (!$this->storage->file_exists($this->internalPath)) {
254
-			$sharePath = dirname($sharePath);
255
-		}
256
-
257
-		$accessList = [];
258
-		if ($this->encryptionModule->needDetailedAccessList()) {
259
-			$accessList = $this->file->getAccessList($sharePath);
260
-		}
261
-		$this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList);
262
-
263
-		if (
264
-			$mode === 'w'
265
-			|| $mode === 'w+'
266
-			|| $mode === 'wb'
267
-			|| $mode === 'wb+'
268
-		) {
269
-			// We're writing a new file so start write counter with 0 bytes
270
-			$this->unencryptedSize = 0;
271
-			$this->writeHeader();
272
-			$this->headerSize = $this->util->getHeaderSize();
273
-			$this->size = $this->headerSize;
274
-		} else {
275
-			$this->skipHeader();
276
-		}
277
-
278
-		return true;
279
-
280
-	}
281
-
282
-	public function stream_eof() {
283
-		return $this->position >= $this->unencryptedSize;
284
-	}
285
-
286
-	public function stream_read($count) {
287
-
288
-		$result = '';
289
-
290
-		$count = min($count, $this->unencryptedSize - $this->position);
291
-		while ($count > 0) {
292
-			$remainingLength = $count;
293
-			// update the cache of the current block
294
-			$this->readCache();
295
-			// determine the relative position in the current block
296
-			$blockPosition = ($this->position % $this->unencryptedBlockSize);
297
-			// if entire read inside current block then only position needs to be updated
298
-			if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
299
-				$result .= substr($this->cache, $blockPosition, $remainingLength);
300
-				$this->position += $remainingLength;
301
-				$count = 0;
302
-				// otherwise remainder of current block is fetched, the block is flushed and the position updated
303
-			} else {
304
-				$result .= substr($this->cache, $blockPosition);
305
-				$this->flush();
306
-				$this->position += ($this->unencryptedBlockSize - $blockPosition);
307
-				$count -= ($this->unencryptedBlockSize - $blockPosition);
308
-			}
309
-		}
310
-		return $result;
311
-
312
-	}
313
-
314
-	public function stream_write($data) {
315
-
316
-		$length = 0;
317
-		// loop over $data to fit it in 6126 sized unencrypted blocks
318
-		while (isset($data[0])) {
319
-			$remainingLength = strlen($data);
320
-
321
-			// set the cache to the current 6126 block
322
-			$this->readCache();
323
-
324
-			// for seekable streams the pointer is moved back to the beginning of the encrypted block
325
-			// flush will start writing there when the position moves to another block
326
-			$positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
327
-				$this->util->getBlockSize() + $this->headerSize;
328
-			$resultFseek = $this->parentStreamSeek($positionInFile);
329
-
330
-			// only allow writes on seekable streams, or at the end of the encrypted stream
331
-			if (!($this->readOnly) && ($resultFseek || $positionInFile === $this->size)) {
332
-
333
-				// switch the writeFlag so flush() will write the block
334
-				$this->writeFlag = true;
335
-
336
-				// determine the relative position in the current block
337
-				$blockPosition = ($this->position % $this->unencryptedBlockSize);
338
-				// check if $data fits in current block
339
-				// if so, overwrite existing data (if any)
340
-				// update position and liberate $data
341
-				if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
342
-					$this->cache = substr($this->cache, 0, $blockPosition)
343
-						. $data . substr($this->cache, $blockPosition + $remainingLength);
344
-					$this->position += $remainingLength;
345
-					$length += $remainingLength;
346
-					$data = '';
347
-					// if $data doesn't fit the current block, the fill the current block and reiterate
348
-					// after the block is filled, it is flushed and $data is updatedxxx
349
-				} else {
350
-					$this->cache = substr($this->cache, 0, $blockPosition) .
351
-						substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
352
-					$this->flush();
353
-					$this->position += ($this->unencryptedBlockSize - $blockPosition);
354
-					$length += ($this->unencryptedBlockSize - $blockPosition);
355
-					$data = substr($data, $this->unencryptedBlockSize - $blockPosition);
356
-				}
357
-			} else {
358
-				$data = '';
359
-			}
360
-			$this->unencryptedSize = max($this->unencryptedSize, $this->position);
361
-		}
362
-		return $length;
363
-	}
364
-
365
-	public function stream_tell() {
366
-		return $this->position;
367
-	}
368
-
369
-	public function stream_seek($offset, $whence = SEEK_SET) {
370
-
371
-		$return = false;
372
-
373
-		switch ($whence) {
374
-			case SEEK_SET:
375
-				$newPosition = $offset;
376
-				break;
377
-			case SEEK_CUR:
378
-				$newPosition = $this->position + $offset;
379
-				break;
380
-			case SEEK_END:
381
-				$newPosition = $this->unencryptedSize + $offset;
382
-				break;
383
-			default:
384
-				return $return;
385
-		}
386
-
387
-		if ($newPosition > $this->unencryptedSize || $newPosition < 0) {
388
-			return $return;
389
-		}
390
-
391
-		$newFilePosition = floor($newPosition / $this->unencryptedBlockSize)
392
-			* $this->util->getBlockSize() + $this->headerSize;
393
-
394
-		$oldFilePosition = parent::stream_tell();
395
-		if ($this->parentStreamSeek($newFilePosition)) {
396
-			$this->parentStreamSeek($oldFilePosition);
397
-			$this->flush();
398
-			$this->parentStreamSeek($newFilePosition);
399
-			$this->position = $newPosition;
400
-			$return = true;
401
-		}
402
-		return $return;
403
-
404
-	}
405
-
406
-	public function stream_close() {
407
-		$this->flush('end');
408
-		$position = (int)floor($this->position/$this->unencryptedBlockSize);
409
-		$remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
410
-		if ($this->readOnly === false) {
411
-			if(!empty($remainingData)) {
412
-				parent::stream_write($remainingData);
413
-			}
414
-			$this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
415
-		}
416
-		return parent::stream_close();
417
-	}
418
-
419
-	/**
420
-	 * write block to file
421
-	 * @param string $positionPrefix
422
-	 */
423
-	protected function flush($positionPrefix = '') {
424
-		// write to disk only when writeFlag was set to 1
425
-		if ($this->writeFlag) {
426
-			// Disable the file proxies so that encryption is not
427
-			// automatically attempted when the file is written to disk -
428
-			// we are handling that separately here and we don't want to
429
-			// get into an infinite loop
430
-			$position = (int)floor($this->position/$this->unencryptedBlockSize);
431
-			$encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
432
-			$bytesWritten = parent::stream_write($encrypted);
433
-			$this->writeFlag = false;
434
-			// Check whether the write concerns the last block
435
-			// If so then update the encrypted filesize
436
-			// Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
437
-			// We recalculate the encrypted filesize as we do not know the context of calling flush()
438
-			$completeBlocksInFile=(int)floor($this->unencryptedSize/$this->unencryptedBlockSize);
439
-			if ($completeBlocksInFile === (int)floor($this->position/$this->unencryptedBlockSize)) {
440
-				$this->size = $this->util->getBlockSize() * $completeBlocksInFile;
441
-				$this->size += $bytesWritten;
442
-				$this->size += $this->headerSize;
443
-			}
444
-		}
445
-		// always empty the cache (otherwise readCache() will not fill it with the new block)
446
-		$this->cache = '';
447
-	}
448
-
449
-	/**
450
-	 * read block to file
451
-	 */
452
-	protected function readCache() {
453
-		// cache should always be empty string when this function is called
454
-		// don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
455
-		if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
456
-			// Get the data from the file handle
457
-			$data = parent::stream_read($this->util->getBlockSize());
458
-			$position = (int)floor($this->position/$this->unencryptedBlockSize);
459
-			$numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
460
-			if($numberOfChunks === $position) {
461
-				$position .= 'end';
462
-			}
463
-			$this->cache = $this->encryptionModule->decrypt($data, $position);
464
-		}
465
-	}
466
-
467
-	/**
468
-	 * write header at beginning of encrypted file
469
-	 *
470
-	 * @return integer
471
-	 * @throws EncryptionHeaderKeyExistsException if header key is already in use
472
-	 */
473
-	protected function writeHeader() {
474
-		$header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
475
-		return parent::stream_write($header);
476
-	}
477
-
478
-	/**
479
-	 * read first block to skip the header
480
-	 */
481
-	protected function skipHeader() {
482
-		parent::stream_read($this->headerSize);
483
-	}
484
-
485
-	/**
486
-	 * call stream_seek() from parent class
487
-	 *
488
-	 * @param integer $position
489
-	 * @return bool
490
-	 */
491
-	protected function parentStreamSeek($position) {
492
-		return parent::stream_seek($position);
493
-	}
494
-
495
-	/**
496
-	 * @param string $path
497
-	 * @param array $options
498
-	 * @return bool
499
-	 */
500
-	public function dir_opendir($path, $options) {
501
-		return false;
502
-	}
35
+    /** @var \OC\Encryption\Util */
36
+    protected $util;
37
+
38
+    /** @var \OC\Encryption\File */
39
+    protected $file;
40
+
41
+    /** @var \OCP\Encryption\IEncryptionModule */
42
+    protected $encryptionModule;
43
+
44
+    /** @var \OC\Files\Storage\Storage */
45
+    protected $storage;
46
+
47
+    /** @var \OC\Files\Storage\Wrapper\Encryption */
48
+    protected $encryptionStorage;
49
+
50
+    /** @var string */
51
+    protected $internalPath;
52
+
53
+    /** @var string */
54
+    protected $cache;
55
+
56
+    /** @var integer */
57
+    protected $size;
58
+
59
+    /** @var integer */
60
+    protected $position;
61
+
62
+    /** @var integer */
63
+    protected $unencryptedSize;
64
+
65
+    /** @var integer */
66
+    protected $headerSize;
67
+
68
+    /** @var integer */
69
+    protected $unencryptedBlockSize;
70
+
71
+    /** @var array */
72
+    protected $header;
73
+
74
+    /** @var string */
75
+    protected $fullPath;
76
+
77
+    /** @var  bool */
78
+    protected $signed;
79
+
80
+    /**
81
+     * header data returned by the encryption module, will be written to the file
82
+     * in case of a write operation
83
+     *
84
+     * @var array
85
+     */
86
+    protected $newHeader;
87
+
88
+    /**
89
+     * user who perform the read/write operation null for public access
90
+     *
91
+     * @var string
92
+     */
93
+    protected $uid;
94
+
95
+    /** @var bool */
96
+    protected $readOnly;
97
+
98
+    /** @var bool */
99
+    protected $writeFlag;
100
+
101
+    /** @var array */
102
+    protected $expectedContextProperties;
103
+
104
+    public function __construct() {
105
+        $this->expectedContextProperties = array(
106
+            'source',
107
+            'storage',
108
+            'internalPath',
109
+            'fullPath',
110
+            'encryptionModule',
111
+            'header',
112
+            'uid',
113
+            'file',
114
+            'util',
115
+            'size',
116
+            'unencryptedSize',
117
+            'encryptionStorage',
118
+            'headerSize',
119
+            'signed'
120
+        );
121
+    }
122
+
123
+
124
+    /**
125
+     * Wraps a stream with the provided callbacks
126
+     *
127
+     * @param resource $source
128
+     * @param string $internalPath relative to mount point
129
+     * @param string $fullPath relative to data/
130
+     * @param array $header
131
+     * @param string $uid
132
+     * @param \OCP\Encryption\IEncryptionModule $encryptionModule
133
+     * @param \OC\Files\Storage\Storage $storage
134
+     * @param \OC\Files\Storage\Wrapper\Encryption $encStorage
135
+     * @param \OC\Encryption\Util $util
136
+     * @param \OC\Encryption\File $file
137
+     * @param string $mode
138
+     * @param int $size
139
+     * @param int $unencryptedSize
140
+     * @param int $headerSize
141
+     * @param bool $signed
142
+     * @param string $wrapper stream wrapper class
143
+     * @return resource
144
+     *
145
+     * @throws \BadMethodCallException
146
+     */
147
+    public static function wrap($source, $internalPath, $fullPath, array $header,
148
+                                $uid,
149
+                                \OCP\Encryption\IEncryptionModule $encryptionModule,
150
+                                \OC\Files\Storage\Storage $storage,
151
+                                \OC\Files\Storage\Wrapper\Encryption $encStorage,
152
+                                \OC\Encryption\Util $util,
153
+                                    \OC\Encryption\File $file,
154
+                                $mode,
155
+                                $size,
156
+                                $unencryptedSize,
157
+                                $headerSize,
158
+                                $signed,
159
+                                $wrapper =  'OC\Files\Stream\Encryption') {
160
+
161
+        $context = stream_context_create(array(
162
+            'ocencryption' => array(
163
+                'source' => $source,
164
+                'storage' => $storage,
165
+                'internalPath' => $internalPath,
166
+                'fullPath' => $fullPath,
167
+                'encryptionModule' => $encryptionModule,
168
+                'header' => $header,
169
+                'uid' => $uid,
170
+                'util' => $util,
171
+                'file' => $file,
172
+                'size' => $size,
173
+                'unencryptedSize' => $unencryptedSize,
174
+                'encryptionStorage' => $encStorage,
175
+                'headerSize' => $headerSize,
176
+                'signed' => $signed
177
+            )
178
+        ));
179
+
180
+        return self::wrapSource($source, $context, 'ocencryption', $wrapper, $mode);
181
+    }
182
+
183
+    /**
184
+     * add stream wrapper
185
+     *
186
+     * @param resource $source
187
+     * @param string $mode
188
+     * @param resource $context
189
+     * @param string $protocol
190
+     * @param string $class
191
+     * @return resource
192
+     * @throws \BadMethodCallException
193
+     */
194
+    protected static function wrapSource($source, $context, $protocol, $class, $mode = 'r+') {
195
+        try {
196
+            stream_wrapper_register($protocol, $class);
197
+            if (@rewinddir($source) === false) {
198
+                $wrapped = fopen($protocol . '://', $mode, false, $context);
199
+            } else {
200
+                $wrapped = opendir($protocol . '://', $context);
201
+            }
202
+        } catch (\BadMethodCallException $e) {
203
+            stream_wrapper_unregister($protocol);
204
+            throw $e;
205
+        }
206
+        stream_wrapper_unregister($protocol);
207
+        return $wrapped;
208
+    }
209
+
210
+    /**
211
+     * Load the source from the stream context and return the context options
212
+     *
213
+     * @param string $name
214
+     * @return array
215
+     * @throws \BadMethodCallException
216
+     */
217
+    protected function loadContext($name) {
218
+        $context = parent::loadContext($name);
219
+
220
+        foreach ($this->expectedContextProperties as $property) {
221
+            if (array_key_exists($property, $context)) {
222
+                $this->{$property} = $context[$property];
223
+            } else {
224
+                throw new \BadMethodCallException('Invalid context, "' . $property . '" options not set');
225
+            }
226
+        }
227
+        return $context;
228
+
229
+    }
230
+
231
+    public function stream_open($path, $mode, $options, &$opened_path) {
232
+        $this->loadContext('ocencryption');
233
+
234
+        $this->position = 0;
235
+        $this->cache = '';
236
+        $this->writeFlag = false;
237
+        $this->unencryptedBlockSize = $this->encryptionModule->getUnencryptedBlockSize($this->signed);
238
+
239
+        if (
240
+            $mode === 'w'
241
+            || $mode === 'w+'
242
+            || $mode === 'wb'
243
+            || $mode === 'wb+'
244
+            || $mode === 'r+'
245
+            || $mode === 'rb+'
246
+        ) {
247
+            $this->readOnly = false;
248
+        } else {
249
+            $this->readOnly = true;
250
+        }
251
+
252
+        $sharePath = $this->fullPath;
253
+        if (!$this->storage->file_exists($this->internalPath)) {
254
+            $sharePath = dirname($sharePath);
255
+        }
256
+
257
+        $accessList = [];
258
+        if ($this->encryptionModule->needDetailedAccessList()) {
259
+            $accessList = $this->file->getAccessList($sharePath);
260
+        }
261
+        $this->newHeader = $this->encryptionModule->begin($this->fullPath, $this->uid, $mode, $this->header, $accessList);
262
+
263
+        if (
264
+            $mode === 'w'
265
+            || $mode === 'w+'
266
+            || $mode === 'wb'
267
+            || $mode === 'wb+'
268
+        ) {
269
+            // We're writing a new file so start write counter with 0 bytes
270
+            $this->unencryptedSize = 0;
271
+            $this->writeHeader();
272
+            $this->headerSize = $this->util->getHeaderSize();
273
+            $this->size = $this->headerSize;
274
+        } else {
275
+            $this->skipHeader();
276
+        }
277
+
278
+        return true;
279
+
280
+    }
281
+
282
+    public function stream_eof() {
283
+        return $this->position >= $this->unencryptedSize;
284
+    }
285
+
286
+    public function stream_read($count) {
287
+
288
+        $result = '';
289
+
290
+        $count = min($count, $this->unencryptedSize - $this->position);
291
+        while ($count > 0) {
292
+            $remainingLength = $count;
293
+            // update the cache of the current block
294
+            $this->readCache();
295
+            // determine the relative position in the current block
296
+            $blockPosition = ($this->position % $this->unencryptedBlockSize);
297
+            // if entire read inside current block then only position needs to be updated
298
+            if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
299
+                $result .= substr($this->cache, $blockPosition, $remainingLength);
300
+                $this->position += $remainingLength;
301
+                $count = 0;
302
+                // otherwise remainder of current block is fetched, the block is flushed and the position updated
303
+            } else {
304
+                $result .= substr($this->cache, $blockPosition);
305
+                $this->flush();
306
+                $this->position += ($this->unencryptedBlockSize - $blockPosition);
307
+                $count -= ($this->unencryptedBlockSize - $blockPosition);
308
+            }
309
+        }
310
+        return $result;
311
+
312
+    }
313
+
314
+    public function stream_write($data) {
315
+
316
+        $length = 0;
317
+        // loop over $data to fit it in 6126 sized unencrypted blocks
318
+        while (isset($data[0])) {
319
+            $remainingLength = strlen($data);
320
+
321
+            // set the cache to the current 6126 block
322
+            $this->readCache();
323
+
324
+            // for seekable streams the pointer is moved back to the beginning of the encrypted block
325
+            // flush will start writing there when the position moves to another block
326
+            $positionInFile = (int)floor($this->position / $this->unencryptedBlockSize) *
327
+                $this->util->getBlockSize() + $this->headerSize;
328
+            $resultFseek = $this->parentStreamSeek($positionInFile);
329
+
330
+            // only allow writes on seekable streams, or at the end of the encrypted stream
331
+            if (!($this->readOnly) && ($resultFseek || $positionInFile === $this->size)) {
332
+
333
+                // switch the writeFlag so flush() will write the block
334
+                $this->writeFlag = true;
335
+
336
+                // determine the relative position in the current block
337
+                $blockPosition = ($this->position % $this->unencryptedBlockSize);
338
+                // check if $data fits in current block
339
+                // if so, overwrite existing data (if any)
340
+                // update position and liberate $data
341
+                if ($remainingLength < ($this->unencryptedBlockSize - $blockPosition)) {
342
+                    $this->cache = substr($this->cache, 0, $blockPosition)
343
+                        . $data . substr($this->cache, $blockPosition + $remainingLength);
344
+                    $this->position += $remainingLength;
345
+                    $length += $remainingLength;
346
+                    $data = '';
347
+                    // if $data doesn't fit the current block, the fill the current block and reiterate
348
+                    // after the block is filled, it is flushed and $data is updatedxxx
349
+                } else {
350
+                    $this->cache = substr($this->cache, 0, $blockPosition) .
351
+                        substr($data, 0, $this->unencryptedBlockSize - $blockPosition);
352
+                    $this->flush();
353
+                    $this->position += ($this->unencryptedBlockSize - $blockPosition);
354
+                    $length += ($this->unencryptedBlockSize - $blockPosition);
355
+                    $data = substr($data, $this->unencryptedBlockSize - $blockPosition);
356
+                }
357
+            } else {
358
+                $data = '';
359
+            }
360
+            $this->unencryptedSize = max($this->unencryptedSize, $this->position);
361
+        }
362
+        return $length;
363
+    }
364
+
365
+    public function stream_tell() {
366
+        return $this->position;
367
+    }
368
+
369
+    public function stream_seek($offset, $whence = SEEK_SET) {
370
+
371
+        $return = false;
372
+
373
+        switch ($whence) {
374
+            case SEEK_SET:
375
+                $newPosition = $offset;
376
+                break;
377
+            case SEEK_CUR:
378
+                $newPosition = $this->position + $offset;
379
+                break;
380
+            case SEEK_END:
381
+                $newPosition = $this->unencryptedSize + $offset;
382
+                break;
383
+            default:
384
+                return $return;
385
+        }
386
+
387
+        if ($newPosition > $this->unencryptedSize || $newPosition < 0) {
388
+            return $return;
389
+        }
390
+
391
+        $newFilePosition = floor($newPosition / $this->unencryptedBlockSize)
392
+            * $this->util->getBlockSize() + $this->headerSize;
393
+
394
+        $oldFilePosition = parent::stream_tell();
395
+        if ($this->parentStreamSeek($newFilePosition)) {
396
+            $this->parentStreamSeek($oldFilePosition);
397
+            $this->flush();
398
+            $this->parentStreamSeek($newFilePosition);
399
+            $this->position = $newPosition;
400
+            $return = true;
401
+        }
402
+        return $return;
403
+
404
+    }
405
+
406
+    public function stream_close() {
407
+        $this->flush('end');
408
+        $position = (int)floor($this->position/$this->unencryptedBlockSize);
409
+        $remainingData = $this->encryptionModule->end($this->fullPath, $position . 'end');
410
+        if ($this->readOnly === false) {
411
+            if(!empty($remainingData)) {
412
+                parent::stream_write($remainingData);
413
+            }
414
+            $this->encryptionStorage->updateUnencryptedSize($this->fullPath, $this->unencryptedSize);
415
+        }
416
+        return parent::stream_close();
417
+    }
418
+
419
+    /**
420
+     * write block to file
421
+     * @param string $positionPrefix
422
+     */
423
+    protected function flush($positionPrefix = '') {
424
+        // write to disk only when writeFlag was set to 1
425
+        if ($this->writeFlag) {
426
+            // Disable the file proxies so that encryption is not
427
+            // automatically attempted when the file is written to disk -
428
+            // we are handling that separately here and we don't want to
429
+            // get into an infinite loop
430
+            $position = (int)floor($this->position/$this->unencryptedBlockSize);
431
+            $encrypted = $this->encryptionModule->encrypt($this->cache, $position . $positionPrefix);
432
+            $bytesWritten = parent::stream_write($encrypted);
433
+            $this->writeFlag = false;
434
+            // Check whether the write concerns the last block
435
+            // If so then update the encrypted filesize
436
+            // Note that the unencrypted pointer and filesize are NOT yet updated when flush() is called
437
+            // We recalculate the encrypted filesize as we do not know the context of calling flush()
438
+            $completeBlocksInFile=(int)floor($this->unencryptedSize/$this->unencryptedBlockSize);
439
+            if ($completeBlocksInFile === (int)floor($this->position/$this->unencryptedBlockSize)) {
440
+                $this->size = $this->util->getBlockSize() * $completeBlocksInFile;
441
+                $this->size += $bytesWritten;
442
+                $this->size += $this->headerSize;
443
+            }
444
+        }
445
+        // always empty the cache (otherwise readCache() will not fill it with the new block)
446
+        $this->cache = '';
447
+    }
448
+
449
+    /**
450
+     * read block to file
451
+     */
452
+    protected function readCache() {
453
+        // cache should always be empty string when this function is called
454
+        // don't try to fill the cache when trying to write at the end of the unencrypted file when it coincides with new block
455
+        if ($this->cache === '' && !($this->position === $this->unencryptedSize && ($this->position % $this->unencryptedBlockSize) === 0)) {
456
+            // Get the data from the file handle
457
+            $data = parent::stream_read($this->util->getBlockSize());
458
+            $position = (int)floor($this->position/$this->unencryptedBlockSize);
459
+            $numberOfChunks = (int)($this->unencryptedSize / $this->unencryptedBlockSize);
460
+            if($numberOfChunks === $position) {
461
+                $position .= 'end';
462
+            }
463
+            $this->cache = $this->encryptionModule->decrypt($data, $position);
464
+        }
465
+    }
466
+
467
+    /**
468
+     * write header at beginning of encrypted file
469
+     *
470
+     * @return integer
471
+     * @throws EncryptionHeaderKeyExistsException if header key is already in use
472
+     */
473
+    protected function writeHeader() {
474
+        $header = $this->util->createHeader($this->newHeader, $this->encryptionModule);
475
+        return parent::stream_write($header);
476
+    }
477
+
478
+    /**
479
+     * read first block to skip the header
480
+     */
481
+    protected function skipHeader() {
482
+        parent::stream_read($this->headerSize);
483
+    }
484
+
485
+    /**
486
+     * call stream_seek() from parent class
487
+     *
488
+     * @param integer $position
489
+     * @return bool
490
+     */
491
+    protected function parentStreamSeek($position) {
492
+        return parent::stream_seek($position);
493
+    }
494
+
495
+    /**
496
+     * @param string $path
497
+     * @param array $options
498
+     * @return bool
499
+     */
500
+    public function dir_opendir($path, $options) {
501
+        return false;
502
+    }
503 503
 
504 504
 }
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.