VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/zip/tar.cpp@ 50154

Last change on this file since 50154 was 50154, checked in by vboxsync, 11 years ago

Reduce the TAR API to what's currently used.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.3 KB
Line 
1/* $Id: tar.cpp 50154 2014-01-21 19:51:00Z vboxsync $ */
2/** @file
3 * IPRT - Tar archive I/O.
4 */
5
6/*
7 * Copyright (C) 2009-2014 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#define RT_USE_TAR_VFS_FOR_ALL_READS // the old code sticks around for a short while for debugging the new.
28
29/******************************************************************************
30 * Header Files *
31 ******************************************************************************/
32#include "internal/iprt.h"
33#include <iprt/tar.h>
34
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#include <iprt/err.h>
38#include <iprt/file.h>
39#include <iprt/mem.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
43# include <iprt/vfs.h>
44# include <iprt/zip.h>
45#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
46
47
48#include "internal/magics.h"
49#include "tar.h"
50
51
52/******************************************************************************
53 * Structures and Typedefs *
54 ******************************************************************************/
55
56/** @name RTTARRECORD::h::linkflag
57 * @{ */
58#define LF_OLDNORMAL '\0' /**< Normal disk file, Unix compatible */
59#define LF_NORMAL '0' /**< Normal disk file */
60#define LF_LINK '1' /**< Link to previously dumped file */
61#define LF_SYMLINK '2' /**< Symbolic link */
62#define LF_CHR '3' /**< Character special file */
63#define LF_BLK '4' /**< Block special file */
64#define LF_DIR '5' /**< Directory */
65#define LF_FIFO '6' /**< FIFO special file */
66#define LF_CONTIG '7' /**< Contiguous file */
67/** @} */
68
69/**
70 * A tar file header.
71 */
72typedef union RTTARRECORD
73{
74 char d[512];
75 struct h
76 {
77 char name[100];
78 char mode[8];
79 char uid[8];
80 char gid[8];
81 char size[12];
82 char mtime[12];
83 char chksum[8];
84 char linkflag;
85 char linkname[100];
86 char magic[8];
87 char uname[32];
88 char gname[32];
89 char devmajor[8];
90 char devminor[8];
91 } h;
92} RTTARRECORD;
93AssertCompileSize(RTTARRECORD, 512);
94AssertCompileMemberOffset(RTTARRECORD, h.size, 100+8*3);
95AssertCompileMemberSize(RTTARRECORD, h.name, RTTAR_NAME_MAX+1);
96/** Pointer to a tar file header. */
97typedef RTTARRECORD *PRTTARRECORD;
98
99/** Pointer to a tar file handle. */
100typedef struct RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
101
102/**
103 * The internal data of a tar handle.
104 */
105typedef struct RTTARINTERNAL
106{
107 /** The magic (RTTAR_MAGIC). */
108 uint32_t u32Magic;
109 /** The handle to the tar file. */
110 RTFILE hTarFile;
111 /** The open mode for hTarFile. */
112 uint32_t fOpenMode;
113 /** Whether a file within the archive is currently open for writing.
114 * Only one can be open. */
115 bool fFileOpenForWrite;
116 /** Whether operating in stream mode. */
117 bool fStreamMode;
118#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
119 /** The file cache of one file. */
120 PRTTARFILEINTERNAL pFileCache;
121#else /* RT_USE_TAR_VFS_FOR_ALL_READS */
122 /** The tar file VFS handle. */
123 RTVFSFILE hVfsFile;
124 /** The tar file system VFS handle. */
125 RTVFSFSSTREAM hVfsFss;
126 /** Set if hVfsFss is at the start of the stream and doesn't need rewinding. */
127 bool fFssAtStart;
128 /** The current stream object (fStreamMode = true). */
129 RTVFSIOSTREAM hVfsCur;
130 /** The name of the current object (fStreamMode = true). */
131 char *pszVfsCurName;
132#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
133} RTTARINTERNAL;
134/** Pointer to a the internal data of a tar handle. */
135typedef RTTARINTERNAL* PRTTARINTERNAL;
136
137/**
138 * The internal data of a file within a tar file.
139 */
140typedef struct RTTARFILEINTERNAL
141{
142 /** The magic (RTTARFILE_MAGIC). */
143 uint32_t u32Magic;
144 /** The open mode. */
145 uint32_t fOpenMode;
146 /** Pointer to back to the tar file. */
147 PRTTARINTERNAL pTar;
148 /** The name of the file. */
149 char *pszFilename;
150 /** The offset into the archive where the file header starts. */
151 uint64_t offStart;
152 /** The size of the file. */
153 uint64_t cbSize;
154 /** The size set by RTTarFileSetSize(). */
155 uint64_t cbSetSize;
156 /** The current offset within this file. */
157 uint64_t offCurrent;
158#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
159 /** The link flag. */
160 char linkflag;
161#endif
162#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
163 /** The VFS I/O stream (only for reading atm). */
164 RTVFSIOSTREAM hVfsIos;
165#endif
166} RTTARFILEINTERNAL;
167/** Pointer to the internal data of a tar file. */
168typedef RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
169
170
171
172/******************************************************************************
173 * Defined Constants And Macros *
174 ******************************************************************************/
175
176/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
177/* RTTAR */
178#define RTTAR_VALID_RETURN_RC(hHandle, rc) \
179 do { \
180 AssertPtrReturn((hHandle), (rc)); \
181 AssertReturn((hHandle)->u32Magic == RTTAR_MAGIC, (rc)); \
182 } while (0)
183/* RTTARFILE */
184#define RTTARFILE_VALID_RETURN_RC(hHandle, rc) \
185 do { \
186 AssertPtrReturn((hHandle), (rc)); \
187 AssertReturn((hHandle)->u32Magic == RTTARFILE_MAGIC, (rc)); \
188 } while (0)
189
190/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
191/* RTTAR */
192#define RTTAR_VALID_RETURN(hHandle) RTTAR_VALID_RETURN_RC((hHandle), VERR_INVALID_HANDLE)
193/* RTTARFILE */
194#define RTTARFILE_VALID_RETURN(hHandle) RTTARFILE_VALID_RETURN_RC((hHandle), VERR_INVALID_HANDLE)
195
196/** Validates a handle and returns (void) if not valid. */
197/* RTTAR */
198#define RTTAR_VALID_RETURN_VOID(hHandle) \
199 do { \
200 AssertPtrReturnVoid(hHandle); \
201 AssertReturnVoid((hHandle)->u32Magic == RTTAR_MAGIC); \
202 } while (0)
203/* RTTARFILE */
204#define RTTARFILE_VALID_RETURN_VOID(hHandle) \
205 do { \
206 AssertPtrReturnVoid(hHandle); \
207 AssertReturnVoid((hHandle)->u32Magic == RTTARFILE_MAGIC); \
208 } while (0)
209
210
211/******************************************************************************
212 * Internal Functions *
213 ******************************************************************************/
214
215DECLINLINE(void) rtTarSizeToRec(PRTTARRECORD pRecord, uint64_t cbSize)
216{
217 /*
218 * Small enough for the standard octal string encoding?
219 *
220 * Note! We could actually use the terminator character as well if we liked,
221 * but let not do that as it's easier to test this way.
222 */
223 if (cbSize < _4G * 2U)
224 RTStrPrintf(pRecord->h.size, sizeof(pRecord->h.size), "%0.11llo", cbSize);
225 else
226 {
227 /*
228 * Base 256 extension. Set the highest bit of the left most character.
229 * We don't deal with negatives here, cause the size have to be greater
230 * than zero.
231 *
232 * Note! The base-256 extension are never used by gtar or libarchive
233 * with the "ustar \0" format version, only the later
234 * "ustar\000" version. However, this shouldn't cause much
235 * trouble as they are not picky about what they read.
236 */
237 size_t cchField = sizeof(pRecord->h.size) - 1;
238 unsigned char *puchField = (unsigned char*)pRecord->h.size;
239 puchField[0] = 0x80;
240 do
241 {
242 puchField[cchField--] = cbSize & 0xff;
243 cbSize >>= 8;
244 } while (cchField);
245 }
246}
247
248#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
249DECLINLINE(uint64_t) rtTarRecToSize(PRTTARRECORD pRecord)
250{
251 int64_t cbSize = 0;
252 if (pRecord->h.size[0] & 0x80)
253 {
254 size_t cchField = sizeof(pRecord->h.size);
255 unsigned char const *puchField = (unsigned char const *)pRecord->h.size;
256
257 /*
258 * The first byte has the bit 7 set to indicate base-256, while bit 6
259 * is the signed bit. Bits 5:0 are the most significant value bits.
260 */
261 cbSize = !(0x40 & *puchField) ? 0 : -1;
262 cbSize = (cbSize << 6) | (*puchField & 0x3f);
263 cchField--;
264 puchField++;
265
266 /*
267 * The remaining bytes are used in full.
268 */
269 while (cchField-- > 0)
270 {
271 if (RT_UNLIKELY( cbSize > INT64_MAX / 256
272 || cbSize < INT64_MIN / 256))
273 {
274 cbSize = cbSize < 0 ? INT64_MIN : INT64_MAX;
275 break;
276 }
277 cbSize = (cbSize << 8) | *puchField++;
278 }
279 }
280 else
281 RTStrToInt64Full(pRecord->h.size, 8, &cbSize);
282
283 if (cbSize < 0)
284 cbSize = 0;
285
286 return (uint64_t)cbSize;
287}
288#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
289
290/**
291 * Calculates the TAR header checksums and detects if it's all zeros.
292 *
293 * @returns true if all zeros, false if not.
294 * @param pHdr The header to checksum.
295 * @param pi32Unsigned Where to store the checksum calculated using
296 * unsigned chars. This is the one POSIX
297 * specifies.
298 * @param pi32Signed Where to store the checksum calculated using
299 * signed chars.
300 *
301 * @remarks The reason why we calculate the checksum as both signed and unsigned
302 * has to do with various the char C type being signed on some hosts
303 * and unsigned on others.
304 *
305 * @remarks Borrowed from tarvfs.cpp.
306 */
307static bool rtZipTarCalcChkSum(PCRTZIPTARHDR pHdr, int32_t *pi32Unsigned, int32_t *pi32Signed)
308{
309 int32_t i32Unsigned = 0;
310 int32_t i32Signed = 0;
311
312 /*
313 * Sum up the entire header.
314 */
315 const char *pch = (const char *)pHdr;
316 const char *pchEnd = pch + sizeof(*pHdr);
317 do
318 {
319 i32Unsigned += *(unsigned char *)pch;
320 i32Signed += *(signed char *)pch;
321 } while (++pch != pchEnd);
322
323 /*
324 * Check if it's all zeros and replace the chksum field with spaces.
325 */
326 bool const fZeroHdr = i32Unsigned == 0;
327
328 pch = pHdr->Common.chksum;
329 pchEnd = pch + sizeof(pHdr->Common.chksum);
330 do
331 {
332 i32Unsigned -= *(unsigned char *)pch;
333 i32Signed -= *(signed char *)pch;
334 } while (++pch != pchEnd);
335
336 i32Unsigned += (unsigned char)' ' * sizeof(pHdr->Common.chksum);
337 i32Signed += (signed char)' ' * sizeof(pHdr->Common.chksum);
338
339 *pi32Unsigned = i32Unsigned;
340 if (pi32Signed)
341 *pi32Signed = i32Signed;
342 return fZeroHdr;
343}
344
345#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
346DECLINLINE(int) rtTarReadHeaderRecord(RTFILE hFile, PRTTARRECORD pRecord)
347{
348 int rc = RTFileRead(hFile, pRecord, sizeof(RTTARRECORD), NULL);
349 /* Check for EOF. EOF is valid in this case, cause it indicates no more
350 * data in the tar archive. */
351 if (rc == VERR_EOF)
352 return VERR_TAR_END_OF_FILE;
353 /* Report any other errors */
354 else if (RT_FAILURE(rc))
355 return rc;
356
357 /* Check for data integrity & an EOF record */
358 int32_t iUnsignedChksum, iSignedChksum;
359 if (rtZipTarCalcChkSum((PCRTZIPTARHDR)pRecord, &iUnsignedChksum, &iSignedChksum))
360 return VERR_TAR_END_OF_FILE;
361
362 /* Verify the checksum */
363 uint32_t sum;
364 rc = RTStrToUInt32Full(pRecord->h.chksum, 8, &sum);
365 if ( RT_SUCCESS(rc)
366 && ( sum == (uint32_t)iSignedChksum
367 || sum == (uint32_t)iUnsignedChksum) )
368 {
369 /* Make sure the strings are zero terminated. */
370 pRecord->h.name[sizeof(pRecord->h.name) - 1] = 0;
371 pRecord->h.linkname[sizeof(pRecord->h.linkname) - 1] = 0;
372 pRecord->h.magic[sizeof(pRecord->h.magic) - 1] = 0;
373 pRecord->h.uname[sizeof(pRecord->h.uname) - 1] = 0;
374 pRecord->h.gname[sizeof(pRecord->h.gname) - 1] = 0;
375 }
376 else
377 rc = VERR_TAR_CHKSUM_MISMATCH;
378
379 return rc;
380}
381#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
382
383DECLINLINE(int) rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize,
384 RTUID uid, RTGID gid, RTFMODE fmode, int64_t mtime)
385{
386 /** @todo check for field overflows. */
387 /* Fill the header record */
388// RT_ZERO(pRecord); - done by the caller.
389 /** @todo use RTStrCopy */
390 size_t cb = RTStrPrintf(pRecord->h.name, sizeof(pRecord->h.name), "%s", pszSrcName);
391 if (cb < strlen(pszSrcName))
392 return VERR_BUFFER_OVERFLOW;
393 RTStrPrintf(pRecord->h.mode, sizeof(pRecord->h.mode), "%0.7o", fmode);
394 RTStrPrintf(pRecord->h.uid, sizeof(pRecord->h.uid), "%0.7o", uid);
395 RTStrPrintf(pRecord->h.gid, sizeof(pRecord->h.gid), "%0.7o", gid);
396 rtTarSizeToRec(pRecord, cbSize);
397 RTStrPrintf(pRecord->h.mtime, sizeof(pRecord->h.mtime), "%0.11llo", mtime);
398 RTStrPrintf(pRecord->h.magic, sizeof(pRecord->h.magic), "ustar ");
399 RTStrPrintf(pRecord->h.uname, sizeof(pRecord->h.uname), "someone");
400 RTStrPrintf(pRecord->h.gname, sizeof(pRecord->h.gname), "someone");
401 pRecord->h.linkflag = LF_NORMAL;
402
403 /* Create the checksum out of the new header */
404 int32_t iUnsignedChksum, iSignedChksum;
405 if (rtZipTarCalcChkSum((PCRTZIPTARHDR)pRecord, &iUnsignedChksum, &iSignedChksum))
406 return VERR_TAR_END_OF_FILE;
407
408 /* Format the checksum */
409 RTStrPrintf(pRecord->h.chksum, sizeof(pRecord->h.chksum), "%0.7o", iUnsignedChksum);
410
411 return VINF_SUCCESS;
412}
413
414DECLINLINE(void *) rtTarMemTmpAlloc(size_t *pcbSize)
415{
416 *pcbSize = 0;
417 /* Allocate a reasonably large buffer, fall back on a tiny one.
418 * Note: has to be 512 byte aligned and >= 512 byte. */
419 size_t cbTmp = _1M;
420 void *pvTmp = RTMemTmpAlloc(cbTmp);
421 if (!pvTmp)
422 {
423 cbTmp = sizeof(RTTARRECORD);
424 pvTmp = RTMemTmpAlloc(cbTmp);
425 }
426 *pcbSize = cbTmp;
427 return pvTmp;
428}
429
430DECLINLINE(int) rtTarAppendZeros(RTTARFILE hFile, uint64_t cbSize)
431{
432 /* Allocate a temporary buffer for copying the tar content in blocks. */
433 size_t cbTmp = 0;
434 void *pvTmp = rtTarMemTmpAlloc(&cbTmp);
435 if (!pvTmp)
436 return VERR_NO_MEMORY;
437 RT_BZERO(pvTmp, cbTmp);
438
439 int rc = VINF_SUCCESS;
440 uint64_t cbAllWritten = 0;
441 size_t cbWritten = 0;
442 for (;;)
443 {
444 if (cbAllWritten >= cbSize)
445 break;
446 size_t cbToWrite = RT_MIN(cbSize - cbAllWritten, cbTmp);
447 rc = RTTarFileWrite(hFile, pvTmp, cbToWrite, &cbWritten);
448 if (RT_FAILURE(rc))
449 break;
450 cbAllWritten += cbWritten;
451 }
452
453 RTMemTmpFree(pvTmp);
454
455 return rc;
456}
457
458DECLINLINE(PRTTARFILEINTERNAL) rtCreateTarFileInternal(PRTTARINTERNAL pInt, const char *pszFilename, uint32_t fOpen)
459{
460 PRTTARFILEINTERNAL pFileInt = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(RTTARFILEINTERNAL));
461 if (!pFileInt)
462 return NULL;
463
464 pFileInt->u32Magic = RTTARFILE_MAGIC;
465#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
466 pFileInt->pTar = pInt;
467#endif
468 pFileInt->fOpenMode = fOpen;
469 pFileInt->pszFilename = RTStrDup(pszFilename);
470 if (!pFileInt->pszFilename)
471 {
472#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
473 pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
474#endif
475 RTMemFree(pFileInt);
476 return NULL;
477 }
478
479 return pFileInt;
480}
481
482#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
483
484/**
485 * Creates a tar file handle for a read-only VFS stream object.
486 *
487 * @returns IPRT status code.
488 * @param pszName The file name. Automatically freed on failure.
489 * @param hVfsIos The VFS I/O stream we create the handle around.
490 * The reference is NOT consumed.
491 * @param fOpen The open flags.
492 * @param ppFile Where to return the handle.
493 */
494static int rtTarFileCreateHandleForReadOnly(char *pszName, RTVFSIOSTREAM hVfsIos, uint32_t fOpen, PRTTARFILEINTERNAL *ppFile)
495{
496 int rc;
497 PRTTARFILEINTERNAL pNewFile = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(*pNewFile));
498 if (pNewFile)
499 {
500 RTFSOBJINFO ObjInfo;
501 rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_UNIX);
502 if (RT_SUCCESS(rc))
503 {
504 pNewFile->u32Magic = RTTARFILE_MAGIC;
505 pNewFile->pTar = NULL;
506 pNewFile->pszFilename = pszName;
507 pNewFile->offStart = UINT64_MAX;
508 pNewFile->cbSize = ObjInfo.cbObject;
509 pNewFile->cbSetSize = 0;
510 pNewFile->offCurrent = 0;
511 pNewFile->fOpenMode = fOpen;
512 pNewFile->hVfsIos = hVfsIos;
513
514 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos); Assert(cRefs != UINT32_MAX); NOREF(cRefs);
515
516 *ppFile = pNewFile;
517 return VINF_SUCCESS;
518 }
519
520 RTMemFree(pNewFile);
521 }
522 else
523 rc = VERR_NO_MEMORY;
524 RTStrFree(pszName);
525 return rc;
526}
527
528#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
529
530DECLINLINE(PRTTARFILEINTERNAL) rtCopyTarFileInternal(PRTTARFILEINTERNAL pInt)
531{
532 PRTTARFILEINTERNAL pNewInt = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(RTTARFILEINTERNAL));
533 if (!pNewInt)
534 return NULL;
535
536 memcpy(pNewInt, pInt, sizeof(RTTARFILEINTERNAL));
537 pNewInt->pszFilename = RTStrDup(pInt->pszFilename);
538 if (!pNewInt->pszFilename)
539 {
540 RTMemFree(pNewInt);
541 return NULL;
542 }
543
544 return pNewInt;
545}
546
547#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
548
549DECLINLINE(void) rtDeleteTarFileInternal(PRTTARFILEINTERNAL pInt)
550{
551 if (pInt)
552 {
553 if (pInt->pszFilename)
554 RTStrFree(pInt->pszFilename);
555#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
556 if (pInt->hVfsIos != NIL_RTVFSIOSTREAM)
557 {
558 RTVfsIoStrmRelease(pInt->hVfsIos);
559 pInt->hVfsIos = NIL_RTVFSIOSTREAM;
560 }
561#endif
562
563 pInt->u32Magic = RTTARFILE_MAGIC_DEAD;
564 RTMemFree(pInt);
565 }
566}
567
568static int rtTarAppendFileFromFile(RTTAR hTar, const char *pszSrcName, const uint64_t cbOverallSize, uint64_t &cbOverallWritten, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
569{
570 /* Open the source file */
571 RTFILE hOldFile;
572 int rc = RTFileOpen(&hOldFile, pszSrcName, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE);
573 if (RT_FAILURE(rc))
574 return rc;
575
576 RTTARFILE hFile = NIL_RTTARFILE;
577 void *pvTmp = NULL;
578 do
579 {
580 /* Get the size of the source file */
581 uint64_t cbToCopy;
582 rc = RTFileGetSize(hOldFile, &cbToCopy);
583 if (RT_FAILURE(rc))
584 break;
585
586 rc = RTTarFileOpen(hTar, &hFile, RTPathFilename(pszSrcName), RTFILE_O_OPEN | RTFILE_O_WRITE);
587 if (RT_FAILURE(rc))
588 break;
589
590 /* Get some info from the source file */
591 RTFSOBJINFO info;
592 RTUID uid = 0;
593 RTGID gid = 0;
594 RTFMODE fmode = 0600; /* Make some save default */
595 int64_t mtime = 0;
596
597 /* This isn't critical. Use the defaults if it fails. */
598 rc = RTFileQueryInfo(hOldFile, &info, RTFSOBJATTRADD_UNIX);
599 if (RT_SUCCESS(rc))
600 {
601 fmode = info.Attr.fMode & RTFS_UNIX_MASK;
602 uid = info.Attr.u.Unix.uid;
603 gid = info.Attr.u.Unix.gid;
604 mtime = RTTimeSpecGetSeconds(&info.ModificationTime);
605 }
606
607 /* Set the mode from the other file */
608 rc = RTTarFileSetMode(hFile, fmode);
609 if (RT_FAILURE(rc))
610 break;
611
612 /* Set the modification time from the other file */
613 RTTIMESPEC time;
614 RTTimeSpecSetSeconds(&time, mtime);
615 rc = RTTarFileSetTime(hFile, &time);
616 if (RT_FAILURE(rc))
617 break;
618
619 /* Set the owner from the other file */
620 rc = RTTarFileSetOwner(hFile, uid, gid);
621 if (RT_FAILURE(rc))
622 break;
623
624 /* Allocate a temporary buffer for copying the tar content in blocks. */
625 size_t cbTmp = 0;
626 pvTmp = rtTarMemTmpAlloc(&cbTmp);
627 if (!pvTmp)
628 {
629 rc = VERR_NO_MEMORY;
630 break;
631 }
632
633 /* Copy the content from pszSrcName over to hFile. This is done block
634 * wise in 512 byte steps. After this copying is finished hFile will be
635 * on a 512 byte boundary, regardless if the file copied is 512 byte
636 * size aligned. */
637 uint64_t cbAllWritten = 0; /* Already copied */
638 uint64_t cbRead = 0; /* Actually read in the last step */
639 for (;;)
640 {
641 if (pfnProgressCallback)
642 pfnProgressCallback((unsigned)(100.0 / cbOverallSize * cbOverallWritten), pvUser);
643 if (cbAllWritten >= cbToCopy)
644 break;
645
646 /* Read one block. Either its the buffer size or the rest of the
647 * file. */
648 cbRead = RT_MIN(cbToCopy - cbAllWritten, cbTmp);
649 rc = RTFileRead(hOldFile, pvTmp, cbRead, NULL);
650 if (RT_FAILURE(rc))
651 break;
652
653 /* Write one block. */
654 rc = RTTarFileWriteAt(hFile, cbAllWritten, pvTmp, cbRead, NULL);
655 if (RT_FAILURE(rc))
656 break;
657
658 /* Count how many bytes (of the original file) are written already */
659 cbAllWritten += cbRead;
660 cbOverallWritten += cbRead;
661 }
662 } while (0);
663
664 /* Cleanup */
665 if (pvTmp)
666 RTMemTmpFree(pvTmp);
667
668 if (hFile)
669 RTTarFileClose(hFile);
670
671 RTFileClose(hOldFile);
672
673 return rc;
674}
675
676#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
677
678static int rtTarSkipData(RTFILE hFile, PRTTARRECORD pRecord)
679{
680 int rc = VINF_SUCCESS;
681 /* Seek over the data parts (512 bytes aligned) */
682 int64_t offSeek = RT_ALIGN(rtTarRecToSize(pRecord), sizeof(RTTARRECORD));
683 if (offSeek > 0)
684 rc = RTFileSeek(hFile, offSeek, RTFILE_SEEK_CURRENT, NULL);
685 return rc;
686}
687
688static int rtTarFindFile(RTFILE hFile, const char *pszFile, uint64_t *poff, uint64_t *pcbSize)
689{
690 /* Assume we are on the file head. */
691 int rc = VINF_SUCCESS;
692 bool fFound = false;
693 RTTARRECORD record;
694 for (;;)
695 {
696 /* Read & verify a header record */
697 rc = rtTarReadHeaderRecord(hFile, &record);
698 /* Check for error or EOF. */
699 if (RT_FAILURE(rc))
700 break;
701
702 /* We support normal files only */
703 if ( record.h.linkflag == LF_OLDNORMAL
704 || record.h.linkflag == LF_NORMAL)
705 {
706 if (!RTStrCmp(record.h.name, pszFile))
707 {
708 /* Get the file size */
709 *pcbSize = rtTarRecToSize(&record);
710 /* Seek back, to position the file pointer at the start of the header. */
711 rc = RTFileSeek(hFile, -(int64_t)sizeof(RTTARRECORD), RTFILE_SEEK_CURRENT, poff);
712 fFound = true;
713 break;
714 }
715 }
716 rc = rtTarSkipData(hFile, &record);
717 if (RT_FAILURE(rc))
718 break;
719 }
720
721 if (rc == VERR_TAR_END_OF_FILE)
722 rc = VINF_SUCCESS;
723
724 /* Something found? */
725 if ( RT_SUCCESS(rc)
726 && !fFound)
727 rc = VERR_FILE_NOT_FOUND;
728
729 return rc;
730}
731
732#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
733
734
735/******************************************************************************
736 * Public Functions *
737 ******************************************************************************/
738
739RTR3DECL(int) RTTarOpen(PRTTAR phTar, const char *pszTarname, uint32_t fMode, bool fStream)
740{
741 AssertReturn(!fStream || !(fMode & RTFILE_O_WRITE), VERR_INVALID_PARAMETER);
742
743 /*
744 * Create a tar instance.
745 */
746 PRTTARINTERNAL pThis = (PRTTARINTERNAL)RTMemAllocZ(sizeof(RTTARINTERNAL));
747 if (!pThis)
748 return VERR_NO_MEMORY;
749
750 pThis->u32Magic = RTTAR_MAGIC;
751 pThis->fOpenMode = fMode;
752 pThis->fStreamMode = fStream && (fMode & RTFILE_O_READ);
753
754 /*
755 * Open the tar file.
756 */
757 int rc;
758#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
759 pThis->hVfsFile = NIL_RTVFSFILE;
760 pThis->hVfsFss = NIL_RTVFSFSSTREAM;
761 pThis->fFssAtStart = false;
762 pThis->hVfsCur = NIL_RTVFSIOSTREAM;
763 pThis->pszVfsCurName = NULL;
764
765 if (!(fMode & RTFILE_O_WRITE))
766 {
767 rc = RTVfsFileOpenNormal(pszTarname, fMode, &pThis->hVfsFile);
768 if (RT_SUCCESS(rc))
769 {
770 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(pThis->hVfsFile);
771 rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &pThis->hVfsFss);
772 if (RT_SUCCESS(rc))
773 pThis->fFssAtStart = true;
774 else
775 {
776 RTVfsFileRelease(pThis->hVfsFile);
777 pThis->hVfsFile = NIL_RTVFSFILE;
778 }
779 RTVfsIoStrmRelease(hVfsIos);
780 }
781 }
782 else
783#endif
784 rc = RTFileOpen(&pThis->hTarFile, pszTarname, fMode);
785 if (RT_SUCCESS(rc))
786 {
787 *phTar = pThis;
788 return VINF_SUCCESS;
789 }
790
791 RTMemFree(pThis);
792 return rc;
793}
794
795RTR3DECL(int) RTTarClose(RTTAR hTar)
796{
797 if (hTar == NIL_RTTAR)
798 return VINF_SUCCESS;
799
800 PRTTARINTERNAL pInt = hTar;
801 RTTAR_VALID_RETURN(pInt);
802
803 int rc = VINF_SUCCESS;
804
805 /* gtar gives a warning, but the documentation says EOF is indicated by a
806 * zero block. Disabled for now. */
807#if 0
808 {
809 /* Append the EOF record which is filled all by zeros */
810 RTTARRECORD record;
811 RT_ZERO(record);
812 rc = RTFileWrite(pInt->hTarFile, &record, sizeof(record), NULL);
813 }
814#endif
815
816#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
817 if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
818 {
819 uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX);
820 pInt->hVfsFss = NIL_RTVFSFSSTREAM;
821 }
822
823 if (pInt->hVfsFile != NIL_RTVFSFILE)
824 {
825 uint32_t cRefs = RTVfsFileRelease(pInt->hVfsFile); Assert(cRefs != UINT32_MAX);
826 pInt->hVfsFile = NIL_RTVFSFILE;
827 }
828
829 if (pInt->hVfsCur != NIL_RTVFSIOSTREAM)
830 {
831 RTVfsIoStrmRelease(pInt->hVfsCur);
832 pInt->hVfsCur = NIL_RTVFSIOSTREAM;
833 }
834
835 if (pInt->pszVfsCurName)
836 {
837 RTStrFree(pInt->pszVfsCurName);
838 pInt->pszVfsCurName = NULL;
839 }
840#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
841
842 if (pInt->hTarFile != NIL_RTFILE)
843 {
844 rc = RTFileClose(pInt->hTarFile);
845 pInt->hTarFile = NIL_RTFILE;
846 }
847
848#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
849 /* Delete any remaining cached file headers. */
850 if (pInt->pFileCache)
851 {
852 rtDeleteTarFileInternal(pInt->pFileCache);
853 pInt->pFileCache = NULL;
854 }
855#endif
856
857 pInt->u32Magic = RTTAR_MAGIC_DEAD;
858
859 RTMemFree(pInt);
860
861 return rc;
862}
863
864RTR3DECL(int) RTTarFileOpen(RTTAR hTar, PRTTARFILE phFile, const char *pszFilename, uint32_t fOpen)
865{
866 AssertReturn((fOpen & RTFILE_O_READ) || (fOpen & RTFILE_O_WRITE), VERR_INVALID_PARAMETER);
867
868 PRTTARINTERNAL pInt = hTar;
869 RTTAR_VALID_RETURN(pInt);
870
871 if (!pInt->hTarFile)
872 return VERR_INVALID_HANDLE;
873
874 if (pInt->fStreamMode)
875 return VERR_INVALID_STATE;
876
877 if (fOpen & RTFILE_O_WRITE)
878 {
879 if (!(pInt->fOpenMode & RTFILE_O_WRITE))
880 return VERR_WRITE_PROTECT;
881 if (pInt->fFileOpenForWrite)
882 return VERR_TOO_MANY_OPEN_FILES;
883 }
884
885 int rc = VINF_SUCCESS;
886#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
887 if (!(fOpen & RTFILE_O_WRITE))
888 {
889 /*
890 * Rewind the stream if necessary.
891 */
892 if (!pInt->fFssAtStart)
893 {
894 if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
895 {
896 uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX);
897 pInt->hVfsFss = NIL_RTVFSFSSTREAM;
898 }
899
900 if (pInt->hVfsFile == NIL_RTVFSFILE)
901 {
902 rc = RTVfsFileFromRTFile(pInt->hTarFile, RTFILE_O_READ, true /*fLeaveOpen*/, &pInt->hVfsFile);
903 if (RT_FAILURE(rc))
904 return rc;
905 }
906 Assert(pInt->hVfsCur == NIL_RTVFSIOSTREAM && pInt->pszVfsCurName == NULL);
907
908 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(pInt->hVfsFile);
909 rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &pInt->hVfsFss);
910 RTVfsIoStrmRelease(hVfsIos);
911 if (RT_FAILURE(rc))
912 return rc;
913 }
914
915 /*
916 * Search the file system stream.
917 */
918 pInt->fFssAtStart = false;
919 for (;;)
920 {
921 char *pszName;
922 RTVFSOBJTYPE enmType;
923 RTVFSOBJ hVfsObj;
924 rc = RTVfsFsStrmNext(pInt->hVfsFss, &pszName, &enmType, &hVfsObj);
925 if (rc == VERR_EOF)
926 return VERR_FILE_NOT_FOUND;
927 if (RT_FAILURE(rc))
928 return rc;
929
930 if (!RTStrCmp(pszName, pszFilename))
931 {
932 if (enmType == RTVFSOBJTYPE_FILE || enmType == RTVFSOBJTYPE_IO_STREAM)
933 rc = rtTarFileCreateHandleForReadOnly(pszName, RTVfsObjToIoStream(hVfsObj), fOpen, phFile);
934 else
935 {
936 rc = VERR_UNEXPECTED_FS_OBJ_TYPE;
937 RTStrFree(pszName);
938 }
939 RTVfsObjRelease(hVfsObj);
940 break;
941 }
942 RTStrFree(pszName);
943 RTVfsObjRelease(hVfsObj);
944 } /* Search loop. */
945 }
946 else
947#endif /* RT_USE_TAR_VFS_FOR_ALL_READS */
948 {
949 PRTTARFILEINTERNAL pFileInt = rtCreateTarFileInternal(pInt, pszFilename, fOpen);
950 if (!pFileInt)
951 return VERR_NO_MEMORY;
952
953 do /* break loop */
954 {
955#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
956 if (pFileInt->fOpenMode & RTFILE_O_WRITE)
957#endif
958 {
959 pInt->fFileOpenForWrite = true;
960
961 /* If we are in write mode, we also in append mode. Add an dummy
962 * header at the end of the current file. It will be filled by the
963 * close operation. */
964 rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_END, &pFileInt->offStart);
965 if (RT_FAILURE(rc))
966 break;
967 RTTARRECORD record;
968 RT_ZERO(record);
969 rc = RTFileWrite(pFileInt->pTar->hTarFile, &record, sizeof(RTTARRECORD), NULL);
970 if (RT_FAILURE(rc))
971 break;
972 }
973#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
974 else
975 {
976 Assert(pFileInt->fOpenMode & RTFILE_O_READ); /* see first assertion */
977
978 /* We need to be on the start of the file */
979 rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_BEGIN, NULL);
980 if (RT_FAILURE(rc))
981 break;
982
983 /* Search for the file. */
984 rc = rtTarFindFile(pFileInt->pTar->hTarFile, pszFilename, &pFileInt->offStart, &pFileInt->cbSize);
985 if (RT_FAILURE(rc))
986 break;
987 }
988#endif
989 } while (0);
990
991 /* Cleanup on failure */
992 if (RT_FAILURE(rc))
993 {
994 if (pFileInt->pszFilename)
995 RTStrFree(pFileInt->pszFilename);
996 RTMemFree(pFileInt);
997 }
998 else
999 *phFile = (RTTARFILE)pFileInt;
1000 }
1001
1002 return rc;
1003}
1004
1005RTR3DECL(int) RTTarFileClose(RTTARFILE hFile)
1006{
1007 /* Already closed? */
1008 if (hFile == NIL_RTTARFILE)
1009 return VINF_SUCCESS;
1010
1011 PRTTARFILEINTERNAL pFileInt = hFile;
1012 RTTARFILE_VALID_RETURN(pFileInt);
1013
1014 int rc = VINF_SUCCESS;
1015
1016 /* In read mode: */
1017 if (pFileInt->fOpenMode & RTFILE_O_READ)
1018 {
1019#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
1020 /* In read mode, we want to make sure to stay at the aligned end of this
1021 * file, so the next file could be read immediately. */
1022 uint64_t offCur = RTFileTell(pFileInt->pTar->hTarFile);
1023
1024 /* Check that the file pointer is somewhere within the last open file.
1025 * If we are at the beginning (nothing read yet) nothing will be done.
1026 * A user could open/close a file more than once, without reading
1027 * something. */
1028 if ( pFileInt->offStart + sizeof(RTTARRECORD) < offCur
1029 && offCur < RT_ALIGN(pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD)))
1030 {
1031 /* Seek to the next file header. */
1032 uint64_t offNext = RT_ALIGN(pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize, sizeof(RTTARRECORD));
1033 rc = RTFileSeek(pFileInt->pTar->hTarFile, offNext - offCur, RTFILE_SEEK_CURRENT, NULL);
1034 }
1035#endif
1036 }
1037 else if (pFileInt->fOpenMode & RTFILE_O_WRITE)
1038 {
1039 pFileInt->pTar->fFileOpenForWrite = false;
1040 do
1041 {
1042 /* If the user has called RTTarFileSetSize in the meantime, we have
1043 to make sure the file has the right size. */
1044 if (pFileInt->cbSetSize > pFileInt->cbSize)
1045 {
1046 rc = rtTarAppendZeros(hFile, pFileInt->cbSetSize - pFileInt->cbSize);
1047 if (RT_FAILURE(rc))
1048 break;
1049 }
1050
1051 /* If the written size isn't 512 byte aligned, we need to fix this. */
1052 RTTARRECORD record;
1053 RT_ZERO(record);
1054 uint64_t cbSizeAligned = RT_ALIGN(pFileInt->cbSize, sizeof(RTTARRECORD));
1055 if (cbSizeAligned != pFileInt->cbSize)
1056 {
1057 /* Note the RTFile method. We didn't increase the cbSize or cbCurrentPos here. */
1058 rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
1059 pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize,
1060 &record,
1061 cbSizeAligned - pFileInt->cbSize,
1062 NULL);
1063 if (RT_FAILURE(rc))
1064 break;
1065 }
1066
1067 /* Create a header record for the file */
1068 /* Todo: mode, gid, uid, mtime should be setable (or detected myself) */
1069 RTTIMESPEC time;
1070 RTTimeNow(&time);
1071 rc = rtTarCreateHeaderRecord(&record, pFileInt->pszFilename, pFileInt->cbSize,
1072 0, 0, 0600, RTTimeSpecGetSeconds(&time));
1073 if (RT_FAILURE(rc))
1074 break;
1075
1076 /* Write this at the start of the file data */
1077 rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart, &record, sizeof(RTTARRECORD), NULL);
1078 if (RT_FAILURE(rc))
1079 break;
1080 }
1081 while (0);
1082 }
1083
1084 /* Now cleanup and delete the handle */
1085 rtDeleteTarFileInternal(pFileInt);
1086
1087 return rc;
1088}
1089
1090#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
1091RTR3DECL(int) RTTarFileSeek(RTTARFILE hFile, uint64_t offSeek, unsigned uMethod, uint64_t *poffActual)
1092{
1093 PRTTARFILEINTERNAL pFileInt = hFile;
1094 RTTARFILE_VALID_RETURN(pFileInt);
1095
1096 if (pFileInt->pTar->fStreamMode)
1097 return VERR_INVALID_STATE;
1098
1099 switch (uMethod)
1100 {
1101 case RTFILE_SEEK_BEGIN:
1102 {
1103 if (offSeek > pFileInt->cbSize)
1104 return VERR_SEEK_ON_DEVICE;
1105 pFileInt->offCurrent = offSeek;
1106 break;
1107 }
1108 case RTFILE_SEEK_CURRENT:
1109 {
1110 if (pFileInt->offCurrent + offSeek > pFileInt->cbSize)
1111 return VERR_SEEK_ON_DEVICE;
1112 pFileInt->offCurrent += offSeek;
1113 break;
1114 }
1115 case RTFILE_SEEK_END:
1116 {
1117 if ((int64_t)pFileInt->cbSize - (int64_t)offSeek < 0)
1118 return VERR_NEGATIVE_SEEK;
1119 pFileInt->offCurrent = pFileInt->cbSize - offSeek;
1120 break;
1121 }
1122 default: AssertFailedReturn(VERR_INVALID_PARAMETER);
1123 }
1124
1125 if (poffActual)
1126 *poffActual = pFileInt->offCurrent;
1127
1128 return VINF_SUCCESS;
1129}
1130#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1131
1132
1133#ifndef RT_USE_TAR_VFS_FOR_ALL_READS
1134RTR3DECL(uint64_t) RTTarFileTell(RTTARFILE hFile)
1135{
1136 PRTTARFILEINTERNAL pFileInt = hFile;
1137 RTTARFILE_VALID_RETURN_RC(pFileInt, UINT64_MAX);
1138
1139 return pFileInt->offCurrent;
1140}
1141#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1142
1143RTR3DECL(int) RTTarFileRead(RTTARFILE hFile, void *pvBuf, size_t cbToRead, size_t *pcbRead)
1144{
1145 PRTTARFILEINTERNAL pFileInt = hFile;
1146 RTTARFILE_VALID_RETURN(pFileInt);
1147
1148 return RTTarFileReadAt(hFile, pFileInt->offCurrent, pvBuf, cbToRead, pcbRead);
1149}
1150
1151RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
1152{
1153 PRTTARFILEINTERNAL pFileInt = hFile;
1154 RTTARFILE_VALID_RETURN(pFileInt);
1155
1156#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
1157
1158 size_t cbTmpRead = 0;
1159 int rc = RTVfsIoStrmReadAt(pFileInt->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, &cbTmpRead);
1160 if (RT_SUCCESS(rc))
1161 {
1162 pFileInt->offCurrent = off + cbTmpRead;
1163 if (pcbRead)
1164 *pcbRead = cbTmpRead;
1165 if (rc == VINF_EOF)
1166 rc = pcbRead ? VINF_SUCCESS : VERR_EOF;
1167 }
1168 else if (pcbRead)
1169 *pcbRead = 0;
1170#else
1171
1172 /* Check that we not read behind the end of file. If so return immediately. */
1173 if (off > pFileInt->cbSize)
1174 {
1175 if (pcbRead)
1176 *pcbRead = 0;
1177 return VINF_SUCCESS; /* ??? VERR_EOF */
1178 }
1179
1180 size_t cbToCopy = RT_MIN(pFileInt->cbSize - off, cbToRead);
1181 size_t cbTmpRead = 0;
1182 int rc = RTFileReadAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToCopy, &cbTmpRead);
1183 pFileInt->offCurrent = off + cbTmpRead;
1184 if (pcbRead)
1185 *pcbRead = cbTmpRead;
1186
1187#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1188
1189 return rc;
1190}
1191
1192RTR3DECL(int) RTTarFileWrite(RTTARFILE hFile, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
1193{
1194 PRTTARFILEINTERNAL pFileInt = hFile;
1195 RTTARFILE_VALID_RETURN(pFileInt);
1196
1197 /** @todo Optimize this, by checking the current pos */
1198 return RTTarFileWriteAt(hFile, pFileInt->offCurrent, pvBuf, cbToWrite, pcbWritten);
1199}
1200
1201RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
1202{
1203 PRTTARFILEINTERNAL pFileInt = hFile;
1204 RTTARFILE_VALID_RETURN(pFileInt);
1205
1206 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
1207 return VERR_WRITE_ERROR;
1208
1209 size_t cbTmpWritten = 0;
1210 int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToWrite, &cbTmpWritten);
1211 pFileInt->cbSize += cbTmpWritten;
1212 pFileInt->offCurrent = off + cbTmpWritten;
1213 if (pcbWritten)
1214 *pcbWritten = cbTmpWritten;
1215
1216 return rc;
1217}
1218
1219RTR3DECL(int) RTTarFileGetSize(RTTARFILE hFile, uint64_t *pcbSize)
1220{
1221 /* Validate input */
1222 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
1223
1224 PRTTARFILEINTERNAL pFileInt = hFile;
1225 RTTARFILE_VALID_RETURN(pFileInt);
1226
1227 *pcbSize = RT_MAX(pFileInt->cbSetSize, pFileInt->cbSize);
1228
1229 return VINF_SUCCESS;
1230}
1231
1232RTR3DECL(int) RTTarFileSetSize(RTTARFILE hFile, uint64_t cbSize)
1233{
1234 PRTTARFILEINTERNAL pFileInt = hFile;
1235 RTTARFILE_VALID_RETURN(pFileInt);
1236
1237 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
1238 return VERR_WRITE_ERROR;
1239
1240 /** @todo If cbSize is smaller than pFileInt->cbSize we have to
1241 * truncate the current file. */
1242 pFileInt->cbSetSize = cbSize;
1243
1244 return VINF_SUCCESS;
1245}
1246
1247RTR3DECL(int) RTTarFileSetMode(RTTARFILE hFile, uint32_t fMode)
1248{
1249 PRTTARFILEINTERNAL pFileInt = hFile;
1250 RTTARFILE_VALID_RETURN(pFileInt);
1251
1252 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
1253 return VERR_WRITE_ERROR;
1254
1255 /* Convert the mode to an string. */
1256 char szMode[RT_SIZEOFMEMB(RTTARRECORD, h.mode)];
1257 RTStrPrintf(szMode, sizeof(szMode), "%0.7o", fMode);
1258
1259 /* Write it directly into the header */
1260 return RTFileWriteAt(pFileInt->pTar->hTarFile,
1261 pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.mode),
1262 szMode,
1263 RT_SIZEOFMEMB(RTTARRECORD, h.mode),
1264 NULL);
1265}
1266
1267RTR3DECL(int) RTTarFileSetTime(RTTARFILE hFile, PRTTIMESPEC pTime)
1268{
1269 PRTTARFILEINTERNAL pFileInt = hFile;
1270 RTTARFILE_VALID_RETURN(pFileInt);
1271
1272 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
1273 return VERR_WRITE_ERROR;
1274
1275 /* Convert the time to an string. */
1276 char szModTime[RT_SIZEOFMEMB(RTTARRECORD, h.mtime)];
1277 RTStrPrintf(szModTime, sizeof(szModTime), "%0.11llo", RTTimeSpecGetSeconds(pTime));
1278
1279 /* Write it directly into the header */
1280 return RTFileWriteAt(pFileInt->pTar->hTarFile,
1281 pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.mtime),
1282 szModTime,
1283 RT_SIZEOFMEMB(RTTARRECORD, h.mtime),
1284 NULL);
1285}
1286
1287RTR3DECL(int) RTTarFileSetOwner(RTTARFILE hFile, uint32_t uid, uint32_t gid)
1288{
1289 PRTTARFILEINTERNAL pFileInt = hFile;
1290 RTTARFILE_VALID_RETURN(pFileInt);
1291
1292 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
1293 return VERR_WRITE_ERROR;
1294 AssertReturn(uid == (uint32_t)-1 || uid <= 07777777, VERR_OUT_OF_RANGE);
1295 AssertReturn(gid == (uint32_t)-1 || gid <= 07777777, VERR_OUT_OF_RANGE);
1296
1297 int rc = VINF_SUCCESS;
1298
1299 if (uid != (uint32_t)-1)
1300 {
1301 /* Convert the uid to an string. */
1302 char szUid[RT_SIZEOFMEMB(RTTARRECORD, h.uid)];
1303 RTStrPrintf(szUid, sizeof(szUid), "%0.7o", uid);
1304
1305 /* Write it directly into the header */
1306 rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
1307 pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.uid),
1308 szUid,
1309 RT_SIZEOFMEMB(RTTARRECORD, h.uid),
1310 NULL);
1311 if (RT_FAILURE(rc))
1312 return rc;
1313 }
1314
1315 if (gid != (uint32_t)-1)
1316 {
1317 /* Convert the gid to an string. */
1318 char szGid[RT_SIZEOFMEMB(RTTARRECORD, h.gid)];
1319 RTStrPrintf(szGid, sizeof(szGid), "%0.7o", gid);
1320
1321 /* Write it directly into the header */
1322 rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
1323 pFileInt->offStart + RT_OFFSETOF(RTTARRECORD, h.gid),
1324 szGid,
1325 RT_SIZEOFMEMB(RTTARRECORD, h.gid),
1326 NULL);
1327 if (RT_FAILURE(rc))
1328 return rc;
1329 }
1330
1331 return rc;
1332}
1333
1334/******************************************************************************
1335 * Convenience Functions *
1336 ******************************************************************************/
1337
1338RTR3DECL(int) RTTarList(const char *pszTarFile, char ***ppapszFiles, size_t *pcFiles)
1339{
1340 /* Validate input */
1341 AssertPtrReturn(pszTarFile, VERR_INVALID_POINTER);
1342 AssertPtrReturn(ppapszFiles, VERR_INVALID_POINTER);
1343 AssertPtrReturn(pcFiles, VERR_INVALID_POINTER);
1344
1345 /* Open the tar file */
1346 RTTAR hTar;
1347 int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE, false /*fStream*/);
1348 if (RT_FAILURE(rc))
1349 return rc;
1350
1351#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
1352 /*
1353 * Enumerate the VFS file system stream.
1354 */
1355 size_t cFiles = 0;
1356 size_t cFilesAllocated = 0;
1357 char **papszFiles = NULL;
1358 for (;;)
1359 {
1360 char *pszName;
1361 RTVFSOBJTYPE enmType;
1362 RTVFSOBJ hVfsObj;
1363 rc = RTVfsFsStrmNext(hTar->hVfsFss, &pszName, &enmType, &hVfsObj);
1364 if (rc == VERR_EOF)
1365 {
1366 RTTarClose(hTar);
1367 *pcFiles = cFiles;
1368 *ppapszFiles = papszFiles;
1369 return VINF_SUCCESS;
1370 }
1371 if (RT_FAILURE(rc))
1372 break;
1373
1374 if (cFiles >= cFilesAllocated)
1375 {
1376 size_t cNew = !cFilesAllocated ? 64 : cFilesAllocated < _1M ? cFilesAllocated * 2 : cFilesAllocated + _1M;
1377 void *pvNew = RTMemRealloc(papszFiles, cNew * sizeof(char *));
1378 if (!pvNew)
1379 {
1380 rc = VERR_NO_MEMORY;
1381 RTStrFree(pszName);
1382 RTVfsObjRelease(hVfsObj);
1383 break;
1384 }
1385 cFilesAllocated = cNew;
1386 papszFiles = (char **)pvNew;
1387 }
1388
1389 papszFiles[cFiles++] = pszName;
1390
1391 RTVfsObjRelease(hVfsObj);
1392 } /* Search loop. */
1393
1394 /*
1395 * Failed, clean up and return.
1396 */
1397 if (papszFiles)
1398 {
1399 while (cFiles-- > 0)
1400 RTStrFree(papszFiles[cFiles]);
1401 RTMemFree(papszFiles);
1402 }
1403
1404#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1405
1406 /* This is done by internal methods, cause we didn't have a RTTARDIR
1407 * interface, yet. This should be fixed someday. */
1408
1409 PRTTARINTERNAL pInt = hTar;
1410 char **papszFiles = NULL;
1411 size_t cFiles = 0;
1412 do /* break loop */
1413 {
1414 /* Initialize the file name array with one slot */
1415 size_t cFilesAlloc = 1;
1416 papszFiles = (char **)RTMemAlloc(sizeof(char *));
1417 if (!papszFiles)
1418 {
1419 rc = VERR_NO_MEMORY;
1420 break;
1421 }
1422
1423 /* Iterate through the tar file record by record. Skip data records as we
1424 * didn't need them. */
1425 RTTARRECORD record;
1426 for (;;)
1427 {
1428 /* Read & verify a header record */
1429 rc = rtTarReadHeaderRecord(pInt->hTarFile, &record);
1430 /* Check for error or EOF. */
1431 if (RT_FAILURE(rc))
1432 break;
1433 /* We support normal files only */
1434 if ( record.h.linkflag == LF_OLDNORMAL
1435 || record.h.linkflag == LF_NORMAL)
1436 {
1437 if (cFiles >= cFilesAlloc)
1438 {
1439 /* Double the array size, make sure the size doesn't wrap. */
1440 void *pvNew = NULL;
1441 size_t cbNew = cFilesAlloc * sizeof(char *) * 2;
1442 if (cbNew / sizeof(char *) / 2 == cFilesAlloc)
1443 pvNew = RTMemRealloc(papszFiles, cbNew);
1444 if (!pvNew)
1445 {
1446 rc = VERR_NO_MEMORY;
1447 break;
1448 }
1449 papszFiles = (char **)pvNew;
1450 cFilesAlloc *= 2;
1451 }
1452
1453 /* Duplicate the name */
1454 papszFiles[cFiles] = RTStrDup(record.h.name);
1455 if (!papszFiles[cFiles])
1456 {
1457 rc = VERR_NO_MEMORY;
1458 break;
1459 }
1460 cFiles++;
1461 }
1462 rc = rtTarSkipData(pInt->hTarFile, &record);
1463 if (RT_FAILURE(rc))
1464 break;
1465 }
1466 } while (0);
1467
1468 if (rc == VERR_TAR_END_OF_FILE)
1469 rc = VINF_SUCCESS;
1470
1471 /* Return the file array on success, dispose of it on failure. */
1472 if (RT_SUCCESS(rc))
1473 {
1474 *pcFiles = cFiles;
1475 *ppapszFiles = papszFiles;
1476 }
1477 else
1478 {
1479 while (cFiles-- > 0)
1480 RTStrFree(papszFiles[cFiles]);
1481 RTMemFree(papszFiles);
1482 }
1483#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1484
1485 RTTarClose(hTar);
1486
1487 return rc;
1488}
1489
1490RTR3DECL(int) RTTarCreate(const char *pszTarFile, const char * const *papszFiles, size_t cFiles, PFNRTPROGRESS pfnProgressCallback, void *pvUser)
1491{
1492 /* Validate input */
1493 AssertPtrReturn(pszTarFile, VERR_INVALID_POINTER);
1494 AssertPtrReturn(papszFiles, VERR_INVALID_POINTER);
1495 AssertReturn(cFiles, VERR_INVALID_PARAMETER);
1496 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_POINTER);
1497 AssertPtrNullReturn(pvUser, VERR_INVALID_POINTER);
1498
1499 RTTAR hTar;
1500 int rc = RTTarOpen(&hTar, pszTarFile, RTFILE_O_CREATE | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE, false /*fStream*/);
1501 if (RT_FAILURE(rc))
1502 return rc;
1503
1504 /* Get the overall size of all files to pack into the tar archive. Only
1505 necessary if there is a progress callback. */
1506 uint64_t cbOverallSize = 0;
1507 if (pfnProgressCallback)
1508 for (size_t i = 0; i < cFiles; ++i)
1509 {
1510 uint64_t cbSize;
1511 rc = RTFileQuerySize(papszFiles[i], &cbSize);
1512 if (RT_FAILURE(rc))
1513 break;
1514 cbOverallSize += cbSize;
1515 }
1516 uint64_t cbOverallWritten = 0;
1517 for (size_t i = 0; i < cFiles; ++i)
1518 {
1519 rc = rtTarAppendFileFromFile(hTar, papszFiles[i], cbOverallSize, cbOverallWritten, pfnProgressCallback, pvUser);
1520 if (RT_FAILURE(rc))
1521 break;
1522 }
1523
1524 /* Cleanup */
1525 RTTarClose(hTar);
1526
1527 return rc;
1528}
1529
1530/******************************************************************************
1531 * Streaming Functions *
1532 ******************************************************************************/
1533
1534RTR3DECL(int) RTTarCurrentFile(RTTAR hTar, char **ppszFilename)
1535{
1536 /* Validate input. */
1537 AssertPtrNullReturn(ppszFilename, VERR_INVALID_POINTER);
1538
1539 PRTTARINTERNAL pInt = hTar;
1540 RTTAR_VALID_RETURN(pInt);
1541
1542#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
1543 if (!pInt->fStreamMode)
1544 return VERR_INVALID_STATE;
1545
1546 if (!pInt->pszVfsCurName)
1547 {
1548 int rc = RTTarSeekNextFile(pInt);
1549 if (RT_FAILURE(rc))
1550 return rc;
1551 }
1552 Assert(pInt->pszVfsCurName);
1553
1554 if (ppszFilename)
1555 {
1556 *ppszFilename = RTStrDup(pInt->pszVfsCurName);
1557 if (!*ppszFilename)
1558 return VERR_NO_STR_MEMORY;
1559 }
1560
1561 return pInt->hVfsCur != NIL_RTVFSIOSTREAM ? VINF_SUCCESS : VINF_TAR_DIR_PATH;
1562
1563#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1564 /* Open and close the file on the current position. This makes sure the
1565 * cache is filled in case we never read something before. On success it
1566 * will return the current filename. */
1567 RTTARFILE hFile;
1568 int rc = RTTarFileOpenCurrentFile(hTar, &hFile, ppszFilename, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
1569 if (RT_SUCCESS(rc))
1570 RTTarFileClose(hFile);
1571
1572 return rc;
1573#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1574}
1575
1576RTR3DECL(int) RTTarSeekNextFile(RTTAR hTar)
1577{
1578 PRTTARINTERNAL pInt = hTar;
1579 RTTAR_VALID_RETURN(pInt);
1580
1581 if (!pInt->fStreamMode)
1582 return VERR_INVALID_STATE;
1583
1584#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
1585 /*
1586 * Release the current object.
1587 */
1588 if (pInt->hVfsCur != NIL_RTVFSIOSTREAM)
1589 {
1590 RTVfsIoStrmRelease(pInt->hVfsCur);
1591 pInt->hVfsCur = NIL_RTVFSIOSTREAM;
1592 }
1593
1594 if (pInt->pszVfsCurName)
1595 {
1596 RTStrFree(pInt->pszVfsCurName);
1597 pInt->pszVfsCurName = NULL;
1598 }
1599
1600 /*
1601 * Find the next file.
1602 */
1603 for (;;)
1604 {
1605 char *pszName;
1606 RTVFSOBJTYPE enmType;
1607 RTVFSOBJ hVfsObj;
1608 int rc = RTVfsFsStrmNext(hTar->hVfsFss, &pszName, &enmType, &hVfsObj);
1609 if (rc == VERR_EOF)
1610 return VERR_TAR_END_OF_FILE;
1611
1612 if ( enmType == RTVFSOBJTYPE_FILE
1613 || enmType == RTVFSOBJTYPE_IO_STREAM
1614 || enmType == RTVFSOBJTYPE_DIR)
1615 {
1616 pInt->pszVfsCurName = pszName;
1617 if (enmType == RTVFSOBJTYPE_DIR)
1618 rc = VINF_TAR_DIR_PATH;
1619 else
1620 {
1621 pInt->hVfsCur = RTVfsObjToIoStream(hVfsObj);
1622 Assert(pInt->hVfsCur != NIL_RTVFSIOSTREAM);
1623 rc = VINF_SUCCESS;
1624 }
1625 RTVfsObjRelease(hVfsObj);
1626 return rc;
1627 }
1628 RTStrFree(pszName);
1629 RTVfsObjRelease(hVfsObj);
1630 }
1631
1632#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1633 int rc = VINF_SUCCESS;
1634
1635 /* If there is nothing in the cache, it means we never read something. Just
1636 * ask for the current filename to fill the cache. */
1637 if (!pInt->pFileCache)
1638 {
1639 rc = RTTarCurrentFile(hTar, NULL);
1640 if (RT_FAILURE(rc))
1641 return rc;
1642 }
1643
1644 /* Check that the file pointer is somewhere within the last open file.
1645 * If not we are somehow busted. */
1646 uint64_t offCur = RTFileTell(pInt->hTarFile);
1647 if (!( pInt->pFileCache->offStart <= offCur
1648 && offCur <= pInt->pFileCache->offStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize))
1649 return VERR_INVALID_STATE;
1650
1651 /* Seek to the next file header. */
1652 uint64_t offNext = RT_ALIGN(pInt->pFileCache->offStart + sizeof(RTTARRECORD) + pInt->pFileCache->cbSize, sizeof(RTTARRECORD));
1653 if (pInt->pFileCache->cbSize != 0)
1654 {
1655 rc = RTFileSeek(pInt->hTarFile, offNext - offCur, RTFILE_SEEK_CURRENT, NULL);
1656 if (RT_FAILURE(rc))
1657 return rc;
1658 }
1659 else
1660 {
1661 /* Else delete the last open file cache. Might be recreated below. */
1662 rtDeleteTarFileInternal(pInt->pFileCache);
1663 pInt->pFileCache = NULL;
1664 }
1665
1666 /* Again check the current filename to fill the cache with the new value. */
1667 return RTTarCurrentFile(hTar, NULL);
1668#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1669}
1670
1671RTR3DECL(int) RTTarFileOpenCurrentFile(RTTAR hTar, PRTTARFILE phFile, char **ppszFilename, uint32_t fOpen)
1672{
1673 /* Validate input. */
1674 AssertPtrReturn(phFile, VERR_INVALID_POINTER);
1675 AssertPtrNullReturn(ppszFilename, VERR_INVALID_POINTER);
1676 AssertReturn((fOpen & RTFILE_O_READ), VERR_INVALID_PARAMETER); /* Only valid in read mode. */
1677
1678 PRTTARINTERNAL pInt = hTar;
1679 RTTAR_VALID_RETURN(pInt);
1680
1681 if (!pInt->fStreamMode)
1682 return VERR_INVALID_STATE;
1683
1684#ifdef RT_USE_TAR_VFS_FOR_ALL_READS
1685 /*
1686 * Make sure there is a current file (first call w/o RTTarSeekNextFile call).
1687 */
1688 if (pInt->hVfsCur == NIL_RTVFSIOSTREAM)
1689 {
1690 if (pInt->pszVfsCurName)
1691 return -VINF_TAR_DIR_PATH;
1692
1693 int rc = RTTarSeekNextFile(pInt);
1694 if (RT_FAILURE(rc))
1695 return rc;
1696
1697 if (pInt->hVfsCur == NIL_RTVFSIOSTREAM)
1698 return -VINF_TAR_DIR_PATH;
1699 }
1700 Assert(pInt->pszVfsCurName);
1701
1702 /*
1703 * Return a copy of the filename if requested.
1704 */
1705 if (ppszFilename)
1706 {
1707 *ppszFilename = RTStrDup(pInt->pszVfsCurName);
1708 if (!*ppszFilename)
1709 return VERR_NO_STR_MEMORY;
1710 }
1711
1712 /*
1713 * Create a handle for it.
1714 */
1715 int rc = rtTarFileCreateHandleForReadOnly(RTStrDup(pInt->pszVfsCurName), pInt->hVfsCur, RTFILE_O_READ, phFile);
1716 if (RT_FAILURE(rc) && ppszFilename)
1717 {
1718 RTStrFree(*ppszFilename);
1719 *ppszFilename = NULL;
1720 }
1721
1722#else /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1723
1724 int rc = VINF_SUCCESS;
1725
1726 /* Is there some cached entry? */
1727 if (pInt->pFileCache)
1728 {
1729 if (pInt->pFileCache->offStart + sizeof(RTTARRECORD) < RTFileTell(pInt->hTarFile))
1730 {
1731 /* Else delete the last open file cache. Might be recreated below. */
1732 rtDeleteTarFileInternal(pInt->pFileCache);
1733 pInt->pFileCache = NULL;
1734 }
1735 else/* Are we still directly behind that header? */
1736 {
1737 /* Yes, so the streaming can start. Just return the cached file
1738 * structure to the caller. */
1739 *phFile = rtCopyTarFileInternal(pInt->pFileCache);
1740 if (ppszFilename)
1741 *ppszFilename = RTStrDup(pInt->pFileCache->pszFilename);
1742 if (pInt->pFileCache->linkflag == LF_DIR)
1743 return VINF_TAR_DIR_PATH;
1744 return VINF_SUCCESS;
1745 }
1746
1747 }
1748
1749 PRTTARFILEINTERNAL pFileInt = NULL;
1750 do /* break loop */
1751 {
1752 /* Try to read a header entry from the current position. If we aren't
1753 * on a header record, the header checksum will show and an error will
1754 * be returned. */
1755 RTTARRECORD record;
1756 /* Read & verify a header record */
1757 rc = rtTarReadHeaderRecord(pInt->hTarFile, &record);
1758 /* Check for error or EOF. */
1759 if (RT_FAILURE(rc))
1760 break;
1761
1762 /* We support normal files only */
1763 if ( record.h.linkflag == LF_OLDNORMAL
1764 || record.h.linkflag == LF_NORMAL
1765 || record.h.linkflag == LF_DIR)
1766 {
1767 pFileInt = rtCreateTarFileInternal(pInt, record.h.name, fOpen);
1768 if (!pFileInt)
1769 {
1770 rc = VERR_NO_MEMORY;
1771 break;
1772 }
1773
1774 /* Get the file size */
1775 pFileInt->cbSize = rtTarRecToSize(&record);
1776 /* The start is -512 from here. */
1777 pFileInt->offStart = RTFileTell(pInt->hTarFile) - sizeof(RTTARRECORD);
1778 /* remember the type of a file */
1779 pFileInt->linkflag = record.h.linkflag;
1780
1781 /* Copy the new file structure to our cache. */
1782 pInt->pFileCache = rtCopyTarFileInternal(pFileInt);
1783 if (ppszFilename)
1784 *ppszFilename = RTStrDup(pFileInt->pszFilename);
1785
1786 if (pFileInt->linkflag == LF_DIR)
1787 rc = VINF_TAR_DIR_PATH;
1788 }
1789 } while (0);
1790
1791 if (RT_FAILURE(rc))
1792 {
1793 if (pFileInt)
1794 rtDeleteTarFileInternal(pFileInt);
1795 }
1796 else
1797 *phFile = pFileInt;
1798
1799#endif /* !RT_USE_TAR_VFS_FOR_ALL_READS */
1800 return rc;
1801}
1802
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