Bump for 3.6-28
[LibreOffice.git] / sal / osl / unx / readwrite_helper.c
blob0d8c9672daf83f76d9a1c15e88b3b3d20a110f8e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
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/
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.
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.
20 * Major Contributor(s):
22 * For minor contributions see the git repository.
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.
31 #include "readwrite_helper.h"
33 #include <osl/diagnose.h>
34 #include <system.h>
36 sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
38 sal_Int32 nToWrite = dataSize;
39 unsigned char* dataToWrite = data;
41 // Check for overflow as we convert a signed to an unsigned.
42 OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
43 while ( nToWrite ) {
44 sal_Int32 nWritten = write(fd, dataToWrite, nToWrite);
45 if ( nWritten < 0 ) {
46 if ( errno == EINTR )
47 continue;
49 return sal_False;
53 OSL_ASSERT(nWritten > 0);
54 nToWrite -= nWritten;
55 dataToWrite += nWritten;
58 return sal_True;
61 sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count )
63 sal_Int32 nToRead = count;
64 unsigned char* bufferForReading = buffer;
66 // Check for overflow as we convert a signed to an unsigned.
67 OSL_ASSERT(count == (sal_uInt32)nToRead);
68 while ( nToRead ) {
69 sal_Int32 nRead = read(fd, bufferForReading, nToRead);
70 if ( nRead < 0 ) {
71 // We were interrupted before reading, retry.
72 if (errno == EINTR)
73 continue;
75 return sal_False;
78 // If we reach the EOF, we consider this a partial transfer and thus
79 // an error.
80 if ( nRead == 0 )
81 return sal_False;
83 nToRead -= nRead;
84 bufferForReading += nRead;
87 return sal_True;
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */