server-info.c 5.8 KB
Newer Older
J
Junio C Hamano 已提交
1
#include "cache.h"
2
#include "repository.h"
J
Junio C Hamano 已提交
3 4 5
#include "refs.h"
#include "object.h"
#include "commit.h"
6
#include "tag.h"
7
#include "packfile.h"
8
#include "object-store.h"
J
Junio C Hamano 已提交
9

J
Jeff King 已提交
10 11 12 13 14 15 16 17 18 19
/*
 * Create the file "path" by writing to a temporary file and renaming
 * it into place. The contents of the file come from "generate", which
 * should return non-zero if it encounters an error.
 */
static int update_info_file(char *path, int (*generate)(FILE *))
{
	char *tmp = mkpathdup("%s_XXXXXX", path);
	int ret = -1;
	int fd = -1;
20
	FILE *fp = NULL, *to_close;
J
Jeff King 已提交
21 22

	safe_create_leading_directories(path);
23
	fd = git_mkstemp_mode(tmp, 0666);
J
Jeff King 已提交
24 25
	if (fd < 0)
		goto out;
26
	to_close = fp = fdopen(fd, "w");
J
Jeff King 已提交
27 28
	if (!fp)
		goto out;
29
	fd = -1;
J
Jeff King 已提交
30 31 32
	ret = generate(fp);
	if (ret)
		goto out;
33 34
	fp = NULL;
	if (fclose(to_close))
J
Jeff King 已提交
35 36 37 38 39 40 41 42 43
		goto out;
	if (adjust_shared_perm(tmp) < 0)
		goto out;
	if (rename(tmp, path) < 0)
		goto out;
	ret = 0;

out:
	if (ret) {
44
		error_errno("unable to update %s", path);
J
Jeff King 已提交
45 46 47 48 49 50 51 52 53
		if (fp)
			fclose(fp);
		else if (fd >= 0)
			close(fd);
		unlink(tmp);
	}
	free(tmp);
	return ret;
}
J
Junio C Hamano 已提交
54

55 56
static int add_info_ref(const char *path, const struct object_id *oid,
			int flag, void *cb_data)
J
Junio C Hamano 已提交
57
{
J
Jeff King 已提交
58
	FILE *fp = cb_data;
59
	struct object *o = parse_object(oid);
60 61
	if (!o)
		return -1;
62

63
	if (fprintf(fp, "%s	%s\n", oid_to_hex(oid), path) < 0)
J
Jeff King 已提交
64 65
		return -1;

66
	if (o->type == OBJ_TAG) {
67 68
		o = deref_tag(o, path, 0);
		if (o)
J
Jeff King 已提交
69
			if (fprintf(fp, "%s	%s^{}\n",
70
				oid_to_hex(&o->oid), path) < 0)
J
Jeff King 已提交
71
				return -1;
72
	}
J
Junio C Hamano 已提交
73 74 75
	return 0;
}

J
Jeff King 已提交
76 77
static int generate_info_refs(FILE *fp)
{
78
	return for_each_ref(add_info_ref, fp);
J
Jeff King 已提交
79 80
}

J
Junio C Hamano 已提交
81 82
static int update_info_refs(int force)
{
J
Jeff King 已提交
83 84 85 86
	char *path = git_pathdup("info/refs");
	int ret = update_info_file(path, generate_info_refs);
	free(path);
	return ret;
J
Junio C Hamano 已提交
87 88 89
}

/* packs */
L
Linus Torvalds 已提交
90
static struct pack_info {
J
Junio C Hamano 已提交
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
	struct packed_git *p;
	int old_num;
	int new_num;
	int nr_alloc;
	int nr_heads;
	unsigned char (*head)[20];
} **info;
static int num_pack;
static const char *objdir;
static int objdirlen;

static struct pack_info *find_pack_by_name(const char *name)
{
	int i;
	for (i = 0; i < num_pack; i++) {
		struct packed_git *p = info[i]->p;
		/* skip "/pack/" after ".git/objects" */
		if (!strcmp(p->pack_name + objdirlen + 6, name))
			return info[i];
	}
	return NULL;
}

/* Returns non-zero when we detect that the info in the
 * old file is useless.
 */
static int parse_pack_def(const char *line, int old_cnt)
{
	struct pack_info *i = find_pack_by_name(line + 2);
	if (i) {
		i->old_num = old_cnt;
		return 0;
	}
	else {
125
		/* The file describes a pack that is no longer here */
J
Junio C Hamano 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138
		return 1;
	}
}

/* Returns non-zero when we detect that the info in the
 * old file is useless.
 */
static int read_pack_info_file(const char *infofile)
{
	FILE *fp;
	char line[1000];
	int old_cnt = 0;

139
	fp = fopen_or_warn(infofile, "r");
J
Junio C Hamano 已提交
140
	if (!fp)
P
Pavel Roskin 已提交
141
		return 1; /* nonexistent is not an error. */
J
Junio C Hamano 已提交
142 143 144

	while (fgets(line, sizeof(line), fp)) {
		int len = strlen(line);
145
		if (len && line[len-1] == '\n')
J
Junio C Hamano 已提交
146 147 148 149
			line[--len] = 0;

		if (!len)
			continue;
J
Junio C Hamano 已提交
150 151 152 153 154 155

		switch (line[0]) {
		case 'P': /* P name */
			if (parse_pack_def(line, old_cnt++))
				goto out_stale;
			break;
156
		case 'D': /* we used to emit D but that was misguided. */
157 158
		case 'T': /* we used to emit T but nobody uses it. */
			goto out_stale;
J
Junio C Hamano 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172
		default:
			error("unrecognized: %s", line);
			break;
		}
	}
	fclose(fp);
	return 0;
 out_stale:
	fclose(fp);
	return 1;
}

static int compare_info(const void *a_, const void *b_)
{
173 174
	struct pack_info *const *a = a_;
	struct pack_info *const *b = b_;
J
Junio C Hamano 已提交
175 176 177 178 179 180 181 182 183 184 185

	if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
		/* Keep the order in the original */
		return (*a)->old_num - (*b)->old_num;
	else if (0 <= (*a)->old_num)
		/* Only A existed in the original so B is obviously newer */
		return -1;
	else if (0 <= (*b)->old_num)
		/* The other way around. */
		return 1;

186
	/* then it does not matter but at least keep the comparison stable */
187 188 189 190 191 192
	if ((*a)->p == (*b)->p)
		return 0;
	else if ((*a)->p < (*b)->p)
		return -1;
	else
		return 1;
J
Junio C Hamano 已提交
193 194 195 196 197 198 199 200 201 202 203 204
}

static void init_pack_info(const char *infofile, int force)
{
	struct packed_git *p;
	int stale;
	int i = 0;

	objdir = get_object_directory();
	objdirlen = strlen(objdir);

	prepare_packed_git();
205
	for (p = get_packed_git(the_repository); p; p = p->next) {
J
Junio C Hamano 已提交
206 207 208
		/* we ignore things on alternate path since they are
		 * not available to the pullers in general.
		 */
209
		if (!p->pack_local)
J
Junio C Hamano 已提交
210 211 212 213 214
			continue;
		i++;
	}
	num_pack = i;
	info = xcalloc(num_pack, sizeof(struct pack_info *));
215
	for (i = 0, p = get_packed_git(the_repository); p; p = p->next) {
216
		if (!p->pack_local)
J
Junio C Hamano 已提交
217
			continue;
218
		info[i] = xcalloc(1, sizeof(struct pack_info));
J
Junio C Hamano 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
		info[i]->p = p;
		info[i]->old_num = -1;
		i++;
	}

	if (infofile && !force)
		stale = read_pack_info_file(infofile);
	else
		stale = 1;

	for (i = 0; i < num_pack; i++) {
		if (stale) {
			info[i]->old_num = -1;
			info[i]->nr_heads = 0;
		}
	}

236
	/* renumber them */
R
René Scharfe 已提交
237
	QSORT(info, num_pack, compare_info);
J
Junio C Hamano 已提交
238 239 240 241
	for (i = 0; i < num_pack; i++)
		info[i]->new_num = i;
}

242 243 244 245 246 247 248 249
static void free_pack_info(void)
{
	int i;
	for (i = 0; i < num_pack; i++)
		free(info[i]);
	free(info);
}

J
Jeff King 已提交
250
static int write_pack_info_file(FILE *fp)
J
Junio C Hamano 已提交
251
{
252
	int i;
J
Jeff King 已提交
253 254 255 256 257 258 259
	for (i = 0; i < num_pack; i++) {
		if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
			return -1;
	}
	if (fputc('\n', fp) == EOF)
		return -1;
	return 0;
J
Junio C Hamano 已提交
260 261 262 263
}

static int update_info_packs(int force)
{
J
Jeff King 已提交
264 265
	char *infofile = mkpathdup("%s/info/packs", get_object_directory());
	int ret;
J
Junio C Hamano 已提交
266 267

	init_pack_info(infofile, force);
J
Jeff King 已提交
268
	ret = update_info_file(infofile, write_pack_info_file);
269
	free_pack_info();
J
Jeff King 已提交
270 271
	free(infofile);
	return ret;
J
Junio C Hamano 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285
}

/* public */
int update_server_info(int force)
{
	/* We would add more dumb-server support files later,
	 * including index of available pack files and their
	 * intended audiences.
	 */
	int errs = 0;

	errs = errs | update_info_refs(force);
	errs = errs | update_info_packs(force);

286
	/* remove leftover rev-cache file if there is any */
287
	unlink_or_warn(git_path("info/rev-cache"));
288

J
Junio C Hamano 已提交
289 290
	return errs;
}