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 <calvinwan@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Calvin Wan committed
Jul 5, 2023 at 17:09 UTC
382f6940afc4f2c5f1e939d0ca8ba835056cf2d0
2 files changed
+112
-111
git-compat-util.h
+1
-111
@@ -625,7 +625,7 @@ static inline int git_has_dir_sep(const char *path)
625
626
#include "compat/bswap.h"
627
628
-struct strbuf;
628
+#include "wrapper.h"
629
630
/* General helper functions */
631
NORETURN void usage(const char *err);
@@ -1045,36 +1045,6 @@ static inline int cast_size_t_to_int(size_t a)
1045
# define xalloca(size) (xmalloc(size))
1046
# define xalloca_free(p) (free(p))
1047
#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);
1048
1049
/*
1050
* 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)
1146
return (size_t) len;
1147
}
1148
1179
-__attribute__((format (printf, 3, 4)))
1180
-int xsnprintf(char *dst, size_t max, const char *fmt, ...);
1181
-
1149
#ifndef HOST_NAME_MAX
1150
#define HOST_NAME_MAX 256
1151
#endif
1152
1186
-int xgethostname(char *buf, size_t len);
1187
-
1153
/* in ctype.c, for kwset users */
1154
extern const unsigned char tolower_trans_tbl[256];
1155
@@ -1425,72 +1390,6 @@ void bug_fl(const char *file, int line, const char *fmt, ...);
1390
#endif
1391
#endif
1392
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
-
1393
#ifndef SHELL_PATH
1394
# define SHELL_PATH "/bin/sh"
1395
#endif
@@ -1630,13 +1529,4 @@ static inline void *container_of_or_null_offset(void *ptr, size_t offset)
1529
((uintptr_t)&(ptr)->member - (uintptr_t)(ptr))
1530
#endif /* !__GNUC__ */
1531
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
-
1532
#endif
wrapper.h
+111
@@ -1,6 +1,42 @@
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
/* set default permissions by passing mode arguments to open(2) */
41
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
42
int git_mkstemp_mode(char *pattern, int mode);
@@ -33,4 +69,79 @@ void write_file(const char *path, const char *fmt, ...);
69
/* Return 1 if the file is empty or does not exists, 0 otherwise. */
70
int is_empty_or_missing_file(const char *filename);
71
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
+
147
#endif /* WRAPPER_H */