Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * Julien Chaffraix <julien.chaffraix@gmail.com>
17 : * Portions created by the Initial Developer are Copyright (C) 2011 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Major Contributor(s):
21 : *
22 : * For minor contributions see the git repository.
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
26 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
27 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
28 : * instead of those above.
29 : */
30 :
31 : #include "readwrite_helper.h"
32 :
33 : #include <osl/diagnose.h>
34 : #include <system.h>
35 :
36 399 : sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
37 : {
38 399 : sal_Int32 nToWrite = dataSize;
39 399 : unsigned char* dataToWrite = data;
40 :
41 : // Check for overflow as we convert a signed to an unsigned.
42 : OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
43 1197 : while ( nToWrite ) {
44 399 : sal_Int32 nWritten = write(fd, dataToWrite, nToWrite);
45 399 : if ( nWritten < 0 ) {
46 0 : if ( errno == EINTR )
47 0 : continue;
48 :
49 0 : return sal_False;
50 :
51 : }
52 :
53 : OSL_ASSERT(nWritten > 0);
54 399 : nToWrite -= nWritten;
55 399 : dataToWrite += nWritten;
56 : }
57 :
58 399 : return sal_True;
59 : }
60 :
61 0 : sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count )
62 : {
63 0 : sal_Int32 nToRead = count;
64 0 : unsigned char* bufferForReading = buffer;
65 :
66 : // Check for overflow as we convert a signed to an unsigned.
67 : OSL_ASSERT(count == (sal_uInt32)nToRead);
68 0 : while ( nToRead ) {
69 0 : sal_Int32 nRead = read(fd, bufferForReading, nToRead);
70 0 : if ( nRead < 0 ) {
71 : // We were interrupted before reading, retry.
72 0 : if (errno == EINTR)
73 0 : continue;
74 :
75 0 : return sal_False;
76 : }
77 :
78 : // If we reach the EOF, we consider this a partial transfer and thus
79 : // an error.
80 0 : if ( nRead == 0 )
81 0 : return sal_False;
82 :
83 0 : nToRead -= nRead;
84 0 : bufferForReading += nRead;
85 : }
86 :
87 0 : return sal_True;
88 : }
89 :
90 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|