interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / wrapper.h

183 lines · 5936 bytes Raw Blame History
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 xwritev(int fd, struct iovec *iov, int iovcnt);
20ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
21int xdup(int fd);
22FILE *xfopen(const char *path, const char *mode);
23FILE *xfdopen(int fd, const char *mode);
24int xmkstemp(char *temp_filename);
25int xmkstemp_mode(char *temp_filename, int mode);
26char *xgetcwd(void);
27FILE *fopen_for_writing(const char *path);
28FILE *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 */
34int xstrncmpz(const char *s, const char *t, size_t len);
35
36__attribute__((format (printf, 3, 4)))
37int xsnprintf(char *dst, size_t max, const char *fmt, ...);
38
39int xgethostname(char *buf, size_t len);
40
41char *git_mkdtemp(char *pattern);
42
43/* set default permissions by passing mode arguments to open(2) */
44int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
45int git_mkstemp_mode(char *pattern, int mode);
46
47ssize_t read_in_full(int fd, void *buf, size_t count);
48ssize_t write_in_full(int fd, const void *buf, size_t count);
49ssize_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 */
58ssize_t writev_in_full(int fd, struct iovec *iov, int iovcnt);
59
60static 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 */
69void 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)))
79void write_file(const char *path, const char *fmt, ...);
80
81/* Return 1 if the file does not exist, 0 otherwise. */
82int is_missing_file(const char *filename);
83/* Return 1 if the file is empty or does not exist, 0 otherwise. */
84int is_empty_or_missing_file(const char *filename);
85
86enum 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 */
102int 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 */
109int 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 */
116int 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 */
122int 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)
129int access_or_warn(const char *path, int mode, unsigned flag);
130int access_or_die(const char *path, int mode, unsigned flag);
131
132/* Warn on an inaccessible file if errno indicates this is an error */
133int 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 */
140int open_nofollow(const char *path, int flags);
141
142void sleep_millisec(int millisec);
143
144enum {
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 */
157int 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 */
163uint32_t git_rand(unsigned flags);
164
165/* Provide log2 of the given `size_t`. */
166static 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 */