LCOV - code coverage report
Current view: directory - src/shared/gio - gio_pio.c (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 47 42 89.4 %
Date: 2012-02-16 Functions: 0 0 -

       1                 : /*
       2                 :  * Copyright (c) 2011 The Native Client Authors. All rights reserved.
       3                 :  * Use of this source code is governed by a BSD-style license that can be
       4                 :  * found in the LICENSE file.
       5                 :  */
       6                 : 
       7                 : #include <errno.h>
       8                 : 
       9                 : /*
      10                 :  * Native Client Generic I/O backend using seekless operations on
      11                 :  * POSIXesque file descriptors.  This is immune to whether the file
      12                 :  * descriptor is shared among multiple processes that might be moving
      13                 :  * its file offset independently.
      14                 :  */
      15                 : 
      16                 : #include "native_client/src/include/portability.h"
      17                 : #include "native_client/src/include/portability_io.h"
      18                 : #include "native_client/src/shared/gio/gio.h"
      19                 : 
      20                 : 
      21                 : static const struct GioVtbl kGioPioVtbl = {
      22                 :   GioPioRead,
      23                 :   GioPioWrite,
      24                 :   GioPioSeek,
      25                 :   GioPioFlush,
      26                 :   GioPioClose,
      27                 :   GioPioDtor,
      28                 : };
      29                 : 
      30                 : 
      31               5 : int GioPioCtor(struct GioPio *self, int fd) {
      32               5 :   self->fd = fd;
      33               5 :   self->pos = 0;
      34               5 :   self->base.vtbl = &kGioPioVtbl;
      35               5 :   return 1;
      36                 : }
      37                 : 
      38                 : #if NACL_WINDOWS
      39                 : static HANDLE GioPioWinSetup(struct GioPio *self, OVERLAPPED *ov) {
      40                 :   ov->Offset = self->pos;
      41                 :   ov->OffsetHigh = 0;
      42                 :   ov->hEvent = 0;
      43                 :   return (HANDLE) _get_osfhandle(self->fd);
      44                 : }
      45                 : #endif
      46                 : 
      47              19 : ssize_t GioPioRead(struct Gio *vself, void *buf, size_t count) {
      48              19 :   struct GioPio *self = (struct GioPio *) vself;
      49                 :   ssize_t ret;
      50                 : 
      51                 : #if NACL_WINDOWS
      52                 :   OVERLAPPED ov;
      53                 :   DWORD nread;
      54                 :   HANDLE fh = GioPioWinSetup(self, &ov);
      55                 :   if (ReadFile(fh, buf, (DWORD) count, &nread, &ov)) {
      56                 :     ret = nread;
      57                 :   } else if (GetLastError() == ERROR_HANDLE_EOF) {
      58                 :     ret = 0;
      59                 :   } else {
      60                 :     errno = EIO;
      61                 :     ret = -1;
      62                 :   }
      63                 : #else
      64              19 :   ret = pread(self->fd, buf, count, self->pos);
      65                 : #endif
      66                 : 
      67              19 :   if (ret > 0)
      68              17 :     self->pos += (off_t) ret;
      69                 : 
      70              19 :   return ret;
      71                 : }
      72                 : 
      73                 : 
      74               2 : ssize_t GioPioWrite(struct Gio *vself, const void *buf, size_t count) {
      75               2 :   struct GioPio *self = (struct GioPio *) vself;
      76                 :   ssize_t ret;
      77                 : 
      78                 : #if NACL_WINDOWS
      79                 :   OVERLAPPED ov;
      80                 :   DWORD nread;
      81                 :   HANDLE fh = GioPioWinSetup(self, &ov);
      82                 :   if (WriteFile(fh, buf, (DWORD) count, &nread, &ov)) {
      83                 :     ret = nread;
      84                 :   } else {
      85                 :     errno = EIO;
      86                 :     ret = -1;
      87                 :   }
      88                 : #else
      89               2 :   ret = pwrite(self->fd, buf, count, self->pos);
      90                 : #endif
      91                 : 
      92               2 :   if (ret > 0)
      93               2 :     self->pos += (off_t) ret;
      94                 : 
      95               2 :   return ret;
      96                 : }
      97                 : 
      98                 : 
      99              14 : off_t GioPioSeek(struct Gio *vself, off_t offset, int whence) {
     100              14 :   struct GioPio *self = (struct GioPio *) vself;
     101                 : 
     102              14 :   switch (whence) {
     103                 :     case SEEK_SET:
     104               5 :       break;
     105                 :     case SEEK_CUR:
     106               6 :       offset += self->pos;
     107               6 :       break;
     108                 :     case SEEK_END:
     109               3 :       offset = lseek(self->fd, offset, SEEK_END);
     110               3 :       if (offset < 0)
     111               0 :         return -1;
     112               3 :       break;
     113                 :     default:
     114               0 :       errno = EINVAL;
     115               0 :       return -1;
     116                 :   }
     117                 : 
     118              14 :   if (offset < 0) {
     119               2 :     errno = EINVAL;
     120               2 :     return -1;
     121                 :   }
     122                 : 
     123              12 :   self->pos = offset;
     124              12 :   return offset;
     125                 : }
     126                 : 
     127                 : 
     128               2 : int GioPioFlush(struct Gio *vself) {
     129                 :   UNREFERENCED_PARAMETER(vself);
     130               2 :   return 0;
     131                 : }
     132                 : 
     133                 : 
     134               5 : int GioPioClose(struct Gio *vself) {
     135               5 :   struct GioPio *self = (struct GioPio *) vself;
     136                 : 
     137               5 :   if (CLOSE(self->fd) == 0) {
     138               5 :     self->fd = -1;
     139               5 :     return 0;
     140                 :   }
     141                 : 
     142               0 :   return -1;
     143                 : }
     144                 : 
     145                 : 
     146               5 : void GioPioDtor(struct Gio *vself) {
     147               5 :   struct GioPio *self = (struct GioPio *) vself;
     148               5 :   if (-1 != self->fd) {
     149               0 :     (void) CLOSE(self->fd);
     150                 :   }
     151               5 : }

Generated by: LCOV version 1.7