1 | /* $Id: fileio-at-posix.cpp 77235 2019-02-09 18:30:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File I/O, RTFileReadAt and RTFileWriteAt, posix.
|
---|
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 <errno.h>
|
---|
32 | #include <sys/types.h>
|
---|
33 | #include <unistd.h>
|
---|
34 |
|
---|
35 | #include "internal/iprt.h"
|
---|
36 | #include <iprt/file.h>
|
---|
37 |
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/log.h>
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | RTDECL(int) RTFileReadAt(RTFILE hFile, RTFOFF off, void *pvBuf, size_t cbToRead, size_t *pcbRead)
|
---|
44 | {
|
---|
45 | ssize_t cbRead = pread(RTFileToNative(hFile), pvBuf, cbToRead, off);
|
---|
46 | if (cbRead >= 0)
|
---|
47 | {
|
---|
48 | if (pcbRead)
|
---|
49 | /* caller can handle partial read. */
|
---|
50 | *pcbRead = cbRead;
|
---|
51 | else
|
---|
52 | {
|
---|
53 | /* Caller expects all to be read. */
|
---|
54 | while ((ssize_t)cbToRead > cbRead)
|
---|
55 | {
|
---|
56 | ssize_t cbReadPart = pread(RTFileToNative(hFile), (char*)pvBuf + cbRead, cbToRead - cbRead, off + cbRead);
|
---|
57 | if (cbReadPart <= 0)
|
---|
58 | {
|
---|
59 | if (cbReadPart == 0)
|
---|
60 | return VERR_EOF;
|
---|
61 | return RTErrConvertFromErrno(errno);
|
---|
62 | }
|
---|
63 | cbRead += cbReadPart;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | return VINF_SUCCESS;
|
---|
67 | }
|
---|
68 |
|
---|
69 | return RTErrConvertFromErrno(errno);
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | RTDECL(int) RTFileWriteAt(RTFILE hFile, RTFOFF off, const void *pvBuf, size_t cbToWrite, size_t *pcbWritten)
|
---|
74 | {
|
---|
75 | ssize_t cbWritten = pwrite(RTFileToNative(hFile), pvBuf, cbToWrite, off);
|
---|
76 | if (cbWritten >= 0)
|
---|
77 | {
|
---|
78 | if (pcbWritten)
|
---|
79 | /* caller can handle partial write. */
|
---|
80 | *pcbWritten = cbWritten;
|
---|
81 | else
|
---|
82 | {
|
---|
83 | /* Caller expects all to be write. */
|
---|
84 | while ((ssize_t)cbToWrite > cbWritten)
|
---|
85 | {
|
---|
86 | ssize_t cbWrittenPart = pwrite(RTFileToNative(hFile), (const char *)pvBuf + cbWritten, cbToWrite - cbWritten,
|
---|
87 | off + cbWritten);
|
---|
88 | if (cbWrittenPart < 0)
|
---|
89 | return cbWrittenPart < 0 ? RTErrConvertFromErrno(errno) : VERR_TRY_AGAIN;
|
---|
90 | cbWritten += cbWrittenPart;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return VINF_SUCCESS;
|
---|
94 | }
|
---|
95 | return RTErrConvertFromErrno(errno);
|
---|
96 | }
|
---|
97 |
|
---|