Support for reading and writing 0 bytes to fdstore 45/38245/2 tizen
authorMateusz Malicki <m.malicki2@samsung.com>
Wed, 15 Apr 2015 09:26:09 +0000 (11:26 +0200)
committerMateusz Malicki <m.malicki2@samsung.com>
Wed, 15 Apr 2015 15:29:11 +0000 (17:29 +0200)
[Bug/Feature]   Zero bytes can't be read and write (it's needed by fdstore-visitor)
[Cause]         N/A
[Solution]      N/A
[Verification]  N/A

Change-Id: Ic9d3ed84a09797fc09a795a799b22fa66facd843

src/config/fdstore.cpp

index 7934893..c1cedfe 100644 (file)
@@ -101,20 +101,19 @@ void FDStore::write(const void* bufferPtr, const size_t size, const unsigned int
         int n  = ::write(mFD,
                          reinterpret_cast<const char*>(bufferPtr) + nTotal,
                          size - nTotal);
-        if (n > 0) {
+        if (n >= 0) {
             nTotal += n;
+            if (nTotal == size) {
+                // All data is written, break loop
+                break;
+            }
         } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
             // Neglected errors
         } else {
-            throw ConfigException("Error during reading: " + std::string(strerror(errno)));
+            throw ConfigException("Error during writing: " + std::string(strerror(errno)));
         }
 
-        if (nTotal >= size) {
-            // All data is written, break loop
-            break;
-        } else {
-            waitForEvent(mFD, POLLOUT, deadline);
-        }
+        waitForEvent(mFD, POLLOUT, deadline);
     }
 }
 
@@ -128,20 +127,22 @@ void FDStore::read(void* bufferPtr, const size_t size, const unsigned int timeou
         int n  = ::read(mFD,
                         reinterpret_cast<char*>(bufferPtr) + nTotal,
                         size - nTotal);
-        if (n > 0) {
+        if (n >= 0) {
             nTotal += n;
+            if (nTotal == size) {
+                // All data is read, break loop
+                break;
+            }
+            if (n == 0) {
+                throw ConfigException("Peer disconnected");
+            }
         } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
             // Neglected errors
         } else {
             throw ConfigException("Error during reading: " + std::string(strerror(errno)));
         }
 
-        if (nTotal >= size) {
-            // All data is read, break loop
-            break;
-        } else {
-            waitForEvent(mFD, POLLIN, deadline);
-        }
+        waitForEvent(mFD, POLLIN, deadline);
     }
 }
 } // namespace config