Completed
Pull Request — master (#4001)
by Morris
11:15
created
lib/private/Files/FileInfo.php 1 patch
Indentation   +352 added lines, -352 removed lines patch added patch discarded remove patch
@@ -37,356 +37,356 @@
 block discarded – undo
37 37
 use OCP\IUser;
38 38
 
39 39
 class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
40
-	/**
41
-	 * @var array $data
42
-	 */
43
-	private $data;
44
-
45
-	/**
46
-	 * @var string $path
47
-	 */
48
-	private $path;
49
-
50
-	/**
51
-	 * @var \OC\Files\Storage\Storage $storage
52
-	 */
53
-	private $storage;
54
-
55
-	/**
56
-	 * @var string $internalPath
57
-	 */
58
-	private $internalPath;
59
-
60
-	/**
61
-	 * @var \OCP\Files\Mount\IMountPoint
62
-	 */
63
-	private $mount;
64
-
65
-	/**
66
-	 * @var IUser
67
-	 */
68
-	private $owner;
69
-
70
-	/**
71
-	 * @var string[]
72
-	 */
73
-	private $childEtags = [];
74
-
75
-	/**
76
-	 * @var IMountPoint[]
77
-	 */
78
-	private $subMounts = [];
79
-
80
-	private $subMountsUsed = false;
81
-
82
-	/**
83
-	 * @param string|boolean $path
84
-	 * @param Storage\Storage $storage
85
-	 * @param string $internalPath
86
-	 * @param array|ICacheEntry $data
87
-	 * @param \OCP\Files\Mount\IMountPoint $mount
88
-	 * @param \OCP\IUser|null $owner
89
-	 */
90
-	public function __construct($path, $storage, $internalPath, $data, $mount, $owner= null) {
91
-		$this->path = $path;
92
-		$this->storage = $storage;
93
-		$this->internalPath = $internalPath;
94
-		$this->data = $data;
95
-		$this->mount = $mount;
96
-		$this->owner = $owner;
97
-	}
98
-
99
-	public function offsetSet($offset, $value) {
100
-		$this->data[$offset] = $value;
101
-	}
102
-
103
-	public function offsetExists($offset) {
104
-		return isset($this->data[$offset]);
105
-	}
106
-
107
-	public function offsetUnset($offset) {
108
-		unset($this->data[$offset]);
109
-	}
110
-
111
-	public function offsetGet($offset) {
112
-		if ($offset === 'type') {
113
-			return $this->getType();
114
-		} else if ($offset === 'etag') {
115
-			return $this->getEtag();
116
-		} else if ($offset === 'size') {
117
-			return $this->getSize();
118
-		} else if ($offset === 'mtime') {
119
-			return $this->getMTime();
120
-		} elseif ($offset === 'permissions') {
121
-			return $this->getPermissions();
122
-		} elseif (isset($this->data[$offset])) {
123
-			return $this->data[$offset];
124
-		} else {
125
-			return null;
126
-		}
127
-	}
128
-
129
-	/**
130
-	 * @return string
131
-	 */
132
-	public function getPath() {
133
-		return $this->path;
134
-	}
135
-
136
-	/**
137
-	 * @return \OCP\Files\Storage
138
-	 */
139
-	public function getStorage() {
140
-		return $this->storage;
141
-	}
142
-
143
-	/**
144
-	 * @return string
145
-	 */
146
-	public function getInternalPath() {
147
-		return $this->internalPath;
148
-	}
149
-
150
-	/**
151
-	 * Get FileInfo ID or null in case of part file
152
-	 *
153
-	 * @return int/null
154
-	 */
155
-	public function getId() {
156
-		return isset($this->data['fileid']) ? intval($this->data['fileid']) : null;
157
-	}
158
-
159
-	/**
160
-	 * @return string
161
-	 */
162
-	public function getMimetype() {
163
-		return $this->data['mimetype'];
164
-	}
165
-
166
-	/**
167
-	 * @return string
168
-	 */
169
-	public function getMimePart() {
170
-		return $this->data['mimepart'];
171
-	}
172
-
173
-	/**
174
-	 * @return string
175
-	 */
176
-	public function getName() {
177
-		return basename($this->getPath());
178
-	}
179
-
180
-	/**
181
-	 * @return string
182
-	 */
183
-	public function getEtag() {
184
-		$this->updateEntryfromSubMounts();
185
-		if (count($this->childEtags) > 0) {
186
-			$combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
187
-			return md5($combinedEtag);
188
-		} else {
189
-			return $this->data['etag'];
190
-		}
191
-	}
192
-
193
-	/**
194
-	 * @return int
195
-	 */
196
-	public function getSize() {
197
-		$this->updateEntryfromSubMounts();
198
-		return isset($this->data['size']) ? intval($this->data['size']) : 0;
199
-	}
200
-
201
-	/**
202
-	 * @return int
203
-	 */
204
-	public function getMTime() {
205
-		$this->updateEntryfromSubMounts();
206
-		return intval($this->data['mtime']);
207
-	}
208
-
209
-	/**
210
-	 * @return bool
211
-	 */
212
-	public function isEncrypted() {
213
-		return $this->data['encrypted'];
214
-	}
215
-
216
-	/**
217
-	 * Return the currently version used for the HMAC in the encryption app
218
-	 *
219
-	 * @return int
220
-	 */
221
-	public function getEncryptedVersion() {
222
-		return isset($this->data['encryptedVersion']) ? intval($this->data['encryptedVersion']) : 1;
223
-	}
224
-
225
-	/**
226
-	 * @return int
227
-	 */
228
-	public function getPermissions() {
229
-		$perms = $this->data['permissions'];
230
-		if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) {
231
-			$perms = $perms & ~\OCP\Constants::PERMISSION_SHARE;
232
-		}
233
-		return intval($perms);
234
-	}
235
-
236
-	/**
237
-	 * @return \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
238
-	 */
239
-	public function getType() {
240
-		if (!isset($this->data['type'])) {
241
-			$this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE;
242
-		}
243
-		return $this->data['type'];
244
-	}
245
-
246
-	public function getData() {
247
-		return $this->data;
248
-	}
249
-
250
-	/**
251
-	 * @param int $permissions
252
-	 * @return bool
253
-	 */
254
-	protected function checkPermissions($permissions) {
255
-		return ($this->getPermissions() & $permissions) === $permissions;
256
-	}
257
-
258
-	/**
259
-	 * @return bool
260
-	 */
261
-	public function isReadable() {
262
-		return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
263
-	}
264
-
265
-	/**
266
-	 * @return bool
267
-	 */
268
-	public function isUpdateable() {
269
-		return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
270
-	}
271
-
272
-	/**
273
-	 * Check whether new files or folders can be created inside this folder
274
-	 *
275
-	 * @return bool
276
-	 */
277
-	public function isCreatable() {
278
-		return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
279
-	}
280
-
281
-	/**
282
-	 * @return bool
283
-	 */
284
-	public function isDeletable() {
285
-		return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
286
-	}
287
-
288
-	/**
289
-	 * @return bool
290
-	 */
291
-	public function isShareable() {
292
-		return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
293
-	}
294
-
295
-	/**
296
-	 * Check if a file or folder is shared
297
-	 *
298
-	 * @return bool
299
-	 */
300
-	public function isShared() {
301
-		$sid = $this->getStorage()->getId();
302
-		if (!is_null($sid)) {
303
-			$sid = explode(':', $sid);
304
-			return ($sid[0] === 'shared');
305
-		}
306
-
307
-		return false;
308
-	}
309
-
310
-	public function isMounted() {
311
-		$storage = $this->getStorage();
312
-		if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
313
-			return false;
314
-		}
315
-		$sid = $storage->getId();
316
-		if (!is_null($sid)) {
317
-			$sid = explode(':', $sid);
318
-			return ($sid[0] !== 'home' and $sid[0] !== 'shared');
319
-		}
320
-
321
-		return false;
322
-	}
323
-
324
-	/**
325
-	 * Get the mountpoint the file belongs to
326
-	 *
327
-	 * @return \OCP\Files\Mount\IMountPoint
328
-	 */
329
-	public function getMountPoint() {
330
-		return $this->mount;
331
-	}
332
-
333
-	/**
334
-	 * Get the owner of the file
335
-	 *
336
-	 * @return \OCP\IUser
337
-	 */
338
-	public function getOwner() {
339
-		return $this->owner;
340
-	}
341
-
342
-	/**
343
-	 * @param IMountPoint[] $mounts
344
-	 */
345
-	public function setSubMounts(array $mounts) {
346
-		$this->subMounts = $mounts;
347
-	}
348
-
349
-	private function updateEntryfromSubMounts() {
350
-		if ($this->subMountsUsed) {
351
-			return;
352
-		}
353
-		$this->subMountsUsed = true;
354
-		foreach ($this->subMounts as $mount) {
355
-			$subStorage = $mount->getStorage();
356
-			if ($subStorage) {
357
-				$subCache = $subStorage->getCache('');
358
-				$rootEntry = $subCache->get('');
359
-				$this->addSubEntry($rootEntry, $mount->getMountPoint());
360
-			}
361
-		}
362
-	}
363
-
364
-	/**
365
-	 * Add a cache entry which is the child of this folder
366
-	 *
367
-	 * Sets the size, etag and size to for cross-storage childs
368
-	 *
369
-	 * @param array|ICacheEntry $data cache entry for the child
370
-	 * @param string $entryPath full path of the child entry
371
-	 */
372
-	public function addSubEntry($data, $entryPath) {
373
-		$this->data['size'] += isset($data['size']) ? $data['size'] : 0;
374
-		if (isset($data['mtime'])) {
375
-			$this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
376
-		}
377
-		if (isset($data['etag'])) {
378
-			// prefix the etag with the relative path of the subentry to propagate etag on mount moves
379
-			$relativeEntryPath = substr($entryPath, strlen($this->getPath()));
380
-			// attach the permissions to propagate etag on permision changes of submounts
381
-			$permissions = isset($data['permissions']) ? $data['permissions'] : 0;
382
-			$this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
383
-		}
384
-	}
385
-
386
-	/**
387
-	 * @inheritdoc
388
-	 */
389
-	public function getChecksum() {
390
-		return $this->data['checksum'];
391
-	}
40
+    /**
41
+     * @var array $data
42
+     */
43
+    private $data;
44
+
45
+    /**
46
+     * @var string $path
47
+     */
48
+    private $path;
49
+
50
+    /**
51
+     * @var \OC\Files\Storage\Storage $storage
52
+     */
53
+    private $storage;
54
+
55
+    /**
56
+     * @var string $internalPath
57
+     */
58
+    private $internalPath;
59
+
60
+    /**
61
+     * @var \OCP\Files\Mount\IMountPoint
62
+     */
63
+    private $mount;
64
+
65
+    /**
66
+     * @var IUser
67
+     */
68
+    private $owner;
69
+
70
+    /**
71
+     * @var string[]
72
+     */
73
+    private $childEtags = [];
74
+
75
+    /**
76
+     * @var IMountPoint[]
77
+     */
78
+    private $subMounts = [];
79
+
80
+    private $subMountsUsed = false;
81
+
82
+    /**
83
+     * @param string|boolean $path
84
+     * @param Storage\Storage $storage
85
+     * @param string $internalPath
86
+     * @param array|ICacheEntry $data
87
+     * @param \OCP\Files\Mount\IMountPoint $mount
88
+     * @param \OCP\IUser|null $owner
89
+     */
90
+    public function __construct($path, $storage, $internalPath, $data, $mount, $owner= null) {
91
+        $this->path = $path;
92
+        $this->storage = $storage;
93
+        $this->internalPath = $internalPath;
94
+        $this->data = $data;
95
+        $this->mount = $mount;
96
+        $this->owner = $owner;
97
+    }
98
+
99
+    public function offsetSet($offset, $value) {
100
+        $this->data[$offset] = $value;
101
+    }
102
+
103
+    public function offsetExists($offset) {
104
+        return isset($this->data[$offset]);
105
+    }
106
+
107
+    public function offsetUnset($offset) {
108
+        unset($this->data[$offset]);
109
+    }
110
+
111
+    public function offsetGet($offset) {
112
+        if ($offset === 'type') {
113
+            return $this->getType();
114
+        } else if ($offset === 'etag') {
115
+            return $this->getEtag();
116
+        } else if ($offset === 'size') {
117
+            return $this->getSize();
118
+        } else if ($offset === 'mtime') {
119
+            return $this->getMTime();
120
+        } elseif ($offset === 'permissions') {
121
+            return $this->getPermissions();
122
+        } elseif (isset($this->data[$offset])) {
123
+            return $this->data[$offset];
124
+        } else {
125
+            return null;
126
+        }
127
+    }
128
+
129
+    /**
130
+     * @return string
131
+     */
132
+    public function getPath() {
133
+        return $this->path;
134
+    }
135
+
136
+    /**
137
+     * @return \OCP\Files\Storage
138
+     */
139
+    public function getStorage() {
140
+        return $this->storage;
141
+    }
142
+
143
+    /**
144
+     * @return string
145
+     */
146
+    public function getInternalPath() {
147
+        return $this->internalPath;
148
+    }
149
+
150
+    /**
151
+     * Get FileInfo ID or null in case of part file
152
+     *
153
+     * @return int/null
154
+     */
155
+    public function getId() {
156
+        return isset($this->data['fileid']) ? intval($this->data['fileid']) : null;
157
+    }
158
+
159
+    /**
160
+     * @return string
161
+     */
162
+    public function getMimetype() {
163
+        return $this->data['mimetype'];
164
+    }
165
+
166
+    /**
167
+     * @return string
168
+     */
169
+    public function getMimePart() {
170
+        return $this->data['mimepart'];
171
+    }
172
+
173
+    /**
174
+     * @return string
175
+     */
176
+    public function getName() {
177
+        return basename($this->getPath());
178
+    }
179
+
180
+    /**
181
+     * @return string
182
+     */
183
+    public function getEtag() {
184
+        $this->updateEntryfromSubMounts();
185
+        if (count($this->childEtags) > 0) {
186
+            $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
187
+            return md5($combinedEtag);
188
+        } else {
189
+            return $this->data['etag'];
190
+        }
191
+    }
192
+
193
+    /**
194
+     * @return int
195
+     */
196
+    public function getSize() {
197
+        $this->updateEntryfromSubMounts();
198
+        return isset($this->data['size']) ? intval($this->data['size']) : 0;
199
+    }
200
+
201
+    /**
202
+     * @return int
203
+     */
204
+    public function getMTime() {
205
+        $this->updateEntryfromSubMounts();
206
+        return intval($this->data['mtime']);
207
+    }
208
+
209
+    /**
210
+     * @return bool
211
+     */
212
+    public function isEncrypted() {
213
+        return $this->data['encrypted'];
214
+    }
215
+
216
+    /**
217
+     * Return the currently version used for the HMAC in the encryption app
218
+     *
219
+     * @return int
220
+     */
221
+    public function getEncryptedVersion() {
222
+        return isset($this->data['encryptedVersion']) ? intval($this->data['encryptedVersion']) : 1;
223
+    }
224
+
225
+    /**
226
+     * @return int
227
+     */
228
+    public function getPermissions() {
229
+        $perms = $this->data['permissions'];
230
+        if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) {
231
+            $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE;
232
+        }
233
+        return intval($perms);
234
+    }
235
+
236
+    /**
237
+     * @return \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
238
+     */
239
+    public function getType() {
240
+        if (!isset($this->data['type'])) {
241
+            $this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE;
242
+        }
243
+        return $this->data['type'];
244
+    }
245
+
246
+    public function getData() {
247
+        return $this->data;
248
+    }
249
+
250
+    /**
251
+     * @param int $permissions
252
+     * @return bool
253
+     */
254
+    protected function checkPermissions($permissions) {
255
+        return ($this->getPermissions() & $permissions) === $permissions;
256
+    }
257
+
258
+    /**
259
+     * @return bool
260
+     */
261
+    public function isReadable() {
262
+        return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
263
+    }
264
+
265
+    /**
266
+     * @return bool
267
+     */
268
+    public function isUpdateable() {
269
+        return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
270
+    }
271
+
272
+    /**
273
+     * Check whether new files or folders can be created inside this folder
274
+     *
275
+     * @return bool
276
+     */
277
+    public function isCreatable() {
278
+        return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
279
+    }
280
+
281
+    /**
282
+     * @return bool
283
+     */
284
+    public function isDeletable() {
285
+        return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
286
+    }
287
+
288
+    /**
289
+     * @return bool
290
+     */
291
+    public function isShareable() {
292
+        return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
293
+    }
294
+
295
+    /**
296
+     * Check if a file or folder is shared
297
+     *
298
+     * @return bool
299
+     */
300
+    public function isShared() {
301
+        $sid = $this->getStorage()->getId();
302
+        if (!is_null($sid)) {
303
+            $sid = explode(':', $sid);
304
+            return ($sid[0] === 'shared');
305
+        }
306
+
307
+        return false;
308
+    }
309
+
310
+    public function isMounted() {
311
+        $storage = $this->getStorage();
312
+        if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
313
+            return false;
314
+        }
315
+        $sid = $storage->getId();
316
+        if (!is_null($sid)) {
317
+            $sid = explode(':', $sid);
318
+            return ($sid[0] !== 'home' and $sid[0] !== 'shared');
319
+        }
320
+
321
+        return false;
322
+    }
323
+
324
+    /**
325
+     * Get the mountpoint the file belongs to
326
+     *
327
+     * @return \OCP\Files\Mount\IMountPoint
328
+     */
329
+    public function getMountPoint() {
330
+        return $this->mount;
331
+    }
332
+
333
+    /**
334
+     * Get the owner of the file
335
+     *
336
+     * @return \OCP\IUser
337
+     */
338
+    public function getOwner() {
339
+        return $this->owner;
340
+    }
341
+
342
+    /**
343
+     * @param IMountPoint[] $mounts
344
+     */
345
+    public function setSubMounts(array $mounts) {
346
+        $this->subMounts = $mounts;
347
+    }
348
+
349
+    private function updateEntryfromSubMounts() {
350
+        if ($this->subMountsUsed) {
351
+            return;
352
+        }
353
+        $this->subMountsUsed = true;
354
+        foreach ($this->subMounts as $mount) {
355
+            $subStorage = $mount->getStorage();
356
+            if ($subStorage) {
357
+                $subCache = $subStorage->getCache('');
358
+                $rootEntry = $subCache->get('');
359
+                $this->addSubEntry($rootEntry, $mount->getMountPoint());
360
+            }
361
+        }
362
+    }
363
+
364
+    /**
365
+     * Add a cache entry which is the child of this folder
366
+     *
367
+     * Sets the size, etag and size to for cross-storage childs
368
+     *
369
+     * @param array|ICacheEntry $data cache entry for the child
370
+     * @param string $entryPath full path of the child entry
371
+     */
372
+    public function addSubEntry($data, $entryPath) {
373
+        $this->data['size'] += isset($data['size']) ? $data['size'] : 0;
374
+        if (isset($data['mtime'])) {
375
+            $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
376
+        }
377
+        if (isset($data['etag'])) {
378
+            // prefix the etag with the relative path of the subentry to propagate etag on mount moves
379
+            $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
380
+            // attach the permissions to propagate etag on permision changes of submounts
381
+            $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
382
+            $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
383
+        }
384
+    }
385
+
386
+    /**
387
+     * @inheritdoc
388
+     */
389
+    public function getChecksum() {
390
+        return $this->data['checksum'];
391
+    }
392 392
 }
Please login to merge, or discard this patch.