VirtualBox

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

Last change on this file since 76531 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.2 KB
Line 
1/* $Id: tar.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
2/** @file
3 * IPRT - Tar archive I/O.
4 */
5
6/*
7 * Copyright (C) 2009-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/tar.h>
33
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/err.h>
37#include <iprt/file.h>
38#include <iprt/mem.h>
39#include <iprt/path.h>
40#include <iprt/string.h>
41#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); NOREF(cRefs);
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); NOREF(cRefs);
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 pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
326 if (pFileInt->pszFilename)
327 return pFileInt;
328
329 RTMemFree(pFileInt);
330 return NULL;
331
332}
333
334
335RTR3DECL(int) RTTarFileOpen(RTTAR hTar, PRTTARFILE phFile, const char *pszFilename, uint32_t fOpen)
336{
337 /* Write only interface now. */
338 AssertReturn(fOpen & RTFILE_O_WRITE, VERR_INVALID_PARAMETER);
339
340 PRTTARINTERNAL pInt = hTar;
341 RTTAR_VALID_RETURN(pInt);
342
343 if (!pInt->hTarFile)
344 return VERR_INVALID_HANDLE;
345
346 if (fOpen & RTFILE_O_WRITE)
347 {
348 if (!(pInt->fOpenMode & RTFILE_O_WRITE))
349 return VERR_WRITE_PROTECT;
350 if (pInt->fFileOpenForWrite)
351 return VERR_TOO_MANY_OPEN_FILES;
352 }
353
354 int rc = VINF_SUCCESS;
355 if (!(fOpen & RTFILE_O_WRITE))
356 {
357 /*
358 * Rewind the stream if necessary.
359 */
360 if (!pInt->fFssAtStart)
361 {
362 if (pInt->hVfsFss != NIL_RTVFSFSSTREAM)
363 {
364 uint32_t cRefs = RTVfsFsStrmRelease(pInt->hVfsFss); Assert(cRefs != UINT32_MAX); NOREF(cRefs);
365 pInt->hVfsFss = NIL_RTVFSFSSTREAM;
366 }
367
368 if (pInt->hVfsFile == NIL_RTVFSFILE)
369 {
370 rc = RTVfsFileFromRTFile(pInt->hTarFile, RTFILE_O_READ, true /*fLeaveOpen*/, &pInt->hVfsFile);
371 if (RT_FAILURE(rc))
372 return rc;
373 }
374
375 RTVFSIOSTREAM hVfsIos = RTVfsFileToIoStream(pInt->hVfsFile);
376 rc = RTZipTarFsStreamFromIoStream(hVfsIos, 0 /*fFlags*/, &pInt->hVfsFss);
377 RTVfsIoStrmRelease(hVfsIos);
378 if (RT_FAILURE(rc))
379 return rc;
380 }
381
382 /*
383 * Search the file system stream.
384 */
385 pInt->fFssAtStart = false;
386 for (;;)
387 {
388 char *pszName;
389 RTVFSOBJTYPE enmType;
390 RTVFSOBJ hVfsObj;
391 rc = RTVfsFsStrmNext(pInt->hVfsFss, &pszName, &enmType, &hVfsObj);
392 if (rc == VERR_EOF)
393 return VERR_FILE_NOT_FOUND;
394 if (RT_FAILURE(rc))
395 return rc;
396
397 if (!RTStrCmp(pszName, pszFilename))
398 {
399 if (enmType == RTVFSOBJTYPE_FILE || enmType == RTVFSOBJTYPE_IO_STREAM)
400 rc = rtTarFileCreateHandleForReadOnly(pszName, RTVfsObjToIoStream(hVfsObj), fOpen, phFile);
401 else
402 {
403 rc = VERR_UNEXPECTED_FS_OBJ_TYPE;
404 RTStrFree(pszName);
405 }
406 RTVfsObjRelease(hVfsObj);
407 break;
408 }
409 RTStrFree(pszName);
410 RTVfsObjRelease(hVfsObj);
411 } /* Search loop. */
412 }
413 else
414 {
415 PRTTARFILEINTERNAL pFileInt = rtTarFileCreateForWrite(pInt, pszFilename, fOpen);
416 if (!pFileInt)
417 return VERR_NO_MEMORY;
418
419 pInt->fFileOpenForWrite = true;
420
421 /* If we are in write mode, we also in append mode. Add an dummy
422 * header at the end of the current file. It will be filled by the
423 * close operation. */
424 rc = RTFileSeek(pFileInt->pTar->hTarFile, 0, RTFILE_SEEK_END, &pFileInt->offStart);
425 if (RT_SUCCESS(rc))
426 {
427 RTTARRECORD record;
428 RT_ZERO(record);
429 rc = RTFileWrite(pFileInt->pTar->hTarFile, &record, sizeof(RTTARRECORD), NULL);
430 }
431
432 if (RT_SUCCESS(rc))
433 *phFile = (RTTARFILE)pFileInt;
434 else
435 {
436 /* Cleanup on failure */
437 if (pFileInt->pszFilename)
438 RTStrFree(pFileInt->pszFilename);
439 RTMemFree(pFileInt);
440 }
441 }
442
443 return rc;
444}
445
446
447static void rtTarSizeToRec(PRTTARRECORD pRecord, uint64_t cbSize)
448{
449 /*
450 * Small enough for the standard octal string encoding?
451 *
452 * Note! We could actually use the terminator character as well if we liked,
453 * but let not do that as it's easier to test this way.
454 */
455 if (cbSize < _4G * 2U)
456 RTStrPrintf(pRecord->h.size, sizeof(pRecord->h.size), "%0.11llo", cbSize);
457 else
458 {
459 /*
460 * Base 256 extension. Set the highest bit of the left most character.
461 * We don't deal with negatives here, cause the size have to be greater
462 * than zero.
463 *
464 * Note! The base-256 extension are never used by gtar or libarchive
465 * with the "ustar \0" format version, only the later
466 * "ustar\000" version. However, this shouldn't cause much
467 * trouble as they are not picky about what they read.
468 */
469 size_t cchField = sizeof(pRecord->h.size) - 1;
470 unsigned char *puchField = (unsigned char*)pRecord->h.size;
471 puchField[0] = 0x80;
472 do
473 {
474 puchField[cchField--] = cbSize & 0xff;
475 cbSize >>= 8;
476 } while (cchField);
477 }
478}
479
480
481static int rtTarCreateHeaderRecord(PRTTARRECORD pRecord, const char *pszSrcName, uint64_t cbSize,
482 RTUID uid, RTGID gid, RTFMODE fmode, int64_t mtime)
483{
484 /** @todo check for field overflows. */
485 /* Fill the header record */
486// RT_ZERO(pRecord); - done by the caller.
487 /** @todo use RTStrCopy */
488 size_t cb = RTStrPrintf(pRecord->h.name, sizeof(pRecord->h.name), "%s", pszSrcName);
489 if (cb < strlen(pszSrcName))
490 return VERR_BUFFER_OVERFLOW;
491 RTStrPrintf(pRecord->h.mode, sizeof(pRecord->h.mode), "%0.7o", fmode);
492 RTStrPrintf(pRecord->h.uid, sizeof(pRecord->h.uid), "%0.7o", uid);
493 RTStrPrintf(pRecord->h.gid, sizeof(pRecord->h.gid), "%0.7o", gid);
494 rtTarSizeToRec(pRecord, cbSize);
495 RTStrPrintf(pRecord->h.mtime, sizeof(pRecord->h.mtime), "%0.11llo", mtime);
496 RTStrPrintf(pRecord->h.magic, sizeof(pRecord->h.magic), "ustar ");
497 RTStrPrintf(pRecord->h.uname, sizeof(pRecord->h.uname), "someone");
498 RTStrPrintf(pRecord->h.gname, sizeof(pRecord->h.gname), "someone");
499 pRecord->h.linkflag = LF_NORMAL;
500
501 /* Create the checksum out of the new header */
502 int32_t iUnsignedChksum, iSignedChksum;
503 if (rtZipTarCalcChkSum((PCRTZIPTARHDR)pRecord, &iUnsignedChksum, &iSignedChksum))
504 return VERR_TAR_END_OF_FILE;
505
506 /* Format the checksum */
507 RTStrPrintf(pRecord->h.chksum, sizeof(pRecord->h.chksum), "%0.7o", iUnsignedChksum);
508
509 return VINF_SUCCESS;
510}
511
512
513DECLINLINE(void *) rtTarMemTmpAlloc(size_t *pcbSize)
514{
515 *pcbSize = 0;
516 /* Allocate a reasonably large buffer, fall back on a tiny one.
517 * Note: has to be 512 byte aligned and >= 512 byte. */
518 size_t cbTmp = _1M;
519 void *pvTmp = RTMemTmpAlloc(cbTmp);
520 if (!pvTmp)
521 {
522 cbTmp = sizeof(RTTARRECORD);
523 pvTmp = RTMemTmpAlloc(cbTmp);
524 }
525 *pcbSize = cbTmp;
526 return pvTmp;
527}
528
529
530static int rtTarAppendZeros(PRTTARFILEINTERNAL pFileInt, uint64_t cbSize)
531{
532 /* Allocate a temporary buffer for copying the tar content in blocks. */
533 size_t cbTmp = 0;
534 void *pvTmp = rtTarMemTmpAlloc(&cbTmp);
535 if (!pvTmp)
536 return VERR_NO_MEMORY;
537 RT_BZERO(pvTmp, cbTmp);
538
539 int rc = VINF_SUCCESS;
540 uint64_t cbAllWritten = 0;
541 size_t cbWritten = 0;
542 for (;;)
543 {
544 if (cbAllWritten >= cbSize)
545 break;
546 size_t cbToWrite = RT_MIN(cbSize - cbAllWritten, cbTmp);
547 rc = RTTarFileWriteAt(pFileInt, pFileInt->offCurrent, pvTmp, cbToWrite, &cbWritten);
548 if (RT_FAILURE(rc))
549 break;
550 cbAllWritten += cbWritten;
551 }
552
553 RTMemTmpFree(pvTmp);
554
555 return rc;
556}
557
558
559RTR3DECL(int) RTTarFileClose(RTTARFILE hFile)
560{
561 /* Already closed? */
562 if (hFile == NIL_RTTARFILE)
563 return VINF_SUCCESS;
564
565 PRTTARFILEINTERNAL pFileInt = hFile;
566 RTTARFILE_VALID_RETURN(pFileInt);
567
568 int rc = VINF_SUCCESS;
569
570 /* In write mode: */
571 if ((pFileInt->fOpenMode & (RTFILE_O_WRITE | RTFILE_O_READ)) == RTFILE_O_WRITE)
572 {
573 pFileInt->pTar->fFileOpenForWrite = false;
574 do
575 {
576 /* If the user has called RTTarFileSetSize in the meantime, we have
577 to make sure the file has the right size. */
578 if (pFileInt->cbSetSize > pFileInt->cbSize)
579 {
580 rc = rtTarAppendZeros(pFileInt, pFileInt->cbSetSize - pFileInt->cbSize);
581 if (RT_FAILURE(rc))
582 break;
583 }
584
585 /* If the written size isn't 512 byte aligned, we need to fix this. */
586 RTTARRECORD record;
587 RT_ZERO(record);
588 uint64_t cbSizeAligned = RT_ALIGN(pFileInt->cbSize, sizeof(RTTARRECORD));
589 if (cbSizeAligned != pFileInt->cbSize)
590 {
591 /* Note the RTFile method. We didn't increase the cbSize or cbCurrentPos here. */
592 rc = RTFileWriteAt(pFileInt->pTar->hTarFile,
593 pFileInt->offStart + sizeof(RTTARRECORD) + pFileInt->cbSize,
594 &record,
595 cbSizeAligned - pFileInt->cbSize,
596 NULL);
597 if (RT_FAILURE(rc))
598 break;
599 }
600
601 /* Create a header record for the file */
602 /** @todo mode, gid, uid, mtime should be setable (or detected myself) */
603 RTTIMESPEC time;
604 RTTimeNow(&time);
605 rc = rtTarCreateHeaderRecord(&record, pFileInt->pszFilename, pFileInt->cbSize,
606 0, 0, 0600, RTTimeSpecGetSeconds(&time));
607 if (RT_FAILURE(rc))
608 break;
609
610 /* Write this at the start of the file data */
611 rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart, &record, sizeof(RTTARRECORD), NULL);
612 if (RT_FAILURE(rc))
613 break;
614 }
615 while (0);
616 }
617
618 /*
619 * Now cleanup and delete the handle.
620 */
621 if (pFileInt->pszFilename)
622 RTStrFree(pFileInt->pszFilename);
623 if (pFileInt->hVfsIos != NIL_RTVFSIOSTREAM)
624 {
625 RTVfsIoStrmRelease(pFileInt->hVfsIos);
626 pFileInt->hVfsIos = NIL_RTVFSIOSTREAM;
627 }
628 pFileInt->u32Magic = RTTARFILE_MAGIC_DEAD;
629 RTMemFree(pFileInt);
630
631 return rc;
632}
633
634
635RTR3DECL(int) RTTarFileReadAt(RTTARFILE hFile, uint64_t off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
636{
637 PRTTARFILEINTERNAL pFileInt = hFile;
638 RTTARFILE_VALID_RETURN(pFileInt);
639
640 size_t cbTmpRead = 0;
641 int rc = RTVfsIoStrmReadAt(pFileInt->hVfsIos, off, pvBuf, cbToRead, true /*fBlocking*/, &cbTmpRead);
642 if (RT_SUCCESS(rc))
643 {
644 pFileInt->offCurrent = off + cbTmpRead;
645 if (pcbRead)
646 *pcbRead = cbTmpRead;
647 if (rc == VINF_EOF)
648 rc = pcbRead ? VINF_SUCCESS : VERR_EOF;
649 }
650 else if (pcbRead)
651 *pcbRead = 0;
652 return rc;
653}
654
655
656RTR3DECL(int) RTTarFileWriteAt(RTTARFILE hFile, uint64_t off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
657{
658 PRTTARFILEINTERNAL pFileInt = hFile;
659 RTTARFILE_VALID_RETURN(pFileInt);
660
661 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
662 return VERR_ACCESS_DENIED;
663
664 size_t cbTmpWritten = 0;
665 int rc = RTFileWriteAt(pFileInt->pTar->hTarFile, pFileInt->offStart + 512 + off, pvBuf, cbToWrite, &cbTmpWritten);
666 pFileInt->cbSize += cbTmpWritten;
667 pFileInt->offCurrent = off + cbTmpWritten;
668 if (pcbWritten)
669 *pcbWritten = cbTmpWritten;
670
671 return rc;
672}
673
674
675RTR3DECL(int) RTTarFileGetSize(RTTARFILE hFile, uint64_t *pcbSize)
676{
677 /* Validate input */
678 AssertPtrReturn(pcbSize, VERR_INVALID_POINTER);
679
680 PRTTARFILEINTERNAL pFileInt = hFile;
681 RTTARFILE_VALID_RETURN(pFileInt);
682
683 *pcbSize = RT_MAX(pFileInt->cbSetSize, pFileInt->cbSize);
684
685 return VINF_SUCCESS;
686}
687
688
689RTR3DECL(int) RTTarFileSetSize(RTTARFILE hFile, uint64_t cbSize)
690{
691 PRTTARFILEINTERNAL pFileInt = hFile;
692 RTTARFILE_VALID_RETURN(pFileInt);
693
694 if ((pFileInt->fOpenMode & RTFILE_O_WRITE) != RTFILE_O_WRITE)
695 return VERR_WRITE_ERROR;
696
697 /** @todo If cbSize is smaller than pFileInt->cbSize we have to
698 * truncate the current file. */
699 pFileInt->cbSetSize = cbSize;
700
701 return VINF_SUCCESS;
702}
703
Note: See TracBrowser for help on using the repository browser.

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