1 | <?php |
||
20 | class Tar extends Abstraction implements Executable |
||
21 | { |
||
22 | /** |
||
23 | * Path to compress |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $path; |
||
28 | |||
29 | /** |
||
30 | * Compression to use |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $compression; |
||
35 | |||
36 | /** |
||
37 | * Compress program to use. |
||
38 | * --use-compress-program |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | private $compressProgram; |
||
43 | |||
44 | /** |
||
45 | * Path to dump file |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $tarPathname; |
||
50 | |||
51 | /** |
||
52 | * List of excluded path. |
||
53 | * --exclude='foo' |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | private $excludes = []; |
||
58 | |||
59 | /** |
||
60 | * Force local file resolution |
||
61 | * --force-local |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $local = false; |
||
66 | |||
67 | /** |
||
68 | * Ignore failed reads |
||
69 | * --ignore-failed-read |
||
70 | * |
||
71 | * @var bool |
||
72 | */ |
||
73 | private $ignoreFailedRead; |
||
74 | |||
75 | /** |
||
76 | * Should the source directory be removed. |
||
77 | * |
||
78 | * @var boolean |
||
79 | */ |
||
80 | private $removeSourceDir = false; |
||
81 | |||
82 | /** |
||
83 | * Limit data throughput |
||
84 | * | pv -L ${limit} |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $pvLimit = ''; |
||
89 | |||
90 | /** |
||
91 | * List of available compressors |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | private static $availableCompressions = [ |
||
96 | 'bzip2' => 'j', |
||
97 | 'gzip' => 'z', |
||
98 | 'xz' => 'J' |
||
99 | ]; |
||
100 | |||
101 | /** |
||
102 | * Constructor. |
||
103 | * |
||
104 | * @param string $path |
||
105 | */ |
||
106 | 31 | public function __construct(string $path = '') |
|
110 | |||
111 | /** |
||
112 | * Return 'tar' compressor option e.g. 'j' for bzip2. |
||
113 | * |
||
114 | * @param string $compressor |
||
115 | * @return string |
||
116 | */ |
||
117 | 10 | protected function getCompressionOption(string $compressor) : string |
|
121 | |||
122 | /** |
||
123 | * Compress tar. |
||
124 | * |
||
125 | * @param string $compression |
||
126 | * @return \phpbu\App\Cli\Executable\Tar |
||
127 | */ |
||
128 | 21 | public function useCompression(string $compression) : Tar |
|
135 | |||
136 | /** |
||
137 | * Set compress program. |
||
138 | * |
||
139 | * @param string $program |
||
140 | * @return \phpbu\App\Cli\Executable\Tar |
||
141 | */ |
||
142 | 14 | public function useCompressProgram(string $program) : Tar |
|
147 | |||
148 | /** |
||
149 | * Add an path to exclude. |
||
150 | * |
||
151 | * @param string $path |
||
152 | * @return \phpbu\App\Cli\Executable\Tar |
||
153 | */ |
||
154 | 2 | public function addExclude(string $path) : Tar |
|
159 | |||
160 | /** |
||
161 | * Force local file resolution. |
||
162 | * |
||
163 | * @param bool $bool |
||
164 | * @return \phpbu\App\Cli\Executable\Tar |
||
165 | */ |
||
166 | 14 | public function forceLocal(bool $bool) : Tar |
|
171 | |||
172 | /** |
||
173 | * Ignore failed reads setter. |
||
174 | * |
||
175 | * @param bool $bool |
||
176 | * @return \phpbu\App\Cli\Executable\Tar |
||
177 | */ |
||
178 | 14 | public function ignoreFailedRead(bool $bool) : Tar |
|
183 | |||
184 | /** |
||
185 | * Limit the data throughput. |
||
186 | * |
||
187 | * @param string $limit |
||
188 | * @return \phpbu\App\Cli\Executable\Tar |
||
189 | */ |
||
190 | 15 | public function throttle(string $limit) : Tar |
|
195 | |||
196 | /** |
||
197 | * Does the tar handle the compression. |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 3 | public function handlesCompression() : bool |
|
205 | |||
206 | /** |
||
207 | * Set folder to compress. |
||
208 | * |
||
209 | * @param string $path |
||
210 | * @return \phpbu\App\Cli\Executable\Tar |
||
211 | * @throws \phpbu\App\Exception |
||
212 | */ |
||
213 | 29 | public function archiveDirectory(string $path) : Tar |
|
219 | |||
220 | /** |
||
221 | * Set target filename. |
||
222 | * |
||
223 | * @param string $path |
||
224 | * @return \phpbu\App\Cli\Executable\Tar |
||
225 | */ |
||
226 | 27 | public function archiveTo(string $path) : Tar |
|
231 | |||
232 | /** |
||
233 | * Delete the source directory. |
||
234 | * |
||
235 | * @param boolean $bool |
||
236 | * @return \phpbu\App\Cli\Executable\Tar |
||
237 | */ |
||
238 | 18 | public function removeSourceDirectory(bool $bool) : Tar |
|
243 | |||
244 | /** |
||
245 | * Tar CommandLine generator. |
||
246 | * |
||
247 | * @return \SebastianFeldmann\Cli\CommandLine |
||
248 | */ |
||
249 | 29 | protected function createCommandLine() : CommandLine |
|
282 | |||
283 | /** |
||
284 | * Adds necessary exclude options to tat command. |
||
285 | * |
||
286 | * @param \SebastianFeldmann\Cli\Command\Executable $tar |
||
287 | */ |
||
288 | 27 | protected function setExcludeOptions(Cmd $tar) |
|
294 | |||
295 | /** |
||
296 | * Add a remove command if requested. |
||
297 | * |
||
298 | * @param \SebastianFeldmann\Cli\CommandLine $process |
||
299 | */ |
||
300 | 27 | protected function addRemoveCommand(CommandLine $process) |
|
306 | |||
307 | /** |
||
308 | * Return 'rm' command. |
||
309 | * |
||
310 | * @return \SebastianFeldmann\Cli\Command\Executable |
||
311 | */ |
||
312 | 6 | protected function getRmCommand() : Cmd |
|
318 | |||
319 | /** |
||
320 | * Check directory to compress. |
||
321 | * |
||
322 | * @param string $path |
||
323 | * @throws \phpbu\App\Exception |
||
324 | */ |
||
325 | 29 | private function validateDirectory(string $path) |
|
331 | |||
332 | /** |
||
333 | * Check if source and target values are set. |
||
334 | * |
||
335 | * @throws \phpbu\App\Exception |
||
336 | */ |
||
337 | 29 | private function validateSetup() |
|
346 | |||
347 | /** |
||
348 | * Return true if a given compression is valid false otherwise. |
||
349 | * |
||
350 | * @param string $compression |
||
351 | * @return bool |
||
352 | */ |
||
353 | 23 | public static function isCompressionValid(string $compression) : bool |
|
357 | |||
358 | /** |
||
359 | * Should output be throttled through pv. |
||
360 | * |
||
361 | * @return bool |
||
362 | */ |
||
363 | 27 | public function isThrottled() : bool |
|
367 | } |
||
368 |