[Libreoffice-commits] .: 4 commits - sal/osl

Michael Meeks michael at kemper.freedesktop.org
Tue Apr 26 07:14:49 PDT 2011


 sal/osl/unx/file_misc.cxx      |   20 +++++-----
 sal/osl/unx/makefile.mk        |    6 ++-
 sal/osl/unx/process.c          |   28 +++------------
 sal/osl/unx/profile.c          |   10 -----
 sal/osl/unx/readwrite_helper.c |   76 +++++++++++++++++++++++++++++++++++++++++
 sal/osl/unx/readwrite_helper.h |   46 ++++++++++++++++++++++++
 6 files changed, 143 insertions(+), 43 deletions(-)

New commits:
commit ab692f0f53d14e0d030ac34ac44bb05862f18280
Author: Julien Chaffraix <julien.chaffraix at gmail.com>
Date:   Sun Apr 24 08:37:35 2011 -0700

    Migrated oslDoCopyFile to using the read/write helpers.

diff --git a/sal/osl/unx/file_misc.cxx b/sal/osl/unx/file_misc.cxx
index eb7b94a..51691d5 100644
--- a/sal/osl/unx/file_misc.cxx
+++ b/sal/osl/unx/file_misc.cxx
@@ -39,6 +39,7 @@
 #include "file_path_helper.hxx"
 #include "file_url.h"
 #include "uunxapi.hxx"
+#include "readwrite_helper.h"
 
 #include <sys/types.h>
 #include <errno.h>
@@ -1010,7 +1011,6 @@ static int oslDoCopyFile(const sal_Char* pszSourceFileName, const sal_Char* pszD
         return nRet;
     }
 
-    size_t nWritten = 0;
     size_t nRemains = nSourceSize;
    
     if ( nRemains )
@@ -1021,18 +1021,18 @@ static int oslDoCopyFile(const sal_Char* pszSourceFileName, const sal_Char* pszD
 
         do
         {
-            nRead = 0;
-            nWritten = 0;
-
             size_t nToRead = std::min( sizeof(pBuffer), nRemains );
-            nRead = read( SourceFileFD, pBuffer, nToRead );
-            if ( (size_t)-1 != nRead )
-                nWritten = write( DestFileFD, pBuffer, nRead );
+            sal_Bool succeeded = safeRead( SourceFileFD, pBuffer, nToRead );
+            if ( !succeeded )
+                break;
+
+            succeeded = safeWrite( DestFileFD, pBuffer, nToRead );
+            if ( !succeeded )
+                break;
 
-            if ( (size_t)-1 != nWritten )
-                nRemains -= nWritten;
+            nRemains -= nToRead;
         }
-        while( nRemains && (size_t)-1 != nRead && nRead == nWritten );
+        while( nRemains );
     }
 
     if ( nRemains )
diff --git a/sal/osl/unx/readwrite_helper.h b/sal/osl/unx/readwrite_helper.h
index 494c886..c96c4e3 100644
--- a/sal/osl/unx/readwrite_helper.h
+++ b/sal/osl/unx/readwrite_helper.h
@@ -29,9 +29,18 @@
 
 #include <sal/types.h>
 
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
 sal_Bool safeWrite( int fd, void* data, sal_uInt32 dataSize );
 
 // This function *will* read |count| bytes from |fd|, busy looping
 // if needed. Don't use it when you don't know if you can request enough
 // data. It will return sal_False for any partial transfer or error.
 sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count );
+
+#ifdef __cplusplus
+}
+#endif
commit 71a8ef19e9d2006875bf939e19cb472923404d78
Author: Julien Chaffraix <julien.chaffraix at gmail.com>
Date:   Sat Apr 23 07:53:54 2011 -0700

    Introduced safeRead.
    
    Same function as safeWrite. Converted some methods in process.c to use it.
    This usually simplifies the code and add better error checking.

diff --git a/sal/osl/unx/process.c b/sal/osl/unx/process.c
index 41848b9..bc868ed 100644
--- a/sal/osl/unx/process.c
+++ b/sal/osl/unx/process.c
@@ -283,15 +283,15 @@ static sal_Bool sendFdPipe(int PipeFD, int SocketFD)
         OSL_TRACE("sendFdPipe : sending failed (%s)",strerror(errno));
     }
 
-    nSend=read(PipeFD,&RetCode,sizeof(RetCode));
+    bRet = safeRead(PipeFD, &RetCode, sizeof(RetCode));
 
-    if ( nSend > 0 && RetCode == 1 )
+    if ( bRet && RetCode == 1 )
     {
         OSL_TRACE("sendFdPipe : resource was received\n");
     }
     else
     {
-        OSL_TRACE("sendFdPipe : resource wasn't received\n");        
+        OSL_TRACE("sendFdPipe : resource wasn't received (error %s)\n", strerror(errno));
     }    
     
 #if defined(IOCHANNEL_TRANSFER_BSD_RENO)
@@ -1168,7 +1168,7 @@ sal_Bool osl_getProcStat(pid_t pid, struct osl_procStat* procstat)
         char* tmp=0;
         char prstatbuf[512];
         memset(prstatbuf,0,512);
-        bRet = read(fd,prstatbuf,511) == 511;
+        bRet = safeRead(fd, prstatbuf, 511);
 
         close(fd);
         /*printf("%s\n\n",prstatbuf);*/
@@ -1222,7 +1222,7 @@ sal_Bool osl_getProcStatus(pid_t pid, struct osl_procStat* procstat)
         char* tmp=0;
         char prstatusbuf[512];
         memset(prstatusbuf,0,512);
-        bRet = read(fd,prstatusbuf,511) == 511;
+        bRet = safeRead(fd, prstatusbuf, 511);
 
         close(fd);
 
diff --git a/sal/osl/unx/readwrite_helper.c b/sal/osl/unx/readwrite_helper.c
index 4777d08..41aa41e 100644
--- a/sal/osl/unx/readwrite_helper.c
+++ b/sal/osl/unx/readwrite_helper.c
@@ -48,3 +48,29 @@ sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
 
     return sal_True;
 }
+
+sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count )
+{
+    sal_Int32 nToRead = count;
+    // Check for overflow as we convert a signed to an unsigned.
+    OSL_ASSERT(count == (sal_uInt32)nToRead);
+    while ( nToRead ) {
+        sal_Int32 nRead = read(fd, buffer, nToRead);
+        if ( nRead < 0 ) {
+            // We were interrupted before reading, retry.
+            if (errno == EINTR)
+                continue;
+
+            return sal_False;
+        }
+
+        // If we reach the EOF, we consider this a partial transfer and thus
+        // an error.
+        if ( nRead == 0 )
+            return sal_False;
+
+        nToRead -= nRead;
+    }
+
+    return sal_True;
+}
diff --git a/sal/osl/unx/readwrite_helper.h b/sal/osl/unx/readwrite_helper.h
index dd16f58..494c886 100644
--- a/sal/osl/unx/readwrite_helper.h
+++ b/sal/osl/unx/readwrite_helper.h
@@ -30,3 +30,8 @@
 #include <sal/types.h>
 
 sal_Bool safeWrite( int fd, void* data, sal_uInt32 dataSize );
+
+// This function *will* read |count| bytes from |fd|, busy looping
+// if needed. Don't use it when you don't know if you can request enough
+// data. It will return sal_False for any partial transfer or error.
+sal_Bool safeRead( int fd, void* buffer, sal_uInt32 count );
commit 7836b4ba0a69b8dfc2a602aa117046bee070277b
Author: Julien Chaffraix <julien.chaffraix at gmail.com>
Date:   Sat Apr 23 07:38:27 2011 -0700

    Switched a write to safeWrite in profile.c.
    
    Cleaned up some code related to a now unneeded assertion.

diff --git a/sal/osl/unx/profile.c b/sal/osl/unx/profile.c
index 7c7b04c..f6bdd40 100644
--- a/sal/osl/unx/profile.c
+++ b/sal/osl/unx/profile.c
@@ -437,7 +437,6 @@ sal_Bool SAL_CALL osl_flushProfile(oslProfile Profile)
 
 static sal_Bool writeProfileImpl(osl_TFile* pFile)
 {
-    int BytesWritten=0;
 #if OSL_DEBUG_LEVEL > 1
     unsigned int nLen=0;
 #endif
@@ -459,19 +458,12 @@ static sal_Bool writeProfileImpl(osl_TFile* pFile)
     OSL_ASSERT(nLen == (pFile->m_nWriteBufLen - pFile->m_nWriteBufFree));
 #endif
 
-    BytesWritten = write(pFile->m_Handle, pFile->m_pWriteBuf, pFile->m_nWriteBufLen - pFile->m_nWriteBufFree);
-
-    if ( BytesWritten <= 0 )
+    if ( !safeWrite(pFile->m_Handle, pFile->m_pWriteBuf, pFile->m_nWriteBufLen - pFile->m_nWriteBufFree) )
     {
         OSL_TRACE("write failed '%s'\n",strerror(errno));
         return (sal_False);
     }
 
-#if OSL_DEBUG_LEVEL > 1
-    OSL_ASSERT(
-        BytesWritten >= 0 && SAL_INT_CAST(unsigned int, BytesWritten) == nLen);
-#endif
-
     free(pFile->m_pWriteBuf);
     pFile->m_pWriteBuf=0;
     pFile->m_nWriteBufLen=0;
commit dc30a8c141e9b097331ee9848cca89713d92a9a9
Author: Julien Chaffraix <julien.chaffraix at gmail.com>
Date:   Sat Apr 23 07:26:44 2011 -0700

    Refactored the safeWrite method in a new file.
    
    This enables other files to use it, which will be added in a follow-up step.

diff --git a/sal/osl/unx/makefile.mk b/sal/osl/unx/makefile.mk
index 6fb6768..4a1c113 100644
--- a/sal/osl/unx/makefile.mk
+++ b/sal/osl/unx/makefile.mk
@@ -78,7 +78,8 @@ SLOFILES= \
             $(SLO)$/file_volume.obj \
             $(SLO)$/uunxapi.obj\
             $(SLO)$/process_impl.obj\
-            $(SLO)$/salinit.obj
+            $(SLO)$/salinit.obj \
+            $(SLO)$/readwrite_helper.obj
 
 OBJFILES=   $(OBJ)$/conditn.obj  \
             $(OBJ)$/diagnose.obj \
@@ -107,7 +108,8 @@ OBJFILES=   $(OBJ)$/conditn.obj  \
             $(OBJ)$/file_volume.obj \
             $(OBJ)$/uunxapi.obj\
             $(OBJ)$/process_impl.obj\
-            $(OBJ)$/salinit.obj
+            $(OBJ)$/salinit.obj \
+            $(OBJ)$/readwrite_helper.obj
             
 
 .IF "$(OS)"=="MACOSX"
diff --git a/sal/osl/unx/process.c b/sal/osl/unx/process.c
index aa7d3a8..41848b9 100644
--- a/sal/osl/unx/process.c
+++ b/sal/osl/unx/process.c
@@ -64,6 +64,7 @@
 #include <grp.h>
 
 #include "procimpl.h"
+#include "readwrite_helper.h"
 #include "sockimpl.h"
 #include "secimpl.h"
 
@@ -300,23 +301,6 @@ static sal_Bool sendFdPipe(int PipeFD, int SocketFD)
     return bRet;
 }
 
-static sal_Bool safeWrite(int socket, void* data, sal_uInt32 dataSize)
-{
-    sal_Int32 nToWrite = dataSize;
-    // Check for overflow as we convert a signed to an unsigned.
-    OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
-    while ( nToWrite ) {
-        sal_Int32 nWritten = write(socket, data, nToWrite);
-        if ( nWritten < 0 )
-            return sal_False;
-
-        OSL_ASSERT(nWritten > 0);
-        nToWrite -= nWritten;
-    }
-
-    return sal_True;
-}
-
 /**********************************************
  receiveFdPipe
  *********************************************/
diff --git a/sal/osl/unx/readwrite_helper.c b/sal/osl/unx/readwrite_helper.c
new file mode 100644
index 0000000..4777d08
--- /dev/null
+++ b/sal/osl/unx/readwrite_helper.c
@@ -0,0 +1,50 @@
+/*
+ * Version: MPL 1.1 / GPLv3+ / LGPLv3+
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License or as specified alternatively below. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Initial Developer of the Original Code is
+ *       Julien Chaffraix <julien.chaffraix at gmail.com>
+ * Portions created by the Initial Developer are Copyright (C) 2011 the
+ * Initial Developer. All Rights Reserved.
+ *
+ * Major Contributor(s):
+ *
+ * For minor contributions see the git repository.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+ * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+ * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+ * instead of those above.
+ */
+
+#include "readwrite_helper.h"
+
+#include <osl/diagnose.h>
+#include <system.h>
+
+sal_Bool safeWrite(int fd, void* data, sal_uInt32 dataSize)
+{
+    sal_Int32 nToWrite = dataSize;
+    // Check for overflow as we convert a signed to an unsigned.
+    OSL_ASSERT(dataSize == (sal_uInt32)nToWrite);
+    while ( nToWrite ) {
+        sal_Int32 nWritten = write(fd, data, nToWrite);
+        if ( nWritten < 0 )
+            return sal_False;
+
+        OSL_ASSERT(nWritten > 0);
+        nToWrite -= nWritten;
+    }
+
+    return sal_True;
+}
diff --git a/sal/osl/unx/readwrite_helper.h b/sal/osl/unx/readwrite_helper.h
new file mode 100644
index 0000000..dd16f58
--- /dev/null
+++ b/sal/osl/unx/readwrite_helper.h
@@ -0,0 +1,32 @@
+/*
+ * Version: MPL 1.1 / GPLv3+ / LGPLv3+
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License or as specified alternatively below. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Initial Developer of the Original Code is
+ *       Julien Chaffraix <julien.chaffraix at gmail.com>
+ * Portions created by the Initial Developer are Copyright (C) 2011 the
+ * Initial Developer. All Rights Reserved.
+ *
+ * Major Contributor(s):
+ *
+ * For minor contributions see the git repository.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+ * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+ * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+ * instead of those above.
+ */
+
+#include <sal/types.h>
+
+sal_Bool safeWrite( int fd, void* data, sal_uInt32 dataSize );


More information about the Libreoffice-commits mailing list