Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : */
9 :
10 : #include "readwrite_helper.h"
11 :
12 : #include <osl/diagnose.h>
13 : #include <system.h>
14 :
15 0 : sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
16 : {
17 0 : sal_Int32 nToWrite = dataSize;
18 0 : unsigned char* dataToWrite = data;
19 :
20 : // Check for overflow as we convert a signed to an unsigned.
21 : OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
22 0 : while ( nToWrite ) {
23 0 : sal_Int32 nWritten = write(fd, dataToWrite, nToWrite);
24 0 : if ( nWritten < 0 ) {
25 0 : if ( errno == EINTR )
26 0 : continue;
27 :
28 0 : return sal_False;
29 :
30 : }
31 :
32 : OSL_ASSERT(nWritten > 0);
33 0 : nToWrite -= nWritten;
34 0 : dataToWrite += nWritten;
35 : }
36 :
37 0 : return sal_True;
38 : }
39 :
40 0 : sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count )
41 : {
42 0 : sal_Int32 nToRead = count;
43 0 : unsigned char* bufferForReading = buffer;
44 :
45 : // Check for overflow as we convert a signed to an unsigned.
46 : OSL_ASSERT(count == (sal_uInt32)nToRead);
47 0 : while ( nToRead ) {
48 0 : sal_Int32 nRead = read(fd, bufferForReading, nToRead);
49 0 : if ( nRead < 0 ) {
50 : // We were interrupted before reading, retry.
51 0 : if (errno == EINTR)
52 0 : continue;
53 :
54 0 : return sal_False;
55 : }
56 :
57 : // If we reach the EOF, we consider this a partial transfer and thus
58 : // an error.
59 0 : if ( nRead == 0 )
60 0 : return sal_False;
61 :
62 0 : nToRead -= nRead;
63 0 : bufferForReading += nRead;
64 : }
65 :
66 0 : return sal_True;
67 : }
68 :
69 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|