VirtualBox

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

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

Export/import OVA/OVF package supports ISO images. The problem with the wrong search files in the archive has been resolved. (see #5429). 2 new elements SASD and EPASD were added in the OVF XML file structure (see #6022).

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