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