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