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