]> git.ipfire.org Git - thirdparty/git.git/blame - server-info.c
log-tree: replace include of revision.h with simple forward declaration
[thirdparty/git.git] / server-info.c
CommitLineData
623b80be 1#include "git-compat-util.h"
36bf1958 2#include "alloc.h"
e941c48d 3#include "dir.h"
32a8f510 4#include "environment.h"
41771fa4 5#include "hex.h"
a80d72db 6#include "repository.h"
8f3f9b09
JH
7#include "refs.h"
8#include "object.h"
9#include "commit.h"
b614e3d7 10#include "tag.h"
0abe14f6 11#include "packfile.h"
87bed179 12#include "object-file.h"
a80d72db 13#include "object-store.h"
623b80be 14#include "server-info.h"
f4f476b6 15#include "strbuf.h"
d5ebb50d 16#include "wrapper.h"
f4f476b6
EW
17
18struct update_info_ctx {
19 FILE *cur_fp;
20 FILE *old_fp; /* becomes NULL if it differs from cur_fp */
21 struct strbuf cur_sb;
22 struct strbuf old_sb;
23};
24
25static void uic_mark_stale(struct update_info_ctx *uic)
26{
27 fclose(uic->old_fp);
28 uic->old_fp = NULL;
29}
30
31static int uic_is_stale(const struct update_info_ctx *uic)
32{
33 return uic->old_fp == NULL;
34}
35
48ca53ca 36__attribute__((format (printf, 2, 3)))
f4f476b6
EW
37static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
38{
39 va_list ap;
40 int ret = -1;
41
42 va_start(ap, fmt);
43
44 if (uic_is_stale(uic)) {
45 ret = vfprintf(uic->cur_fp, fmt, ap);
46 } else {
47 ssize_t r;
48 struct strbuf *cur = &uic->cur_sb;
49 struct strbuf *old = &uic->old_sb;
50
51 strbuf_reset(cur);
52 strbuf_vinsertf(cur, 0, fmt, ap);
53
54 strbuf_reset(old);
55 strbuf_grow(old, cur->len);
56 r = fread(old->buf, 1, cur->len, uic->old_fp);
57 if (r != cur->len || memcmp(old->buf, cur->buf, r))
58 uic_mark_stale(uic);
59
60 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
61 ret = 0;
62 }
63
64 va_end(ap);
65
66 return ret;
67}
8f3f9b09 68
d38379ec
JK
69/*
70 * Create the file "path" by writing to a temporary file and renaming
71 * it into place. The contents of the file come from "generate", which
72 * should return non-zero if it encounters an error.
73 */
f4f476b6
EW
74static int update_info_file(char *path,
75 int (*generate)(struct update_info_ctx *),
76 int force)
d38379ec
JK
77{
78 char *tmp = mkpathdup("%s_XXXXXX", path);
79 int ret = -1;
80 int fd = -1;
f4f476b6
EW
81 FILE *to_close;
82 struct update_info_ctx uic = {
83 .cur_fp = NULL,
84 .old_fp = NULL,
85 .cur_sb = STRBUF_INIT,
86 .old_sb = STRBUF_INIT
87 };
d38379ec
JK
88
89 safe_create_leading_directories(path);
d91175b2 90 fd = git_mkstemp_mode(tmp, 0666);
d38379ec
JK
91 if (fd < 0)
92 goto out;
f4f476b6
EW
93 to_close = uic.cur_fp = fdopen(fd, "w");
94 if (!uic.cur_fp)
d38379ec 95 goto out;
fa1912c8 96 fd = -1;
f4f476b6
EW
97
98 /* no problem on ENOENT and old_fp == NULL, it's stale, now */
99 if (!force)
100 uic.old_fp = fopen_or_warn(path, "r");
101
102 /*
15beaaa3 103 * uic_printf will compare incremental comparison against old_fp
f4f476b6
EW
104 * and mark uic as stale if needed
105 */
106 ret = generate(&uic);
d38379ec
JK
107 if (ret)
108 goto out;
f4f476b6
EW
109
110 /* new file may be shorter than the old one, check here */
111 if (!uic_is_stale(&uic)) {
112 struct stat st;
113 long new_len = ftell(uic.cur_fp);
114 int old_fd = fileno(uic.old_fp);
115
116 if (new_len < 0) {
117 ret = -1;
118 goto out;
119 }
120 if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len))
121 uic_mark_stale(&uic);
122 }
123
124 uic.cur_fp = NULL;
fa1912c8 125 if (fclose(to_close))
d38379ec 126 goto out;
f4f476b6
EW
127
128 if (uic_is_stale(&uic)) {
129 if (adjust_shared_perm(tmp) < 0)
130 goto out;
131 if (rename(tmp, path) < 0)
132 goto out;
133 } else {
134 unlink(tmp);
135 }
d38379ec
JK
136 ret = 0;
137
138out:
139 if (ret) {
02382f51 140 error_errno("unable to update %s", path);
f4f476b6
EW
141 if (uic.cur_fp)
142 fclose(uic.cur_fp);
d38379ec
JK
143 else if (fd >= 0)
144 close(fd);
145 unlink(tmp);
146 }
147 free(tmp);
f4f476b6
EW
148 if (uic.old_fp)
149 fclose(uic.old_fp);
150 strbuf_release(&uic.old_sb);
151 strbuf_release(&uic.cur_sb);
d38379ec
JK
152 return ret;
153}
8f3f9b09 154
e2b0bcdf 155static int add_info_ref(const char *path, const struct object_id *oid,
5cf88fd8 156 int flag UNUSED,
63e14ee2 157 void *cb_data)
8f3f9b09 158{
f4f476b6 159 struct update_info_ctx *uic = cb_data;
109cd76d 160 struct object *o = parse_object(the_repository, oid);
76f8a302
SP
161 if (!o)
162 return -1;
f6b42a81 163
f4f476b6 164 if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0)
d38379ec
JK
165 return -1;
166
1974632c 167 if (o->type == OBJ_TAG) {
a74093da 168 o = deref_tag(the_repository, o, path, 0);
9534f40b 169 if (o)
f4f476b6 170 if (uic_printf(uic, "%s %s^{}\n",
f2fd0760 171 oid_to_hex(&o->oid), path) < 0)
d38379ec 172 return -1;
f6b42a81 173 }
8f3f9b09
JH
174 return 0;
175}
176
f4f476b6 177static int generate_info_refs(struct update_info_ctx *uic)
d38379ec 178{
f4f476b6 179 return for_each_ref(add_info_ref, uic);
d38379ec
JK
180}
181
f4f476b6 182static int update_info_refs(int force)
8f3f9b09 183{
d38379ec 184 char *path = git_pathdup("info/refs");
f4f476b6 185 int ret = update_info_file(path, generate_info_refs, force);
d38379ec
JK
186 free(path);
187 return ret;
8f3f9b09
JH
188}
189
190/* packs */
4d8fa916 191static struct pack_info {
8f3f9b09
JH
192 struct packed_git *p;
193 int old_num;
194 int new_num;
8f3f9b09
JH
195} **info;
196static int num_pack;
8f3f9b09 197
8f3f9b09
JH
198static struct pack_info *find_pack_by_name(const char *name)
199{
200 int i;
201 for (i = 0; i < num_pack; i++) {
202 struct packed_git *p = info[i]->p;
b9fb142e 203 if (!strcmp(pack_basename(p), name))
8f3f9b09
JH
204 return info[i];
205 }
206 return NULL;
207}
208
8f3f9b09
JH
209/* Returns non-zero when we detect that the info in the
210 * old file is useless.
211 */
b83a3089 212static int parse_pack_def(const char *packname, int old_cnt)
8f3f9b09 213{
b83a3089 214 struct pack_info *i = find_pack_by_name(packname);
8f3f9b09
JH
215 if (i) {
216 i->old_num = old_cnt;
217 return 0;
218 }
219 else {
6f42f89c 220 /* The file describes a pack that is no longer here */
8f3f9b09
JH
221 return 1;
222 }
223}
224
8f3f9b09
JH
225/* Returns non-zero when we detect that the info in the
226 * old file is useless.
227 */
228static int read_pack_info_file(const char *infofile)
229{
230 FILE *fp;
4ecbd649 231 struct strbuf line = STRBUF_INIT;
8f3f9b09 232 int old_cnt = 0;
965cc517 233 int stale = 1;
8f3f9b09 234
e9d983f1 235 fp = fopen_or_warn(infofile, "r");
8f3f9b09 236 if (!fp)
addf88e4 237 return 1; /* nonexistent is not an error. */
8f3f9b09 238
4ecbd649 239 while (strbuf_getline(&line, fp) != EOF) {
b83a3089 240 const char *arg;
8ac4838a 241
4ecbd649 242 if (!line.len)
8ac4838a 243 continue;
8f3f9b09 244
4ecbd649 245 if (skip_prefix(line.buf, "P ", &arg)) {
b83a3089
JK
246 /* P name */
247 if (parse_pack_def(arg, old_cnt++))
8f3f9b09 248 goto out_stale;
4ecbd649 249 } else if (line.buf[0] == 'D') {
b83a3089 250 /* we used to emit D but that was misguided. */
3e15c67c 251 goto out_stale;
4ecbd649 252 } else if (line.buf[0] == 'T') {
b83a3089
JK
253 /* we used to emit T but nobody uses it. */
254 goto out_stale;
255 } else {
4ecbd649 256 error("unrecognized: %s", line.buf);
8f3f9b09
JH
257 }
258 }
965cc517
JK
259 stale = 0;
260
8f3f9b09 261 out_stale:
4ecbd649 262 strbuf_release(&line);
8f3f9b09 263 fclose(fp);
965cc517 264 return stale;
8f3f9b09
JH
265}
266
8f3f9b09
JH
267static int compare_info(const void *a_, const void *b_)
268{
4b25d091
FC
269 struct pack_info *const *a = a_;
270 struct pack_info *const *b = b_;
8f3f9b09
JH
271
272 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
273 /* Keep the order in the original */
274 return (*a)->old_num - (*b)->old_num;
275 else if (0 <= (*a)->old_num)
276 /* Only A existed in the original so B is obviously newer */
277 return -1;
278 else if (0 <= (*b)->old_num)
279 /* The other way around. */
280 return 1;
281
d5eac498 282 /* then it does not matter but at least keep the comparison stable */
2dee5816
JH
283 if ((*a)->p == (*b)->p)
284 return 0;
285 else if ((*a)->p < (*b)->p)
286 return -1;
287 else
288 return 1;
8f3f9b09
JH
289}
290
291static void init_pack_info(const char *infofile, int force)
292{
293 struct packed_git *p;
294 int stale;
e941c48d
EW
295 int i;
296 size_t alloc = 0;
8f3f9b09 297
454ea2e4 298 for (p = get_all_packs(the_repository); p; p = p->next) {
8f3f9b09
JH
299 /* we ignore things on alternate path since they are
300 * not available to the pullers in general.
301 */
e941c48d 302 if (!p->pack_local || !file_exists(p->pack_name))
8f3f9b09 303 continue;
e941c48d
EW
304
305 i = num_pack++;
306 ALLOC_GROW(info, num_pack, alloc);
ca56dadb 307 CALLOC_ARRAY(info[i], 1);
8f3f9b09
JH
308 info[i]->p = p;
309 info[i]->old_num = -1;
8f3f9b09
JH
310 }
311
312 if (infofile && !force)
313 stale = read_pack_info_file(infofile);
314 else
315 stale = 1;
316
910710bb 317 for (i = 0; i < num_pack; i++)
318 if (stale)
8f3f9b09 319 info[i]->old_num = -1;
8f3f9b09 320
6f42f89c 321 /* renumber them */
9ed0d8d6 322 QSORT(info, num_pack, compare_info);
8f3f9b09
JH
323 for (i = 0; i < num_pack; i++)
324 info[i]->new_num = i;
8f3f9b09
JH
325}
326
3907a407
JK
327static void free_pack_info(void)
328{
329 int i;
330 for (i = 0; i < num_pack; i++)
331 free(info[i]);
332 free(info);
333}
334
f4f476b6 335static int write_pack_info_file(struct update_info_ctx *uic)
8f3f9b09 336{
3e15c67c 337 int i;
d38379ec 338 for (i = 0; i < num_pack; i++) {
f4f476b6 339 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
d38379ec
JK
340 return -1;
341 }
f4f476b6 342 if (uic_printf(uic, "\n") < 0)
d38379ec
JK
343 return -1;
344 return 0;
8f3f9b09
JH
345}
346
347static int update_info_packs(int force)
348{
d38379ec
JK
349 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
350 int ret;
8f3f9b09
JH
351
352 init_pack_info(infofile, force);
f4f476b6 353 ret = update_info_file(infofile, write_pack_info_file, force);
3907a407 354 free_pack_info();
d38379ec
JK
355 free(infofile);
356 return ret;
8f3f9b09
JH
357}
358
8f3f9b09
JH
359/* public */
360int update_server_info(int force)
361{
362 /* We would add more dumb-server support files later,
363 * including index of available pack files and their
364 * intended audiences.
365 */
366 int errs = 0;
367
f4f476b6 368 errs = errs | update_info_refs(force);
8f3f9b09 369 errs = errs | update_info_packs(force);
8f3f9b09 370
6f42f89c 371 /* remove leftover rev-cache file if there is any */
691f1a28 372 unlink_or_warn(git_path("info/rev-cache"));
6f42f89c 373
8f3f9b09
JH
374 return errs;
375}