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