posix.c 3.75 KB
Newer Older
Vicent Marti committed
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
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.
 */
Vicent Marti committed
7 8 9 10 11 12
#include "common.h"
#include "posix.h"
#include "path.h"
#include <stdio.h>
#include <ctype.h>

13 14
#ifndef GIT_WIN32

Vicent Marti committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#ifdef NO_ADDRINFO
int p_getaddrinfo(
	const char *host,
	const char *port,
	struct addrinfo *hints,
	struct addrinfo **info)
{
	GIT_UNUSED(hints);
	
	struct addrinfo *ainfo, *ai;
	int p = 0;
	
	if ((ainfo = malloc(sizeof(struct addrinfo))) == NULL)
		return -1;
		
	if ((ainfo->ai_hostent = gethostbyname(host)) == NULL)
		return -2;
		
	ainfo->ai_servent = getservbyname(port, 0);
	
	if (ainfo->ai_servent)
		ainfo->ai_port = ainfo->ai_servent->s_port;
	else
		ainfo->ai_port = atol(port);

	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;
	
	if (ainfo->ai_hostent->h_addr_list[1] == NULL) {
		ainfo->ai_next = NULL;
		return 0;
	}
	
	ai = ainfo;
	
	for (p = 1; ainfo->ai_hostent->h_addr_list[p] != NULL; p++) {
		ai->ai_next = malloc(sizeof(struct addrinfo));
		memcpy(&ai->ai_next, ainfo, sizeof(struct addrinfo));
		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;
	}
	
	ai->ai_next = NULL;
	return 0;
}

void p_freeaddrinfo(struct addrinfo *info)
{
	struct addrinfo *p, *next;
	
	p = info;
	
	while(p != NULL) {
		next = p->ai_next;
		free(p);
		p = next;
	}
}

const char *p_gai_strerror(int ret)
{
	switch(ret) {
		case -1:
			return "Out of memory";
		break;
		
		case -2:
			return "Address lookup failed";
		break;
		
		default:
			return "Unknown error";
		break;
	}
}
#endif /* NO_ADDRINFO */

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

	if (flags & O_CREAT)
	{
		va_list arg_list;

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

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

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

127 128 129 130 131 132 133 134 135
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)
136
		return -1;
137 138

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

141
	return 0;
142 143
}

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

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

154
	return -1;
155 156
}

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

Vicent Marti committed
159 160 161 162
int p_read(git_file fd, void *buf, size_t cnt)
{
	char *b = buf;
	while (cnt) {
163 164 165 166 167 168 169
		ssize_t r;
#ifdef GIT_WIN32
		assert((size_t)((unsigned int)cnt) == cnt);
		r = read(fd, b, (unsigned int)cnt);
#else
		r = read(fd, b, cnt);
#endif
Vicent Marti committed
170 171 172
		if (r < 0) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
173
			return -1;
Vicent Marti committed
174 175 176 177 178 179 180 181 182
		}
		if (!r)
			break;
		cnt -= r;
		b += r;
	}
	return (int)(b - (char *)buf);
}

183
int p_write(git_file fd, const void *buf, size_t cnt)
Vicent Marti committed
184
{
185
	const char *b = buf;
Vicent Marti committed
186
	while (cnt) {
187 188 189 190 191 192 193
		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
194 195 196
		if (r < 0) {
			if (errno == EINTR || errno == EAGAIN)
				continue;
197
			return -1;
Vicent Marti committed
198 199 200
		}
		if (!r) {
			errno = EPIPE;
201
			return -1;
Vicent Marti committed
202 203 204 205
		}
		cnt -= r;
		b += r;
	}
206
	return 0;
Vicent Marti committed
207
}