This repository has been archived by the owner on Sep 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
fs.h
205 lines (163 loc) · 5.92 KB
/
fs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in
* the LICENSE file in the root directory of this source tree. An
* additional grant of patent rights can be found in the PATENTS file
* in the same directory.
*
*/
#pragma once
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <sys/time.h>
#include <dirent.h>
#include <sys/file.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <poll.h>
#include "util.h"
// Commit a cleanup object to closing the given file descriptor.
void cleanup_commit_close_fd(struct cleanup* cl, int fd);
// Open a file. The returned file descriptor is owned by the
// current reslist.
int xopen(const char* pathname, int flags, mode_t mode);
int try_xopen(const char* pathname, int flags, mode_t mode);
// Close a file descriptor. Fail if FD is not an open file
// descriptor. Do not use to close file descriptors owned
// by reslists.
void xclose(int fd);
// Allocate a pipe. The file descriptors are owned by the
// current reslits.
void xpipe(int* read_end, int* write_end);
// Duplicate a file descriptor. The new file descriptor is owned by
// the current reslist.
int xdup(int fd);
int xdup3nc(int oldfd, int newfd, int flags);
// Open a file descriptor as a stream. The returned FILE object is
// owned by the current reslist. It does _not_ own FD. The caller
// must guarante that FD remains alive for however long the resulting
// FILE* is in use.
FILE* xfdopen(int fd, const char* mode);
// All file descriptors we allocate are configured to be
// close-on-exit. This routine allows FD to be inherited across exec.
void allow_inherit(int fd);
char* xreadlink(const char* path);
// A FDH (File Descriptor Handle) is a package of a file descriptor
// and a reslist that owns it. It allows us to allocate an file
// descriptor owned by a reslist and close that file descriptor before
// its owning reslist is destroyed.
struct fdh {
struct reslist* rl; // Owns both fd and fdh
int fd;
};
// Duplicate an existing FD as an FDH
struct fdh* fdh_dup(int fd);
// Deallocate an FDH, closing its file descriptor. FDH is invalid
// after this call.
void fdh_destroy(struct fdh* fdh);
enum blocking_mode { blocking, non_blocking };
enum blocking_mode fd_set_blocking_mode(int fd, enum blocking_mode mode);
void hack_reopen_tty(int fd);
// Read up to SZ bytes from FD into BUF. May return on short read
// even without EOF. Return zero to indicate EOF.
size_t xread(int fd, void* buf, size_t sz);
// Read SZ bytes from FD into BUF, retrying on EINTR.
// May return short read on EOF.
size_t read_all(int fd, void* buf, size_t sz);
// Write SZ bytes to FD, retrying on EINTR.
void write_all(int fd, const void* buf, size_t sz);
void write_all_v(int fd, const struct iovec* iov, int iovcnt);
#ifndef HAVE_DUP3
int dup3(int oldfd, int newfd, int flags);
#endif
#define XPPOLL_LINUX_SYSCALL 1
#define XPPOLL_KQUEUE 2
#define XPPOLL_SYSTEM 3
#define XPPOLL_STUPID_WRAPPER 4
#if defined(__linux__)
# define XPPOLL XPPOLL_LINUX_SYSCALL
#elif defined(HAVE_PPOLL) && !defined(__GLIBC__)
# define XPPOLL XPPOLL_SYSTEM
#elif defined(HAVE_KQUEUE)
# define XPPOLL XPPOLL_KQUEUE
#elif defined(HAVE_PPOLL) && defined(__GLIBC__)
# define XPPOLL XPPOLL_SYSTEM
# define XPPOLL_BROKEN 1
#else
# define XPPOLL XPPOLL_STUPID_WRAPPER
# define XPPOLL_BROKEN 1
#endif
// See ppoll(2) or ppoll(3). If XPPOLL_BROKEN is defined, this
// routine may not atomically set SIGMASK and begin waiting, so you'll
// need to arrange for some other wakeup mechanism to avoid racing
// against signal delivery.
int xppoll(struct pollfd *fds, nfds_t nfds,
const struct timespec *timeout_ts,
const sigset_t *sigmask);
int xpoll(struct pollfd* fds, nfds_t nfds, int timeout);
#ifndef HAVE_MKOSTEMP
int mkostemp(char *template, int flags);
#endif
int xnamed_tempfile(const char** name);
void replace_stdin_stdout_with_dev_null(void);
#ifndef NDEBUG
void assert_cloexec(int fd);
#else
# define assert_cloexec(_fd) ((void)(_fd))
#endif
int merge_O_CLOEXEC_into_fd_flags(int fd, int flags);
// See dirname(3)
char* xdirname(const char* path);
// See basename(3)
char* xbasename(const char* path);
// Try to make sure FD has SIZE bytes available total; if the
// filesystem or OS doesn't support preallocation, return false.
// Otherwise, return true on success or die on failure.
bool fallocate_if_supported(int fd, uint64_t size);
void xfsync(int fd);
void xftruncate(int fd, uint64_t size);
void xrename(const char* old, const char* new);
void hint_sequential_access(int fd);
void _fs_on_init(void);
void xputc(char c, FILE* out);
void xputs(const char* s, FILE* out);
void xflush(FILE* out);
void xfwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream);
__attribute__((format(printf,2,3)))
void xprintf(FILE* out, const char* fmt, ...);
const char* system_tempdir(void);
struct sha256_hash {
uint8_t digest[32];
};
struct sha256_hash sha256_fd(int fd);
void xrewindfd(int fd);
#ifdef HAVE_REALPATH
const char* xrealpath(const char* path);
#endif
const char* my_fb_adb_directory(void);
void unlink_cleanup(void* filename);
void xflock(int fd, int operation);
// Read FD to EOF, returning a pointer to the bytes we read, which we
// NUL-terminate. (Of course, the string will appear to terminate
// early if we read a NUL byte from the FD.)
char* slurp_fd(int fd, size_t* nr_bytes_read_out);
struct growable_buffer slurp_fd_buf(int fd);
// Read a line into a heap-allocated and NUL-terminated buffer.
// The line terminator, if one was present, is included in the
// returned string. On EOF, return NULL.
char* slurp_line(FILE* file, size_t* nr_bytes_read_out);
struct stat xfstat(int fd);
struct stat xstat(const char* path);
int xF_GETFL(int fd);
void xF_SETFL(int fd, int flags);
DIR* xopendir(const char* path);