VirtualBox

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

Last change on this file since 39070 was 39070, checked in by vboxsync, 13 years ago

VMM,IPRT: -Wunused-function.

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