LCOV - code coverage report
Current view: top level - lib/fdio - fdio.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 17 17 100.0 %
Date: 2021-11-24 03:28:48 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2003-2004, Roger Dingledine
       2             :  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
       3             :  * Copyright (c) 2007-2021, The Tor Project, Inc. */
       4             : /* See LICENSE for licensing information */
       5             : 
       6             : /**
       7             :  * \file fdio.c
       8             :  *
       9             :  * \brief Low-level compatibility wrappers for fd-based IO.
      10             :  **/
      11             : 
      12             : #include "orconfig.h"
      13             : 
      14             : #ifdef HAVE_UNISTD_H
      15             : #include <unistd.h>
      16             : #endif
      17             : #ifdef _WIN32
      18             : #include <windows.h>
      19             : #endif
      20             : #ifdef HAVE_SYS_TYPES_H
      21             : #include <sys/types.h>
      22             : #endif
      23             : 
      24             : #include "lib/fdio/fdio.h"
      25             : #include "lib/cc/torint.h"
      26             : #include "lib/err/torerr.h"
      27             : 
      28             : #include <stdlib.h>
      29             : #include <stdio.h>
      30             : 
      31             : /* Some old versions of Unix didn't define constants for these values,
      32             :  * and instead expect you to say 0, 1, or 2. */
      33             : 
      34             : /** @cond */
      35             : #ifndef SEEK_SET
      36             : #define SEEK_SET 0
      37             : #endif
      38             : #ifndef SEEK_CUR
      39             : #define SEEK_CUR 1
      40             : #endif
      41             : #ifndef SEEK_END
      42             : #define SEEK_END 2
      43             : #endif
      44             : /** @endcond */
      45             : 
      46             : /** Return the position of <b>fd</b> with respect to the start of the file. */
      47             : off_t
      48          28 : tor_fd_getpos(int fd)
      49             : {
      50             : #ifdef _WIN32
      51             :   return (off_t) _lseeki64(fd, 0, SEEK_CUR);
      52             : #else
      53          28 :   return (off_t) lseek(fd, 0, SEEK_CUR);
      54             : #endif
      55             : }
      56             : 
      57             : /** Move <b>fd</b> to the end of the file. Return -1 on error, 0 on success.
      58             :  * If the file is a pipe, do nothing and succeed.
      59             :  **/
      60             : int
      61         123 : tor_fd_seekend(int fd)
      62             : {
      63             : #ifdef _WIN32
      64             :   return _lseeki64(fd, 0, SEEK_END) < 0 ? -1 : 0;
      65             : #else
      66         123 :   off_t rc = lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
      67             : #ifdef ESPIPE
      68             :   /* If we get an error and ESPIPE, then it's a pipe or a socket of a fifo:
      69             :    * no need to worry. */
      70             :   if (rc < 0 && errno == ESPIPE)
      71             :     rc = 0;
      72             : #endif /* defined(ESPIPE) */
      73         123 :   return (rc < 0) ? -1 : 0;
      74             : #endif /* defined(_WIN32) */
      75             : }
      76             : 
      77             : /** Move <b>fd</b> to position <b>pos</b> in the file. Return -1 on error, 0
      78             :  * on success. */
      79             : int
      80           1 : tor_fd_setpos(int fd, off_t pos)
      81             : {
      82             : #ifdef _WIN32
      83             :   return _lseeki64(fd, pos, SEEK_SET) < 0 ? -1 : 0;
      84             : #else
      85           1 :   return lseek(fd, pos, SEEK_SET) < 0 ? -1 : 0;
      86             : #endif
      87             : }
      88             : 
      89             : /** Replacement for ftruncate(fd, 0): move to the front of the file and remove
      90             :  * all the rest of the file. Return -1 on error, 0 on success. */
      91             : int
      92           1 : tor_ftruncate(int fd)
      93             : {
      94             :   /* Rumor has it that some versions of ftruncate do not move the file pointer.
      95             :    */
      96           1 :   if (tor_fd_setpos(fd, 0) < 0)
      97             :     return -1;
      98             : 
      99             : #ifdef _WIN32
     100             :   return _chsize(fd, 0);
     101             : #else
     102           1 :   return ftruncate(fd, 0);
     103             : #endif
     104             : }
     105             : 
     106             : /** Minimal version of write_all, for use by logging. */
     107             : int
     108        3436 : write_all_to_fd_minimal(int fd, const char *buf, size_t count)
     109             : {
     110        3436 :   size_t written = 0;
     111        3436 :   raw_assert(count < SSIZE_MAX);
     112             : 
     113        6872 :   while (written < count) {
     114        3436 :     ssize_t result = write(fd, buf+written, count-written);
     115        3436 :     if (result<0)
     116             :       return -1;
     117        3436 :     written += result;
     118             :   }
     119             :   return 0;
     120             : }

Generated by: LCOV version 1.14