VirtualBox

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

Last change on this file since 46336 was 46336, checked in by vboxsync, 12 years ago

Runtime/tar: don't crop the file name silently if it does not fit into the tar record

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