LCOV - code coverage report
Current view: top level - util/io - fd.cc (source / functions) Hit Total Coverage
Test: report.info Lines: 20 23 87.0 %
Date: 2012-05-15 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 11 16 68.8 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
       2                 :            : //
       3                 :            : // Permission to use, copy, modify, and/or distribute this software for any
       4                 :            : // purpose with or without fee is hereby granted, provided that the above
       5                 :            : // copyright notice and this permission notice appear in all copies.
       6                 :            : //
       7                 :            : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
       8                 :            : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
       9                 :            : // AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
      10                 :            : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
      11                 :            : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
      12                 :            : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
      13                 :            : // PERFORMANCE OF THIS SOFTWARE.
      14                 :            : 
      15                 :            : #include "fd.h"
      16                 :            : 
      17                 :            : #include <unistd.h>
      18                 :            : #include <cerrno>
      19                 :            : 
      20                 :            : namespace isc {
      21                 :            : namespace util {
      22                 :            : namespace io {
      23                 :            : 
      24                 :            : bool
      25                 :        120 : write_data(const int fd, const void *buffer_v, const size_t length) {
      26                 :        120 :     const unsigned char* buffer(static_cast<const unsigned char*>(buffer_v));
      27                 :        120 :     size_t remaining = length;  // Amount remaining to be written
      28                 :            : 
      29                 :            :     // Just keep writing until all is written
      30         [ +  + ]:        240 :     while (remaining > 0) {
      31                 :        120 :         const int written = write(fd, buffer, remaining);
      32         [ +  + ]:        120 :         if (written == -1) {
      33         [ -  + ]:          2 :             if (errno == EINTR) { // Just keep going
      34                 :          0 :                 continue;
      35                 :            :             } else {
      36                 :            :                 return (false);
      37                 :            :             }
      38                 :            : 
      39         [ +  - ]:        118 :         } else if (written > 0) {
      40                 :            :             // Wrote "written" bytes from the buffer
      41                 :        118 :             remaining -= written;
      42                 :        118 :             buffer += written;
      43                 :            : 
      44                 :            :         } else {
      45                 :            :             // Wrote zero bytes from the buffer. We should not get here as any
      46                 :            :             // error that causes zero bytes to be written should have returned
      47                 :            :             // -1.  However, write(2) can return 0, and in this case we
      48                 :            :             // interpret it as an error.
      49                 :            :             return (false);
      50                 :            :         }
      51                 :            :     }
      52                 :            :     return (true);
      53                 :            : }
      54                 :            : 
      55                 :            : ssize_t
      56                 :        227 : read_data(const int fd, void *buffer_v, const size_t length) {
      57                 :        227 :     unsigned char* buffer(static_cast<unsigned char*>(buffer_v));
      58                 :        227 :     size_t remaining = length;   // Amount remaining to be read
      59                 :            : 
      60         [ +  + ]:        453 :     while (remaining > 0) {
      61                 :        234 :         const int amount = read(fd, buffer, remaining);
      62         [ -  + ]:        234 :         if (amount == -1) {
      63         [ #  # ]:          0 :             if (errno == EINTR) { // Continue on interrupted call
      64                 :          0 :                 continue;
      65                 :            :             } else {
      66                 :            :                 return (-1);
      67                 :            :             }
      68         [ +  + ]:        234 :         } else if (amount > 0) {
      69                 :            :             // Read "amount" bytes into the buffer
      70                 :        226 :             remaining -= amount;
      71                 :        226 :             buffer += amount;
      72                 :            :         } else {
      73                 :            :             // EOF - end the read
      74                 :            :             break;
      75                 :            :         }
      76                 :            :     }
      77                 :            : 
      78                 :            :     // Return total number of bytes read
      79                 :        227 :     return (static_cast<ssize_t>(length - remaining));
      80                 :            : }
      81                 :            : 
      82                 :            : }
      83                 :            : }
      84                 :            : }

Generated by: LCOV version 1.9