Raw
1 #ifndef WRAPPER_H
2 #define WRAPPER_H
3
4 char *xstrdup(const char *str);
5 void *xmalloc(size_t size);
6 void *xmallocz(size_t size);
7 void *xmallocz_gently(size_t size);
8 void *xmemdupz(const void *data, size_t len);
9 char *xstrndup(const char *str, size_t len);
10 void *xrealloc(void *ptr, size_t size);
11 void *xcalloc(size_t nmemb, size_t size);
12 void xsetenv(const char *name, const char *value, int overwrite);
13 void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
14 const char *mmap_os_err(void);
15 void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
16 int xopen(const char *path, int flags, ...);
17 ssize_t xread(int fd, void *buf, size_t len);
18 ssize_t xwrite(int fd, const void *buf, size_t len);
19 ssize_t xwritev(int fd, struct iovec *iov, int iovcnt);
20 ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
21 int xdup(int fd);
22 FILE *xfopen(const char *path, const char *mode);
23 FILE *xfdopen(int fd, const char *mode);
24 int xmkstemp(char *temp_filename);
25 int xmkstemp_mode(char *temp_filename, int mode);
26 char *xgetcwd(void);
27 FILE *fopen_for_writing(const char *path);
28 FILE *fopen_or_warn(const char *path, const char *mode);
29
30 /*
31 * Like strncmp, but only return zero if s is NUL-terminated and exactly len
32 * characters long. If it is not, consider it greater than t.
33 */
34 int xstrncmpz(const char *s, const char *t, size_t len);
35
36 __attribute__((format (printf, 3, 4)))
37 int xsnprintf(char *dst, size_t max, const char *fmt, ...);
38
39 int xgethostname(char *buf, size_t len);
40
41 char *git_mkdtemp(char *pattern);
42
43 /* set default permissions by passing mode arguments to open(2) */
44 int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
45 int git_mkstemp_mode(char *pattern, int mode);
46
47 ssize_t read_in_full(int fd, void *buf, size_t count);
48 ssize_t write_in_full(int fd, const void *buf, size_t count);
49 ssize_t pread_in_full(int fd, void *buf, size_t count, off_t offset);
50
51 /*
52 * Try to write all iovecs. Returns -1 in case an error occurred with a proper
53 * errno set, the number of bytes written otherwise.
54 *
55 * Note that the iovec will be modified as a result of this call to adjust for
56 * partial writes!
57 */
58 ssize_t writev_in_full(int fd, struct iovec *iov, int iovcnt);
59
60 static inline ssize_t write_str_in_full(int fd, const char *str)
61 {
62 return write_in_full(fd, str, strlen(str));
63 }
64
65 /**
66 * Open (and truncate) the file at path, write the contents of buf to it,
67 * and close it. Dies if any errors are encountered.
68 */
69 void write_file_buf(const char *path, const char *buf, size_t len);
70
71 /**
72 * Like write_file_buf(), but format the contents into a buffer first.
73 * Additionally, write_file() will append a newline if one is not already
74 * present, making it convenient to write text files:
75 *
76 * write_file(path, "counter: %d", ctr);
77 */
78 __attribute__((format (printf, 2, 3)))
79 void write_file(const char *path, const char *fmt, ...);
80
81 /* Return 1 if the file does not exist, 0 otherwise. */
82 int is_missing_file(const char *filename);
83 /* Return 1 if the file is empty or does not exist, 0 otherwise. */
84 int is_empty_or_missing_file(const char *filename);
85
86 enum fsync_action {
87 FSYNC_WRITEOUT_ONLY,
88 FSYNC_HARDWARE_FLUSH
89 };
90
91 /*
92 * Issues an fsync against the specified file according to the specified mode.
93 *
94 * FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
95 * systems to flush the OS cache without issuing a flush command to the storage
96 * controller. If those interfaces are unavailable, the function fails with
97 * ENOSYS.
98 *
99 * FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
100 * changes are durable. It is not expected to fail.
101 */
102 int git_fsync(int fd, enum fsync_action action);
103
104 /*
105 * Preserves errno, prints a message, but gives no warning for ENOENT.
106 * Returns 0 on success, which includes trying to unlink an object that does
107 * not exist.
108 */
109 int unlink_or_warn(const char *path);
110 /*
111 * Tries to unlink file. Returns 0 if unlink succeeded
112 * or the file already didn't exist. Returns -1 and
113 * appends a message to err suitable for
114 * 'error("%s", err->buf)' on error.
115 */
116 int unlink_or_msg(const char *file, struct strbuf *err);
117 /*
118 * Preserves errno, prints a message, but gives no warning for ENOENT.
119 * Returns 0 on success, which includes trying to remove a directory that does
120 * not exist.
121 */
122 int rmdir_or_warn(const char *path);
123
124 /*
125 * Call access(2), but warn for any error except "missing file"
126 * (ENOENT or ENOTDIR).
127 */
128 #define ACCESS_EACCES_OK (1U << 0)
129 int access_or_warn(const char *path, int mode, unsigned flag);
130 int access_or_die(const char *path, int mode, unsigned flag);
131
132 /* Warn on an inaccessible file if errno indicates this is an error */
133 int warn_on_fopen_errors(const char *path);
134
135 /*
136 * Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
137 * may be racy. Do not use this as protection against an attacker who can
138 * simultaneously create paths.
139 */
140 int open_nofollow(const char *path, int flags);
141
142 void sleep_millisec(int millisec);
143
144 enum {
145 /*
146 * Accept insecure bytes, which some CSPRNG implementations may return
147 * in case the entropy pool has been exhausted.
148 */
149 CSPRNG_BYTES_INSECURE = (1 << 0),
150 };
151
152 /*
153 * Generate len bytes from the system cryptographically secure PRNG.
154 * Returns 0 on success and -1 on error, setting errno. The inability to
155 * satisfy the full request is an error. Accepts CSPRNG flags.
156 */
157 int csprng_bytes(void *buf, size_t len, unsigned flags);
158
159 /*
160 * Returns a random uint32_t, uniformly distributed across all possible
161 * values. Accepts CSPRNG flags.
162 */
163 uint32_t git_rand(unsigned flags);
164
165 /* Provide log2 of the given `size_t`. */
166 static inline unsigned log2u(uintmax_t sz)
167 {
168 unsigned l = 0;
169
170 /*
171 * Technically this isn't required, but it helps the compiler optimize
172 * this to a `bsr` instruction.
173 */
174 if (!sz)
175 return 0;
176
177 for (; sz; sz >>= 1)
178 l++;
179
180 return l - 1;
181 }
182
183 #endif /* WRAPPER_H */