1 | /* $Id: fileio-sg-generic.cpp 77209 2019-02-07 23:46:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, RTFileSgReadAt & RTFileSgWriteAt, generic.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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/file.h>
|
---|
33 |
|
---|
34 | #include <iprt/errcore.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Read bytes from a file at a given offset into a S/G buffer.
|
---|
39 | * This function may modify the file position.
|
---|
40 | *
|
---|
41 | * @returns iprt status code.
|
---|
42 | * @param hFile Handle to the file.
|
---|
43 | * @param off Where to read.
|
---|
44 | * @param pSgBuf Pointer to the S/G buffer to read into.
|
---|
45 | * @param cbToRead How much to read.
|
---|
46 | * @param pcbRead How much we actually read.
|
---|
47 | * If NULL an error will be returned for a partial read.
|
---|
48 | */
|
---|
49 | RTDECL(int) RTFileSgReadAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToRead, size_t *pcbRead)
|
---|
50 | {
|
---|
51 | int rc = VINF_SUCCESS;
|
---|
52 | size_t cbRead = 0;
|
---|
53 | while (cbToRead)
|
---|
54 | {
|
---|
55 | size_t cbBuf = cbToRead;
|
---|
56 | void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf);
|
---|
57 |
|
---|
58 | size_t cbThisRead = 0;
|
---|
59 | rc = RTFileReadAt(hFile, off, pvBuf, cbBuf, pcbRead ? &cbThisRead : NULL);
|
---|
60 | if (RT_SUCCESS(rc))
|
---|
61 | cbRead += cbThisRead;
|
---|
62 | else
|
---|
63 | break;
|
---|
64 | if ( cbThisRead < cbBuf
|
---|
65 | && pcbRead)
|
---|
66 | break;
|
---|
67 |
|
---|
68 | cbToRead -= cbBuf;
|
---|
69 | off += cbBuf;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (pcbRead)
|
---|
73 | *pcbRead = cbRead;
|
---|
74 |
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Write bytes from a S/G buffer to a file at a given offset.
|
---|
81 | * This function may modify the file position.
|
---|
82 | *
|
---|
83 | * @returns iprt status code.
|
---|
84 | * @param hFile Handle to the file.
|
---|
85 | * @param off Where to write.
|
---|
86 | * @param pSgBuf What to write.
|
---|
87 | * @param cbToWrite How much to write.
|
---|
88 | * @param pcbWritten How much we actually wrote.
|
---|
89 | * If NULL an error will be returned for a partial write.
|
---|
90 | */
|
---|
91 | RTDECL(int) RTFileSgWriteAt(RTFILE hFile, RTFOFF off, PRTSGBUF pSgBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
92 | {
|
---|
93 | int rc = VINF_SUCCESS;
|
---|
94 | size_t cbWritten = 0;
|
---|
95 | while (cbToWrite)
|
---|
96 | {
|
---|
97 | size_t cbBuf = cbToWrite;
|
---|
98 | void *pvBuf = RTSgBufGetNextSegment(pSgBuf, &cbBuf);
|
---|
99 |
|
---|
100 | size_t cbThisWritten = 0;
|
---|
101 | rc = RTFileWriteAt(hFile, off, pvBuf, cbBuf, pcbWritten ? &cbThisWritten : NULL);
|
---|
102 | if (RT_SUCCESS(rc))
|
---|
103 | cbWritten += cbThisWritten;
|
---|
104 | else
|
---|
105 | break;
|
---|
106 | if ( cbThisWritten < cbBuf
|
---|
107 | && pcbWritten)
|
---|
108 | break;
|
---|
109 |
|
---|
110 | cbToWrite -= cbBuf;
|
---|
111 | off += cbBuf;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (pcbWritten)
|
---|
115 | *pcbWritten = cbWritten;
|
---|
116 |
|
---|
117 | return rc;
|
---|
118 | }
|
---|
119 |
|
---|