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