VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/tarcmd.cpp@ 62508

Last change on this file since 62508 was 62477, checked in by vboxsync, 9 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.6 KB
Line 
1/* $Id: tarcmd.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
2/** @file
3 * IPRT - A mini TAR Command.
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/asm.h>
34#include <iprt/buildconfig.h>
35#include <iprt/ctype.h>
36#include <iprt/dir.h>
37#include <iprt/file.h>
38#include <iprt/getopt.h>
39#include <iprt/initterm.h>
40#include <iprt/mem.h>
41#include <iprt/message.h>
42#include <iprt/param.h>
43#include <iprt/path.h>
44#include <iprt/stream.h>
45#include <iprt/string.h>
46#include <iprt/symlink.h>
47#include <iprt/vfs.h>
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53#define RTZIPTARCMD_OPT_DELETE 1000
54#define RTZIPTARCMD_OPT_OWNER 1001
55#define RTZIPTARCMD_OPT_GROUP 1002
56#define RTZIPTARCMD_OPT_UTC 1003
57#define RTZIPTARCMD_OPT_PREFIX 1004
58#define RTZIPTARCMD_OPT_FILE_MODE_AND_MASK 1005
59#define RTZIPTARCMD_OPT_FILE_MODE_OR_MASK 1006
60#define RTZIPTARCMD_OPT_DIR_MODE_AND_MASK 1007
61#define RTZIPTARCMD_OPT_DIR_MODE_OR_MASK 1008
62#define RTZIPTARCMD_OPT_FORMAT 1009
63#define RTZIPTARCMD_OPT_READ_AHEAD 1010
64
65/** File format. */
66typedef enum RTZIPTARFORMAT
67{
68 RTZIPTARFORMAT_INVALID = 0,
69 /** Autodetect if possible, defaulting to TAR. */
70 RTZIPTARFORMAT_AUTO_DEFAULT,
71 /** TAR. */
72 RTZIPTARFORMAT_TAR,
73 /** XAR. */
74 RTZIPTARFORMAT_XAR
75} RTZIPTARFORMAT;
76
77
78/*********************************************************************************************************************************
79* Structures and Typedefs *
80*********************************************************************************************************************************/
81/**
82 * IPRT TAR option structure.
83 */
84typedef struct RTZIPTARCMDOPS
85{
86 /** The file format. */
87 RTZIPTARFORMAT enmFormat;
88
89 /** The operation (Acdrtux or RTZIPTARCMD_OPT_DELETE). */
90 int iOperation;
91 /** The long operation option name. */
92 const char *pszOperation;
93
94 /** The directory to change into when packing and unpacking. */
95 const char *pszDirectory;
96 /** The tar file name. */
97 const char *pszFile;
98 /** Whether we're verbose or quiet. */
99 bool fVerbose;
100 /** Whether to preserve the original file owner when restoring. */
101 bool fPreserveOwner;
102 /** Whether to preserve the original file group when restoring. */
103 bool fPreserveGroup;
104 /** Whether to skip restoring the modification time (only time stored by the
105 * traditional TAR format). */
106 bool fNoModTime;
107 /** Whether to add a read ahead thread. */
108 bool fReadAhead;
109 /** The compressor/decompressor method to employ (0, z or j). */
110 char chZipper;
111
112 /** The owner to set. NULL if not applicable.
113 * Always resolved into uidOwner for extraction. */
114 const char *pszOwner;
115 /** The owner ID to set. NIL_RTUID if not applicable. */
116 RTUID uidOwner;
117 /** The group to set. NULL if not applicable.
118 * Always resolved into gidGroup for extraction. */
119 const char *pszGroup;
120 /** The group ID to set. NIL_RTGUID if not applicable. */
121 RTGID gidGroup;
122 /** Display the modification times in UTC instead of local time. */
123 bool fDisplayUtc;
124 /** File mode AND mask. */
125 RTFMODE fFileModeAndMask;
126 /** File mode OR mask. */
127 RTFMODE fFileModeOrMask;
128 /** Directory mode AND mask. */
129 RTFMODE fDirModeAndMask;
130 /** Directory mode OR mask. */
131 RTFMODE fDirModeOrMask;
132
133 /** What to prefix all names with when creating, adding, whatever. */
134 const char *pszPrefix;
135
136 /** The number of files(, directories or whatever) specified. */
137 uint32_t cFiles;
138 /** Array of files(, directories or whatever).
139 * Terminated by a NULL entry. */
140 const char * const *papszFiles;
141} RTZIPTARCMDOPS;
142/** Pointer to the IPRT tar options. */
143typedef RTZIPTARCMDOPS *PRTZIPTARCMDOPS;
144
145/**
146 * Callback used by rtZipTarDoWithMembers
147 *
148 * @returns rcExit or RTEXITCODE_FAILURE.
149 * @param pOpts The tar options.
150 * @param hVfsObj The tar object to display
151 * @param pszName The name.
152 * @param rcExit The current exit code.
153 */
154typedef RTEXITCODE (*PFNDOWITHMEMBER)(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit);
155
156
157/**
158 * Checks if @a pszName is a member of @a papszNames, optionally returning the
159 * index.
160 *
161 * @returns true if the name is in the list, otherwise false.
162 * @param pszName The name to find.
163 * @param papszNames The array of names.
164 * @param piName Where to optionally return the array index.
165 */
166static bool rtZipTarCmdIsNameInArray(const char *pszName, const char * const *papszNames, uint32_t *piName)
167{
168 for (uint32_t iName = 0; papszNames[iName]; iName++)
169 if (!strcmp(papszNames[iName], pszName))
170 {
171 if (piName)
172 *piName = iName;
173 return true;
174 }
175 return false;
176}
177
178
179/**
180 * Opens the input archive specified by the options.
181 *
182 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE + printed message.
183 * @param pOpts The options.
184 * @param phVfsFss Where to return the TAR filesystem stream handle.
185 */
186static RTEXITCODE rtZipTarCmdOpenInputArchive(PRTZIPTARCMDOPS pOpts, PRTVFSFSSTREAM phVfsFss)
187{
188 int rc;
189
190 /*
191 * Open the input file.
192 */
193 RTVFSIOSTREAM hVfsIos;
194 if ( pOpts->pszFile
195 && strcmp(pOpts->pszFile, "-") != 0)
196 {
197 const char *pszError;
198 rc = RTVfsChainOpenIoStream(pOpts->pszFile,
199 RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN,
200 &hVfsIos,
201 &pszError);
202 if (RT_FAILURE(rc))
203 {
204 if (pszError && *pszError)
205 return RTMsgErrorExit(RTEXITCODE_FAILURE,
206 "RTVfsChainOpenIoStream failed with rc=%Rrc:\n"
207 " '%s'\n"
208 " %*s^\n",
209 rc, pOpts->pszFile, pszError - pOpts->pszFile, "");
210 return RTMsgErrorExit(RTEXITCODE_FAILURE,
211 "Failed with %Rrc opening the input archive '%s'", rc, pOpts->pszFile);
212 }
213 }
214 else
215 {
216 rc = RTVfsIoStrmFromStdHandle(RTHANDLESTD_INPUT,
217 RTFILE_O_READ | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN,
218 true /*fLeaveOpen*/,
219 &hVfsIos);
220 if (RT_FAILURE(rc))
221 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to prepare standard in for reading: %Rrc", rc);
222 }
223
224 /*
225 * Pass it thru a decompressor?
226 */
227 RTVFSIOSTREAM hVfsIosDecomp = NIL_RTVFSIOSTREAM;
228 switch (pOpts->chZipper)
229 {
230 /* no */
231 case '\0':
232 rc = VINF_SUCCESS;
233 break;
234
235 /* gunzip */
236 case 'z':
237 rc = RTZipGzipDecompressIoStream(hVfsIos, 0 /*fFlags*/, &hVfsIosDecomp);
238 if (RT_FAILURE(rc))
239 RTMsgError("Failed to open gzip decompressor: %Rrc", rc);
240 break;
241
242 /* bunzip2 */
243 case 'j':
244 rc = VERR_NOT_SUPPORTED;
245 RTMsgError("bzip2 is not supported by this build");
246 break;
247
248 /* bug */
249 default:
250 rc = VERR_INTERNAL_ERROR_2;
251 RTMsgError("unknown decompression method '%c'", pOpts->chZipper);
252 break;
253 }
254 if (RT_FAILURE(rc))
255 {
256 RTVfsIoStrmRelease(hVfsIos);
257 return RTEXITCODE_FAILURE;
258 }
259
260 if (hVfsIosDecomp != NIL_RTVFSIOSTREAM)
261 {
262 RTVfsIoStrmRelease(hVfsIos);
263 hVfsIos = hVfsIosDecomp;
264 hVfsIosDecomp = NIL_RTVFSIOSTREAM;
265 }
266
267 /*
268 * Open the filesystem stream.
269 */
270 if (pOpts->enmFormat == RTZIPTARFORMAT_TAR)
271 rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0/*fFlags*/, phVfsFss);
272 else if (pOpts->enmFormat == RTZIPTARFORMAT_XAR)
273#ifdef IPRT_WITH_XAR /* Requires C++ and XML, so only in some configruation of IPRT. */
274 rc = RTZipXarFsStreamFromIoStream(hVfsIos, 0/*fFlags*/, phVfsFss);
275#else
276 rc = VERR_NOT_SUPPORTED;
277#endif
278 else /** @todo make RTZipTarFsStreamFromIoStream fail if not tar file! */
279 rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0/*fFlags*/, phVfsFss);
280 RTVfsIoStrmRelease(hVfsIos);
281 if (RT_FAILURE(rc))
282 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open tar filesystem stream: %Rrc", rc);
283
284 return RTEXITCODE_SUCCESS;
285}
286
287
288/**
289 * Worker for the --list and --extract commands.
290 *
291 * @returns The appropriate exit code.
292 * @param pOpts The tar options.
293 * @param pfnCallback The command specific callback.
294 */
295static RTEXITCODE rtZipTarDoWithMembers(PRTZIPTARCMDOPS pOpts, PFNDOWITHMEMBER pfnCallback)
296{
297 /*
298 * Allocate a bitmap to go with the file list. This will be used to
299 * indicate which files we've processed and which not.
300 */
301 uint32_t *pbmFound = NULL;
302 if (pOpts->cFiles)
303 {
304 pbmFound = (uint32_t *)RTMemAllocZ(((pOpts->cFiles + 31) / 32) * sizeof(uint32_t));
305 if (!pbmFound)
306 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to allocate the found-file-bitmap");
307 }
308
309
310 /*
311 * Open the input archive.
312 */
313 RTVFSFSSTREAM hVfsFssIn;
314 RTEXITCODE rcExit = rtZipTarCmdOpenInputArchive(pOpts, &hVfsFssIn);
315 if (rcExit == RTEXITCODE_SUCCESS)
316 {
317 /*
318 * Process the stream.
319 */
320 for (;;)
321 {
322 /*
323 * Retrive the next object.
324 */
325 char *pszName;
326 RTVFSOBJ hVfsObj;
327 int rc = RTVfsFsStrmNext(hVfsFssIn, &pszName, NULL, &hVfsObj);
328 if (RT_FAILURE(rc))
329 {
330 if (rc != VERR_EOF)
331 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsFsStrmNext returned %Rrc", rc);
332 break;
333 }
334
335 /*
336 * Should we process this entry?
337 */
338 uint32_t iFile = UINT32_MAX;
339 if ( !pOpts->cFiles
340 || rtZipTarCmdIsNameInArray(pszName, pOpts->papszFiles, &iFile) )
341 {
342 if (pbmFound)
343 ASMBitSet(pbmFound, iFile);
344
345 rcExit = pfnCallback(pOpts, hVfsObj, pszName, rcExit);
346 }
347
348 /*
349 * Release the current object and string.
350 */
351 RTVfsObjRelease(hVfsObj);
352 RTStrFree(pszName);
353 }
354
355 /*
356 * Complain about any files we didn't find.
357 */
358 for (uint32_t iFile = 0; iFile < pOpts->cFiles; iFile++)
359 if (!ASMBitTest(pbmFound, iFile))
360 {
361 RTMsgError("%s: Was not found in the archive", pOpts->papszFiles[iFile]);
362 rcExit = RTEXITCODE_FAILURE;
363 }
364
365 RTVfsFsStrmRelease(hVfsFssIn);
366 }
367 RTMemFree(pbmFound);
368 return rcExit;
369}
370
371
372/**
373 * Checks if the name contains any escape sequences.
374 *
375 * An escape sequence would generally be one or more '..' references. On DOS
376 * like system, something that would make up a drive letter reference is also
377 * considered an escape sequence.
378 *
379 * @returns true / false.
380 * @param pszName The name to consider.
381 */
382static bool rtZipTarHasEscapeSequence(const char *pszName)
383{
384#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
385 if (pszName[0] == ':')
386 return true;
387#endif
388 while (*pszName)
389 {
390 while (RTPATH_IS_SEP(*pszName))
391 pszName++;
392 if ( pszName[0] == '.'
393 && pszName[1] == '.'
394 && (pszName[2] == '\0' || RTPATH_IS_SLASH(pszName[2])) )
395 return true;
396 while (*pszName && !RTPATH_IS_SEP(*pszName))
397 pszName++;
398 }
399
400 return false;
401}
402
403#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
404
405/**
406 * Queries the user ID to use when extracting a member.
407 *
408 * @returns rcExit or RTEXITCODE_FAILURE.
409 * @param pOpts The tar options.
410 * @param pUser The user info.
411 * @param pszName The file name to use when complaining.
412 * @param rcExit The current exit code.
413 * @param pUid Where to return the user ID.
414 */
415static RTEXITCODE rtZipTarQueryExtractOwner(PRTZIPTARCMDOPS pOpts, PCRTFSOBJINFO pOwner, const char *pszName, RTEXITCODE rcExit,
416 PRTUID pUid)
417{
418 if (pOpts->uidOwner != NIL_RTUID)
419 *pUid = pOpts->uidOwner;
420 else if (pOpts->fPreserveGroup)
421 {
422 if (!pOwner->Attr.u.UnixGroup.szName[0])
423 *pUid = pOwner->Attr.u.UnixOwner.uid;
424 else
425 {
426 *pUid = NIL_RTUID;
427 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: User resolving is not implemented.", pszName);
428 }
429 }
430 else
431 *pUid = NIL_RTUID;
432 return rcExit;
433}
434
435
436/**
437 * Queries the group ID to use when extracting a member.
438 *
439 * @returns rcExit or RTEXITCODE_FAILURE.
440 * @param pOpts The tar options.
441 * @param pGroup The group info.
442 * @param pszName The file name to use when complaining.
443 * @param rcExit The current exit code.
444 * @param pGid Where to return the group ID.
445 */
446static RTEXITCODE rtZipTarQueryExtractGroup(PRTZIPTARCMDOPS pOpts, PCRTFSOBJINFO pGroup, const char *pszName, RTEXITCODE rcExit,
447 PRTGID pGid)
448{
449 if (pOpts->gidGroup != NIL_RTGID)
450 *pGid = pOpts->gidGroup;
451 else if (pOpts->fPreserveGroup)
452 {
453 if (!pGroup->Attr.u.UnixGroup.szName[0])
454 *pGid = pGroup->Attr.u.UnixGroup.gid;
455 else
456 {
457 *pGid = NIL_RTGID;
458 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Group resolving is not implemented.", pszName);
459 }
460 }
461 else
462 *pGid = NIL_RTGID;
463 return rcExit;
464}
465
466#endif /* !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2) */
467
468
469/**
470 * Extracts a file.
471 *
472 * Since we can restore permissions and attributes more efficiently by working
473 * directly on the file handle, we have special code path for files.
474 *
475 * @returns rcExit or RTEXITCODE_FAILURE.
476 * @param pOpts The tar options.
477 * @param hVfsObj The tar object to display
478 * @param rcExit The current exit code.
479 * @param pUnixInfo The unix fs object info.
480 * @param pOwner The owner info.
481 * @param pGroup The group info.
482 */
483static RTEXITCODE rtZipTarCmdExtractFile(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, RTEXITCODE rcExit,
484 const char *pszDst, PCRTFSOBJINFO pUnixInfo, PCRTFSOBJINFO pOwner, PCRTFSOBJINFO pGroup)
485{
486 /*
487 * Open the destination file and create a stream object for it.
488 */
489 uint32_t fOpen = RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_ACCESS_ATTR_DEFAULT
490 | ((RTFS_UNIX_IWUSR | RTFS_UNIX_IRUSR) << RTFILE_O_CREATE_MODE_SHIFT);
491 RTFILE hFile;
492 int rc = RTFileOpen(&hFile, pszDst, fOpen);
493 if (RT_FAILURE(rc))
494 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating file: %Rrc", pszDst, rc);
495
496 RTVFSIOSTREAM hVfsIosDst;
497 rc = RTVfsIoStrmFromRTFile(hFile, fOpen, true /*fLeaveOpen*/, &hVfsIosDst);
498 if (RT_SUCCESS(rc))
499 {
500 /*
501 * Convert source to a stream and optionally add a read ahead stage.
502 */
503 RTVFSIOSTREAM hVfsIosSrc = RTVfsObjToIoStream(hVfsObj);
504 if (pOpts->fReadAhead)
505 {
506 RTVFSIOSTREAM hVfsReadAhead;
507 rc = RTVfsCreateReadAheadForIoStream(hVfsIosSrc, 0 /*fFlag*/, 16 /*cBuffers*/, _256K /*cbBuffer*/, &hVfsReadAhead);
508 if (RT_SUCCESS(rc))
509 {
510 RTVfsIoStrmRelease(hVfsIosSrc);
511 hVfsIosSrc = hVfsReadAhead;
512 }
513 else
514 AssertRC(rc); /* can be ignored in release builds. */
515 }
516
517 /*
518 * Pump the data thru.
519 */
520 rc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, (uint32_t)RT_MIN(pUnixInfo->cbObject, _1M));
521 if (RT_SUCCESS(rc))
522 {
523 /*
524 * Correct the file mode and other attributes.
525 */
526 if (!pOpts->fNoModTime)
527 {
528 rc = RTFileSetTimes(hFile, NULL, &pUnixInfo->ModificationTime, NULL, NULL);
529 if (RT_FAILURE(rc))
530 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error setting times: %Rrc", pszDst, rc);
531 }
532
533#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
534 if ( pOpts->uidOwner != NIL_RTUID
535 || pOpts->gidGroup != NIL_RTGID
536 || pOpts->fPreserveOwner
537 || pOpts->fPreserveGroup)
538 {
539 RTUID uidFile;
540 rcExit = rtZipTarQueryExtractOwner(pOpts, pOwner, pszDst, rcExit, &uidFile);
541
542 RTGID gidFile;
543 rcExit = rtZipTarQueryExtractGroup(pOpts, pGroup, pszDst, rcExit, &gidFile);
544 if (uidFile != NIL_RTUID || gidFile != NIL_RTGID)
545 {
546 rc = RTFileSetOwner(hFile, uidFile, gidFile);
547 if (RT_FAILURE(rc))
548 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error owner/group: %Rrc", pszDst, rc);
549 }
550 }
551#endif
552
553 RTFMODE fMode = (pUnixInfo->Attr.fMode & pOpts->fFileModeAndMask) | pOpts->fFileModeOrMask;
554 rc = RTFileSetMode(hFile, fMode | RTFS_TYPE_FILE);
555 if (RT_FAILURE(rc))
556 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing mode: %Rrc", pszDst, rc);
557 }
558 else
559 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error writing out file: %Rrc", pszDst, rc);
560 RTVfsIoStrmRelease(hVfsIosSrc);
561 RTVfsIoStrmRelease(hVfsIosDst);
562 }
563 else
564 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating I/O stream for file: %Rrc", pszDst, rc);
565 RTFileClose(hFile);
566 return rcExit;
567}
568
569
570/**
571 * @callback_method_impl{PFNDOWITHMEMBER, Implements --extract.}
572 */
573static RTEXITCODE rtZipTarCmdExtractCallback(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit)
574{
575 if (pOpts->fVerbose)
576 RTPrintf("%s\n", pszName);
577
578 /*
579 * Query all the information.
580 */
581 RTFSOBJINFO UnixInfo;
582 int rc = RTVfsObjQueryInfo(hVfsObj, &UnixInfo, RTFSOBJATTRADD_UNIX);
583 if (RT_FAILURE(rc))
584 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsObjQueryInfo returned %Rrc on '%s'", rc, pszName);
585
586 RTFSOBJINFO Owner;
587 rc = RTVfsObjQueryInfo(hVfsObj, &Owner, RTFSOBJATTRADD_UNIX_OWNER);
588 if (RT_FAILURE(rc))
589 return RTMsgErrorExit(RTEXITCODE_FAILURE,
590 "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
591 rc, pszName);
592
593 RTFSOBJINFO Group;
594 rc = RTVfsObjQueryInfo(hVfsObj, &Group, RTFSOBJATTRADD_UNIX_GROUP);
595 if (RT_FAILURE(rc))
596 return RTMsgErrorExit(RTEXITCODE_FAILURE,
597 "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
598 rc, pszName);
599
600 char szTarget[RTPATH_MAX];
601 szTarget[0] = '\0';
602 RTVFSSYMLINK hVfsSymlink = RTVfsObjToSymlink(hVfsObj);
603 if (hVfsSymlink != NIL_RTVFSSYMLINK)
604 {
605 rc = RTVfsSymlinkRead(hVfsSymlink, szTarget, sizeof(szTarget));
606 RTVfsSymlinkRelease(hVfsSymlink);
607 if (RT_FAILURE(rc))
608 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: RTVfsSymlinkRead failed: %Rrc", pszName, rc);
609 if (!RTFS_IS_SYMLINK(UnixInfo.Attr.fMode))
610 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Hardlinks are not supported.", pszName);
611 if (!szTarget[0])
612 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Link target is empty.", pszName);
613 }
614 else if (RTFS_IS_SYMLINK(UnixInfo.Attr.fMode))
615 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to get symlink object for '%s'", pszName);
616
617 if (rtZipTarHasEscapeSequence(pszName))
618 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Name '%s' contains an escape sequence.", pszName);
619
620 /*
621 * Construct the path to the extracted member.
622 */
623 char szDst[RTPATH_MAX];
624 rc = RTPathJoin(szDst, sizeof(szDst), pOpts->pszDirectory ? pOpts->pszDirectory : ".", pszName);
625 if (RT_FAILURE(rc))
626 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Failed to construct destination path for: %Rrc", pszName, rc);
627
628 /*
629 * Extract according to the type.
630 */
631 switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK)
632 {
633 case RTFS_TYPE_FILE:
634 return rtZipTarCmdExtractFile(pOpts, hVfsObj, rcExit, szDst, &UnixInfo, &Owner, &Group);
635
636 case RTFS_TYPE_DIRECTORY:
637 rc = RTDirCreateFullPath(szDst, UnixInfo.Attr.fMode & RTFS_UNIX_ALL_ACCESS_PERMS);
638 if (RT_FAILURE(rc))
639 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating directory: %Rrc", szDst, rc);
640 break;
641
642 case RTFS_TYPE_SYMLINK:
643 rc = RTSymlinkCreate(szDst, szTarget, RTSYMLINKTYPE_UNKNOWN, 0);
644 if (RT_FAILURE(rc))
645 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating symbolic link: %Rrc", szDst, rc);
646 break;
647
648 case RTFS_TYPE_FIFO:
649 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: FIFOs are not supported.", pszName);
650 case RTFS_TYPE_DEV_CHAR:
651 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: FIFOs are not supported.", pszName);
652 case RTFS_TYPE_DEV_BLOCK:
653 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Block devices are not supported.", pszName);
654 case RTFS_TYPE_SOCKET:
655 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Sockets are not supported.", pszName);
656 case RTFS_TYPE_WHITEOUT:
657 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Whiteouts are not support.", pszName);
658 default:
659 return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Unknown file type.", pszName);
660 }
661
662 /*
663 * Set other attributes as requested.
664 *
665 * Note! File extraction does get here.
666 */
667 if (!pOpts->fNoModTime)
668 {
669 rc = RTPathSetTimesEx(szDst, NULL, &UnixInfo.ModificationTime, NULL, NULL, RTPATH_F_ON_LINK);
670 if (RT_FAILURE(rc) && rc != VERR_NOT_SUPPORTED && rc != VERR_NS_SYMLINK_SET_TIME)
671 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing modification time: %Rrc.", pszName, rc);
672 }
673
674#if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
675 if ( pOpts->uidOwner != NIL_RTUID
676 || pOpts->gidGroup != NIL_RTGID
677 || pOpts->fPreserveOwner
678 || pOpts->fPreserveGroup)
679 {
680 RTUID uidFile;
681 rcExit = rtZipTarQueryExtractOwner(pOpts, &Owner, szDst, rcExit, &uidFile);
682
683 RTGID gidFile;
684 rcExit = rtZipTarQueryExtractGroup(pOpts, &Group, szDst, rcExit, &gidFile);
685 if (uidFile != NIL_RTUID || gidFile != NIL_RTGID)
686 {
687 rc = RTPathSetOwnerEx(szDst, uidFile, gidFile, RTPATH_F_ON_LINK);
688 if (RT_FAILURE(rc))
689 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error owner/group: %Rrc", szDst, rc);
690 }
691 }
692#endif
693
694#if !defined(RT_OS_WINDOWS) /** @todo implement RTPathSetMode on windows... */
695 if (!RTFS_IS_SYMLINK(UnixInfo.Attr.fMode)) /* RTPathSetMode follows symbolic links atm. */
696 {
697 RTFMODE fMode;
698 if (RTFS_IS_DIRECTORY(UnixInfo.Attr.fMode))
699 fMode = (UnixInfo.Attr.fMode & (pOpts->fDirModeAndMask | RTFS_TYPE_MASK)) | pOpts->fDirModeOrMask;
700 else
701 fMode = (UnixInfo.Attr.fMode & (pOpts->fFileModeAndMask | RTFS_TYPE_MASK)) | pOpts->fFileModeOrMask;
702 rc = RTPathSetMode(szDst, fMode);
703 if (RT_FAILURE(rc))
704 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing mode: %Rrc", szDst, rc);
705 }
706#endif
707
708 return rcExit;
709}
710
711
712/**
713 * @callback_method_impl{PFNDOWITHMEMBER, Implements --list.}
714 */
715static RTEXITCODE rtZipTarCmdListCallback(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit)
716{
717 /*
718 * This is very simple in non-verbose mode.
719 */
720 if (!pOpts->fVerbose)
721 {
722 RTPrintf("%s\n", pszName);
723 return rcExit;
724 }
725
726 /*
727 * Query all the information.
728 */
729 RTFSOBJINFO UnixInfo;
730 int rc = RTVfsObjQueryInfo(hVfsObj, &UnixInfo, RTFSOBJATTRADD_UNIX);
731 if (RT_FAILURE(rc))
732 {
733 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsObjQueryInfo returned %Rrc on '%s'", rc, pszName);
734 RT_ZERO(UnixInfo);
735 }
736
737 RTFSOBJINFO Owner;
738 rc = RTVfsObjQueryInfo(hVfsObj, &Owner, RTFSOBJATTRADD_UNIX_OWNER);
739 if (RT_FAILURE(rc))
740 {
741 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE,
742 "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
743 rc, pszName);
744 RT_ZERO(Owner);
745 }
746
747 RTFSOBJINFO Group;
748 rc = RTVfsObjQueryInfo(hVfsObj, &Group, RTFSOBJATTRADD_UNIX_GROUP);
749 if (RT_FAILURE(rc))
750 {
751 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE,
752 "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
753 rc, pszName);
754 RT_ZERO(Group);
755 }
756
757 const char *pszLinkType = NULL;
758 char szTarget[RTPATH_MAX];
759 szTarget[0] = '\0';
760 RTVFSSYMLINK hVfsSymlink = RTVfsObjToSymlink(hVfsObj);
761 if (hVfsSymlink != NIL_RTVFSSYMLINK)
762 {
763 rc = RTVfsSymlinkRead(hVfsSymlink, szTarget, sizeof(szTarget));
764 if (RT_FAILURE(rc))
765 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsSymlinkRead returned %Rrc on '%s'", rc, pszName);
766 RTVfsSymlinkRelease(hVfsSymlink);
767 pszLinkType = RTFS_IS_SYMLINK(UnixInfo.Attr.fMode) ? "->" : "link to";
768 }
769 else if (RTFS_IS_SYMLINK(UnixInfo.Attr.fMode))
770 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to get symlink object for '%s'", pszName);
771
772 /*
773 * Translate the mode mask.
774 */
775 char szMode[16];
776 switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK)
777 {
778 case RTFS_TYPE_FIFO: szMode[0] = 'f'; break;
779 case RTFS_TYPE_DEV_CHAR: szMode[0] = 'c'; break;
780 case RTFS_TYPE_DIRECTORY: szMode[0] = 'd'; break;
781 case RTFS_TYPE_DEV_BLOCK: szMode[0] = 'b'; break;
782 case RTFS_TYPE_FILE: szMode[0] = '-'; break;
783 case RTFS_TYPE_SYMLINK: szMode[0] = 'l'; break;
784 case RTFS_TYPE_SOCKET: szMode[0] = 's'; break;
785 case RTFS_TYPE_WHITEOUT: szMode[0] = 'w'; break;
786 default: szMode[0] = '?'; break;
787 }
788 if (pszLinkType && szMode[0] != 's')
789 szMode[0] = 'h';
790
791 szMode[1] = UnixInfo.Attr.fMode & RTFS_UNIX_IRUSR ? 'r' : '-';
792 szMode[2] = UnixInfo.Attr.fMode & RTFS_UNIX_IWUSR ? 'w' : '-';
793 szMode[3] = UnixInfo.Attr.fMode & RTFS_UNIX_IXUSR ? 'x' : '-';
794
795 szMode[4] = UnixInfo.Attr.fMode & RTFS_UNIX_IRGRP ? 'r' : '-';
796 szMode[5] = UnixInfo.Attr.fMode & RTFS_UNIX_IWGRP ? 'w' : '-';
797 szMode[6] = UnixInfo.Attr.fMode & RTFS_UNIX_IXGRP ? 'x' : '-';
798
799 szMode[7] = UnixInfo.Attr.fMode & RTFS_UNIX_IROTH ? 'r' : '-';
800 szMode[8] = UnixInfo.Attr.fMode & RTFS_UNIX_IWOTH ? 'w' : '-';
801 szMode[9] = UnixInfo.Attr.fMode & RTFS_UNIX_IXOTH ? 'x' : '-';
802 szMode[10] = '\0';
803
804 /** @todo sticky and set-uid/gid bits. */
805
806 /*
807 * Make sure we've got valid owner and group strings.
808 */
809 if (!Owner.Attr.u.UnixGroup.szName[0])
810 RTStrPrintf(Owner.Attr.u.UnixOwner.szName, sizeof(Owner.Attr.u.UnixOwner.szName),
811 "%u", UnixInfo.Attr.u.Unix.uid);
812
813 if (!Group.Attr.u.UnixOwner.szName[0])
814 RTStrPrintf(Group.Attr.u.UnixGroup.szName, sizeof(Group.Attr.u.UnixGroup.szName),
815 "%u", UnixInfo.Attr.u.Unix.gid);
816
817 /*
818 * Format the modification time.
819 */
820 char szModTime[32];
821 RTTIME ModTime;
822 PRTTIME pTime;
823 if (!pOpts->fDisplayUtc)
824 pTime = RTTimeLocalExplode(&ModTime, &UnixInfo.ModificationTime);
825 else
826 pTime = RTTimeExplode(&ModTime, &UnixInfo.ModificationTime);
827 if (!pTime)
828 RT_ZERO(ModTime);
829 RTStrPrintf(szModTime, sizeof(szModTime), "%04d-%02u-%02u %02u:%02u",
830 ModTime.i32Year, ModTime.u8Month, ModTime.u8MonthDay, ModTime.u8Hour, ModTime.u8Minute);
831
832 /*
833 * Format the size and figure how much space is needed between the
834 * user/group and the size.
835 */
836 char szSize[64];
837 size_t cchSize;
838 switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK)
839 {
840 case RTFS_TYPE_DEV_CHAR:
841 case RTFS_TYPE_DEV_BLOCK:
842 cchSize = RTStrPrintf(szSize, sizeof(szSize), "%u,%u",
843 RTDEV_MAJOR(UnixInfo.Attr.u.Unix.Device), RTDEV_MINOR(UnixInfo.Attr.u.Unix.Device));
844 break;
845 default:
846 cchSize = RTStrPrintf(szSize, sizeof(szSize), "%RU64", UnixInfo.cbObject);
847 break;
848 }
849
850 size_t cchUserGroup = strlen(Owner.Attr.u.UnixOwner.szName)
851 + 1
852 + strlen(Group.Attr.u.UnixGroup.szName);
853 ssize_t cchPad = cchUserGroup + cchSize + 1 < 19
854 ? 19 - (cchUserGroup + cchSize + 1)
855 : 0;
856
857 /*
858 * Go to press.
859 */
860 if (pszLinkType)
861 RTPrintf("%s %s/%s%*s %s %s %s %s %s\n",
862 szMode,
863 Owner.Attr.u.UnixOwner.szName, Group.Attr.u.UnixGroup.szName,
864 cchPad, "",
865 szSize,
866 szModTime,
867 pszName,
868 pszLinkType,
869 szTarget);
870 else
871 RTPrintf("%s %s/%s%*s %s %s %s\n",
872 szMode,
873 Owner.Attr.u.UnixOwner.szName, Group.Attr.u.UnixGroup.szName,
874 cchPad, "",
875 szSize,
876 szModTime,
877 pszName);
878
879 return rcExit;
880}
881
882
883/**
884 * Display usage.
885 *
886 * @param pszProgName The program name.
887 */
888static void rtZipTarUsage(const char *pszProgName)
889{
890 /*
891 * 0 1 2 3 4 5 6 7 8
892 * 012345678901234567890123456789012345678901234567890123456789012345678901234567890
893 */
894 RTPrintf("Usage: %s [options]\n"
895 "\n",
896 pszProgName);
897 RTPrintf("Operations:\n"
898 " -A, --concatenate, --catenate\n"
899 " Append the content of one tar archive to another. (not impl)\n"
900 " -c, --create\n"
901 " Create a new tar archive. (not impl)\n"
902 " -d, --diff, --compare\n"
903 " Compare atar archive with the file system. (not impl)\n"
904 " -r, --append\n"
905 " Append more files to the tar archive. (not impl)\n"
906 " -t, --list\n"
907 " List the contents of the tar archive.\n"
908 " -u, --update\n"
909 " Update the archive, adding files that are newer than the\n"
910 " ones in the archive. (not impl)\n"
911 " -x, --extract, --get\n"
912 " Extract the files from the tar archive.\n"
913 " --delete\n"
914 " Delete files from the tar archive.\n"
915 "\n"
916 );
917 RTPrintf("Basic Options:\n"
918 " -C <dir>, --directory <dir> (-A, -C, -d, -r, -u, -x)\n"
919 " Sets the base directory for input and output file members.\n"
920 " This does not apply to --file, even if it preceeds it.\n"
921 " -f <archive>, --file <archive> (all)\n"
922 " The tar file to create or process. '-' indicates stdout/stdin,\n"
923 " which is is the default.\n"
924 " -v, --verbose (all)\n"
925 " Verbose operation.\n"
926 " -p, --preserve-permissions (-x)\n"
927 " Preserve all permissions when extracting. Must be used\n"
928 " before the mode mask options as it will change some of these.\n"
929 " -j, --bzip2 (all)\n"
930 " Compress/decompress the archive with bzip2.\n"
931 " -z, --gzip, --gunzip, --ungzip (all)\n"
932 " Compress/decompress the archive with gzip.\n"
933 "\n");
934 RTPrintf("Misc Options:\n"
935 " --owner <uid/username> (-A, -C, -d, -r, -u, -x)\n"
936 " Set the owner of extracted and archived files to the user specified.\n"
937 " --group <uid/username> (-A, -C, -d, -r, -u, -x)\n"
938 " Set the group of extracted and archived files to the group specified.\n"
939 " --utc (-t)\n"
940 " Display timestamps as UTC instead of local time.\n"
941 "\n");
942 RTPrintf("IPRT Options:\n"
943 " --prefix <dir-prefix> (-A, -C, -d, -r, -u)\n"
944 " Directory prefix to give the members added to the archive.\n"
945 " --file-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
946 " Restrict the access mode of regular and special files.\n"
947 " --file-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
948 " Include the given access mode for regular and special files.\n"
949 " --dir-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
950 " Restrict the access mode of directories.\n"
951 " --dir-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
952 " Include the given access mode for directories.\n"
953 " --read-ahead (-x)\n"
954 " Enabled read ahead thread when extracting files.\n"
955 "\n");
956 RTPrintf("Standard Options:\n"
957 " -h, -?, --help\n"
958 " Display this help text.\n"
959 " -V, --version\n"
960 " Display version number.\n");
961}
962
963
964RTDECL(RTEXITCODE) RTZipTarCmd(unsigned cArgs, char **papszArgs)
965{
966 /*
967 * Parse the command line.
968 *
969 * N.B. This is less flexible that your regular tar program in that it
970 * requires the operation to be specified as an option. On the other
971 * hand, you can specify it where ever you like in the command line.
972 */
973 static const RTGETOPTDEF s_aOptions[] =
974 {
975 /* operations */
976 { "--concatenate", 'A', RTGETOPT_REQ_NOTHING },
977 { "--catenate", 'A', RTGETOPT_REQ_NOTHING },
978 { "--create", 'c', RTGETOPT_REQ_NOTHING },
979 { "--diff", 'd', RTGETOPT_REQ_NOTHING },
980 { "--compare", 'd', RTGETOPT_REQ_NOTHING },
981 { "--append", 'r', RTGETOPT_REQ_NOTHING },
982 { "--list", 't', RTGETOPT_REQ_NOTHING },
983 { "--update", 'u', RTGETOPT_REQ_NOTHING },
984 { "--extract", 'x', RTGETOPT_REQ_NOTHING },
985 { "--get", 'x', RTGETOPT_REQ_NOTHING },
986 { "--delete", RTZIPTARCMD_OPT_DELETE, RTGETOPT_REQ_NOTHING },
987
988 /* basic options */
989 { "--directory", 'C', RTGETOPT_REQ_STRING },
990 { "--file", 'f', RTGETOPT_REQ_STRING },
991 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
992 { "--preserve-permissions", 'p', RTGETOPT_REQ_NOTHING },
993 { "--bzip2", 'j', RTGETOPT_REQ_NOTHING },
994 { "--gzip", 'z', RTGETOPT_REQ_NOTHING },
995 { "--gunzip", 'z', RTGETOPT_REQ_NOTHING },
996 { "--ungzip", 'z', RTGETOPT_REQ_NOTHING },
997
998 /* other options. */
999 { "--owner", RTZIPTARCMD_OPT_OWNER, RTGETOPT_REQ_STRING },
1000 { "--group", RTZIPTARCMD_OPT_GROUP, RTGETOPT_REQ_STRING },
1001 { "--utc", RTZIPTARCMD_OPT_UTC, RTGETOPT_REQ_NOTHING },
1002
1003 /* IPRT extensions */
1004 { "--prefix", RTZIPTARCMD_OPT_PREFIX, RTGETOPT_REQ_STRING },
1005 { "--file-mode-and-mask", RTZIPTARCMD_OPT_FILE_MODE_AND_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
1006 { "--file-mode-or-mask", RTZIPTARCMD_OPT_FILE_MODE_OR_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
1007 { "--dir-mode-and-mask", RTZIPTARCMD_OPT_DIR_MODE_AND_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
1008 { "--dir-mode-or-mask", RTZIPTARCMD_OPT_DIR_MODE_OR_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
1009 { "--format", RTZIPTARCMD_OPT_FORMAT, RTGETOPT_REQ_STRING },
1010 { "--read-ahead", RTZIPTARCMD_OPT_READ_AHEAD, RTGETOPT_REQ_NOTHING },
1011 };
1012
1013 RTGETOPTSTATE GetState;
1014 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
1015 RTGETOPTINIT_FLAGS_OPTS_FIRST);
1016 if (RT_FAILURE(rc))
1017 return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOpt failed: %Rrc", rc);
1018
1019 RTZIPTARCMDOPS Opts;
1020 RT_ZERO(Opts);
1021 Opts.enmFormat = RTZIPTARFORMAT_AUTO_DEFAULT;
1022 Opts.uidOwner = NIL_RTUID;
1023 Opts.gidGroup = NIL_RTUID;
1024 Opts.fFileModeAndMask = RTFS_UNIX_ALL_ACCESS_PERMS;
1025 Opts.fDirModeAndMask = RTFS_UNIX_ALL_ACCESS_PERMS;
1026#if 0
1027 if (RTPermIsSuperUser())
1028 {
1029 Opts.fFileModeAndMask = RTFS_UNIX_ALL_PERMS;
1030 Opts.fDirModeAndMask = RTFS_UNIX_ALL_PERMS;
1031 Opts.fPreserveOwner = true;
1032 Opts.fPreserveGroup = true;
1033 }
1034#endif
1035
1036 RTGETOPTUNION ValueUnion;
1037 while ( (rc = RTGetOpt(&GetState, &ValueUnion)) != 0
1038 && rc != VINF_GETOPT_NOT_OPTION)
1039 {
1040 switch (rc)
1041 {
1042 /* operations */
1043 case 'A':
1044 case 'c':
1045 case 'd':
1046 case 'r':
1047 case 't':
1048 case 'u':
1049 case 'x':
1050 case RTZIPTARCMD_OPT_DELETE:
1051 if (Opts.iOperation)
1052 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Conflicting tar operation (%s already set, now %s)",
1053 Opts.pszOperation, ValueUnion.pDef->pszLong);
1054 Opts.iOperation = rc;
1055 Opts.pszOperation = ValueUnion.pDef->pszLong;
1056 break;
1057
1058 /* basic options */
1059 case 'C':
1060 if (Opts.pszDirectory)
1061 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify -C/--directory once");
1062 Opts.pszDirectory = ValueUnion.psz;
1063 break;
1064
1065 case 'f':
1066 if (Opts.pszFile)
1067 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify -f/--file once");
1068 Opts.pszFile = ValueUnion.psz;
1069 break;
1070
1071 case 'v':
1072 Opts.fVerbose = true;
1073 break;
1074
1075 case 'p':
1076 Opts.fFileModeAndMask = RTFS_UNIX_ALL_PERMS;
1077 Opts.fDirModeAndMask = RTFS_UNIX_ALL_PERMS;
1078 Opts.fPreserveOwner = true;
1079 Opts.fPreserveGroup = true;
1080 break;
1081
1082 case 'j':
1083 case 'z':
1084 if (Opts.chZipper)
1085 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify one compressor / decompressor");
1086 Opts.chZipper = rc;
1087 break;
1088
1089 case RTZIPTARCMD_OPT_OWNER:
1090 if (Opts.pszOwner)
1091 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify --owner once");
1092 Opts.pszOwner = ValueUnion.psz;
1093
1094 rc = RTStrToUInt32Full(Opts.pszOwner, 0, &ValueUnion.u32);
1095 if (RT_SUCCESS(rc) && rc != VINF_SUCCESS)
1096 return RTMsgErrorExit(RTEXITCODE_SYNTAX,
1097 "Error convering --owner '%s' into a number: %Rrc", Opts.pszOwner, rc);
1098 if (RT_SUCCESS(rc))
1099 {
1100 Opts.uidOwner = ValueUnion.u32;
1101 Opts.pszOwner = NULL;
1102 }
1103 break;
1104
1105 case RTZIPTARCMD_OPT_GROUP:
1106 if (Opts.pszGroup)
1107 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify --group once");
1108 Opts.pszGroup = ValueUnion.psz;
1109
1110 rc = RTStrToUInt32Full(Opts.pszGroup, 0, &ValueUnion.u32);
1111 if (RT_SUCCESS(rc) && rc != VINF_SUCCESS)
1112 return RTMsgErrorExit(RTEXITCODE_SYNTAX,
1113 "Error convering --group '%s' into a number: %Rrc", Opts.pszGroup, rc);
1114 if (RT_SUCCESS(rc))
1115 {
1116 Opts.gidGroup = ValueUnion.u32;
1117 Opts.pszGroup = NULL;
1118 }
1119 break;
1120
1121 case RTZIPTARCMD_OPT_UTC:
1122 Opts.fDisplayUtc = true;
1123 break;
1124
1125 /* iprt extensions */
1126 case RTZIPTARCMD_OPT_PREFIX:
1127 if (Opts.pszPrefix)
1128 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify --prefix once");
1129 Opts.pszPrefix = ValueUnion.psz;
1130 break;
1131
1132 case RTZIPTARCMD_OPT_FILE_MODE_AND_MASK:
1133 Opts.fFileModeAndMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
1134 break;
1135
1136 case RTZIPTARCMD_OPT_FILE_MODE_OR_MASK:
1137 Opts.fFileModeOrMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
1138 break;
1139
1140 case RTZIPTARCMD_OPT_DIR_MODE_AND_MASK:
1141 Opts.fDirModeAndMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
1142 break;
1143
1144 case RTZIPTARCMD_OPT_DIR_MODE_OR_MASK:
1145 Opts.fDirModeOrMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
1146 break;
1147
1148 case RTZIPTARCMD_OPT_FORMAT:
1149 if (!strcmp(ValueUnion.psz, "auto") || !strcmp(ValueUnion.psz, "default"))
1150 Opts.enmFormat = RTZIPTARFORMAT_AUTO_DEFAULT;
1151 else if (!strcmp(ValueUnion.psz, "tar"))
1152 Opts.enmFormat = RTZIPTARFORMAT_TAR;
1153 else if (!strcmp(ValueUnion.psz, "xar"))
1154 Opts.enmFormat = RTZIPTARFORMAT_XAR;
1155 else
1156 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown archive format: '%s'", ValueUnion.psz);
1157 break;
1158
1159 case RTZIPTARCMD_OPT_READ_AHEAD:
1160 Opts.fReadAhead = true;
1161 break;
1162
1163 /* Standard bits. */
1164 case 'h':
1165 rtZipTarUsage(RTPathFilename(papszArgs[0]));
1166 return RTEXITCODE_SUCCESS;
1167
1168 case 'V':
1169 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
1170 return RTEXITCODE_SUCCESS;
1171
1172 default:
1173 return RTGetOptPrintError(rc, &ValueUnion);
1174 }
1175 }
1176
1177 if (rc == VINF_GETOPT_NOT_OPTION)
1178 {
1179 /* this is kind of ugly. */
1180 Assert((unsigned)GetState.iNext - 1 <= cArgs);
1181 Opts.papszFiles = (const char * const *)&papszArgs[GetState.iNext - 1];
1182 Opts.cFiles = cArgs - GetState.iNext + 1;
1183 }
1184
1185 /*
1186 * Post proceess the options.
1187 */
1188 if (Opts.iOperation == 0)
1189 {
1190 Opts.iOperation = 't';
1191 Opts.pszOperation = "--list";
1192 }
1193
1194 if ( Opts.iOperation == 'x'
1195 && Opts.pszOwner)
1196 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The use of --owner with %s has not implemented yet", Opts.pszOperation);
1197
1198 if ( Opts.iOperation == 'x'
1199 && Opts.pszGroup)
1200 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The use of --group with %s has not implemented yet", Opts.pszOperation);
1201
1202 /*
1203 * Do the job.
1204 */
1205 switch (Opts.iOperation)
1206 {
1207 case 't':
1208 return rtZipTarDoWithMembers(&Opts, rtZipTarCmdListCallback);
1209
1210 case 'x':
1211 return rtZipTarDoWithMembers(&Opts, rtZipTarCmdExtractCallback);
1212
1213 case 'A':
1214 case 'c':
1215 case 'd':
1216 case 'r':
1217 case 'u':
1218 case RTZIPTARCMD_OPT_DELETE:
1219 return RTMsgErrorExit(RTEXITCODE_FAILURE, "The operation %s is not implemented yet", Opts.pszOperation);
1220
1221 default:
1222 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Internal error");
1223 }
1224}
1225
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