Tor 0.4.9.0-alpha-dev
fdio.c
Go to the documentation of this file.
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. */
47off_t
49{
50#ifdef _WIN32
51 return (off_t) _lseeki64(fd, 0, SEEK_CUR);
52#else
53 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 **/
60int
62{
63#ifdef _WIN32
64 return _lseeki64(fd, 0, SEEK_END) < 0 ? -1 : 0;
65#else
66 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 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. */
79int
80tor_fd_setpos(int fd, off_t pos)
81{
82#ifdef _WIN32
83 return _lseeki64(fd, pos, SEEK_SET) < 0 ? -1 : 0;
84#else
85 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. */
91int
93{
94 /* Rumor has it that some versions of ftruncate do not move the file pointer.
95 */
96 if (tor_fd_setpos(fd, 0) < 0)
97 return -1;
98
99#ifdef _WIN32
100 return _chsize(fd, 0);
101#else
102 return ftruncate(fd, 0);
103#endif
104}
105
106/** Minimal version of write_all, for use by logging. */
107int
108write_all_to_fd_minimal(int fd, const char *buf, size_t count)
109{
110 size_t written = 0;
111 raw_assert(count < SSIZE_MAX);
112
113 while (written < count) {
114 ssize_t result = write(fd, buf+written, count-written);
115 if (result<0)
116 return -1;
117 written += result;
118 }
119 return 0;
120}
int tor_fd_seekend(int fd)
Definition: fdio.c:61
off_t tor_fd_getpos(int fd)
Definition: fdio.c:48
int write_all_to_fd_minimal(int fd, const char *buf, size_t count)
Definition: fdio.c:108
int tor_ftruncate(int fd)
Definition: fdio.c:92
int tor_fd_setpos(int fd, off_t pos)
Definition: fdio.c:80
Header for fdio.c.
Headers for torerr.c.
Integer definitions used throughout Tor.