smart.c 9.87 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#include "git2.h"
#include "smart.h"
#include "refs.h"
10
#include "refspec.h"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

static int git_smart__recv_cb(gitno_buffer *buf)
{
	transport_smart *t = (transport_smart *) buf->cb_data;
	size_t old_len, bytes_read;
	int error;

	assert(t->current_stream);

	old_len = buf->offset;

	if ((error = t->current_stream->read(t->current_stream, buf->data + buf->offset, buf->len - buf->offset, &bytes_read)) < 0)
		return error;

	buf->offset += bytes_read;

27 28 29
	if (t->packetsize_cb && !t->cancelled.val) {
		error = t->packetsize_cb(bytes_read, t->packetsize_payload);
		if (error) {
30
			git_atomic_set(&t->cancelled, 1);
31
			return GIT_EUSER;
32
		}
33
	}
34 35 36 37

	return (int)(buf->offset - old_len);
}

38
GIT_INLINE(int) git_smart__reset_stream(transport_smart *t, bool close_subtransport)
39 40 41 42 43
{
	if (t->current_stream) {
		t->current_stream->free(t->current_stream);
		t->current_stream = NULL;
	}
44 45 46 47 48 49

	if (close_subtransport &&
		t->wrapped->close(t->wrapped) < 0)
		return -1;

	return 0;
50 51 52 53 54 55
}

static int git_smart__set_callbacks(
	git_transport *transport,
	git_transport_message_cb progress_cb,
	git_transport_message_cb error_cb,
56
	git_transport_certificate_check_cb certificate_check_cb,
57 58 59 60 61 62
	void *message_cb_payload)
{
	transport_smart *t = (transport_smart *)transport;

	t->progress_cb = progress_cb;
	t->error_cb = error_cb;
63
	t->certificate_check_cb = certificate_check_cb;
64 65 66 67 68
	t->message_cb_payload = message_cb_payload;

	return 0;
}

69
int git_smart__update_heads(transport_smart *t, git_vector *symrefs)
70 71 72 73 74 75 76 77 78 79
{
	size_t i;
	git_pkt *pkt;

	git_vector_clear(&t->heads);
	git_vector_foreach(&t->refs, i, pkt) {
		git_pkt_ref *ref = (git_pkt_ref *) pkt;
		if (pkt->type != GIT_PKT_REF)
			continue;

80 81 82 83
		if (symrefs) {
			git_refspec *spec;
			git_buf buf = GIT_BUF_INIT;
			size_t j;
84
			int error = 0;
85 86 87 88 89 90 91 92 93 94 95 96 97 98

			git_vector_foreach(symrefs, j, spec) {
				git_buf_clear(&buf);
				if (git_refspec_src_matches(spec, ref->head.name) &&
				    !(error = git_refspec_transform(&buf, spec, ref->head.name)))
					ref->head.symref_target = git_buf_detach(&buf);
			}

			git_buf_free(&buf);

			if (error < 0)
				return error;
		}

99 100 101 102 103 104 105
		if (git_vector_insert(&t->heads, &ref->head) < 0)
			return -1;
	}

	return 0;
}

106 107 108 109 110 111 112 113 114 115 116 117 118
static void free_symrefs(git_vector *symrefs)
{
	git_refspec *spec;
	size_t i;

	git_vector_foreach(symrefs, i, spec) {
		git_refspec__free(spec);
		git__free(spec);
	}

	git_vector_free(symrefs);
}

119 120 121 122
static int git_smart__connect(
	git_transport *transport,
	const char *url,
	git_cred_acquire_cb cred_acquire_cb,
123
	void *cred_acquire_payload,
124 125
	int direction,
	int flags)
126 127 128 129 130
{
	transport_smart *t = (transport_smart *)transport;
	git_smart_subtransport_stream *stream;
	int error;
	git_pkt *pkt;
131
	git_pkt_ref *first;
132
	git_vector symrefs;
133
	git_smart_service_t service;
134

135 136
	if (git_smart__reset_stream(t, true) < 0)
		return -1;
137 138 139 140 141 142

	t->url = git__strdup(url);
	GITERR_CHECK_ALLOC(t->url);

	t->direction = direction;
	t->flags = flags;
143
	t->cred_acquire_cb = cred_acquire_cb;
144
	t->cred_acquire_payload = cred_acquire_payload;
145

146 147 148 149 150 151 152 153
	if (GIT_DIRECTION_FETCH == t->direction)
		service = GIT_SERVICE_UPLOADPACK_LS;
	else if (GIT_DIRECTION_PUSH == t->direction)
		service = GIT_SERVICE_RECEIVEPACK_LS;
	else {
		giterr_set(GITERR_NET, "Invalid direction");
		return -1;
	}
154

155 156
	if ((error = t->wrapped->action(&stream, t->wrapped, t->url, service)) < 0)
		return error;
157

158 159
	/* Save off the current stream (i.e. socket) that we are working with */
	t->current_stream = stream;
160

161
	gitno_buffer_setup_callback(&t->buffer, t->buffer_data, sizeof(t->buffer_data), git_smart__recv_cb, t);
162

163 164 165 166 167 168 169
	/* 2 flushes for RPC; 1 for stateful */
	if ((error = git_smart__store_refs(t, t->rpc ? 2 : 1)) < 0)
		return error;

	/* Strip the comment packet for RPC */
	if (t->rpc) {
		pkt = (git_pkt *)git_vector_get(&t->refs, 0);
170

171 172 173 174 175 176 177 178
		if (!pkt || GIT_PKT_COMMENT != pkt->type) {
			giterr_set(GITERR_NET, "Invalid response");
			return -1;
		} else {
			/* Remove the comment pkt from the list */
			git_vector_remove(&t->refs, 0);
			git__free(pkt);
		}
179
	}
180 181 182 183 184 185

	/* We now have loaded the refs. */
	t->have_refs = 1;

	first = (git_pkt_ref *)git_vector_get(&t->refs, 0);

186 187 188
	if ((error = git_vector_init(&symrefs, 1, NULL)) < 0)
		return error;

189
	/* Detect capabilities */
190
	if (git_smart__detect_caps(first, &t->caps, &symrefs) < 0)
191
		return -1;
192 193 194 195 196 197

	/* If the only ref in the list is capabilities^{} with OID_ZERO, remove it */
	if (1 == t->refs.length && !strcmp(first->head.name, "capabilities^{}") &&
		git_oid_iszero(&first->head.oid)) {
		git_vector_clear(&t->refs);
		git_pkt_free((git_pkt *)first);
198 199
	}

200
	/* Keep a list of heads for _ls */
201 202 203
	git_smart__update_heads(t, &symrefs);

	free_symrefs(&symrefs);
204

205 206 207 208 209 210 211
	if (t->rpc && git_smart__reset_stream(t, false) < 0)
		return -1;

	/* We're now logically connected. */
	t->connected = 1;

	return 0;
212 213
}

214
static int git_smart__ls(const git_remote_head ***out, size_t *size, git_transport *transport)
215 216 217 218 219 220 221 222
{
	transport_smart *t = (transport_smart *)transport;

	if (!t->have_refs) {
		giterr_set(GITERR_NET, "The transport has not yet loaded the refs");
		return -1;
	}

223 224
	*out = (const git_remote_head **) t->heads.contents;
	*size = t->heads.length;
225 226 227 228 229 230 231 232 233 234

	return 0;
}

int git_smart__negotiation_step(git_transport *transport, void *data, size_t len)
{
	transport_smart *t = (transport_smart *)transport;
	git_smart_subtransport_stream *stream;
	int error;

235 236 237 238 239 240 241 242 243 244 245 246 247
	if (t->rpc && git_smart__reset_stream(t, false) < 0)
		return -1;

	if (GIT_DIRECTION_FETCH != t->direction) {
		giterr_set(GITERR_NET, "This operation is only valid for fetch");
		return -1;
	}

	if ((error = t->wrapped->action(&stream, t->wrapped, t->url, GIT_SERVICE_UPLOADPACK)) < 0)
		return error;

	/* If this is a stateful implementation, the stream we get back should be the same */
	assert(t->rpc || t->current_stream == stream);
248

249 250
	/* Save off the current stream (i.e. socket) that we are working with */
	t->current_stream = stream;
251

252 253
	if ((error = stream->write(stream, (const char *)data, len)) < 0)
		return error;
254

255
	gitno_buffer_setup_callback(&t->buffer, t->buffer_data, sizeof(t->buffer_data), git_smart__recv_cb, t);
256

257 258
	return 0;
}
259

260 261 262 263 264 265
int git_smart__get_push_stream(transport_smart *t, git_smart_subtransport_stream **stream)
{
	int error;

	if (t->rpc && git_smart__reset_stream(t, false) < 0)
		return -1;
266

267 268 269
	if (GIT_DIRECTION_PUSH != t->direction) {
		giterr_set(GITERR_NET, "This operation is only valid for push");
		return -1;
270 271
	}

272 273 274 275 276 277 278 279 280
	if ((error = t->wrapped->action(stream, t->wrapped, t->url, GIT_SERVICE_RECEIVEPACK)) < 0)
		return error;

	/* If this is a stateful implementation, the stream we get back should be the same */
	assert(t->rpc || t->current_stream == *stream);

	/* Save off the current stream (i.e. socket) that we are working with */
	t->current_stream = *stream;

281
	gitno_buffer_setup_callback(&t->buffer, t->buffer_data, sizeof(t->buffer_data), git_smart__recv_cb, t);
282 283

	return 0;
284 285 286 287 288 289 290 291 292
}

static void git_smart__cancel(git_transport *transport)
{
	transport_smart *t = (transport_smart *)transport;

	git_atomic_set(&t->cancelled, 1);
}

293
static int git_smart__is_connected(git_transport *transport)
294 295 296
{
	transport_smart *t = (transport_smart *)transport;

297
	return t->connected;
298 299 300 301 302 303 304 305 306 307 308 309 310 311
}

static int git_smart__read_flags(git_transport *transport, int *flags)
{
	transport_smart *t = (transport_smart *)transport;

	*flags = t->flags;

	return 0;
}

static int git_smart__close(git_transport *transport)
{
	transport_smart *t = (transport_smart *)transport;
312 313 314 315
	git_vector *common = &t->common;
	unsigned int i;
	git_pkt *p;
	int ret;
316 317 318 319 320 321 322 323 324 325 326 327
	git_smart_subtransport_stream *stream;
	const char flush[] = "0000";

	/*
	 * If we're still connected at this point and not using RPC,
	 * we should say goodbye by sending a flush, or git-daemon
	 * will complain that we disconnected unexpectedly.
	 */
	if (t->connected && !t->rpc &&
	    !t->wrapped->action(&stream, t->wrapped, t->url, GIT_SERVICE_UPLOADPACK)) {
		t->current_stream->write(t->current_stream, flush, 4);
	}
328 329 330 331 332 333 334 335 336 337 338 339

	ret = git_smart__reset_stream(t, true);

	git_vector_foreach(common, i, p)
		git_pkt_free(p);

	git_vector_free(common);

	if (t->url) {
		git__free(t->url);
		t->url = NULL;
	}
340 341 342

	t->connected = 0;

343
	return ret;
344 345 346 347 348
}

static void git_smart__free(git_transport *transport)
{
	transport_smart *t = (transport_smart *)transport;
349 350 351
	git_vector *refs = &t->refs;
	unsigned int i;
	git_pkt *p;
352 353 354 355 356 357 358

	/* Make sure that the current stream is closed, if we have one. */
	git_smart__close(transport);

	/* Free the subtransport */
	t->wrapped->free(t->wrapped);

359
	git_vector_free(&t->heads);
360 361 362 363 364
	git_vector_foreach(refs, i, p)
		git_pkt_free(p);

	git_vector_free(refs);

365 366 367
	git__free(t);
}

368 369 370 371 372 373 374
static int ref_name_cmp(const void *a, const void *b)
{
	const git_pkt_ref *ref_a = a, *ref_b = b;

	return strcmp(ref_a->head.name, ref_b->head.name);
}

375
int git_transport_smart(git_transport **out, git_remote *owner, void *param)
376 377 378 379 380 381 382
{
	transport_smart *t;
	git_smart_subtransport_definition *definition = (git_smart_subtransport_definition *)param;

	if (!param)
		return -1;

383
	t = git__calloc(sizeof(transport_smart), 1);
384 385
	GITERR_CHECK_ALLOC(t);

386
	t->parent.version = GIT_TRANSPORT_VERSION;
387 388 389 390 391 392
	t->parent.set_callbacks = git_smart__set_callbacks;
	t->parent.connect = git_smart__connect;
	t->parent.close = git_smart__close;
	t->parent.free = git_smart__free;
	t->parent.negotiate_fetch = git_smart__negotiate_fetch;
	t->parent.download_pack = git_smart__download_pack;
393
	t->parent.push = git_smart__push;
394 395 396 397
	t->parent.ls = git_smart__ls;
	t->parent.is_connected = git_smart__is_connected;
	t->parent.read_flags = git_smart__read_flags;
	t->parent.cancel = git_smart__cancel;
398

399
	t->owner = owner;
400 401
	t->rpc = definition->rpc;

402
	if (git_vector_init(&t->refs, 16, ref_name_cmp) < 0) {
403 404 405 406
		git__free(t);
		return -1;
	}

407 408 409 410 411
	if (git_vector_init(&t->heads, 16, ref_name_cmp) < 0) {
		git__free(t);
		return -1;
	}

412 413 414
	if (definition->callback(&t->wrapped, &t->parent) < 0) {
		git__free(t);
		return -1;
415
	}
416 417 418 419

	*out = (git_transport *) t;
	return 0;
}