1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 #ifndef INCLUDE_posix_h__
8 #define INCLUDE_posix_h__
9 
10 #include "common.h"
11 
12 #include <fcntl.h>
13 #include <time.h>
14 
15 /* stat: file mode type testing macros */
16 #ifndef S_IFGITLINK
17 #define S_IFGITLINK 0160000
18 #define S_ISGITLINK(m) (((m) & S_IFMT) == S_IFGITLINK)
19 #endif
20 
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_IWUSR
28 #define S_IWUSR 00200
29 #endif
30 
31 #ifndef S_IXUSR
32 #define S_IXUSR 00100
33 #endif
34 
35 #ifndef S_ISLNK
36 #define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK)
37 #endif
38 
39 #ifndef S_ISDIR
40 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
41 #endif
42 
43 #ifndef S_ISREG
44 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
45 #endif
46 
47 #ifndef S_ISFIFO
48 #define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
49 #endif
50 
51 /* if S_ISGID is not defined, then don't try to set it */
52 #ifndef S_ISGID
53 #define S_ISGID 0
54 #endif
55 
56 #ifndef O_BINARY
57 #define O_BINARY 0
58 #endif
59 #ifndef O_CLOEXEC
60 #define O_CLOEXEC 0
61 #endif
62 #ifndef SOCK_CLOEXEC
63 #define SOCK_CLOEXEC 0
64 #endif
65 
66 /* access() mode parameter #defines	*/
67 #ifndef F_OK
68 #define F_OK 0 /* existence check */
69 #endif
70 #ifndef W_OK
71 #define W_OK 2 /* write mode check */
72 #endif
73 #ifndef R_OK
74 #define R_OK 4 /* read mode check */
75 #endif
76 
77 /* Determine whether an errno value indicates that a read or write failed
78  * because the descriptor is blocked.
79  */
80 #if defined(EWOULDBLOCK)
81 #define GIT_ISBLOCKED(e) ((e) == EAGAIN || (e) == EWOULDBLOCK)
82 #else
83 #define GIT_ISBLOCKED(e) ((e) == EAGAIN)
84 #endif
85 
86 /* define some standard errnos that the runtime may be missing.  for example,
87  * mingw lacks EAFNOSUPPORT. */
88 #ifndef EAFNOSUPPORT
89 #define EAFNOSUPPORT (INT_MAX-1)
90 #endif
91 
92 typedef int git_file;
93 
94 /**
95  * Standard POSIX Methods
96  *
97  * All the methods starting with the `p_` prefix are
98  * direct ports of the standard POSIX methods.
99  *
100  * Some of the methods are slightly wrapped to provide
101  * saner defaults. Some of these methods are emulated
102  * in Windows platforms.
103  *
104  * Use your manpages to check the docs on these.
105  */
106 
107 extern ssize_t p_read(git_file fd, void *buf, size_t cnt);
108 extern int p_write(git_file fd, const void *buf, size_t cnt);
109 
110 #define p_close(fd) close(fd)
111 #define p_umask(m) umask(m)
112 
113 extern int p_open(const char *path, int flags, ...);
114 extern int p_creat(const char *path, mode_t mode);
115 extern int p_getcwd(char *buffer_out, size_t size);
116 extern int p_rename(const char *from, const char *to);
117 
118 extern int git__page_size(size_t *page_size);
119 extern int git__mmap_alignment(size_t *page_size);
120 
121 /* The number of times `p_fsync` has been called.  Note that this is for
122  * test code only; it it not necessarily thread-safe and should not be
123  * relied upon in production.
124  */
125 extern size_t p_fsync__cnt;
126 
127 /**
128  * Platform-dependent methods
129  */
130 #ifdef GIT_WIN32
131 #	include "win32/posix.h"
132 #else
133 #	include "unix/posix.h"
134 #endif
135 
136 #include "strnlen.h"
137 
138 #ifdef NO_READDIR_R
p_readdir_r(DIR * dirp,struct dirent * entry,struct dirent ** result)139 GIT_INLINE(int) p_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result)
140 {
141 	GIT_UNUSED(entry);
142 	*result = readdir(dirp);
143 	return 0;
144 }
145 #else /* NO_READDIR_R */
146 #	define p_readdir_r(d,e,r) readdir_r(d,e,r)
147 #endif
148 
149 #ifdef NO_ADDRINFO
150 #	include <netdb.h>
151 struct addrinfo {
152 	struct hostent *ai_hostent;
153 	struct servent *ai_servent;
154 	struct sockaddr_in ai_addr_in;
155 	struct sockaddr *ai_addr;
156 	size_t ai_addrlen;
157 	int ai_family;
158 	int ai_socktype;
159 	int ai_protocol;
160 	long ai_port;
161 	struct addrinfo *ai_next;
162 };
163 
164 extern int p_getaddrinfo(const char *host, const char *port,
165 	struct addrinfo *hints, struct addrinfo **info);
166 extern void p_freeaddrinfo(struct addrinfo *info);
167 extern const char *p_gai_strerror(int ret);
168 #else
169 #	define p_getaddrinfo(a, b, c, d) getaddrinfo(a, b, c, d)
170 #	define p_freeaddrinfo(a) freeaddrinfo(a)
171 #	define p_gai_strerror(c) gai_strerror(c)
172 #endif /* NO_ADDRINFO */
173 
174 #endif
175