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