-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathReader.php
224 lines (186 loc) · 5.01 KB
/
Reader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace Mishak\ArchiveTar;
class Reader implements \Iterator {
private $compression;
private $filename;
public function __construct($filename, $compression = self::DETECT)
{
$this->filename = $filename;
if ($compression === self::DETECT) {
$this->detectCompression();
} elseif (is_string($this->compression)) {
$this->compression = $compression;
} else {
throw new \InvalidArgumentException;
}
$this->open();
}
public function __destruct()
{
$this->close();
}
const GZIP = 'gz',
BZIP2 = 'bz2',
NONE = 'none',
DETECT = null;
private function detectCompression()
{
if (preg_match('/\.(tar\.gz|tgz)$/', $this->filename)) {
$this->compression = self::GZIP;
} elseif (preg_match('/\.(tar\.bz2|tbz|tb2|tbz2)$/', $this->filename)) {
$this->compression = self::BZIP2;
} elseif (preg_match('/\.tar$/', $this->filename)) {
$this->compression = self::NONE;
} else {
throw new ReaderException("Unsupported compression '$this->filename'.");
}
}
const MANIPULATION_OPEN = 0,
MANIPULATION_CLOSE = 1;
private $manipulation = array(
self::GZIP => array('gzopen', 'gzclose'),
self::BZIP2 => array('bzopen', 'bzclose'),
self::NONE => array('fopen', 'fclose'),
);
private $file;
private function open()
{
$this->file = $this->manipulation[$this->compression][self::MANIPULATION_OPEN]($this->filename, 'rb');
if (!$this->file) {
throw new ReaderException("Cannot open file '$this->filename'.");
}
}
private function close()
{
$this->manipulation[$this->compression][self::MANIPULATION_CLOSE]($this->file);
$this->file = NULL;
}
private $record;
private $index;
public function current()
{
return $this->record;
}
public function key()
{
return $this->index;
}
public function next()
{
return $this->record = $this->readRecord();
}
public function rewind()
{
fseek($this->file, 0);
$this->next();
}
public function valid()
{
return $this->record !== NULL;
}
private $readContents = TRUE;
/**
* Sets read mode of file contents.
*
* @param bool|callback $readContents
* FALSE disables reading of contents
* TRUE will read all contents and return them with record
* callback should expects parameters $record, $chunk, $bytesLeft and $bytesRead. Chunk will be of size of buffer or smaller.
*/
public function setReadContents($readContents = TRUE)
{
$this->readContents = $readContents;
}
private $buffer = 8195;
/**
* Sets buffer size for reading file contents
*
* @param int
*/
public function setBuffer($buffer)
{
if (0 < $buffer && $buffer <= PHP_INT_MAX) {
$this->buffer = $buffer;
} else {
throw new ReaderException("Buffer must be greater than 0 and less or equal to " . PHP_INT_MAX . " (PHP_INT_MAX).");
}
}
const REGULAR = '0',
AREGULAR = "\0", // backward compatibility
HARDLINK = '1',
SYMLINK = '2',
CHARACTER = '2',
BLOCK = '2',
DIRECTORY = '2',
FIFO = '2',
CONTIGUOUS = '2',
GLOBALHEADER = 'g',
EXTENDEDHEADER = 'x',
LONGLINK = 'K';
private function readRecord()
{
$block = $this->readBlock();
$this->index++;
return $block;
}
const BLOCK_SIZE = 512;
private function readBlock()
{
if (feof($this->file)) {
return NULL;
} else {
$header = fread($this->file, self::BLOCK_SIZE);
$record = unpack("a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100symlink/a6magic/a2version/a32owner/a32group/a8deviceMajor/a8deviceMinor/a155prefix/a12unpacked", $header);
$record['filename'] = $record['prefix'] . $record['name'];
// convert to decimal values
foreach (array('uid', 'gid', 'size', 'mtime', 'checksum') as $key) {
$record[$key] = octdec($record[$key]);
}
if ($record['checksum'] == 0x00000000) {
return NULL;
} elseif (0 !== strpos($record['magic'], 'ustar')) {
throw new ReaderException('Unsupported archive type.');
}
$checksum = 0;
for ($i = 0; $i < self::BLOCK_SIZE; $i++) {
$checksum += 148 <= $i && $i < 156 ? 32 : ord($header[$i]);
}
if ($record['checksum'] != $checksum) {
throw new ReaderException('Archive is corrupted.');
}
$length = $record['size'];
if (is_float($length)) {
$padding = self::BLOCK_SIZE - fmod($length, self::BLOCK_SIZE);
} else {
$padding = self::BLOCK_SIZE - $length % self::BLOCK_SIZE;
}
$file['contents'] = NULL;
if ($length == 0 && is_callable($this->readContents)) {
call_user_func($this->readContents, $record, '', 0, 0);
}
while ($length > 0) {
if ($length > $this->buffer) {
$read = $this->buffer;
$length -= $this->buffer;
} else {
$read = $length;
$length = 0;
}
if ($this->readContents === FALSE) {
fseek($this->file, $read, SEEK_CUR);
} else {
$chunk = fread($this->file, $read);
if (is_callable($this->readContents)) {
call_user_func($this->readContents, $record, $chunk, $length, $read);
} else {
$file['contents'] .= $read;
}
}
}
if ($padding !== self::BLOCK_SIZE) {
fseek($this->file, $padding, SEEK_CUR);
}
return $record;
}
}
}