]>
git.proxmox.com Git - libgit2.git/blob - src/posix.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
14 size_t p_fsync__cnt
= 0;
23 struct addrinfo
*hints
,
24 struct addrinfo
**info
)
26 struct addrinfo
*ainfo
, *ai
;
31 if ((ainfo
= git__malloc(sizeof(struct addrinfo
))) == NULL
)
34 if ((ainfo
->ai_hostent
= gethostbyname(host
)) == NULL
) {
39 ainfo
->ai_servent
= getservbyname(port
, 0);
41 if (ainfo
->ai_servent
)
42 ainfo
->ai_port
= ainfo
->ai_servent
->s_port
;
44 ainfo
->ai_port
= htons(atol(port
));
46 memcpy(&ainfo
->ai_addr_in
.sin_addr
,
47 ainfo
->ai_hostent
->h_addr_list
[0],
48 ainfo
->ai_hostent
->h_length
);
50 ainfo
->ai_protocol
= 0;
51 ainfo
->ai_socktype
= hints
->ai_socktype
;
52 ainfo
->ai_family
= ainfo
->ai_hostent
->h_addrtype
;
53 ainfo
->ai_addr_in
.sin_family
= ainfo
->ai_family
;
54 ainfo
->ai_addr_in
.sin_port
= ainfo
->ai_port
;
55 ainfo
->ai_addr
= (struct addrinfo
*)&ainfo
->ai_addr_in
;
56 ainfo
->ai_addrlen
= sizeof(struct sockaddr_in
);
60 if (ainfo
->ai_hostent
->h_addr_list
[1] == NULL
) {
61 ainfo
->ai_next
= NULL
;
67 for (p
= 1; ainfo
->ai_hostent
->h_addr_list
[p
] != NULL
; p
++) {
68 if (!(ai
->ai_next
= git__malloc(sizeof(struct addrinfo
)))) {
69 p_freeaddrinfo(ainfo
);
72 memcpy(ai
->ai_next
, ainfo
, sizeof(struct addrinfo
));
73 memcpy(&ai
->ai_next
->ai_addr_in
.sin_addr
,
74 ainfo
->ai_hostent
->h_addr_list
[p
],
75 ainfo
->ai_hostent
->h_length
);
76 ai
->ai_next
->ai_addr
= (struct addrinfo
*)&ai
->ai_next
->ai_addr_in
;
84 void p_freeaddrinfo(struct addrinfo
*info
)
86 struct addrinfo
*p
, *next
;
97 const char *p_gai_strerror(int ret
)
100 case -1: return "Out of memory"; break;
101 case -2: return "Address lookup failed"; break;
102 default: return "Unknown error"; break;
106 #endif /* NO_ADDRINFO */
108 int p_open(const char *path
, volatile int flags
, ...)
112 #ifdef GIT_DEBUG_STRICT_OPEN
113 if (strstr(path
, "//") != NULL
) {
119 if (flags
& O_CREAT
) {
122 va_start(arg_list
, flags
);
123 mode
= (mode_t
)va_arg(arg_list
, int);
127 return open(path
, flags
| O_BINARY
| O_CLOEXEC
, mode
);
130 int p_creat(const char *path
, mode_t mode
)
132 return open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_CLOEXEC
, mode
);
135 int p_getcwd(char *buffer_out
, size_t size
)
139 GIT_ASSERT_ARG(buffer_out
);
140 GIT_ASSERT_ARG(size
> 0);
142 cwd_buffer
= getcwd(buffer_out
, size
);
144 if (cwd_buffer
== NULL
)
147 git_fs_path_mkposix(buffer_out
);
148 git_fs_path_string_to_dir(buffer_out
, size
); /* append trailing slash */
153 int p_rename(const char *from
, const char *to
)
155 if (!link(from
, to
)) {
160 if (!rename(from
, to
))
166 #endif /* GIT_WIN32 */
168 ssize_t
p_read(git_file fd
, void *buf
, size_t cnt
)
172 if (!git__is_ssizet(cnt
)) {
174 SetLastError(ERROR_INVALID_PARAMETER
);
183 r
= read(fd
, b
, cnt
> INT_MAX
? INT_MAX
: (unsigned int)cnt
);
185 r
= read(fd
, b
, cnt
);
188 if (errno
== EINTR
|| errno
== EAGAIN
)
197 return (b
- (char *)buf
);
200 int p_write(git_file fd
, const void *buf
, size_t cnt
)
207 GIT_ASSERT((size_t)((unsigned int)cnt
) == cnt
);
208 r
= write(fd
, b
, (unsigned int)cnt
);
210 r
= write(fd
, b
, cnt
);
213 if (errno
== EINTR
|| GIT_ISBLOCKED(errno
))
231 int git__page_size(size_t *page_size
)
233 /* dummy; here we don't need any alignment anyway */
238 int git__mmap_alignment(size_t *alignment
)
240 /* dummy; here we don't need any alignment anyway */
246 int p_mmap(git_map
*out
, size_t len
, int prot
, int flags
, int fd
, off64_t offset
)
249 size_t remaining_len
;
251 GIT_MMAP_VALIDATE(out
, len
, prot
, flags
);
253 /* writes cannot be emulated without handling pagefaults since write happens by
254 * writing to mapped memory */
255 if (prot
& GIT_PROT_WRITE
) {
256 git_error_set(GIT_ERROR_OS
, "trying to map %s-writeable",
257 ((flags
& GIT_MAP_TYPE
) == GIT_MAP_SHARED
) ? "shared": "private");
261 if (!git__is_ssizet(len
)) {
267 out
->data
= git__malloc(len
);
268 GIT_ERROR_CHECK_ALLOC(out
->data
);
271 ptr
= (const char *)out
->data
;
272 while (remaining_len
> 0) {
274 HANDLE_EINTR(nb
, p_pread(fd
, (void *)ptr
, remaining_len
, offset
));
276 git_error_set(GIT_ERROR_OS
, "mmap emulation failed");
277 git__free(out
->data
);
291 int p_munmap(git_map
*map
)
294 git__free(map
->data
);
296 /* Initializing will help debug use-after-free */