Skip to content

Commit 2f795d8

Browse files
committed
Cleanup portability/compatibility layer
* Removes mingw-compat.h * Cleans up separation of compiler/platform idiosyncrasies * Unifies mingw/msvc stat structures and functions * (Tries to) hide more compiler specific implementation details (even in our internal API)
1 parent d07fd44 commit 2f795d8

7 files changed

Lines changed: 106 additions & 90 deletions

File tree

src/path.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define INCLUDE_path_h__
99

1010
#include "common.h"
11+
#include "posix.h"
1112
#include "buffer.h"
1213
#include "vector.h"
1314

src/posix.h

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,61 @@
1212
#include <time.h>
1313
#include "fnmatch.h"
1414

15+
/* stat: file mode type testing macros */
1516
#ifndef S_IFGITLINK
1617
#define S_IFGITLINK 0160000
1718
#define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
1819
#endif
1920

21+
#ifndef S_IFLNK
22+
#define S_IFLNK 0120000
23+
#undef _S_IFLNK
24+
#define _S_IFLNK S_IFLNK
25+
#endif
26+
27+
#ifndef S_IXUSR
28+
#define S_IXUSR 00100
29+
#endif
30+
31+
#ifndef S_ISLNK
32+
#define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK)
33+
#endif
34+
35+
#ifndef S_ISDIR
36+
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
37+
#endif
38+
39+
#ifndef S_ISREG
40+
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
41+
#endif
42+
43+
#ifndef S_ISFIFO
44+
#define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
45+
#endif
46+
2047
/* if S_ISGID is not defined, then don't try to set it */
2148
#ifndef S_ISGID
2249
#define S_ISGID 0
2350
#endif
2451

25-
#if !defined(O_BINARY)
52+
#ifndef O_BINARY
2653
#define O_BINARY 0
2754
#endif
28-
#if !defined(O_CLOEXEC)
55+
#ifndef O_CLOEXEC
2956
#define O_CLOEXEC 0
3057
#endif
3158

59+
/* access() mode parameter #defines */
60+
#ifndef F_OK
61+
#define F_OK 0 /* existence check */
62+
#endif
63+
#ifndef W_OK
64+
#define W_OK 2 /* write mode check */
65+
#endif
66+
#ifndef R_OK
67+
#define R_OK 4 /* read mode check */
68+
#endif
69+
3270
/* Determine whether an errno value indicates that a read or write failed
3371
* because the descriptor is blocked.
3472
*/
@@ -38,6 +76,12 @@
3876
#define GIT_ISBLOCKED(e) ((e) == EAGAIN)
3977
#endif
4078

79+
/* define some standard errnos that the runtime may be missing. for example,
80+
* mingw lacks EAFNOSUPPORT. */
81+
#ifndef EAFNOSUPPORT
82+
#define EAFNOSUPPORT (INT_MAX-1)
83+
#endif
84+
4185
typedef int git_file;
4286

4387
/**
@@ -56,8 +100,6 @@ typedef int git_file;
56100
extern int p_read(git_file fd, void *buf, size_t cnt);
57101
extern int p_write(git_file fd, const void *buf, size_t cnt);
58102

59-
#define p_fstat(f,b) fstat(f, b)
60-
#define p_lseek(f,n,w) lseek(f, n, w)
61103
#define p_close(fd) close(fd)
62104
#define p_umask(m) umask(m)
63105

@@ -66,30 +108,6 @@ extern int p_creat(const char *path, mode_t mode);
66108
extern int p_getcwd(char *buffer_out, size_t size);
67109
extern int p_rename(const char *from, const char *to);
68110

69-
#ifndef GIT_WIN32
70-
71-
#define p_stat(p,b) stat(p, b)
72-
#define p_chdir(p) chdir(p)
73-
#define p_rmdir(p) rmdir(p)
74-
#define p_chmod(p,m) chmod(p, m)
75-
#define p_access(p,m) access(p,m)
76-
#define p_ftruncate(fd, sz) ftruncate(fd, sz)
77-
#define p_recv(s,b,l,f) recv(s,b,l,f)
78-
#define p_send(s,b,l,f) send(s,b,l,f)
79-
typedef int GIT_SOCKET;
80-
#define INVALID_SOCKET -1
81-
82-
#define p_localtime_r localtime_r
83-
#define p_gmtime_r gmtime_r
84-
85-
#else
86-
87-
typedef SOCKET GIT_SOCKET;
88-
extern struct tm * p_localtime_r (const time_t *timer, struct tm *result);
89-
extern struct tm * p_gmtime_r (const time_t *timer, struct tm *result);
90-
91-
#endif
92-
93111
/**
94112
* Platform-dependent methods
95113
*/

src/unix/posix.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,27 @@
1010
#include <stdio.h>
1111
#include <sys/param.h>
1212

13+
typedef int GIT_SOCKET;
14+
#define INVALID_SOCKET -1
15+
16+
#define p_strcasecmp(s1, s2) strcasecmp(s1, s2)
17+
#define p_strncasecmp(s1, s2, c) strncasecmp(s1, s2, c)
18+
19+
#define p_lseek(f,n,w) lseek(f, n, w)
20+
#define p_fstat(f,b) fstat(f, b)
1321
#define p_lstat(p,b) lstat(p,b)
22+
1423
#define p_readlink(a, b, c) readlink(a, b, c)
1524
#define p_symlink(o,n) symlink(o, n)
1625
#define p_link(o,n) link(o, n)
1726
#define p_unlink(p) unlink(p)
1827
#define p_mkdir(p,m) mkdir(p, m)
1928
#define p_fsync(fd) fsync(fd)
2029

30+
#define p_recv(s,b,l,f) recv(s,b,l,f)
31+
#define p_send(s,b,l,f) send(s,b,l,f)
32+
#define p_inet_pton(a, b, c) inet_pton(a, b, c)
33+
2134
/* The OpenBSD realpath function behaves differently */
2235
#if !defined(__OpenBSD__)
2336
# define p_realpath(p, po) realpath(p, po)
@@ -28,9 +41,17 @@ char *p_realpath(const char *, char *);
2841
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a)
2942
#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
3043
#define p_mkstemp(p) mkstemp(p)
31-
#define p_inet_pton(a, b, c) inet_pton(a, b, c)
44+
#define p_stat(p,b) stat(p, b)
45+
#define p_chdir(p) chdir(p)
46+
#define p_chmod(p,m) chmod(p, m)
47+
#define p_rmdir(p) rmdir(p)
48+
#define p_access(p,m) access(p,m)
49+
#define p_ftruncate(fd, sz) ftruncate(fd, sz)
3250

3351
/* see win32/posix.h for explanation about why this exists */
3452
#define p_lstat_posixly(p,b) lstat(p,b)
3553

54+
#define p_localtime_r(c, r) localtime_r(c, r)
55+
#define p_gmtime_r(c, r) gmtime_r(c, r)
56+
3657
#endif

src/win32/mingw-compat.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@
99

1010
#if defined(__MINGW32__)
1111

12-
/* use a 64-bit file offset type */
13-
# undef lseek
14-
# define lseek _lseeki64
15-
# undef stat
16-
# define stat _stati64
17-
# undef fstat
18-
# define fstat _fstati64
19-
20-
/* stat: file mode type testing macros */
21-
# define _S_IFLNK 0120000
22-
# define S_IFLNK _S_IFLNK
23-
# define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK)
12+
#if _WIN32_WINNT >= 0x0601
13+
#define stat __stat64
14+
#else
15+
#define stat _stati64
16+
#endif
2417

2518
#endif
2619

src/win32/msvc-compat.h

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,10 @@
99

1010
#if defined(_MSC_VER)
1111

12-
/* access() mode parameter #defines */
13-
# define F_OK 0 /* existence check */
14-
# define W_OK 2 /* write mode check */
15-
# define R_OK 4 /* read mode check */
12+
/* 64-bit stat information, regardless of USE_32BIT_TIME_T define */
13+
#define stat __stat64
1614

17-
# define lseek _lseeki64
18-
# define stat __stat64
19-
# define fstat _fstat64
20-
21-
/* stat: file mode type testing macros */
22-
# define _S_IFLNK 0120000
23-
# define S_IFLNK _S_IFLNK
24-
# define S_IXUSR 00100
25-
26-
# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
27-
# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
28-
# define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
29-
# define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK)
30-
31-
# define mode_t unsigned short
32-
33-
/* case-insensitive string comparison */
34-
# define strcasecmp _stricmp
35-
# define strncasecmp _strnicmp
36-
37-
/* MSVC doesn't define ssize_t at all */
15+
typedef unsigned short mode_t;
3816
typedef SSIZE_T ssize_t;
3917

4018
/* define snprintf using variadic macro support if available */

src/win32/posix.h

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,18 @@
1212
#include "utf-conv.h"
1313
#include "dir.h"
1414

15-
/* define some standard errnos that the runtime may be missing. for example,
16-
* mingw lacks EAFNOSUPPORT. */
15+
typedef SOCKET GIT_SOCKET;
1716

18-
#ifndef EAFNOSUPPORT
19-
# define EAFNOSUPPORT (INT_MAX-1)
20-
#endif
21-
22-
#if defined(_MSC_VER) && _MSC_VER >= 1500
23-
# define p_ftruncate(fd, sz) _chsize_s(fd, sz)
24-
#else /* MinGW */
25-
# define p_ftruncate(fd, sz) _chsize(fd, sz)
26-
#endif
17+
#define p_strcasecmp(s1, s2) _stricmp(s1, s2)
18+
#define p_strncasecmp(s1, s2, c) _strnicmp(s1, s2, c)
2719

28-
GIT_INLINE(int) p_link(const char *old, const char *new)
29-
{
30-
GIT_UNUSED(old);
31-
GIT_UNUSED(new);
32-
errno = ENOSYS;
33-
return -1;
34-
}
20+
#define p_lseek(f,n,w) _lseeki64(f, n, w)
21+
#define p_fstat(f,b) _fstat64(f, b)
22+
extern int p_lstat(const char *file_name, struct stat *buf);
3523

36-
extern int p_mkdir(const char *path, mode_t mode);
24+
extern int p_link(const char *old, const char *new);
3725
extern int p_unlink(const char *path);
38-
extern int p_lstat(const char *file_name, struct stat *buf);
26+
extern int p_mkdir(const char *path, mode_t mode);
3927
extern int p_readlink(const char *path, char *buf, size_t bufsiz);
4028
extern int p_symlink(const char *old, const char *new);
4129
extern char *p_realpath(const char *orig_path, char *buffer);
@@ -47,15 +35,15 @@ extern int p_chdir(const char* path);
4735
extern int p_chmod(const char* path, mode_t mode);
4836
extern int p_rmdir(const char* path);
4937
extern int p_access(const char* path, mode_t mode);
38+
extern int p_ftruncate(int fd, long size);
5039
extern int p_fsync(int fd);
51-
extern int p_open(const char *path, int flags, ...);
52-
extern int p_creat(const char *path, mode_t mode);
53-
extern int p_getcwd(char *buffer_out, size_t size);
54-
extern int p_rename(const char *from, const char *to);
5540
extern int p_recv(GIT_SOCKET socket, void *buffer, size_t length, int flags);
5641
extern int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags);
5742
extern int p_inet_pton(int af, const char* src, void* dst);
5843

44+
extern struct tm * p_localtime_r (const time_t *timer, struct tm *result);
45+
extern struct tm * p_gmtime_r (const time_t *timer, struct tm *result);
46+
5947
/* p_lstat is almost but not quite POSIX correct. Specifically, the use of
6048
* ENOTDIR is wrong, in that it does not mean precisely that a non-directory
6149
* entry was encountered. Making it correct is potentially expensive,

src/win32/posix_w32.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ static int utf8_to_16_with_errno(git_win32_path dest, const char *src)
5151
return len;
5252
}
5353

54+
int p_ftruncate(int fd, long size)
55+
{
56+
#if defined(_MSC_VER) && _MSC_VER >= 1500
57+
return _chsize_s(fd, size);
58+
#else
59+
return _chsize(fd, size);
60+
#endif
61+
}
62+
5463
int p_mkdir(const char *path, mode_t mode)
5564
{
5665
git_win32_path buf;
@@ -63,6 +72,14 @@ int p_mkdir(const char *path, mode_t mode)
6372
return _wmkdir(buf);
6473
}
6574

75+
int p_link(const char *old, const char *new)
76+
{
77+
GIT_UNUSED(old);
78+
GIT_UNUSED(new);
79+
errno = ENOSYS;
80+
return -1;
81+
}
82+
6683
int p_unlink(const char *path)
6784
{
6885
git_win32_path buf;

0 commit comments

Comments
 (0)