posix.c 4.96 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7

Vicent Marti committed
8
#include "posix.h"
9

Vicent Marti committed
10 11 12 13
#include "path.h"
#include <stdio.h>
#include <ctype.h>

14 15
size_t p_fsync__cnt = 0;

16 17
#ifndef GIT_WIN32

Vicent Marti committed
18
#ifdef NO_ADDRINFO
19

Vicent Marti committed
20 21 22 23 24 25 26 27
int p_getaddrinfo(
	const char *host,
	const char *port,
	struct addrinfo *hints,
	struct addrinfo **info)
{
	struct addrinfo *ainfo, *ai;
	int p = 0;
28 29 30

	GIT_UNUSED(hints);

31
	if ((ainfo = git__malloc(sizeof(struct addrinfo))) == NULL)
Vicent Marti committed
32
		return -1;
33 34

	if ((ainfo->ai_hostent = gethostbyname(host)) == NULL) {
35
		git__free(ainfo);
Vicent Marti committed
36
		return -2;
37 38
	}

Vicent Marti committed
39
	ainfo->ai_servent = getservbyname(port, 0);
40

Vicent Marti committed
41 42 43
	if (ainfo->ai_servent)
		ainfo->ai_port = ainfo->ai_servent->s_port;
	else
44
		ainfo->ai_port = htons(atol(port));
Vicent Marti committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58

	memcpy(&ainfo->ai_addr_in.sin_addr,
			ainfo->ai_hostent->h_addr_list[0],
			ainfo->ai_hostent->h_length);

	ainfo->ai_protocol = 0;
	ainfo->ai_socktype = hints->ai_socktype;
	ainfo->ai_family = ainfo->ai_hostent->h_addrtype;
	ainfo->ai_addr_in.sin_family = ainfo->ai_family;
	ainfo->ai_addr_in.sin_port = ainfo->ai_port;
	ainfo->ai_addr = (struct addrinfo *)&ainfo->ai_addr_in;
	ainfo->ai_addrlen = sizeof(struct sockaddr_in);

	*info = ainfo;
59

Vicent Marti committed
60 61 62 63
	if (ainfo->ai_hostent->h_addr_list[1] == NULL) {
		ainfo->ai_next = NULL;
		return 0;
	}
64

Vicent Marti committed
65
	ai = ainfo;
66

Vicent Marti committed
67
	for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
68
		if (!(ai->ai_next = git__malloc(sizeof(struct addrinfo)))) {
69 70 71
			p_freeaddrinfo(ainfo);
			return -1;
		}
72
		memcpy(ai->ai_next, ainfo, sizeof(struct addrinfo));
Vicent Marti committed
73 74 75 76 77 78
		memcpy(&ai->ai_next->ai_addr_in.sin_addr,
			ainfo->ai_hostent->h_addr_list[p],
			ainfo->ai_hostent->h_length);
		ai->ai_next->ai_addr = (struct addrinfo *)&ai->ai_next->ai_addr_in;
		ai = ai->ai_next;
	}
79

Vicent Marti committed
80 81 82 83 84 85 86
	ai->ai_next = NULL;
	return 0;
}

void p_freeaddrinfo(struct addrinfo *info)
{
	struct addrinfo *p, *next;
87

Vicent Marti committed
88
	p = info;
89

Vicent Marti committed
90 91
	while(p != NULL) {
		next = p->ai_next;
92
		git__free(p);
Vicent Marti committed
93 94 95 96 97 98 99
		p = next;
	}
}

const char *p_gai_strerror(int ret)
{
	switch(ret) {
100 101 102
	case -1: return "Out of memory"; break;
	case -2: return "Address lookup failed"; break;
	default: return "Unknown error"; break;
Vicent Marti committed
103 104
	}
}
105

Vicent Marti committed
106 107
#endif /* NO_ADDRINFO */

108
int p_open(const char *path, volatile int flags, ...)
Vicent Marti committed
109
{
110 111
	mode_t mode = 0;

112
	if (flags & O_CREAT) {
113 114 115
		va_list arg_list;

		va_start(arg_list, flags);
116
		mode = (mode_t)va_arg(arg_list, int);
117 118 119
		va_end(arg_list);
	}

120
	return open(path, flags | O_BINARY | O_CLOEXEC, mode);
Vicent Marti committed
121 122
}

123
int p_creat(const char *path, mode_t mode)
Vicent Marti committed
124
{
125
	return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_CLOEXEC, mode);
Vicent Marti committed
126 127
}

128 129 130 131 132 133 134 135 136
int p_getcwd(char *buffer_out, size_t size)
{
	char *cwd_buffer;

	assert(buffer_out && size > 0);

	cwd_buffer = getcwd(buffer_out, size);

	if (cwd_buffer == NULL)
137
		return -1;
138 139

	git_path_mkposix(buffer_out);
140
	git_path_string_to_dir(buffer_out, size); /* append trailing slash */
141

142
	return 0;
143 144
}

145 146 147 148
int p_rename(const char *from, const char *to)
{
	if (!link(from, to)) {
		p_unlink(from);
149
		return 0;
150 151 152
	}

	if (!rename(from, to))
153
		return 0;
154

155
	return -1;
156 157
}

Vicent Marti committed
158
#endif /* GIT_WIN32 */
159

160
ssize_t p_read(git_file fd, void *buf, size_t cnt)
Vicent Marti committed
161 162
{
	char *b = buf;
163

164 165 166 167 168 169 170 171
	if (!git__is_ssizet(cnt)) {
#ifdef GIT_WIN32
		SetLastError(ERROR_INVALID_PARAMETER);
#endif
		errno = EINVAL;
		return -1;
	}

Vicent Marti committed
172
	while (cnt) {
173 174
		ssize_t r;
#ifdef GIT_WIN32
175
		r = read(fd, b, cnt > INT_MAX ? INT_MAX : (unsigned int)cnt);
176 177 178
#else
		r = read(fd, b, cnt);
#endif
Vicent Marti committed
179 180 181
		if (r < 0) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
182
			return -1;
Vicent Marti committed
183 184 185 186 187 188
		}
		if (!r)
			break;
		cnt -= r;
		b += r;
	}
189
	return (b - (char *)buf);
Vicent Marti committed
190 191
}

192
int p_write(git_file fd, const void *buf, size_t cnt)
Vicent Marti committed
193
{
194
	const char *b = buf;
195

Vicent Marti committed
196
	while (cnt) {
197 198 199 200 201 202 203
		ssize_t r;
#ifdef GIT_WIN32
		assert((size_t)((unsigned int)cnt) == cnt);
		r = write(fd, b, (unsigned int)cnt);
#else
		r = write(fd, b, cnt);
#endif
Vicent Marti committed
204
		if (r < 0) {
205
			if (errno == EINTR || GIT_ISBLOCKED(errno))
Vicent Marti committed
206
				continue;
207
			return -1;
Vicent Marti committed
208 209 210
		}
		if (!r) {
			errno = EPIPE;
211
			return -1;
Vicent Marti committed
212 213 214 215
		}
		cnt -= r;
		b += r;
	}
216
	return 0;
Vicent Marti committed
217
}
218

219
#ifdef NO_MMAP
220

221 222
#include "map.h"

223
int git__page_size(size_t *page_size)
224 225
{
	/* dummy; here we don't need any alignment anyway */
226 227
	*page_size = 4096;
	return 0;
228 229
}

230 231 232 233 234 235 236
int git__mmap_alignment(size_t *alignment)
{
	/* dummy; here we don't need any alignment anyway */
	*alignment = 4096;
	return 0;
}

237

238
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, off64_t offset)
239 240 241 242 243 244 245
{
	GIT_MMAP_VALIDATE(out, len, prot, flags);

	out->data = NULL;
	out->len = 0;

	if ((prot & GIT_PROT_WRITE) && ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)) {
246
		git_error_set(GIT_ERROR_OS, "trying to map shared-writeable");
247 248 249
		return -1;
	}

250
	out->data = git__malloc(len);
251
	GIT_ERROR_CHECK_ALLOC(out->data);
252

253 254 255
	if (!git__is_ssizet(len) ||
		(p_lseek(fd, offset, SEEK_SET) < 0) ||
		(p_read(fd, out->data, len) != (ssize_t)len)) {
256
		git_error_set(GIT_ERROR_OS, "mmap emulation failed");
257 258 259 260 261 262 263 264 265 266
		return -1;
	}

	out->len = len;
	return 0;
}

int p_munmap(git_map *map)
{
	assert(map != NULL);
267
	git__free(map->data);
268 269 270 271 272

	return 0;
}

#endif