aboutsummaryrefslogtreecommitdiff
path: root/git2d/main.c
blob: fe439ca3de333065147607d7f9caaf638d981b9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

/*-
 * SPDX-License-Identifier: AGPL-3.0-only
 * SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
 */

#include <err.h>
#include <errno.h>
#include <git2.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "bare.h"

typedef struct {
	int fd;
} conn_io_t;

static bare_error
conn_read(void *buffer, void *dst, uint64_t sz)
{
	conn_io_t *io = buffer;
	ssize_t rsz = read(io->fd, dst, sz);
	return (rsz == (ssize_t)sz) ? BARE_ERROR_NONE : BARE_ERROR_READ_FAILED;
}

void *
session(void *_conn)
{
	int		conn = *(int *)_conn;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
	int		ret = 0;
#pragma GCC diagnostic pop

	int		err;
	git_repository *repo = NULL;

	char path[4096];
	conn_io_t io = {.fd = conn};
	struct bare_reader reader = {
		.buffer = &io,
		.read = conn_read,
	};

	err = bare_get_data(&reader, (uint8_t *)path, sizeof(path) - 1);
	if (err != BARE_ERROR_NONE) {
		ret = 10;
		goto close;
	}
	path[sizeof(path) - 1] = '\0';

	err = git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_NO_SEARCH | GIT_REPOSITORY_OPEN_BARE | GIT_REPOSITORY_OPEN_NO_DOTGIT, NULL);
	if (err != 0) {
		ret = 1;
		goto close;
	}

	git_object     *obj = NULL;
	err = git_revparse_single(&obj, repo, "HEAD^{tree}");
	if (err != 0) {
		ret = 2;
		goto free_repo;
	}
	git_tree       *tree = (git_tree *) obj;

	git_tree_entry *entry = NULL;
	err = git_tree_entry_bypath(&entry, tree, "README.md");
	if (err != 0) {
		ret = 3;
		goto free_tree;
	}

	git_otype	objtype = git_tree_entry_type(entry);
	if (objtype != GIT_OBJECT_BLOB) {
		ret = 4;
		goto free_tree_entry;
	}

	git_object     *obj2 = NULL;
	err = git_tree_entry_to_object(&obj2, repo, entry);
	if (err != 0) {
		ret = 5;
		goto free_tree_entry;
	}

	git_blob       *blob = (git_blob *) obj2;
	const void     *content = git_blob_rawcontent(blob);
	if (content == NULL) {
		ret = 6;
		goto free_blob;
	}
	write(conn, content, git_blob_rawsize(blob));

free_blob:
	git_blob_free(blob);
free_tree_entry:
	git_tree_entry_free(entry);
free_tree:
	git_tree_free(tree);
free_repo:
	git_repository_free(repo);

close:
	close(conn);
	free((int *)_conn);

	/* TODO: Handle ret */

	return NULL;

	/* TODO: Actually use it properly */
	if (0)
		goto close;
}

int
main(void)
{
	git_libgit2_init();

	int		sock;
	if ((sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0)
		err(1, "socket");

	struct sockaddr_un addr;
	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strcpy(addr.sun_path, "/home/runxiyu/Lindenii/forge/git2d.sock");

	if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un))) {
		if (errno == EADDRINUSE) {
			unlink("/home/runxiyu/Lindenii/forge/git2d.sock");
			if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)))
				err(1, "bind");
		} else {
			err(1, "bind");
		}
	}

	listen(sock, 0);

	for (;;) {
		int		*conn = malloc(sizeof(int));
		if (conn == NULL)
			err(1, "malloc");

		*conn = accept(sock, 0, 0);
		if (*conn == -1)
			err(1, "accept");

		pthread_t	thread;

		pthread_create(&thread, NULL, session, (void *)conn);
	}

	close(sock);

	git_libgit2_shutdown();
}