Passed
Push — master ( d37f01...3cd258 )
by Roeland
22:34 queued 11:59
created
lib/public/Files/File.php 1 patch
Indentation   +62 added lines, -62 removed lines patch added patch discarded remove patch
@@ -42,72 +42,72 @@
 block discarded – undo
42 42
  * @since 6.0.0
43 43
  */
44 44
 interface File extends Node {
45
-	/**
46
-	 * Get the content of the file as string
47
-	 *
48
-	 * @return string
49
-	 * @throws NotPermittedException
50
-	 * @throws LockedException
51
-	 * @since 6.0.0
52
-	 */
53
-	public function getContent();
45
+    /**
46
+     * Get the content of the file as string
47
+     *
48
+     * @return string
49
+     * @throws NotPermittedException
50
+     * @throws LockedException
51
+     * @since 6.0.0
52
+     */
53
+    public function getContent();
54 54
 
55
-	/**
56
-	 * Write to the file from string data
57
-	 *
58
-	 * @param string|resource $data
59
-	 * @throws NotPermittedException
60
-	 * @throws GenericFileException
61
-	 * @throws LockedException
62
-	 * @since 6.0.0
63
-	 */
64
-	public function putContent($data);
55
+    /**
56
+     * Write to the file from string data
57
+     *
58
+     * @param string|resource $data
59
+     * @throws NotPermittedException
60
+     * @throws GenericFileException
61
+     * @throws LockedException
62
+     * @since 6.0.0
63
+     */
64
+    public function putContent($data);
65 65
 
66
-	/**
67
-	 * Get the mimetype of the file
68
-	 *
69
-	 * @return string
70
-	 * @since 6.0.0
71
-	 */
72
-	public function getMimeType();
66
+    /**
67
+     * Get the mimetype of the file
68
+     *
69
+     * @return string
70
+     * @since 6.0.0
71
+     */
72
+    public function getMimeType();
73 73
 
74
-	/**
75
-	 * Open the file as stream, resulting resource can be operated as stream like the result from php's own fopen
76
-	 *
77
-	 * @param string $mode
78
-	 * @return resource
79
-	 * @throws NotPermittedException
80
-	 * @throws LockedException
81
-	 * @since 6.0.0
82
-	 */
83
-	public function fopen($mode);
74
+    /**
75
+     * Open the file as stream, resulting resource can be operated as stream like the result from php's own fopen
76
+     *
77
+     * @param string $mode
78
+     * @return resource
79
+     * @throws NotPermittedException
80
+     * @throws LockedException
81
+     * @since 6.0.0
82
+     */
83
+    public function fopen($mode);
84 84
 
85
-	/**
86
-	 * Compute the hash of the file
87
-	 * Type of hash is set with $type and can be anything supported by php's hash_file
88
-	 *
89
-	 * @param string $type
90
-	 * @param bool $raw
91
-	 * @return string
92
-	 * @since 6.0.0
93
-	 */
94
-	public function hash($type, $raw = false);
85
+    /**
86
+     * Compute the hash of the file
87
+     * Type of hash is set with $type and can be anything supported by php's hash_file
88
+     *
89
+     * @param string $type
90
+     * @param bool $raw
91
+     * @return string
92
+     * @since 6.0.0
93
+     */
94
+    public function hash($type, $raw = false);
95 95
 
96
-	/**
97
-	 * Get the stored checksum for this file
98
-	 *
99
-	 * @return string
100
-	 * @since 9.0.0
101
-	 * @throws InvalidPathException
102
-	 * @throws NotFoundException
103
-	 */
104
-	public function getChecksum();
96
+    /**
97
+     * Get the stored checksum for this file
98
+     *
99
+     * @return string
100
+     * @since 9.0.0
101
+     * @throws InvalidPathException
102
+     * @throws NotFoundException
103
+     */
104
+    public function getChecksum();
105 105
 
106
-	/**
107
-	 * Get the extension of this file
108
-	 *
109
-	 * @return string
110
-	 * @since 15.0.0
111
-	 */
112
-	public function getExtension(): string;
106
+    /**
107
+     * Get the extension of this file
108
+     *
109
+     * @return string
110
+     * @since 15.0.0
111
+     */
112
+    public function getExtension(): string;
113 113
 }
Please login to merge, or discard this patch.
lib/public/Files/Node.php 1 patch
Indentation   +225 added lines, -225 removed lines patch added patch discarded remove patch
@@ -43,251 +43,251 @@
 block discarded – undo
43 43
  * @since 6.0.0 - extends FileInfo was added in 8.0.0
44 44
  */
45 45
 interface Node extends FileInfo {
46
-	/**
47
-	 * Move the file or folder to a new location
48
-	 *
49
-	 * @param string $targetPath the absolute target path
50
-	 * @return Node
51
-	 * @throws NotFoundException
52
-	 * @throws NotPermittedException if move not allowed or failed
53
-	 * @throws LockedException
54
-	 * @throws InvalidPathException
55
-	 * @since 6.0.0
56
-	 */
57
-	public function move($targetPath);
46
+    /**
47
+     * Move the file or folder to a new location
48
+     *
49
+     * @param string $targetPath the absolute target path
50
+     * @return Node
51
+     * @throws NotFoundException
52
+     * @throws NotPermittedException if move not allowed or failed
53
+     * @throws LockedException
54
+     * @throws InvalidPathException
55
+     * @since 6.0.0
56
+     */
57
+    public function move($targetPath);
58 58
 
59
-	/**
60
-	 * Delete the file or folder
61
-	 *
62
-	 * @return void
63
-	 * @throws NotPermittedException
64
-	 * @throws InvalidPathException
65
-	 * @throws NotFoundException
66
-	 * @since 6.0.0
67
-	 */
68
-	public function delete();
59
+    /**
60
+     * Delete the file or folder
61
+     *
62
+     * @return void
63
+     * @throws NotPermittedException
64
+     * @throws InvalidPathException
65
+     * @throws NotFoundException
66
+     * @since 6.0.0
67
+     */
68
+    public function delete();
69 69
 
70
-	/**
71
-	 * Cope the file or folder to a new location
72
-	 *
73
-	 * @param string $targetPath the absolute target path
74
-	 * @return Node
75
-	 * @since 6.0.0
76
-	 */
77
-	public function copy($targetPath);
70
+    /**
71
+     * Cope the file or folder to a new location
72
+     *
73
+     * @param string $targetPath the absolute target path
74
+     * @return Node
75
+     * @since 6.0.0
76
+     */
77
+    public function copy($targetPath);
78 78
 
79
-	/**
80
-	 * Change the modified date of the file or folder
81
-	 * If $mtime is omitted the current time will be used
82
-	 *
83
-	 * @param int $mtime (optional) modified date as unix timestamp
84
-	 * @throws InvalidPathException
85
-	 * @throws NotFoundException
86
-	 * @throws NotPermittedException
87
-	 * @return void
88
-	 * @since 6.0.0
89
-	 */
90
-	public function touch($mtime = null);
79
+    /**
80
+     * Change the modified date of the file or folder
81
+     * If $mtime is omitted the current time will be used
82
+     *
83
+     * @param int $mtime (optional) modified date as unix timestamp
84
+     * @throws InvalidPathException
85
+     * @throws NotFoundException
86
+     * @throws NotPermittedException
87
+     * @return void
88
+     * @since 6.0.0
89
+     */
90
+    public function touch($mtime = null);
91 91
 
92
-	/**
93
-	 * Get the storage backend the file or folder is stored on
94
-	 *
95
-	 * @return Storage
96
-	 * @throws NotFoundException
97
-	 * @since 6.0.0
98
-	 */
99
-	public function getStorage();
92
+    /**
93
+     * Get the storage backend the file or folder is stored on
94
+     *
95
+     * @return Storage
96
+     * @throws NotFoundException
97
+     * @since 6.0.0
98
+     */
99
+    public function getStorage();
100 100
 
101
-	/**
102
-	 * Get the full path of the file or folder
103
-	 *
104
-	 * @return string
105
-	 * @since 6.0.0
106
-	 */
107
-	public function getPath();
101
+    /**
102
+     * Get the full path of the file or folder
103
+     *
104
+     * @return string
105
+     * @since 6.0.0
106
+     */
107
+    public function getPath();
108 108
 
109
-	/**
110
-	 * Get the path of the file or folder relative to the mountpoint of it's storage
111
-	 *
112
-	 * @return string
113
-	 * @since 6.0.0
114
-	 */
115
-	public function getInternalPath();
109
+    /**
110
+     * Get the path of the file or folder relative to the mountpoint of it's storage
111
+     *
112
+     * @return string
113
+     * @since 6.0.0
114
+     */
115
+    public function getInternalPath();
116 116
 
117
-	/**
118
-	 * Get the internal file id for the file or folder
119
-	 *
120
-	 * @return int
121
-	 * @throws InvalidPathException
122
-	 * @throws NotFoundException
123
-	 * @since 6.0.0
124
-	 */
125
-	public function getId();
117
+    /**
118
+     * Get the internal file id for the file or folder
119
+     *
120
+     * @return int
121
+     * @throws InvalidPathException
122
+     * @throws NotFoundException
123
+     * @since 6.0.0
124
+     */
125
+    public function getId();
126 126
 
127
-	/**
128
-	 * Get metadata of the file or folder
129
-	 * The returned array contains the following values:
130
-	 *  - mtime
131
-	 *  - size
132
-	 *
133
-	 * @return array
134
-	 * @since 6.0.0
135
-	 */
136
-	public function stat();
127
+    /**
128
+     * Get metadata of the file or folder
129
+     * The returned array contains the following values:
130
+     *  - mtime
131
+     *  - size
132
+     *
133
+     * @return array
134
+     * @since 6.0.0
135
+     */
136
+    public function stat();
137 137
 
138
-	/**
139
-	 * Get the modified date of the file or folder as unix timestamp
140
-	 *
141
-	 * @return int
142
-	 * @throws InvalidPathException
143
-	 * @throws NotFoundException
144
-	 * @since 6.0.0
145
-	 */
146
-	public function getMTime();
138
+    /**
139
+     * Get the modified date of the file or folder as unix timestamp
140
+     *
141
+     * @return int
142
+     * @throws InvalidPathException
143
+     * @throws NotFoundException
144
+     * @since 6.0.0
145
+     */
146
+    public function getMTime();
147 147
 
148
-	/**
149
-	 * Get the size of the file or folder in bytes
150
-	 *
151
-	 * @param bool $includeMounts
152
-	 * @return int
153
-	 * @throws InvalidPathException
154
-	 * @throws NotFoundException
155
-	 * @since 6.0.0
156
-	 */
157
-	public function getSize($includeMounts = true);
148
+    /**
149
+     * Get the size of the file or folder in bytes
150
+     *
151
+     * @param bool $includeMounts
152
+     * @return int
153
+     * @throws InvalidPathException
154
+     * @throws NotFoundException
155
+     * @since 6.0.0
156
+     */
157
+    public function getSize($includeMounts = true);
158 158
 
159
-	/**
160
-	 * Get the Etag of the file or folder
161
-	 * The Etag is an string id used to detect changes to a file or folder,
162
-	 * every time the file or folder is changed the Etag will change to
163
-	 *
164
-	 * @return string
165
-	 * @throws InvalidPathException
166
-	 * @throws NotFoundException
167
-	 * @since 6.0.0
168
-	 */
169
-	public function getEtag();
159
+    /**
160
+     * Get the Etag of the file or folder
161
+     * The Etag is an string id used to detect changes to a file or folder,
162
+     * every time the file or folder is changed the Etag will change to
163
+     *
164
+     * @return string
165
+     * @throws InvalidPathException
166
+     * @throws NotFoundException
167
+     * @since 6.0.0
168
+     */
169
+    public function getEtag();
170 170
 
171 171
 
172
-	/**
173
-	 * Get the permissions of the file or folder as a combination of one or more of the following constants:
174
-	 *  - \OCP\Constants::PERMISSION_READ
175
-	 *  - \OCP\Constants::PERMISSION_UPDATE
176
-	 *  - \OCP\Constants::PERMISSION_CREATE
177
-	 *  - \OCP\Constants::PERMISSION_DELETE
178
-	 *  - \OCP\Constants::PERMISSION_SHARE
179
-	 *
180
-	 * @return int
181
-	 * @throws InvalidPathException
182
-	 * @throws NotFoundException
183
-	 * @since 6.0.0 - namespace of constants has changed in 8.0.0
184
-	 */
185
-	public function getPermissions();
172
+    /**
173
+     * Get the permissions of the file or folder as a combination of one or more of the following constants:
174
+     *  - \OCP\Constants::PERMISSION_READ
175
+     *  - \OCP\Constants::PERMISSION_UPDATE
176
+     *  - \OCP\Constants::PERMISSION_CREATE
177
+     *  - \OCP\Constants::PERMISSION_DELETE
178
+     *  - \OCP\Constants::PERMISSION_SHARE
179
+     *
180
+     * @return int
181
+     * @throws InvalidPathException
182
+     * @throws NotFoundException
183
+     * @since 6.0.0 - namespace of constants has changed in 8.0.0
184
+     */
185
+    public function getPermissions();
186 186
 
187
-	/**
188
-	 * Check if the file or folder is readable
189
-	 *
190
-	 * @return bool
191
-	 * @throws InvalidPathException
192
-	 * @throws NotFoundException
193
-	 * @since 6.0.0
194
-	 */
195
-	public function isReadable();
187
+    /**
188
+     * Check if the file or folder is readable
189
+     *
190
+     * @return bool
191
+     * @throws InvalidPathException
192
+     * @throws NotFoundException
193
+     * @since 6.0.0
194
+     */
195
+    public function isReadable();
196 196
 
197
-	/**
198
-	 * Check if the file or folder is writable
199
-	 *
200
-	 * @return bool
201
-	 * @throws InvalidPathException
202
-	 * @throws NotFoundException
203
-	 * @since 6.0.0
204
-	 */
205
-	public function isUpdateable();
197
+    /**
198
+     * Check if the file or folder is writable
199
+     *
200
+     * @return bool
201
+     * @throws InvalidPathException
202
+     * @throws NotFoundException
203
+     * @since 6.0.0
204
+     */
205
+    public function isUpdateable();
206 206
 
207
-	/**
208
-	 * Check if the file or folder is deletable
209
-	 *
210
-	 * @return bool
211
-	 * @throws InvalidPathException
212
-	 * @throws NotFoundException
213
-	 * @since 6.0.0
214
-	 */
215
-	public function isDeletable();
207
+    /**
208
+     * Check if the file or folder is deletable
209
+     *
210
+     * @return bool
211
+     * @throws InvalidPathException
212
+     * @throws NotFoundException
213
+     * @since 6.0.0
214
+     */
215
+    public function isDeletable();
216 216
 
217
-	/**
218
-	 * Check if the file or folder is shareable
219
-	 *
220
-	 * @return bool
221
-	 * @throws InvalidPathException
222
-	 * @throws NotFoundException
223
-	 * @since 6.0.0
224
-	 */
225
-	public function isShareable();
217
+    /**
218
+     * Check if the file or folder is shareable
219
+     *
220
+     * @return bool
221
+     * @throws InvalidPathException
222
+     * @throws NotFoundException
223
+     * @since 6.0.0
224
+     */
225
+    public function isShareable();
226 226
 
227
-	/**
228
-	 * Get the parent folder of the file or folder
229
-	 *
230
-	 * @return Folder
231
-	 * @since 6.0.0
232
-	 */
233
-	public function getParent();
227
+    /**
228
+     * Get the parent folder of the file or folder
229
+     *
230
+     * @return Folder
231
+     * @since 6.0.0
232
+     */
233
+    public function getParent();
234 234
 
235
-	/**
236
-	 * Get the filename of the file or folder
237
-	 *
238
-	 * @return string
239
-	 * @since 6.0.0
240
-	 */
241
-	public function getName();
235
+    /**
236
+     * Get the filename of the file or folder
237
+     *
238
+     * @return string
239
+     * @since 6.0.0
240
+     */
241
+    public function getName();
242 242
 
243
-	/**
244
-	 * Acquire a lock on this file or folder.
245
-	 *
246
-	 * A shared (read) lock will prevent any exclusive (write) locks from being created but any number of shared locks
247
-	 * can be active at the same time.
248
-	 * An exclusive lock will prevent any other lock from being created (both shared and exclusive).
249
-	 *
250
-	 * A locked exception will be thrown if any conflicting lock already exists
251
-	 *
252
-	 * Note that this uses mandatory locking, if you acquire an exclusive lock on a file it will block *all*
253
-	 * other operations for that file, even within the same php process.
254
-	 *
255
-	 * Acquiring any lock on a file will also create a shared lock on all parent folders of that file.
256
-	 *
257
-	 * Note that in most cases you won't need to manually manage the locks for any files you're working with,
258
-	 * any filesystem operation will automatically acquire the relevant locks for that operation.
259
-	 *
260
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
261
-	 * @throws LockedException
262
-	 * @since 9.1.0
263
-	 */
264
-	public function lock($type);
243
+    /**
244
+     * Acquire a lock on this file or folder.
245
+     *
246
+     * A shared (read) lock will prevent any exclusive (write) locks from being created but any number of shared locks
247
+     * can be active at the same time.
248
+     * An exclusive lock will prevent any other lock from being created (both shared and exclusive).
249
+     *
250
+     * A locked exception will be thrown if any conflicting lock already exists
251
+     *
252
+     * Note that this uses mandatory locking, if you acquire an exclusive lock on a file it will block *all*
253
+     * other operations for that file, even within the same php process.
254
+     *
255
+     * Acquiring any lock on a file will also create a shared lock on all parent folders of that file.
256
+     *
257
+     * Note that in most cases you won't need to manually manage the locks for any files you're working with,
258
+     * any filesystem operation will automatically acquire the relevant locks for that operation.
259
+     *
260
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
261
+     * @throws LockedException
262
+     * @since 9.1.0
263
+     */
264
+    public function lock($type);
265 265
 
266
-	/**
267
-	 * Check the type of an existing lock.
268
-	 *
269
-	 * A shared lock can be changed to an exclusive lock is there is exactly one shared lock on the file,
270
-	 * an exclusive lock can always be changed to a shared lock since there can only be one exclusive lock int he first place.
271
-	 *
272
-	 * A locked exception will be thrown when these preconditions are not met.
273
-	 * Note that this is also the case if no existing lock exists for the file.
274
-	 *
275
-	 * @param int $targetType \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
276
-	 * @throws LockedException
277
-	 * @since 9.1.0
278
-	 */
279
-	public function changeLock($targetType);
266
+    /**
267
+     * Check the type of an existing lock.
268
+     *
269
+     * A shared lock can be changed to an exclusive lock is there is exactly one shared lock on the file,
270
+     * an exclusive lock can always be changed to a shared lock since there can only be one exclusive lock int he first place.
271
+     *
272
+     * A locked exception will be thrown when these preconditions are not met.
273
+     * Note that this is also the case if no existing lock exists for the file.
274
+     *
275
+     * @param int $targetType \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
276
+     * @throws LockedException
277
+     * @since 9.1.0
278
+     */
279
+    public function changeLock($targetType);
280 280
 
281
-	/**
282
-	 * Release an existing lock.
283
-	 *
284
-	 * This will also free up the shared locks on any parent folder that were automatically acquired when locking the file.
285
-	 *
286
-	 * Note that this method will not give any sort of error when trying to free a lock that doesn't exist.
287
-	 *
288
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
289
-	 * @throws LockedException
290
-	 * @since 9.1.0
291
-	 */
292
-	public function unlock($type);
281
+    /**
282
+     * Release an existing lock.
283
+     *
284
+     * This will also free up the shared locks on any parent folder that were automatically acquired when locking the file.
285
+     *
286
+     * Note that this method will not give any sort of error when trying to free a lock that doesn't exist.
287
+     *
288
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
289
+     * @throws LockedException
290
+     * @since 9.1.0
291
+     */
292
+    public function unlock($type);
293 293
 }
Please login to merge, or discard this patch.
lib/private/Files/View.php 1 patch
Indentation   +2101 added lines, -2101 removed lines patch added patch discarded remove patch
@@ -82,2105 +82,2105 @@
 block discarded – undo
82 82
  * \OC\Files\Storage\Storage object
83 83
  */
84 84
 class View {
85
-	/** @var string */
86
-	private $fakeRoot = '';
87
-
88
-	/**
89
-	 * @var \OCP\Lock\ILockingProvider
90
-	 */
91
-	protected $lockingProvider;
92
-
93
-	private $lockingEnabled;
94
-
95
-	private $updaterEnabled = true;
96
-
97
-	/** @var \OC\User\Manager */
98
-	private $userManager;
99
-
100
-	/** @var \OCP\ILogger */
101
-	private $logger;
102
-
103
-	/**
104
-	 * @param string $root
105
-	 * @throws \Exception If $root contains an invalid path
106
-	 */
107
-	public function __construct($root = '') {
108
-		if (is_null($root)) {
109
-			throw new \InvalidArgumentException('Root can\'t be null');
110
-		}
111
-		if (!Filesystem::isValidPath($root)) {
112
-			throw new \Exception();
113
-		}
114
-
115
-		$this->fakeRoot = $root;
116
-		$this->lockingProvider = \OC::$server->getLockingProvider();
117
-		$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
118
-		$this->userManager = \OC::$server->getUserManager();
119
-		$this->logger = \OC::$server->getLogger();
120
-	}
121
-
122
-	public function getAbsolutePath($path = '/') {
123
-		if ($path === null) {
124
-			return null;
125
-		}
126
-		$this->assertPathLength($path);
127
-		if ($path === '') {
128
-			$path = '/';
129
-		}
130
-		if ($path[0] !== '/') {
131
-			$path = '/' . $path;
132
-		}
133
-		return $this->fakeRoot . $path;
134
-	}
135
-
136
-	/**
137
-	 * change the root to a fake root
138
-	 *
139
-	 * @param string $fakeRoot
140
-	 * @return boolean|null
141
-	 */
142
-	public function chroot($fakeRoot) {
143
-		if (!$fakeRoot == '') {
144
-			if ($fakeRoot[0] !== '/') {
145
-				$fakeRoot = '/' . $fakeRoot;
146
-			}
147
-		}
148
-		$this->fakeRoot = $fakeRoot;
149
-	}
150
-
151
-	/**
152
-	 * get the fake root
153
-	 *
154
-	 * @return string
155
-	 */
156
-	public function getRoot() {
157
-		return $this->fakeRoot;
158
-	}
159
-
160
-	/**
161
-	 * get path relative to the root of the view
162
-	 *
163
-	 * @param string $path
164
-	 * @return string
165
-	 */
166
-	public function getRelativePath($path) {
167
-		$this->assertPathLength($path);
168
-		if ($this->fakeRoot == '') {
169
-			return $path;
170
-		}
171
-
172
-		if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) {
173
-			return '/';
174
-		}
175
-
176
-		// missing slashes can cause wrong matches!
177
-		$root = rtrim($this->fakeRoot, '/') . '/';
178
-
179
-		if (strpos($path, $root) !== 0) {
180
-			return null;
181
-		} else {
182
-			$path = substr($path, strlen($this->fakeRoot));
183
-			if (strlen($path) === 0) {
184
-				return '/';
185
-			} else {
186
-				return $path;
187
-			}
188
-		}
189
-	}
190
-
191
-	/**
192
-	 * get the mountpoint of the storage object for a path
193
-	 * ( note: because a storage is not always mounted inside the fakeroot, the
194
-	 * returned mountpoint is relative to the absolute root of the filesystem
195
-	 * and does not take the chroot into account )
196
-	 *
197
-	 * @param string $path
198
-	 * @return string
199
-	 */
200
-	public function getMountPoint($path) {
201
-		return Filesystem::getMountPoint($this->getAbsolutePath($path));
202
-	}
203
-
204
-	/**
205
-	 * get the mountpoint of the storage object for a path
206
-	 * ( note: because a storage is not always mounted inside the fakeroot, the
207
-	 * returned mountpoint is relative to the absolute root of the filesystem
208
-	 * and does not take the chroot into account )
209
-	 *
210
-	 * @param string $path
211
-	 * @return \OCP\Files\Mount\IMountPoint
212
-	 */
213
-	public function getMount($path) {
214
-		return Filesystem::getMountManager()->find($this->getAbsolutePath($path));
215
-	}
216
-
217
-	/**
218
-	 * resolve a path to a storage and internal path
219
-	 *
220
-	 * @param string $path
221
-	 * @return array an array consisting of the storage and the internal path
222
-	 */
223
-	public function resolvePath($path) {
224
-		$a = $this->getAbsolutePath($path);
225
-		$p = Filesystem::normalizePath($a);
226
-		return Filesystem::resolvePath($p);
227
-	}
228
-
229
-	/**
230
-	 * return the path to a local version of the file
231
-	 * we need this because we can't know if a file is stored local or not from
232
-	 * outside the filestorage and for some purposes a local file is needed
233
-	 *
234
-	 * @param string $path
235
-	 * @return string
236
-	 */
237
-	public function getLocalFile($path) {
238
-		$parent = substr($path, 0, strrpos($path, '/'));
239
-		$path = $this->getAbsolutePath($path);
240
-		list($storage, $internalPath) = Filesystem::resolvePath($path);
241
-		if (Filesystem::isValidPath($parent) and $storage) {
242
-			return $storage->getLocalFile($internalPath);
243
-		} else {
244
-			return null;
245
-		}
246
-	}
247
-
248
-	/**
249
-	 * @param string $path
250
-	 * @return string
251
-	 */
252
-	public function getLocalFolder($path) {
253
-		$parent = substr($path, 0, strrpos($path, '/'));
254
-		$path = $this->getAbsolutePath($path);
255
-		list($storage, $internalPath) = Filesystem::resolvePath($path);
256
-		if (Filesystem::isValidPath($parent) and $storage) {
257
-			return $storage->getLocalFolder($internalPath);
258
-		} else {
259
-			return null;
260
-		}
261
-	}
262
-
263
-	/**
264
-	 * the following functions operate with arguments and return values identical
265
-	 * to those of their PHP built-in equivalents. Mostly they are merely wrappers
266
-	 * for \OC\Files\Storage\Storage via basicOperation().
267
-	 */
268
-	public function mkdir($path) {
269
-		return $this->basicOperation('mkdir', $path, array('create', 'write'));
270
-	}
271
-
272
-	/**
273
-	 * remove mount point
274
-	 *
275
-	 * @param \OC\Files\Mount\MoveableMount $mount
276
-	 * @param string $path relative to data/
277
-	 * @return boolean
278
-	 */
279
-	protected function removeMount($mount, $path) {
280
-		if ($mount instanceof MoveableMount) {
281
-			// cut of /user/files to get the relative path to data/user/files
282
-			$pathParts = explode('/', $path, 4);
283
-			$relPath = '/' . $pathParts[3];
284
-			$this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true);
285
-			\OC_Hook::emit(
286
-				Filesystem::CLASSNAME, "umount",
287
-				array(Filesystem::signal_param_path => $relPath)
288
-			);
289
-			$this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true);
290
-			$result = $mount->removeMount();
291
-			$this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true);
292
-			if ($result) {
293
-				\OC_Hook::emit(
294
-					Filesystem::CLASSNAME, "post_umount",
295
-					array(Filesystem::signal_param_path => $relPath)
296
-				);
297
-			}
298
-			$this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true);
299
-			return $result;
300
-		} else {
301
-			// do not allow deleting the storage's root / the mount point
302
-			// because for some storages it might delete the whole contents
303
-			// but isn't supposed to work that way
304
-			return false;
305
-		}
306
-	}
307
-
308
-	public function disableCacheUpdate() {
309
-		$this->updaterEnabled = false;
310
-	}
311
-
312
-	public function enableCacheUpdate() {
313
-		$this->updaterEnabled = true;
314
-	}
315
-
316
-	protected function writeUpdate(Storage $storage, $internalPath, $time = null) {
317
-		if ($this->updaterEnabled) {
318
-			if (is_null($time)) {
319
-				$time = time();
320
-			}
321
-			$storage->getUpdater()->update($internalPath, $time);
322
-		}
323
-	}
324
-
325
-	protected function removeUpdate(Storage $storage, $internalPath) {
326
-		if ($this->updaterEnabled) {
327
-			$storage->getUpdater()->remove($internalPath);
328
-		}
329
-	}
330
-
331
-	protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) {
332
-		if ($this->updaterEnabled) {
333
-			$targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
334
-		}
335
-	}
336
-
337
-	/**
338
-	 * @param string $path
339
-	 * @return bool|mixed
340
-	 */
341
-	public function rmdir($path) {
342
-		$absolutePath = $this->getAbsolutePath($path);
343
-		$mount = Filesystem::getMountManager()->find($absolutePath);
344
-		if ($mount->getInternalPath($absolutePath) === '') {
345
-			return $this->removeMount($mount, $absolutePath);
346
-		}
347
-		if ($this->is_dir($path)) {
348
-			$result = $this->basicOperation('rmdir', $path, array('delete'));
349
-		} else {
350
-			$result = false;
351
-		}
352
-
353
-		if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
354
-			$storage = $mount->getStorage();
355
-			$internalPath = $mount->getInternalPath($absolutePath);
356
-			$storage->getUpdater()->remove($internalPath);
357
-		}
358
-		return $result;
359
-	}
360
-
361
-	/**
362
-	 * @param string $path
363
-	 * @return resource
364
-	 */
365
-	public function opendir($path) {
366
-		return $this->basicOperation('opendir', $path, array('read'));
367
-	}
368
-
369
-	/**
370
-	 * @param string $path
371
-	 * @return bool|mixed
372
-	 */
373
-	public function is_dir($path) {
374
-		if ($path == '/') {
375
-			return true;
376
-		}
377
-		return $this->basicOperation('is_dir', $path);
378
-	}
379
-
380
-	/**
381
-	 * @param string $path
382
-	 * @return bool|mixed
383
-	 */
384
-	public function is_file($path) {
385
-		if ($path == '/') {
386
-			return false;
387
-		}
388
-		return $this->basicOperation('is_file', $path);
389
-	}
390
-
391
-	/**
392
-	 * @param string $path
393
-	 * @return mixed
394
-	 */
395
-	public function stat($path) {
396
-		return $this->basicOperation('stat', $path);
397
-	}
398
-
399
-	/**
400
-	 * @param string $path
401
-	 * @return mixed
402
-	 */
403
-	public function filetype($path) {
404
-		return $this->basicOperation('filetype', $path);
405
-	}
406
-
407
-	/**
408
-	 * @param string $path
409
-	 * @return mixed
410
-	 */
411
-	public function filesize($path) {
412
-		return $this->basicOperation('filesize', $path);
413
-	}
414
-
415
-	/**
416
-	 * @param string $path
417
-	 * @return bool|mixed
418
-	 * @throws \OCP\Files\InvalidPathException
419
-	 */
420
-	public function readfile($path) {
421
-		$this->assertPathLength($path);
422
-		@ob_end_clean();
423
-		$handle = $this->fopen($path, 'rb');
424
-		if ($handle) {
425
-			$chunkSize = 8192; // 8 kB chunks
426
-			while (!feof($handle)) {
427
-				echo fread($handle, $chunkSize);
428
-				flush();
429
-			}
430
-			fclose($handle);
431
-			return $this->filesize($path);
432
-		}
433
-		return false;
434
-	}
435
-
436
-	/**
437
-	 * @param string $path
438
-	 * @param int $from
439
-	 * @param int $to
440
-	 * @return bool|mixed
441
-	 * @throws \OCP\Files\InvalidPathException
442
-	 * @throws \OCP\Files\UnseekableException
443
-	 */
444
-	public function readfilePart($path, $from, $to) {
445
-		$this->assertPathLength($path);
446
-		@ob_end_clean();
447
-		$handle = $this->fopen($path, 'rb');
448
-		if ($handle) {
449
-			$chunkSize = 8192; // 8 kB chunks
450
-			$startReading = true;
451
-
452
-			if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) {
453
-				// forward file handle via chunked fread because fseek seem to have failed
454
-
455
-				$end = $from + 1;
456
-				while (!feof($handle) && ftell($handle) < $end) {
457
-					$len = $from - ftell($handle);
458
-					if ($len > $chunkSize) {
459
-						$len = $chunkSize;
460
-					}
461
-					$result = fread($handle, $len);
462
-
463
-					if ($result === false) {
464
-						$startReading = false;
465
-						break;
466
-					}
467
-				}
468
-			}
469
-
470
-			if ($startReading) {
471
-				$end = $to + 1;
472
-				while (!feof($handle) && ftell($handle) < $end) {
473
-					$len = $end - ftell($handle);
474
-					if ($len > $chunkSize) {
475
-						$len = $chunkSize;
476
-					}
477
-					echo fread($handle, $len);
478
-					flush();
479
-				}
480
-				return ftell($handle) - $from;
481
-			}
482
-
483
-			throw new \OCP\Files\UnseekableException('fseek error');
484
-		}
485
-		return false;
486
-	}
487
-
488
-	/**
489
-	 * @param string $path
490
-	 * @return mixed
491
-	 */
492
-	public function isCreatable($path) {
493
-		return $this->basicOperation('isCreatable', $path);
494
-	}
495
-
496
-	/**
497
-	 * @param string $path
498
-	 * @return mixed
499
-	 */
500
-	public function isReadable($path) {
501
-		return $this->basicOperation('isReadable', $path);
502
-	}
503
-
504
-	/**
505
-	 * @param string $path
506
-	 * @return mixed
507
-	 */
508
-	public function isUpdatable($path) {
509
-		return $this->basicOperation('isUpdatable', $path);
510
-	}
511
-
512
-	/**
513
-	 * @param string $path
514
-	 * @return bool|mixed
515
-	 */
516
-	public function isDeletable($path) {
517
-		$absolutePath = $this->getAbsolutePath($path);
518
-		$mount = Filesystem::getMountManager()->find($absolutePath);
519
-		if ($mount->getInternalPath($absolutePath) === '') {
520
-			return $mount instanceof MoveableMount;
521
-		}
522
-		return $this->basicOperation('isDeletable', $path);
523
-	}
524
-
525
-	/**
526
-	 * @param string $path
527
-	 * @return mixed
528
-	 */
529
-	public function isSharable($path) {
530
-		return $this->basicOperation('isSharable', $path);
531
-	}
532
-
533
-	/**
534
-	 * @param string $path
535
-	 * @return bool|mixed
536
-	 */
537
-	public function file_exists($path) {
538
-		if ($path == '/') {
539
-			return true;
540
-		}
541
-		return $this->basicOperation('file_exists', $path);
542
-	}
543
-
544
-	/**
545
-	 * @param string $path
546
-	 * @return mixed
547
-	 */
548
-	public function filemtime($path) {
549
-		return $this->basicOperation('filemtime', $path);
550
-	}
551
-
552
-	/**
553
-	 * @param string $path
554
-	 * @param int|string $mtime
555
-	 * @return bool
556
-	 */
557
-	public function touch($path, $mtime = null) {
558
-		if (!is_null($mtime) and !is_numeric($mtime)) {
559
-			$mtime = strtotime($mtime);
560
-		}
561
-
562
-		$hooks = array('touch');
563
-
564
-		if (!$this->file_exists($path)) {
565
-			$hooks[] = 'create';
566
-			$hooks[] = 'write';
567
-		}
568
-		try {
569
-			$result = $this->basicOperation('touch', $path, $hooks, $mtime);
570
-		} catch (\Exception $e) {
571
-			$this->logger->logException($e, ['level' => ILogger::INFO, 'message' => 'Error while setting modified time']);
572
-			$result = false;
573
-		}
574
-		if (!$result) {
575
-			// If create file fails because of permissions on external storage like SMB folders,
576
-			// check file exists and return false if not.
577
-			if (!$this->file_exists($path)) {
578
-				return false;
579
-			}
580
-			if (is_null($mtime)) {
581
-				$mtime = time();
582
-			}
583
-			//if native touch fails, we emulate it by changing the mtime in the cache
584
-			$this->putFileInfo($path, array('mtime' => floor($mtime)));
585
-		}
586
-		return true;
587
-	}
588
-
589
-	/**
590
-	 * @param string $path
591
-	 * @return mixed
592
-	 * @throws LockedException
593
-	 */
594
-	public function file_get_contents($path) {
595
-		return $this->basicOperation('file_get_contents', $path, array('read'));
596
-	}
597
-
598
-	/**
599
-	 * @param bool $exists
600
-	 * @param string $path
601
-	 * @param bool $run
602
-	 */
603
-	protected function emit_file_hooks_pre($exists, $path, &$run) {
604
-		if (!$exists) {
605
-			\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array(
606
-				Filesystem::signal_param_path => $this->getHookPath($path),
607
-				Filesystem::signal_param_run => &$run,
608
-			));
609
-		} else {
610
-			\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array(
611
-				Filesystem::signal_param_path => $this->getHookPath($path),
612
-				Filesystem::signal_param_run => &$run,
613
-			));
614
-		}
615
-		\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array(
616
-			Filesystem::signal_param_path => $this->getHookPath($path),
617
-			Filesystem::signal_param_run => &$run,
618
-		));
619
-	}
620
-
621
-	/**
622
-	 * @param bool $exists
623
-	 * @param string $path
624
-	 */
625
-	protected function emit_file_hooks_post($exists, $path) {
626
-		if (!$exists) {
627
-			\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array(
628
-				Filesystem::signal_param_path => $this->getHookPath($path),
629
-			));
630
-		} else {
631
-			\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array(
632
-				Filesystem::signal_param_path => $this->getHookPath($path),
633
-			));
634
-		}
635
-		\OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array(
636
-			Filesystem::signal_param_path => $this->getHookPath($path),
637
-		));
638
-	}
639
-
640
-	/**
641
-	 * @param string $path
642
-	 * @param string|resource $data
643
-	 * @return bool|mixed
644
-	 * @throws LockedException
645
-	 */
646
-	public function file_put_contents($path, $data) {
647
-		if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
648
-			$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
649
-			if (Filesystem::isValidPath($path)
650
-				and !Filesystem::isFileBlacklisted($path)
651
-			) {
652
-				$path = $this->getRelativePath($absolutePath);
653
-
654
-				$this->lockFile($path, ILockingProvider::LOCK_SHARED);
655
-
656
-				$exists = $this->file_exists($path);
657
-				$run = true;
658
-				if ($this->shouldEmitHooks($path)) {
659
-					$this->emit_file_hooks_pre($exists, $path, $run);
660
-				}
661
-				if (!$run) {
662
-					$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
663
-					return false;
664
-				}
665
-
666
-				$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
667
-
668
-				/** @var \OC\Files\Storage\Storage $storage */
669
-				list($storage, $internalPath) = $this->resolvePath($path);
670
-				$target = $storage->fopen($internalPath, 'w');
671
-				if ($target) {
672
-					list (, $result) = \OC_Helper::streamCopy($data, $target);
673
-					fclose($target);
674
-					fclose($data);
675
-
676
-					$this->writeUpdate($storage, $internalPath);
677
-
678
-					$this->changeLock($path, ILockingProvider::LOCK_SHARED);
679
-
680
-					if ($this->shouldEmitHooks($path) && $result !== false) {
681
-						$this->emit_file_hooks_post($exists, $path);
682
-					}
683
-					$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
684
-					return $result;
685
-				} else {
686
-					$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
687
-					return false;
688
-				}
689
-			} else {
690
-				return false;
691
-			}
692
-		} else {
693
-			$hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write');
694
-			return $this->basicOperation('file_put_contents', $path, $hooks, $data);
695
-		}
696
-	}
697
-
698
-	/**
699
-	 * @param string $path
700
-	 * @return bool|mixed
701
-	 */
702
-	public function unlink($path) {
703
-		if ($path === '' || $path === '/') {
704
-			// do not allow deleting the root
705
-			return false;
706
-		}
707
-		$postFix = (substr($path, -1) === '/') ? '/' : '';
708
-		$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
709
-		$mount = Filesystem::getMountManager()->find($absolutePath . $postFix);
710
-		if ($mount and $mount->getInternalPath($absolutePath) === '') {
711
-			return $this->removeMount($mount, $absolutePath);
712
-		}
713
-		if ($this->is_dir($path)) {
714
-			$result = $this->basicOperation('rmdir', $path, ['delete']);
715
-		} else {
716
-			$result = $this->basicOperation('unlink', $path, ['delete']);
717
-		}
718
-		if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
719
-			$storage = $mount->getStorage();
720
-			$internalPath = $mount->getInternalPath($absolutePath);
721
-			$storage->getUpdater()->remove($internalPath);
722
-			return true;
723
-		} else {
724
-			return $result;
725
-		}
726
-	}
727
-
728
-	/**
729
-	 * @param string $directory
730
-	 * @return bool|mixed
731
-	 */
732
-	public function deleteAll($directory) {
733
-		return $this->rmdir($directory);
734
-	}
735
-
736
-	/**
737
-	 * Rename/move a file or folder from the source path to target path.
738
-	 *
739
-	 * @param string $path1 source path
740
-	 * @param string $path2 target path
741
-	 *
742
-	 * @return bool|mixed
743
-	 * @throws LockedException
744
-	 */
745
-	public function rename($path1, $path2) {
746
-		$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
747
-		$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
748
-		$result = false;
749
-		if (
750
-			Filesystem::isValidPath($path2)
751
-			and Filesystem::isValidPath($path1)
752
-			and !Filesystem::isFileBlacklisted($path2)
753
-		) {
754
-			$path1 = $this->getRelativePath($absolutePath1);
755
-			$path2 = $this->getRelativePath($absolutePath2);
756
-			$exists = $this->file_exists($path2);
757
-
758
-			if ($path1 == null or $path2 == null) {
759
-				return false;
760
-			}
761
-
762
-			$this->lockFile($path1, ILockingProvider::LOCK_SHARED, true);
763
-			try {
764
-				$this->lockFile($path2, ILockingProvider::LOCK_SHARED, true);
765
-
766
-				$run = true;
767
-				if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
768
-					// if it was a rename from a part file to a regular file it was a write and not a rename operation
769
-					$this->emit_file_hooks_pre($exists, $path2, $run);
770
-				} elseif ($this->shouldEmitHooks($path1)) {
771
-					\OC_Hook::emit(
772
-						Filesystem::CLASSNAME, Filesystem::signal_rename,
773
-						array(
774
-							Filesystem::signal_param_oldpath => $this->getHookPath($path1),
775
-							Filesystem::signal_param_newpath => $this->getHookPath($path2),
776
-							Filesystem::signal_param_run => &$run
777
-						)
778
-					);
779
-				}
780
-				if ($run) {
781
-					$this->verifyPath(dirname($path2), basename($path2));
782
-
783
-					$manager = Filesystem::getMountManager();
784
-					$mount1 = $this->getMount($path1);
785
-					$mount2 = $this->getMount($path2);
786
-					$storage1 = $mount1->getStorage();
787
-					$storage2 = $mount2->getStorage();
788
-					$internalPath1 = $mount1->getInternalPath($absolutePath1);
789
-					$internalPath2 = $mount2->getInternalPath($absolutePath2);
790
-
791
-					$this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true);
792
-					try {
793
-						$this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true);
794
-
795
-						if ($internalPath1 === '') {
796
-							if ($mount1 instanceof MoveableMount) {
797
-								$sourceParentMount = $this->getMount(dirname($path1));
798
-								if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) {
799
-									/**
800
-									 * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
801
-									 */
802
-									$sourceMountPoint = $mount1->getMountPoint();
803
-									$result = $mount1->moveMount($absolutePath2);
804
-									$manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
805
-								} else {
806
-									$result = false;
807
-								}
808
-							} else {
809
-								$result = false;
810
-							}
811
-							// moving a file/folder within the same mount point
812
-						} elseif ($storage1 === $storage2) {
813
-							if ($storage1) {
814
-								$result = $storage1->rename($internalPath1, $internalPath2);
815
-							} else {
816
-								$result = false;
817
-							}
818
-							// moving a file/folder between storages (from $storage1 to $storage2)
819
-						} else {
820
-							$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
821
-						}
822
-
823
-						if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
824
-							// if it was a rename from a part file to a regular file it was a write and not a rename operation
825
-							$this->writeUpdate($storage2, $internalPath2);
826
-						} else if ($result) {
827
-							if ($internalPath1 !== '') { // don't do a cache update for moved mounts
828
-								$this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2);
829
-							}
830
-						}
831
-					} catch(\Exception $e) {
832
-						throw $e;
833
-					} finally {
834
-						$this->changeLock($path1, ILockingProvider::LOCK_SHARED, true);
835
-						$this->changeLock($path2, ILockingProvider::LOCK_SHARED, true);
836
-					}
837
-
838
-					if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
839
-						if ($this->shouldEmitHooks()) {
840
-							$this->emit_file_hooks_post($exists, $path2);
841
-						}
842
-					} elseif ($result) {
843
-						if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) {
844
-							\OC_Hook::emit(
845
-								Filesystem::CLASSNAME,
846
-								Filesystem::signal_post_rename,
847
-								array(
848
-									Filesystem::signal_param_oldpath => $this->getHookPath($path1),
849
-									Filesystem::signal_param_newpath => $this->getHookPath($path2)
850
-								)
851
-							);
852
-						}
853
-					}
854
-				}
855
-			} catch(\Exception $e) {
856
-				throw $e;
857
-			} finally {
858
-				$this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true);
859
-				$this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true);
860
-			}
861
-		}
862
-		return $result;
863
-	}
864
-
865
-	/**
866
-	 * Copy a file/folder from the source path to target path
867
-	 *
868
-	 * @param string $path1 source path
869
-	 * @param string $path2 target path
870
-	 * @param bool $preserveMtime whether to preserve mtime on the copy
871
-	 *
872
-	 * @return bool|mixed
873
-	 */
874
-	public function copy($path1, $path2, $preserveMtime = false) {
875
-		$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
876
-		$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
877
-		$result = false;
878
-		if (
879
-			Filesystem::isValidPath($path2)
880
-			and Filesystem::isValidPath($path1)
881
-			and !Filesystem::isFileBlacklisted($path2)
882
-		) {
883
-			$path1 = $this->getRelativePath($absolutePath1);
884
-			$path2 = $this->getRelativePath($absolutePath2);
885
-
886
-			if ($path1 == null or $path2 == null) {
887
-				return false;
888
-			}
889
-			$run = true;
890
-
891
-			$this->lockFile($path2, ILockingProvider::LOCK_SHARED);
892
-			$this->lockFile($path1, ILockingProvider::LOCK_SHARED);
893
-			$lockTypePath1 = ILockingProvider::LOCK_SHARED;
894
-			$lockTypePath2 = ILockingProvider::LOCK_SHARED;
895
-
896
-			try {
897
-
898
-				$exists = $this->file_exists($path2);
899
-				if ($this->shouldEmitHooks()) {
900
-					\OC_Hook::emit(
901
-						Filesystem::CLASSNAME,
902
-						Filesystem::signal_copy,
903
-						array(
904
-							Filesystem::signal_param_oldpath => $this->getHookPath($path1),
905
-							Filesystem::signal_param_newpath => $this->getHookPath($path2),
906
-							Filesystem::signal_param_run => &$run
907
-						)
908
-					);
909
-					$this->emit_file_hooks_pre($exists, $path2, $run);
910
-				}
911
-				if ($run) {
912
-					$mount1 = $this->getMount($path1);
913
-					$mount2 = $this->getMount($path2);
914
-					$storage1 = $mount1->getStorage();
915
-					$internalPath1 = $mount1->getInternalPath($absolutePath1);
916
-					$storage2 = $mount2->getStorage();
917
-					$internalPath2 = $mount2->getInternalPath($absolutePath2);
918
-
919
-					$this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE);
920
-					$lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE;
921
-
922
-					if ($mount1->getMountPoint() == $mount2->getMountPoint()) {
923
-						if ($storage1) {
924
-							$result = $storage1->copy($internalPath1, $internalPath2);
925
-						} else {
926
-							$result = false;
927
-						}
928
-					} else {
929
-						$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
930
-					}
931
-
932
-					$this->writeUpdate($storage2, $internalPath2);
933
-
934
-					$this->changeLock($path2, ILockingProvider::LOCK_SHARED);
935
-					$lockTypePath2 = ILockingProvider::LOCK_SHARED;
936
-
937
-					if ($this->shouldEmitHooks() && $result !== false) {
938
-						\OC_Hook::emit(
939
-							Filesystem::CLASSNAME,
940
-							Filesystem::signal_post_copy,
941
-							array(
942
-								Filesystem::signal_param_oldpath => $this->getHookPath($path1),
943
-								Filesystem::signal_param_newpath => $this->getHookPath($path2)
944
-							)
945
-						);
946
-						$this->emit_file_hooks_post($exists, $path2);
947
-					}
948
-
949
-				}
950
-			} catch (\Exception $e) {
951
-				$this->unlockFile($path2, $lockTypePath2);
952
-				$this->unlockFile($path1, $lockTypePath1);
953
-				throw $e;
954
-			}
955
-
956
-			$this->unlockFile($path2, $lockTypePath2);
957
-			$this->unlockFile($path1, $lockTypePath1);
958
-
959
-		}
960
-		return $result;
961
-	}
962
-
963
-	/**
964
-	 * @param string $path
965
-	 * @param string $mode 'r' or 'w'
966
-	 * @return resource
967
-	 * @throws LockedException
968
-	 */
969
-	public function fopen($path, $mode) {
970
-		$mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support
971
-		$hooks = array();
972
-		switch ($mode) {
973
-			case 'r':
974
-				$hooks[] = 'read';
975
-				break;
976
-			case 'r+':
977
-			case 'w+':
978
-			case 'x+':
979
-			case 'a+':
980
-				$hooks[] = 'read';
981
-				$hooks[] = 'write';
982
-				break;
983
-			case 'w':
984
-			case 'x':
985
-			case 'a':
986
-				$hooks[] = 'write';
987
-				break;
988
-			default:
989
-				\OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR);
990
-		}
991
-
992
-		if ($mode !== 'r' && $mode !== 'w') {
993
-			\OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends');
994
-		}
995
-
996
-		return $this->basicOperation('fopen', $path, $hooks, $mode);
997
-	}
998
-
999
-	/**
1000
-	 * @param string $path
1001
-	 * @return bool|string
1002
-	 * @throws \OCP\Files\InvalidPathException
1003
-	 */
1004
-	public function toTmpFile($path) {
1005
-		$this->assertPathLength($path);
1006
-		if (Filesystem::isValidPath($path)) {
1007
-			$source = $this->fopen($path, 'r');
1008
-			if ($source) {
1009
-				$extension = pathinfo($path, PATHINFO_EXTENSION);
1010
-				$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
1011
-				file_put_contents($tmpFile, $source);
1012
-				return $tmpFile;
1013
-			} else {
1014
-				return false;
1015
-			}
1016
-		} else {
1017
-			return false;
1018
-		}
1019
-	}
1020
-
1021
-	/**
1022
-	 * @param string $tmpFile
1023
-	 * @param string $path
1024
-	 * @return bool|mixed
1025
-	 * @throws \OCP\Files\InvalidPathException
1026
-	 */
1027
-	public function fromTmpFile($tmpFile, $path) {
1028
-		$this->assertPathLength($path);
1029
-		if (Filesystem::isValidPath($path)) {
1030
-
1031
-			// Get directory that the file is going into
1032
-			$filePath = dirname($path);
1033
-
1034
-			// Create the directories if any
1035
-			if (!$this->file_exists($filePath)) {
1036
-				$result = $this->createParentDirectories($filePath);
1037
-				if ($result === false) {
1038
-					return false;
1039
-				}
1040
-			}
1041
-
1042
-			$source = fopen($tmpFile, 'r');
1043
-			if ($source) {
1044
-				$result = $this->file_put_contents($path, $source);
1045
-				// $this->file_put_contents() might have already closed
1046
-				// the resource, so we check it, before trying to close it
1047
-				// to avoid messages in the error log.
1048
-				if (is_resource($source)) {
1049
-					fclose($source);
1050
-				}
1051
-				unlink($tmpFile);
1052
-				return $result;
1053
-			} else {
1054
-				return false;
1055
-			}
1056
-		} else {
1057
-			return false;
1058
-		}
1059
-	}
1060
-
1061
-
1062
-	/**
1063
-	 * @param string $path
1064
-	 * @return mixed
1065
-	 * @throws \OCP\Files\InvalidPathException
1066
-	 */
1067
-	public function getMimeType($path) {
1068
-		$this->assertPathLength($path);
1069
-		return $this->basicOperation('getMimeType', $path);
1070
-	}
1071
-
1072
-	/**
1073
-	 * @param string $type
1074
-	 * @param string $path
1075
-	 * @param bool $raw
1076
-	 * @return bool|null|string
1077
-	 */
1078
-	public function hash($type, $path, $raw = false) {
1079
-		$postFix = (substr($path, -1) === '/') ? '/' : '';
1080
-		$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
1081
-		if (Filesystem::isValidPath($path)) {
1082
-			$path = $this->getRelativePath($absolutePath);
1083
-			if ($path == null) {
1084
-				return false;
1085
-			}
1086
-			if ($this->shouldEmitHooks($path)) {
1087
-				\OC_Hook::emit(
1088
-					Filesystem::CLASSNAME,
1089
-					Filesystem::signal_read,
1090
-					array(Filesystem::signal_param_path => $this->getHookPath($path))
1091
-				);
1092
-			}
1093
-			list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
1094
-			if ($storage) {
1095
-				return $storage->hash($type, $internalPath, $raw);
1096
-			}
1097
-		}
1098
-		return null;
1099
-	}
1100
-
1101
-	/**
1102
-	 * @param string $path
1103
-	 * @return mixed
1104
-	 * @throws \OCP\Files\InvalidPathException
1105
-	 */
1106
-	public function free_space($path = '/') {
1107
-		$this->assertPathLength($path);
1108
-		$result = $this->basicOperation('free_space', $path);
1109
-		if ($result === null) {
1110
-			throw new InvalidPathException();
1111
-		}
1112
-		return $result;
1113
-	}
1114
-
1115
-	/**
1116
-	 * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage
1117
-	 *
1118
-	 * @param string $operation
1119
-	 * @param string $path
1120
-	 * @param array $hooks (optional)
1121
-	 * @param mixed $extraParam (optional)
1122
-	 * @return mixed
1123
-	 * @throws LockedException
1124
-	 *
1125
-	 * This method takes requests for basic filesystem functions (e.g. reading & writing
1126
-	 * files), processes hooks and proxies, sanitises paths, and finally passes them on to
1127
-	 * \OC\Files\Storage\Storage for delegation to a storage backend for execution
1128
-	 */
1129
-	private function basicOperation($operation, $path, $hooks = [], $extraParam = null) {
1130
-		$postFix = (substr($path, -1) === '/') ? '/' : '';
1131
-		$absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
1132
-		if (Filesystem::isValidPath($path)
1133
-			and !Filesystem::isFileBlacklisted($path)
1134
-		) {
1135
-			$path = $this->getRelativePath($absolutePath);
1136
-			if ($path == null) {
1137
-				return false;
1138
-			}
1139
-
1140
-			if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) {
1141
-				// always a shared lock during pre-hooks so the hook can read the file
1142
-				$this->lockFile($path, ILockingProvider::LOCK_SHARED);
1143
-			}
1144
-
1145
-			$run = $this->runHooks($hooks, $path);
1146
-			/** @var \OC\Files\Storage\Storage $storage */
1147
-			list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
1148
-			if ($run and $storage) {
1149
-				if (in_array('write', $hooks) || in_array('delete', $hooks)) {
1150
-					try {
1151
-						$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
1152
-					} catch (LockedException $e) {
1153
-						// release the shared lock we acquired before quiting
1154
-						$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1155
-						throw $e;
1156
-					}
1157
-				}
1158
-				try {
1159
-					if (!is_null($extraParam)) {
1160
-						$result = $storage->$operation($internalPath, $extraParam);
1161
-					} else {
1162
-						$result = $storage->$operation($internalPath);
1163
-					}
1164
-				} catch (\Exception $e) {
1165
-					if (in_array('write', $hooks) || in_array('delete', $hooks)) {
1166
-						$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
1167
-					} else if (in_array('read', $hooks)) {
1168
-						$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1169
-					}
1170
-					throw $e;
1171
-				}
1172
-
1173
-				if ($result && in_array('delete', $hooks) and $result) {
1174
-					$this->removeUpdate($storage, $internalPath);
1175
-				}
1176
-				if ($result && in_array('write', $hooks,  true) && $operation !== 'fopen' && $operation !== 'touch') {
1177
-					$this->writeUpdate($storage, $internalPath);
1178
-				}
1179
-				if ($result && in_array('touch', $hooks)) {
1180
-					$this->writeUpdate($storage, $internalPath, $extraParam);
1181
-				}
1182
-
1183
-				if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) {
1184
-					$this->changeLock($path, ILockingProvider::LOCK_SHARED);
1185
-				}
1186
-
1187
-				$unlockLater = false;
1188
-				if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) {
1189
-					$unlockLater = true;
1190
-					// make sure our unlocking callback will still be called if connection is aborted
1191
-					ignore_user_abort(true);
1192
-					$result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) {
1193
-						if (in_array('write', $hooks)) {
1194
-							$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
1195
-						} else if (in_array('read', $hooks)) {
1196
-							$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1197
-						}
1198
-					});
1199
-				}
1200
-
1201
-				if ($this->shouldEmitHooks($path) && $result !== false) {
1202
-					if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
1203
-						$this->runHooks($hooks, $path, true);
1204
-					}
1205
-				}
1206
-
1207
-				if (!$unlockLater
1208
-					&& (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks))
1209
-				) {
1210
-					$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1211
-				}
1212
-				return $result;
1213
-			} else {
1214
-				$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1215
-			}
1216
-		}
1217
-		return null;
1218
-	}
1219
-
1220
-	/**
1221
-	 * get the path relative to the default root for hook usage
1222
-	 *
1223
-	 * @param string $path
1224
-	 * @return string
1225
-	 */
1226
-	private function getHookPath($path) {
1227
-		if (!Filesystem::getView()) {
1228
-			return $path;
1229
-		}
1230
-		return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path));
1231
-	}
1232
-
1233
-	private function shouldEmitHooks($path = '') {
1234
-		if ($path && Cache\Scanner::isPartialFile($path)) {
1235
-			return false;
1236
-		}
1237
-		if (!Filesystem::$loaded) {
1238
-			return false;
1239
-		}
1240
-		$defaultRoot = Filesystem::getRoot();
1241
-		if ($defaultRoot === null) {
1242
-			return false;
1243
-		}
1244
-		if ($this->fakeRoot === $defaultRoot) {
1245
-			return true;
1246
-		}
1247
-		$fullPath = $this->getAbsolutePath($path);
1248
-
1249
-		if ($fullPath === $defaultRoot) {
1250
-			return true;
1251
-		}
1252
-
1253
-		return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/');
1254
-	}
1255
-
1256
-	/**
1257
-	 * @param string[] $hooks
1258
-	 * @param string $path
1259
-	 * @param bool $post
1260
-	 * @return bool
1261
-	 */
1262
-	private function runHooks($hooks, $path, $post = false) {
1263
-		$relativePath = $path;
1264
-		$path = $this->getHookPath($path);
1265
-		$prefix = $post ? 'post_' : '';
1266
-		$run = true;
1267
-		if ($this->shouldEmitHooks($relativePath)) {
1268
-			foreach ($hooks as $hook) {
1269
-				if ($hook != 'read') {
1270
-					\OC_Hook::emit(
1271
-						Filesystem::CLASSNAME,
1272
-						$prefix . $hook,
1273
-						array(
1274
-							Filesystem::signal_param_run => &$run,
1275
-							Filesystem::signal_param_path => $path
1276
-						)
1277
-					);
1278
-				} elseif (!$post) {
1279
-					\OC_Hook::emit(
1280
-						Filesystem::CLASSNAME,
1281
-						$prefix . $hook,
1282
-						array(
1283
-							Filesystem::signal_param_path => $path
1284
-						)
1285
-					);
1286
-				}
1287
-			}
1288
-		}
1289
-		return $run;
1290
-	}
1291
-
1292
-	/**
1293
-	 * check if a file or folder has been updated since $time
1294
-	 *
1295
-	 * @param string $path
1296
-	 * @param int $time
1297
-	 * @return bool
1298
-	 */
1299
-	public function hasUpdated($path, $time) {
1300
-		return $this->basicOperation('hasUpdated', $path, array(), $time);
1301
-	}
1302
-
1303
-	/**
1304
-	 * @param string $ownerId
1305
-	 * @return \OC\User\User
1306
-	 */
1307
-	private function getUserObjectForOwner($ownerId) {
1308
-		$owner = $this->userManager->get($ownerId);
1309
-		if ($owner instanceof IUser) {
1310
-			return $owner;
1311
-		} else {
1312
-			return new User($ownerId, null, \OC::$server->getEventDispatcher());
1313
-		}
1314
-	}
1315
-
1316
-	/**
1317
-	 * Get file info from cache
1318
-	 *
1319
-	 * If the file is not in cached it will be scanned
1320
-	 * If the file has changed on storage the cache will be updated
1321
-	 *
1322
-	 * @param \OC\Files\Storage\Storage $storage
1323
-	 * @param string $internalPath
1324
-	 * @param string $relativePath
1325
-	 * @return ICacheEntry|bool
1326
-	 */
1327
-	private function getCacheEntry($storage, $internalPath, $relativePath) {
1328
-		$cache = $storage->getCache($internalPath);
1329
-		$data = $cache->get($internalPath);
1330
-		$watcher = $storage->getWatcher($internalPath);
1331
-
1332
-		try {
1333
-			// if the file is not in the cache or needs to be updated, trigger the scanner and reload the data
1334
-			if (!$data || $data['size'] === -1) {
1335
-				if (!$storage->file_exists($internalPath)) {
1336
-					return false;
1337
-				}
1338
-				// don't need to get a lock here since the scanner does it's own locking
1339
-				$scanner = $storage->getScanner($internalPath);
1340
-				$scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
1341
-				$data = $cache->get($internalPath);
1342
-			} else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) {
1343
-				$this->lockFile($relativePath, ILockingProvider::LOCK_SHARED);
1344
-				$watcher->update($internalPath, $data);
1345
-				$storage->getPropagator()->propagateChange($internalPath, time());
1346
-				$data = $cache->get($internalPath);
1347
-				$this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED);
1348
-			}
1349
-		} catch (LockedException $e) {
1350
-			// if the file is locked we just use the old cache info
1351
-		}
1352
-
1353
-		return $data;
1354
-	}
1355
-
1356
-	/**
1357
-	 * get the filesystem info
1358
-	 *
1359
-	 * @param string $path
1360
-	 * @param boolean|string $includeMountPoints true to add mountpoint sizes,
1361
-	 * 'ext' to add only ext storage mount point sizes. Defaults to true.
1362
-	 * defaults to true
1363
-	 * @return \OC\Files\FileInfo|false False if file does not exist
1364
-	 */
1365
-	public function getFileInfo($path, $includeMountPoints = true) {
1366
-		$this->assertPathLength($path);
1367
-		if (!Filesystem::isValidPath($path)) {
1368
-			return false;
1369
-		}
1370
-		if (Cache\Scanner::isPartialFile($path)) {
1371
-			return $this->getPartFileInfo($path);
1372
-		}
1373
-		$relativePath = $path;
1374
-		$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
1375
-
1376
-		$mount = Filesystem::getMountManager()->find($path);
1377
-		if (!$mount) {
1378
-			\OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path);
1379
-			return false;
1380
-		}
1381
-		$storage = $mount->getStorage();
1382
-		$internalPath = $mount->getInternalPath($path);
1383
-		if ($storage) {
1384
-			$data = $this->getCacheEntry($storage, $internalPath, $relativePath);
1385
-
1386
-			if (!$data instanceof ICacheEntry) {
1387
-				return false;
1388
-			}
1389
-
1390
-			if ($mount instanceof MoveableMount && $internalPath === '') {
1391
-				$data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
1392
-			}
1393
-			$ownerId = $storage->getOwner($internalPath);
1394
-			$owner = null;
1395
-			if ($ownerId !== null) {
1396
-				// ownerId might be null if files are accessed with an access token without file system access
1397
-				$owner = $this->getUserObjectForOwner($ownerId);
1398
-			}
1399
-			$info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner);
1400
-
1401
-			if ($data and isset($data['fileid'])) {
1402
-				if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') {
1403
-					//add the sizes of other mount points to the folder
1404
-					$extOnly = ($includeMountPoints === 'ext');
1405
-					$mounts = Filesystem::getMountManager()->findIn($path);
1406
-					$info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) {
1407
-						$subStorage = $mount->getStorage();
1408
-						return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage);
1409
-					}));
1410
-				}
1411
-			}
1412
-
1413
-			return $info;
1414
-		} else {
1415
-			\OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint());
1416
-		}
1417
-
1418
-		return false;
1419
-	}
1420
-
1421
-	/**
1422
-	 * get the content of a directory
1423
-	 *
1424
-	 * @param string $directory path under datadirectory
1425
-	 * @param string $mimetype_filter limit returned content to this mimetype or mimepart
1426
-	 * @return FileInfo[]
1427
-	 */
1428
-	public function getDirectoryContent($directory, $mimetype_filter = '') {
1429
-		$this->assertPathLength($directory);
1430
-		if (!Filesystem::isValidPath($directory)) {
1431
-			return [];
1432
-		}
1433
-		$path = $this->getAbsolutePath($directory);
1434
-		$path = Filesystem::normalizePath($path);
1435
-		$mount = $this->getMount($directory);
1436
-		if (!$mount) {
1437
-			return [];
1438
-		}
1439
-		$storage = $mount->getStorage();
1440
-		$internalPath = $mount->getInternalPath($path);
1441
-		if ($storage) {
1442
-			$cache = $storage->getCache($internalPath);
1443
-			$user = \OC_User::getUser();
1444
-
1445
-			$data = $this->getCacheEntry($storage, $internalPath, $directory);
1446
-
1447
-			if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) {
1448
-				return [];
1449
-			}
1450
-
1451
-			$folderId = $data['fileid'];
1452
-			$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
1453
-
1454
-			$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
1455
-
1456
-			$fileNames = array_map(function(ICacheEntry $content) {
1457
-				return $content->getName();
1458
-			}, $contents);
1459
-			/**
1460
-			 * @var \OC\Files\FileInfo[] $fileInfos
1461
-			 */
1462
-			$fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) {
1463
-				if ($sharingDisabled) {
1464
-					$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
1465
-				}
1466
-				$owner = $this->getUserObjectForOwner($storage->getOwner($content['path']));
1467
-				return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner);
1468
-			}, $contents);
1469
-			$files = array_combine($fileNames, $fileInfos);
1470
-
1471
-			//add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
1472
-			$mounts = Filesystem::getMountManager()->findIn($path);
1473
-			$dirLength = strlen($path);
1474
-			foreach ($mounts as $mount) {
1475
-				$mountPoint = $mount->getMountPoint();
1476
-				$subStorage = $mount->getStorage();
1477
-				if ($subStorage) {
1478
-					$subCache = $subStorage->getCache('');
1479
-
1480
-					$rootEntry = $subCache->get('');
1481
-					if (!$rootEntry) {
1482
-						$subScanner = $subStorage->getScanner('');
1483
-						try {
1484
-							$subScanner->scanFile('');
1485
-						} catch (\OCP\Files\StorageNotAvailableException $e) {
1486
-							continue;
1487
-						} catch (\OCP\Files\StorageInvalidException $e) {
1488
-							continue;
1489
-						} catch (\Exception $e) {
1490
-							// sometimes when the storage is not available it can be any exception
1491
-							\OC::$server->getLogger()->logException($e, [
1492
-								'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"',
1493
-								'level' => ILogger::ERROR,
1494
-								'app' => 'lib',
1495
-							]);
1496
-							continue;
1497
-						}
1498
-						$rootEntry = $subCache->get('');
1499
-					}
1500
-
1501
-					if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) {
1502
-						$relativePath = trim(substr($mountPoint, $dirLength), '/');
1503
-						if ($pos = strpos($relativePath, '/')) {
1504
-							//mountpoint inside subfolder add size to the correct folder
1505
-							$entryName = substr($relativePath, 0, $pos);
1506
-							foreach ($files as &$entry) {
1507
-								if ($entry->getName() === $entryName) {
1508
-									$entry->addSubEntry($rootEntry, $mountPoint);
1509
-								}
1510
-							}
1511
-						} else { //mountpoint in this folder, add an entry for it
1512
-							$rootEntry['name'] = $relativePath;
1513
-							$rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
1514
-							$permissions = $rootEntry['permissions'];
1515
-							// do not allow renaming/deleting the mount point if they are not shared files/folders
1516
-							// for shared files/folders we use the permissions given by the owner
1517
-							if ($mount instanceof MoveableMount) {
1518
-								$rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
1519
-							} else {
1520
-								$rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE));
1521
-							}
1522
-
1523
-							$rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/
1524
-
1525
-							// if sharing was disabled for the user we remove the share permissions
1526
-							if (\OCP\Util::isSharingDisabledForUser()) {
1527
-								$rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
1528
-							}
1529
-
1530
-							$owner = $this->getUserObjectForOwner($subStorage->getOwner(''));
1531
-							$files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner);
1532
-						}
1533
-					}
1534
-				}
1535
-			}
1536
-
1537
-			if ($mimetype_filter) {
1538
-				$files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) {
1539
-					if (strpos($mimetype_filter, '/')) {
1540
-						return $file->getMimetype() === $mimetype_filter;
1541
-					} else {
1542
-						return $file->getMimePart() === $mimetype_filter;
1543
-					}
1544
-				});
1545
-			}
1546
-
1547
-			return array_values($files);
1548
-		} else {
1549
-			return [];
1550
-		}
1551
-	}
1552
-
1553
-	/**
1554
-	 * change file metadata
1555
-	 *
1556
-	 * @param string $path
1557
-	 * @param array|\OCP\Files\FileInfo $data
1558
-	 * @return int
1559
-	 *
1560
-	 * returns the fileid of the updated file
1561
-	 */
1562
-	public function putFileInfo($path, $data) {
1563
-		$this->assertPathLength($path);
1564
-		if ($data instanceof FileInfo) {
1565
-			$data = $data->getData();
1566
-		}
1567
-		$path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
1568
-		/**
1569
-		 * @var \OC\Files\Storage\Storage $storage
1570
-		 * @var string $internalPath
1571
-		 */
1572
-		list($storage, $internalPath) = Filesystem::resolvePath($path);
1573
-		if ($storage) {
1574
-			$cache = $storage->getCache($path);
1575
-
1576
-			if (!$cache->inCache($internalPath)) {
1577
-				$scanner = $storage->getScanner($internalPath);
1578
-				$scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
1579
-			}
1580
-
1581
-			return $cache->put($internalPath, $data);
1582
-		} else {
1583
-			return -1;
1584
-		}
1585
-	}
1586
-
1587
-	/**
1588
-	 * search for files with the name matching $query
1589
-	 *
1590
-	 * @param string $query
1591
-	 * @return FileInfo[]
1592
-	 */
1593
-	public function search($query) {
1594
-		return $this->searchCommon('search', array('%' . $query . '%'));
1595
-	}
1596
-
1597
-	/**
1598
-	 * search for files with the name matching $query
1599
-	 *
1600
-	 * @param string $query
1601
-	 * @return FileInfo[]
1602
-	 */
1603
-	public function searchRaw($query) {
1604
-		return $this->searchCommon('search', array($query));
1605
-	}
1606
-
1607
-	/**
1608
-	 * search for files by mimetype
1609
-	 *
1610
-	 * @param string $mimetype
1611
-	 * @return FileInfo[]
1612
-	 */
1613
-	public function searchByMime($mimetype) {
1614
-		return $this->searchCommon('searchByMime', array($mimetype));
1615
-	}
1616
-
1617
-	/**
1618
-	 * search for files by tag
1619
-	 *
1620
-	 * @param string|int $tag name or tag id
1621
-	 * @param string $userId owner of the tags
1622
-	 * @return FileInfo[]
1623
-	 */
1624
-	public function searchByTag($tag, $userId) {
1625
-		return $this->searchCommon('searchByTag', array($tag, $userId));
1626
-	}
1627
-
1628
-	/**
1629
-	 * @param string $method cache method
1630
-	 * @param array $args
1631
-	 * @return FileInfo[]
1632
-	 */
1633
-	private function searchCommon($method, $args) {
1634
-		$files = array();
1635
-		$rootLength = strlen($this->fakeRoot);
1636
-
1637
-		$mount = $this->getMount('');
1638
-		$mountPoint = $mount->getMountPoint();
1639
-		$storage = $mount->getStorage();
1640
-		if ($storage) {
1641
-			$cache = $storage->getCache('');
1642
-
1643
-			$results = call_user_func_array(array($cache, $method), $args);
1644
-			foreach ($results as $result) {
1645
-				if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
1646
-					$internalPath = $result['path'];
1647
-					$path = $mountPoint . $result['path'];
1648
-					$result['path'] = substr($mountPoint . $result['path'], $rootLength);
1649
-					$owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
1650
-					$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
1651
-				}
1652
-			}
1653
-
1654
-			$mounts = Filesystem::getMountManager()->findIn($this->fakeRoot);
1655
-			foreach ($mounts as $mount) {
1656
-				$mountPoint = $mount->getMountPoint();
1657
-				$storage = $mount->getStorage();
1658
-				if ($storage) {
1659
-					$cache = $storage->getCache('');
1660
-
1661
-					$relativeMountPoint = substr($mountPoint, $rootLength);
1662
-					$results = call_user_func_array(array($cache, $method), $args);
1663
-					if ($results) {
1664
-						foreach ($results as $result) {
1665
-							$internalPath = $result['path'];
1666
-							$result['path'] = rtrim($relativeMountPoint . $result['path'], '/');
1667
-							$path = rtrim($mountPoint . $internalPath, '/');
1668
-							$owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
1669
-							$files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
1670
-						}
1671
-					}
1672
-				}
1673
-			}
1674
-		}
1675
-		return $files;
1676
-	}
1677
-
1678
-	/**
1679
-	 * Get the owner for a file or folder
1680
-	 *
1681
-	 * @param string $path
1682
-	 * @return string the user id of the owner
1683
-	 * @throws NotFoundException
1684
-	 */
1685
-	public function getOwner($path) {
1686
-		$info = $this->getFileInfo($path);
1687
-		if (!$info) {
1688
-			throw new NotFoundException($path . ' not found while trying to get owner');
1689
-		}
1690
-		return $info->getOwner()->getUID();
1691
-	}
1692
-
1693
-	/**
1694
-	 * get the ETag for a file or folder
1695
-	 *
1696
-	 * @param string $path
1697
-	 * @return string
1698
-	 */
1699
-	public function getETag($path) {
1700
-		/**
1701
-		 * @var Storage\Storage $storage
1702
-		 * @var string $internalPath
1703
-		 */
1704
-		list($storage, $internalPath) = $this->resolvePath($path);
1705
-		if ($storage) {
1706
-			return $storage->getETag($internalPath);
1707
-		} else {
1708
-			return null;
1709
-		}
1710
-	}
1711
-
1712
-	/**
1713
-	 * Get the path of a file by id, relative to the view
1714
-	 *
1715
-	 * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file
1716
-	 *
1717
-	 * @param int $id
1718
-	 * @throws NotFoundException
1719
-	 * @return string
1720
-	 */
1721
-	public function getPath($id) {
1722
-		$id = (int)$id;
1723
-		$manager = Filesystem::getMountManager();
1724
-		$mounts = $manager->findIn($this->fakeRoot);
1725
-		$mounts[] = $manager->find($this->fakeRoot);
1726
-		// reverse the array so we start with the storage this view is in
1727
-		// which is the most likely to contain the file we're looking for
1728
-		$mounts = array_reverse($mounts);
1729
-
1730
-		// put non shared mounts in front of the shared mount
1731
-		// this prevent unneeded recursion into shares
1732
-		usort($mounts, function(IMountPoint $a, IMountPoint $b) {
1733
-			return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1;
1734
-		});
1735
-
1736
-		foreach ($mounts as $mount) {
1737
-			/**
1738
-			 * @var \OC\Files\Mount\MountPoint $mount
1739
-			 */
1740
-			if ($mount->getStorage()) {
1741
-				$cache = $mount->getStorage()->getCache();
1742
-				$internalPath = $cache->getPathById($id);
1743
-				if (is_string($internalPath)) {
1744
-					$fullPath = $mount->getMountPoint() . $internalPath;
1745
-					if (!is_null($path = $this->getRelativePath($fullPath))) {
1746
-						return $path;
1747
-					}
1748
-				}
1749
-			}
1750
-		}
1751
-		throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
1752
-	}
1753
-
1754
-	/**
1755
-	 * @param string $path
1756
-	 * @throws InvalidPathException
1757
-	 */
1758
-	private function assertPathLength($path) {
1759
-		$maxLen = min(PHP_MAXPATHLEN, 4000);
1760
-		// Check for the string length - performed using isset() instead of strlen()
1761
-		// because isset() is about 5x-40x faster.
1762
-		if (isset($path[$maxLen])) {
1763
-			$pathLen = strlen($path);
1764
-			throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path");
1765
-		}
1766
-	}
1767
-
1768
-	/**
1769
-	 * check if it is allowed to move a mount point to a given target.
1770
-	 * It is not allowed to move a mount point into a different mount point or
1771
-	 * into an already shared folder
1772
-	 *
1773
-	 * @param IStorage $targetStorage
1774
-	 * @param string $targetInternalPath
1775
-	 * @return boolean
1776
-	 */
1777
-	private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) {
1778
-
1779
-		// note: cannot use the view because the target is already locked
1780
-		$fileId = (int)$targetStorage->getCache()->getId($targetInternalPath);
1781
-		if ($fileId === -1) {
1782
-			// target might not exist, need to check parent instead
1783
-			$fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath));
1784
-		}
1785
-
1786
-		// check if any of the parents were shared by the current owner (include collections)
1787
-		$shares = \OCP\Share::getItemShared(
1788
-			'folder',
1789
-			$fileId,
1790
-			\OCP\Share::FORMAT_NONE,
1791
-			null,
1792
-			true
1793
-		);
1794
-
1795
-		if (count($shares) > 0) {
1796
-			\OCP\Util::writeLog('files',
1797
-				'It is not allowed to move one mount point into a shared folder',
1798
-				ILogger::DEBUG);
1799
-			return false;
1800
-		}
1801
-
1802
-		return true;
1803
-	}
1804
-
1805
-	/**
1806
-	 * Get a fileinfo object for files that are ignored in the cache (part files)
1807
-	 *
1808
-	 * @param string $path
1809
-	 * @return \OCP\Files\FileInfo
1810
-	 */
1811
-	private function getPartFileInfo($path) {
1812
-		$mount = $this->getMount($path);
1813
-		$storage = $mount->getStorage();
1814
-		$internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
1815
-		$owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
1816
-		return new FileInfo(
1817
-			$this->getAbsolutePath($path),
1818
-			$storage,
1819
-			$internalPath,
1820
-			[
1821
-				'fileid' => null,
1822
-				'mimetype' => $storage->getMimeType($internalPath),
1823
-				'name' => basename($path),
1824
-				'etag' => null,
1825
-				'size' => $storage->filesize($internalPath),
1826
-				'mtime' => $storage->filemtime($internalPath),
1827
-				'encrypted' => false,
1828
-				'permissions' => \OCP\Constants::PERMISSION_ALL
1829
-			],
1830
-			$mount,
1831
-			$owner
1832
-		);
1833
-	}
1834
-
1835
-	/**
1836
-	 * @param string $path
1837
-	 * @param string $fileName
1838
-	 * @throws InvalidPathException
1839
-	 */
1840
-	public function verifyPath($path, $fileName) {
1841
-		try {
1842
-			/** @type \OCP\Files\Storage $storage */
1843
-			list($storage, $internalPath) = $this->resolvePath($path);
1844
-			$storage->verifyPath($internalPath, $fileName);
1845
-		} catch (ReservedWordException $ex) {
1846
-			$l = \OC::$server->getL10N('lib');
1847
-			throw new InvalidPathException($l->t('File name is a reserved word'));
1848
-		} catch (InvalidCharacterInPathException $ex) {
1849
-			$l = \OC::$server->getL10N('lib');
1850
-			throw new InvalidPathException($l->t('File name contains at least one invalid character'));
1851
-		} catch (FileNameTooLongException $ex) {
1852
-			$l = \OC::$server->getL10N('lib');
1853
-			throw new InvalidPathException($l->t('File name is too long'));
1854
-		} catch (InvalidDirectoryException $ex) {
1855
-			$l = \OC::$server->getL10N('lib');
1856
-			throw new InvalidPathException($l->t('Dot files are not allowed'));
1857
-		} catch (EmptyFileNameException $ex) {
1858
-			$l = \OC::$server->getL10N('lib');
1859
-			throw new InvalidPathException($l->t('Empty filename is not allowed'));
1860
-		}
1861
-	}
1862
-
1863
-	/**
1864
-	 * get all parent folders of $path
1865
-	 *
1866
-	 * @param string $path
1867
-	 * @return string[]
1868
-	 */
1869
-	private function getParents($path) {
1870
-		$path = trim($path, '/');
1871
-		if (!$path) {
1872
-			return [];
1873
-		}
1874
-
1875
-		$parts = explode('/', $path);
1876
-
1877
-		// remove the single file
1878
-		array_pop($parts);
1879
-		$result = array('/');
1880
-		$resultPath = '';
1881
-		foreach ($parts as $part) {
1882
-			if ($part) {
1883
-				$resultPath .= '/' . $part;
1884
-				$result[] = $resultPath;
1885
-			}
1886
-		}
1887
-		return $result;
1888
-	}
1889
-
1890
-	/**
1891
-	 * Returns the mount point for which to lock
1892
-	 *
1893
-	 * @param string $absolutePath absolute path
1894
-	 * @param bool $useParentMount true to return parent mount instead of whatever
1895
-	 * is mounted directly on the given path, false otherwise
1896
-	 * @return \OC\Files\Mount\MountPoint mount point for which to apply locks
1897
-	 */
1898
-	private function getMountForLock($absolutePath, $useParentMount = false) {
1899
-		$results = [];
1900
-		$mount = Filesystem::getMountManager()->find($absolutePath);
1901
-		if (!$mount) {
1902
-			return $results;
1903
-		}
1904
-
1905
-		if ($useParentMount) {
1906
-			// find out if something is mounted directly on the path
1907
-			$internalPath = $mount->getInternalPath($absolutePath);
1908
-			if ($internalPath === '') {
1909
-				// resolve the parent mount instead
1910
-				$mount = Filesystem::getMountManager()->find(dirname($absolutePath));
1911
-			}
1912
-		}
1913
-
1914
-		return $mount;
1915
-	}
1916
-
1917
-	/**
1918
-	 * Lock the given path
1919
-	 *
1920
-	 * @param string $path the path of the file to lock, relative to the view
1921
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
1922
-	 * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
1923
-	 *
1924
-	 * @return bool False if the path is excluded from locking, true otherwise
1925
-	 * @throws LockedException if the path is already locked
1926
-	 */
1927
-	private function lockPath($path, $type, $lockMountPoint = false) {
1928
-		$absolutePath = $this->getAbsolutePath($path);
1929
-		$absolutePath = Filesystem::normalizePath($absolutePath);
1930
-		if (!$this->shouldLockFile($absolutePath)) {
1931
-			return false;
1932
-		}
1933
-
1934
-		$mount = $this->getMountForLock($absolutePath, $lockMountPoint);
1935
-		if ($mount) {
1936
-			try {
1937
-				$storage = $mount->getStorage();
1938
-				if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
1939
-					$storage->acquireLock(
1940
-						$mount->getInternalPath($absolutePath),
1941
-						$type,
1942
-						$this->lockingProvider
1943
-					);
1944
-				}
1945
-			} catch (LockedException $e) {
1946
-				// rethrow with the a human-readable path
1947
-				throw new LockedException(
1948
-					$this->getPathRelativeToFiles($absolutePath),
1949
-					$e
1950
-				);
1951
-			}
1952
-		}
1953
-
1954
-		return true;
1955
-	}
1956
-
1957
-	/**
1958
-	 * Change the lock type
1959
-	 *
1960
-	 * @param string $path the path of the file to lock, relative to the view
1961
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
1962
-	 * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
1963
-	 *
1964
-	 * @return bool False if the path is excluded from locking, true otherwise
1965
-	 * @throws LockedException if the path is already locked
1966
-	 */
1967
-	public function changeLock($path, $type, $lockMountPoint = false) {
1968
-		$path = Filesystem::normalizePath($path);
1969
-		$absolutePath = $this->getAbsolutePath($path);
1970
-		$absolutePath = Filesystem::normalizePath($absolutePath);
1971
-		if (!$this->shouldLockFile($absolutePath)) {
1972
-			return false;
1973
-		}
1974
-
1975
-		$mount = $this->getMountForLock($absolutePath, $lockMountPoint);
1976
-		if ($mount) {
1977
-			try {
1978
-				$storage = $mount->getStorage();
1979
-				if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
1980
-					$storage->changeLock(
1981
-						$mount->getInternalPath($absolutePath),
1982
-						$type,
1983
-						$this->lockingProvider
1984
-					);
1985
-				}
1986
-			} catch (LockedException $e) {
1987
-				try {
1988
-					// rethrow with the a human-readable path
1989
-					throw new LockedException(
1990
-						$this->getPathRelativeToFiles($absolutePath),
1991
-						$e
1992
-					);
1993
-				} catch (\InvalidArgumentException $e) {
1994
-					throw new LockedException(
1995
-						$absolutePath,
1996
-						$e
1997
-					);
1998
-				}
1999
-			}
2000
-		}
2001
-
2002
-		return true;
2003
-	}
2004
-
2005
-	/**
2006
-	 * Unlock the given path
2007
-	 *
2008
-	 * @param string $path the path of the file to unlock, relative to the view
2009
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
2010
-	 * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
2011
-	 *
2012
-	 * @return bool False if the path is excluded from locking, true otherwise
2013
-	 * @throws LockedException
2014
-	 */
2015
-	private function unlockPath($path, $type, $lockMountPoint = false) {
2016
-		$absolutePath = $this->getAbsolutePath($path);
2017
-		$absolutePath = Filesystem::normalizePath($absolutePath);
2018
-		if (!$this->shouldLockFile($absolutePath)) {
2019
-			return false;
2020
-		}
2021
-
2022
-		$mount = $this->getMountForLock($absolutePath, $lockMountPoint);
2023
-		if ($mount) {
2024
-			$storage = $mount->getStorage();
2025
-			if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
2026
-				$storage->releaseLock(
2027
-					$mount->getInternalPath($absolutePath),
2028
-					$type,
2029
-					$this->lockingProvider
2030
-				);
2031
-			}
2032
-		}
2033
-
2034
-		return true;
2035
-	}
2036
-
2037
-	/**
2038
-	 * Lock a path and all its parents up to the root of the view
2039
-	 *
2040
-	 * @param string $path the path of the file to lock relative to the view
2041
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
2042
-	 * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
2043
-	 *
2044
-	 * @return bool False if the path is excluded from locking, true otherwise
2045
-	 * @throws LockedException
2046
-	 */
2047
-	public function lockFile($path, $type, $lockMountPoint = false) {
2048
-		$absolutePath = $this->getAbsolutePath($path);
2049
-		$absolutePath = Filesystem::normalizePath($absolutePath);
2050
-		if (!$this->shouldLockFile($absolutePath)) {
2051
-			return false;
2052
-		}
2053
-
2054
-		$this->lockPath($path, $type, $lockMountPoint);
2055
-
2056
-		$parents = $this->getParents($path);
2057
-		foreach ($parents as $parent) {
2058
-			$this->lockPath($parent, ILockingProvider::LOCK_SHARED);
2059
-		}
2060
-
2061
-		return true;
2062
-	}
2063
-
2064
-	/**
2065
-	 * Unlock a path and all its parents up to the root of the view
2066
-	 *
2067
-	 * @param string $path the path of the file to lock relative to the view
2068
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
2069
-	 * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
2070
-	 *
2071
-	 * @return bool False if the path is excluded from locking, true otherwise
2072
-	 * @throws LockedException
2073
-	 */
2074
-	public function unlockFile($path, $type, $lockMountPoint = false) {
2075
-		$absolutePath = $this->getAbsolutePath($path);
2076
-		$absolutePath = Filesystem::normalizePath($absolutePath);
2077
-		if (!$this->shouldLockFile($absolutePath)) {
2078
-			return false;
2079
-		}
2080
-
2081
-		$this->unlockPath($path, $type, $lockMountPoint);
2082
-
2083
-		$parents = $this->getParents($path);
2084
-		foreach ($parents as $parent) {
2085
-			$this->unlockPath($parent, ILockingProvider::LOCK_SHARED);
2086
-		}
2087
-
2088
-		return true;
2089
-	}
2090
-
2091
-	/**
2092
-	 * Only lock files in data/user/files/
2093
-	 *
2094
-	 * @param string $path Absolute path to the file/folder we try to (un)lock
2095
-	 * @return bool
2096
-	 */
2097
-	protected function shouldLockFile($path) {
2098
-		$path = Filesystem::normalizePath($path);
2099
-
2100
-		$pathSegments = explode('/', $path);
2101
-		if (isset($pathSegments[2])) {
2102
-			// E.g.: /username/files/path-to-file
2103
-			return ($pathSegments[2] === 'files') && (count($pathSegments) > 3);
2104
-		}
2105
-
2106
-		return strpos($path, '/appdata_') !== 0;
2107
-	}
2108
-
2109
-	/**
2110
-	 * Shortens the given absolute path to be relative to
2111
-	 * "$user/files".
2112
-	 *
2113
-	 * @param string $absolutePath absolute path which is under "files"
2114
-	 *
2115
-	 * @return string path relative to "files" with trimmed slashes or null
2116
-	 * if the path was NOT relative to files
2117
-	 *
2118
-	 * @throws \InvalidArgumentException if the given path was not under "files"
2119
-	 * @since 8.1.0
2120
-	 */
2121
-	public function getPathRelativeToFiles($absolutePath) {
2122
-		$path = Filesystem::normalizePath($absolutePath);
2123
-		$parts = explode('/', trim($path, '/'), 3);
2124
-		// "$user", "files", "path/to/dir"
2125
-		if (!isset($parts[1]) || $parts[1] !== 'files') {
2126
-			$this->logger->error(
2127
-				'$absolutePath must be relative to "files", value is "%s"',
2128
-				[
2129
-					$absolutePath
2130
-				]
2131
-			);
2132
-			throw new \InvalidArgumentException('$absolutePath must be relative to "files"');
2133
-		}
2134
-		if (isset($parts[2])) {
2135
-			return $parts[2];
2136
-		}
2137
-		return '';
2138
-	}
2139
-
2140
-	/**
2141
-	 * @param string $filename
2142
-	 * @return array
2143
-	 * @throws \OC\User\NoUserException
2144
-	 * @throws NotFoundException
2145
-	 */
2146
-	public function getUidAndFilename($filename) {
2147
-		$info = $this->getFileInfo($filename);
2148
-		if (!$info instanceof \OCP\Files\FileInfo) {
2149
-			throw new NotFoundException($this->getAbsolutePath($filename) . ' not found');
2150
-		}
2151
-		$uid = $info->getOwner()->getUID();
2152
-		if ($uid != \OCP\User::getUser()) {
2153
-			Filesystem::initMountPoints($uid);
2154
-			$ownerView = new View('/' . $uid . '/files');
2155
-			try {
2156
-				$filename = $ownerView->getPath($info['fileid']);
2157
-			} catch (NotFoundException $e) {
2158
-				throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid);
2159
-			}
2160
-		}
2161
-		return [$uid, $filename];
2162
-	}
2163
-
2164
-	/**
2165
-	 * Creates parent non-existing folders
2166
-	 *
2167
-	 * @param string $filePath
2168
-	 * @return bool
2169
-	 */
2170
-	private function createParentDirectories($filePath) {
2171
-		$directoryParts = explode('/', $filePath);
2172
-		$directoryParts = array_filter($directoryParts);
2173
-		foreach ($directoryParts as $key => $part) {
2174
-			$currentPathElements = array_slice($directoryParts, 0, $key);
2175
-			$currentPath = '/' . implode('/', $currentPathElements);
2176
-			if ($this->is_file($currentPath)) {
2177
-				return false;
2178
-			}
2179
-			if (!$this->file_exists($currentPath)) {
2180
-				$this->mkdir($currentPath);
2181
-			}
2182
-		}
2183
-
2184
-		return true;
2185
-	}
85
+    /** @var string */
86
+    private $fakeRoot = '';
87
+
88
+    /**
89
+     * @var \OCP\Lock\ILockingProvider
90
+     */
91
+    protected $lockingProvider;
92
+
93
+    private $lockingEnabled;
94
+
95
+    private $updaterEnabled = true;
96
+
97
+    /** @var \OC\User\Manager */
98
+    private $userManager;
99
+
100
+    /** @var \OCP\ILogger */
101
+    private $logger;
102
+
103
+    /**
104
+     * @param string $root
105
+     * @throws \Exception If $root contains an invalid path
106
+     */
107
+    public function __construct($root = '') {
108
+        if (is_null($root)) {
109
+            throw new \InvalidArgumentException('Root can\'t be null');
110
+        }
111
+        if (!Filesystem::isValidPath($root)) {
112
+            throw new \Exception();
113
+        }
114
+
115
+        $this->fakeRoot = $root;
116
+        $this->lockingProvider = \OC::$server->getLockingProvider();
117
+        $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
118
+        $this->userManager = \OC::$server->getUserManager();
119
+        $this->logger = \OC::$server->getLogger();
120
+    }
121
+
122
+    public function getAbsolutePath($path = '/') {
123
+        if ($path === null) {
124
+            return null;
125
+        }
126
+        $this->assertPathLength($path);
127
+        if ($path === '') {
128
+            $path = '/';
129
+        }
130
+        if ($path[0] !== '/') {
131
+            $path = '/' . $path;
132
+        }
133
+        return $this->fakeRoot . $path;
134
+    }
135
+
136
+    /**
137
+     * change the root to a fake root
138
+     *
139
+     * @param string $fakeRoot
140
+     * @return boolean|null
141
+     */
142
+    public function chroot($fakeRoot) {
143
+        if (!$fakeRoot == '') {
144
+            if ($fakeRoot[0] !== '/') {
145
+                $fakeRoot = '/' . $fakeRoot;
146
+            }
147
+        }
148
+        $this->fakeRoot = $fakeRoot;
149
+    }
150
+
151
+    /**
152
+     * get the fake root
153
+     *
154
+     * @return string
155
+     */
156
+    public function getRoot() {
157
+        return $this->fakeRoot;
158
+    }
159
+
160
+    /**
161
+     * get path relative to the root of the view
162
+     *
163
+     * @param string $path
164
+     * @return string
165
+     */
166
+    public function getRelativePath($path) {
167
+        $this->assertPathLength($path);
168
+        if ($this->fakeRoot == '') {
169
+            return $path;
170
+        }
171
+
172
+        if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) {
173
+            return '/';
174
+        }
175
+
176
+        // missing slashes can cause wrong matches!
177
+        $root = rtrim($this->fakeRoot, '/') . '/';
178
+
179
+        if (strpos($path, $root) !== 0) {
180
+            return null;
181
+        } else {
182
+            $path = substr($path, strlen($this->fakeRoot));
183
+            if (strlen($path) === 0) {
184
+                return '/';
185
+            } else {
186
+                return $path;
187
+            }
188
+        }
189
+    }
190
+
191
+    /**
192
+     * get the mountpoint of the storage object for a path
193
+     * ( note: because a storage is not always mounted inside the fakeroot, the
194
+     * returned mountpoint is relative to the absolute root of the filesystem
195
+     * and does not take the chroot into account )
196
+     *
197
+     * @param string $path
198
+     * @return string
199
+     */
200
+    public function getMountPoint($path) {
201
+        return Filesystem::getMountPoint($this->getAbsolutePath($path));
202
+    }
203
+
204
+    /**
205
+     * get the mountpoint of the storage object for a path
206
+     * ( note: because a storage is not always mounted inside the fakeroot, the
207
+     * returned mountpoint is relative to the absolute root of the filesystem
208
+     * and does not take the chroot into account )
209
+     *
210
+     * @param string $path
211
+     * @return \OCP\Files\Mount\IMountPoint
212
+     */
213
+    public function getMount($path) {
214
+        return Filesystem::getMountManager()->find($this->getAbsolutePath($path));
215
+    }
216
+
217
+    /**
218
+     * resolve a path to a storage and internal path
219
+     *
220
+     * @param string $path
221
+     * @return array an array consisting of the storage and the internal path
222
+     */
223
+    public function resolvePath($path) {
224
+        $a = $this->getAbsolutePath($path);
225
+        $p = Filesystem::normalizePath($a);
226
+        return Filesystem::resolvePath($p);
227
+    }
228
+
229
+    /**
230
+     * return the path to a local version of the file
231
+     * we need this because we can't know if a file is stored local or not from
232
+     * outside the filestorage and for some purposes a local file is needed
233
+     *
234
+     * @param string $path
235
+     * @return string
236
+     */
237
+    public function getLocalFile($path) {
238
+        $parent = substr($path, 0, strrpos($path, '/'));
239
+        $path = $this->getAbsolutePath($path);
240
+        list($storage, $internalPath) = Filesystem::resolvePath($path);
241
+        if (Filesystem::isValidPath($parent) and $storage) {
242
+            return $storage->getLocalFile($internalPath);
243
+        } else {
244
+            return null;
245
+        }
246
+    }
247
+
248
+    /**
249
+     * @param string $path
250
+     * @return string
251
+     */
252
+    public function getLocalFolder($path) {
253
+        $parent = substr($path, 0, strrpos($path, '/'));
254
+        $path = $this->getAbsolutePath($path);
255
+        list($storage, $internalPath) = Filesystem::resolvePath($path);
256
+        if (Filesystem::isValidPath($parent) and $storage) {
257
+            return $storage->getLocalFolder($internalPath);
258
+        } else {
259
+            return null;
260
+        }
261
+    }
262
+
263
+    /**
264
+     * the following functions operate with arguments and return values identical
265
+     * to those of their PHP built-in equivalents. Mostly they are merely wrappers
266
+     * for \OC\Files\Storage\Storage via basicOperation().
267
+     */
268
+    public function mkdir($path) {
269
+        return $this->basicOperation('mkdir', $path, array('create', 'write'));
270
+    }
271
+
272
+    /**
273
+     * remove mount point
274
+     *
275
+     * @param \OC\Files\Mount\MoveableMount $mount
276
+     * @param string $path relative to data/
277
+     * @return boolean
278
+     */
279
+    protected function removeMount($mount, $path) {
280
+        if ($mount instanceof MoveableMount) {
281
+            // cut of /user/files to get the relative path to data/user/files
282
+            $pathParts = explode('/', $path, 4);
283
+            $relPath = '/' . $pathParts[3];
284
+            $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true);
285
+            \OC_Hook::emit(
286
+                Filesystem::CLASSNAME, "umount",
287
+                array(Filesystem::signal_param_path => $relPath)
288
+            );
289
+            $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true);
290
+            $result = $mount->removeMount();
291
+            $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true);
292
+            if ($result) {
293
+                \OC_Hook::emit(
294
+                    Filesystem::CLASSNAME, "post_umount",
295
+                    array(Filesystem::signal_param_path => $relPath)
296
+                );
297
+            }
298
+            $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true);
299
+            return $result;
300
+        } else {
301
+            // do not allow deleting the storage's root / the mount point
302
+            // because for some storages it might delete the whole contents
303
+            // but isn't supposed to work that way
304
+            return false;
305
+        }
306
+    }
307
+
308
+    public function disableCacheUpdate() {
309
+        $this->updaterEnabled = false;
310
+    }
311
+
312
+    public function enableCacheUpdate() {
313
+        $this->updaterEnabled = true;
314
+    }
315
+
316
+    protected function writeUpdate(Storage $storage, $internalPath, $time = null) {
317
+        if ($this->updaterEnabled) {
318
+            if (is_null($time)) {
319
+                $time = time();
320
+            }
321
+            $storage->getUpdater()->update($internalPath, $time);
322
+        }
323
+    }
324
+
325
+    protected function removeUpdate(Storage $storage, $internalPath) {
326
+        if ($this->updaterEnabled) {
327
+            $storage->getUpdater()->remove($internalPath);
328
+        }
329
+    }
330
+
331
+    protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) {
332
+        if ($this->updaterEnabled) {
333
+            $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
334
+        }
335
+    }
336
+
337
+    /**
338
+     * @param string $path
339
+     * @return bool|mixed
340
+     */
341
+    public function rmdir($path) {
342
+        $absolutePath = $this->getAbsolutePath($path);
343
+        $mount = Filesystem::getMountManager()->find($absolutePath);
344
+        if ($mount->getInternalPath($absolutePath) === '') {
345
+            return $this->removeMount($mount, $absolutePath);
346
+        }
347
+        if ($this->is_dir($path)) {
348
+            $result = $this->basicOperation('rmdir', $path, array('delete'));
349
+        } else {
350
+            $result = false;
351
+        }
352
+
353
+        if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
354
+            $storage = $mount->getStorage();
355
+            $internalPath = $mount->getInternalPath($absolutePath);
356
+            $storage->getUpdater()->remove($internalPath);
357
+        }
358
+        return $result;
359
+    }
360
+
361
+    /**
362
+     * @param string $path
363
+     * @return resource
364
+     */
365
+    public function opendir($path) {
366
+        return $this->basicOperation('opendir', $path, array('read'));
367
+    }
368
+
369
+    /**
370
+     * @param string $path
371
+     * @return bool|mixed
372
+     */
373
+    public function is_dir($path) {
374
+        if ($path == '/') {
375
+            return true;
376
+        }
377
+        return $this->basicOperation('is_dir', $path);
378
+    }
379
+
380
+    /**
381
+     * @param string $path
382
+     * @return bool|mixed
383
+     */
384
+    public function is_file($path) {
385
+        if ($path == '/') {
386
+            return false;
387
+        }
388
+        return $this->basicOperation('is_file', $path);
389
+    }
390
+
391
+    /**
392
+     * @param string $path
393
+     * @return mixed
394
+     */
395
+    public function stat($path) {
396
+        return $this->basicOperation('stat', $path);
397
+    }
398
+
399
+    /**
400
+     * @param string $path
401
+     * @return mixed
402
+     */
403
+    public function filetype($path) {
404
+        return $this->basicOperation('filetype', $path);
405
+    }
406
+
407
+    /**
408
+     * @param string $path
409
+     * @return mixed
410
+     */
411
+    public function filesize($path) {
412
+        return $this->basicOperation('filesize', $path);
413
+    }
414
+
415
+    /**
416
+     * @param string $path
417
+     * @return bool|mixed
418
+     * @throws \OCP\Files\InvalidPathException
419
+     */
420
+    public function readfile($path) {
421
+        $this->assertPathLength($path);
422
+        @ob_end_clean();
423
+        $handle = $this->fopen($path, 'rb');
424
+        if ($handle) {
425
+            $chunkSize = 8192; // 8 kB chunks
426
+            while (!feof($handle)) {
427
+                echo fread($handle, $chunkSize);
428
+                flush();
429
+            }
430
+            fclose($handle);
431
+            return $this->filesize($path);
432
+        }
433
+        return false;
434
+    }
435
+
436
+    /**
437
+     * @param string $path
438
+     * @param int $from
439
+     * @param int $to
440
+     * @return bool|mixed
441
+     * @throws \OCP\Files\InvalidPathException
442
+     * @throws \OCP\Files\UnseekableException
443
+     */
444
+    public function readfilePart($path, $from, $to) {
445
+        $this->assertPathLength($path);
446
+        @ob_end_clean();
447
+        $handle = $this->fopen($path, 'rb');
448
+        if ($handle) {
449
+            $chunkSize = 8192; // 8 kB chunks
450
+            $startReading = true;
451
+
452
+            if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) {
453
+                // forward file handle via chunked fread because fseek seem to have failed
454
+
455
+                $end = $from + 1;
456
+                while (!feof($handle) && ftell($handle) < $end) {
457
+                    $len = $from - ftell($handle);
458
+                    if ($len > $chunkSize) {
459
+                        $len = $chunkSize;
460
+                    }
461
+                    $result = fread($handle, $len);
462
+
463
+                    if ($result === false) {
464
+                        $startReading = false;
465
+                        break;
466
+                    }
467
+                }
468
+            }
469
+
470
+            if ($startReading) {
471
+                $end = $to + 1;
472
+                while (!feof($handle) && ftell($handle) < $end) {
473
+                    $len = $end - ftell($handle);
474
+                    if ($len > $chunkSize) {
475
+                        $len = $chunkSize;
476
+                    }
477
+                    echo fread($handle, $len);
478
+                    flush();
479
+                }
480
+                return ftell($handle) - $from;
481
+            }
482
+
483
+            throw new \OCP\Files\UnseekableException('fseek error');
484
+        }
485
+        return false;
486
+    }
487
+
488
+    /**
489
+     * @param string $path
490
+     * @return mixed
491
+     */
492
+    public function isCreatable($path) {
493
+        return $this->basicOperation('isCreatable', $path);
494
+    }
495
+
496
+    /**
497
+     * @param string $path
498
+     * @return mixed
499
+     */
500
+    public function isReadable($path) {
501
+        return $this->basicOperation('isReadable', $path);
502
+    }
503
+
504
+    /**
505
+     * @param string $path
506
+     * @return mixed
507
+     */
508
+    public function isUpdatable($path) {
509
+        return $this->basicOperation('isUpdatable', $path);
510
+    }
511
+
512
+    /**
513
+     * @param string $path
514
+     * @return bool|mixed
515
+     */
516
+    public function isDeletable($path) {
517
+        $absolutePath = $this->getAbsolutePath($path);
518
+        $mount = Filesystem::getMountManager()->find($absolutePath);
519
+        if ($mount->getInternalPath($absolutePath) === '') {
520
+            return $mount instanceof MoveableMount;
521
+        }
522
+        return $this->basicOperation('isDeletable', $path);
523
+    }
524
+
525
+    /**
526
+     * @param string $path
527
+     * @return mixed
528
+     */
529
+    public function isSharable($path) {
530
+        return $this->basicOperation('isSharable', $path);
531
+    }
532
+
533
+    /**
534
+     * @param string $path
535
+     * @return bool|mixed
536
+     */
537
+    public function file_exists($path) {
538
+        if ($path == '/') {
539
+            return true;
540
+        }
541
+        return $this->basicOperation('file_exists', $path);
542
+    }
543
+
544
+    /**
545
+     * @param string $path
546
+     * @return mixed
547
+     */
548
+    public function filemtime($path) {
549
+        return $this->basicOperation('filemtime', $path);
550
+    }
551
+
552
+    /**
553
+     * @param string $path
554
+     * @param int|string $mtime
555
+     * @return bool
556
+     */
557
+    public function touch($path, $mtime = null) {
558
+        if (!is_null($mtime) and !is_numeric($mtime)) {
559
+            $mtime = strtotime($mtime);
560
+        }
561
+
562
+        $hooks = array('touch');
563
+
564
+        if (!$this->file_exists($path)) {
565
+            $hooks[] = 'create';
566
+            $hooks[] = 'write';
567
+        }
568
+        try {
569
+            $result = $this->basicOperation('touch', $path, $hooks, $mtime);
570
+        } catch (\Exception $e) {
571
+            $this->logger->logException($e, ['level' => ILogger::INFO, 'message' => 'Error while setting modified time']);
572
+            $result = false;
573
+        }
574
+        if (!$result) {
575
+            // If create file fails because of permissions on external storage like SMB folders,
576
+            // check file exists and return false if not.
577
+            if (!$this->file_exists($path)) {
578
+                return false;
579
+            }
580
+            if (is_null($mtime)) {
581
+                $mtime = time();
582
+            }
583
+            //if native touch fails, we emulate it by changing the mtime in the cache
584
+            $this->putFileInfo($path, array('mtime' => floor($mtime)));
585
+        }
586
+        return true;
587
+    }
588
+
589
+    /**
590
+     * @param string $path
591
+     * @return mixed
592
+     * @throws LockedException
593
+     */
594
+    public function file_get_contents($path) {
595
+        return $this->basicOperation('file_get_contents', $path, array('read'));
596
+    }
597
+
598
+    /**
599
+     * @param bool $exists
600
+     * @param string $path
601
+     * @param bool $run
602
+     */
603
+    protected function emit_file_hooks_pre($exists, $path, &$run) {
604
+        if (!$exists) {
605
+            \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array(
606
+                Filesystem::signal_param_path => $this->getHookPath($path),
607
+                Filesystem::signal_param_run => &$run,
608
+            ));
609
+        } else {
610
+            \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array(
611
+                Filesystem::signal_param_path => $this->getHookPath($path),
612
+                Filesystem::signal_param_run => &$run,
613
+            ));
614
+        }
615
+        \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array(
616
+            Filesystem::signal_param_path => $this->getHookPath($path),
617
+            Filesystem::signal_param_run => &$run,
618
+        ));
619
+    }
620
+
621
+    /**
622
+     * @param bool $exists
623
+     * @param string $path
624
+     */
625
+    protected function emit_file_hooks_post($exists, $path) {
626
+        if (!$exists) {
627
+            \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array(
628
+                Filesystem::signal_param_path => $this->getHookPath($path),
629
+            ));
630
+        } else {
631
+            \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array(
632
+                Filesystem::signal_param_path => $this->getHookPath($path),
633
+            ));
634
+        }
635
+        \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array(
636
+            Filesystem::signal_param_path => $this->getHookPath($path),
637
+        ));
638
+    }
639
+
640
+    /**
641
+     * @param string $path
642
+     * @param string|resource $data
643
+     * @return bool|mixed
644
+     * @throws LockedException
645
+     */
646
+    public function file_put_contents($path, $data) {
647
+        if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
648
+            $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
649
+            if (Filesystem::isValidPath($path)
650
+                and !Filesystem::isFileBlacklisted($path)
651
+            ) {
652
+                $path = $this->getRelativePath($absolutePath);
653
+
654
+                $this->lockFile($path, ILockingProvider::LOCK_SHARED);
655
+
656
+                $exists = $this->file_exists($path);
657
+                $run = true;
658
+                if ($this->shouldEmitHooks($path)) {
659
+                    $this->emit_file_hooks_pre($exists, $path, $run);
660
+                }
661
+                if (!$run) {
662
+                    $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
663
+                    return false;
664
+                }
665
+
666
+                $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
667
+
668
+                /** @var \OC\Files\Storage\Storage $storage */
669
+                list($storage, $internalPath) = $this->resolvePath($path);
670
+                $target = $storage->fopen($internalPath, 'w');
671
+                if ($target) {
672
+                    list (, $result) = \OC_Helper::streamCopy($data, $target);
673
+                    fclose($target);
674
+                    fclose($data);
675
+
676
+                    $this->writeUpdate($storage, $internalPath);
677
+
678
+                    $this->changeLock($path, ILockingProvider::LOCK_SHARED);
679
+
680
+                    if ($this->shouldEmitHooks($path) && $result !== false) {
681
+                        $this->emit_file_hooks_post($exists, $path);
682
+                    }
683
+                    $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
684
+                    return $result;
685
+                } else {
686
+                    $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
687
+                    return false;
688
+                }
689
+            } else {
690
+                return false;
691
+            }
692
+        } else {
693
+            $hooks = $this->file_exists($path) ? array('update', 'write') : array('create', 'write');
694
+            return $this->basicOperation('file_put_contents', $path, $hooks, $data);
695
+        }
696
+    }
697
+
698
+    /**
699
+     * @param string $path
700
+     * @return bool|mixed
701
+     */
702
+    public function unlink($path) {
703
+        if ($path === '' || $path === '/') {
704
+            // do not allow deleting the root
705
+            return false;
706
+        }
707
+        $postFix = (substr($path, -1) === '/') ? '/' : '';
708
+        $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
709
+        $mount = Filesystem::getMountManager()->find($absolutePath . $postFix);
710
+        if ($mount and $mount->getInternalPath($absolutePath) === '') {
711
+            return $this->removeMount($mount, $absolutePath);
712
+        }
713
+        if ($this->is_dir($path)) {
714
+            $result = $this->basicOperation('rmdir', $path, ['delete']);
715
+        } else {
716
+            $result = $this->basicOperation('unlink', $path, ['delete']);
717
+        }
718
+        if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete
719
+            $storage = $mount->getStorage();
720
+            $internalPath = $mount->getInternalPath($absolutePath);
721
+            $storage->getUpdater()->remove($internalPath);
722
+            return true;
723
+        } else {
724
+            return $result;
725
+        }
726
+    }
727
+
728
+    /**
729
+     * @param string $directory
730
+     * @return bool|mixed
731
+     */
732
+    public function deleteAll($directory) {
733
+        return $this->rmdir($directory);
734
+    }
735
+
736
+    /**
737
+     * Rename/move a file or folder from the source path to target path.
738
+     *
739
+     * @param string $path1 source path
740
+     * @param string $path2 target path
741
+     *
742
+     * @return bool|mixed
743
+     * @throws LockedException
744
+     */
745
+    public function rename($path1, $path2) {
746
+        $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
747
+        $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
748
+        $result = false;
749
+        if (
750
+            Filesystem::isValidPath($path2)
751
+            and Filesystem::isValidPath($path1)
752
+            and !Filesystem::isFileBlacklisted($path2)
753
+        ) {
754
+            $path1 = $this->getRelativePath($absolutePath1);
755
+            $path2 = $this->getRelativePath($absolutePath2);
756
+            $exists = $this->file_exists($path2);
757
+
758
+            if ($path1 == null or $path2 == null) {
759
+                return false;
760
+            }
761
+
762
+            $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true);
763
+            try {
764
+                $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true);
765
+
766
+                $run = true;
767
+                if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
768
+                    // if it was a rename from a part file to a regular file it was a write and not a rename operation
769
+                    $this->emit_file_hooks_pre($exists, $path2, $run);
770
+                } elseif ($this->shouldEmitHooks($path1)) {
771
+                    \OC_Hook::emit(
772
+                        Filesystem::CLASSNAME, Filesystem::signal_rename,
773
+                        array(
774
+                            Filesystem::signal_param_oldpath => $this->getHookPath($path1),
775
+                            Filesystem::signal_param_newpath => $this->getHookPath($path2),
776
+                            Filesystem::signal_param_run => &$run
777
+                        )
778
+                    );
779
+                }
780
+                if ($run) {
781
+                    $this->verifyPath(dirname($path2), basename($path2));
782
+
783
+                    $manager = Filesystem::getMountManager();
784
+                    $mount1 = $this->getMount($path1);
785
+                    $mount2 = $this->getMount($path2);
786
+                    $storage1 = $mount1->getStorage();
787
+                    $storage2 = $mount2->getStorage();
788
+                    $internalPath1 = $mount1->getInternalPath($absolutePath1);
789
+                    $internalPath2 = $mount2->getInternalPath($absolutePath2);
790
+
791
+                    $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true);
792
+                    try {
793
+                        $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true);
794
+
795
+                        if ($internalPath1 === '') {
796
+                            if ($mount1 instanceof MoveableMount) {
797
+                                $sourceParentMount = $this->getMount(dirname($path1));
798
+                                if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) {
799
+                                    /**
800
+                                     * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
801
+                                     */
802
+                                    $sourceMountPoint = $mount1->getMountPoint();
803
+                                    $result = $mount1->moveMount($absolutePath2);
804
+                                    $manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
805
+                                } else {
806
+                                    $result = false;
807
+                                }
808
+                            } else {
809
+                                $result = false;
810
+                            }
811
+                            // moving a file/folder within the same mount point
812
+                        } elseif ($storage1 === $storage2) {
813
+                            if ($storage1) {
814
+                                $result = $storage1->rename($internalPath1, $internalPath2);
815
+                            } else {
816
+                                $result = false;
817
+                            }
818
+                            // moving a file/folder between storages (from $storage1 to $storage2)
819
+                        } else {
820
+                            $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
821
+                        }
822
+
823
+                        if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
824
+                            // if it was a rename from a part file to a regular file it was a write and not a rename operation
825
+                            $this->writeUpdate($storage2, $internalPath2);
826
+                        } else if ($result) {
827
+                            if ($internalPath1 !== '') { // don't do a cache update for moved mounts
828
+                                $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2);
829
+                            }
830
+                        }
831
+                    } catch(\Exception $e) {
832
+                        throw $e;
833
+                    } finally {
834
+                        $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true);
835
+                        $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true);
836
+                    }
837
+
838
+                    if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
839
+                        if ($this->shouldEmitHooks()) {
840
+                            $this->emit_file_hooks_post($exists, $path2);
841
+                        }
842
+                    } elseif ($result) {
843
+                        if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) {
844
+                            \OC_Hook::emit(
845
+                                Filesystem::CLASSNAME,
846
+                                Filesystem::signal_post_rename,
847
+                                array(
848
+                                    Filesystem::signal_param_oldpath => $this->getHookPath($path1),
849
+                                    Filesystem::signal_param_newpath => $this->getHookPath($path2)
850
+                                )
851
+                            );
852
+                        }
853
+                    }
854
+                }
855
+            } catch(\Exception $e) {
856
+                throw $e;
857
+            } finally {
858
+                $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true);
859
+                $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true);
860
+            }
861
+        }
862
+        return $result;
863
+    }
864
+
865
+    /**
866
+     * Copy a file/folder from the source path to target path
867
+     *
868
+     * @param string $path1 source path
869
+     * @param string $path2 target path
870
+     * @param bool $preserveMtime whether to preserve mtime on the copy
871
+     *
872
+     * @return bool|mixed
873
+     */
874
+    public function copy($path1, $path2, $preserveMtime = false) {
875
+        $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
876
+        $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
877
+        $result = false;
878
+        if (
879
+            Filesystem::isValidPath($path2)
880
+            and Filesystem::isValidPath($path1)
881
+            and !Filesystem::isFileBlacklisted($path2)
882
+        ) {
883
+            $path1 = $this->getRelativePath($absolutePath1);
884
+            $path2 = $this->getRelativePath($absolutePath2);
885
+
886
+            if ($path1 == null or $path2 == null) {
887
+                return false;
888
+            }
889
+            $run = true;
890
+
891
+            $this->lockFile($path2, ILockingProvider::LOCK_SHARED);
892
+            $this->lockFile($path1, ILockingProvider::LOCK_SHARED);
893
+            $lockTypePath1 = ILockingProvider::LOCK_SHARED;
894
+            $lockTypePath2 = ILockingProvider::LOCK_SHARED;
895
+
896
+            try {
897
+
898
+                $exists = $this->file_exists($path2);
899
+                if ($this->shouldEmitHooks()) {
900
+                    \OC_Hook::emit(
901
+                        Filesystem::CLASSNAME,
902
+                        Filesystem::signal_copy,
903
+                        array(
904
+                            Filesystem::signal_param_oldpath => $this->getHookPath($path1),
905
+                            Filesystem::signal_param_newpath => $this->getHookPath($path2),
906
+                            Filesystem::signal_param_run => &$run
907
+                        )
908
+                    );
909
+                    $this->emit_file_hooks_pre($exists, $path2, $run);
910
+                }
911
+                if ($run) {
912
+                    $mount1 = $this->getMount($path1);
913
+                    $mount2 = $this->getMount($path2);
914
+                    $storage1 = $mount1->getStorage();
915
+                    $internalPath1 = $mount1->getInternalPath($absolutePath1);
916
+                    $storage2 = $mount2->getStorage();
917
+                    $internalPath2 = $mount2->getInternalPath($absolutePath2);
918
+
919
+                    $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE);
920
+                    $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE;
921
+
922
+                    if ($mount1->getMountPoint() == $mount2->getMountPoint()) {
923
+                        if ($storage1) {
924
+                            $result = $storage1->copy($internalPath1, $internalPath2);
925
+                        } else {
926
+                            $result = false;
927
+                        }
928
+                    } else {
929
+                        $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
930
+                    }
931
+
932
+                    $this->writeUpdate($storage2, $internalPath2);
933
+
934
+                    $this->changeLock($path2, ILockingProvider::LOCK_SHARED);
935
+                    $lockTypePath2 = ILockingProvider::LOCK_SHARED;
936
+
937
+                    if ($this->shouldEmitHooks() && $result !== false) {
938
+                        \OC_Hook::emit(
939
+                            Filesystem::CLASSNAME,
940
+                            Filesystem::signal_post_copy,
941
+                            array(
942
+                                Filesystem::signal_param_oldpath => $this->getHookPath($path1),
943
+                                Filesystem::signal_param_newpath => $this->getHookPath($path2)
944
+                            )
945
+                        );
946
+                        $this->emit_file_hooks_post($exists, $path2);
947
+                    }
948
+
949
+                }
950
+            } catch (\Exception $e) {
951
+                $this->unlockFile($path2, $lockTypePath2);
952
+                $this->unlockFile($path1, $lockTypePath1);
953
+                throw $e;
954
+            }
955
+
956
+            $this->unlockFile($path2, $lockTypePath2);
957
+            $this->unlockFile($path1, $lockTypePath1);
958
+
959
+        }
960
+        return $result;
961
+    }
962
+
963
+    /**
964
+     * @param string $path
965
+     * @param string $mode 'r' or 'w'
966
+     * @return resource
967
+     * @throws LockedException
968
+     */
969
+    public function fopen($path, $mode) {
970
+        $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support
971
+        $hooks = array();
972
+        switch ($mode) {
973
+            case 'r':
974
+                $hooks[] = 'read';
975
+                break;
976
+            case 'r+':
977
+            case 'w+':
978
+            case 'x+':
979
+            case 'a+':
980
+                $hooks[] = 'read';
981
+                $hooks[] = 'write';
982
+                break;
983
+            case 'w':
984
+            case 'x':
985
+            case 'a':
986
+                $hooks[] = 'write';
987
+                break;
988
+            default:
989
+                \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, ILogger::ERROR);
990
+        }
991
+
992
+        if ($mode !== 'r' && $mode !== 'w') {
993
+            \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends');
994
+        }
995
+
996
+        return $this->basicOperation('fopen', $path, $hooks, $mode);
997
+    }
998
+
999
+    /**
1000
+     * @param string $path
1001
+     * @return bool|string
1002
+     * @throws \OCP\Files\InvalidPathException
1003
+     */
1004
+    public function toTmpFile($path) {
1005
+        $this->assertPathLength($path);
1006
+        if (Filesystem::isValidPath($path)) {
1007
+            $source = $this->fopen($path, 'r');
1008
+            if ($source) {
1009
+                $extension = pathinfo($path, PATHINFO_EXTENSION);
1010
+                $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension);
1011
+                file_put_contents($tmpFile, $source);
1012
+                return $tmpFile;
1013
+            } else {
1014
+                return false;
1015
+            }
1016
+        } else {
1017
+            return false;
1018
+        }
1019
+    }
1020
+
1021
+    /**
1022
+     * @param string $tmpFile
1023
+     * @param string $path
1024
+     * @return bool|mixed
1025
+     * @throws \OCP\Files\InvalidPathException
1026
+     */
1027
+    public function fromTmpFile($tmpFile, $path) {
1028
+        $this->assertPathLength($path);
1029
+        if (Filesystem::isValidPath($path)) {
1030
+
1031
+            // Get directory that the file is going into
1032
+            $filePath = dirname($path);
1033
+
1034
+            // Create the directories if any
1035
+            if (!$this->file_exists($filePath)) {
1036
+                $result = $this->createParentDirectories($filePath);
1037
+                if ($result === false) {
1038
+                    return false;
1039
+                }
1040
+            }
1041
+
1042
+            $source = fopen($tmpFile, 'r');
1043
+            if ($source) {
1044
+                $result = $this->file_put_contents($path, $source);
1045
+                // $this->file_put_contents() might have already closed
1046
+                // the resource, so we check it, before trying to close it
1047
+                // to avoid messages in the error log.
1048
+                if (is_resource($source)) {
1049
+                    fclose($source);
1050
+                }
1051
+                unlink($tmpFile);
1052
+                return $result;
1053
+            } else {
1054
+                return false;
1055
+            }
1056
+        } else {
1057
+            return false;
1058
+        }
1059
+    }
1060
+
1061
+
1062
+    /**
1063
+     * @param string $path
1064
+     * @return mixed
1065
+     * @throws \OCP\Files\InvalidPathException
1066
+     */
1067
+    public function getMimeType($path) {
1068
+        $this->assertPathLength($path);
1069
+        return $this->basicOperation('getMimeType', $path);
1070
+    }
1071
+
1072
+    /**
1073
+     * @param string $type
1074
+     * @param string $path
1075
+     * @param bool $raw
1076
+     * @return bool|null|string
1077
+     */
1078
+    public function hash($type, $path, $raw = false) {
1079
+        $postFix = (substr($path, -1) === '/') ? '/' : '';
1080
+        $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
1081
+        if (Filesystem::isValidPath($path)) {
1082
+            $path = $this->getRelativePath($absolutePath);
1083
+            if ($path == null) {
1084
+                return false;
1085
+            }
1086
+            if ($this->shouldEmitHooks($path)) {
1087
+                \OC_Hook::emit(
1088
+                    Filesystem::CLASSNAME,
1089
+                    Filesystem::signal_read,
1090
+                    array(Filesystem::signal_param_path => $this->getHookPath($path))
1091
+                );
1092
+            }
1093
+            list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
1094
+            if ($storage) {
1095
+                return $storage->hash($type, $internalPath, $raw);
1096
+            }
1097
+        }
1098
+        return null;
1099
+    }
1100
+
1101
+    /**
1102
+     * @param string $path
1103
+     * @return mixed
1104
+     * @throws \OCP\Files\InvalidPathException
1105
+     */
1106
+    public function free_space($path = '/') {
1107
+        $this->assertPathLength($path);
1108
+        $result = $this->basicOperation('free_space', $path);
1109
+        if ($result === null) {
1110
+            throw new InvalidPathException();
1111
+        }
1112
+        return $result;
1113
+    }
1114
+
1115
+    /**
1116
+     * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage
1117
+     *
1118
+     * @param string $operation
1119
+     * @param string $path
1120
+     * @param array $hooks (optional)
1121
+     * @param mixed $extraParam (optional)
1122
+     * @return mixed
1123
+     * @throws LockedException
1124
+     *
1125
+     * This method takes requests for basic filesystem functions (e.g. reading & writing
1126
+     * files), processes hooks and proxies, sanitises paths, and finally passes them on to
1127
+     * \OC\Files\Storage\Storage for delegation to a storage backend for execution
1128
+     */
1129
+    private function basicOperation($operation, $path, $hooks = [], $extraParam = null) {
1130
+        $postFix = (substr($path, -1) === '/') ? '/' : '';
1131
+        $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
1132
+        if (Filesystem::isValidPath($path)
1133
+            and !Filesystem::isFileBlacklisted($path)
1134
+        ) {
1135
+            $path = $this->getRelativePath($absolutePath);
1136
+            if ($path == null) {
1137
+                return false;
1138
+            }
1139
+
1140
+            if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) {
1141
+                // always a shared lock during pre-hooks so the hook can read the file
1142
+                $this->lockFile($path, ILockingProvider::LOCK_SHARED);
1143
+            }
1144
+
1145
+            $run = $this->runHooks($hooks, $path);
1146
+            /** @var \OC\Files\Storage\Storage $storage */
1147
+            list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
1148
+            if ($run and $storage) {
1149
+                if (in_array('write', $hooks) || in_array('delete', $hooks)) {
1150
+                    try {
1151
+                        $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
1152
+                    } catch (LockedException $e) {
1153
+                        // release the shared lock we acquired before quiting
1154
+                        $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1155
+                        throw $e;
1156
+                    }
1157
+                }
1158
+                try {
1159
+                    if (!is_null($extraParam)) {
1160
+                        $result = $storage->$operation($internalPath, $extraParam);
1161
+                    } else {
1162
+                        $result = $storage->$operation($internalPath);
1163
+                    }
1164
+                } catch (\Exception $e) {
1165
+                    if (in_array('write', $hooks) || in_array('delete', $hooks)) {
1166
+                        $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
1167
+                    } else if (in_array('read', $hooks)) {
1168
+                        $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1169
+                    }
1170
+                    throw $e;
1171
+                }
1172
+
1173
+                if ($result && in_array('delete', $hooks) and $result) {
1174
+                    $this->removeUpdate($storage, $internalPath);
1175
+                }
1176
+                if ($result && in_array('write', $hooks,  true) && $operation !== 'fopen' && $operation !== 'touch') {
1177
+                    $this->writeUpdate($storage, $internalPath);
1178
+                }
1179
+                if ($result && in_array('touch', $hooks)) {
1180
+                    $this->writeUpdate($storage, $internalPath, $extraParam);
1181
+                }
1182
+
1183
+                if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) {
1184
+                    $this->changeLock($path, ILockingProvider::LOCK_SHARED);
1185
+                }
1186
+
1187
+                $unlockLater = false;
1188
+                if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) {
1189
+                    $unlockLater = true;
1190
+                    // make sure our unlocking callback will still be called if connection is aborted
1191
+                    ignore_user_abort(true);
1192
+                    $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) {
1193
+                        if (in_array('write', $hooks)) {
1194
+                            $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
1195
+                        } else if (in_array('read', $hooks)) {
1196
+                            $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1197
+                        }
1198
+                    });
1199
+                }
1200
+
1201
+                if ($this->shouldEmitHooks($path) && $result !== false) {
1202
+                    if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
1203
+                        $this->runHooks($hooks, $path, true);
1204
+                    }
1205
+                }
1206
+
1207
+                if (!$unlockLater
1208
+                    && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks))
1209
+                ) {
1210
+                    $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1211
+                }
1212
+                return $result;
1213
+            } else {
1214
+                $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
1215
+            }
1216
+        }
1217
+        return null;
1218
+    }
1219
+
1220
+    /**
1221
+     * get the path relative to the default root for hook usage
1222
+     *
1223
+     * @param string $path
1224
+     * @return string
1225
+     */
1226
+    private function getHookPath($path) {
1227
+        if (!Filesystem::getView()) {
1228
+            return $path;
1229
+        }
1230
+        return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path));
1231
+    }
1232
+
1233
+    private function shouldEmitHooks($path = '') {
1234
+        if ($path && Cache\Scanner::isPartialFile($path)) {
1235
+            return false;
1236
+        }
1237
+        if (!Filesystem::$loaded) {
1238
+            return false;
1239
+        }
1240
+        $defaultRoot = Filesystem::getRoot();
1241
+        if ($defaultRoot === null) {
1242
+            return false;
1243
+        }
1244
+        if ($this->fakeRoot === $defaultRoot) {
1245
+            return true;
1246
+        }
1247
+        $fullPath = $this->getAbsolutePath($path);
1248
+
1249
+        if ($fullPath === $defaultRoot) {
1250
+            return true;
1251
+        }
1252
+
1253
+        return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/');
1254
+    }
1255
+
1256
+    /**
1257
+     * @param string[] $hooks
1258
+     * @param string $path
1259
+     * @param bool $post
1260
+     * @return bool
1261
+     */
1262
+    private function runHooks($hooks, $path, $post = false) {
1263
+        $relativePath = $path;
1264
+        $path = $this->getHookPath($path);
1265
+        $prefix = $post ? 'post_' : '';
1266
+        $run = true;
1267
+        if ($this->shouldEmitHooks($relativePath)) {
1268
+            foreach ($hooks as $hook) {
1269
+                if ($hook != 'read') {
1270
+                    \OC_Hook::emit(
1271
+                        Filesystem::CLASSNAME,
1272
+                        $prefix . $hook,
1273
+                        array(
1274
+                            Filesystem::signal_param_run => &$run,
1275
+                            Filesystem::signal_param_path => $path
1276
+                        )
1277
+                    );
1278
+                } elseif (!$post) {
1279
+                    \OC_Hook::emit(
1280
+                        Filesystem::CLASSNAME,
1281
+                        $prefix . $hook,
1282
+                        array(
1283
+                            Filesystem::signal_param_path => $path
1284
+                        )
1285
+                    );
1286
+                }
1287
+            }
1288
+        }
1289
+        return $run;
1290
+    }
1291
+
1292
+    /**
1293
+     * check if a file or folder has been updated since $time
1294
+     *
1295
+     * @param string $path
1296
+     * @param int $time
1297
+     * @return bool
1298
+     */
1299
+    public function hasUpdated($path, $time) {
1300
+        return $this->basicOperation('hasUpdated', $path, array(), $time);
1301
+    }
1302
+
1303
+    /**
1304
+     * @param string $ownerId
1305
+     * @return \OC\User\User
1306
+     */
1307
+    private function getUserObjectForOwner($ownerId) {
1308
+        $owner = $this->userManager->get($ownerId);
1309
+        if ($owner instanceof IUser) {
1310
+            return $owner;
1311
+        } else {
1312
+            return new User($ownerId, null, \OC::$server->getEventDispatcher());
1313
+        }
1314
+    }
1315
+
1316
+    /**
1317
+     * Get file info from cache
1318
+     *
1319
+     * If the file is not in cached it will be scanned
1320
+     * If the file has changed on storage the cache will be updated
1321
+     *
1322
+     * @param \OC\Files\Storage\Storage $storage
1323
+     * @param string $internalPath
1324
+     * @param string $relativePath
1325
+     * @return ICacheEntry|bool
1326
+     */
1327
+    private function getCacheEntry($storage, $internalPath, $relativePath) {
1328
+        $cache = $storage->getCache($internalPath);
1329
+        $data = $cache->get($internalPath);
1330
+        $watcher = $storage->getWatcher($internalPath);
1331
+
1332
+        try {
1333
+            // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data
1334
+            if (!$data || $data['size'] === -1) {
1335
+                if (!$storage->file_exists($internalPath)) {
1336
+                    return false;
1337
+                }
1338
+                // don't need to get a lock here since the scanner does it's own locking
1339
+                $scanner = $storage->getScanner($internalPath);
1340
+                $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
1341
+                $data = $cache->get($internalPath);
1342
+            } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) {
1343
+                $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED);
1344
+                $watcher->update($internalPath, $data);
1345
+                $storage->getPropagator()->propagateChange($internalPath, time());
1346
+                $data = $cache->get($internalPath);
1347
+                $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED);
1348
+            }
1349
+        } catch (LockedException $e) {
1350
+            // if the file is locked we just use the old cache info
1351
+        }
1352
+
1353
+        return $data;
1354
+    }
1355
+
1356
+    /**
1357
+     * get the filesystem info
1358
+     *
1359
+     * @param string $path
1360
+     * @param boolean|string $includeMountPoints true to add mountpoint sizes,
1361
+     * 'ext' to add only ext storage mount point sizes. Defaults to true.
1362
+     * defaults to true
1363
+     * @return \OC\Files\FileInfo|false False if file does not exist
1364
+     */
1365
+    public function getFileInfo($path, $includeMountPoints = true) {
1366
+        $this->assertPathLength($path);
1367
+        if (!Filesystem::isValidPath($path)) {
1368
+            return false;
1369
+        }
1370
+        if (Cache\Scanner::isPartialFile($path)) {
1371
+            return $this->getPartFileInfo($path);
1372
+        }
1373
+        $relativePath = $path;
1374
+        $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
1375
+
1376
+        $mount = Filesystem::getMountManager()->find($path);
1377
+        if (!$mount) {
1378
+            \OC::$server->getLogger()->warning('Mountpoint not found for path: ' . $path);
1379
+            return false;
1380
+        }
1381
+        $storage = $mount->getStorage();
1382
+        $internalPath = $mount->getInternalPath($path);
1383
+        if ($storage) {
1384
+            $data = $this->getCacheEntry($storage, $internalPath, $relativePath);
1385
+
1386
+            if (!$data instanceof ICacheEntry) {
1387
+                return false;
1388
+            }
1389
+
1390
+            if ($mount instanceof MoveableMount && $internalPath === '') {
1391
+                $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
1392
+            }
1393
+            $ownerId = $storage->getOwner($internalPath);
1394
+            $owner = null;
1395
+            if ($ownerId !== null) {
1396
+                // ownerId might be null if files are accessed with an access token without file system access
1397
+                $owner = $this->getUserObjectForOwner($ownerId);
1398
+            }
1399
+            $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner);
1400
+
1401
+            if ($data and isset($data['fileid'])) {
1402
+                if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') {
1403
+                    //add the sizes of other mount points to the folder
1404
+                    $extOnly = ($includeMountPoints === 'ext');
1405
+                    $mounts = Filesystem::getMountManager()->findIn($path);
1406
+                    $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) {
1407
+                        $subStorage = $mount->getStorage();
1408
+                        return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage);
1409
+                    }));
1410
+                }
1411
+            }
1412
+
1413
+            return $info;
1414
+        } else {
1415
+            \OC::$server->getLogger()->warning('Storage not valid for mountpoint: ' . $mount->getMountPoint());
1416
+        }
1417
+
1418
+        return false;
1419
+    }
1420
+
1421
+    /**
1422
+     * get the content of a directory
1423
+     *
1424
+     * @param string $directory path under datadirectory
1425
+     * @param string $mimetype_filter limit returned content to this mimetype or mimepart
1426
+     * @return FileInfo[]
1427
+     */
1428
+    public function getDirectoryContent($directory, $mimetype_filter = '') {
1429
+        $this->assertPathLength($directory);
1430
+        if (!Filesystem::isValidPath($directory)) {
1431
+            return [];
1432
+        }
1433
+        $path = $this->getAbsolutePath($directory);
1434
+        $path = Filesystem::normalizePath($path);
1435
+        $mount = $this->getMount($directory);
1436
+        if (!$mount) {
1437
+            return [];
1438
+        }
1439
+        $storage = $mount->getStorage();
1440
+        $internalPath = $mount->getInternalPath($path);
1441
+        if ($storage) {
1442
+            $cache = $storage->getCache($internalPath);
1443
+            $user = \OC_User::getUser();
1444
+
1445
+            $data = $this->getCacheEntry($storage, $internalPath, $directory);
1446
+
1447
+            if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) {
1448
+                return [];
1449
+            }
1450
+
1451
+            $folderId = $data['fileid'];
1452
+            $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
1453
+
1454
+            $sharingDisabled = \OCP\Util::isSharingDisabledForUser();
1455
+
1456
+            $fileNames = array_map(function(ICacheEntry $content) {
1457
+                return $content->getName();
1458
+            }, $contents);
1459
+            /**
1460
+             * @var \OC\Files\FileInfo[] $fileInfos
1461
+             */
1462
+            $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) {
1463
+                if ($sharingDisabled) {
1464
+                    $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
1465
+                }
1466
+                $owner = $this->getUserObjectForOwner($storage->getOwner($content['path']));
1467
+                return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner);
1468
+            }, $contents);
1469
+            $files = array_combine($fileNames, $fileInfos);
1470
+
1471
+            //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
1472
+            $mounts = Filesystem::getMountManager()->findIn($path);
1473
+            $dirLength = strlen($path);
1474
+            foreach ($mounts as $mount) {
1475
+                $mountPoint = $mount->getMountPoint();
1476
+                $subStorage = $mount->getStorage();
1477
+                if ($subStorage) {
1478
+                    $subCache = $subStorage->getCache('');
1479
+
1480
+                    $rootEntry = $subCache->get('');
1481
+                    if (!$rootEntry) {
1482
+                        $subScanner = $subStorage->getScanner('');
1483
+                        try {
1484
+                            $subScanner->scanFile('');
1485
+                        } catch (\OCP\Files\StorageNotAvailableException $e) {
1486
+                            continue;
1487
+                        } catch (\OCP\Files\StorageInvalidException $e) {
1488
+                            continue;
1489
+                        } catch (\Exception $e) {
1490
+                            // sometimes when the storage is not available it can be any exception
1491
+                            \OC::$server->getLogger()->logException($e, [
1492
+                                'message' => 'Exception while scanning storage "' . $subStorage->getId() . '"',
1493
+                                'level' => ILogger::ERROR,
1494
+                                'app' => 'lib',
1495
+                            ]);
1496
+                            continue;
1497
+                        }
1498
+                        $rootEntry = $subCache->get('');
1499
+                    }
1500
+
1501
+                    if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) {
1502
+                        $relativePath = trim(substr($mountPoint, $dirLength), '/');
1503
+                        if ($pos = strpos($relativePath, '/')) {
1504
+                            //mountpoint inside subfolder add size to the correct folder
1505
+                            $entryName = substr($relativePath, 0, $pos);
1506
+                            foreach ($files as &$entry) {
1507
+                                if ($entry->getName() === $entryName) {
1508
+                                    $entry->addSubEntry($rootEntry, $mountPoint);
1509
+                                }
1510
+                            }
1511
+                        } else { //mountpoint in this folder, add an entry for it
1512
+                            $rootEntry['name'] = $relativePath;
1513
+                            $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
1514
+                            $permissions = $rootEntry['permissions'];
1515
+                            // do not allow renaming/deleting the mount point if they are not shared files/folders
1516
+                            // for shared files/folders we use the permissions given by the owner
1517
+                            if ($mount instanceof MoveableMount) {
1518
+                                $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
1519
+                            } else {
1520
+                                $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE));
1521
+                            }
1522
+
1523
+                            $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/
1524
+
1525
+                            // if sharing was disabled for the user we remove the share permissions
1526
+                            if (\OCP\Util::isSharingDisabledForUser()) {
1527
+                                $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
1528
+                            }
1529
+
1530
+                            $owner = $this->getUserObjectForOwner($subStorage->getOwner(''));
1531
+                            $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner);
1532
+                        }
1533
+                    }
1534
+                }
1535
+            }
1536
+
1537
+            if ($mimetype_filter) {
1538
+                $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) {
1539
+                    if (strpos($mimetype_filter, '/')) {
1540
+                        return $file->getMimetype() === $mimetype_filter;
1541
+                    } else {
1542
+                        return $file->getMimePart() === $mimetype_filter;
1543
+                    }
1544
+                });
1545
+            }
1546
+
1547
+            return array_values($files);
1548
+        } else {
1549
+            return [];
1550
+        }
1551
+    }
1552
+
1553
+    /**
1554
+     * change file metadata
1555
+     *
1556
+     * @param string $path
1557
+     * @param array|\OCP\Files\FileInfo $data
1558
+     * @return int
1559
+     *
1560
+     * returns the fileid of the updated file
1561
+     */
1562
+    public function putFileInfo($path, $data) {
1563
+        $this->assertPathLength($path);
1564
+        if ($data instanceof FileInfo) {
1565
+            $data = $data->getData();
1566
+        }
1567
+        $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
1568
+        /**
1569
+         * @var \OC\Files\Storage\Storage $storage
1570
+         * @var string $internalPath
1571
+         */
1572
+        list($storage, $internalPath) = Filesystem::resolvePath($path);
1573
+        if ($storage) {
1574
+            $cache = $storage->getCache($path);
1575
+
1576
+            if (!$cache->inCache($internalPath)) {
1577
+                $scanner = $storage->getScanner($internalPath);
1578
+                $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
1579
+            }
1580
+
1581
+            return $cache->put($internalPath, $data);
1582
+        } else {
1583
+            return -1;
1584
+        }
1585
+    }
1586
+
1587
+    /**
1588
+     * search for files with the name matching $query
1589
+     *
1590
+     * @param string $query
1591
+     * @return FileInfo[]
1592
+     */
1593
+    public function search($query) {
1594
+        return $this->searchCommon('search', array('%' . $query . '%'));
1595
+    }
1596
+
1597
+    /**
1598
+     * search for files with the name matching $query
1599
+     *
1600
+     * @param string $query
1601
+     * @return FileInfo[]
1602
+     */
1603
+    public function searchRaw($query) {
1604
+        return $this->searchCommon('search', array($query));
1605
+    }
1606
+
1607
+    /**
1608
+     * search for files by mimetype
1609
+     *
1610
+     * @param string $mimetype
1611
+     * @return FileInfo[]
1612
+     */
1613
+    public function searchByMime($mimetype) {
1614
+        return $this->searchCommon('searchByMime', array($mimetype));
1615
+    }
1616
+
1617
+    /**
1618
+     * search for files by tag
1619
+     *
1620
+     * @param string|int $tag name or tag id
1621
+     * @param string $userId owner of the tags
1622
+     * @return FileInfo[]
1623
+     */
1624
+    public function searchByTag($tag, $userId) {
1625
+        return $this->searchCommon('searchByTag', array($tag, $userId));
1626
+    }
1627
+
1628
+    /**
1629
+     * @param string $method cache method
1630
+     * @param array $args
1631
+     * @return FileInfo[]
1632
+     */
1633
+    private function searchCommon($method, $args) {
1634
+        $files = array();
1635
+        $rootLength = strlen($this->fakeRoot);
1636
+
1637
+        $mount = $this->getMount('');
1638
+        $mountPoint = $mount->getMountPoint();
1639
+        $storage = $mount->getStorage();
1640
+        if ($storage) {
1641
+            $cache = $storage->getCache('');
1642
+
1643
+            $results = call_user_func_array(array($cache, $method), $args);
1644
+            foreach ($results as $result) {
1645
+                if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
1646
+                    $internalPath = $result['path'];
1647
+                    $path = $mountPoint . $result['path'];
1648
+                    $result['path'] = substr($mountPoint . $result['path'], $rootLength);
1649
+                    $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
1650
+                    $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
1651
+                }
1652
+            }
1653
+
1654
+            $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot);
1655
+            foreach ($mounts as $mount) {
1656
+                $mountPoint = $mount->getMountPoint();
1657
+                $storage = $mount->getStorage();
1658
+                if ($storage) {
1659
+                    $cache = $storage->getCache('');
1660
+
1661
+                    $relativeMountPoint = substr($mountPoint, $rootLength);
1662
+                    $results = call_user_func_array(array($cache, $method), $args);
1663
+                    if ($results) {
1664
+                        foreach ($results as $result) {
1665
+                            $internalPath = $result['path'];
1666
+                            $result['path'] = rtrim($relativeMountPoint . $result['path'], '/');
1667
+                            $path = rtrim($mountPoint . $internalPath, '/');
1668
+                            $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
1669
+                            $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner);
1670
+                        }
1671
+                    }
1672
+                }
1673
+            }
1674
+        }
1675
+        return $files;
1676
+    }
1677
+
1678
+    /**
1679
+     * Get the owner for a file or folder
1680
+     *
1681
+     * @param string $path
1682
+     * @return string the user id of the owner
1683
+     * @throws NotFoundException
1684
+     */
1685
+    public function getOwner($path) {
1686
+        $info = $this->getFileInfo($path);
1687
+        if (!$info) {
1688
+            throw new NotFoundException($path . ' not found while trying to get owner');
1689
+        }
1690
+        return $info->getOwner()->getUID();
1691
+    }
1692
+
1693
+    /**
1694
+     * get the ETag for a file or folder
1695
+     *
1696
+     * @param string $path
1697
+     * @return string
1698
+     */
1699
+    public function getETag($path) {
1700
+        /**
1701
+         * @var Storage\Storage $storage
1702
+         * @var string $internalPath
1703
+         */
1704
+        list($storage, $internalPath) = $this->resolvePath($path);
1705
+        if ($storage) {
1706
+            return $storage->getETag($internalPath);
1707
+        } else {
1708
+            return null;
1709
+        }
1710
+    }
1711
+
1712
+    /**
1713
+     * Get the path of a file by id, relative to the view
1714
+     *
1715
+     * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file
1716
+     *
1717
+     * @param int $id
1718
+     * @throws NotFoundException
1719
+     * @return string
1720
+     */
1721
+    public function getPath($id) {
1722
+        $id = (int)$id;
1723
+        $manager = Filesystem::getMountManager();
1724
+        $mounts = $manager->findIn($this->fakeRoot);
1725
+        $mounts[] = $manager->find($this->fakeRoot);
1726
+        // reverse the array so we start with the storage this view is in
1727
+        // which is the most likely to contain the file we're looking for
1728
+        $mounts = array_reverse($mounts);
1729
+
1730
+        // put non shared mounts in front of the shared mount
1731
+        // this prevent unneeded recursion into shares
1732
+        usort($mounts, function(IMountPoint $a, IMountPoint $b) {
1733
+            return $a instanceof SharedMount && (!$b instanceof SharedMount) ? 1 : -1;
1734
+        });
1735
+
1736
+        foreach ($mounts as $mount) {
1737
+            /**
1738
+             * @var \OC\Files\Mount\MountPoint $mount
1739
+             */
1740
+            if ($mount->getStorage()) {
1741
+                $cache = $mount->getStorage()->getCache();
1742
+                $internalPath = $cache->getPathById($id);
1743
+                if (is_string($internalPath)) {
1744
+                    $fullPath = $mount->getMountPoint() . $internalPath;
1745
+                    if (!is_null($path = $this->getRelativePath($fullPath))) {
1746
+                        return $path;
1747
+                    }
1748
+                }
1749
+            }
1750
+        }
1751
+        throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id));
1752
+    }
1753
+
1754
+    /**
1755
+     * @param string $path
1756
+     * @throws InvalidPathException
1757
+     */
1758
+    private function assertPathLength($path) {
1759
+        $maxLen = min(PHP_MAXPATHLEN, 4000);
1760
+        // Check for the string length - performed using isset() instead of strlen()
1761
+        // because isset() is about 5x-40x faster.
1762
+        if (isset($path[$maxLen])) {
1763
+            $pathLen = strlen($path);
1764
+            throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path");
1765
+        }
1766
+    }
1767
+
1768
+    /**
1769
+     * check if it is allowed to move a mount point to a given target.
1770
+     * It is not allowed to move a mount point into a different mount point or
1771
+     * into an already shared folder
1772
+     *
1773
+     * @param IStorage $targetStorage
1774
+     * @param string $targetInternalPath
1775
+     * @return boolean
1776
+     */
1777
+    private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath) {
1778
+
1779
+        // note: cannot use the view because the target is already locked
1780
+        $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath);
1781
+        if ($fileId === -1) {
1782
+            // target might not exist, need to check parent instead
1783
+            $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath));
1784
+        }
1785
+
1786
+        // check if any of the parents were shared by the current owner (include collections)
1787
+        $shares = \OCP\Share::getItemShared(
1788
+            'folder',
1789
+            $fileId,
1790
+            \OCP\Share::FORMAT_NONE,
1791
+            null,
1792
+            true
1793
+        );
1794
+
1795
+        if (count($shares) > 0) {
1796
+            \OCP\Util::writeLog('files',
1797
+                'It is not allowed to move one mount point into a shared folder',
1798
+                ILogger::DEBUG);
1799
+            return false;
1800
+        }
1801
+
1802
+        return true;
1803
+    }
1804
+
1805
+    /**
1806
+     * Get a fileinfo object for files that are ignored in the cache (part files)
1807
+     *
1808
+     * @param string $path
1809
+     * @return \OCP\Files\FileInfo
1810
+     */
1811
+    private function getPartFileInfo($path) {
1812
+        $mount = $this->getMount($path);
1813
+        $storage = $mount->getStorage();
1814
+        $internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
1815
+        $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath));
1816
+        return new FileInfo(
1817
+            $this->getAbsolutePath($path),
1818
+            $storage,
1819
+            $internalPath,
1820
+            [
1821
+                'fileid' => null,
1822
+                'mimetype' => $storage->getMimeType($internalPath),
1823
+                'name' => basename($path),
1824
+                'etag' => null,
1825
+                'size' => $storage->filesize($internalPath),
1826
+                'mtime' => $storage->filemtime($internalPath),
1827
+                'encrypted' => false,
1828
+                'permissions' => \OCP\Constants::PERMISSION_ALL
1829
+            ],
1830
+            $mount,
1831
+            $owner
1832
+        );
1833
+    }
1834
+
1835
+    /**
1836
+     * @param string $path
1837
+     * @param string $fileName
1838
+     * @throws InvalidPathException
1839
+     */
1840
+    public function verifyPath($path, $fileName) {
1841
+        try {
1842
+            /** @type \OCP\Files\Storage $storage */
1843
+            list($storage, $internalPath) = $this->resolvePath($path);
1844
+            $storage->verifyPath($internalPath, $fileName);
1845
+        } catch (ReservedWordException $ex) {
1846
+            $l = \OC::$server->getL10N('lib');
1847
+            throw new InvalidPathException($l->t('File name is a reserved word'));
1848
+        } catch (InvalidCharacterInPathException $ex) {
1849
+            $l = \OC::$server->getL10N('lib');
1850
+            throw new InvalidPathException($l->t('File name contains at least one invalid character'));
1851
+        } catch (FileNameTooLongException $ex) {
1852
+            $l = \OC::$server->getL10N('lib');
1853
+            throw new InvalidPathException($l->t('File name is too long'));
1854
+        } catch (InvalidDirectoryException $ex) {
1855
+            $l = \OC::$server->getL10N('lib');
1856
+            throw new InvalidPathException($l->t('Dot files are not allowed'));
1857
+        } catch (EmptyFileNameException $ex) {
1858
+            $l = \OC::$server->getL10N('lib');
1859
+            throw new InvalidPathException($l->t('Empty filename is not allowed'));
1860
+        }
1861
+    }
1862
+
1863
+    /**
1864
+     * get all parent folders of $path
1865
+     *
1866
+     * @param string $path
1867
+     * @return string[]
1868
+     */
1869
+    private function getParents($path) {
1870
+        $path = trim($path, '/');
1871
+        if (!$path) {
1872
+            return [];
1873
+        }
1874
+
1875
+        $parts = explode('/', $path);
1876
+
1877
+        // remove the single file
1878
+        array_pop($parts);
1879
+        $result = array('/');
1880
+        $resultPath = '';
1881
+        foreach ($parts as $part) {
1882
+            if ($part) {
1883
+                $resultPath .= '/' . $part;
1884
+                $result[] = $resultPath;
1885
+            }
1886
+        }
1887
+        return $result;
1888
+    }
1889
+
1890
+    /**
1891
+     * Returns the mount point for which to lock
1892
+     *
1893
+     * @param string $absolutePath absolute path
1894
+     * @param bool $useParentMount true to return parent mount instead of whatever
1895
+     * is mounted directly on the given path, false otherwise
1896
+     * @return \OC\Files\Mount\MountPoint mount point for which to apply locks
1897
+     */
1898
+    private function getMountForLock($absolutePath, $useParentMount = false) {
1899
+        $results = [];
1900
+        $mount = Filesystem::getMountManager()->find($absolutePath);
1901
+        if (!$mount) {
1902
+            return $results;
1903
+        }
1904
+
1905
+        if ($useParentMount) {
1906
+            // find out if something is mounted directly on the path
1907
+            $internalPath = $mount->getInternalPath($absolutePath);
1908
+            if ($internalPath === '') {
1909
+                // resolve the parent mount instead
1910
+                $mount = Filesystem::getMountManager()->find(dirname($absolutePath));
1911
+            }
1912
+        }
1913
+
1914
+        return $mount;
1915
+    }
1916
+
1917
+    /**
1918
+     * Lock the given path
1919
+     *
1920
+     * @param string $path the path of the file to lock, relative to the view
1921
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
1922
+     * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
1923
+     *
1924
+     * @return bool False if the path is excluded from locking, true otherwise
1925
+     * @throws LockedException if the path is already locked
1926
+     */
1927
+    private function lockPath($path, $type, $lockMountPoint = false) {
1928
+        $absolutePath = $this->getAbsolutePath($path);
1929
+        $absolutePath = Filesystem::normalizePath($absolutePath);
1930
+        if (!$this->shouldLockFile($absolutePath)) {
1931
+            return false;
1932
+        }
1933
+
1934
+        $mount = $this->getMountForLock($absolutePath, $lockMountPoint);
1935
+        if ($mount) {
1936
+            try {
1937
+                $storage = $mount->getStorage();
1938
+                if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
1939
+                    $storage->acquireLock(
1940
+                        $mount->getInternalPath($absolutePath),
1941
+                        $type,
1942
+                        $this->lockingProvider
1943
+                    );
1944
+                }
1945
+            } catch (LockedException $e) {
1946
+                // rethrow with the a human-readable path
1947
+                throw new LockedException(
1948
+                    $this->getPathRelativeToFiles($absolutePath),
1949
+                    $e
1950
+                );
1951
+            }
1952
+        }
1953
+
1954
+        return true;
1955
+    }
1956
+
1957
+    /**
1958
+     * Change the lock type
1959
+     *
1960
+     * @param string $path the path of the file to lock, relative to the view
1961
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
1962
+     * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
1963
+     *
1964
+     * @return bool False if the path is excluded from locking, true otherwise
1965
+     * @throws LockedException if the path is already locked
1966
+     */
1967
+    public function changeLock($path, $type, $lockMountPoint = false) {
1968
+        $path = Filesystem::normalizePath($path);
1969
+        $absolutePath = $this->getAbsolutePath($path);
1970
+        $absolutePath = Filesystem::normalizePath($absolutePath);
1971
+        if (!$this->shouldLockFile($absolutePath)) {
1972
+            return false;
1973
+        }
1974
+
1975
+        $mount = $this->getMountForLock($absolutePath, $lockMountPoint);
1976
+        if ($mount) {
1977
+            try {
1978
+                $storage = $mount->getStorage();
1979
+                if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
1980
+                    $storage->changeLock(
1981
+                        $mount->getInternalPath($absolutePath),
1982
+                        $type,
1983
+                        $this->lockingProvider
1984
+                    );
1985
+                }
1986
+            } catch (LockedException $e) {
1987
+                try {
1988
+                    // rethrow with the a human-readable path
1989
+                    throw new LockedException(
1990
+                        $this->getPathRelativeToFiles($absolutePath),
1991
+                        $e
1992
+                    );
1993
+                } catch (\InvalidArgumentException $e) {
1994
+                    throw new LockedException(
1995
+                        $absolutePath,
1996
+                        $e
1997
+                    );
1998
+                }
1999
+            }
2000
+        }
2001
+
2002
+        return true;
2003
+    }
2004
+
2005
+    /**
2006
+     * Unlock the given path
2007
+     *
2008
+     * @param string $path the path of the file to unlock, relative to the view
2009
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
2010
+     * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
2011
+     *
2012
+     * @return bool False if the path is excluded from locking, true otherwise
2013
+     * @throws LockedException
2014
+     */
2015
+    private function unlockPath($path, $type, $lockMountPoint = false) {
2016
+        $absolutePath = $this->getAbsolutePath($path);
2017
+        $absolutePath = Filesystem::normalizePath($absolutePath);
2018
+        if (!$this->shouldLockFile($absolutePath)) {
2019
+            return false;
2020
+        }
2021
+
2022
+        $mount = $this->getMountForLock($absolutePath, $lockMountPoint);
2023
+        if ($mount) {
2024
+            $storage = $mount->getStorage();
2025
+            if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) {
2026
+                $storage->releaseLock(
2027
+                    $mount->getInternalPath($absolutePath),
2028
+                    $type,
2029
+                    $this->lockingProvider
2030
+                );
2031
+            }
2032
+        }
2033
+
2034
+        return true;
2035
+    }
2036
+
2037
+    /**
2038
+     * Lock a path and all its parents up to the root of the view
2039
+     *
2040
+     * @param string $path the path of the file to lock relative to the view
2041
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
2042
+     * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
2043
+     *
2044
+     * @return bool False if the path is excluded from locking, true otherwise
2045
+     * @throws LockedException
2046
+     */
2047
+    public function lockFile($path, $type, $lockMountPoint = false) {
2048
+        $absolutePath = $this->getAbsolutePath($path);
2049
+        $absolutePath = Filesystem::normalizePath($absolutePath);
2050
+        if (!$this->shouldLockFile($absolutePath)) {
2051
+            return false;
2052
+        }
2053
+
2054
+        $this->lockPath($path, $type, $lockMountPoint);
2055
+
2056
+        $parents = $this->getParents($path);
2057
+        foreach ($parents as $parent) {
2058
+            $this->lockPath($parent, ILockingProvider::LOCK_SHARED);
2059
+        }
2060
+
2061
+        return true;
2062
+    }
2063
+
2064
+    /**
2065
+     * Unlock a path and all its parents up to the root of the view
2066
+     *
2067
+     * @param string $path the path of the file to lock relative to the view
2068
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
2069
+     * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
2070
+     *
2071
+     * @return bool False if the path is excluded from locking, true otherwise
2072
+     * @throws LockedException
2073
+     */
2074
+    public function unlockFile($path, $type, $lockMountPoint = false) {
2075
+        $absolutePath = $this->getAbsolutePath($path);
2076
+        $absolutePath = Filesystem::normalizePath($absolutePath);
2077
+        if (!$this->shouldLockFile($absolutePath)) {
2078
+            return false;
2079
+        }
2080
+
2081
+        $this->unlockPath($path, $type, $lockMountPoint);
2082
+
2083
+        $parents = $this->getParents($path);
2084
+        foreach ($parents as $parent) {
2085
+            $this->unlockPath($parent, ILockingProvider::LOCK_SHARED);
2086
+        }
2087
+
2088
+        return true;
2089
+    }
2090
+
2091
+    /**
2092
+     * Only lock files in data/user/files/
2093
+     *
2094
+     * @param string $path Absolute path to the file/folder we try to (un)lock
2095
+     * @return bool
2096
+     */
2097
+    protected function shouldLockFile($path) {
2098
+        $path = Filesystem::normalizePath($path);
2099
+
2100
+        $pathSegments = explode('/', $path);
2101
+        if (isset($pathSegments[2])) {
2102
+            // E.g.: /username/files/path-to-file
2103
+            return ($pathSegments[2] === 'files') && (count($pathSegments) > 3);
2104
+        }
2105
+
2106
+        return strpos($path, '/appdata_') !== 0;
2107
+    }
2108
+
2109
+    /**
2110
+     * Shortens the given absolute path to be relative to
2111
+     * "$user/files".
2112
+     *
2113
+     * @param string $absolutePath absolute path which is under "files"
2114
+     *
2115
+     * @return string path relative to "files" with trimmed slashes or null
2116
+     * if the path was NOT relative to files
2117
+     *
2118
+     * @throws \InvalidArgumentException if the given path was not under "files"
2119
+     * @since 8.1.0
2120
+     */
2121
+    public function getPathRelativeToFiles($absolutePath) {
2122
+        $path = Filesystem::normalizePath($absolutePath);
2123
+        $parts = explode('/', trim($path, '/'), 3);
2124
+        // "$user", "files", "path/to/dir"
2125
+        if (!isset($parts[1]) || $parts[1] !== 'files') {
2126
+            $this->logger->error(
2127
+                '$absolutePath must be relative to "files", value is "%s"',
2128
+                [
2129
+                    $absolutePath
2130
+                ]
2131
+            );
2132
+            throw new \InvalidArgumentException('$absolutePath must be relative to "files"');
2133
+        }
2134
+        if (isset($parts[2])) {
2135
+            return $parts[2];
2136
+        }
2137
+        return '';
2138
+    }
2139
+
2140
+    /**
2141
+     * @param string $filename
2142
+     * @return array
2143
+     * @throws \OC\User\NoUserException
2144
+     * @throws NotFoundException
2145
+     */
2146
+    public function getUidAndFilename($filename) {
2147
+        $info = $this->getFileInfo($filename);
2148
+        if (!$info instanceof \OCP\Files\FileInfo) {
2149
+            throw new NotFoundException($this->getAbsolutePath($filename) . ' not found');
2150
+        }
2151
+        $uid = $info->getOwner()->getUID();
2152
+        if ($uid != \OCP\User::getUser()) {
2153
+            Filesystem::initMountPoints($uid);
2154
+            $ownerView = new View('/' . $uid . '/files');
2155
+            try {
2156
+                $filename = $ownerView->getPath($info['fileid']);
2157
+            } catch (NotFoundException $e) {
2158
+                throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid);
2159
+            }
2160
+        }
2161
+        return [$uid, $filename];
2162
+    }
2163
+
2164
+    /**
2165
+     * Creates parent non-existing folders
2166
+     *
2167
+     * @param string $filePath
2168
+     * @return bool
2169
+     */
2170
+    private function createParentDirectories($filePath) {
2171
+        $directoryParts = explode('/', $filePath);
2172
+        $directoryParts = array_filter($directoryParts);
2173
+        foreach ($directoryParts as $key => $part) {
2174
+            $currentPathElements = array_slice($directoryParts, 0, $key);
2175
+            $currentPath = '/' . implode('/', $currentPathElements);
2176
+            if ($this->is_file($currentPath)) {
2177
+                return false;
2178
+            }
2179
+            if (!$this->file_exists($currentPath)) {
2180
+                $this->mkdir($currentPath);
2181
+            }
2182
+        }
2183
+
2184
+        return true;
2185
+    }
2186 2186
 }
Please login to merge, or discard this patch.
lib/private/Files/Node/File.php 1 patch
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -31,128 +31,128 @@
 block discarded – undo
31 31
 use OCP\Lock\LockedException;
32 32
 
33 33
 class File extends Node implements \OCP\Files\File {
34
-	/**
35
-	 * Creates a Folder that represents a non-existing path
36
-	 *
37
-	 * @param string $path path
38
-	 * @return string non-existing node class
39
-	 */
40
-	protected function createNonExistingNode($path) {
41
-		return new NonExistingFile($this->root, $this->view, $path);
42
-	}
34
+    /**
35
+     * Creates a Folder that represents a non-existing path
36
+     *
37
+     * @param string $path path
38
+     * @return string non-existing node class
39
+     */
40
+    protected function createNonExistingNode($path) {
41
+        return new NonExistingFile($this->root, $this->view, $path);
42
+    }
43 43
 
44
-	/**
45
-	 * @return string
46
-	 * @throws NotPermittedException
47
-	 * @throws LockedException
48
-	 */
49
-	public function getContent() {
50
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
51
-			/**
52
-			 * @var \OC\Files\Storage\Storage $storage;
53
-			 */
54
-			return $this->view->file_get_contents($this->path);
55
-		} else {
56
-			throw new NotPermittedException();
57
-		}
58
-	}
44
+    /**
45
+     * @return string
46
+     * @throws NotPermittedException
47
+     * @throws LockedException
48
+     */
49
+    public function getContent() {
50
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_READ)) {
51
+            /**
52
+             * @var \OC\Files\Storage\Storage $storage;
53
+             */
54
+            return $this->view->file_get_contents($this->path);
55
+        } else {
56
+            throw new NotPermittedException();
57
+        }
58
+    }
59 59
 
60
-	/**
61
-	 * @param string|resource $data
62
-	 * @throws NotPermittedException
63
-	 * @throws \OCP\Files\GenericFileException
64
-	 * @throws LockedException
65
-	 */
66
-	public function putContent($data) {
67
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
68
-			$this->sendHooks(array('preWrite'));
69
-			if ($this->view->file_put_contents($this->path, $data) === false) {
70
-				throw new GenericFileException('file_put_contents failed');
71
-			}
72
-			$this->fileInfo = null;
73
-			$this->sendHooks(array('postWrite'));
74
-		} else {
75
-			throw new NotPermittedException();
76
-		}
77
-	}
60
+    /**
61
+     * @param string|resource $data
62
+     * @throws NotPermittedException
63
+     * @throws \OCP\Files\GenericFileException
64
+     * @throws LockedException
65
+     */
66
+    public function putContent($data) {
67
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
68
+            $this->sendHooks(array('preWrite'));
69
+            if ($this->view->file_put_contents($this->path, $data) === false) {
70
+                throw new GenericFileException('file_put_contents failed');
71
+            }
72
+            $this->fileInfo = null;
73
+            $this->sendHooks(array('postWrite'));
74
+        } else {
75
+            throw new NotPermittedException();
76
+        }
77
+    }
78 78
 
79
-	/**
80
-	 * @param string $mode
81
-	 * @return resource
82
-	 * @throws NotPermittedException
83
-	 * @throws LockedException
84
-	 */
85
-	public function fopen($mode) {
86
-		$preHooks = array();
87
-		$postHooks = array();
88
-		$requiredPermissions = \OCP\Constants::PERMISSION_READ;
89
-		switch ($mode) {
90
-			case 'r+':
91
-			case 'rb+':
92
-			case 'w+':
93
-			case 'wb+':
94
-			case 'x+':
95
-			case 'xb+':
96
-			case 'a+':
97
-			case 'ab+':
98
-			case 'w':
99
-			case 'wb':
100
-			case 'x':
101
-			case 'xb':
102
-			case 'a':
103
-			case 'ab':
104
-				$preHooks[] = 'preWrite';
105
-				$postHooks[] = 'postWrite';
106
-				$requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
107
-				break;
108
-		}
79
+    /**
80
+     * @param string $mode
81
+     * @return resource
82
+     * @throws NotPermittedException
83
+     * @throws LockedException
84
+     */
85
+    public function fopen($mode) {
86
+        $preHooks = array();
87
+        $postHooks = array();
88
+        $requiredPermissions = \OCP\Constants::PERMISSION_READ;
89
+        switch ($mode) {
90
+            case 'r+':
91
+            case 'rb+':
92
+            case 'w+':
93
+            case 'wb+':
94
+            case 'x+':
95
+            case 'xb+':
96
+            case 'a+':
97
+            case 'ab+':
98
+            case 'w':
99
+            case 'wb':
100
+            case 'x':
101
+            case 'xb':
102
+            case 'a':
103
+            case 'ab':
104
+                $preHooks[] = 'preWrite';
105
+                $postHooks[] = 'postWrite';
106
+                $requiredPermissions |= \OCP\Constants::PERMISSION_UPDATE;
107
+                break;
108
+        }
109 109
 
110
-		if ($this->checkPermissions($requiredPermissions)) {
111
-			$this->sendHooks($preHooks);
112
-			$result = $this->view->fopen($this->path, $mode);
113
-			$this->sendHooks($postHooks);
114
-			return $result;
115
-		} else {
116
-			throw new NotPermittedException();
117
-		}
118
-	}
110
+        if ($this->checkPermissions($requiredPermissions)) {
111
+            $this->sendHooks($preHooks);
112
+            $result = $this->view->fopen($this->path, $mode);
113
+            $this->sendHooks($postHooks);
114
+            return $result;
115
+        } else {
116
+            throw new NotPermittedException();
117
+        }
118
+    }
119 119
 
120
-	/**
121
-	 * @throws NotPermittedException
122
-	 * @throws \OCP\Files\InvalidPathException
123
-	 * @throws \OCP\Files\NotFoundException
124
-	 */
125
-	public function delete() {
126
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
127
-			$this->sendHooks(array('preDelete'));
128
-			$fileInfo = $this->getFileInfo();
129
-			$this->view->unlink($this->path);
130
-			$nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
131
-			$this->sendHooks(['postDelete'], [$nonExisting]);
132
-			$this->exists = false;
133
-			$this->fileInfo = null;
134
-		} else {
135
-			throw new NotPermittedException();
136
-		}
137
-	}
120
+    /**
121
+     * @throws NotPermittedException
122
+     * @throws \OCP\Files\InvalidPathException
123
+     * @throws \OCP\Files\NotFoundException
124
+     */
125
+    public function delete() {
126
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
127
+            $this->sendHooks(array('preDelete'));
128
+            $fileInfo = $this->getFileInfo();
129
+            $this->view->unlink($this->path);
130
+            $nonExisting = new NonExistingFile($this->root, $this->view, $this->path, $fileInfo);
131
+            $this->sendHooks(['postDelete'], [$nonExisting]);
132
+            $this->exists = false;
133
+            $this->fileInfo = null;
134
+        } else {
135
+            throw new NotPermittedException();
136
+        }
137
+    }
138 138
 
139
-	/**
140
-	 * @param string $type
141
-	 * @param bool $raw
142
-	 * @return string
143
-	 */
144
-	public function hash($type, $raw = false) {
145
-		return $this->view->hash($type, $this->path, $raw);
146
-	}
139
+    /**
140
+     * @param string $type
141
+     * @param bool $raw
142
+     * @return string
143
+     */
144
+    public function hash($type, $raw = false) {
145
+        return $this->view->hash($type, $this->path, $raw);
146
+    }
147 147
 
148
-	/**
149
-	 * @inheritdoc
150
-	 */
151
-	public function getChecksum() {
152
-		return $this->getFileInfo()->getChecksum();
153
-	}
148
+    /**
149
+     * @inheritdoc
150
+     */
151
+    public function getChecksum() {
152
+        return $this->getFileInfo()->getChecksum();
153
+    }
154 154
 
155
-	public function getExtension(): string {
156
-		return $this->getFileInfo()->getExtension();
157
-	}
155
+    public function getExtension(): string {
156
+        return $this->getFileInfo()->getExtension();
157
+    }
158 158
 }
Please login to merge, or discard this patch.
lib/private/Files/Node/Node.php 1 patch
Indentation   +424 added lines, -424 removed lines patch added patch discarded remove patch
@@ -38,429 +38,429 @@
 block discarded – undo
38 38
 
39 39
 // FIXME: this class really should be abstract
40 40
 class Node implements \OCP\Files\Node {
41
-	/**
42
-	 * @var \OC\Files\View $view
43
-	 */
44
-	protected $view;
45
-
46
-	/**
47
-	 * @var \OC\Files\Node\Root $root
48
-	 */
49
-	protected $root;
50
-
51
-	/**
52
-	 * @var string $path
53
-	 */
54
-	protected $path;
55
-
56
-	/**
57
-	 * @var \OCP\Files\FileInfo
58
-	 */
59
-	protected $fileInfo;
60
-
61
-	/**
62
-	 * @param \OC\Files\View $view
63
-	 * @param \OCP\Files\IRootFolder $root
64
-	 * @param string $path
65
-	 * @param FileInfo $fileInfo
66
-	 */
67
-	public function __construct($root, $view, $path, $fileInfo = null) {
68
-		$this->view = $view;
69
-		$this->root = $root;
70
-		$this->path = $path;
71
-		$this->fileInfo = $fileInfo;
72
-	}
73
-
74
-	/**
75
-	 * Creates a Node of the same type that represents a non-existing path
76
-	 *
77
-	 * @param string $path path
78
-	 * @return string non-existing node class
79
-	 * @throws \Exception
80
-	 */
81
-	protected function createNonExistingNode($path) {
82
-		throw new \Exception('Must be implemented by subclasses');
83
-	}
84
-
85
-	/**
86
-	 * Returns the matching file info
87
-	 *
88
-	 * @return FileInfo
89
-	 * @throws InvalidPathException
90
-	 * @throws NotFoundException
91
-	 */
92
-	public function getFileInfo() {
93
-		if (!Filesystem::isValidPath($this->path)) {
94
-			throw new InvalidPathException();
95
-		}
96
-		if (!$this->fileInfo) {
97
-			$fileInfo = $this->view->getFileInfo($this->path);
98
-			if ($fileInfo instanceof FileInfo) {
99
-				$this->fileInfo = $fileInfo;
100
-			} else {
101
-				throw new NotFoundException();
102
-			}
103
-		}
104
-		return $this->fileInfo;
105
-	}
106
-
107
-	/**
108
-	 * @param string[] $hooks
109
-	 */
110
-	protected function sendHooks($hooks, array $args = null) {
111
-		$args = !empty($args) ? $args : [$this];
112
-		$dispatcher = \OC::$server->getEventDispatcher();
113
-		foreach ($hooks as $hook) {
114
-			$this->root->emit('\OC\Files', $hook, $args);
115
-			$dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
116
-		}
117
-	}
118
-
119
-	/**
120
-	 * @param int $permissions
121
-	 * @return bool
122
-	 * @throws InvalidPathException
123
-	 * @throws NotFoundException
124
-	 */
125
-	protected function checkPermissions($permissions) {
126
-		return ($this->getPermissions() & $permissions) === $permissions;
127
-	}
128
-
129
-	public function delete() {
130
-	}
131
-
132
-	/**
133
-	 * @param int $mtime
134
-	 * @throws InvalidPathException
135
-	 * @throws NotFoundException
136
-	 * @throws NotPermittedException
137
-	 */
138
-	public function touch($mtime = null) {
139
-		if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
140
-			$this->sendHooks(array('preTouch'));
141
-			$this->view->touch($this->path, $mtime);
142
-			$this->sendHooks(array('postTouch'));
143
-			if ($this->fileInfo) {
144
-				if (is_null($mtime)) {
145
-					$mtime = time();
146
-				}
147
-				$this->fileInfo['mtime'] = $mtime;
148
-			}
149
-		} else {
150
-			throw new NotPermittedException();
151
-		}
152
-	}
153
-
154
-	/**
155
-	 * @return \OC\Files\Storage\Storage
156
-	 * @throws \OCP\Files\NotFoundException
157
-	 */
158
-	public function getStorage() {
159
-		list($storage,) = $this->view->resolvePath($this->path);
160
-		return $storage;
161
-	}
162
-
163
-	/**
164
-	 * @return string
165
-	 */
166
-	public function getPath() {
167
-		return $this->path;
168
-	}
169
-
170
-	/**
171
-	 * @return string
172
-	 */
173
-	public function getInternalPath() {
174
-		list(, $internalPath) = $this->view->resolvePath($this->path);
175
-		return $internalPath;
176
-	}
177
-
178
-	/**
179
-	 * @return int
180
-	 * @throws InvalidPathException
181
-	 * @throws NotFoundException
182
-	 */
183
-	public function getId() {
184
-		return $this->getFileInfo()->getId();
185
-	}
186
-
187
-	/**
188
-	 * @return array
189
-	 */
190
-	public function stat() {
191
-		return $this->view->stat($this->path);
192
-	}
193
-
194
-	/**
195
-	 * @return int
196
-	 * @throws InvalidPathException
197
-	 * @throws NotFoundException
198
-	 */
199
-	public function getMTime() {
200
-		return $this->getFileInfo()->getMTime();
201
-	}
202
-
203
-	/**
204
-	 * @param bool $includeMounts
205
-	 * @return int
206
-	 * @throws InvalidPathException
207
-	 * @throws NotFoundException
208
-	 */
209
-	public function getSize($includeMounts = true) {
210
-		return $this->getFileInfo()->getSize($includeMounts);
211
-	}
212
-
213
-	/**
214
-	 * @return string
215
-	 * @throws InvalidPathException
216
-	 * @throws NotFoundException
217
-	 */
218
-	public function getEtag() {
219
-		return $this->getFileInfo()->getEtag();
220
-	}
221
-
222
-	/**
223
-	 * @return int
224
-	 * @throws InvalidPathException
225
-	 * @throws NotFoundException
226
-	 */
227
-	public function getPermissions() {
228
-		return $this->getFileInfo()->getPermissions();
229
-	}
230
-
231
-	/**
232
-	 * @return bool
233
-	 * @throws InvalidPathException
234
-	 * @throws NotFoundException
235
-	 */
236
-	public function isReadable() {
237
-		return $this->getFileInfo()->isReadable();
238
-	}
239
-
240
-	/**
241
-	 * @return bool
242
-	 * @throws InvalidPathException
243
-	 * @throws NotFoundException
244
-	 */
245
-	public function isUpdateable() {
246
-		return $this->getFileInfo()->isUpdateable();
247
-	}
248
-
249
-	/**
250
-	 * @return bool
251
-	 * @throws InvalidPathException
252
-	 * @throws NotFoundException
253
-	 */
254
-	public function isDeletable() {
255
-		return $this->getFileInfo()->isDeletable();
256
-	}
257
-
258
-	/**
259
-	 * @return bool
260
-	 * @throws InvalidPathException
261
-	 * @throws NotFoundException
262
-	 */
263
-	public function isShareable() {
264
-		return $this->getFileInfo()->isShareable();
265
-	}
266
-
267
-	/**
268
-	 * @return bool
269
-	 * @throws InvalidPathException
270
-	 * @throws NotFoundException
271
-	 */
272
-	public function isCreatable() {
273
-		return $this->getFileInfo()->isCreatable();
274
-	}
275
-
276
-	/**
277
-	 * @return Node
278
-	 */
279
-	public function getParent() {
280
-		$newPath = dirname($this->path);
281
-		if ($newPath === '' || $newPath === '.' || $newPath === '/') {
282
-			return $this->root;
283
-		}
284
-		return $this->root->get($newPath);
285
-	}
286
-
287
-	/**
288
-	 * @return string
289
-	 */
290
-	public function getName() {
291
-		return basename($this->path);
292
-	}
293
-
294
-	/**
295
-	 * @param string $path
296
-	 * @return string
297
-	 */
298
-	protected function normalizePath($path) {
299
-		if ($path === '' or $path === '/') {
300
-			return '/';
301
-		}
302
-		//no windows style slashes
303
-		$path = str_replace('\\', '/', $path);
304
-		//add leading slash
305
-		if ($path[0] !== '/') {
306
-			$path = '/' . $path;
307
-		}
308
-		//remove duplicate slashes
309
-		while (strpos($path, '//') !== false) {
310
-			$path = str_replace('//', '/', $path);
311
-		}
312
-		//remove trailing slash
313
-		$path = rtrim($path, '/');
314
-
315
-		return $path;
316
-	}
317
-
318
-	/**
319
-	 * check if the requested path is valid
320
-	 *
321
-	 * @param string $path
322
-	 * @return bool
323
-	 */
324
-	public function isValidPath($path) {
325
-		if (!$path || $path[0] !== '/') {
326
-			$path = '/' . $path;
327
-		}
328
-		if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
329
-			return false;
330
-		}
331
-		return true;
332
-	}
333
-
334
-	public function isMounted() {
335
-		return $this->getFileInfo()->isMounted();
336
-	}
337
-
338
-	public function isShared() {
339
-		return $this->getFileInfo()->isShared();
340
-	}
341
-
342
-	public function getMimeType() {
343
-		return $this->getFileInfo()->getMimetype();
344
-	}
345
-
346
-	public function getMimePart() {
347
-		return $this->getFileInfo()->getMimePart();
348
-	}
349
-
350
-	public function getType() {
351
-		return $this->getFileInfo()->getType();
352
-	}
353
-
354
-	public function isEncrypted() {
355
-		return $this->getFileInfo()->isEncrypted();
356
-	}
357
-
358
-	public function getMountPoint() {
359
-		return $this->getFileInfo()->getMountPoint();
360
-	}
361
-
362
-	public function getOwner() {
363
-		return $this->getFileInfo()->getOwner();
364
-	}
365
-
366
-	public function getChecksum() {
367
-	}
368
-
369
-	public function getExtension(): string {
370
-		return $this->getFileInfo()->getExtension();
371
-	}
372
-
373
-	/**
374
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
375
-	 * @throws LockedException
376
-	 */
377
-	public function lock($type) {
378
-		$this->view->lockFile($this->path, $type);
379
-	}
380
-
381
-	/**
382
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
383
-	 * @throws LockedException
384
-	 */
385
-	public function changeLock($type) {
386
-		$this->view->changeLock($this->path, $type);
387
-	}
388
-
389
-	/**
390
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
391
-	 * @throws LockedException
392
-	 */
393
-	public function unlock($type) {
394
-		$this->view->unlockFile($this->path, $type);
395
-	}
396
-
397
-	/**
398
-	 * @param string $targetPath
399
-	 * @return \OC\Files\Node\Node
400
-	 * @throws InvalidPathException
401
-	 * @throws NotFoundException
402
-	 * @throws NotPermittedException if copy not allowed or failed
403
-	 */
404
-	public function copy($targetPath) {
405
-		$targetPath = $this->normalizePath($targetPath);
406
-		$parent = $this->root->get(dirname($targetPath));
407
-		if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
408
-			$nonExisting = $this->createNonExistingNode($targetPath);
409
-			$this->sendHooks(['preCopy'], [$this, $nonExisting]);
410
-			$this->sendHooks(['preWrite'], [$nonExisting]);
411
-			if (!$this->view->copy($this->path, $targetPath)) {
412
-				throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
413
-			}
414
-			$targetNode = $this->root->get($targetPath);
415
-			$this->sendHooks(['postCopy'], [$this, $targetNode]);
416
-			$this->sendHooks(['postWrite'], [$targetNode]);
417
-			return $targetNode;
418
-		} else {
419
-			throw new NotPermittedException('No permission to copy to path ' . $targetPath);
420
-		}
421
-	}
422
-
423
-	/**
424
-	 * @param string $targetPath
425
-	 * @return \OC\Files\Node\Node
426
-	 * @throws InvalidPathException
427
-	 * @throws NotFoundException
428
-	 * @throws NotPermittedException if move not allowed or failed
429
-	 * @throws LockedException
430
-	 */
431
-	public function move($targetPath) {
432
-		$targetPath = $this->normalizePath($targetPath);
433
-		$parent = $this->root->get(dirname($targetPath));
434
-		if (
435
-			$parent instanceof Folder and
436
-			$this->isValidPath($targetPath) and
437
-			(
438
-				$parent->isCreatable() ||
439
-				($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount)
440
-			)
441
-		) {
442
-			$nonExisting = $this->createNonExistingNode($targetPath);
443
-			$this->sendHooks(['preRename'], [$this, $nonExisting]);
444
-			$this->sendHooks(['preWrite'], [$nonExisting]);
445
-			if (!$this->view->rename($this->path, $targetPath)) {
446
-				throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
447
-			}
448
-			$targetNode = $this->root->get($targetPath);
449
-			$this->sendHooks(['postRename'], [$this, $targetNode]);
450
-			$this->sendHooks(['postWrite'], [$targetNode]);
451
-			$this->path = $targetPath;
452
-			return $targetNode;
453
-		} else {
454
-			throw new NotPermittedException('No permission to move to path ' . $targetPath);
455
-		}
456
-	}
457
-
458
-	public function getCreationTime(): int {
459
-		return $this->getFileInfo()->getCreationTime();
460
-	}
461
-
462
-	public function getUploadTime(): int {
463
-		return $this->getFileInfo()->getUploadTime();
464
-	}
41
+    /**
42
+     * @var \OC\Files\View $view
43
+     */
44
+    protected $view;
45
+
46
+    /**
47
+     * @var \OC\Files\Node\Root $root
48
+     */
49
+    protected $root;
50
+
51
+    /**
52
+     * @var string $path
53
+     */
54
+    protected $path;
55
+
56
+    /**
57
+     * @var \OCP\Files\FileInfo
58
+     */
59
+    protected $fileInfo;
60
+
61
+    /**
62
+     * @param \OC\Files\View $view
63
+     * @param \OCP\Files\IRootFolder $root
64
+     * @param string $path
65
+     * @param FileInfo $fileInfo
66
+     */
67
+    public function __construct($root, $view, $path, $fileInfo = null) {
68
+        $this->view = $view;
69
+        $this->root = $root;
70
+        $this->path = $path;
71
+        $this->fileInfo = $fileInfo;
72
+    }
73
+
74
+    /**
75
+     * Creates a Node of the same type that represents a non-existing path
76
+     *
77
+     * @param string $path path
78
+     * @return string non-existing node class
79
+     * @throws \Exception
80
+     */
81
+    protected function createNonExistingNode($path) {
82
+        throw new \Exception('Must be implemented by subclasses');
83
+    }
84
+
85
+    /**
86
+     * Returns the matching file info
87
+     *
88
+     * @return FileInfo
89
+     * @throws InvalidPathException
90
+     * @throws NotFoundException
91
+     */
92
+    public function getFileInfo() {
93
+        if (!Filesystem::isValidPath($this->path)) {
94
+            throw new InvalidPathException();
95
+        }
96
+        if (!$this->fileInfo) {
97
+            $fileInfo = $this->view->getFileInfo($this->path);
98
+            if ($fileInfo instanceof FileInfo) {
99
+                $this->fileInfo = $fileInfo;
100
+            } else {
101
+                throw new NotFoundException();
102
+            }
103
+        }
104
+        return $this->fileInfo;
105
+    }
106
+
107
+    /**
108
+     * @param string[] $hooks
109
+     */
110
+    protected function sendHooks($hooks, array $args = null) {
111
+        $args = !empty($args) ? $args : [$this];
112
+        $dispatcher = \OC::$server->getEventDispatcher();
113
+        foreach ($hooks as $hook) {
114
+            $this->root->emit('\OC\Files', $hook, $args);
115
+            $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
116
+        }
117
+    }
118
+
119
+    /**
120
+     * @param int $permissions
121
+     * @return bool
122
+     * @throws InvalidPathException
123
+     * @throws NotFoundException
124
+     */
125
+    protected function checkPermissions($permissions) {
126
+        return ($this->getPermissions() & $permissions) === $permissions;
127
+    }
128
+
129
+    public function delete() {
130
+    }
131
+
132
+    /**
133
+     * @param int $mtime
134
+     * @throws InvalidPathException
135
+     * @throws NotFoundException
136
+     * @throws NotPermittedException
137
+     */
138
+    public function touch($mtime = null) {
139
+        if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
140
+            $this->sendHooks(array('preTouch'));
141
+            $this->view->touch($this->path, $mtime);
142
+            $this->sendHooks(array('postTouch'));
143
+            if ($this->fileInfo) {
144
+                if (is_null($mtime)) {
145
+                    $mtime = time();
146
+                }
147
+                $this->fileInfo['mtime'] = $mtime;
148
+            }
149
+        } else {
150
+            throw new NotPermittedException();
151
+        }
152
+    }
153
+
154
+    /**
155
+     * @return \OC\Files\Storage\Storage
156
+     * @throws \OCP\Files\NotFoundException
157
+     */
158
+    public function getStorage() {
159
+        list($storage,) = $this->view->resolvePath($this->path);
160
+        return $storage;
161
+    }
162
+
163
+    /**
164
+     * @return string
165
+     */
166
+    public function getPath() {
167
+        return $this->path;
168
+    }
169
+
170
+    /**
171
+     * @return string
172
+     */
173
+    public function getInternalPath() {
174
+        list(, $internalPath) = $this->view->resolvePath($this->path);
175
+        return $internalPath;
176
+    }
177
+
178
+    /**
179
+     * @return int
180
+     * @throws InvalidPathException
181
+     * @throws NotFoundException
182
+     */
183
+    public function getId() {
184
+        return $this->getFileInfo()->getId();
185
+    }
186
+
187
+    /**
188
+     * @return array
189
+     */
190
+    public function stat() {
191
+        return $this->view->stat($this->path);
192
+    }
193
+
194
+    /**
195
+     * @return int
196
+     * @throws InvalidPathException
197
+     * @throws NotFoundException
198
+     */
199
+    public function getMTime() {
200
+        return $this->getFileInfo()->getMTime();
201
+    }
202
+
203
+    /**
204
+     * @param bool $includeMounts
205
+     * @return int
206
+     * @throws InvalidPathException
207
+     * @throws NotFoundException
208
+     */
209
+    public function getSize($includeMounts = true) {
210
+        return $this->getFileInfo()->getSize($includeMounts);
211
+    }
212
+
213
+    /**
214
+     * @return string
215
+     * @throws InvalidPathException
216
+     * @throws NotFoundException
217
+     */
218
+    public function getEtag() {
219
+        return $this->getFileInfo()->getEtag();
220
+    }
221
+
222
+    /**
223
+     * @return int
224
+     * @throws InvalidPathException
225
+     * @throws NotFoundException
226
+     */
227
+    public function getPermissions() {
228
+        return $this->getFileInfo()->getPermissions();
229
+    }
230
+
231
+    /**
232
+     * @return bool
233
+     * @throws InvalidPathException
234
+     * @throws NotFoundException
235
+     */
236
+    public function isReadable() {
237
+        return $this->getFileInfo()->isReadable();
238
+    }
239
+
240
+    /**
241
+     * @return bool
242
+     * @throws InvalidPathException
243
+     * @throws NotFoundException
244
+     */
245
+    public function isUpdateable() {
246
+        return $this->getFileInfo()->isUpdateable();
247
+    }
248
+
249
+    /**
250
+     * @return bool
251
+     * @throws InvalidPathException
252
+     * @throws NotFoundException
253
+     */
254
+    public function isDeletable() {
255
+        return $this->getFileInfo()->isDeletable();
256
+    }
257
+
258
+    /**
259
+     * @return bool
260
+     * @throws InvalidPathException
261
+     * @throws NotFoundException
262
+     */
263
+    public function isShareable() {
264
+        return $this->getFileInfo()->isShareable();
265
+    }
266
+
267
+    /**
268
+     * @return bool
269
+     * @throws InvalidPathException
270
+     * @throws NotFoundException
271
+     */
272
+    public function isCreatable() {
273
+        return $this->getFileInfo()->isCreatable();
274
+    }
275
+
276
+    /**
277
+     * @return Node
278
+     */
279
+    public function getParent() {
280
+        $newPath = dirname($this->path);
281
+        if ($newPath === '' || $newPath === '.' || $newPath === '/') {
282
+            return $this->root;
283
+        }
284
+        return $this->root->get($newPath);
285
+    }
286
+
287
+    /**
288
+     * @return string
289
+     */
290
+    public function getName() {
291
+        return basename($this->path);
292
+    }
293
+
294
+    /**
295
+     * @param string $path
296
+     * @return string
297
+     */
298
+    protected function normalizePath($path) {
299
+        if ($path === '' or $path === '/') {
300
+            return '/';
301
+        }
302
+        //no windows style slashes
303
+        $path = str_replace('\\', '/', $path);
304
+        //add leading slash
305
+        if ($path[0] !== '/') {
306
+            $path = '/' . $path;
307
+        }
308
+        //remove duplicate slashes
309
+        while (strpos($path, '//') !== false) {
310
+            $path = str_replace('//', '/', $path);
311
+        }
312
+        //remove trailing slash
313
+        $path = rtrim($path, '/');
314
+
315
+        return $path;
316
+    }
317
+
318
+    /**
319
+     * check if the requested path is valid
320
+     *
321
+     * @param string $path
322
+     * @return bool
323
+     */
324
+    public function isValidPath($path) {
325
+        if (!$path || $path[0] !== '/') {
326
+            $path = '/' . $path;
327
+        }
328
+        if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
329
+            return false;
330
+        }
331
+        return true;
332
+    }
333
+
334
+    public function isMounted() {
335
+        return $this->getFileInfo()->isMounted();
336
+    }
337
+
338
+    public function isShared() {
339
+        return $this->getFileInfo()->isShared();
340
+    }
341
+
342
+    public function getMimeType() {
343
+        return $this->getFileInfo()->getMimetype();
344
+    }
345
+
346
+    public function getMimePart() {
347
+        return $this->getFileInfo()->getMimePart();
348
+    }
349
+
350
+    public function getType() {
351
+        return $this->getFileInfo()->getType();
352
+    }
353
+
354
+    public function isEncrypted() {
355
+        return $this->getFileInfo()->isEncrypted();
356
+    }
357
+
358
+    public function getMountPoint() {
359
+        return $this->getFileInfo()->getMountPoint();
360
+    }
361
+
362
+    public function getOwner() {
363
+        return $this->getFileInfo()->getOwner();
364
+    }
365
+
366
+    public function getChecksum() {
367
+    }
368
+
369
+    public function getExtension(): string {
370
+        return $this->getFileInfo()->getExtension();
371
+    }
372
+
373
+    /**
374
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
375
+     * @throws LockedException
376
+     */
377
+    public function lock($type) {
378
+        $this->view->lockFile($this->path, $type);
379
+    }
380
+
381
+    /**
382
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
383
+     * @throws LockedException
384
+     */
385
+    public function changeLock($type) {
386
+        $this->view->changeLock($this->path, $type);
387
+    }
388
+
389
+    /**
390
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
391
+     * @throws LockedException
392
+     */
393
+    public function unlock($type) {
394
+        $this->view->unlockFile($this->path, $type);
395
+    }
396
+
397
+    /**
398
+     * @param string $targetPath
399
+     * @return \OC\Files\Node\Node
400
+     * @throws InvalidPathException
401
+     * @throws NotFoundException
402
+     * @throws NotPermittedException if copy not allowed or failed
403
+     */
404
+    public function copy($targetPath) {
405
+        $targetPath = $this->normalizePath($targetPath);
406
+        $parent = $this->root->get(dirname($targetPath));
407
+        if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
408
+            $nonExisting = $this->createNonExistingNode($targetPath);
409
+            $this->sendHooks(['preCopy'], [$this, $nonExisting]);
410
+            $this->sendHooks(['preWrite'], [$nonExisting]);
411
+            if (!$this->view->copy($this->path, $targetPath)) {
412
+                throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
413
+            }
414
+            $targetNode = $this->root->get($targetPath);
415
+            $this->sendHooks(['postCopy'], [$this, $targetNode]);
416
+            $this->sendHooks(['postWrite'], [$targetNode]);
417
+            return $targetNode;
418
+        } else {
419
+            throw new NotPermittedException('No permission to copy to path ' . $targetPath);
420
+        }
421
+    }
422
+
423
+    /**
424
+     * @param string $targetPath
425
+     * @return \OC\Files\Node\Node
426
+     * @throws InvalidPathException
427
+     * @throws NotFoundException
428
+     * @throws NotPermittedException if move not allowed or failed
429
+     * @throws LockedException
430
+     */
431
+    public function move($targetPath) {
432
+        $targetPath = $this->normalizePath($targetPath);
433
+        $parent = $this->root->get(dirname($targetPath));
434
+        if (
435
+            $parent instanceof Folder and
436
+            $this->isValidPath($targetPath) and
437
+            (
438
+                $parent->isCreatable() ||
439
+                ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount)
440
+            )
441
+        ) {
442
+            $nonExisting = $this->createNonExistingNode($targetPath);
443
+            $this->sendHooks(['preRename'], [$this, $nonExisting]);
444
+            $this->sendHooks(['preWrite'], [$nonExisting]);
445
+            if (!$this->view->rename($this->path, $targetPath)) {
446
+                throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
447
+            }
448
+            $targetNode = $this->root->get($targetPath);
449
+            $this->sendHooks(['postRename'], [$this, $targetNode]);
450
+            $this->sendHooks(['postWrite'], [$targetNode]);
451
+            $this->path = $targetPath;
452
+            return $targetNode;
453
+        } else {
454
+            throw new NotPermittedException('No permission to move to path ' . $targetPath);
455
+        }
456
+    }
457
+
458
+    public function getCreationTime(): int {
459
+        return $this->getFileInfo()->getCreationTime();
460
+    }
461
+
462
+    public function getUploadTime(): int {
463
+        return $this->getFileInfo()->getUploadTime();
464
+    }
465 465
 
466 466
 }
Please login to merge, or discard this patch.