1 | /* $Id: tar.cpp 56290 2015-06-09 14:01:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Tar archive I/O.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-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 "internal/iprt.h"
|
---|
32 | #include <iprt/tar.h>
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/file.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <iprt/vfs.h>
|
---|
42 | #include <iprt/zip.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | #include "internal/magics.h"
|
---|
46 | #include "tar.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Structures and Typedefs *
|
---|
51 | *******************************************************************************/
|
---|
52 | /** @name RTTARRECORD::h::linkflag
|
---|
53 | * @{ */
|
---|
54 | #define LF_OLDNORMAL '\0' /**< Normal disk file, Unix compatible */
|
---|
55 | #define LF_NORMAL '0' /**< Normal disk file */
|
---|
56 | #define LF_LINK '1' /**< Link to previously dumped file */
|
---|
57 | #define LF_SYMLINK '2' /**< Symbolic link */
|
---|
58 | #define LF_CHR '3' /**< Character special file */
|
---|
59 | #define LF_BLK '4' /**< Block special file */
|
---|
60 | #define LF_DIR '5' /**< Directory */
|
---|
61 | #define LF_FIFO '6' /**< FIFO special file */
|
---|
62 | #define LF_CONTIG '7' /**< Contiguous file */
|
---|
63 | /** @} */
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * A tar file header.
|
---|
67 | */
|
---|
68 | typedef union RTTARRECORD
|
---|
69 | {
|
---|
70 | char d[512];
|
---|
71 | struct h
|
---|
72 | {
|
---|
73 | char name[100];
|
---|
74 | char mode[8];
|
---|
75 | char uid[8];
|
---|
76 | char gid[8];
|
---|
77 | char size[12];
|
---|
78 | char mtime[12];
|
---|
79 | char chksum[8];
|
---|
80 | char linkflag;
|
---|
81 | char linkname[100];
|
---|
82 | char magic[8];
|
---|
83 | char uname[32];
|
---|
84 | char gname[32];
|
---|
85 | char devmajor[8];
|
---|
86 | char devminor[8];
|
---|
87 | } h;
|
---|
88 | } RTTARRECORD;
|
---|
89 | AssertCompileSize(RTTARRECORD, 512);
|
---|
90 | AssertCompileMemberOffset(RTTARRECORD, h.size, 100+8*3);
|
---|
91 | AssertCompileMemberSize(RTTARRECORD, h.name, RTTAR_NAME_MAX+1);
|
---|
92 | /** Pointer to a tar file header. */
|
---|
93 | typedef RTTARRECORD *PRTTARRECORD;
|
---|
94 |
|
---|
95 | /** Pointer to a tar file handle. */
|
---|
96 | typedef struct RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * The internal data of a tar handle.
|
---|
100 | */
|
---|
101 | typedef struct RTTARINTERNAL
|
---|
102 | {
|
---|
103 | /** The magic (RTTAR_MAGIC). */
|
---|
104 | uint32_t u32Magic;
|
---|
105 | /** The handle to the tar file. */
|
---|
106 | RTFILE hTarFile;
|
---|
107 | /** The open mode for hTarFile. */
|
---|
108 | uint32_t fOpenMode;
|
---|
109 | /** Whether a file within the archive is currently open for writing.
|
---|
110 | * Only one can be open. */
|
---|
111 | bool fFileOpenForWrite;
|
---|
112 | /** The tar file VFS handle (for reading). */
|
---|
113 | RTVFSFILE hVfsFile;
|
---|
114 | /** The tar file system VFS handle. */
|
---|
115 | RTVFSFSSTREAM hVfsFss;
|
---|
116 | /** Set if hVfsFss is at the start of the stream and doesn't need rewinding. */
|
---|
117 | bool fFssAtStart;
|
---|
118 | } RTTARINTERNAL;
|
---|
119 | /** Pointer to a the internal data of a tar handle. */
|
---|
120 | typedef RTTARINTERNAL* PRTTARINTERNAL;
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * The internal data of a file within a tar file.
|
---|
124 | */
|
---|
125 | typedef struct RTTARFILEINTERNAL
|
---|
126 | {
|
---|
127 | /** The magic (RTTARFILE_MAGIC). */
|
---|
128 | uint32_t u32Magic;
|
---|
129 | /** The open mode. */
|
---|
130 | uint32_t fOpenMode;
|
---|
131 | /** Pointer to back to the tar file. */
|
---|
132 | PRTTARINTERNAL pTar;
|
---|
133 | /** The name of the file. */
|
---|
134 | char *pszFilename;
|
---|
135 | /** The offset into the archive where the file header starts. */
|
---|
136 | uint64_t offStart;
|
---|
137 | /** The size of the file. */
|
---|
138 | uint64_t cbSize;
|
---|
139 | /** The size set by RTTarFileSetSize(). */
|
---|
140 | uint64_t cbSetSize;
|
---|
141 | /** The current offset within this file. */
|
---|
142 | uint64_t offCurrent;
|
---|
143 | /** The VFS I/O stream (only for reading atm). */
|
---|
144 | RTVFSIOSTREAM hVfsIos;
|
---|
145 | } RTTARFILEINTERNAL;
|
---|
146 | /** Pointer to the internal data of a tar file. */
|
---|
147 | typedef RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | /*******************************************************************************
|
---|
152 | * Defined Constants And Macros *
|
---|
153 | *******************************************************************************/
|
---|
154 |
|
---|
155 | /** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
|
---|
156 | /* RTTAR */
|
---|
157 | #define RTTAR_VALID_RETURN_RC(hHandle, rc) \
|
---|
158 | do { \
|
---|
159 | AssertPtrReturn((hHandle), (rc)); \
|
---|
160 | AssertReturn((hHandle)->u32Magic == RTTAR_MAGIC, (rc)); \
|
---|
161 | } while (0)
|
---|
162 | /* RTTARFILE */
|
---|
163 | #define RTTARFILE_VALID_RETURN_RC(hHandle, rc) \
|
---|
164 | do { \
|
---|
165 | AssertPtrReturn((hHandle), (rc)); \
|
---|
166 | AssertReturn((hHandle)->u32Magic == RTTARFILE_MAGIC, (rc)); \
|
---|
167 | } while (0)
|
---|
168 |
|
---|
169 | /** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
|
---|
170 | /* RTTAR */
|
---|
171 | #define RTTAR_VALID_RETURN(hHandle) RTTAR_VALID_RETURN_RC((hHandle), VERR_INVALID_HANDLE)
|
---|
172 | /* RTTARFILE */
|
---|
173 | #define RTTARFILE_VALID_RETURN(hHandle) RTTARFILE_VALID_RETURN_RC((hHandle), VERR_INVALID_HANDLE)
|
---|
174 |
|
---|
175 | /** Validates a handle and returns (void) if not valid. */
|
---|
176 | /* RTTAR */
|
---|
177 | #define RTTAR_VALID_RETURN_VOID(hHandle) \
|
---|
178 | do { \
|
---|
179 | AssertPtrReturnVoid(hHandle); \
|
---|
180 | AssertReturnVoid((hHandle)->u32Magic == RTTAR_MAGIC); \
|
---|
181 | } while (0)
|
---|
182 | /* RTTARFILE */
|
---|
183 | #define RTTARFILE_VALID_RETURN_VOID(hHandle) \
|
---|
184 | do { \
|
---|
185 | AssertPtrReturnVoid(hHandle); \
|
---|
186 | AssertReturnVoid((hHandle)->u32Magic == RTTARFILE_MAGIC); \
|
---|
187 | } while (0)
|
---|
188 |
|
---|
189 |
|
---|
190 | RTR3DECL(int) RTTarOpen(PRTTAR phTar, const char *pszTarname, uint32_t fMode)
|
---|
191 | {
|
---|
192 | AssertReturn(fMode & RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Create a tar instance.
|
---|
196 | */
|
---|
197 | PRTTARINTERNAL pThis = (PRTTARINTERNAL)RTMemAllocZ(sizeof(RTTARINTERNAL));
|
---|
198 | if (!pThis)
|
---|
199 | return VERR_NO_MEMORY;
|
---|
200 |
|
---|
201 | pThis->u32Magic = RTTAR_MAGIC;
|
---|
202 | pThis->fOpenMode = fMode;
|
---|
203 | pThis->hVfsFile = NIL_RTVFSFILE;
|
---|
204 | pThis->hVfsFss = NIL_RTVFSFSSTREAM;
|
---|
205 | pThis->fFssAtStart = false;
|
---|
206 |
|
---|
207 | /*
|
---|
208 | * Open the tar file.
|
---|
209 | */
|
---|
210 | int rc = RTFileOpen(&pThis->hTarFile, pszTarname, fMode);
|
---|
211 | if (RT_SUCCESS(rc))
|
---|
212 | {
|
---|
213 | *phTar = pThis;
|
---|
214 | return VINF_SUCCESS;
|
---|
215 | }
|
---|
216 |
|
---|
217 | RTMemFree(pThis);
|
---|
218 | return rc;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | RTR3DECL(int) RTTarClose(RTTAR hTar)
|
---|
223 | {
|
---|
224 | if (hTar == NIL_RTTAR)
|
---|
225 | return VINF_SUCCESS;
|
---|
226 |
|
---|
227 | PRTTARINTERNAL pInt = hTar;
|
---|
228 | RTTAR_VALID_RETURN(pInt);
|
---|
229 |
|
---|
230 | int rc = VINF_SUCCESS;
|
---|
231 |
|
---|
232 | /* gtar gives a warning, but the documentation says EOF is indicated by a
|
---|
233 | * zero block. Disabled for now. */
|
---|
234 | #if 0
|
---|
235 | {
|
---|
236 | /* Append the EOF record which is filled all by zeros */
|
---|
237 | RTTARRECORD record;
|
---|
238 | RT_ZERO(record);
|
---|
239 | rc = RTFileWrite(pInt->hTarFile, &record, sizeof(record), NULL);
|
---|
240 | }
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
|
---|
244 | {
|
---|
245 | uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX);
|
---|
246 | pInt->hVfsFss = NIL_RTVFSFSSTREAM;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if (pInt->hVfsFile != NIL_RTVFSFILE)
|
---|
250 | {
|
---|
251 | uint32_t cRefs = RTVfsFileRelease(pInt->hVfsFile); Assert(cRefs != UINT32_MAX);
|
---|
252 | pInt->hVfsFile = NIL_RTVFSFILE;
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (pInt->hTarFile != NIL_RTFILE)
|
---|
256 | {
|
---|
257 | rc = RTFileClose(pInt->hTarFile);
|
---|
258 | pInt->hTarFile = NIL_RTFILE;
|
---|
259 | }
|
---|
260 |
|
---|
261 | pInt->u32Magic = RTTAR_MAGIC_DEAD;
|
---|
262 |
|
---|
263 | RTMemFree(pInt);
|
---|
264 |
|
---|
265 | return rc;
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Creates a tar file handle for a read-only VFS stream object.
|
---|
271 | *
|
---|
272 | * @returns IPRT status code.
|
---|
273 | * @param pszName The file name. Automatically freed on failure.
|
---|
274 | * @param hVfsIos The VFS I/O stream we create the handle around.
|
---|
275 | * The reference is NOT consumed.
|
---|
276 | * @param fOpen The open flags.
|
---|
277 | * @param ppFile Where to return the handle.
|
---|
278 | */
|
---|
279 | static int rtTarFileCreateHandleForReadOnly(char *pszName, RTVFSIOSTREAM hVfsIos, uint32_t fOpen, PRTTARFILEINTERNAL *ppFile)
|
---|
280 | {
|
---|
281 | int rc;
|
---|
282 | PRTTARFILEINTERNAL pNewFile = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(*pNewFile));
|
---|
283 | if (pNewFile)
|
---|
284 | {
|
---|
285 | RTFSOBJINFO ObjInfo;
|
---|
286 | rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_UNIX);
|
---|
287 | if (RT_SUCCESS(rc))
|
---|
288 | {
|
---|
289 | pNewFile->u32Magic = RTTARFILE_MAGIC;
|
---|
290 | pNewFile->pTar = NULL;
|
---|
291 | pNewFile->pszFilename = pszName;
|
---|
292 | pNewFile->offStart = UINT64_MAX;
|
---|
293 | pNewFile->cbSize = ObjInfo.cbObject;
|
---|
294 | pNewFile->cbSetSize = 0;
|
---|
295 | pNewFile->offCurrent = 0;
|
---|
296 | pNewFile->fOpenMode = fOpen;
|
---|
297 | pNewFile->hVfsIos = hVfsIos;
|
---|
298 |
|
---|
299 | uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos); Assert(cRefs != UINT32_MAX); NOREF(cRefs);
|
---|
300 |
|
---|
301 | *ppFile = pNewFile;
|
---|
302 | return VINF_SUCCESS;
|
---|
303 | }
|
---|
304 |
|
---|
305 | RTMemFree(pNewFile);
|
---|
306 | }
|
---|
307 | else
|
---|
308 | rc = VERR_NO_MEMORY;
|
---|
309 | RTStrFree(pszName);
|
---|
310 | return rc;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | /* Only used for write handles. */
|
---|
315 | static PRTTARFILEINTERNAL rtTarFileCreateForWrite(PRTTARINTERNAL pInt, const char *pszFilename, uint32_t fOpen)
|
---|
316 | {
|
---|
317 | PRTTARFILEINTERNAL pFileInt = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(RTTARFILEINTERNAL));
|
---|
318 | if (!pFileInt)
|
---|
319 | return NULL;
|
---|
320 |
|
---|
321 | pFileInt->u32Magic = RTTARFILE_MAGIC;
|
---|
322 | pFileInt->pTar = pInt;
|
---|
323 | pFileInt->fOpenMode = fOpen;
|
---|
324 | pFileInt->pszFilename = RTStrDup(pszFilename);
|
---|
325 | pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
326 | if (pFileInt->pszFilename)
|
---|
327 | return pFileInt;
|
---|
328 |
|
---|
329 | RTMemFree(pFileInt);
|
---|
330 | return NULL;
|
---|
331 |
|
---|
332 | }
|
---|
333 |
|
---|
334 |
|
---|
335 | RTR3DECL(int) RTTarFileOpen(RTTAR hTar, PRTTARFILE phFile, const char *pszFilename, uint32_t fOpen)
|
---|
336 | {
|
---|
337 | /* Write only interface now. */
|
---|
338 | AssertReturn(fOpen & RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
|
---|
339 |
|
---|
340 | PRTTARINTERNAL pInt = hTar;
|
---|
341 | RTTAR_VALID_RETURN(pInt);
|
---|
342 |
|
---|
343 | if (!pInt->hTarFile)
|
---|
344 | return VERR_INVALID_HANDLE;
|
---|
345 |
|
---|
346 | if (fOpen & RTFILE_O_WRITE)
|
---|
347 | {
|
---|
348 | if (!(pInt->fOpenMode & RTFILE_O_WRITE))
|
---|
349 | return VERR_WRITE_PROTECT;
|
---|
350 | if (pInt->fFileOpenForWrite)
|
---|
351 | return VERR_TOO_MANY_OPEN_FILES;
|
---|
352 | }
|
---|
353 |
|
---|
354 | int rc = VINF_SUCCESS;
|
---|
355 | if (!(fOpen & RTFILE_O_WRITE))
|
---|
356 | {
|
---|
357 | /*
|
---|
358 | * Rewind the stream if necessary.
|
---|
359 | */
|
---|
360 | if (!pInt->fFssAtStart)
|
---|
361 | {
|
---|
362 | if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
|
---|
363 | {
|
---|
364 | uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX);
|
---|
365 | pInt->hVfsFss = NIL_RTVFSFSSTREAM;
|
---|
366 | }
|
---|
367 |
|
---|
368 | if (pInt->hVfsFile == NIL_RTVFSFILE)
|
---|
369 | {
|
---|
370 | rc = RTVfsFileFromRTFile(pInt->hTarFile, RTFILE_O_READ, true /*fLeaveOpen*/, &pInt->hVfsFile);
|
---|
371 | if (RT_FAILURE(rc))
|
---|
372 | return rc;
|
---|
373 | }
|
---|
374 |
|
---|
375 | RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(pInt->hVfsFile);
|
---|
376 | rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &pInt->hVfsFss);
|
---|
377 | RTVfsIoStrmRelease(hVfsIos);
|
---|
378 | if (RT_FAILURE(rc))
|
---|
379 | return rc;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Search the file system stream.
|
---|
384 | */
|
---|
385 | pInt->fFssAtStart = false;
|
---|
386 | for (;;)
|
---|
387 | {
|
---|
388 | char *pszName;
|
---|
389 | RTVFSOBJTYPE enmType;
|
---|
390 | RTVFSOBJ hVfsObj;
|
---|
391 | rc = RTVfsFsStrmNext(pInt->hVfsFss, &pszName, &enmType, &hVfsObj);
|
---|
392 | if (rc == VERR_EOF)
|
---|
393 | return VERR_FILE_NOT_FOUND;
|
---|
394 | if (RT_FAILURE(rc))
|
---|
395 | return rc;
|
---|
396 |
|
---|
397 | if (!RTStrCmp(pszName, pszFilename))
|
---|
398 | {
|
---|
399 | if (enmType == RTVFSOBJTYPE_FILE || enmType == RTVFSOBJTYPE_IO_STREAM)
|
---|
400 | rc = rtTarFileCreateHandleForReadOnly(pszName, RTVfsObjToIoStream(hVfsObj), fOpen, phFile);
|
---|
401 | else
|
---|
402 | {
|
---|
403 | rc = VERR_UNEXPECTED_FS_OBJ_TYPE;
|
---|
404 | RTStrFree(pszName);
|
---|
405 | }
|
---|
406 | RTVfsObjRelease(hVfsObj);
|
---|
407 | break;
|
---|
408 | }
|
---|
409 | RTStrFree(pszName);
|
---|
410 | RTVfsObjRelease(hVfsObj);
|
---|
411 | } /* Search loop. */
|
---|
412 | }
|
---|
413 | else
|
---|
414 | {
|
---|
415 | PRTTARFILEINTERNAL pFileInt = rtTarFileCreateForWrite(pInt, pszFilename, fOpen);
|
---|
416 | if (!pFileInt)
|
---|
417 | return VERR_NO_MEMORY;
|
---|
418 |
|
---|
419 | pInt->fFileOpenForWrite = true;
|
---|
420 |
|
---|
421 | /* If we are in write mode, we also in append mode. Add an dummy
|
---|
422 | * header at the end of the current file. It will be filled by the
|
---|
423 | * close operation. */
|
---|
424 | rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_END, &pFileInt->offStart);
|
---|
425 | if (RT_SUCCESS(rc))
|
---|
426 | {
|
---|
427 | RTTARRECORD record;
|
---|
428 | RT_ZERO(record);
|
---|
429 | rc = RTFileWrite(pFileInt->pTar->hTarFile, &record, sizeof(RTTARRECORD), NULL);
|
---|
430 | }
|
---|
431 |
|
---|
432 | if (RT_SUCCESS(rc))
|
---|
433 | *phFile = (RTTARFILE)pFileInt;
|
---|
434 | else
|
---|
435 | {
|
---|
436 | /* Cleanup on failure */
|
---|
437 | if (pFileInt->pszFilename)
|
---|
438 | RTStrFree(pFileInt->pszFilename);
|
---|
439 | RTMemFree(pFileInt);
|
---|
440 | }
|
---|
441 | }
|
---|
442 |
|
---|
443 | return rc;
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Calculates the TAR header checksums and detects if it's all zeros.
|
---|
449 | *
|
---|
450 | * @returns true if all zeros, false if not.
|
---|
451 | * @param pHdr The header to checksum.
|
---|
452 | * @param pi32Unsigned Where to store the checksum calculated using
|
---|
453 | * unsigned chars. This is the one POSIX
|
---|
454 | * specifies.
|
---|
455 | * @param pi32Signed Where to store the checksum calculated using
|
---|
456 | * signed chars.
|
---|
457 | *
|
---|
458 | * @remarks The reason why we calculate the checksum as both signed and unsigned
|
---|
459 | * has to do with various the char C type being signed on some hosts
|
---|
460 | * and unsigned on others.
|
---|
461 | *
|
---|
462 | * @remarks Borrowed from tarvfs.cpp.
|
---|
463 | */
|
---|
464 | static bool rtZipTarCalcChkSum(PCRTZIPTARHDR pHdr, int32_t *pi32Unsigned, int32_t *pi32Signed)
|
---|
465 | {
|
---|
466 | int32_t i32Unsigned = 0;
|
---|
467 | int32_t i32Signed = 0;
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Sum up the entire header.
|
---|
471 | */
|
---|
472 | const char *pch = (const char *)pHdr;
|
---|
473 | const char *pchEnd = pch + sizeof(*pHdr);
|
---|
474 | do
|
---|
475 | {
|
---|
476 | i32Unsigned += *(unsigned char *)pch;
|
---|
477 | i32Signed += *(signed char *)pch;
|
---|
478 | } while (++pch != pchEnd);
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Check if it's all zeros and replace the chksum field with spaces.
|
---|
482 | */
|
---|
483 | bool const fZeroHdr = i32Unsigned == 0;
|
---|
484 |
|
---|
485 | pch = pHdr->Common.chksum;
|
---|
486 | pchEnd = pch + sizeof(pHdr->Common.chksum);
|
---|
487 | do
|
---|
488 | {
|
---|
489 | i32Unsigned -= *(unsigned char *)pch;
|
---|
490 | i32Signed -= *(signed char *)pch;
|
---|
491 | } while (++pch != pchEnd);
|
---|
492 |
|
---|
493 | i32Unsigned += (unsigned char)' ' * sizeof(pHdr->Common.chksum);
|
---|
494 | i32Signed += (signed char)' ' * sizeof(pHdr->Common.chksum);
|
---|
495 |
|
---|
496 | *pi32Unsigned = i32Unsigned;
|
---|
497 | if (pi32Signed)
|
---|
498 | *pi32Signed = i32Signed;
|
---|
499 | return fZeroHdr;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | static void rtTarSizeToRec(PRTTARRECORD pRecord, uint64_t cbSize)
|
---|
504 | {
|
---|
505 | /*
|
---|
506 | * Small enough for the standard octal string encoding?
|
---|
507 | *
|
---|
508 | * Note! We could actually use the terminator character as well if we liked,
|
---|
509 | * but let not do that as it's easier to test this way.
|
---|
510 | */
|
---|
511 | if (cbSize < _4G * 2U)
|
---|
512 | RTStrPrintf(pRecord->h.size, sizeof(pRecord->h.size), "%0.11llo", cbSize);
|
---|
513 | else
|
---|
514 | {
|
---|
515 | /*
|
---|
516 | * Base 256 extension. Set the highest bit of the left most character.
|
---|
517 | * We don't deal with negatives here, cause the size have to be greater
|
---|
518 | * than zero.
|
---|
519 | *
|
---|
520 | * Note! The base-256 extension are never used by gtar or libarchive
|
---|
521 | * with the "ustar \0" format version, only the later
|
---|
522 | * "ustar\000" version. However, this shouldn't cause much
|
---|
523 | * trouble as they are not picky about what they read.
|
---|
524 | */
|
---|
525 | size_t cchField = sizeof(pRecord->h.size) - 1;
|
---|
526 | unsigned char *puchField = (unsigned char*)pRecord->h.size;
|
---|
527 | puchField[0] = 0x80;
|
---|
528 | do
|
---|
529 | {
|
---|
530 | puchField[cchField--] = cbSize & 0xff;
|
---|
531 | cbSize >>= 8;
|
---|
532 | } while (cchField);
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | static int rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize,
|
---|
538 | RTUID uid, RTGID gid, RTFMODE fmode, int64_t mtime)
|
---|
539 | {
|
---|
540 | /** @todo check for field overflows. */
|
---|
541 | /* Fill the header record */
|
---|
542 | // RT_ZERO(pRecord); - done by the caller.
|
---|
543 | /** @todo use RTStrCopy */
|
---|
544 | size_t cb = RTStrPrintf(pRecord->h.name, sizeof(pRecord->h.name), "%s", pszSrcName);
|
---|
545 | if (cb < strlen(pszSrcName))
|
---|
546 | return VERR_BUFFER_OVERFLOW;
|
---|
547 | RTStrPrintf(pRecord->h.mode, sizeof(pRecord->h.mode), "%0.7o", fmode);
|
---|
548 | RTStrPrintf(pRecord->h.uid, sizeof(pRecord->h.uid), "%0.7o", uid);
|
---|
549 | RTStrPrintf(pRecord->h.gid, sizeof(pRecord->h.gid), "%0.7o", gid);
|
---|
550 | rtTarSizeToRec(pRecord, cbSize);
|
---|
551 | RTStrPrintf(pRecord->h.mtime, sizeof(pRecord->h.mtime), "%0.11llo", mtime);
|
---|
552 | RTStrPrintf(pRecord->h.magic, sizeof(pRecord->h.magic), "ustar ");
|
---|
553 | RTStrPrintf(pRecord->h.uname, sizeof(pRecord->h.uname), "someone");
|
---|
554 | RTStrPrintf(pRecord->h.gname, sizeof(pRecord->h.gname), "someone");
|
---|
555 | pRecord->h.linkflag = LF_NORMAL;
|
---|
556 |
|
---|
557 | /* Create the checksum out of the new header */
|
---|
558 | int32_t iUnsignedChksum, iSignedChksum;
|
---|
559 | if (rtZipTarCalcChkSum((PCRTZIPTARHDR)pRecord, &iUnsignedChksum, &iSignedChksum))
|
---|
560 | return VERR_TAR_END_OF_FILE;
|
---|
561 |
|
---|
562 | /* Format the checksum */
|
---|
563 | RTStrPrintf(pRecord->h.chksum, sizeof(pRecord->h.chksum), "%0.7o", iUnsignedChksum);
|
---|
564 |
|
---|
565 | return VINF_SUCCESS;
|
---|
566 | }
|
---|
567 |
|
---|
568 |
|
---|
569 | DECLINLINE(void *) rtTarMemTmpAlloc(size_t *pcbSize)
|
---|
570 | {
|
---|
571 | *pcbSize = 0;
|
---|
572 | /* Allocate a reasonably large buffer, fall back on a tiny one.
|
---|
573 | * Note: has to be 512 byte aligned and >= 512 byte. */
|
---|
574 | size_t cbTmp = _1M;
|
---|
575 | void *pvTmp = RTMemTmpAlloc(cbTmp);
|
---|
576 | if (!pvTmp)
|
---|
577 | {
|
---|
578 | cbTmp = sizeof(RTTARRECORD);
|
---|
579 | pvTmp = RTMemTmpAlloc(cbTmp);
|
---|
580 | }
|
---|
581 | *pcbSize = cbTmp;
|
---|
582 | return pvTmp;
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | static int rtTarAppendZeros(PRTTARFILEINTERNAL pFileInt, uint64_t cbSize)
|
---|
587 | {
|
---|
588 | /* Allocate a temporary buffer for copying the tar content in blocks. */
|
---|
589 | size_t cbTmp = 0;
|
---|
590 | void *pvTmp = rtTarMemTmpAlloc(&cbTmp);
|
---|
591 | if (!pvTmp)
|
---|
592 | return VERR_NO_MEMORY;
|
---|
593 | RT_BZERO(pvTmp, cbTmp);
|
---|
594 |
|
---|
595 | int rc = VINF_SUCCESS;
|
---|
596 | uint64_t cbAllWritten = 0;
|
---|
597 | size_t cbWritten = 0;
|
---|
598 | for (;;)
|
---|
599 | {
|
---|
600 | if (cbAllWritten >= cbSize)
|
---|
601 | break;
|
---|
602 | size_t cbToWrite = RT_MIN(cbSize - cbAllWritten, cbTmp);
|
---|
603 | rc = RTTarFileWriteAt(pFileInt, pFileInt->offCurrent, pvTmp, cbToWrite, &cbWritten);
|
---|
604 | if (RT_FAILURE(rc))
|
---|
605 | break;
|
---|
606 | cbAllWritten += cbWritten;
|
---|
607 | }
|
---|
608 |
|
---|
609 | RTMemTmpFree(pvTmp);
|
---|
610 |
|
---|
611 | return rc;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | RTR3DECL(int) RTTarFileClose(RTTARFILE hFile)
|
---|
616 | {
|
---|
617 | /* Already closed? */
|
---|
618 | if (hFile == NIL_RTTARFILE)
|
---|
619 | return VINF_SUCCESS;
|
---|
620 |
|
---|
621 | PRTTARFILEINTERNAL pFileInt = hFile;
|
---|
622 | RTTARFILE_VALID_RETURN(pFileInt);
|
---|
623 |
|
---|
624 | int rc = VINF_SUCCESS;
|
---|
625 |
|
---|
626 | /* In write mode: */
|
---|
627 | if ((pFileInt->fOpenMode & (RTFILE_O_WRITE | RTFILE_O_READ)) == RTFILE_O_WRITE)
|
---|
628 | {
|
---|
629 | pFileInt->pTar->fFileOpenForWrite = false;
|
---|
630 | do
|
---|
631 | {
|
---|
632 | /* If the user has called RTTarFileSetSize in the meantime, we have
|
---|
633 | to make sure the file has the right size. */
|
---|
634 | if (pFileInt->cbSetSize > pFileInt->cbSize)
|
---|
635 | {
|
---|
636 | rc = rtTarAppendZeros(pFileInt, pFileInt->cbSetSize - pFileInt->cbSize);
|
---|
637 | if (RT_FAILURE(rc))
|
---|
638 | break;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /* If the written size isn't 512 byte aligned, we need to fix this. */
|
---|
642 | RTTARRECORD record;
|
---|
643 | RT_ZERO(record);
|
---|
644 | uint64_t cbSizeAligned = RT_ALIGN(pFileInt->cbSize, sizeof(RTTARRECORD));
|
---|
645 | if (cbSizeAligned != pFileInt->cbSize)
|
---|
646 | {
|
---|
647 | /* Note the RTFile method. We didn't increase the cbSize or cbCurrentPos here. */
|
---|
648 | rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
|
---|
649 | pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize,
|
---|
650 | &record,
|
---|
651 | cbSizeAligned - pFileInt->cbSize,
|
---|
652 | NULL);
|
---|
653 | if (RT_FAILURE(rc))
|
---|
654 | break;
|
---|
655 | }
|
---|
656 |
|
---|
657 | /* Create a header record for the file */
|
---|
658 | /* Todo: mode, gid, uid, mtime should be setable (or detected myself) */
|
---|
659 | RTTIMESPEC time;
|
---|
660 | RTTimeNow(&time);
|
---|
661 | rc = rtTarCreateHeaderRecord(&record, pFileInt->pszFilename, pFileInt->cbSize,
|
---|
662 | 0, 0, 0600, RTTimeSpecGetSeconds(&time));
|
---|
663 | if (RT_FAILURE(rc))
|
---|
664 | break;
|
---|
665 |
|
---|
666 | /* Write this at the start of the file data */
|
---|
667 | rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart, &record, sizeof(RTTARRECORD), NULL);
|
---|
668 | if (RT_FAILURE(rc))
|
---|
669 | break;
|
---|
670 | }
|
---|
671 | while (0);
|
---|
672 | }
|
---|
673 |
|
---|
674 | /*
|
---|
675 | * Now cleanup and delete the handle.
|
---|
676 | */
|
---|
677 | if (pFileInt->pszFilename)
|
---|
678 | RTStrFree(pFileInt->pszFilename);
|
---|
679 | if (pFileInt->hVfsIos != NIL_RTVFSIOSTREAM)
|
---|
680 | {
|
---|
681 | RTVfsIoStrmRelease(pFileInt->hVfsIos);
|
---|
682 | pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
|
---|
683 | }
|
---|
684 | pFileInt->u32Magic = RTTARFILE_MAGIC_DEAD;
|
---|
685 | RTMemFree(pFileInt);
|
---|
686 |
|
---|
687 | return rc;
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 | RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
692 | {
|
---|
693 | PRTTARFILEINTERNAL pFileInt = hFile;
|
---|
694 | RTTARFILE_VALID_RETURN(pFileInt);
|
---|
695 |
|
---|
696 | size_t cbTmpRead = 0;
|
---|
697 | int rc = RTVfsIoStrmReadAt(pFileInt->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, &cbTmpRead);
|
---|
698 | if (RT_SUCCESS(rc))
|
---|
699 | {
|
---|
700 | pFileInt->offCurrent = off + cbTmpRead;
|
---|
701 | if (pcbRead)
|
---|
702 | *pcbRead = cbTmpRead;
|
---|
703 | if (rc == VINF_EOF)
|
---|
704 | rc = pcbRead ? VINF_SUCCESS : VERR_EOF;
|
---|
705 | }
|
---|
706 | else if (pcbRead)
|
---|
707 | *pcbRead = 0;
|
---|
708 | return rc;
|
---|
709 | }
|
---|
710 |
|
---|
711 |
|
---|
712 | RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
713 | {
|
---|
714 | PRTTARFILEINTERNAL pFileInt = hFile;
|
---|
715 | RTTARFILE_VALID_RETURN(pFileInt);
|
---|
716 |
|
---|
717 | if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
|
---|
718 | return VERR_ACCESS_DENIED;
|
---|
719 |
|
---|
720 | size_t cbTmpWritten = 0;
|
---|
721 | int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToWrite, &cbTmpWritten);
|
---|
722 | pFileInt->cbSize += cbTmpWritten;
|
---|
723 | pFileInt->offCurrent = off + cbTmpWritten;
|
---|
724 | if (pcbWritten)
|
---|
725 | *pcbWritten = cbTmpWritten;
|
---|
726 |
|
---|
727 | return rc;
|
---|
728 | }
|
---|
729 |
|
---|
730 |
|
---|
731 | RTR3DECL(int) RTTarFileGetSize(RTTARFILE hFile, uint64_t *pcbSize)
|
---|
732 | {
|
---|
733 | /* Validate input */
|
---|
734 | AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
|
---|
735 |
|
---|
736 | PRTTARFILEINTERNAL pFileInt = hFile;
|
---|
737 | RTTARFILE_VALID_RETURN(pFileInt);
|
---|
738 |
|
---|
739 | *pcbSize = RT_MAX(pFileInt->cbSetSize, pFileInt->cbSize);
|
---|
740 |
|
---|
741 | return VINF_SUCCESS;
|
---|
742 | }
|
---|
743 |
|
---|
744 |
|
---|
745 | RTR3DECL(int) RTTarFileSetSize(RTTARFILE hFile, uint64_t cbSize)
|
---|
746 | {
|
---|
747 | PRTTARFILEINTERNAL pFileInt = hFile;
|
---|
748 | RTTARFILE_VALID_RETURN(pFileInt);
|
---|
749 |
|
---|
750 | if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
|
---|
751 | return VERR_WRITE_ERROR;
|
---|
752 |
|
---|
753 | /** @todo If cbSize is smaller than pFileInt->cbSize we have to
|
---|
754 | * truncate the current file. */
|
---|
755 | pFileInt->cbSetSize = cbSize;
|
---|
756 |
|
---|
757 | return VINF_SUCCESS;
|
---|
758 | }
|
---|
759 |
|
---|