1 | /* $Id: tarcmd.cpp 59826 2016-02-25 20:59:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - A mini TAR Command.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 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. */
|
---|
66 | typedef 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 | */
|
---|
84 | typedef 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. */
|
---|
143 | typedef 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 | */
|
---|
154 | typedef 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 | */
|
---|
166 | static 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 | */
|
---|
186 | static 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 | */
|
---|
295 | static 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 | */
|
---|
382 | static 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 |
|
---|
404 | /**
|
---|
405 | * Queries the user ID to use when extracting a member.
|
---|
406 | *
|
---|
407 | * @returns rcExit or RTEXITCODE_FAILURE.
|
---|
408 | * @param pOpts The tar options.
|
---|
409 | * @param pUser The user info.
|
---|
410 | * @param pszName The file name to use when complaining.
|
---|
411 | * @param rcExit The current exit code.
|
---|
412 | * @param pUid Where to return the user ID.
|
---|
413 | */
|
---|
414 | static RTEXITCODE rtZipTarQueryExtractOwner(PRTZIPTARCMDOPS pOpts, PCRTFSOBJINFO pOwner, const char *pszName, RTEXITCODE rcExit,
|
---|
415 | PRTUID pUid)
|
---|
416 | {
|
---|
417 | if (pOpts->uidOwner != NIL_RTUID)
|
---|
418 | *pUid = pOpts->uidOwner;
|
---|
419 | else if (pOpts->fPreserveGroup)
|
---|
420 | {
|
---|
421 | if (!pOwner->Attr.u.UnixGroup.szName[0])
|
---|
422 | *pUid = pOwner->Attr.u.UnixOwner.uid;
|
---|
423 | else
|
---|
424 | {
|
---|
425 | *pUid = NIL_RTUID;
|
---|
426 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: User resolving is not implemented.", pszName);
|
---|
427 | }
|
---|
428 | }
|
---|
429 | else
|
---|
430 | *pUid = NIL_RTUID;
|
---|
431 | return rcExit;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Queries the group ID to use when extracting a member.
|
---|
437 | *
|
---|
438 | * @returns rcExit or RTEXITCODE_FAILURE.
|
---|
439 | * @param pOpts The tar options.
|
---|
440 | * @param pGroup The group info.
|
---|
441 | * @param pszName The file name to use when complaining.
|
---|
442 | * @param rcExit The current exit code.
|
---|
443 | * @param pGid Where to return the group ID.
|
---|
444 | */
|
---|
445 | static RTEXITCODE rtZipTarQueryExtractGroup(PRTZIPTARCMDOPS pOpts, PCRTFSOBJINFO pGroup, const char *pszName, RTEXITCODE rcExit,
|
---|
446 | PRTGID pGid)
|
---|
447 | {
|
---|
448 | if (pOpts->gidGroup != NIL_RTGID)
|
---|
449 | *pGid = pOpts->gidGroup;
|
---|
450 | else if (pOpts->fPreserveGroup)
|
---|
451 | {
|
---|
452 | if (!pGroup->Attr.u.UnixGroup.szName[0])
|
---|
453 | *pGid = pGroup->Attr.u.UnixGroup.gid;
|
---|
454 | else
|
---|
455 | {
|
---|
456 | *pGid = NIL_RTGID;
|
---|
457 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Group resolving is not implemented.", pszName);
|
---|
458 | }
|
---|
459 | }
|
---|
460 | else
|
---|
461 | *pGid = NIL_RTGID;
|
---|
462 | return rcExit;
|
---|
463 | }
|
---|
464 |
|
---|
465 |
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Extracts a file.
|
---|
469 | *
|
---|
470 | * Since we can restore permissions and attributes more efficiently by working
|
---|
471 | * directly on the file handle, we have special code path for files.
|
---|
472 | *
|
---|
473 | * @returns rcExit or RTEXITCODE_FAILURE.
|
---|
474 | * @param pOpts The tar options.
|
---|
475 | * @param hVfsObj The tar object to display
|
---|
476 | * @param rcExit The current exit code.
|
---|
477 | * @param pUnixInfo The unix fs object info.
|
---|
478 | * @param pOwner The owner info.
|
---|
479 | * @param pGroup The group info.
|
---|
480 | */
|
---|
481 | static RTEXITCODE rtZipTarCmdExtractFile(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, RTEXITCODE rcExit,
|
---|
482 | const char *pszDst, PCRTFSOBJINFO pUnixInfo, PCRTFSOBJINFO pOwner, PCRTFSOBJINFO pGroup)
|
---|
483 | {
|
---|
484 | /*
|
---|
485 | * Open the destination file and create a stream object for it.
|
---|
486 | */
|
---|
487 | uint32_t fOpen = RTFILE_O_READWRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_ACCESS_ATTR_DEFAULT
|
---|
488 | | ((RTFS_UNIX_IWUSR | RTFS_UNIX_IRUSR) << RTFILE_O_CREATE_MODE_SHIFT);
|
---|
489 | RTFILE hFile;
|
---|
490 | int rc = RTFileOpen(&hFile, pszDst, fOpen);
|
---|
491 | if (RT_FAILURE(rc))
|
---|
492 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating file: %Rrc", pszDst, rc);
|
---|
493 |
|
---|
494 | RTVFSIOSTREAM hVfsIosDst;
|
---|
495 | rc = RTVfsIoStrmFromRTFile(hFile, fOpen, true /*fLeaveOpen*/, &hVfsIosDst);
|
---|
496 | if (RT_SUCCESS(rc))
|
---|
497 | {
|
---|
498 | /*
|
---|
499 | * Convert source to a stream and optionally add a read ahead stage.
|
---|
500 | */
|
---|
501 | RTVFSIOSTREAM hVfsIosSrc = RTVfsObjToIoStream(hVfsObj);
|
---|
502 | if (pOpts->fReadAhead)
|
---|
503 | {
|
---|
504 | RTVFSIOSTREAM hVfsReadAhead;
|
---|
505 | rc = RTVfsCreateReadAheadForIoStream(hVfsIosSrc, 0 /*fFlag*/, 16 /*cBuffers*/, _256K /*cbBuffer*/, &hVfsReadAhead);
|
---|
506 | if (RT_SUCCESS(rc))
|
---|
507 | {
|
---|
508 | RTVfsIoStrmRelease(hVfsIosSrc);
|
---|
509 | hVfsIosSrc = hVfsReadAhead;
|
---|
510 | }
|
---|
511 | else
|
---|
512 | AssertRC(rc); /* can be ignored in release builds. */
|
---|
513 | }
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * Pump the data thru.
|
---|
517 | */
|
---|
518 | rc = RTVfsUtilPumpIoStreams(hVfsIosSrc, hVfsIosDst, (uint32_t)RT_MIN(pUnixInfo->cbObject, _1M));
|
---|
519 | if (RT_SUCCESS(rc))
|
---|
520 | {
|
---|
521 | /*
|
---|
522 | * Correct the file mode and other attributes.
|
---|
523 | */
|
---|
524 | if (!pOpts->fNoModTime)
|
---|
525 | {
|
---|
526 | rc = RTFileSetTimes(hFile, NULL, &pUnixInfo->ModificationTime, NULL, NULL);
|
---|
527 | if (RT_FAILURE(rc))
|
---|
528 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error setting times: %Rrc", pszDst, rc);
|
---|
529 | }
|
---|
530 |
|
---|
531 | #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|
---|
532 | if ( pOpts->uidOwner != NIL_RTUID
|
---|
533 | || pOpts->gidGroup != NIL_RTGID
|
---|
534 | || pOpts->fPreserveOwner
|
---|
535 | || pOpts->fPreserveGroup)
|
---|
536 | {
|
---|
537 | RTUID uidFile;
|
---|
538 | rcExit = rtZipTarQueryExtractOwner(pOpts, pOwner, pszDst, rcExit, &uidFile);
|
---|
539 |
|
---|
540 | RTGID gidFile;
|
---|
541 | rcExit = rtZipTarQueryExtractGroup(pOpts, pGroup, pszDst, rcExit, &gidFile);
|
---|
542 | if (uidFile != NIL_RTUID || gidFile != NIL_RTGID)
|
---|
543 | {
|
---|
544 | rc = RTFileSetOwner(hFile, uidFile, gidFile);
|
---|
545 | if (RT_FAILURE(rc))
|
---|
546 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error owner/group: %Rrc", pszDst, rc);
|
---|
547 | }
|
---|
548 | }
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | RTFMODE fMode = (pUnixInfo->Attr.fMode & pOpts->fFileModeAndMask) | pOpts->fFileModeOrMask;
|
---|
552 | rc = RTFileSetMode(hFile, fMode | RTFS_TYPE_FILE);
|
---|
553 | if (RT_FAILURE(rc))
|
---|
554 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing mode: %Rrc", pszDst, rc);
|
---|
555 | }
|
---|
556 | else
|
---|
557 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error writing out file: %Rrc", pszDst, rc);
|
---|
558 | RTVfsIoStrmRelease(hVfsIosSrc);
|
---|
559 | RTVfsIoStrmRelease(hVfsIosDst);
|
---|
560 | }
|
---|
561 | else
|
---|
562 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating I/O stream for file: %Rrc", pszDst, rc);
|
---|
563 | RTFileClose(hFile);
|
---|
564 | return rcExit;
|
---|
565 | }
|
---|
566 |
|
---|
567 |
|
---|
568 | /**
|
---|
569 | * @callback_method_impl{PFNDOWITHMEMBER, Implements --extract.}
|
---|
570 | */
|
---|
571 | static RTEXITCODE rtZipTarCmdExtractCallback(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit)
|
---|
572 | {
|
---|
573 | if (pOpts->fVerbose)
|
---|
574 | RTPrintf("%s\n", pszName);
|
---|
575 |
|
---|
576 | /*
|
---|
577 | * Query all the information.
|
---|
578 | */
|
---|
579 | RTFSOBJINFO UnixInfo;
|
---|
580 | int rc = RTVfsObjQueryInfo(hVfsObj, &UnixInfo, RTFSOBJATTRADD_UNIX);
|
---|
581 | if (RT_FAILURE(rc))
|
---|
582 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsObjQueryInfo returned %Rrc on '%s'", rc, pszName);
|
---|
583 |
|
---|
584 | RTFSOBJINFO Owner;
|
---|
585 | rc = RTVfsObjQueryInfo(hVfsObj, &Owner, RTFSOBJATTRADD_UNIX_OWNER);
|
---|
586 | if (RT_FAILURE(rc))
|
---|
587 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
588 | "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
|
---|
589 | rc, pszName);
|
---|
590 |
|
---|
591 | RTFSOBJINFO Group;
|
---|
592 | rc = RTVfsObjQueryInfo(hVfsObj, &Group, RTFSOBJATTRADD_UNIX_GROUP);
|
---|
593 | if (RT_FAILURE(rc))
|
---|
594 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
595 | "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
|
---|
596 | rc, pszName);
|
---|
597 |
|
---|
598 | const char *pszLinkType = NULL;
|
---|
599 | char szTarget[RTPATH_MAX];
|
---|
600 | szTarget[0] = '\0';
|
---|
601 | RTVFSSYMLINK hVfsSymlink = RTVfsObjToSymlink(hVfsObj);
|
---|
602 | if (hVfsSymlink != NIL_RTVFSSYMLINK)
|
---|
603 | {
|
---|
604 | rc = RTVfsSymlinkRead(hVfsSymlink, szTarget, sizeof(szTarget));
|
---|
605 | RTVfsSymlinkRelease(hVfsSymlink);
|
---|
606 | if (RT_FAILURE(rc))
|
---|
607 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: RTVfsSymlinkRead failed: %Rrc", pszName, rc);
|
---|
608 | if (!RTFS_IS_SYMLINK(UnixInfo.Attr.fMode))
|
---|
609 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Hardlinks are not supported.", pszName);
|
---|
610 | if (!szTarget[0])
|
---|
611 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Link target is empty.", pszName);
|
---|
612 | }
|
---|
613 | else if (RTFS_IS_SYMLINK(UnixInfo.Attr.fMode))
|
---|
614 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to get symlink object for '%s'", pszName);
|
---|
615 |
|
---|
616 | if (rtZipTarHasEscapeSequence(pszName))
|
---|
617 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Name '%s' contains an escape sequence.", pszName);
|
---|
618 |
|
---|
619 | /*
|
---|
620 | * Construct the path to the extracted member.
|
---|
621 | */
|
---|
622 | char szDst[RTPATH_MAX];
|
---|
623 | rc = RTPathJoin(szDst, sizeof(szDst), pOpts->pszDirectory ? pOpts->pszDirectory : ".", pszName);
|
---|
624 | if (RT_FAILURE(rc))
|
---|
625 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Failed to construct destination path for: %Rrc", pszName, rc);
|
---|
626 |
|
---|
627 | /*
|
---|
628 | * Extract according to the type.
|
---|
629 | */
|
---|
630 | switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK)
|
---|
631 | {
|
---|
632 | case RTFS_TYPE_FILE:
|
---|
633 | return rtZipTarCmdExtractFile(pOpts, hVfsObj, rcExit, szDst, &UnixInfo, &Owner, &Group);
|
---|
634 |
|
---|
635 | case RTFS_TYPE_DIRECTORY:
|
---|
636 | rc = RTDirCreateFullPath(szDst, UnixInfo.Attr.fMode & RTFS_UNIX_ALL_ACCESS_PERMS);
|
---|
637 | if (RT_FAILURE(rc))
|
---|
638 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating directory: %Rrc", szDst, rc);
|
---|
639 | break;
|
---|
640 |
|
---|
641 | case RTFS_TYPE_SYMLINK:
|
---|
642 | rc = RTSymlinkCreate(szDst, szTarget, RTSYMLINKTYPE_UNKNOWN, 0);
|
---|
643 | if (RT_FAILURE(rc))
|
---|
644 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error creating symbolic link: %Rrc", szDst, rc);
|
---|
645 | break;
|
---|
646 |
|
---|
647 | case RTFS_TYPE_FIFO:
|
---|
648 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: FIFOs are not supported.", pszName);
|
---|
649 | case RTFS_TYPE_DEV_CHAR:
|
---|
650 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: FIFOs are not supported.", pszName);
|
---|
651 | case RTFS_TYPE_DEV_BLOCK:
|
---|
652 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Block devices are not supported.", pszName);
|
---|
653 | case RTFS_TYPE_SOCKET:
|
---|
654 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Sockets are not supported.", pszName);
|
---|
655 | case RTFS_TYPE_WHITEOUT:
|
---|
656 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Whiteouts are not support.", pszName);
|
---|
657 | default:
|
---|
658 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Unknown file type.", pszName);
|
---|
659 | }
|
---|
660 |
|
---|
661 | /*
|
---|
662 | * Set other attributes as requested.
|
---|
663 | *
|
---|
664 | * Note! File extraction does get here.
|
---|
665 | */
|
---|
666 | if (!pOpts->fNoModTime)
|
---|
667 | {
|
---|
668 | rc = RTPathSetTimesEx(szDst, NULL, &UnixInfo.ModificationTime, NULL, NULL, RTPATH_F_ON_LINK);
|
---|
669 | if (RT_FAILURE(rc) && rc != VERR_NOT_SUPPORTED && rc != VERR_NS_SYMLINK_SET_TIME)
|
---|
670 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing modification time: %Rrc.", pszName, rc);
|
---|
671 | }
|
---|
672 |
|
---|
673 | #if !defined(RT_OS_WINDOWS) && !defined(RT_OS_OS2)
|
---|
674 | if ( pOpts->uidOwner != NIL_RTUID
|
---|
675 | || pOpts->gidGroup != NIL_RTGID
|
---|
676 | || pOpts->fPreserveOwner
|
---|
677 | || pOpts->fPreserveGroup)
|
---|
678 | {
|
---|
679 | RTUID uidFile;
|
---|
680 | rcExit = rtZipTarQueryExtractOwner(pOpts, &Owner, szDst, rcExit, &uidFile);
|
---|
681 |
|
---|
682 | RTGID gidFile;
|
---|
683 | rcExit = rtZipTarQueryExtractGroup(pOpts, &Group, szDst, rcExit, &gidFile);
|
---|
684 | if (uidFile != NIL_RTUID || gidFile != NIL_RTGID)
|
---|
685 | {
|
---|
686 | rc = RTPathSetOwnerEx(szDst, uidFile, gidFile, RTPATH_F_ON_LINK);
|
---|
687 | if (RT_FAILURE(rc))
|
---|
688 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error owner/group: %Rrc", szDst, rc);
|
---|
689 | }
|
---|
690 | }
|
---|
691 | #endif
|
---|
692 |
|
---|
693 | #if !defined(RT_OS_WINDOWS) /** @todo implement RTPathSetMode on windows... */
|
---|
694 | if (!RTFS_IS_SYMLINK(UnixInfo.Attr.fMode)) /* RTPathSetMode follows symbolic links atm. */
|
---|
695 | {
|
---|
696 | RTFMODE fMode;
|
---|
697 | if (RTFS_IS_DIRECTORY(UnixInfo.Attr.fMode))
|
---|
698 | fMode = (UnixInfo.Attr.fMode & (pOpts->fDirModeAndMask | RTFS_TYPE_MASK)) | pOpts->fDirModeOrMask;
|
---|
699 | else
|
---|
700 | fMode = (UnixInfo.Attr.fMode & (pOpts->fFileModeAndMask | RTFS_TYPE_MASK)) | pOpts->fFileModeOrMask;
|
---|
701 | rc = RTPathSetMode(szDst, fMode);
|
---|
702 | if (RT_FAILURE(rc))
|
---|
703 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "%s: Error changing mode: %Rrc", szDst, rc);
|
---|
704 | }
|
---|
705 | #endif
|
---|
706 |
|
---|
707 | return rcExit;
|
---|
708 | }
|
---|
709 |
|
---|
710 |
|
---|
711 | /**
|
---|
712 | * @callback_method_impl{PFNDOWITHMEMBER, Implements --list.}
|
---|
713 | */
|
---|
714 | static RTEXITCODE rtZipTarCmdListCallback(PRTZIPTARCMDOPS pOpts, RTVFSOBJ hVfsObj, const char *pszName, RTEXITCODE rcExit)
|
---|
715 | {
|
---|
716 | /*
|
---|
717 | * This is very simple in non-verbose mode.
|
---|
718 | */
|
---|
719 | if (!pOpts->fVerbose)
|
---|
720 | {
|
---|
721 | RTPrintf("%s\n", pszName);
|
---|
722 | return rcExit;
|
---|
723 | }
|
---|
724 |
|
---|
725 | /*
|
---|
726 | * Query all the information.
|
---|
727 | */
|
---|
728 | RTFSOBJINFO UnixInfo;
|
---|
729 | int rc = RTVfsObjQueryInfo(hVfsObj, &UnixInfo, RTFSOBJATTRADD_UNIX);
|
---|
730 | if (RT_FAILURE(rc))
|
---|
731 | {
|
---|
732 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsObjQueryInfo returned %Rrc on '%s'", rc, pszName);
|
---|
733 | RT_ZERO(UnixInfo);
|
---|
734 | }
|
---|
735 |
|
---|
736 | RTFSOBJINFO Owner;
|
---|
737 | rc = RTVfsObjQueryInfo(hVfsObj, &Owner, RTFSOBJATTRADD_UNIX_OWNER);
|
---|
738 | if (RT_FAILURE(rc))
|
---|
739 | {
|
---|
740 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
741 | "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
|
---|
742 | rc, pszName);
|
---|
743 | RT_ZERO(Owner);
|
---|
744 | }
|
---|
745 |
|
---|
746 | RTFSOBJINFO Group;
|
---|
747 | rc = RTVfsObjQueryInfo(hVfsObj, &Group, RTFSOBJATTRADD_UNIX_GROUP);
|
---|
748 | if (RT_FAILURE(rc))
|
---|
749 | {
|
---|
750 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
751 | "RTVfsObjQueryInfo(,,UNIX_OWNER) returned %Rrc on '%s'",
|
---|
752 | rc, pszName);
|
---|
753 | RT_ZERO(Group);
|
---|
754 | }
|
---|
755 |
|
---|
756 | const char *pszLinkType = NULL;
|
---|
757 | char szTarget[RTPATH_MAX];
|
---|
758 | szTarget[0] = '\0';
|
---|
759 | RTVFSSYMLINK hVfsSymlink = RTVfsObjToSymlink(hVfsObj);
|
---|
760 | if (hVfsSymlink != NIL_RTVFSSYMLINK)
|
---|
761 | {
|
---|
762 | rc = RTVfsSymlinkRead(hVfsSymlink, szTarget, sizeof(szTarget));
|
---|
763 | if (RT_FAILURE(rc))
|
---|
764 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "RTVfsSymlinkRead returned %Rrc on '%s'", rc, pszName);
|
---|
765 | RTVfsSymlinkRelease(hVfsSymlink);
|
---|
766 | pszLinkType = RTFS_IS_SYMLINK(UnixInfo.Attr.fMode) ? "->" : "link to";
|
---|
767 | }
|
---|
768 | else if (RTFS_IS_SYMLINK(UnixInfo.Attr.fMode))
|
---|
769 | rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to get symlink object for '%s'", pszName);
|
---|
770 |
|
---|
771 | /*
|
---|
772 | * Translate the mode mask.
|
---|
773 | */
|
---|
774 | char szMode[16];
|
---|
775 | switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK)
|
---|
776 | {
|
---|
777 | case RTFS_TYPE_FIFO: szMode[0] = 'f'; break;
|
---|
778 | case RTFS_TYPE_DEV_CHAR: szMode[0] = 'c'; break;
|
---|
779 | case RTFS_TYPE_DIRECTORY: szMode[0] = 'd'; break;
|
---|
780 | case RTFS_TYPE_DEV_BLOCK: szMode[0] = 'b'; break;
|
---|
781 | case RTFS_TYPE_FILE: szMode[0] = '-'; break;
|
---|
782 | case RTFS_TYPE_SYMLINK: szMode[0] = 'l'; break;
|
---|
783 | case RTFS_TYPE_SOCKET: szMode[0] = 's'; break;
|
---|
784 | case RTFS_TYPE_WHITEOUT: szMode[0] = 'w'; break;
|
---|
785 | default: szMode[0] = '?'; break;
|
---|
786 | }
|
---|
787 | if (pszLinkType && szMode[0] != 's')
|
---|
788 | szMode[0] = 'h';
|
---|
789 |
|
---|
790 | szMode[1] = UnixInfo.Attr.fMode & RTFS_UNIX_IRUSR ? 'r' : '-';
|
---|
791 | szMode[2] = UnixInfo.Attr.fMode & RTFS_UNIX_IWUSR ? 'w' : '-';
|
---|
792 | szMode[3] = UnixInfo.Attr.fMode & RTFS_UNIX_IXUSR ? 'x' : '-';
|
---|
793 |
|
---|
794 | szMode[4] = UnixInfo.Attr.fMode & RTFS_UNIX_IRGRP ? 'r' : '-';
|
---|
795 | szMode[5] = UnixInfo.Attr.fMode & RTFS_UNIX_IWGRP ? 'w' : '-';
|
---|
796 | szMode[6] = UnixInfo.Attr.fMode & RTFS_UNIX_IXGRP ? 'x' : '-';
|
---|
797 |
|
---|
798 | szMode[7] = UnixInfo.Attr.fMode & RTFS_UNIX_IROTH ? 'r' : '-';
|
---|
799 | szMode[8] = UnixInfo.Attr.fMode & RTFS_UNIX_IWOTH ? 'w' : '-';
|
---|
800 | szMode[9] = UnixInfo.Attr.fMode & RTFS_UNIX_IXOTH ? 'x' : '-';
|
---|
801 | szMode[10] = '\0';
|
---|
802 |
|
---|
803 | /** @todo sticky and set-uid/gid bits. */
|
---|
804 |
|
---|
805 | /*
|
---|
806 | * Make sure we've got valid owner and group strings.
|
---|
807 | */
|
---|
808 | if (!Owner.Attr.u.UnixGroup.szName[0])
|
---|
809 | RTStrPrintf(Owner.Attr.u.UnixOwner.szName, sizeof(Owner.Attr.u.UnixOwner.szName),
|
---|
810 | "%u", UnixInfo.Attr.u.Unix.uid);
|
---|
811 |
|
---|
812 | if (!Group.Attr.u.UnixOwner.szName[0])
|
---|
813 | RTStrPrintf(Group.Attr.u.UnixGroup.szName, sizeof(Group.Attr.u.UnixGroup.szName),
|
---|
814 | "%u", UnixInfo.Attr.u.Unix.gid);
|
---|
815 |
|
---|
816 | /*
|
---|
817 | * Format the modification time.
|
---|
818 | */
|
---|
819 | char szModTime[32];
|
---|
820 | RTTIME ModTime;
|
---|
821 | PRTTIME pTime;
|
---|
822 | if (!pOpts->fDisplayUtc)
|
---|
823 | pTime = RTTimeLocalExplode(&ModTime, &UnixInfo.ModificationTime);
|
---|
824 | else
|
---|
825 | pTime = RTTimeExplode(&ModTime, &UnixInfo.ModificationTime);
|
---|
826 | if (!pTime)
|
---|
827 | RT_ZERO(ModTime);
|
---|
828 | RTStrPrintf(szModTime, sizeof(szModTime), "%04d-%02u-%02u %02u:%02u",
|
---|
829 | ModTime.i32Year, ModTime.u8Month, ModTime.u8MonthDay, ModTime.u8Hour, ModTime.u8Minute);
|
---|
830 |
|
---|
831 | /*
|
---|
832 | * Format the size and figure how much space is needed between the
|
---|
833 | * user/group and the size.
|
---|
834 | */
|
---|
835 | char szSize[64];
|
---|
836 | size_t cchSize;
|
---|
837 | switch (UnixInfo.Attr.fMode & RTFS_TYPE_MASK)
|
---|
838 | {
|
---|
839 | case RTFS_TYPE_DEV_CHAR:
|
---|
840 | case RTFS_TYPE_DEV_BLOCK:
|
---|
841 | cchSize = RTStrPrintf(szSize, sizeof(szSize), "%u,%u",
|
---|
842 | RTDEV_MAJOR(UnixInfo.Attr.u.Unix.Device), RTDEV_MINOR(UnixInfo.Attr.u.Unix.Device));
|
---|
843 | break;
|
---|
844 | default:
|
---|
845 | cchSize = RTStrPrintf(szSize, sizeof(szSize), "%RU64", UnixInfo.cbObject);
|
---|
846 | break;
|
---|
847 | }
|
---|
848 |
|
---|
849 | size_t cchUserGroup = strlen(Owner.Attr.u.UnixOwner.szName)
|
---|
850 | + 1
|
---|
851 | + strlen(Group.Attr.u.UnixGroup.szName);
|
---|
852 | ssize_t cchPad = cchUserGroup + cchSize + 1 < 19
|
---|
853 | ? 19 - (cchUserGroup + cchSize + 1)
|
---|
854 | : 0;
|
---|
855 |
|
---|
856 | /*
|
---|
857 | * Go to press.
|
---|
858 | */
|
---|
859 | if (pszLinkType)
|
---|
860 | RTPrintf("%s %s/%s%*s %s %s %s %s %s\n",
|
---|
861 | szMode,
|
---|
862 | Owner.Attr.u.UnixOwner.szName, Group.Attr.u.UnixGroup.szName,
|
---|
863 | cchPad, "",
|
---|
864 | szSize,
|
---|
865 | szModTime,
|
---|
866 | pszName,
|
---|
867 | pszLinkType,
|
---|
868 | szTarget);
|
---|
869 | else
|
---|
870 | RTPrintf("%s %s/%s%*s %s %s %s\n",
|
---|
871 | szMode,
|
---|
872 | Owner.Attr.u.UnixOwner.szName, Group.Attr.u.UnixGroup.szName,
|
---|
873 | cchPad, "",
|
---|
874 | szSize,
|
---|
875 | szModTime,
|
---|
876 | pszName);
|
---|
877 |
|
---|
878 | return rcExit;
|
---|
879 | }
|
---|
880 |
|
---|
881 |
|
---|
882 | /**
|
---|
883 | * Display usage.
|
---|
884 | *
|
---|
885 | * @param pszProgName The program name.
|
---|
886 | */
|
---|
887 | static void rtZipTarUsage(const char *pszProgName)
|
---|
888 | {
|
---|
889 | /*
|
---|
890 | * 0 1 2 3 4 5 6 7 8
|
---|
891 | * 012345678901234567890123456789012345678901234567890123456789012345678901234567890
|
---|
892 | */
|
---|
893 | RTPrintf("Usage: %s [options]\n"
|
---|
894 | "\n",
|
---|
895 | pszProgName);
|
---|
896 | RTPrintf("Operations:\n"
|
---|
897 | " -A, --concatenate, --catenate\n"
|
---|
898 | " Append the content of one tar archive to another. (not impl)\n"
|
---|
899 | " -c, --create\n"
|
---|
900 | " Create a new tar archive. (not impl)\n"
|
---|
901 | " -d, --diff, --compare\n"
|
---|
902 | " Compare atar archive with the file system. (not impl)\n"
|
---|
903 | " -r, --append\n"
|
---|
904 | " Append more files to the tar archive. (not impl)\n"
|
---|
905 | " -t, --list\n"
|
---|
906 | " List the contents of the tar archive.\n"
|
---|
907 | " -u, --update\n"
|
---|
908 | " Update the archive, adding files that are newer than the\n"
|
---|
909 | " ones in the archive. (not impl)\n"
|
---|
910 | " -x, --extract, --get\n"
|
---|
911 | " Extract the files from the tar archive.\n"
|
---|
912 | " --delete\n"
|
---|
913 | " Delete files from the tar archive.\n"
|
---|
914 | "\n"
|
---|
915 | );
|
---|
916 | RTPrintf("Basic Options:\n"
|
---|
917 | " -C <dir>, --directory <dir> (-A, -C, -d, -r, -u, -x)\n"
|
---|
918 | " Sets the base directory for input and output file members.\n"
|
---|
919 | " This does not apply to --file, even if it preceeds it.\n"
|
---|
920 | " -f <archive>, --file <archive> (all)\n"
|
---|
921 | " The tar file to create or process. '-' indicates stdout/stdin,\n"
|
---|
922 | " which is is the default.\n"
|
---|
923 | " -v, --verbose (all)\n"
|
---|
924 | " Verbose operation.\n"
|
---|
925 | " -p, --preserve-permissions (-x)\n"
|
---|
926 | " Preserve all permissions when extracting. Must be used\n"
|
---|
927 | " before the mode mask options as it will change some of these.\n"
|
---|
928 | " -j, --bzip2 (all)\n"
|
---|
929 | " Compress/decompress the archive with bzip2.\n"
|
---|
930 | " -z, --gzip, --gunzip, --ungzip (all)\n"
|
---|
931 | " Compress/decompress the archive with gzip.\n"
|
---|
932 | "\n");
|
---|
933 | RTPrintf("Misc Options:\n"
|
---|
934 | " --owner <uid/username> (-A, -C, -d, -r, -u, -x)\n"
|
---|
935 | " Set the owner of extracted and archived files to the user specified.\n"
|
---|
936 | " --group <uid/username> (-A, -C, -d, -r, -u, -x)\n"
|
---|
937 | " Set the group of extracted and archived files to the group specified.\n"
|
---|
938 | " --utc (-t)\n"
|
---|
939 | " Display timestamps as UTC instead of local time.\n"
|
---|
940 | "\n");
|
---|
941 | RTPrintf("IPRT Options:\n"
|
---|
942 | " --prefix <dir-prefix> (-A, -C, -d, -r, -u)\n"
|
---|
943 | " Directory prefix to give the members added to the archive.\n"
|
---|
944 | " --file-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
|
---|
945 | " Restrict the access mode of regular and special files.\n"
|
---|
946 | " --file-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
|
---|
947 | " Include the given access mode for regular and special files.\n"
|
---|
948 | " --dir-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
|
---|
949 | " Restrict the access mode of directories.\n"
|
---|
950 | " --dir-mode-and-mask <octal-mode> (-A, -C, -d, -r, -u, -x)\n"
|
---|
951 | " Include the given access mode for directories.\n"
|
---|
952 | " --read-ahead (-x)\n"
|
---|
953 | " Enabled read ahead thread when extracting files.\n"
|
---|
954 | "\n");
|
---|
955 | RTPrintf("Standard Options:\n"
|
---|
956 | " -h, -?, --help\n"
|
---|
957 | " Display this help text.\n"
|
---|
958 | " -V, --version\n"
|
---|
959 | " Display version number.\n");
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 | RTDECL(RTEXITCODE) RTZipTarCmd(unsigned cArgs, char **papszArgs)
|
---|
964 | {
|
---|
965 | /*
|
---|
966 | * Parse the command line.
|
---|
967 | *
|
---|
968 | * N.B. This is less flexible that your regular tar program in that it
|
---|
969 | * requires the operation to be specified as an option. On the other
|
---|
970 | * hand, you can specify it where ever you like in the command line.
|
---|
971 | */
|
---|
972 | static const RTGETOPTDEF s_aOptions[] =
|
---|
973 | {
|
---|
974 | /* operations */
|
---|
975 | { "--concatenate", 'A', RTGETOPT_REQ_NOTHING },
|
---|
976 | { "--catenate", 'A', RTGETOPT_REQ_NOTHING },
|
---|
977 | { "--create", 'c', RTGETOPT_REQ_NOTHING },
|
---|
978 | { "--diff", 'd', RTGETOPT_REQ_NOTHING },
|
---|
979 | { "--compare", 'd', RTGETOPT_REQ_NOTHING },
|
---|
980 | { "--append", 'r', RTGETOPT_REQ_NOTHING },
|
---|
981 | { "--list", 't', RTGETOPT_REQ_NOTHING },
|
---|
982 | { "--update", 'u', RTGETOPT_REQ_NOTHING },
|
---|
983 | { "--extract", 'x', RTGETOPT_REQ_NOTHING },
|
---|
984 | { "--get", 'x', RTGETOPT_REQ_NOTHING },
|
---|
985 | { "--delete", RTZIPTARCMD_OPT_DELETE, RTGETOPT_REQ_NOTHING },
|
---|
986 |
|
---|
987 | /* basic options */
|
---|
988 | { "--directory", 'C', RTGETOPT_REQ_STRING },
|
---|
989 | { "--file", 'f', RTGETOPT_REQ_STRING },
|
---|
990 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
991 | { "--preserve-permissions", 'p', RTGETOPT_REQ_NOTHING },
|
---|
992 | { "--bzip2", 'j', RTGETOPT_REQ_NOTHING },
|
---|
993 | { "--gzip", 'z', RTGETOPT_REQ_NOTHING },
|
---|
994 | { "--gunzip", 'z', RTGETOPT_REQ_NOTHING },
|
---|
995 | { "--ungzip", 'z', RTGETOPT_REQ_NOTHING },
|
---|
996 |
|
---|
997 | /* other options. */
|
---|
998 | { "--owner", RTZIPTARCMD_OPT_OWNER, RTGETOPT_REQ_STRING },
|
---|
999 | { "--group", RTZIPTARCMD_OPT_GROUP, RTGETOPT_REQ_STRING },
|
---|
1000 | { "--utc", RTZIPTARCMD_OPT_UTC, RTGETOPT_REQ_NOTHING },
|
---|
1001 |
|
---|
1002 | /* IPRT extensions */
|
---|
1003 | { "--prefix", RTZIPTARCMD_OPT_PREFIX, RTGETOPT_REQ_STRING },
|
---|
1004 | { "--file-mode-and-mask", RTZIPTARCMD_OPT_FILE_MODE_AND_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
|
---|
1005 | { "--file-mode-or-mask", RTZIPTARCMD_OPT_FILE_MODE_OR_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
|
---|
1006 | { "--dir-mode-and-mask", RTZIPTARCMD_OPT_DIR_MODE_AND_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
|
---|
1007 | { "--dir-mode-or-mask", RTZIPTARCMD_OPT_DIR_MODE_OR_MASK, RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT },
|
---|
1008 | { "--format", RTZIPTARCMD_OPT_FORMAT, RTGETOPT_REQ_STRING },
|
---|
1009 | { "--read-ahead", RTZIPTARCMD_OPT_READ_AHEAD, RTGETOPT_REQ_NOTHING },
|
---|
1010 | };
|
---|
1011 |
|
---|
1012 | RTGETOPTSTATE GetState;
|
---|
1013 | int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
|
---|
1014 | RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
1015 | if (RT_FAILURE(rc))
|
---|
1016 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOpt failed: %Rrc", rc);
|
---|
1017 |
|
---|
1018 | RTZIPTARCMDOPS Opts;
|
---|
1019 | RT_ZERO(Opts);
|
---|
1020 | Opts.enmFormat = RTZIPTARFORMAT_AUTO_DEFAULT;
|
---|
1021 | Opts.uidOwner = NIL_RTUID;
|
---|
1022 | Opts.gidGroup = NIL_RTUID;
|
---|
1023 | Opts.fFileModeAndMask = RTFS_UNIX_ALL_ACCESS_PERMS;
|
---|
1024 | Opts.fDirModeAndMask = RTFS_UNIX_ALL_ACCESS_PERMS;
|
---|
1025 | #if 0
|
---|
1026 | if (RTPermIsSuperUser())
|
---|
1027 | {
|
---|
1028 | Opts.fFileModeAndMask = RTFS_UNIX_ALL_PERMS;
|
---|
1029 | Opts.fDirModeAndMask = RTFS_UNIX_ALL_PERMS;
|
---|
1030 | Opts.fPreserveOwner = true;
|
---|
1031 | Opts.fPreserveGroup = true;
|
---|
1032 | }
|
---|
1033 | #endif
|
---|
1034 |
|
---|
1035 | RTGETOPTUNION ValueUnion;
|
---|
1036 | while ( (rc = RTGetOpt(&GetState, &ValueUnion)) != 0
|
---|
1037 | && rc != VINF_GETOPT_NOT_OPTION)
|
---|
1038 | {
|
---|
1039 | switch (rc)
|
---|
1040 | {
|
---|
1041 | /* operations */
|
---|
1042 | case 'A':
|
---|
1043 | case 'c':
|
---|
1044 | case 'd':
|
---|
1045 | case 'r':
|
---|
1046 | case 't':
|
---|
1047 | case 'u':
|
---|
1048 | case 'x':
|
---|
1049 | case RTZIPTARCMD_OPT_DELETE:
|
---|
1050 | if (Opts.iOperation)
|
---|
1051 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Conflicting tar operation (%s already set, now %s)",
|
---|
1052 | Opts.pszOperation, ValueUnion.pDef->pszLong);
|
---|
1053 | Opts.iOperation = rc;
|
---|
1054 | Opts.pszOperation = ValueUnion.pDef->pszLong;
|
---|
1055 | break;
|
---|
1056 |
|
---|
1057 | /* basic options */
|
---|
1058 | case 'C':
|
---|
1059 | if (Opts.pszDirectory)
|
---|
1060 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify -C/--directory once");
|
---|
1061 | Opts.pszDirectory = ValueUnion.psz;
|
---|
1062 | break;
|
---|
1063 |
|
---|
1064 | case 'f':
|
---|
1065 | if (Opts.pszFile)
|
---|
1066 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify -f/--file once");
|
---|
1067 | Opts.pszFile = ValueUnion.psz;
|
---|
1068 | break;
|
---|
1069 |
|
---|
1070 | case 'v':
|
---|
1071 | Opts.fVerbose = true;
|
---|
1072 | break;
|
---|
1073 |
|
---|
1074 | case 'p':
|
---|
1075 | Opts.fFileModeAndMask = RTFS_UNIX_ALL_PERMS;
|
---|
1076 | Opts.fDirModeAndMask = RTFS_UNIX_ALL_PERMS;
|
---|
1077 | Opts.fPreserveOwner = true;
|
---|
1078 | Opts.fPreserveGroup = true;
|
---|
1079 | break;
|
---|
1080 |
|
---|
1081 | case 'j':
|
---|
1082 | case 'z':
|
---|
1083 | if (Opts.chZipper)
|
---|
1084 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify one compressor / decompressor");
|
---|
1085 | Opts.chZipper = rc;
|
---|
1086 | break;
|
---|
1087 |
|
---|
1088 | case RTZIPTARCMD_OPT_OWNER:
|
---|
1089 | if (Opts.pszOwner)
|
---|
1090 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify --owner once");
|
---|
1091 | Opts.pszOwner = ValueUnion.psz;
|
---|
1092 |
|
---|
1093 | rc = RTStrToUInt32Full(Opts.pszOwner, 0, &ValueUnion.u32);
|
---|
1094 | if (RT_SUCCESS(rc) && rc != VINF_SUCCESS)
|
---|
1095 | return RTMsgErrorExit(RTEXITCODE_SYNTAX,
|
---|
1096 | "Error convering --owner '%s' into a number: %Rrc", Opts.pszOwner, rc);
|
---|
1097 | if (RT_SUCCESS(rc))
|
---|
1098 | {
|
---|
1099 | Opts.uidOwner = ValueUnion.u32;
|
---|
1100 | Opts.pszOwner = NULL;
|
---|
1101 | }
|
---|
1102 | break;
|
---|
1103 |
|
---|
1104 | case RTZIPTARCMD_OPT_GROUP:
|
---|
1105 | if (Opts.pszGroup)
|
---|
1106 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify --group once");
|
---|
1107 | Opts.pszGroup = ValueUnion.psz;
|
---|
1108 |
|
---|
1109 | rc = RTStrToUInt32Full(Opts.pszGroup, 0, &ValueUnion.u32);
|
---|
1110 | if (RT_SUCCESS(rc) && rc != VINF_SUCCESS)
|
---|
1111 | return RTMsgErrorExit(RTEXITCODE_SYNTAX,
|
---|
1112 | "Error convering --group '%s' into a number: %Rrc", Opts.pszGroup, rc);
|
---|
1113 | if (RT_SUCCESS(rc))
|
---|
1114 | {
|
---|
1115 | Opts.gidGroup = ValueUnion.u32;
|
---|
1116 | Opts.pszGroup = NULL;
|
---|
1117 | }
|
---|
1118 | break;
|
---|
1119 |
|
---|
1120 | case RTZIPTARCMD_OPT_UTC:
|
---|
1121 | Opts.fDisplayUtc = true;
|
---|
1122 | break;
|
---|
1123 |
|
---|
1124 | /* iprt extensions */
|
---|
1125 | case RTZIPTARCMD_OPT_PREFIX:
|
---|
1126 | if (Opts.pszPrefix)
|
---|
1127 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "You may only specify --prefix once");
|
---|
1128 | Opts.pszPrefix = ValueUnion.psz;
|
---|
1129 | break;
|
---|
1130 |
|
---|
1131 | case RTZIPTARCMD_OPT_FILE_MODE_AND_MASK:
|
---|
1132 | Opts.fFileModeAndMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
|
---|
1133 | break;
|
---|
1134 |
|
---|
1135 | case RTZIPTARCMD_OPT_FILE_MODE_OR_MASK:
|
---|
1136 | Opts.fFileModeOrMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
|
---|
1137 | break;
|
---|
1138 |
|
---|
1139 | case RTZIPTARCMD_OPT_DIR_MODE_AND_MASK:
|
---|
1140 | Opts.fDirModeAndMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
|
---|
1141 | break;
|
---|
1142 |
|
---|
1143 | case RTZIPTARCMD_OPT_DIR_MODE_OR_MASK:
|
---|
1144 | Opts.fDirModeOrMask = ValueUnion.u32 & RTFS_UNIX_ALL_PERMS;
|
---|
1145 | break;
|
---|
1146 |
|
---|
1147 | case RTZIPTARCMD_OPT_FORMAT:
|
---|
1148 | if (!strcmp(ValueUnion.psz, "auto") || !strcmp(ValueUnion.psz, "default"))
|
---|
1149 | Opts.enmFormat = RTZIPTARFORMAT_AUTO_DEFAULT;
|
---|
1150 | else if (!strcmp(ValueUnion.psz, "tar"))
|
---|
1151 | Opts.enmFormat = RTZIPTARFORMAT_TAR;
|
---|
1152 | else if (!strcmp(ValueUnion.psz, "xar"))
|
---|
1153 | Opts.enmFormat = RTZIPTARFORMAT_XAR;
|
---|
1154 | else
|
---|
1155 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown archive format: '%s'", ValueUnion.psz);
|
---|
1156 | break;
|
---|
1157 |
|
---|
1158 | case RTZIPTARCMD_OPT_READ_AHEAD:
|
---|
1159 | Opts.fReadAhead = true;
|
---|
1160 | break;
|
---|
1161 |
|
---|
1162 | /* Standard bits. */
|
---|
1163 | case 'h':
|
---|
1164 | rtZipTarUsage(RTPathFilename(papszArgs[0]));
|
---|
1165 | return RTEXITCODE_SUCCESS;
|
---|
1166 |
|
---|
1167 | case 'V':
|
---|
1168 | RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
|
---|
1169 | return RTEXITCODE_SUCCESS;
|
---|
1170 |
|
---|
1171 | default:
|
---|
1172 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
1173 | }
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | if (rc == VINF_GETOPT_NOT_OPTION)
|
---|
1177 | {
|
---|
1178 | /* this is kind of ugly. */
|
---|
1179 | Assert((unsigned)GetState.iNext - 1 <= cArgs);
|
---|
1180 | Opts.papszFiles = (const char * const *)&papszArgs[GetState.iNext - 1];
|
---|
1181 | Opts.cFiles = cArgs - GetState.iNext + 1;
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 | /*
|
---|
1185 | * Post proceess the options.
|
---|
1186 | */
|
---|
1187 | if (Opts.iOperation == 0)
|
---|
1188 | {
|
---|
1189 | Opts.iOperation = 't';
|
---|
1190 | Opts.pszOperation = "--list";
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | if ( Opts.iOperation == 'x'
|
---|
1194 | && Opts.pszOwner)
|
---|
1195 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "The use of --owner with %s has not implemented yet", Opts.pszOperation);
|
---|
1196 |
|
---|
1197 | if ( Opts.iOperation == 'x'
|
---|
1198 | && Opts.pszGroup)
|
---|
1199 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "The use of --group with %s has not implemented yet", Opts.pszOperation);
|
---|
1200 |
|
---|
1201 | /*
|
---|
1202 | * Do the job.
|
---|
1203 | */
|
---|
1204 | switch (Opts.iOperation)
|
---|
1205 | {
|
---|
1206 | case 't':
|
---|
1207 | return rtZipTarDoWithMembers(&Opts, rtZipTarCmdListCallback);
|
---|
1208 |
|
---|
1209 | case 'x':
|
---|
1210 | return rtZipTarDoWithMembers(&Opts, rtZipTarCmdExtractCallback);
|
---|
1211 |
|
---|
1212 | case 'A':
|
---|
1213 | case 'c':
|
---|
1214 | case 'd':
|
---|
1215 | case 'r':
|
---|
1216 | case 'u':
|
---|
1217 | case RTZIPTARCMD_OPT_DELETE:
|
---|
1218 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "The operation %s is not implemented yet", Opts.pszOperation);
|
---|
1219 |
|
---|
1220 | default:
|
---|
1221 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Internal error");
|
---|
1222 | }
|
---|
1223 | }
|
---|
1224 |
|
---|