object: convert parse_object* to take struct object_id
[git/git.git] / server-info.c
CommitLineData
8f3f9b09
JH
1#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "commit.h"
b614e3d7 5#include "tag.h"
8f3f9b09 6
d38379ec
JK
7/*
8 * Create the file "path" by writing to a temporary file and renaming
9 * it into place. The contents of the file come from "generate", which
10 * should return non-zero if it encounters an error.
11 */
12static int update_info_file(char *path, int (*generate)(FILE *))
13{
14 char *tmp = mkpathdup("%s_XXXXXX", path);
15 int ret = -1;
16 int fd = -1;
fa1912c8 17 FILE *fp = NULL, *to_close;
d38379ec
JK
18
19 safe_create_leading_directories(path);
d91175b2 20 fd = git_mkstemp_mode(tmp, 0666);
d38379ec
JK
21 if (fd < 0)
22 goto out;
fa1912c8 23 to_close = fp = fdopen(fd, "w");
d38379ec
JK
24 if (!fp)
25 goto out;
fa1912c8 26 fd = -1;
d38379ec
JK
27 ret = generate(fp);
28 if (ret)
29 goto out;
fa1912c8
RS
30 fp = NULL;
31 if (fclose(to_close))
d38379ec
JK
32 goto out;
33 if (adjust_shared_perm(tmp) < 0)
34 goto out;
35 if (rename(tmp, path) < 0)
36 goto out;
37 ret = 0;
38
39out:
40 if (ret) {
02382f51 41 error_errno("unable to update %s", path);
d38379ec
JK
42 if (fp)
43 fclose(fp);
44 else if (fd >= 0)
45 close(fd);
46 unlink(tmp);
47 }
48 free(tmp);
49 return ret;
50}
8f3f9b09 51
e2b0bcdf
MH
52static int add_info_ref(const char *path, const struct object_id *oid,
53 int flag, void *cb_data)
8f3f9b09 54{
d38379ec 55 FILE *fp = cb_data;
c251c83d 56 struct object *o = parse_object(oid);
76f8a302
SP
57 if (!o)
58 return -1;
f6b42a81 59
e2b0bcdf 60 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
d38379ec
JK
61 return -1;
62
1974632c 63 if (o->type == OBJ_TAG) {
9534f40b
JH
64 o = deref_tag(o, path, 0);
65 if (o)
d38379ec 66 if (fprintf(fp, "%s %s^{}\n",
f2fd0760 67 oid_to_hex(&o->oid), path) < 0)
d38379ec 68 return -1;
f6b42a81 69 }
8f3f9b09
JH
70 return 0;
71}
72
d38379ec
JK
73static int generate_info_refs(FILE *fp)
74{
e2b0bcdf 75 return for_each_ref(add_info_ref, fp);
d38379ec
JK
76}
77
8f3f9b09
JH
78static int update_info_refs(int force)
79{
d38379ec
JK
80 char *path = git_pathdup("info/refs");
81 int ret = update_info_file(path, generate_info_refs);
82 free(path);
83 return ret;
8f3f9b09
JH
84}
85
86/* packs */
4d8fa916 87static struct pack_info {
8f3f9b09
JH
88 struct packed_git *p;
89 int old_num;
90 int new_num;
91 int nr_alloc;
92 int nr_heads;
93 unsigned char (*head)[20];
8f3f9b09
JH
94} **info;
95static int num_pack;
96static const char *objdir;
97static int objdirlen;
98
8f3f9b09
JH
99static struct pack_info *find_pack_by_name(const char *name)
100{
101 int i;
102 for (i = 0; i < num_pack; i++) {
103 struct packed_git *p = info[i]->p;
104 /* skip "/pack/" after ".git/objects" */
105 if (!strcmp(p->pack_name + objdirlen + 6, name))
106 return info[i];
107 }
108 return NULL;
109}
110
8f3f9b09
JH
111/* Returns non-zero when we detect that the info in the
112 * old file is useless.
113 */
114static int parse_pack_def(const char *line, int old_cnt)
115{
116 struct pack_info *i = find_pack_by_name(line + 2);
117 if (i) {
118 i->old_num = old_cnt;
119 return 0;
120 }
121 else {
6f42f89c 122 /* The file describes a pack that is no longer here */
8f3f9b09
JH
123 return 1;
124 }
125}
126
8f3f9b09
JH
127/* Returns non-zero when we detect that the info in the
128 * old file is useless.
129 */
130static int read_pack_info_file(const char *infofile)
131{
132 FILE *fp;
133 char line[1000];
134 int old_cnt = 0;
135
136 fp = fopen(infofile, "r");
137 if (!fp)
addf88e4 138 return 1; /* nonexistent is not an error. */
8f3f9b09
JH
139
140 while (fgets(line, sizeof(line), fp)) {
141 int len = strlen(line);
872c930d 142 if (len && line[len-1] == '\n')
8ac4838a
JH
143 line[--len] = 0;
144
145 if (!len)
146 continue;
8f3f9b09
JH
147
148 switch (line[0]) {
149 case 'P': /* P name */
150 if (parse_pack_def(line, old_cnt++))
151 goto out_stale;
152 break;
6f42f89c 153 case 'D': /* we used to emit D but that was misguided. */
3e15c67c
JH
154 case 'T': /* we used to emit T but nobody uses it. */
155 goto out_stale;
8f3f9b09
JH
156 default:
157 error("unrecognized: %s", line);
158 break;
159 }
160 }
161 fclose(fp);
162 return 0;
163 out_stale:
164 fclose(fp);
165 return 1;
166}
167
8f3f9b09
JH
168static int compare_info(const void *a_, const void *b_)
169{
4b25d091
FC
170 struct pack_info *const *a = a_;
171 struct pack_info *const *b = b_;
8f3f9b09
JH
172
173 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
174 /* Keep the order in the original */
175 return (*a)->old_num - (*b)->old_num;
176 else if (0 <= (*a)->old_num)
177 /* Only A existed in the original so B is obviously newer */
178 return -1;
179 else if (0 <= (*b)->old_num)
180 /* The other way around. */
181 return 1;
182
d5eac498 183 /* then it does not matter but at least keep the comparison stable */
2dee5816
JH
184 if ((*a)->p == (*b)->p)
185 return 0;
186 else if ((*a)->p < (*b)->p)
187 return -1;
188 else
189 return 1;
8f3f9b09
JH
190}
191
192static void init_pack_info(const char *infofile, int force)
193{
194 struct packed_git *p;
195 int stale;
196 int i = 0;
8f3f9b09
JH
197
198 objdir = get_object_directory();
199 objdirlen = strlen(objdir);
200
201 prepare_packed_git();
202 for (p = packed_git; p; p = p->next) {
203 /* we ignore things on alternate path since they are
204 * not available to the pullers in general.
205 */
f13d7db4 206 if (!p->pack_local)
8f3f9b09
JH
207 continue;
208 i++;
209 }
210 num_pack = i;
211 info = xcalloc(num_pack, sizeof(struct pack_info *));
212 for (i = 0, p = packed_git; p; p = p->next) {
f13d7db4 213 if (!p->pack_local)
8f3f9b09 214 continue;
6f42f89c 215 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b09
JH
216 info[i]->p = p;
217 info[i]->old_num = -1;
218 i++;
219 }
220
221 if (infofile && !force)
222 stale = read_pack_info_file(infofile);
223 else
224 stale = 1;
225
226 for (i = 0; i < num_pack; i++) {
227 if (stale) {
228 info[i]->old_num = -1;
8f3f9b09
JH
229 info[i]->nr_heads = 0;
230 }
8f3f9b09
JH
231 }
232
6f42f89c 233 /* renumber them */
9ed0d8d6 234 QSORT(info, num_pack, compare_info);
8f3f9b09
JH
235 for (i = 0; i < num_pack; i++)
236 info[i]->new_num = i;
8f3f9b09
JH
237}
238
3907a407
JK
239static void free_pack_info(void)
240{
241 int i;
242 for (i = 0; i < num_pack; i++)
243 free(info[i]);
244 free(info);
245}
246
d38379ec 247static int write_pack_info_file(FILE *fp)
8f3f9b09 248{
3e15c67c 249 int i;
d38379ec
JK
250 for (i = 0; i < num_pack; i++) {
251 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
252 return -1;
253 }
254 if (fputc('\n', fp) == EOF)
255 return -1;
256 return 0;
8f3f9b09
JH
257}
258
259static int update_info_packs(int force)
260{
d38379ec
JK
261 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
262 int ret;
8f3f9b09
JH
263
264 init_pack_info(infofile, force);
d38379ec 265 ret = update_info_file(infofile, write_pack_info_file);
3907a407 266 free_pack_info();
d38379ec
JK
267 free(infofile);
268 return ret;
8f3f9b09
JH
269}
270
8f3f9b09
JH
271/* public */
272int update_server_info(int force)
273{
274 /* We would add more dumb-server support files later,
275 * including index of available pack files and their
276 * intended audiences.
277 */
278 int errs = 0;
279
280 errs = errs | update_info_refs(force);
281 errs = errs | update_info_packs(force);
8f3f9b09 282
6f42f89c 283 /* remove leftover rev-cache file if there is any */
691f1a28 284 unlink_or_warn(git_path("info/rev-cache"));
6f42f89c 285
8f3f9b09
JH
286 return errs;
287}