LCOV - code coverage report
Current view: top level - common - io_utils.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core r Lines: 23 29 79.3 %
Date: 2011-08-21 Functions: 3 3 100.0 %
Branches: 12 20 60.0 %

           Branch data     Line data    Source code
       1                 :            : /** @file io_utils.cc
       2                 :            :  * @brief Wrappers for low-level POSIX I/O routines.
       3                 :            :  */
       4                 :            : /* Copyright (C) 2006,2007,2008,2009,2011 Olly Betts
       5                 :            :  * Copyright (C) 2010 Richard Boulton
       6                 :            :  *
       7                 :            :  * This program is free software; you can redistribute it and/or modify
       8                 :            :  * it under the terms of the GNU General Public License as published by
       9                 :            :  * the Free Software Foundation; either version 2 of the License, or
      10                 :            :  * (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This program is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15                 :            :  * GNU General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU General Public License
      18                 :            :  * along with this program; if not, write to the Free Software
      19                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : 
      24                 :            : #include "io_utils.h"
      25                 :            : 
      26                 :            : #ifdef __WIN32__
      27                 :            : # include "msvc_posix_wrapper.h"
      28                 :            : #endif
      29                 :            : 
      30                 :            : #include "safeerrno.h"
      31                 :            : #include "safeunistd.h"
      32                 :            : 
      33                 :            : #include <string>
      34                 :            : 
      35                 :            : #include <xapian/error.h>
      36                 :            : 
      37                 :            : bool
      38                 :      15250 : io_unlink(const std::string & filename)
      39                 :            : {
      40                 :            : #ifdef __WIN32__
      41                 :            :     if (msvc_posix_unlink(filename.c_str()) == 0) {
      42                 :            : #else
      43         [ +  + ]:      15250 :     if (unlink(filename.c_str()) == 0) {
      44                 :            : #endif
      45                 :       1954 :         return true;
      46                 :            :     }
      47         [ -  + ]:      13296 :     if (errno != ENOENT) {
      48                 :          0 :         throw Xapian::DatabaseError(filename + ": delete failed", errno);
      49                 :            :     }
      50                 :      15250 :     return false;
      51                 :            : }
      52                 :            : 
      53                 :            : size_t
      54                 :      68299 : io_read(int fd, char * p, size_t n, size_t min)
      55                 :            : {
      56                 :      68299 :     size_t total = 0;
      57         [ +  + ]:     126652 :     while (n) {
      58                 :     124726 :         ssize_t c = read(fd, p, n);
      59         [ +  + ]:     124726 :         if (c <= 0) {
      60         [ +  - ]:      66373 :             if (c == 0) {
      61         [ +  - ]:      66373 :                 if (total >= min) break;
      62                 :          0 :                 throw Xapian::DatabaseError("Couldn't read enough (EOF)");
      63                 :            :             }
      64         [ #  # ]:          0 :             if (errno == EINTR) continue;
      65                 :          0 :             throw Xapian::DatabaseError("Error reading from file", errno);
      66                 :            :         }
      67                 :      58353 :         p += c;
      68                 :      58353 :         total += c;
      69                 :      58353 :         n -= c;
      70                 :            :     }
      71                 :      68299 :     return total;
      72                 :            : }
      73                 :            : 
      74                 :            : /** Write n bytes from block pointed to by p to file descriptor fd. */
      75                 :            : void
      76                 :      15356 : io_write(int fd, const char * p, size_t n)
      77                 :            : {
      78         [ +  + ]:      30712 :     while (n) {
      79                 :      15356 :         ssize_t c = write(fd, p, n);
      80         [ -  + ]:      15356 :         if (c < 0) {
      81         [ #  # ]:          0 :             if (errno == EINTR) continue;
      82                 :          0 :             throw Xapian::DatabaseError("Error writing to file", errno);
      83                 :            :         }
      84                 :      15356 :         p += c;
      85                 :      15356 :         n -= c;
      86                 :            :     }
      87                 :      15356 : }

Generated by: LCOV version 1.8