Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / server-info.c
blob7701d7c20a12af53d2f91737f12f3dfb97ac59e9
1 #include "cache.h"
2 #include "dir.h"
3 #include "repository.h"
4 #include "refs.h"
5 #include "object.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "packfile.h"
9 #include "object-store.h"
10 #include "strbuf.h"
12 struct update_info_ctx {
13 FILE *cur_fp;
14 FILE *old_fp; /* becomes NULL if it differs from cur_fp */
15 struct strbuf cur_sb;
16 struct strbuf old_sb;
19 static void uic_mark_stale(struct update_info_ctx *uic)
21 fclose(uic->old_fp);
22 uic->old_fp = NULL;
25 static int uic_is_stale(const struct update_info_ctx *uic)
27 return uic->old_fp == NULL;
30 __attribute__((format (printf, 2, 3)))
31 static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
33 va_list ap;
34 int ret = -1;
36 va_start(ap, fmt);
38 if (uic_is_stale(uic)) {
39 ret = vfprintf(uic->cur_fp, fmt, ap);
40 } else {
41 ssize_t r;
42 struct strbuf *cur = &uic->cur_sb;
43 struct strbuf *old = &uic->old_sb;
45 strbuf_reset(cur);
46 strbuf_vinsertf(cur, 0, fmt, ap);
48 strbuf_reset(old);
49 strbuf_grow(old, cur->len);
50 r = fread(old->buf, 1, cur->len, uic->old_fp);
51 if (r != cur->len || memcmp(old->buf, cur->buf, r))
52 uic_mark_stale(uic);
54 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
55 ret = 0;
58 va_end(ap);
60 return ret;
64 * Create the file "path" by writing to a temporary file and renaming
65 * it into place. The contents of the file come from "generate", which
66 * should return non-zero if it encounters an error.
68 static int update_info_file(char *path,
69 int (*generate)(struct update_info_ctx *),
70 int force)
72 char *tmp = mkpathdup("%s_XXXXXX", path);
73 int ret = -1;
74 int fd = -1;
75 FILE *to_close;
76 struct update_info_ctx uic = {
77 .cur_fp = NULL,
78 .old_fp = NULL,
79 .cur_sb = STRBUF_INIT,
80 .old_sb = STRBUF_INIT
83 safe_create_leading_directories(path);
84 fd = git_mkstemp_mode(tmp, 0666);
85 if (fd < 0)
86 goto out;
87 to_close = uic.cur_fp = fdopen(fd, "w");
88 if (!uic.cur_fp)
89 goto out;
90 fd = -1;
92 /* no problem on ENOENT and old_fp == NULL, it's stale, now */
93 if (!force)
94 uic.old_fp = fopen_or_warn(path, "r");
97 * uic_printf will compare incremental comparison against old_fp
98 * and mark uic as stale if needed
100 ret = generate(&uic);
101 if (ret)
102 goto out;
104 /* new file may be shorter than the old one, check here */
105 if (!uic_is_stale(&uic)) {
106 struct stat st;
107 long new_len = ftell(uic.cur_fp);
108 int old_fd = fileno(uic.old_fp);
110 if (new_len < 0) {
111 ret = -1;
112 goto out;
114 if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len))
115 uic_mark_stale(&uic);
118 uic.cur_fp = NULL;
119 if (fclose(to_close))
120 goto out;
122 if (uic_is_stale(&uic)) {
123 if (adjust_shared_perm(tmp) < 0)
124 goto out;
125 if (rename(tmp, path) < 0)
126 goto out;
127 } else {
128 unlink(tmp);
130 ret = 0;
132 out:
133 if (ret) {
134 error_errno("unable to update %s", path);
135 if (uic.cur_fp)
136 fclose(uic.cur_fp);
137 else if (fd >= 0)
138 close(fd);
139 unlink(tmp);
141 free(tmp);
142 if (uic.old_fp)
143 fclose(uic.old_fp);
144 strbuf_release(&uic.old_sb);
145 strbuf_release(&uic.cur_sb);
146 return ret;
149 static int add_info_ref(const char *path, const struct object_id *oid,
150 int flag, void *cb_data)
152 struct update_info_ctx *uic = cb_data;
153 struct object *o = parse_object(the_repository, oid);
154 if (!o)
155 return -1;
157 if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0)
158 return -1;
160 if (o->type == OBJ_TAG) {
161 o = deref_tag(the_repository, o, path, 0);
162 if (o)
163 if (uic_printf(uic, "%s %s^{}\n",
164 oid_to_hex(&o->oid), path) < 0)
165 return -1;
167 return 0;
170 static int generate_info_refs(struct update_info_ctx *uic)
172 return for_each_ref(add_info_ref, uic);
175 static int update_info_refs(int force)
177 char *path = git_pathdup("info/refs");
178 int ret = update_info_file(path, generate_info_refs, force);
179 free(path);
180 return ret;
183 /* packs */
184 static struct pack_info {
185 struct packed_git *p;
186 int old_num;
187 int new_num;
188 } **info;
189 static int num_pack;
191 static struct pack_info *find_pack_by_name(const char *name)
193 int i;
194 for (i = 0; i < num_pack; i++) {
195 struct packed_git *p = info[i]->p;
196 if (!strcmp(pack_basename(p), name))
197 return info[i];
199 return NULL;
202 /* Returns non-zero when we detect that the info in the
203 * old file is useless.
205 static int parse_pack_def(const char *packname, int old_cnt)
207 struct pack_info *i = find_pack_by_name(packname);
208 if (i) {
209 i->old_num = old_cnt;
210 return 0;
212 else {
213 /* The file describes a pack that is no longer here */
214 return 1;
218 /* Returns non-zero when we detect that the info in the
219 * old file is useless.
221 static int read_pack_info_file(const char *infofile)
223 FILE *fp;
224 struct strbuf line = STRBUF_INIT;
225 int old_cnt = 0;
226 int stale = 1;
228 fp = fopen_or_warn(infofile, "r");
229 if (!fp)
230 return 1; /* nonexistent is not an error. */
232 while (strbuf_getline(&line, fp) != EOF) {
233 const char *arg;
235 if (!line.len)
236 continue;
238 if (skip_prefix(line.buf, "P ", &arg)) {
239 /* P name */
240 if (parse_pack_def(arg, old_cnt++))
241 goto out_stale;
242 } else if (line.buf[0] == 'D') {
243 /* we used to emit D but that was misguided. */
244 goto out_stale;
245 } else if (line.buf[0] == 'T') {
246 /* we used to emit T but nobody uses it. */
247 goto out_stale;
248 } else {
249 error("unrecognized: %s", line.buf);
252 stale = 0;
254 out_stale:
255 strbuf_release(&line);
256 fclose(fp);
257 return stale;
260 static int compare_info(const void *a_, const void *b_)
262 struct pack_info *const *a = a_;
263 struct pack_info *const *b = b_;
265 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
266 /* Keep the order in the original */
267 return (*a)->old_num - (*b)->old_num;
268 else if (0 <= (*a)->old_num)
269 /* Only A existed in the original so B is obviously newer */
270 return -1;
271 else if (0 <= (*b)->old_num)
272 /* The other way around. */
273 return 1;
275 /* then it does not matter but at least keep the comparison stable */
276 if ((*a)->p == (*b)->p)
277 return 0;
278 else if ((*a)->p < (*b)->p)
279 return -1;
280 else
281 return 1;
284 static void init_pack_info(const char *infofile, int force)
286 struct packed_git *p;
287 int stale;
288 int i;
289 size_t alloc = 0;
291 for (p = get_all_packs(the_repository); p; p = p->next) {
292 /* we ignore things on alternate path since they are
293 * not available to the pullers in general.
295 if (!p->pack_local || !file_exists(p->pack_name))
296 continue;
298 i = num_pack++;
299 ALLOC_GROW(info, num_pack, alloc);
300 CALLOC_ARRAY(info[i], 1);
301 info[i]->p = p;
302 info[i]->old_num = -1;
305 if (infofile && !force)
306 stale = read_pack_info_file(infofile);
307 else
308 stale = 1;
310 for (i = 0; i < num_pack; i++)
311 if (stale)
312 info[i]->old_num = -1;
314 /* renumber them */
315 QSORT(info, num_pack, compare_info);
316 for (i = 0; i < num_pack; i++)
317 info[i]->new_num = i;
320 static void free_pack_info(void)
322 int i;
323 for (i = 0; i < num_pack; i++)
324 free(info[i]);
325 free(info);
328 static int write_pack_info_file(struct update_info_ctx *uic)
330 int i;
331 for (i = 0; i < num_pack; i++) {
332 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
333 return -1;
335 if (uic_printf(uic, "\n") < 0)
336 return -1;
337 return 0;
340 static int update_info_packs(int force)
342 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
343 int ret;
345 init_pack_info(infofile, force);
346 ret = update_info_file(infofile, write_pack_info_file, force);
347 free_pack_info();
348 free(infofile);
349 return ret;
352 /* public */
353 int update_server_info(int force)
355 /* We would add more dumb-server support files later,
356 * including index of available pack files and their
357 * intended audiences.
359 int errs = 0;
361 errs = errs | update_info_refs(force);
362 errs = errs | update_info_packs(force);
364 /* remove leftover rev-cache file if there is any */
365 unlink_or_warn(git_path("info/rev-cache"));
367 return errs;