Passed
Push — master ( 2495e1...f552f2 )
by Roeland
55:59 queued 39:25
created
lib/private/Archive/Archive.php 1 patch
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -31,110 +31,110 @@
 block discarded – undo
31 31
 namespace OC\Archive;
32 32
 
33 33
 abstract class Archive {
34
-	/**
35
-	 * @param $source
36
-	 */
37
-	abstract public function __construct($source);
38
-	/**
39
-	 * add an empty folder to the archive
40
-	 * @param string $path
41
-	 * @return bool
42
-	 */
43
-	abstract public function addFolder($path);
44
-	/**
45
-	 * add a file to the archive
46
-	 * @param string $path
47
-	 * @param string $source either a local file or string data
48
-	 * @return bool
49
-	 */
50
-	abstract public function addFile($path, $source = '');
51
-	/**
52
-	 * rename a file or folder in the archive
53
-	 * @param string $source
54
-	 * @param string $dest
55
-	 * @return bool
56
-	 */
57
-	abstract public function rename($source, $dest);
58
-	/**
59
-	 * get the uncompressed size of a file in the archive
60
-	 * @param string $path
61
-	 * @return int
62
-	 */
63
-	abstract public function filesize($path);
64
-	/**
65
-	 * get the last modified time of a file in the archive
66
-	 * @param string $path
67
-	 * @return int
68
-	 */
69
-	abstract public function mtime($path);
70
-	/**
71
-	 * get the files in a folder
72
-	 * @param string $path
73
-	 * @return array
74
-	 */
75
-	abstract public function getFolder($path);
76
-	/**
77
-	 * get all files in the archive
78
-	 * @return array
79
-	 */
80
-	abstract public function getFiles();
81
-	/**
82
-	 * get the content of a file
83
-	 * @param string $path
84
-	 * @return string
85
-	 */
86
-	abstract public function getFile($path);
87
-	/**
88
-	 * extract a single file from the archive
89
-	 * @param string $path
90
-	 * @param string $dest
91
-	 * @return bool
92
-	 */
93
-	abstract public function extractFile($path, $dest);
94
-	/**
95
-	 * extract the archive
96
-	 * @param string $dest
97
-	 * @return bool
98
-	 */
99
-	abstract public function extract($dest);
100
-	/**
101
-	 * check if a file or folder exists in the archive
102
-	 * @param string $path
103
-	 * @return bool
104
-	 */
105
-	abstract public function fileExists($path);
106
-	/**
107
-	 * remove a file or folder from the archive
108
-	 * @param string $path
109
-	 * @return bool
110
-	 */
111
-	abstract public function remove($path);
112
-	/**
113
-	 * get a file handler
114
-	 * @param string $path
115
-	 * @param string $mode
116
-	 * @return bool|resource
117
-	 */
118
-	abstract public function getStream($path, $mode);
119
-	/**
120
-	 * add a folder and all its content
121
-	 * @param string $path
122
-	 * @param string $source
123
-	 */
124
-	public function addRecursive($path, $source) {
125
-		$dh = opendir($source);
126
-		if (is_resource($dh)) {
127
-			$this->addFolder($path);
128
-			while (($file = readdir($dh)) !== false) {
129
-				if ($file === '.' || $file === '..') {
130
-					continue;
131
-				}
132
-				if (is_dir($source.'/'.$file)) {
133
-					$this->addRecursive($path.'/'.$file, $source.'/'.$file);
134
-				} else {
135
-					$this->addFile($path.'/'.$file, $source.'/'.$file);
136
-				}
137
-			}
138
-		}
139
-	}
34
+    /**
35
+     * @param $source
36
+     */
37
+    abstract public function __construct($source);
38
+    /**
39
+     * add an empty folder to the archive
40
+     * @param string $path
41
+     * @return bool
42
+     */
43
+    abstract public function addFolder($path);
44
+    /**
45
+     * add a file to the archive
46
+     * @param string $path
47
+     * @param string $source either a local file or string data
48
+     * @return bool
49
+     */
50
+    abstract public function addFile($path, $source = '');
51
+    /**
52
+     * rename a file or folder in the archive
53
+     * @param string $source
54
+     * @param string $dest
55
+     * @return bool
56
+     */
57
+    abstract public function rename($source, $dest);
58
+    /**
59
+     * get the uncompressed size of a file in the archive
60
+     * @param string $path
61
+     * @return int
62
+     */
63
+    abstract public function filesize($path);
64
+    /**
65
+     * get the last modified time of a file in the archive
66
+     * @param string $path
67
+     * @return int
68
+     */
69
+    abstract public function mtime($path);
70
+    /**
71
+     * get the files in a folder
72
+     * @param string $path
73
+     * @return array
74
+     */
75
+    abstract public function getFolder($path);
76
+    /**
77
+     * get all files in the archive
78
+     * @return array
79
+     */
80
+    abstract public function getFiles();
81
+    /**
82
+     * get the content of a file
83
+     * @param string $path
84
+     * @return string
85
+     */
86
+    abstract public function getFile($path);
87
+    /**
88
+     * extract a single file from the archive
89
+     * @param string $path
90
+     * @param string $dest
91
+     * @return bool
92
+     */
93
+    abstract public function extractFile($path, $dest);
94
+    /**
95
+     * extract the archive
96
+     * @param string $dest
97
+     * @return bool
98
+     */
99
+    abstract public function extract($dest);
100
+    /**
101
+     * check if a file or folder exists in the archive
102
+     * @param string $path
103
+     * @return bool
104
+     */
105
+    abstract public function fileExists($path);
106
+    /**
107
+     * remove a file or folder from the archive
108
+     * @param string $path
109
+     * @return bool
110
+     */
111
+    abstract public function remove($path);
112
+    /**
113
+     * get a file handler
114
+     * @param string $path
115
+     * @param string $mode
116
+     * @return bool|resource
117
+     */
118
+    abstract public function getStream($path, $mode);
119
+    /**
120
+     * add a folder and all its content
121
+     * @param string $path
122
+     * @param string $source
123
+     */
124
+    public function addRecursive($path, $source) {
125
+        $dh = opendir($source);
126
+        if (is_resource($dh)) {
127
+            $this->addFolder($path);
128
+            while (($file = readdir($dh)) !== false) {
129
+                if ($file === '.' || $file === '..') {
130
+                    continue;
131
+                }
132
+                if (is_dir($source.'/'.$file)) {
133
+                    $this->addRecursive($path.'/'.$file, $source.'/'.$file);
134
+                } else {
135
+                    $this->addFile($path.'/'.$file, $source.'/'.$file);
136
+                }
137
+            }
138
+        }
139
+    }
140 140
 }
Please login to merge, or discard this patch.
lib/private/Archive/TAR.php 1 patch
Indentation   +335 added lines, -335 removed lines patch added patch discarded remove patch
@@ -36,360 +36,360 @@
 block discarded – undo
36 36
 use Icewind\Streams\CallbackWrapper;
37 37
 
38 38
 class TAR extends Archive {
39
-	public const PLAIN = 0;
40
-	public const GZIP = 1;
41
-	public const BZIP = 2;
39
+    public const PLAIN = 0;
40
+    public const GZIP = 1;
41
+    public const BZIP = 2;
42 42
 
43
-	private $fileList;
44
-	private $cachedHeaders;
43
+    private $fileList;
44
+    private $cachedHeaders;
45 45
 
46
-	/**
47
-	 * @var \Archive_Tar tar
48
-	 */
49
-	private $tar = null;
50
-	private $path;
46
+    /**
47
+     * @var \Archive_Tar tar
48
+     */
49
+    private $tar = null;
50
+    private $path;
51 51
 
52
-	/**
53
-	 * @param string $source
54
-	 */
55
-	public function __construct($source) {
56
-		$types = [null, 'gz', 'bz2'];
57
-		$this->path = $source;
58
-		$this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]);
59
-	}
52
+    /**
53
+     * @param string $source
54
+     */
55
+    public function __construct($source) {
56
+        $types = [null, 'gz', 'bz2'];
57
+        $this->path = $source;
58
+        $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]);
59
+    }
60 60
 
61
-	/**
62
-	 * try to detect the type of tar compression
63
-	 *
64
-	 * @param string $file
65
-	 * @return integer
66
-	 */
67
-	public static function getTarType($file) {
68
-		if (strpos($file, '.')) {
69
-			$extension = substr($file, strrpos($file, '.'));
70
-			switch ($extension) {
71
-				case '.gz':
72
-				case '.tgz':
73
-					return self::GZIP;
74
-				case '.bz':
75
-				case '.bz2':
76
-					return self::BZIP;
77
-				case '.tar':
78
-					return self::PLAIN;
79
-				default:
80
-					return self::PLAIN;
81
-			}
82
-		} else {
83
-			return self::PLAIN;
84
-		}
85
-	}
61
+    /**
62
+     * try to detect the type of tar compression
63
+     *
64
+     * @param string $file
65
+     * @return integer
66
+     */
67
+    public static function getTarType($file) {
68
+        if (strpos($file, '.')) {
69
+            $extension = substr($file, strrpos($file, '.'));
70
+            switch ($extension) {
71
+                case '.gz':
72
+                case '.tgz':
73
+                    return self::GZIP;
74
+                case '.bz':
75
+                case '.bz2':
76
+                    return self::BZIP;
77
+                case '.tar':
78
+                    return self::PLAIN;
79
+                default:
80
+                    return self::PLAIN;
81
+            }
82
+        } else {
83
+            return self::PLAIN;
84
+        }
85
+    }
86 86
 
87
-	/**
88
-	 * add an empty folder to the archive
89
-	 *
90
-	 * @param string $path
91
-	 * @return bool
92
-	 */
93
-	public function addFolder($path) {
94
-		$tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
95
-		$path = rtrim($path, '/') . '/';
96
-		if ($this->fileExists($path)) {
97
-			return false;
98
-		}
99
-		$parts = explode('/', $path);
100
-		$folder = $tmpBase;
101
-		foreach ($parts as $part) {
102
-			$folder .= '/' . $part;
103
-			if (!is_dir($folder)) {
104
-				mkdir($folder);
105
-			}
106
-		}
107
-		$result = $this->tar->addModify([$tmpBase . $path], '', $tmpBase);
108
-		rmdir($tmpBase . $path);
109
-		$this->fileList = false;
110
-		$this->cachedHeaders = false;
111
-		return $result;
112
-	}
87
+    /**
88
+     * add an empty folder to the archive
89
+     *
90
+     * @param string $path
91
+     * @return bool
92
+     */
93
+    public function addFolder($path) {
94
+        $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
95
+        $path = rtrim($path, '/') . '/';
96
+        if ($this->fileExists($path)) {
97
+            return false;
98
+        }
99
+        $parts = explode('/', $path);
100
+        $folder = $tmpBase;
101
+        foreach ($parts as $part) {
102
+            $folder .= '/' . $part;
103
+            if (!is_dir($folder)) {
104
+                mkdir($folder);
105
+            }
106
+        }
107
+        $result = $this->tar->addModify([$tmpBase . $path], '', $tmpBase);
108
+        rmdir($tmpBase . $path);
109
+        $this->fileList = false;
110
+        $this->cachedHeaders = false;
111
+        return $result;
112
+    }
113 113
 
114
-	/**
115
-	 * add a file to the archive
116
-	 *
117
-	 * @param string $path
118
-	 * @param string $source either a local file or string data
119
-	 * @return bool
120
-	 */
121
-	public function addFile($path, $source = '') {
122
-		if ($this->fileExists($path)) {
123
-			$this->remove($path);
124
-		}
125
-		if ($source and $source[0] == '/' and file_exists($source)) {
126
-			$source = file_get_contents($source);
127
-		}
128
-		$result = $this->tar->addString($path, $source);
129
-		$this->fileList = false;
130
-		$this->cachedHeaders = false;
131
-		return $result;
132
-	}
114
+    /**
115
+     * add a file to the archive
116
+     *
117
+     * @param string $path
118
+     * @param string $source either a local file or string data
119
+     * @return bool
120
+     */
121
+    public function addFile($path, $source = '') {
122
+        if ($this->fileExists($path)) {
123
+            $this->remove($path);
124
+        }
125
+        if ($source and $source[0] == '/' and file_exists($source)) {
126
+            $source = file_get_contents($source);
127
+        }
128
+        $result = $this->tar->addString($path, $source);
129
+        $this->fileList = false;
130
+        $this->cachedHeaders = false;
131
+        return $result;
132
+    }
133 133
 
134
-	/**
135
-	 * rename a file or folder in the archive
136
-	 *
137
-	 * @param string $source
138
-	 * @param string $dest
139
-	 * @return bool
140
-	 */
141
-	public function rename($source, $dest) {
142
-		//no proper way to delete, rename entire archive, rename file and remake archive
143
-		$tmp = \OC::$server->getTempManager()->getTemporaryFolder();
144
-		$this->tar->extract($tmp);
145
-		rename($tmp . $source, $tmp . $dest);
146
-		$this->tar = null;
147
-		unlink($this->path);
148
-		$types = [null, 'gz', 'bz'];
149
-		$this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
150
-		$this->tar->createModify([$tmp], '', $tmp . '/');
151
-		$this->fileList = false;
152
-		$this->cachedHeaders = false;
153
-		return true;
154
-	}
134
+    /**
135
+     * rename a file or folder in the archive
136
+     *
137
+     * @param string $source
138
+     * @param string $dest
139
+     * @return bool
140
+     */
141
+    public function rename($source, $dest) {
142
+        //no proper way to delete, rename entire archive, rename file and remake archive
143
+        $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
144
+        $this->tar->extract($tmp);
145
+        rename($tmp . $source, $tmp . $dest);
146
+        $this->tar = null;
147
+        unlink($this->path);
148
+        $types = [null, 'gz', 'bz'];
149
+        $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
150
+        $this->tar->createModify([$tmp], '', $tmp . '/');
151
+        $this->fileList = false;
152
+        $this->cachedHeaders = false;
153
+        return true;
154
+    }
155 155
 
156
-	/**
157
-	 * @param string $file
158
-	 */
159
-	private function getHeader($file) {
160
-		if (!$this->cachedHeaders) {
161
-			$this->cachedHeaders = $this->tar->listContent();
162
-		}
163
-		foreach ($this->cachedHeaders as $header) {
164
-			if ($file == $header['filename']
165
-				or $file . '/' == $header['filename']
166
-				or '/' . $file . '/' == $header['filename']
167
-				or '/' . $file == $header['filename']
168
-			) {
169
-				return $header;
170
-			}
171
-		}
172
-		return null;
173
-	}
156
+    /**
157
+     * @param string $file
158
+     */
159
+    private function getHeader($file) {
160
+        if (!$this->cachedHeaders) {
161
+            $this->cachedHeaders = $this->tar->listContent();
162
+        }
163
+        foreach ($this->cachedHeaders as $header) {
164
+            if ($file == $header['filename']
165
+                or $file . '/' == $header['filename']
166
+                or '/' . $file . '/' == $header['filename']
167
+                or '/' . $file == $header['filename']
168
+            ) {
169
+                return $header;
170
+            }
171
+        }
172
+        return null;
173
+    }
174 174
 
175
-	/**
176
-	 * get the uncompressed size of a file in the archive
177
-	 *
178
-	 * @param string $path
179
-	 * @return int
180
-	 */
181
-	public function filesize($path) {
182
-		$stat = $this->getHeader($path);
183
-		return $stat['size'];
184
-	}
175
+    /**
176
+     * get the uncompressed size of a file in the archive
177
+     *
178
+     * @param string $path
179
+     * @return int
180
+     */
181
+    public function filesize($path) {
182
+        $stat = $this->getHeader($path);
183
+        return $stat['size'];
184
+    }
185 185
 
186
-	/**
187
-	 * get the last modified time of a file in the archive
188
-	 *
189
-	 * @param string $path
190
-	 * @return int
191
-	 */
192
-	public function mtime($path) {
193
-		$stat = $this->getHeader($path);
194
-		return $stat['mtime'];
195
-	}
186
+    /**
187
+     * get the last modified time of a file in the archive
188
+     *
189
+     * @param string $path
190
+     * @return int
191
+     */
192
+    public function mtime($path) {
193
+        $stat = $this->getHeader($path);
194
+        return $stat['mtime'];
195
+    }
196 196
 
197
-	/**
198
-	 * get the files in a folder
199
-	 *
200
-	 * @param string $path
201
-	 * @return array
202
-	 */
203
-	public function getFolder($path) {
204
-		$files = $this->getFiles();
205
-		$folderContent = [];
206
-		$pathLength = strlen($path);
207
-		foreach ($files as $file) {
208
-			if ($file[0] == '/') {
209
-				$file = substr($file, 1);
210
-			}
211
-			if (substr($file, 0, $pathLength) == $path and $file != $path) {
212
-				$result = substr($file, $pathLength);
213
-				if ($pos = strpos($result, '/')) {
214
-					$result = substr($result, 0, $pos + 1);
215
-				}
216
-				if (array_search($result, $folderContent) === false) {
217
-					$folderContent[] = $result;
218
-				}
219
-			}
220
-		}
221
-		return $folderContent;
222
-	}
197
+    /**
198
+     * get the files in a folder
199
+     *
200
+     * @param string $path
201
+     * @return array
202
+     */
203
+    public function getFolder($path) {
204
+        $files = $this->getFiles();
205
+        $folderContent = [];
206
+        $pathLength = strlen($path);
207
+        foreach ($files as $file) {
208
+            if ($file[0] == '/') {
209
+                $file = substr($file, 1);
210
+            }
211
+            if (substr($file, 0, $pathLength) == $path and $file != $path) {
212
+                $result = substr($file, $pathLength);
213
+                if ($pos = strpos($result, '/')) {
214
+                    $result = substr($result, 0, $pos + 1);
215
+                }
216
+                if (array_search($result, $folderContent) === false) {
217
+                    $folderContent[] = $result;
218
+                }
219
+            }
220
+        }
221
+        return $folderContent;
222
+    }
223 223
 
224
-	/**
225
-	 * get all files in the archive
226
-	 *
227
-	 * @return array
228
-	 */
229
-	public function getFiles() {
230
-		if ($this->fileList) {
231
-			return $this->fileList;
232
-		}
233
-		if (!$this->cachedHeaders) {
234
-			$this->cachedHeaders = $this->tar->listContent();
235
-		}
236
-		$files = [];
237
-		foreach ($this->cachedHeaders as $header) {
238
-			$files[] = $header['filename'];
239
-		}
240
-		$this->fileList = $files;
241
-		return $files;
242
-	}
224
+    /**
225
+     * get all files in the archive
226
+     *
227
+     * @return array
228
+     */
229
+    public function getFiles() {
230
+        if ($this->fileList) {
231
+            return $this->fileList;
232
+        }
233
+        if (!$this->cachedHeaders) {
234
+            $this->cachedHeaders = $this->tar->listContent();
235
+        }
236
+        $files = [];
237
+        foreach ($this->cachedHeaders as $header) {
238
+            $files[] = $header['filename'];
239
+        }
240
+        $this->fileList = $files;
241
+        return $files;
242
+    }
243 243
 
244
-	/**
245
-	 * get the content of a file
246
-	 *
247
-	 * @param string $path
248
-	 * @return string
249
-	 */
250
-	public function getFile($path) {
251
-		return $this->tar->extractInString($path);
252
-	}
244
+    /**
245
+     * get the content of a file
246
+     *
247
+     * @param string $path
248
+     * @return string
249
+     */
250
+    public function getFile($path) {
251
+        return $this->tar->extractInString($path);
252
+    }
253 253
 
254
-	/**
255
-	 * extract a single file from the archive
256
-	 *
257
-	 * @param string $path
258
-	 * @param string $dest
259
-	 * @return bool
260
-	 */
261
-	public function extractFile($path, $dest) {
262
-		$tmp = \OC::$server->getTempManager()->getTemporaryFolder();
263
-		if (!$this->fileExists($path)) {
264
-			return false;
265
-		}
266
-		if ($this->fileExists('/' . $path)) {
267
-			$success = $this->tar->extractList(['/' . $path], $tmp);
268
-		} else {
269
-			$success = $this->tar->extractList([$path], $tmp);
270
-		}
271
-		if ($success) {
272
-			rename($tmp . $path, $dest);
273
-		}
274
-		\OCP\Files::rmdirr($tmp);
275
-		return $success;
276
-	}
254
+    /**
255
+     * extract a single file from the archive
256
+     *
257
+     * @param string $path
258
+     * @param string $dest
259
+     * @return bool
260
+     */
261
+    public function extractFile($path, $dest) {
262
+        $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
263
+        if (!$this->fileExists($path)) {
264
+            return false;
265
+        }
266
+        if ($this->fileExists('/' . $path)) {
267
+            $success = $this->tar->extractList(['/' . $path], $tmp);
268
+        } else {
269
+            $success = $this->tar->extractList([$path], $tmp);
270
+        }
271
+        if ($success) {
272
+            rename($tmp . $path, $dest);
273
+        }
274
+        \OCP\Files::rmdirr($tmp);
275
+        return $success;
276
+    }
277 277
 
278
-	/**
279
-	 * extract the archive
280
-	 *
281
-	 * @param string $dest
282
-	 * @return bool
283
-	 */
284
-	public function extract($dest) {
285
-		return $this->tar->extract($dest);
286
-	}
278
+    /**
279
+     * extract the archive
280
+     *
281
+     * @param string $dest
282
+     * @return bool
283
+     */
284
+    public function extract($dest) {
285
+        return $this->tar->extract($dest);
286
+    }
287 287
 
288
-	/**
289
-	 * check if a file or folder exists in the archive
290
-	 *
291
-	 * @param string $path
292
-	 * @return bool
293
-	 */
294
-	public function fileExists($path) {
295
-		$files = $this->getFiles();
296
-		if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) {
297
-			return true;
298
-		} else {
299
-			$folderPath = rtrim($path, '/') . '/';
300
-			$pathLength = strlen($folderPath);
301
-			foreach ($files as $file) {
302
-				if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
303
-					return true;
304
-				}
305
-			}
306
-		}
307
-		if ($path[0] != '/') { //not all programs agree on the use of a leading /
308
-			return $this->fileExists('/' . $path);
309
-		} else {
310
-			return false;
311
-		}
312
-	}
288
+    /**
289
+     * check if a file or folder exists in the archive
290
+     *
291
+     * @param string $path
292
+     * @return bool
293
+     */
294
+    public function fileExists($path) {
295
+        $files = $this->getFiles();
296
+        if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) {
297
+            return true;
298
+        } else {
299
+            $folderPath = rtrim($path, '/') . '/';
300
+            $pathLength = strlen($folderPath);
301
+            foreach ($files as $file) {
302
+                if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
303
+                    return true;
304
+                }
305
+            }
306
+        }
307
+        if ($path[0] != '/') { //not all programs agree on the use of a leading /
308
+            return $this->fileExists('/' . $path);
309
+        } else {
310
+            return false;
311
+        }
312
+    }
313 313
 
314
-	/**
315
-	 * remove a file or folder from the archive
316
-	 *
317
-	 * @param string $path
318
-	 * @return bool
319
-	 */
320
-	public function remove($path) {
321
-		if (!$this->fileExists($path)) {
322
-			return false;
323
-		}
324
-		$this->fileList = false;
325
-		$this->cachedHeaders = false;
326
-		//no proper way to delete, extract entire archive, delete file and remake archive
327
-		$tmp = \OC::$server->getTempManager()->getTemporaryFolder();
328
-		$this->tar->extract($tmp);
329
-		\OCP\Files::rmdirr($tmp . $path);
330
-		$this->tar = null;
331
-		unlink($this->path);
332
-		$this->reopen();
333
-		$this->tar->createModify([$tmp], '', $tmp);
334
-		return true;
335
-	}
314
+    /**
315
+     * remove a file or folder from the archive
316
+     *
317
+     * @param string $path
318
+     * @return bool
319
+     */
320
+    public function remove($path) {
321
+        if (!$this->fileExists($path)) {
322
+            return false;
323
+        }
324
+        $this->fileList = false;
325
+        $this->cachedHeaders = false;
326
+        //no proper way to delete, extract entire archive, delete file and remake archive
327
+        $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
328
+        $this->tar->extract($tmp);
329
+        \OCP\Files::rmdirr($tmp . $path);
330
+        $this->tar = null;
331
+        unlink($this->path);
332
+        $this->reopen();
333
+        $this->tar->createModify([$tmp], '', $tmp);
334
+        return true;
335
+    }
336 336
 
337
-	/**
338
-	 * get a file handler
339
-	 *
340
-	 * @param string $path
341
-	 * @param string $mode
342
-	 * @return bool|resource
343
-	 */
344
-	public function getStream($path, $mode) {
345
-		if (strrpos($path, '.') !== false) {
346
-			$ext = substr($path, strrpos($path, '.'));
347
-		} else {
348
-			$ext = '';
349
-		}
350
-		$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
351
-		if ($this->fileExists($path)) {
352
-			$this->extractFile($path, $tmpFile);
353
-		} elseif ($mode == 'r' or $mode == 'rb') {
354
-			return false;
355
-		}
356
-		if ($mode == 'r' or $mode == 'rb') {
357
-			return fopen($tmpFile, $mode);
358
-		} else {
359
-			$handle = fopen($tmpFile, $mode);
360
-			return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
361
-				$this->writeBack($tmpFile, $path);
362
-			});
363
-		}
364
-	}
337
+    /**
338
+     * get a file handler
339
+     *
340
+     * @param string $path
341
+     * @param string $mode
342
+     * @return bool|resource
343
+     */
344
+    public function getStream($path, $mode) {
345
+        if (strrpos($path, '.') !== false) {
346
+            $ext = substr($path, strrpos($path, '.'));
347
+        } else {
348
+            $ext = '';
349
+        }
350
+        $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
351
+        if ($this->fileExists($path)) {
352
+            $this->extractFile($path, $tmpFile);
353
+        } elseif ($mode == 'r' or $mode == 'rb') {
354
+            return false;
355
+        }
356
+        if ($mode == 'r' or $mode == 'rb') {
357
+            return fopen($tmpFile, $mode);
358
+        } else {
359
+            $handle = fopen($tmpFile, $mode);
360
+            return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
361
+                $this->writeBack($tmpFile, $path);
362
+            });
363
+        }
364
+    }
365 365
 
366
-	/**
367
-	 * write back temporary files
368
-	 */
369
-	public function writeBack($tmpFile, $path) {
370
-		$this->addFile($path, $tmpFile);
371
-		unlink($tmpFile);
372
-	}
366
+    /**
367
+     * write back temporary files
368
+     */
369
+    public function writeBack($tmpFile, $path) {
370
+        $this->addFile($path, $tmpFile);
371
+        unlink($tmpFile);
372
+    }
373 373
 
374
-	/**
375
-	 * reopen the archive to ensure everything is written
376
-	 */
377
-	private function reopen() {
378
-		if ($this->tar) {
379
-			$this->tar->_close();
380
-			$this->tar = null;
381
-		}
382
-		$types = [null, 'gz', 'bz'];
383
-		$this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
384
-	}
374
+    /**
375
+     * reopen the archive to ensure everything is written
376
+     */
377
+    private function reopen() {
378
+        if ($this->tar) {
379
+            $this->tar->_close();
380
+            $this->tar = null;
381
+        }
382
+        $types = [null, 'gz', 'bz'];
383
+        $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
384
+    }
385 385
 
386
-	/**
387
-	 * Get error object from archive_tar.
388
-	 */
389
-	public function getError(): ?\PEAR_Error {
390
-		if ($this->tar instanceof \Archive_Tar && $this->tar->error_object instanceof \PEAR_Error) {
391
-			return $this->tar->error_object;
392
-		}
393
-		return null;
394
-	}
386
+    /**
387
+     * Get error object from archive_tar.
388
+     */
389
+    public function getError(): ?\PEAR_Error {
390
+        if ($this->tar instanceof \Archive_Tar && $this->tar->error_object instanceof \PEAR_Error) {
391
+            return $this->tar->error_object;
392
+        }
393
+        return null;
394
+    }
395 395
 }
Please login to merge, or discard this patch.
lib/private/Archive/ZIP.php 1 patch
Indentation   +192 added lines, -192 removed lines patch added patch discarded remove patch
@@ -36,199 +36,199 @@
 block discarded – undo
36 36
 use OCP\ILogger;
37 37
 
38 38
 class ZIP extends Archive {
39
-	/**
40
-	 * @var \ZipArchive zip
41
-	 */
42
-	private $zip = null;
43
-	private $path;
39
+    /**
40
+     * @var \ZipArchive zip
41
+     */
42
+    private $zip = null;
43
+    private $path;
44 44
 
45
-	/**
46
-	 * @param string $source
47
-	 */
48
-	public function __construct($source) {
49
-		$this->path = $source;
50
-		$this->zip = new \ZipArchive();
51
-		if ($this->zip->open($source, \ZipArchive::CREATE)) {
52
-		} else {
53
-			\OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
54
-		}
55
-	}
56
-	/**
57
-	 * add an empty folder to the archive
58
-	 * @param string $path
59
-	 * @return bool
60
-	 */
61
-	public function addFolder($path) {
62
-		return $this->zip->addEmptyDir($path);
63
-	}
64
-	/**
65
-	 * add a file to the archive
66
-	 * @param string $path
67
-	 * @param string $source either a local file or string data
68
-	 * @return bool
69
-	 */
70
-	public function addFile($path, $source = '') {
71
-		if ($source and $source[0] == '/' and file_exists($source)) {
72
-			$result = $this->zip->addFile($source, $path);
73
-		} else {
74
-			$result = $this->zip->addFromString($path, $source);
75
-		}
76
-		if ($result) {
77
-			$this->zip->close();//close and reopen to save the zip
78
-			$this->zip->open($this->path);
79
-		}
80
-		return $result;
81
-	}
82
-	/**
83
-	 * rename a file or folder in the archive
84
-	 * @param string $source
85
-	 * @param string $dest
86
-	 * @return boolean|null
87
-	 */
88
-	public function rename($source, $dest) {
89
-		$source = $this->stripPath($source);
90
-		$dest = $this->stripPath($dest);
91
-		$this->zip->renameName($source, $dest);
92
-	}
93
-	/**
94
-	 * get the uncompressed size of a file in the archive
95
-	 * @param string $path
96
-	 * @return int
97
-	 */
98
-	public function filesize($path) {
99
-		$stat = $this->zip->statName($path);
100
-		return $stat['size'];
101
-	}
102
-	/**
103
-	 * get the last modified time of a file in the archive
104
-	 * @param string $path
105
-	 * @return int
106
-	 */
107
-	public function mtime($path) {
108
-		return filemtime($this->path);
109
-	}
110
-	/**
111
-	 * get the files in a folder
112
-	 * @param string $path
113
-	 * @return array
114
-	 */
115
-	public function getFolder($path) {
116
-		$files = $this->getFiles();
117
-		$folderContent = [];
118
-		$pathLength = strlen($path);
119
-		foreach ($files as $file) {
120
-			if (substr($file, 0, $pathLength) == $path and $file != $path) {
121
-				if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
122
-					$folderContent[] = substr($file, $pathLength);
123
-				}
124
-			}
125
-		}
126
-		return $folderContent;
127
-	}
128
-	/**
129
-	 * get all files in the archive
130
-	 * @return array
131
-	 */
132
-	public function getFiles() {
133
-		$fileCount = $this->zip->numFiles;
134
-		$files = [];
135
-		for ($i = 0;$i < $fileCount;$i++) {
136
-			$files[] = $this->zip->getNameIndex($i);
137
-		}
138
-		return $files;
139
-	}
140
-	/**
141
-	 * get the content of a file
142
-	 * @param string $path
143
-	 * @return string
144
-	 */
145
-	public function getFile($path) {
146
-		return $this->zip->getFromName($path);
147
-	}
148
-	/**
149
-	 * extract a single file from the archive
150
-	 * @param string $path
151
-	 * @param string $dest
152
-	 * @return boolean|null
153
-	 */
154
-	public function extractFile($path, $dest) {
155
-		$fp = $this->zip->getStream($path);
156
-		file_put_contents($dest, $fp);
157
-	}
158
-	/**
159
-	 * extract the archive
160
-	 * @param string $dest
161
-	 * @return bool
162
-	 */
163
-	public function extract($dest) {
164
-		return $this->zip->extractTo($dest);
165
-	}
166
-	/**
167
-	 * check if a file or folder exists in the archive
168
-	 * @param string $path
169
-	 * @return bool
170
-	 */
171
-	public function fileExists($path) {
172
-		return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
173
-	}
174
-	/**
175
-	 * remove a file or folder from the archive
176
-	 * @param string $path
177
-	 * @return bool
178
-	 */
179
-	public function remove($path) {
180
-		if ($this->fileExists($path.'/')) {
181
-			return $this->zip->deleteName($path.'/');
182
-		} else {
183
-			return $this->zip->deleteName($path);
184
-		}
185
-	}
186
-	/**
187
-	 * get a file handler
188
-	 * @param string $path
189
-	 * @param string $mode
190
-	 * @return bool|resource
191
-	 */
192
-	public function getStream($path, $mode) {
193
-		if ($mode == 'r' or $mode == 'rb') {
194
-			return $this->zip->getStream($path);
195
-		} else {
196
-			//since we can't directly get a writable stream,
197
-			//make a temp copy of the file and put it back
198
-			//in the archive when the stream is closed
199
-			if (strrpos($path, '.') !== false) {
200
-				$ext = substr($path, strrpos($path, '.'));
201
-			} else {
202
-				$ext = '';
203
-			}
204
-			$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
205
-			if ($this->fileExists($path)) {
206
-				$this->extractFile($path, $tmpFile);
207
-			}
208
-			$handle = fopen($tmpFile, $mode);
209
-			return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
210
-				$this->writeBack($tmpFile, $path);
211
-			});
212
-		}
213
-	}
45
+    /**
46
+     * @param string $source
47
+     */
48
+    public function __construct($source) {
49
+        $this->path = $source;
50
+        $this->zip = new \ZipArchive();
51
+        if ($this->zip->open($source, \ZipArchive::CREATE)) {
52
+        } else {
53
+            \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
54
+        }
55
+    }
56
+    /**
57
+     * add an empty folder to the archive
58
+     * @param string $path
59
+     * @return bool
60
+     */
61
+    public function addFolder($path) {
62
+        return $this->zip->addEmptyDir($path);
63
+    }
64
+    /**
65
+     * add a file to the archive
66
+     * @param string $path
67
+     * @param string $source either a local file or string data
68
+     * @return bool
69
+     */
70
+    public function addFile($path, $source = '') {
71
+        if ($source and $source[0] == '/' and file_exists($source)) {
72
+            $result = $this->zip->addFile($source, $path);
73
+        } else {
74
+            $result = $this->zip->addFromString($path, $source);
75
+        }
76
+        if ($result) {
77
+            $this->zip->close();//close and reopen to save the zip
78
+            $this->zip->open($this->path);
79
+        }
80
+        return $result;
81
+    }
82
+    /**
83
+     * rename a file or folder in the archive
84
+     * @param string $source
85
+     * @param string $dest
86
+     * @return boolean|null
87
+     */
88
+    public function rename($source, $dest) {
89
+        $source = $this->stripPath($source);
90
+        $dest = $this->stripPath($dest);
91
+        $this->zip->renameName($source, $dest);
92
+    }
93
+    /**
94
+     * get the uncompressed size of a file in the archive
95
+     * @param string $path
96
+     * @return int
97
+     */
98
+    public function filesize($path) {
99
+        $stat = $this->zip->statName($path);
100
+        return $stat['size'];
101
+    }
102
+    /**
103
+     * get the last modified time of a file in the archive
104
+     * @param string $path
105
+     * @return int
106
+     */
107
+    public function mtime($path) {
108
+        return filemtime($this->path);
109
+    }
110
+    /**
111
+     * get the files in a folder
112
+     * @param string $path
113
+     * @return array
114
+     */
115
+    public function getFolder($path) {
116
+        $files = $this->getFiles();
117
+        $folderContent = [];
118
+        $pathLength = strlen($path);
119
+        foreach ($files as $file) {
120
+            if (substr($file, 0, $pathLength) == $path and $file != $path) {
121
+                if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
122
+                    $folderContent[] = substr($file, $pathLength);
123
+                }
124
+            }
125
+        }
126
+        return $folderContent;
127
+    }
128
+    /**
129
+     * get all files in the archive
130
+     * @return array
131
+     */
132
+    public function getFiles() {
133
+        $fileCount = $this->zip->numFiles;
134
+        $files = [];
135
+        for ($i = 0;$i < $fileCount;$i++) {
136
+            $files[] = $this->zip->getNameIndex($i);
137
+        }
138
+        return $files;
139
+    }
140
+    /**
141
+     * get the content of a file
142
+     * @param string $path
143
+     * @return string
144
+     */
145
+    public function getFile($path) {
146
+        return $this->zip->getFromName($path);
147
+    }
148
+    /**
149
+     * extract a single file from the archive
150
+     * @param string $path
151
+     * @param string $dest
152
+     * @return boolean|null
153
+     */
154
+    public function extractFile($path, $dest) {
155
+        $fp = $this->zip->getStream($path);
156
+        file_put_contents($dest, $fp);
157
+    }
158
+    /**
159
+     * extract the archive
160
+     * @param string $dest
161
+     * @return bool
162
+     */
163
+    public function extract($dest) {
164
+        return $this->zip->extractTo($dest);
165
+    }
166
+    /**
167
+     * check if a file or folder exists in the archive
168
+     * @param string $path
169
+     * @return bool
170
+     */
171
+    public function fileExists($path) {
172
+        return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
173
+    }
174
+    /**
175
+     * remove a file or folder from the archive
176
+     * @param string $path
177
+     * @return bool
178
+     */
179
+    public function remove($path) {
180
+        if ($this->fileExists($path.'/')) {
181
+            return $this->zip->deleteName($path.'/');
182
+        } else {
183
+            return $this->zip->deleteName($path);
184
+        }
185
+    }
186
+    /**
187
+     * get a file handler
188
+     * @param string $path
189
+     * @param string $mode
190
+     * @return bool|resource
191
+     */
192
+    public function getStream($path, $mode) {
193
+        if ($mode == 'r' or $mode == 'rb') {
194
+            return $this->zip->getStream($path);
195
+        } else {
196
+            //since we can't directly get a writable stream,
197
+            //make a temp copy of the file and put it back
198
+            //in the archive when the stream is closed
199
+            if (strrpos($path, '.') !== false) {
200
+                $ext = substr($path, strrpos($path, '.'));
201
+            } else {
202
+                $ext = '';
203
+            }
204
+            $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
205
+            if ($this->fileExists($path)) {
206
+                $this->extractFile($path, $tmpFile);
207
+            }
208
+            $handle = fopen($tmpFile, $mode);
209
+            return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
210
+                $this->writeBack($tmpFile, $path);
211
+            });
212
+        }
213
+    }
214 214
 
215
-	/**
216
-	 * write back temporary files
217
-	 */
218
-	public function writeBack($tmpFile, $path) {
219
-		$this->addFile($path, $tmpFile);
220
-		unlink($tmpFile);
221
-	}
215
+    /**
216
+     * write back temporary files
217
+     */
218
+    public function writeBack($tmpFile, $path) {
219
+        $this->addFile($path, $tmpFile);
220
+        unlink($tmpFile);
221
+    }
222 222
 
223
-	/**
224
-	 * @param string $path
225
-	 * @return string
226
-	 */
227
-	private function stripPath($path) {
228
-		if (!$path || $path[0] == '/') {
229
-			return substr($path, 1);
230
-		} else {
231
-			return $path;
232
-		}
233
-	}
223
+    /**
224
+     * @param string $path
225
+     * @return string
226
+     */
227
+    private function stripPath($path) {
228
+        if (!$path || $path[0] == '/') {
229
+            return substr($path, 1);
230
+        } else {
231
+            return $path;
232
+        }
233
+    }
234 234
 }
Please login to merge, or discard this patch.