VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTGzip.cpp@ 45104

Last change on this file since 45104 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1/* $Id: RTGzip.cpp 44529 2013-02-04 15:54:15Z vboxsync $ */
2/** @file
3 * IPRT - GZIP Utility.
4 */
5
6/*
7 * Copyright (C) 2010-2011 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/stream.h>
40#include <iprt/string.h>
41#include <iprt/vfs.h>
42#include <iprt/zip.h>
43
44
45static bool isStdHandleATty(int fd)
46{
47 /** @todo IPRT is missing this */
48 return false;
49}
50
51
52/**
53 * Pushes data from the input to the output I/O streams.
54 *
55 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
56 * @param hVfsIn The input I/O stream.
57 * @param hVfsOut The input I/O stream.
58 */
59static RTEXITCODE gzipPush(RTVFSIOSTREAM hVfsIn, RTVFSIOSTREAM hVfsOut)
60{
61 for (;;)
62 {
63 uint8_t abBuf[_64K];
64 size_t cbRead;
65 int rc = RTVfsIoStrmRead(hVfsIn, abBuf, sizeof(abBuf), true /*fBlocking*/, &cbRead);
66 if (RT_FAILURE(rc))
67 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsIoStrmRead failed: %Rrc", rc);
68 if (rc == VINF_EOF && cbRead == 0)
69 return RTEXITCODE_SUCCESS;
70
71 rc = RTVfsIoStrmWrite(hVfsOut, abBuf, cbRead, true /*fBlocking*/, NULL /*cbWritten*/);
72 if (RT_FAILURE(rc))
73 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsIoStrmWrite failed: %Rrc", rc);
74 }
75}
76
77static RTEXITCODE gzipCompress(RTVFSIOSTREAM hVfsIn, RTVFSIOSTREAM hVfsOut)
78{
79 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Compression is not yet implemented, sorry.");
80}
81
82
83static RTEXITCODE gzipCompressFile(const char *pszFile, bool fStdOut, bool fForce, PRTVFSIOSTREAM phVfsStdOut)
84{
85 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Compression is not yet implemented, sorry.");
86}
87
88
89static RTEXITCODE gzipDecompress(RTVFSIOSTREAM hVfsIn, RTVFSIOSTREAM hVfsOut)
90{
91 RTEXITCODE rcExit;
92 RTVFSIOSTREAM hVfsGunzip;
93 int rc = RTZipGzipDecompressIoStream(hVfsIn, 0 /*fFlags*/, &hVfsGunzip);
94 if (RT_SUCCESS(rc))
95 {
96 rcExit = gzipPush(hVfsGunzip, hVfsOut);
97 RTVfsIoStrmRelease(hVfsGunzip);
98 }
99 else
100 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTZipGzipDecompressIoStream failed: %Rrc", rc);
101 return rcExit;
102}
103
104
105/**
106 * Handles a file on the command line.
107 *
108 * @returns exit code.
109 * @param pszFile The file to handle.
110 * @param fStdOut Whether to output to standard output or not.
111 * @param fForce Whether to output to or input from terminals.
112 * @param phVfsStdOut Pointer to the standard out handle.
113 * (input/output)
114 */
115static RTEXITCODE gzipDecompressFile(const char *pszFile, bool fStdOut, bool fForce, PRTVFSIOSTREAM phVfsStdOut)
116{
117 /*
118 * Open the specified input file.
119 */
120 const char *pszError;
121 RTVFSIOSTREAM hVfsIn;
122 int rc = RTVfsChainOpenIoStream(pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE, &hVfsIn, &pszError);
123 if (RT_FAILURE(rc))
124 {
125 if (pszError && *pszError)
126 return RTMsgErrorExit(RTEXITCODE_FAILURE,
127 "RTVfsChainOpenIoStream failed with rc=%Rrc:\n"
128 " '%s'\n",
129 " %*s^\n",
130 rc, pszFile, pszError - pszFile, "");
131 return RTMsgErrorExit(RTEXITCODE_FAILURE,
132 "RTVfsChainOpenIoStream failed with rc=%Rrc: '%s'",
133 rc, pszFile);
134 }
135
136 /*
137 * Output the output file.
138 */
139 RTVFSIOSTREAM hVfsOut;
140 char szFinal[RTPATH_MAX];
141 if (fStdOut)
142 {
143 if (*phVfsStdOut == NIL_RTVFSIOSTREAM)
144 {
145 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_OUTPUT, 0 /*fOpen*/, true /*fLeaveOpen*/, phVfsStdOut);
146 if (RT_FAILURE(rc))
147 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to set up standard out: %Rrc", rc);
148 }
149 hVfsOut = *phVfsStdOut;
150 szFinal[0] = '\0';
151 }
152 else
153 {
154 rc = RTStrCopy(szFinal, sizeof(szFinal), pszFile);
155 /** @todo remove the extension? Or are we supposed
156 * to get the org name from the gzip stream? */
157 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Decompressing to file is not implemented");
158 }
159
160 /*
161 * Do the decompressing, then flush and close the output stream (unless
162 * it is stdout).
163 */
164 RTEXITCODE rcExit = gzipDecompress(hVfsIn, hVfsOut);
165 RTVfsIoStrmRelease(hVfsIn);
166 rc = RTVfsIoStrmFlush(hVfsOut);
167 if (RT_FAILURE(rc))
168 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to flush the output file: %Rrc", rc);
169 RTVfsIoStrmRelease(hVfsOut);
170
171 /*
172 * Remove the input file, if that's the desire of the caller, or
173 * remove the output file on decompression failure.
174 */
175 if (!fStdOut)
176 {
177 if (rcExit == RTEXITCODE_SUCCESS)
178 {
179 rc = RTFileDelete(pszFile);
180 if (RT_FAILURE(rc))
181 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTFileDelete failed with %rc: '%s'", rc, pszFile);
182 }
183 else
184 {
185 /* should we do this? */
186 rc = RTFileDelete(szFinal);
187 if (RT_FAILURE(rc))
188 RTMsgError("RTFileDelete failed with %rc: '%s'", rc, pszFile);
189 }
190 }
191
192 return rcExit;
193}
194
195
196static RTEXITCODE gzipTestFile(const char *pszFile, bool fForce)
197{
198 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Testiong has not been implemented");
199}
200
201
202static RTEXITCODE gzipListFile(const char *pszFile, bool fForce)
203{
204 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Listing has not been implemented");
205}
206
207
208int main(int argc, char **argv)
209{
210 int rc = RTR3InitExe(argc, &argv, 0);
211 if (RT_FAILURE(rc))
212 return RTMsgInitFailure(rc);
213
214 /*
215 * Parse the command line.
216 */
217 static const RTGETOPTDEF s_aOptions[] =
218 {
219 { "--ascii", 'a', RTGETOPT_REQ_NOTHING },
220 { "--stdout", 'c', RTGETOPT_REQ_NOTHING },
221 { "--to-stdout", 'c', RTGETOPT_REQ_NOTHING },
222 { "--decompress", 'd', RTGETOPT_REQ_NOTHING },
223 { "--uncompress", 'd', RTGETOPT_REQ_NOTHING },
224 { "--force", 'f', RTGETOPT_REQ_NOTHING },
225 { "--list", 'l', RTGETOPT_REQ_NOTHING },
226 { "--no-name", 'n', RTGETOPT_REQ_NOTHING },
227 { "--name", 'N', RTGETOPT_REQ_NOTHING },
228 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
229 { "--recursive", 'r', RTGETOPT_REQ_NOTHING },
230 { "--suffix", 'S', RTGETOPT_REQ_STRING },
231 { "--test", 't', RTGETOPT_REQ_NOTHING },
232 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
233 { "--fast", '1', RTGETOPT_REQ_NOTHING },
234 { "-1", '1', RTGETOPT_REQ_NOTHING },
235 { "-2", '2', RTGETOPT_REQ_NOTHING },
236 { "-3", '3', RTGETOPT_REQ_NOTHING },
237 { "-4", '4', RTGETOPT_REQ_NOTHING },
238 { "-5", '5', RTGETOPT_REQ_NOTHING },
239 { "-6", '6', RTGETOPT_REQ_NOTHING },
240 { "-7", '7', RTGETOPT_REQ_NOTHING },
241 { "-8", '8', RTGETOPT_REQ_NOTHING },
242 { "-9", '9', RTGETOPT_REQ_NOTHING },
243 { "--best", '9', RTGETOPT_REQ_NOTHING }
244 };
245
246 bool fAscii = false;
247 bool fStdOut = false;
248 bool fDecompress = false;
249 bool fForce = false;
250 bool fList = false;
251 bool fName = true;
252 bool fQuiet = false;
253 bool fRecursive = false;
254 const char *pszSuff = ".gz";
255 bool fTest = false;
256 unsigned uLevel = 6;
257
258 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
259 unsigned cProcessed = 0;
260 RTVFSIOSTREAM hVfsStdOut= NIL_RTVFSIOSTREAM;
261
262 RTGETOPTSTATE GetState;
263 rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
264 RTGETOPTINIT_FLAGS_OPTS_FIRST);
265 for (;;)
266 {
267 RTGETOPTUNION ValueUnion;
268 rc = RTGetOpt(&GetState, &ValueUnion);
269 switch (rc)
270 {
271 case 0:
272 {
273 /*
274 * If we've processed any files we're done. Otherwise take
275 * input from stdin and write the output to stdout.
276 */
277 if (cProcessed > 0)
278 return rcExit;
279#if 0
280 rc = RTVfsFileFromRTFile(1,
281 RTFILE_O_WRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE,
282 true /*fLeaveOpen*/,
283 &hVfsOut);
284
285
286 if (!fForce && isStdHandleATty(fDecompress ? 0 : 1))
287 return RTMsgErrorExit(RTEXITCODE_SYNTAX,
288 "Yeah, right. I'm not %s any compressed data %s the terminal without --force.\n",
289 fDecompress ? "reading" : "writing",
290 fDecompress ? "from" : "to");
291#else
292 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "reading from standard input has not yet been implemented");
293#endif
294 return rcExit;
295 }
296
297 case VINF_GETOPT_NOT_OPTION:
298 {
299 if (!*pszSuff && !fStdOut)
300 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The --suffix option specified an empty string");
301 if (!fStdOut && RTVfsChainIsSpec(ValueUnion.psz))
302 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Must use standard out with VFS chain specifications");
303
304 RTEXITCODE rcExit2;
305 if (fList)
306 rcExit2 = gzipListFile(ValueUnion.psz, fForce);
307 else if (fTest)
308 rcExit2 = gzipTestFile(ValueUnion.psz, fForce);
309 else if (fDecompress)
310 rcExit2 = gzipDecompressFile(ValueUnion.psz, fStdOut, fForce, &hVfsStdOut);
311 else
312 rcExit2 = gzipCompressFile(ValueUnion.psz, fStdOut, fForce, &hVfsStdOut);
313 if (rcExit2 != RTEXITCODE_SUCCESS)
314 rcExit = rcExit2;
315
316 cProcessed++;
317 break;
318 }
319
320 case 'a': fAscii = true; break;
321 case 'c': fStdOut = true; break;
322 case 'd': fDecompress = true; break;
323 case 'f': fForce = true; break;
324 case 'l': fList = true; break;
325 case 'n': fName = false; break;
326 case 'N': fName = true; break;
327 case 'q': fQuiet = true; break;
328 case 'r': fRecursive = true; break;
329 case 'S': pszSuff = ValueUnion.psz; break;
330 case 't': fTest = true; break;
331 case 'v': fQuiet = false; break;
332 case '1': uLevel = 1; break;
333 case '2': uLevel = 2; break;
334 case '3': uLevel = 3; break;
335 case '4': uLevel = 4; break;
336 case '5': uLevel = 5; break;
337 case '6': uLevel = 6; break;
338 case '7': uLevel = 7; break;
339 case '8': uLevel = 8; break;
340 case '9': uLevel = 9; break;
341
342 case 'h':
343 RTPrintf("Usage: to be written\nOption dump:\n");
344 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
345 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
346 return RTEXITCODE_SUCCESS;
347
348 case 'V':
349 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
350 return RTEXITCODE_SUCCESS;
351
352 default:
353 return RTGetOptPrintError(rc, &ValueUnion);
354 }
355 }
356}
357
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette