fdio.c 2.55 KB
Newer Older
1
2
/* Copyright (c) 2003-2004, Roger Dingledine
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
Nick Mathewson's avatar
Nick Mathewson committed
3
 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4
5
/* See LICENSE for licensing information */

6
7
8
9
10
11
/**
 * \file fdio.c
 *
 * \brief Low-level compatibility wrappers for fd-based IO.
 **/

12
13
14
15
16
17
18
19
#include "orconfig.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
Nick Mathewson's avatar
Nick Mathewson committed
20
21
22
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
23
24
25
26
27
28

#include "lib/fdio/fdio.h"
#include "lib/cc/torint.h"
#include "lib/err/torerr.h"

#include <stdlib.h>
Nick Mathewson's avatar
Nick Mathewson committed
29
#include <stdio.h>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

/** @{ */
/** Some old versions of Unix didn't define constants for these values,
 * and instead expect you to say 0, 1, or 2. */
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1
#endif
#ifndef SEEK_END
#define SEEK_END 2
#endif
/** @} */

/** Return the position of <b>fd</b> with respect to the start of the file. */
off_t
tor_fd_getpos(int fd)
{
#ifdef _WIN32
Nick Mathewson's avatar
Nick Mathewson committed
50
  return (off_t) _lseeki64(fd, 0, SEEK_CUR);
51
52
53
54
55
56
57
58
59
60
61
62
#else
  return (off_t) lseek(fd, 0, SEEK_CUR);
#endif
}

/** Move <b>fd</b> to the end of the file. Return -1 on error, 0 on success.
 * If the file is a pipe, do nothing and succeed.
 **/
int
tor_fd_seekend(int fd)
{
#ifdef _WIN32
Nick Mathewson's avatar
Nick Mathewson committed
63
  return _lseeki64(fd, 0, SEEK_END) < 0 ? -1 : 0;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#else
  off_t rc = lseek(fd, 0, SEEK_END) < 0 ? -1 : 0;
#ifdef ESPIPE
  /* If we get an error and ESPIPE, then it's a pipe or a socket of a fifo:
   * no need to worry. */
  if (rc < 0 && errno == ESPIPE)
    rc = 0;
#endif /* defined(ESPIPE) */
  return (rc < 0) ? -1 : 0;
#endif /* defined(_WIN32) */
}

/** Move <b>fd</b> to position <b>pos</b> in the file. Return -1 on error, 0
 * on success. */
int
tor_fd_setpos(int fd, off_t pos)
{
#ifdef _WIN32
Nick Mathewson's avatar
Nick Mathewson committed
82
  return _lseeki64(fd, pos, SEEK_SET) < 0 ? -1 : 0;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#else
  return lseek(fd, pos, SEEK_SET) < 0 ? -1 : 0;
#endif
}

/** Replacement for ftruncate(fd, 0): move to the front of the file and remove
 * all the rest of the file. Return -1 on error, 0 on success. */
int
tor_ftruncate(int fd)
{
  /* Rumor has it that some versions of ftruncate do not move the file pointer.
   */
  if (tor_fd_setpos(fd, 0) < 0)
    return -1;

#ifdef _WIN32
  return _chsize(fd, 0);
#else
  return ftruncate(fd, 0);
#endif
}

/** Minimal version of write_all, for use by logging. */
int
107
write_all_to_fd_minimal(int fd, const char *buf, size_t count)
108
109
110
111
112
113
114
115
116
117
118
119
{
  size_t written = 0;
  raw_assert(count < SSIZE_MAX);

  while (written < count) {
    ssize_t result = write(fd, buf+written, count-written);
    if (result<0)
      return -1;
    written += result;
  }
  return 0;
}