VirtualBox

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

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

Deprecated the old manifest API. r=bird: IPRT does NOT use ugly C++ references, it pointers directly - no cloak and dagger pointers thank you. Please don't reformat if expressions into unreadability. RTDIGESTTYPE_UNKNOWN should be RTDIGESTTYPE_INVALID and there is actually no need to set output variables on failure, just check the status code.

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