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

/* refs */
static FILE *info_ref_fp;

10
static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
J
Junio C Hamano 已提交
11
{
12
	struct object *o = parse_object(sha1);
13 14
	if (!o)
		return -1;
15

J
Junio C Hamano 已提交
16
	fprintf(info_ref_fp, "%s	%s\n", sha1_to_hex(sha1), path);
17
	if (o->type == OBJ_TAG) {
18 19 20 21
		o = deref_tag(o, path, 0);
		if (o)
			fprintf(info_ref_fp, "%s	%s^{}\n",
				sha1_to_hex(o->sha1), path);
22
	}
J
Junio C Hamano 已提交
23 24 25 26 27
	return 0;
}

static int update_info_refs(int force)
{
28
	char *path0 = xstrdup(git_path("info/refs"));
J
Junio C Hamano 已提交
29 30 31 32 33 34 35 36 37
	int len = strlen(path0);
	char *path1 = xmalloc(len + 2);

	strcpy(path1, path0);
	strcpy(path1 + len, "+");

	safe_create_leading_directories(path0);
	info_ref_fp = fopen(path1, "w");
	if (!info_ref_fp)
38
		return error("unable to update %s", path1);
39
	for_each_ref(add_info_ref, NULL);
J
Junio C Hamano 已提交
40
	fclose(info_ref_fp);
41
	adjust_shared_perm(path1);
J
Junio C Hamano 已提交
42 43 44 45 46 47 48
	rename(path1, path0);
	free(path0);
	free(path1);
	return 0;
}

/* packs */
L
Linus Torvalds 已提交
49
static struct pack_info {
J
Junio C Hamano 已提交
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
	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 {
84
		/* The file describes a pack that is no longer here */
J
Junio C Hamano 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		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;

	fp = fopen(infofile, "r");
	if (!fp)
P
Pavel Roskin 已提交
100
		return 1; /* nonexistent is not an error. */
J
Junio C Hamano 已提交
101 102 103 104

	while (fgets(line, sizeof(line), fp)) {
		int len = strlen(line);
		if (line[len-1] == '\n')
J
Junio C Hamano 已提交
105 106 107 108
			line[--len] = 0;

		if (!len)
			continue;
J
Junio C Hamano 已提交
109 110 111 112 113 114

		switch (line[0]) {
		case 'P': /* P name */
			if (parse_pack_def(line, old_cnt++))
				goto out_stale;
			break;
115 116
		case 'D': /* we used to emit D but that was misguided. */
			goto out_stale;
J
Junio C Hamano 已提交
117
			break;
118 119
		case 'T': /* we used to emit T but nobody uses it. */
			goto out_stale;
J
Junio C Hamano 已提交
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
			break;
		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_)
{
	struct pack_info * const* a = a_;
	struct pack_info * const* b = b_;

	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;

148
	/* then it does not matter but at least keep the comparison stable */
149 150 151 152 153 154
	if ((*a)->p == (*b)->p)
		return 0;
	else if ((*a)->p < (*b)->p)
		return -1;
	else
		return 1;
J
Junio C Hamano 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}

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();
	for (p = packed_git; p; p = p->next) {
		/* we ignore things on alternate path since they are
		 * not available to the pullers in general.
		 */
171
		if (!p->pack_local)
J
Junio C Hamano 已提交
172 173 174 175 176 177
			continue;
		i++;
	}
	num_pack = i;
	info = xcalloc(num_pack, sizeof(struct pack_info *));
	for (i = 0, p = packed_git; p; p = p->next) {
178
		if (!p->pack_local)
J
Junio C Hamano 已提交
179
			continue;
180
		info[i] = xcalloc(1, sizeof(struct pack_info));
J
Junio C Hamano 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
		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;
		}
	}

198
	/* renumber them */
J
Junio C Hamano 已提交
199 200 201 202 203 204 205
	qsort(info, num_pack, sizeof(info[0]), compare_info);
	for (i = 0; i < num_pack; i++)
		info[i]->new_num = i;
}

static void write_pack_info_file(FILE *fp)
{
206
	int i;
J
Junio C Hamano 已提交
207 208
	for (i = 0; i < num_pack; i++)
		fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
209
	fputc('\n', fp);
J
Junio C Hamano 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
}

static int update_info_packs(int force)
{
	char infofile[PATH_MAX];
	char name[PATH_MAX];
	int namelen;
	FILE *fp;

	namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
	strcpy(name, infofile);
	strcpy(name + namelen, "+");

	init_pack_info(infofile, force);

	safe_create_leading_directories(name);
	fp = fopen(name, "w");
	if (!fp)
		return error("cannot open %s", name);
	write_pack_info_file(fp);
	fclose(fp);
231
	adjust_shared_perm(name);
J
Junio C Hamano 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	rename(name, infofile);
	return 0;
}

/* 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);

248 249 250
	/* remove leftover rev-cache file if there is any */
	unlink(git_path("info/rev-cache"));

J
Junio C Hamano 已提交
251 252
	return errs;
}