VirtualBox

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

Last change on this file since 50205 was 50205, checked in by vboxsync, 11 years ago

tar.cpp: Drop the TAR file reading code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.5 KB
Line 
1/* $Id: tar.cpp 50205 2014-01-24 00:49:04Z 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
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#include <iprt/vfs.h>
42#include <iprt/zip.h>
43
44
45#include "internal/magics.h"
46#include "tar.h"
47
48
49/*******************************************************************************
50* Structures and Typedefs *
51*******************************************************************************/
52/** @name RTTARRECORD::h::linkflag
53 * @{ */
54#define LF_OLDNORMAL '\0' /**< Normal disk file, Unix compatible */
55#define LF_NORMAL '0' /**< Normal disk file */
56#define LF_LINK '1' /**< Link to previously dumped file */
57#define LF_SYMLINK '2' /**< Symbolic link */
58#define LF_CHR '3' /**< Character special file */
59#define LF_BLK '4' /**< Block special file */
60#define LF_DIR '5' /**< Directory */
61#define LF_FIFO '6' /**< FIFO special file */
62#define LF_CONTIG '7' /**< Contiguous file */
63/** @} */
64
65/**
66 * A tar file header.
67 */
68typedef union RTTARRECORD
69{
70 char d[512];
71 struct h
72 {
73 char name[100];
74 char mode[8];
75 char uid[8];
76 char gid[8];
77 char size[12];
78 char mtime[12];
79 char chksum[8];
80 char linkflag;
81 char linkname[100];
82 char magic[8];
83 char uname[32];
84 char gname[32];
85 char devmajor[8];
86 char devminor[8];
87 } h;
88} RTTARRECORD;
89AssertCompileSize(RTTARRECORD, 512);
90AssertCompileMemberOffset(RTTARRECORD, h.size, 100+8*3);
91AssertCompileMemberSize(RTTARRECORD, h.name, RTTAR_NAME_MAX+1);
92/** Pointer to a tar file header. */
93typedef RTTARRECORD *PRTTARRECORD;
94
95/** Pointer to a tar file handle. */
96typedef struct RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
97
98/**
99 * The internal data of a tar handle.
100 */
101typedef struct RTTARINTERNAL
102{
103 /** The magic (RTTAR_MAGIC). */
104 uint32_t u32Magic;
105 /** The handle to the tar file. */
106 RTFILE hTarFile;
107 /** The open mode for hTarFile. */
108 uint32_t fOpenMode;
109 /** Whether a file within the archive is currently open for writing.
110 * Only one can be open. */
111 bool fFileOpenForWrite;
112 /** The tar file VFS handle (for reading). */
113 RTVFSFILE hVfsFile;
114 /** The tar file system VFS handle. */
115 RTVFSFSSTREAM hVfsFss;
116 /** Set if hVfsFss is at the start of the stream and doesn't need rewinding. */
117 bool fFssAtStart;
118} RTTARINTERNAL;
119/** Pointer to a the internal data of a tar handle. */
120typedef RTTARINTERNAL* PRTTARINTERNAL;
121
122/**
123 * The internal data of a file within a tar file.
124 */
125typedef struct RTTARFILEINTERNAL
126{
127 /** The magic (RTTARFILE_MAGIC). */
128 uint32_t u32Magic;
129 /** The open mode. */
130 uint32_t fOpenMode;
131 /** Pointer to back to the tar file. */
132 PRTTARINTERNAL pTar;
133 /** The name of the file. */
134 char *pszFilename;
135 /** The offset into the archive where the file header starts. */
136 uint64_t offStart;
137 /** The size of the file. */
138 uint64_t cbSize;
139 /** The size set by RTTarFileSetSize(). */
140 uint64_t cbSetSize;
141 /** The current offset within this file. */
142 uint64_t offCurrent;
143 /** The VFS I/O stream (only for reading atm). */
144 RTVFSIOSTREAM hVfsIos;
145} RTTARFILEINTERNAL;
146/** Pointer to the internal data of a tar file. */
147typedef RTTARFILEINTERNAL *PRTTARFILEINTERNAL;
148
149
150
151/*******************************************************************************
152* Defined Constants And Macros *
153*******************************************************************************/
154
155/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
156/* RTTAR */
157#define RTTAR_VALID_RETURN_RC(hHandle, rc) \
158 do { \
159 AssertPtrReturn((hHandle), (rc)); \
160 AssertReturn((hHandle)->u32Magic == RTTAR_MAGIC, (rc)); \
161 } while (0)
162/* RTTARFILE */
163#define RTTARFILE_VALID_RETURN_RC(hHandle, rc) \
164 do { \
165 AssertPtrReturn((hHandle), (rc)); \
166 AssertReturn((hHandle)->u32Magic == RTTARFILE_MAGIC, (rc)); \
167 } while (0)
168
169/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
170/* RTTAR */
171#define RTTAR_VALID_RETURN(hHandle) RTTAR_VALID_RETURN_RC((hHandle), VERR_INVALID_HANDLE)
172/* RTTARFILE */
173#define RTTARFILE_VALID_RETURN(hHandle) RTTARFILE_VALID_RETURN_RC((hHandle), VERR_INVALID_HANDLE)
174
175/** Validates a handle and returns (void) if not valid. */
176/* RTTAR */
177#define RTTAR_VALID_RETURN_VOID(hHandle) \
178 do { \
179 AssertPtrReturnVoid(hHandle); \
180 AssertReturnVoid((hHandle)->u32Magic == RTTAR_MAGIC); \
181 } while (0)
182/* RTTARFILE */
183#define RTTARFILE_VALID_RETURN_VOID(hHandle) \
184 do { \
185 AssertPtrReturnVoid(hHandle); \
186 AssertReturnVoid((hHandle)->u32Magic == RTTARFILE_MAGIC); \
187 } while (0)
188
189
190RTR3DECL(int) RTTarOpen(PRTTAR phTar, const char *pszTarname, uint32_t fMode)
191{
192 AssertReturn(fMode & RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
193
194 /*
195 * Create a tar instance.
196 */
197 PRTTARINTERNAL pThis = (PRTTARINTERNAL)RTMemAllocZ(sizeof(RTTARINTERNAL));
198 if (!pThis)
199 return VERR_NO_MEMORY;
200
201 pThis->u32Magic = RTTAR_MAGIC;
202 pThis->fOpenMode = fMode;
203 pThis->hVfsFile = NIL_RTVFSFILE;
204 pThis->hVfsFss = NIL_RTVFSFSSTREAM;
205 pThis->fFssAtStart = false;
206
207 /*
208 * Open the tar file.
209 */
210 int rc = RTFileOpen(&pThis->hTarFile, pszTarname, fMode);
211 if (RT_SUCCESS(rc))
212 {
213 *phTar = pThis;
214 return VINF_SUCCESS;
215 }
216
217 RTMemFree(pThis);
218 return rc;
219}
220
221
222RTR3DECL(int) RTTarClose(RTTAR hTar)
223{
224 if (hTar == NIL_RTTAR)
225 return VINF_SUCCESS;
226
227 PRTTARINTERNAL pInt = hTar;
228 RTTAR_VALID_RETURN(pInt);
229
230 int rc = VINF_SUCCESS;
231
232 /* gtar gives a warning, but the documentation says EOF is indicated by a
233 * zero block. Disabled for now. */
234#if 0
235 {
236 /* Append the EOF record which is filled all by zeros */
237 RTTARRECORD record;
238 RT_ZERO(record);
239 rc = RTFileWrite(pInt->hTarFile, &record, sizeof(record), NULL);
240 }
241#endif
242
243 if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
244 {
245 uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX);
246 pInt->hVfsFss = NIL_RTVFSFSSTREAM;
247 }
248
249 if (pInt->hVfsFile != NIL_RTVFSFILE)
250 {
251 uint32_t cRefs = RTVfsFileRelease(pInt->hVfsFile); Assert(cRefs != UINT32_MAX);
252 pInt->hVfsFile = NIL_RTVFSFILE;
253 }
254
255 if (pInt->hTarFile != NIL_RTFILE)
256 {
257 rc = RTFileClose(pInt->hTarFile);
258 pInt->hTarFile = NIL_RTFILE;
259 }
260
261 pInt->u32Magic = RTTAR_MAGIC_DEAD;
262
263 RTMemFree(pInt);
264
265 return rc;
266}
267
268
269/**
270 * Creates a tar file handle for a read-only VFS stream object.
271 *
272 * @returns IPRT status code.
273 * @param pszName The file name. Automatically freed on failure.
274 * @param hVfsIos The VFS I/O stream we create the handle around.
275 * The reference is NOT consumed.
276 * @param fOpen The open flags.
277 * @param ppFile Where to return the handle.
278 */
279static int rtTarFileCreateHandleForReadOnly(char *pszName, RTVFSIOSTREAM hVfsIos, uint32_t fOpen, PRTTARFILEINTERNAL *ppFile)
280{
281 int rc;
282 PRTTARFILEINTERNAL pNewFile = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(*pNewFile));
283 if (pNewFile)
284 {
285 RTFSOBJINFO ObjInfo;
286 rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_UNIX);
287 if (RT_SUCCESS(rc))
288 {
289 pNewFile->u32Magic = RTTARFILE_MAGIC;
290 pNewFile->pTar = NULL;
291 pNewFile->pszFilename = pszName;
292 pNewFile->offStart = UINT64_MAX;
293 pNewFile->cbSize = ObjInfo.cbObject;
294 pNewFile->cbSetSize = 0;
295 pNewFile->offCurrent = 0;
296 pNewFile->fOpenMode = fOpen;
297 pNewFile->hVfsIos = hVfsIos;
298
299 uint32_t cRefs = RTVfsIoStrmRetain(hVfsIos); Assert(cRefs != UINT32_MAX); NOREF(cRefs);
300
301 *ppFile = pNewFile;
302 return VINF_SUCCESS;
303 }
304
305 RTMemFree(pNewFile);
306 }
307 else
308 rc = VERR_NO_MEMORY;
309 RTStrFree(pszName);
310 return rc;
311}
312
313
314/* Only used for write handles. */
315static PRTTARFILEINTERNAL rtTarFileCreateForWrite(PRTTARINTERNAL pInt, const char *pszFilename, uint32_t fOpen)
316{
317 PRTTARFILEINTERNAL pFileInt = (PRTTARFILEINTERNAL)RTMemAllocZ(sizeof(RTTARFILEINTERNAL));
318 if (!pFileInt)
319 return NULL;
320
321 pFileInt->u32Magic = RTTARFILE_MAGIC;
322 pFileInt->pTar = pInt;
323 pFileInt->fOpenMode = fOpen;
324 pFileInt->pszFilename = RTStrDup(pszFilename);
325 if (!pFileInt->pszFilename)
326 {
327 pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
328 RTMemFree(pFileInt);
329 return NULL;
330 }
331
332 return pFileInt;
333}
334
335
336RTR3DECL(int) RTTarFileOpen(RTTAR hTar, PRTTARFILE phFile, const char *pszFilename, uint32_t fOpen)
337{
338 /* Write only interface now. */
339 AssertReturn(fOpen & RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
340
341 PRTTARINTERNAL pInt = hTar;
342 RTTAR_VALID_RETURN(pInt);
343
344 if (!pInt->hTarFile)
345 return VERR_INVALID_HANDLE;
346
347 if (fOpen & RTFILE_O_WRITE)
348 {
349 if (!(pInt->fOpenMode & RTFILE_O_WRITE))
350 return VERR_WRITE_PROTECT;
351 if (pInt->fFileOpenForWrite)
352 return VERR_TOO_MANY_OPEN_FILES;
353 }
354
355 int rc = VINF_SUCCESS;
356 if (!(fOpen & RTFILE_O_WRITE))
357 {
358 /*
359 * Rewind the stream if necessary.
360 */
361 if (!pInt->fFssAtStart)
362 {
363 if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
364 {
365 uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX);
366 pInt->hVfsFss = NIL_RTVFSFSSTREAM;
367 }
368
369 if (pInt->hVfsFile == NIL_RTVFSFILE)
370 {
371 rc = RTVfsFileFromRTFile(pInt->hTarFile, RTFILE_O_READ, true /*fLeaveOpen*/, &pInt->hVfsFile);
372 if (RT_FAILURE(rc))
373 return rc;
374 }
375
376 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(pInt->hVfsFile);
377 rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &pInt->hVfsFss);
378 RTVfsIoStrmRelease(hVfsIos);
379 if (RT_FAILURE(rc))
380 return rc;
381 }
382
383 /*
384 * Search the file system stream.
385 */
386 pInt->fFssAtStart = false;
387 for (;;)
388 {
389 char *pszName;
390 RTVFSOBJTYPE enmType;
391 RTVFSOBJ hVfsObj;
392 rc = RTVfsFsStrmNext(pInt->hVfsFss, &pszName, &enmType, &hVfsObj);
393 if (rc == VERR_EOF)
394 return VERR_FILE_NOT_FOUND;
395 if (RT_FAILURE(rc))
396 return rc;
397
398 if (!RTStrCmp(pszName, pszFilename))
399 {
400 if (enmType == RTVFSOBJTYPE_FILE || enmType == RTVFSOBJTYPE_IO_STREAM)
401 rc = rtTarFileCreateHandleForReadOnly(pszName, RTVfsObjToIoStream(hVfsObj), fOpen, phFile);
402 else
403 {
404 rc = VERR_UNEXPECTED_FS_OBJ_TYPE;
405 RTStrFree(pszName);
406 }
407 RTVfsObjRelease(hVfsObj);
408 break;
409 }
410 RTStrFree(pszName);
411 RTVfsObjRelease(hVfsObj);
412 } /* Search loop. */
413 }
414 else
415 {
416 PRTTARFILEINTERNAL pFileInt = rtTarFileCreateForWrite(pInt, pszFilename, fOpen);
417 if (!pFileInt)
418 return VERR_NO_MEMORY;
419
420 pInt->fFileOpenForWrite = true;
421
422 /* If we are in write mode, we also in append mode. Add an dummy
423 * header at the end of the current file. It will be filled by the
424 * close operation. */
425 rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_END, &pFileInt->offStart);
426 if (RT_SUCCESS(rc))
427 {
428 RTTARRECORD record;
429 RT_ZERO(record);
430 rc = RTFileWrite(pFileInt->pTar->hTarFile, &record, sizeof(RTTARRECORD), NULL);
431 }
432
433 if (RT_SUCCESS(rc))
434 *phFile = (RTTARFILE)pFileInt;
435 else
436 {
437 /* Cleanup on failure */
438 if (pFileInt->pszFilename)
439 RTStrFree(pFileInt->pszFilename);
440 RTMemFree(pFileInt);
441 }
442 }
443
444 return rc;
445}
446
447
448/**
449 * Calculates the TAR header checksums and detects if it's all zeros.
450 *
451 * @returns true if all zeros, false if not.
452 * @param pHdr The header to checksum.
453 * @param pi32Unsigned Where to store the checksum calculated using
454 * unsigned chars. This is the one POSIX
455 * specifies.
456 * @param pi32Signed Where to store the checksum calculated using
457 * signed chars.
458 *
459 * @remarks The reason why we calculate the checksum as both signed and unsigned
460 * has to do with various the char C type being signed on some hosts
461 * and unsigned on others.
462 *
463 * @remarks Borrowed from tarvfs.cpp.
464 */
465static bool rtZipTarCalcChkSum(PCRTZIPTARHDR pHdr, int32_t *pi32Unsigned, int32_t *pi32Signed)
466{
467 int32_t i32Unsigned = 0;
468 int32_t i32Signed = 0;
469
470 /*
471 * Sum up the entire header.
472 */
473 const char *pch = (const char *)pHdr;
474 const char *pchEnd = pch + sizeof(*pHdr);
475 do
476 {
477 i32Unsigned += *(unsigned char *)pch;
478 i32Signed += *(signed char *)pch;
479 } while (++pch != pchEnd);
480
481 /*
482 * Check if it's all zeros and replace the chksum field with spaces.
483 */
484 bool const fZeroHdr = i32Unsigned == 0;
485
486 pch = pHdr->Common.chksum;
487 pchEnd = pch + sizeof(pHdr->Common.chksum);
488 do
489 {
490 i32Unsigned -= *(unsigned char *)pch;
491 i32Signed -= *(signed char *)pch;
492 } while (++pch != pchEnd);
493
494 i32Unsigned += (unsigned char)' ' * sizeof(pHdr->Common.chksum);
495 i32Signed += (signed char)' ' * sizeof(pHdr->Common.chksum);
496
497 *pi32Unsigned = i32Unsigned;
498 if (pi32Signed)
499 *pi32Signed = i32Signed;
500 return fZeroHdr;
501}
502
503
504static void rtTarSizeToRec(PRTTARRECORD pRecord, uint64_t cbSize)
505{
506 /*
507 * Small enough for the standard octal string encoding?
508 *
509 * Note! We could actually use the terminator character as well if we liked,
510 * but let not do that as it's easier to test this way.
511 */
512 if (cbSize < _4G * 2U)
513 RTStrPrintf(pRecord->h.size, sizeof(pRecord->h.size), "%0.11llo", cbSize);
514 else
515 {
516 /*
517 * Base 256 extension. Set the highest bit of the left most character.
518 * We don't deal with negatives here, cause the size have to be greater
519 * than zero.
520 *
521 * Note! The base-256 extension are never used by gtar or libarchive
522 * with the "ustar \0" format version, only the later
523 * "ustar\000" version. However, this shouldn't cause much
524 * trouble as they are not picky about what they read.
525 */
526 size_t cchField = sizeof(pRecord->h.size) - 1;
527 unsigned char *puchField = (unsigned char*)pRecord->h.size;
528 puchField[0] = 0x80;
529 do
530 {
531 puchField[cchField--] = cbSize & 0xff;
532 cbSize >>= 8;
533 } while (cchField);
534 }
535}
536
537
538static int rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize,
539 RTUID uid, RTGID gid, RTFMODE fmode, int64_t mtime)
540{
541 /** @todo check for field overflows. */
542 /* Fill the header record */
543// RT_ZERO(pRecord); - done by the caller.
544 /** @todo use RTStrCopy */
545 size_t cb = RTStrPrintf(pRecord->h.name, sizeof(pRecord->h.name), "%s", pszSrcName);
546 if (cb < strlen(pszSrcName))
547 return VERR_BUFFER_OVERFLOW;
548 RTStrPrintf(pRecord->h.mode, sizeof(pRecord->h.mode), "%0.7o", fmode);
549 RTStrPrintf(pRecord->h.uid, sizeof(pRecord->h.uid), "%0.7o", uid);
550 RTStrPrintf(pRecord->h.gid, sizeof(pRecord->h.gid), "%0.7o", gid);
551 rtTarSizeToRec(pRecord, cbSize);
552 RTStrPrintf(pRecord->h.mtime, sizeof(pRecord->h.mtime), "%0.11llo", mtime);
553 RTStrPrintf(pRecord->h.magic, sizeof(pRecord->h.magic), "ustar ");
554 RTStrPrintf(pRecord->h.uname, sizeof(pRecord->h.uname), "someone");
555 RTStrPrintf(pRecord->h.gname, sizeof(pRecord->h.gname), "someone");
556 pRecord->h.linkflag = LF_NORMAL;
557
558 /* Create the checksum out of the new header */
559 int32_t iUnsignedChksum, iSignedChksum;
560 if (rtZipTarCalcChkSum((PCRTZIPTARHDR)pRecord, &iUnsignedChksum, &iSignedChksum))
561 return VERR_TAR_END_OF_FILE;
562
563 /* Format the checksum */
564 RTStrPrintf(pRecord->h.chksum, sizeof(pRecord->h.chksum), "%0.7o", iUnsignedChksum);
565
566 return VINF_SUCCESS;
567}
568
569
570DECLINLINE(void *) rtTarMemTmpAlloc(size_t *pcbSize)
571{
572 *pcbSize = 0;
573 /* Allocate a reasonably large buffer, fall back on a tiny one.
574 * Note: has to be 512 byte aligned and >= 512 byte. */
575 size_t cbTmp = _1M;
576 void *pvTmp = RTMemTmpAlloc(cbTmp);
577 if (!pvTmp)
578 {
579 cbTmp = sizeof(RTTARRECORD);
580 pvTmp = RTMemTmpAlloc(cbTmp);
581 }
582 *pcbSize = cbTmp;
583 return pvTmp;
584}
585
586
587static int rtTarAppendZeros(PRTTARFILEINTERNAL pFileInt, uint64_t cbSize)
588{
589 /* Allocate a temporary buffer for copying the tar content in blocks. */
590 size_t cbTmp = 0;
591 void *pvTmp = rtTarMemTmpAlloc(&cbTmp);
592 if (!pvTmp)
593 return VERR_NO_MEMORY;
594 RT_BZERO(pvTmp, cbTmp);
595
596 int rc = VINF_SUCCESS;
597 uint64_t cbAllWritten = 0;
598 size_t cbWritten = 0;
599 for (;;)
600 {
601 if (cbAllWritten >= cbSize)
602 break;
603 size_t cbToWrite = RT_MIN(cbSize - cbAllWritten, cbTmp);
604 rc = RTTarFileWriteAt(pFileInt, pFileInt->offCurrent, pvTmp, cbToWrite, &cbWritten);
605 if (RT_FAILURE(rc))
606 break;
607 cbAllWritten += cbWritten;
608 }
609
610 RTMemTmpFree(pvTmp);
611
612 return rc;
613}
614
615
616RTR3DECL(int) RTTarFileClose(RTTARFILE hFile)
617{
618 /* Already closed? */
619 if (hFile == NIL_RTTARFILE)
620 return VINF_SUCCESS;
621
622 PRTTARFILEINTERNAL pFileInt = hFile;
623 RTTARFILE_VALID_RETURN(pFileInt);
624
625 int rc = VINF_SUCCESS;
626
627 /* In write mode: */
628 if ((pFileInt->fOpenMode & (RTFILE_O_WRITE | RTFILE_O_READ)) == RTFILE_O_WRITE)
629 {
630 pFileInt->pTar->fFileOpenForWrite = false;
631 do
632 {
633 /* If the user has called RTTarFileSetSize in the meantime, we have
634 to make sure the file has the right size. */
635 if (pFileInt->cbSetSize > pFileInt->cbSize)
636 {
637 rc = rtTarAppendZeros(pFileInt, pFileInt->cbSetSize - pFileInt->cbSize);
638 if (RT_FAILURE(rc))
639 break;
640 }
641
642 /* If the written size isn't 512 byte aligned, we need to fix this. */
643 RTTARRECORD record;
644 RT_ZERO(record);
645 uint64_t cbSizeAligned = RT_ALIGN(pFileInt->cbSize, sizeof(RTTARRECORD));
646 if (cbSizeAligned != pFileInt->cbSize)
647 {
648 /* Note the RTFile method. We didn't increase the cbSize or cbCurrentPos here. */
649 rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
650 pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize,
651 &record,
652 cbSizeAligned - pFileInt->cbSize,
653 NULL);
654 if (RT_FAILURE(rc))
655 break;
656 }
657
658 /* Create a header record for the file */
659 /* Todo: mode, gid, uid, mtime should be setable (or detected myself) */
660 RTTIMESPEC time;
661 RTTimeNow(&time);
662 rc = rtTarCreateHeaderRecord(&record, pFileInt->pszFilename, pFileInt->cbSize,
663 0, 0, 0600, RTTimeSpecGetSeconds(&time));
664 if (RT_FAILURE(rc))
665 break;
666
667 /* Write this at the start of the file data */
668 rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart, &record, sizeof(RTTARRECORD), NULL);
669 if (RT_FAILURE(rc))
670 break;
671 }
672 while (0);
673 }
674
675 /*
676 * Now cleanup and delete the handle.
677 */
678 if (pFileInt->pszFilename)
679 RTStrFree(pFileInt->pszFilename);
680 if (pFileInt->hVfsIos != NIL_RTVFSIOSTREAM)
681 {
682 RTVfsIoStrmRelease(pFileInt->hVfsIos);
683 pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
684 }
685 pFileInt->u32Magic = RTTARFILE_MAGIC_DEAD;
686 RTMemFree(pFileInt);
687
688 return rc;
689}
690
691
692RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
693{
694 PRTTARFILEINTERNAL pFileInt = hFile;
695 RTTARFILE_VALID_RETURN(pFileInt);
696
697 size_t cbTmpRead = 0;
698 int rc = RTVfsIoStrmReadAt(pFileInt->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, &cbTmpRead);
699 if (RT_SUCCESS(rc))
700 {
701 pFileInt->offCurrent = off + cbTmpRead;
702 if (pcbRead)
703 *pcbRead = cbTmpRead;
704 if (rc == VINF_EOF)
705 rc = pcbRead ? VINF_SUCCESS : VERR_EOF;
706 }
707 else if (pcbRead)
708 *pcbRead = 0;
709 return rc;
710}
711
712
713RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
714{
715 PRTTARFILEINTERNAL pFileInt = hFile;
716 RTTARFILE_VALID_RETURN(pFileInt);
717
718 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
719 return VERR_ACCESS_DENIED;
720
721 size_t cbTmpWritten = 0;
722 int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToWrite, &cbTmpWritten);
723 pFileInt->cbSize += cbTmpWritten;
724 pFileInt->offCurrent = off + cbTmpWritten;
725 if (pcbWritten)
726 *pcbWritten = cbTmpWritten;
727
728 return rc;
729}
730
731
732RTR3DECL(int) RTTarFileGetSize(RTTARFILE hFile, uint64_t *pcbSize)
733{
734 /* Validate input */
735 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
736
737 PRTTARFILEINTERNAL pFileInt = hFile;
738 RTTARFILE_VALID_RETURN(pFileInt);
739
740 *pcbSize = RT_MAX(pFileInt->cbSetSize, pFileInt->cbSize);
741
742 return VINF_SUCCESS;
743}
744
745
746RTR3DECL(int) RTTarFileSetSize(RTTARFILE hFile, uint64_t cbSize)
747{
748 PRTTARFILEINTERNAL pFileInt = hFile;
749 RTTARFILE_VALID_RETURN(pFileInt);
750
751 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
752 return VERR_WRITE_ERROR;
753
754 /** @todo If cbSize is smaller than pFileInt->cbSize we have to
755 * truncate the current file. */
756 pFileInt->cbSetSize = cbSize;
757
758 return VINF_SUCCESS;
759}
760
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette