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.hxx"
11 :
12 : #include <osl/diagnose.h>
13 : #include <system.hxx>
14 :
15 7523 : bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
16 : {
17 7523 : sal_Int32 nToWrite = dataSize;
18 7523 : unsigned char* dataToWrite = static_cast<unsigned char *>(data);
19 :
20 : // Check for overflow as we convert a signed to an unsigned.
21 : OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
22 22569 : while ( nToWrite ) {
23 7523 : sal_Int32 nWritten = write(fd, dataToWrite, nToWrite);
24 7523 : if ( nWritten < 0 ) {
25 0 : if ( errno == EINTR )
26 0 : continue;
27 :
28 0 : return false;
29 :
30 : }
31 :
32 : OSL_ASSERT(nWritten > 0);
33 7523 : nToWrite -= nWritten;
34 7523 : dataToWrite += nWritten;
35 : }
36 :
37 7523 : return true;
38 : }
39 :
40 0 : bool safeRead( int fd, void* buffer, sal_uInt32 count )
41 : {
42 0 : sal_Int32 nToRead = count;
43 0 : unsigned char* bufferForReading = static_cast<unsigned char *>(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 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 false;
61 :
62 0 : nToRead -= nRead;
63 0 : bufferForReading += nRead;
64 : }
65 :
66 0 : return true;
67 : }
68 :
69 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|