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