Skip to content

Commit 382f694

Browse files
calvin-wan-googlegitster
authored andcommitted
git-compat-util: move wrapper.c funcs to its header
Since the functions in wrapper.c are widely used across the codebase, include it by default in git-compat-util.h. A future patch will remove now unnecessary inclusions of wrapper.h from other files. Signed-off-by: Calvin Wan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fda5d95 commit 382f694

File tree

2 files changed

+112
-111
lines changed

2 files changed

+112
-111
lines changed

git-compat-util.h

Lines changed: 1 addition & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ static inline int git_has_dir_sep(const char *path)
625625

626626
#include "compat/bswap.h"
627627

628-
struct strbuf;
628+
#include "wrapper.h"
629629

630630
/* General helper functions */
631631
NORETURN void usage(const char *err);
@@ -1045,36 +1045,6 @@ static inline int cast_size_t_to_int(size_t a)
10451045
# define xalloca(size) (xmalloc(size))
10461046
# define xalloca_free(p) (free(p))
10471047
#endif
1048-
char *xstrdup(const char *str);
1049-
void *xmalloc(size_t size);
1050-
void *xmallocz(size_t size);
1051-
void *xmallocz_gently(size_t size);
1052-
void *xmemdupz(const void *data, size_t len);
1053-
char *xstrndup(const char *str, size_t len);
1054-
void *xrealloc(void *ptr, size_t size);
1055-
void *xcalloc(size_t nmemb, size_t size);
1056-
void xsetenv(const char *name, const char *value, int overwrite);
1057-
void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
1058-
const char *mmap_os_err(void);
1059-
void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
1060-
int xopen(const char *path, int flags, ...);
1061-
ssize_t xread(int fd, void *buf, size_t len);
1062-
ssize_t xwrite(int fd, const void *buf, size_t len);
1063-
ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
1064-
int xdup(int fd);
1065-
FILE *xfopen(const char *path, const char *mode);
1066-
FILE *xfdopen(int fd, const char *mode);
1067-
int xmkstemp(char *temp_filename);
1068-
int xmkstemp_mode(char *temp_filename, int mode);
1069-
char *xgetcwd(void);
1070-
FILE *fopen_for_writing(const char *path);
1071-
FILE *fopen_or_warn(const char *path, const char *mode);
1072-
1073-
/*
1074-
* Like strncmp, but only return zero if s is NUL-terminated and exactly len
1075-
* characters long. If it is not, consider it greater than t.
1076-
*/
1077-
int xstrncmpz(const char *s, const char *t, size_t len);
10781048

10791049
/*
10801050
* FREE_AND_NULL(ptr) is like free(ptr) followed by ptr = NULL. Note
@@ -1176,15 +1146,10 @@ static inline size_t xsize_t(off_t len)
11761146
return (size_t) len;
11771147
}
11781148

1179-
__attribute__((format (printf, 3, 4)))
1180-
int xsnprintf(char *dst, size_t max, const char *fmt, ...);
1181-
11821149
#ifndef HOST_NAME_MAX
11831150
#define HOST_NAME_MAX 256
11841151
#endif
11851152

1186-
int xgethostname(char *buf, size_t len);
1187-
11881153
/* in ctype.c, for kwset users */
11891154
extern const unsigned char tolower_trans_tbl[256];
11901155

@@ -1425,72 +1390,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
14251390
#endif
14261391
#endif
14271392

1428-
enum fsync_action {
1429-
FSYNC_WRITEOUT_ONLY,
1430-
FSYNC_HARDWARE_FLUSH
1431-
};
1432-
1433-
/*
1434-
* Issues an fsync against the specified file according to the specified mode.
1435-
*
1436-
* FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
1437-
* systems to flush the OS cache without issuing a flush command to the storage
1438-
* controller. If those interfaces are unavailable, the function fails with
1439-
* ENOSYS.
1440-
*
1441-
* FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
1442-
* changes are durable. It is not expected to fail.
1443-
*/
1444-
int git_fsync(int fd, enum fsync_action action);
1445-
1446-
/*
1447-
* Writes out trace statistics for fsync using the trace2 API.
1448-
*/
1449-
void trace_git_fsync_stats(void);
1450-
1451-
/*
1452-
* Preserves errno, prints a message, but gives no warning for ENOENT.
1453-
* Returns 0 on success, which includes trying to unlink an object that does
1454-
* not exist.
1455-
*/
1456-
int unlink_or_warn(const char *path);
1457-
/*
1458-
* Tries to unlink file. Returns 0 if unlink succeeded
1459-
* or the file already didn't exist. Returns -1 and
1460-
* appends a message to err suitable for
1461-
* 'error("%s", err->buf)' on error.
1462-
*/
1463-
int unlink_or_msg(const char *file, struct strbuf *err);
1464-
/*
1465-
* Preserves errno, prints a message, but gives no warning for ENOENT.
1466-
* Returns 0 on success, which includes trying to remove a directory that does
1467-
* not exist.
1468-
*/
1469-
int rmdir_or_warn(const char *path);
1470-
/*
1471-
* Calls the correct function out of {unlink,rmdir}_or_warn based on
1472-
* the supplied file mode.
1473-
*/
1474-
int remove_or_warn(unsigned int mode, const char *path);
1475-
1476-
/*
1477-
* Call access(2), but warn for any error except "missing file"
1478-
* (ENOENT or ENOTDIR).
1479-
*/
1480-
#define ACCESS_EACCES_OK (1U << 0)
1481-
int access_or_warn(const char *path, int mode, unsigned flag);
1482-
int access_or_die(const char *path, int mode, unsigned flag);
1483-
1484-
/* Warn on an inaccessible file if errno indicates this is an error */
1485-
int warn_on_fopen_errors(const char *path);
1486-
1487-
/*
1488-
* Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
1489-
* may be racy. Do not use this as protection against an attacker who can
1490-
* simultaneously create paths.
1491-
*/
1492-
int open_nofollow(const char *path, int flags);
1493-
14941393
#ifndef SHELL_PATH
14951394
# define SHELL_PATH "/bin/sh"
14961395
#endif
@@ -1630,13 +1529,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
16301529
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
16311530
#endif /* !__GNUC__ */
16321531

1633-
void sleep_millisec(int millisec);
1634-
1635-
/*
1636-
* Generate len bytes from the system cryptographically secure PRNG.
1637-
* Returns 0 on success and -1 on error, setting errno. The inability to
1638-
* satisfy the full request is an error.
1639-
*/
1640-
int csprng_bytes(void *buf, size_t len);
1641-
16421532
#endif

wrapper.h

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
#ifndef WRAPPER_H
22
#define WRAPPER_H
33

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+
440
/* set default permissions by passing mode arguments to open(2) */
541
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
642
int git_mkstemp_mode(char *pattern, int mode);
@@ -33,4 +69,79 @@ void write_file(const char *path, const char *fmt, ...);
3369
/* Return 1 if the file is empty or does not exists, 0 otherwise. */
3470
int is_empty_or_missing_file(const char *filename);
3571

72+
enum fsync_action {
73+
FSYNC_WRITEOUT_ONLY,
74+
FSYNC_HARDWARE_FLUSH
75+
};
76+
77+
/*
78+
* Issues an fsync against the specified file according to the specified mode.
79+
*
80+
* FSYNC_WRITEOUT_ONLY attempts to use interfaces available on some operating
81+
* systems to flush the OS cache without issuing a flush command to the storage
82+
* controller. If those interfaces are unavailable, the function fails with
83+
* ENOSYS.
84+
*
85+
* FSYNC_HARDWARE_FLUSH does an OS writeout and hardware flush to ensure that
86+
* changes are durable. It is not expected to fail.
87+
*/
88+
int git_fsync(int fd, enum fsync_action action);
89+
90+
/*
91+
* Writes out trace statistics for fsync using the trace2 API.
92+
*/
93+
void trace_git_fsync_stats(void);
94+
95+
/*
96+
* Preserves errno, prints a message, but gives no warning for ENOENT.
97+
* Returns 0 on success, which includes trying to unlink an object that does
98+
* not exist.
99+
*/
100+
int unlink_or_warn(const char *path);
101+
/*
102+
* Tries to unlink file. Returns 0 if unlink succeeded
103+
* or the file already didn't exist. Returns -1 and
104+
* appends a message to err suitable for
105+
* 'error("%s", err->buf)' on error.
106+
*/
107+
int unlink_or_msg(const char *file, struct strbuf *err);
108+
/*
109+
* Preserves errno, prints a message, but gives no warning for ENOENT.
110+
* Returns 0 on success, which includes trying to remove a directory that does
111+
* not exist.
112+
*/
113+
int rmdir_or_warn(const char *path);
114+
/*
115+
* Calls the correct function out of {unlink,rmdir}_or_warn based on
116+
* the supplied file mode.
117+
*/
118+
int remove_or_warn(unsigned int mode, const char *path);
119+
120+
/*
121+
* Call access(2), but warn for any error except "missing file"
122+
* (ENOENT or ENOTDIR).
123+
*/
124+
#define ACCESS_EACCES_OK (1U << 0)
125+
int access_or_warn(const char *path, int mode, unsigned flag);
126+
int access_or_die(const char *path, int mode, unsigned flag);
127+
128+
/* Warn on an inaccessible file if errno indicates this is an error */
129+
int warn_on_fopen_errors(const char *path);
130+
131+
/*
132+
* Open with O_NOFOLLOW, or equivalent. Note that the fallback equivalent
133+
* may be racy. Do not use this as protection against an attacker who can
134+
* simultaneously create paths.
135+
*/
136+
int open_nofollow(const char *path, int flags);
137+
138+
void sleep_millisec(int millisec);
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.
144+
*/
145+
int csprng_bytes(void *buf, size_t len);
146+
36147
#endif /* WRAPPER_H */

0 commit comments

Comments
 (0)