VirtualBox

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

Last change on this file since 34952 was 34952, checked in by vboxsync, 14 years ago

tar.cpp: < 8GB, not <=. Added note about using 256-base encoding with the old ustar format. Return MIN/MAX instead of 0 on overflow.

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