1 | /* $Id: gzipcmd.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - GZIP Utility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/zip.h>
|
---|
42 |
|
---|
43 | #include <iprt/buildconfig.h>
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/file.h>
|
---|
46 | #include <iprt/getopt.h>
|
---|
47 | #include <iprt/initterm.h>
|
---|
48 | #include <iprt/message.h>
|
---|
49 | #include <iprt/param.h>
|
---|
50 | #include <iprt/path.h>
|
---|
51 | #include <iprt/stream.h>
|
---|
52 | #include <iprt/string.h>
|
---|
53 | #include <iprt/vfs.h>
|
---|
54 | #include <iprt/zip.h>
|
---|
55 |
|
---|
56 |
|
---|
57 | /*********************************************************************************************************************************
|
---|
58 | * Structures and Typedefs *
|
---|
59 | *********************************************************************************************************************************/
|
---|
60 | /**
|
---|
61 | * Gzip command options.
|
---|
62 | */
|
---|
63 | typedef struct RTGZIPCMDOPTS
|
---|
64 | {
|
---|
65 | bool fAscii;
|
---|
66 | bool fStdOut;
|
---|
67 | bool fDecompress;
|
---|
68 | bool fForce;
|
---|
69 | bool fKeep;
|
---|
70 | bool fList;
|
---|
71 | bool fName;
|
---|
72 | bool fQuiet;
|
---|
73 | bool fRecursive;
|
---|
74 | const char *pszSuff;
|
---|
75 | bool fTest;
|
---|
76 | unsigned uLevel;
|
---|
77 | /** The current output filename (for deletion). */
|
---|
78 | char szOutput[RTPATH_MAX];
|
---|
79 | /** The current input filename (for deletion and messages). */
|
---|
80 | const char *pszInput;
|
---|
81 | } RTGZIPCMDOPTS;
|
---|
82 | /** Pointer to GZIP options. */
|
---|
83 | typedef RTGZIPCMDOPTS *PRTGZIPCMDOPTS;
|
---|
84 | /** Pointer to const GZIP options. */
|
---|
85 | typedef RTGZIPCMDOPTS const *PCRTGZIPCMDOPTS;
|
---|
86 |
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Checks if the given standard handle is a TTY.
|
---|
91 | *
|
---|
92 | * @returns true / false
|
---|
93 | * @param enmStdHandle The standard handle.
|
---|
94 | */
|
---|
95 | static bool gzipIsStdHandleATty(RTHANDLESTD enmStdHandle)
|
---|
96 | {
|
---|
97 | /** @todo Add isatty() to IPRT. */
|
---|
98 | RT_NOREF1(enmStdHandle);
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Pushes data from the input to the output I/O streams.
|
---|
105 | *
|
---|
106 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
|
---|
107 | * @param hVfsSrc The source I/O stream.
|
---|
108 | * @param hVfsDst The destination I/O stream.
|
---|
109 | */
|
---|
110 | static RTEXITCODE gzipPush(RTVFSIOSTREAM hVfsSrc, RTVFSIOSTREAM hVfsDst)
|
---|
111 | {
|
---|
112 | for (;;)
|
---|
113 | {
|
---|
114 | uint8_t abBuf[_64K];
|
---|
115 | size_t cbRead;
|
---|
116 | int rc = RTVfsIoStrmRead(hVfsSrc, abBuf, sizeof(abBuf), true /*fBlocking*/, &cbRead);
|
---|
117 | if (RT_FAILURE(rc))
|
---|
118 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsIoStrmRead failed: %Rrc", rc);
|
---|
119 | if (rc == VINF_EOF && cbRead == 0)
|
---|
120 | return RTEXITCODE_SUCCESS;
|
---|
121 |
|
---|
122 | rc = RTVfsIoStrmWrite(hVfsDst, abBuf, cbRead, true /*fBlocking*/, NULL /*cbWritten*/);
|
---|
123 | if (RT_FAILURE(rc))
|
---|
124 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsIoStrmWrite failed: %Rrc", rc);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Pushes the bytes from the input to the output stream, flushes the output
|
---|
131 | * stream and closes both of them.
|
---|
132 | *
|
---|
133 | * On failure, we will delete the output file, if it's a file. The input file
|
---|
134 | * may be deleted, if we're not told to keep it (--keep, --to-stdout).
|
---|
135 | *
|
---|
136 | * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
|
---|
137 | * @param phVfsSrc The input stream. Set to NIL if closed.
|
---|
138 | * @param pOpts The options.
|
---|
139 | * @param phVfsDst The output stream. Set to NIL if closed.
|
---|
140 | */
|
---|
141 | static RTEXITCODE gzipPushFlushAndClose(PRTVFSIOSTREAM phVfsSrc, PCRTGZIPCMDOPTS pOpts, PRTVFSIOSTREAM phVfsDst)
|
---|
142 | {
|
---|
143 | /*
|
---|
144 | * Push bytes, flush and close the streams.
|
---|
145 | */
|
---|
146 | RTEXITCODE rcExit = gzipPush(*phVfsSrc, *phVfsDst);
|
---|
147 |
|
---|
148 | RTVfsIoStrmRelease(*phVfsSrc);
|
---|
149 | *phVfsSrc = NIL_RTVFSIOSTREAM;
|
---|
150 |
|
---|
151 | int rc = RTVfsIoStrmFlush(*phVfsDst);
|
---|
152 | if (RT_FAILURE(rc) && rc != VERR_INVALID_PARAMETER)
|
---|
153 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to flush the output file: %Rrc", rc);
|
---|
154 | RTVfsIoStrmRelease(*phVfsDst);
|
---|
155 | *phVfsDst = NIL_RTVFSIOSTREAM;
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Do the cleaning up, if needed. Remove the input file, if that's the
|
---|
159 | * desire of the user, or remove the output file on failure.
|
---|
160 | */
|
---|
161 | if (!pOpts->fStdOut)
|
---|
162 | {
|
---|
163 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
164 | {
|
---|
165 | if (!pOpts->fKeep)
|
---|
166 | {
|
---|
167 | rc = RTFileDelete(pOpts->pszInput);
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to delete '%s': %Rrc", pOpts->pszInput, rc);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | rc = RTFileDelete(pOpts->szOutput);
|
---|
175 | if (RT_FAILURE(rc))
|
---|
176 | RTMsgError("Failed to delete '%s': %Rrc", pOpts->szOutput, rc);
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | return rcExit;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Compresses one stream to another.
|
---|
186 | *
|
---|
187 | * @returns Exit code.
|
---|
188 | * @param phVfsSrc The input stream. Set to NIL if closed.
|
---|
189 | * @param pOpts The options.
|
---|
190 | * @param phVfsDst The output stream. Set to NIL if closed.
|
---|
191 | */
|
---|
192 | static RTEXITCODE gzipCompressFile(PRTVFSIOSTREAM phVfsSrc, PCRTGZIPCMDOPTS pOpts, PRTVFSIOSTREAM phVfsDst)
|
---|
193 | {
|
---|
194 | /*
|
---|
195 | * Attach the compressor to the output stream.
|
---|
196 | */
|
---|
197 | RTVFSIOSTREAM hVfsGzip;
|
---|
198 | int rc = RTZipGzipCompressIoStream(*phVfsDst, 0 /*fFlags*/, pOpts->uLevel, &hVfsGzip);
|
---|
199 | if (RT_FAILURE(rc))
|
---|
200 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTZipGzipCompressIoStream failed: %Rrc", rc);
|
---|
201 |
|
---|
202 | uint32_t cRefs = RTVfsIoStrmRelease(*phVfsDst);
|
---|
203 | Assert(cRefs > 0); RT_NOREF_PV(cRefs);
|
---|
204 | *phVfsDst = hVfsGzip;
|
---|
205 |
|
---|
206 | return gzipPushFlushAndClose(phVfsSrc, pOpts, phVfsDst);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Attach a decompressor to the given source stream, replacing and releasing the
|
---|
212 | * input handle with the decompressor.
|
---|
213 | *
|
---|
214 | * @returns Exit code.
|
---|
215 | * @param phVfsSrc The input stream. Replaced on success.
|
---|
216 | */
|
---|
217 | static RTEXITCODE gzipSetupDecompressor(PRTVFSIOSTREAM phVfsSrc)
|
---|
218 | {
|
---|
219 | /*
|
---|
220 | * Attach the decompressor to the input stream.
|
---|
221 | */
|
---|
222 | uint32_t fFlags = 0;
|
---|
223 | fFlags |= RTZIPGZIPDECOMP_F_ALLOW_ZLIB_HDR;
|
---|
224 | RTVFSIOSTREAM hVfsGunzip;
|
---|
225 | int rc = RTZipGzipDecompressIoStream(*phVfsSrc, fFlags, &hVfsGunzip);
|
---|
226 | if (RT_FAILURE(rc))
|
---|
227 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTZipGzipDecompressIoStream failed: %Rrc", rc);
|
---|
228 |
|
---|
229 | uint32_t cRefs = RTVfsIoStrmRelease(*phVfsSrc);
|
---|
230 | Assert(cRefs > 0); RT_NOREF_PV(cRefs);
|
---|
231 | *phVfsSrc = hVfsGunzip;
|
---|
232 |
|
---|
233 | #if 0
|
---|
234 | /* This is a good place for testing stuff. */
|
---|
235 | rc = RTVfsCreateReadAheadForIoStream(*phVfsSrc, 0, 16, _4K+1, &hVfsGunzip);
|
---|
236 | AssertRC(rc);
|
---|
237 | if (RT_SUCCESS(rc))
|
---|
238 | {
|
---|
239 | uint32_t cRefs = RTVfsIoStrmRelease(*phVfsSrc);
|
---|
240 | Assert(cRefs > 0);
|
---|
241 | *phVfsSrc = hVfsGunzip;
|
---|
242 | }
|
---|
243 | #endif
|
---|
244 |
|
---|
245 | return RTEXITCODE_SUCCESS;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Decompresses one stream to another.
|
---|
251 | *
|
---|
252 | * @returns Exit code.
|
---|
253 | * @param phVfsSrc The input stream. Set to NIL if closed.
|
---|
254 | * @param pOpts The options.
|
---|
255 | * @param phVfsDst The output stream. Set to NIL if closed.
|
---|
256 | */
|
---|
257 | static RTEXITCODE gzipDecompressFile(PRTVFSIOSTREAM phVfsSrc, PCRTGZIPCMDOPTS pOpts, PRTVFSIOSTREAM phVfsDst)
|
---|
258 | {
|
---|
259 | RTEXITCODE rcExit = gzipSetupDecompressor(phVfsSrc);
|
---|
260 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
261 | rcExit = gzipPushFlushAndClose(phVfsSrc, pOpts, phVfsDst);
|
---|
262 | return rcExit;
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * For testing the archive (todo).
|
---|
268 | *
|
---|
269 | * @returns Exit code.
|
---|
270 | * @param phVfsSrc The input stream. Set to NIL if closed.
|
---|
271 | * @param pOpts The options.
|
---|
272 | */
|
---|
273 | static RTEXITCODE gzipTestFile(PRTVFSIOSTREAM phVfsSrc, PCRTGZIPCMDOPTS pOpts)
|
---|
274 | {
|
---|
275 | RT_NOREF_PV(pOpts);
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * Read the whole stream.
|
---|
279 | */
|
---|
280 | RTEXITCODE rcExit = gzipSetupDecompressor(phVfsSrc);
|
---|
281 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
282 | {
|
---|
283 | for (;;)
|
---|
284 | {
|
---|
285 | uint8_t abBuf[_64K];
|
---|
286 | size_t cbRead;
|
---|
287 | int rc = RTVfsIoStrmRead(*phVfsSrc, abBuf, sizeof(abBuf), true /*fBlocking*/, &cbRead);
|
---|
288 | if (RT_FAILURE(rc))
|
---|
289 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsIoStrmRead failed: %Rrc", rc);
|
---|
290 | if (rc == VINF_EOF && cbRead == 0)
|
---|
291 | return RTEXITCODE_SUCCESS;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | return rcExit;
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 | static RTEXITCODE gzipListFile(PRTVFSIOSTREAM phVfsSrc, PCRTGZIPCMDOPTS pOpts)
|
---|
299 | {
|
---|
300 | RT_NOREF2(phVfsSrc, pOpts);
|
---|
301 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Listing has not been implemented");
|
---|
302 | }
|
---|
303 |
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Opens the output file.
|
---|
307 | *
|
---|
308 | * @returns Command exit, error messages written using RTMsg*.
|
---|
309 | *
|
---|
310 | * @param pszFile The input filename.
|
---|
311 | * @param pOpts The options, szOutput will be filled in by this
|
---|
312 | * function on success.
|
---|
313 | * @param phVfsIos Where to return the output stream handle.
|
---|
314 | *
|
---|
315 | * @remarks This is actually not quite the way we need to do things.
|
---|
316 | *
|
---|
317 | * First of all, we need a GZIP file system stream for a real GZIP
|
---|
318 | * implementation, since there may be more than one file in the gzipped
|
---|
319 | * file.
|
---|
320 | *
|
---|
321 | * Second, we need to open the output files as we encounter files in the input
|
---|
322 | * file system stream. The gzip format contains timestamp and usually a
|
---|
323 | * filename, the default is to use this name (see the --no-name
|
---|
324 | * option).
|
---|
325 | */
|
---|
326 | static RTEXITCODE gzipOpenOutput(const char *pszFile, PRTGZIPCMDOPTS pOpts, PRTVFSIOSTREAM phVfsIos)
|
---|
327 | {
|
---|
328 | int rc;
|
---|
329 | if (!strcmp(pszFile, "-") || pOpts->fStdOut)
|
---|
330 | {
|
---|
331 | strcpy(pOpts->szOutput, "-");
|
---|
332 |
|
---|
333 | if ( !pOpts->fForce
|
---|
334 | && !pOpts->fDecompress
|
---|
335 | && gzipIsStdHandleATty(RTHANDLESTD_OUTPUT))
|
---|
336 | return RTMsgErrorExit(RTEXITCODE_SYNTAX,
|
---|
337 | "Yeah, right. I'm not writing any compressed data to the terminal without --force.\n");
|
---|
338 |
|
---|
339 | rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT,
|
---|
340 | RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
|
---|
341 | true /*fLeaveOpen*/,
|
---|
342 | phVfsIos);
|
---|
343 | if (RT_FAILURE(rc))
|
---|
344 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening standard output: %Rrc", rc);
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | Assert(!RTVfsChainIsSpec(pszFile));
|
---|
349 |
|
---|
350 | /* Construct an output filename. */
|
---|
351 | rc = RTStrCopy(pOpts->szOutput, sizeof(pOpts->szOutput), pszFile);
|
---|
352 | if (RT_FAILURE(rc))
|
---|
353 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing output filename: %Rrc", rc);
|
---|
354 | if (pOpts->fDecompress)
|
---|
355 | {
|
---|
356 | /** @todo take filename from archive? */
|
---|
357 | size_t cchSuff = strlen(pOpts->pszSuff); Assert(cchSuff > 0);
|
---|
358 | size_t cch = strlen(pOpts->szOutput);
|
---|
359 | if ( cch <= cchSuff
|
---|
360 | || strcmp(&pOpts->szOutput[cch - cchSuff], pOpts->pszSuff))
|
---|
361 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Input file does not end with: '%s'", pOpts->pszSuff);
|
---|
362 | pOpts->szOutput[cch - cchSuff] = '\0';
|
---|
363 | if (!RTPathFilename(pOpts->szOutput))
|
---|
364 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing output filename: Input file name is all suffix.");
|
---|
365 | }
|
---|
366 | else
|
---|
367 | {
|
---|
368 | rc = RTStrCat(pOpts->szOutput, sizeof(pOpts->szOutput), pOpts->pszSuff);
|
---|
369 | if (RT_FAILURE(rc))
|
---|
370 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error constructing output filename: %Rrc", rc);
|
---|
371 | }
|
---|
372 |
|
---|
373 | /* Open the output file. */
|
---|
374 | uint32_t fOpen = RTFILE_O_WRITE | RTFILE_O_DENY_WRITE;
|
---|
375 | if (pOpts->fForce)
|
---|
376 | fOpen |= RTFILE_O_CREATE_REPLACE;
|
---|
377 | else
|
---|
378 | fOpen |= RTFILE_O_CREATE;
|
---|
379 | rc = RTVfsIoStrmOpenNormal(pOpts->szOutput, fOpen, phVfsIos);
|
---|
380 | if (RT_FAILURE(rc))
|
---|
381 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening output file '%s': %Rrc", pOpts->szOutput, rc);
|
---|
382 | }
|
---|
383 |
|
---|
384 | return RTEXITCODE_SUCCESS;
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Opens the input file.
|
---|
390 | *
|
---|
391 | * @returns Command exit, error messages written using RTMsg*.
|
---|
392 | *
|
---|
393 | * @param pszFile The input filename.
|
---|
394 | * @param pOpts The options, szOutput will be filled in by this
|
---|
395 | * function on success.
|
---|
396 | * @param phVfsIos Where to return the input stream handle.
|
---|
397 | */
|
---|
398 | static RTEXITCODE gzipOpenInput(const char *pszFile, PRTGZIPCMDOPTS pOpts, PRTVFSIOSTREAM phVfsIos)
|
---|
399 | {
|
---|
400 | int rc;
|
---|
401 |
|
---|
402 | pOpts->pszInput = pszFile;
|
---|
403 | if (!strcmp(pszFile, "-"))
|
---|
404 | {
|
---|
405 | if ( !pOpts->fForce
|
---|
406 | && pOpts->fDecompress
|
---|
407 | && gzipIsStdHandleATty(RTHANDLESTD_OUTPUT))
|
---|
408 | return RTMsgErrorExit(RTEXITCODE_SYNTAX,
|
---|
409 | "Yeah, right. I'm not reading any compressed data from the terminal without --force.\n");
|
---|
410 |
|
---|
411 | rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_INPUT,
|
---|
412 | RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
|
---|
413 | true /*fLeaveOpen*/,
|
---|
414 | phVfsIos);
|
---|
415 | if (RT_FAILURE(rc))
|
---|
416 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error opening standard input: %Rrc", rc);
|
---|
417 | }
|
---|
418 | else
|
---|
419 | {
|
---|
420 | uint32_t offError = 0;
|
---|
421 | RTERRINFOSTATIC ErrInfo;
|
---|
422 | rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE,
|
---|
423 | phVfsIos, &offError, RTErrInfoInitStatic(&ErrInfo));
|
---|
424 | if (RT_FAILURE(rc))
|
---|
425 | return RTVfsChainMsgErrorExitFailure("RTVfsChainOpenIoStream", pszFile, rc, offError, &ErrInfo.Core);
|
---|
426 | }
|
---|
427 |
|
---|
428 | return RTEXITCODE_SUCCESS;
|
---|
429 |
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * A mini GZIP program.
|
---|
435 | *
|
---|
436 | * @returns Program exit code.
|
---|
437 | *
|
---|
438 | * @param cArgs The number of arguments.
|
---|
439 | * @param papszArgs The argument vector. (Note that this may be
|
---|
440 | * reordered, so the memory must be writable.)
|
---|
441 | */
|
---|
442 | RTDECL(RTEXITCODE) RTZipGzipCmd(unsigned cArgs, char **papszArgs)
|
---|
443 | {
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Parse the command line.
|
---|
447 | */
|
---|
448 | static const RTGETOPTDEF s_aOptions[] =
|
---|
449 | {
|
---|
450 | { "--ascii", 'a', RTGETOPT_REQ_NOTHING },
|
---|
451 | { "--stdout", 'c', RTGETOPT_REQ_NOTHING },
|
---|
452 | { "--to-stdout", 'c', RTGETOPT_REQ_NOTHING },
|
---|
453 | { "--decompress", 'd', RTGETOPT_REQ_NOTHING },
|
---|
454 | { "--uncompress", 'd', RTGETOPT_REQ_NOTHING },
|
---|
455 | { "--force", 'f', RTGETOPT_REQ_NOTHING },
|
---|
456 | { "--keep", 'k', RTGETOPT_REQ_NOTHING },
|
---|
457 | { "--list", 'l', RTGETOPT_REQ_NOTHING },
|
---|
458 | { "--no-name", 'n', RTGETOPT_REQ_NOTHING },
|
---|
459 | { "--name", 'N', RTGETOPT_REQ_NOTHING },
|
---|
460 | { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
|
---|
461 | { "--recursive", 'r', RTGETOPT_REQ_NOTHING },
|
---|
462 | { "--suffix", 'S', RTGETOPT_REQ_STRING },
|
---|
463 | { "--test", 't', RTGETOPT_REQ_NOTHING },
|
---|
464 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
465 | { "--fast", '1', RTGETOPT_REQ_NOTHING },
|
---|
466 | { "-1", '1', RTGETOPT_REQ_NOTHING },
|
---|
467 | { "-2", '2', RTGETOPT_REQ_NOTHING },
|
---|
468 | { "-3", '3', RTGETOPT_REQ_NOTHING },
|
---|
469 | { "-4", '4', RTGETOPT_REQ_NOTHING },
|
---|
470 | { "-5", '5', RTGETOPT_REQ_NOTHING },
|
---|
471 | { "-6", '6', RTGETOPT_REQ_NOTHING },
|
---|
472 | { "-7", '7', RTGETOPT_REQ_NOTHING },
|
---|
473 | { "-8", '8', RTGETOPT_REQ_NOTHING },
|
---|
474 | { "-9", '9', RTGETOPT_REQ_NOTHING },
|
---|
475 | { "--best", '9', RTGETOPT_REQ_NOTHING }
|
---|
476 | };
|
---|
477 |
|
---|
478 | RTGZIPCMDOPTS Opts;
|
---|
479 | Opts.fAscii = false;
|
---|
480 | Opts.fStdOut = false;
|
---|
481 | Opts.fDecompress = false;
|
---|
482 | Opts.fForce = false;
|
---|
483 | Opts.fKeep = false;
|
---|
484 | Opts.fList = false;
|
---|
485 | Opts.fName = true;
|
---|
486 | Opts.fQuiet = false;
|
---|
487 | Opts.fRecursive = false;
|
---|
488 | Opts.pszSuff = ".gz";
|
---|
489 | Opts.fTest = false;
|
---|
490 | Opts.uLevel = 6;
|
---|
491 |
|
---|
492 | RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
|
---|
493 | unsigned cProcessed = 0;
|
---|
494 | //RTVFSIOSTREAM hVfsStdOut= NIL_RTVFSIOSTREAM;
|
---|
495 |
|
---|
496 | RTGETOPTSTATE GetState;
|
---|
497 | int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
498 | RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
499 | if (RT_FAILURE(rc))
|
---|
500 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
|
---|
501 |
|
---|
502 | for (;;)
|
---|
503 | {
|
---|
504 | RTGETOPTUNION ValueUnion;
|
---|
505 | int chOpt = RTGetOpt(&GetState, &ValueUnion);
|
---|
506 | switch (chOpt)
|
---|
507 | {
|
---|
508 | case 0:
|
---|
509 | /*
|
---|
510 | * If we've processed any files we're done. Otherwise take
|
---|
511 | * input from stdin and write the output to stdout.
|
---|
512 | */
|
---|
513 | if (cProcessed > 0)
|
---|
514 | return rcExit;
|
---|
515 | ValueUnion.psz = "-";
|
---|
516 | Opts.fStdOut = true;
|
---|
517 | RT_FALL_THRU();
|
---|
518 | case VINF_GETOPT_NOT_OPTION:
|
---|
519 | {
|
---|
520 | if (!*Opts.pszSuff && !Opts.fStdOut)
|
---|
521 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The --suffix option specified an empty string");
|
---|
522 | if (!Opts.fStdOut && RTVfsChainIsSpec(ValueUnion.psz))
|
---|
523 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Must use standard out with VFS chain specifications");
|
---|
524 | if ( Opts.fName
|
---|
525 | && !Opts.fList
|
---|
526 | && !Opts.fTest
|
---|
527 | && !Opts.fDecompress)
|
---|
528 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The --name option has not yet been implemented. Use --no-name.");
|
---|
529 | if (Opts.fAscii)
|
---|
530 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The --ascii option has not yet been implemented.");
|
---|
531 | if (Opts.fRecursive)
|
---|
532 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The --recursive option has not yet been implemented.");
|
---|
533 |
|
---|
534 | /* Open the input file. */
|
---|
535 | RTVFSIOSTREAM hVfsSrc;
|
---|
536 | RTEXITCODE rcExit2 = gzipOpenInput(ValueUnion.psz, &Opts, &hVfsSrc);
|
---|
537 | if (rcExit2 == RTEXITCODE_SUCCESS)
|
---|
538 | {
|
---|
539 | if (Opts.fList)
|
---|
540 | rcExit2 = gzipListFile(&hVfsSrc, &Opts);
|
---|
541 | else if (Opts.fTest)
|
---|
542 | rcExit2 = gzipTestFile(&hVfsSrc, &Opts);
|
---|
543 | else
|
---|
544 | {
|
---|
545 | RTVFSIOSTREAM hVfsDst;
|
---|
546 | rcExit2 = gzipOpenOutput(ValueUnion.psz, &Opts, &hVfsDst);
|
---|
547 | if (rcExit2 == RTEXITCODE_SUCCESS)
|
---|
548 | {
|
---|
549 | if (Opts.fDecompress)
|
---|
550 | rcExit2 = gzipDecompressFile(&hVfsSrc, &Opts, &hVfsDst);
|
---|
551 | else
|
---|
552 | rcExit2 = gzipCompressFile(&hVfsSrc, &Opts, &hVfsDst);
|
---|
553 | RTVfsIoStrmRelease(hVfsDst);
|
---|
554 | }
|
---|
555 | }
|
---|
556 | RTVfsIoStrmRelease(hVfsSrc);
|
---|
557 | }
|
---|
558 | if (rcExit2 != RTEXITCODE_SUCCESS)
|
---|
559 | rcExit = rcExit2;
|
---|
560 |
|
---|
561 | cProcessed++;
|
---|
562 | break;
|
---|
563 | }
|
---|
564 |
|
---|
565 | case 'a': Opts.fAscii = true; break;
|
---|
566 | case 'c':
|
---|
567 | Opts.fStdOut = true;
|
---|
568 | Opts.fKeep = true;
|
---|
569 | break;
|
---|
570 | case 'd': Opts.fDecompress = true; break;
|
---|
571 | case 'f': Opts.fForce = true; break;
|
---|
572 | case 'k': Opts.fKeep = true; break;
|
---|
573 | case 'l': Opts.fList = true; break;
|
---|
574 | case 'n': Opts.fName = false; break;
|
---|
575 | case 'N': Opts.fName = true; break;
|
---|
576 | case 'q': Opts.fQuiet = true; break;
|
---|
577 | case 'r': Opts.fRecursive = true; break;
|
---|
578 | case 'S': Opts.pszSuff = ValueUnion.psz; break;
|
---|
579 | case 't': Opts.fTest = true; break;
|
---|
580 | case 'v': Opts.fQuiet = false; break;
|
---|
581 | case '1': Opts.uLevel = 1; break;
|
---|
582 | case '2': Opts.uLevel = 2; break;
|
---|
583 | case '3': Opts.uLevel = 3; break;
|
---|
584 | case '4': Opts.uLevel = 4; break;
|
---|
585 | case '5': Opts.uLevel = 5; break;
|
---|
586 | case '6': Opts.uLevel = 6; break;
|
---|
587 | case '7': Opts.uLevel = 7; break;
|
---|
588 | case '8': Opts.uLevel = 8; break;
|
---|
589 | case '9': Opts.uLevel = 9; break;
|
---|
590 |
|
---|
591 | case 'h':
|
---|
592 | RTPrintf("Usage: to be written\nOption dump:\n");
|
---|
593 | for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
|
---|
594 | RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
|
---|
595 | return RTEXITCODE_SUCCESS;
|
---|
596 |
|
---|
597 | case 'V':
|
---|
598 | RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
|
---|
599 | return RTEXITCODE_SUCCESS;
|
---|
600 |
|
---|
601 | default:
|
---|
602 | return RTGetOptPrintError(chOpt, &ValueUnion);
|
---|
603 | }
|
---|
604 | }
|
---|
605 | }
|
---|
606 |
|
---|