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