Merge branch 'kz/merge-fail-early-upon-refresh-failure'
[git.git] / server-info.c
blob37d1085982e9c6255493ac824daeb1c5ad38896b
1 #include "git-compat-util.h"
2 #include "dir.h"
3 #include "environment.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "refs.h"
7 #include "object.h"
8 #include "commit.h"
9 #include "tag.h"
10 #include "packfile.h"
11 #include "path.h"
12 #include "object-file.h"
13 #include "object-store-ll.h"
14 #include "server-info.h"
15 #include "strbuf.h"
16 #include "tempfile.h"
18 struct 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;
25 static void uic_mark_stale(struct update_info_ctx *uic)
27 fclose(uic->old_fp);
28 uic->old_fp = NULL;
31 static int uic_is_stale(const struct update_info_ctx *uic)
33 return uic->old_fp == NULL;
36 __attribute__((format (printf, 2, 3)))
37 static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
39 va_list ap;
40 int ret = -1;
42 va_start(ap, fmt);
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;
51 strbuf_reset(cur);
52 strbuf_vinsertf(cur, 0, fmt, ap);
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);
60 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
61 ret = 0;
64 va_end(ap);
66 return ret;
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.
74 static int update_info_file(char *path,
75 int (*generate)(struct update_info_ctx *),
76 int force)
78 char *tmp = mkpathdup("%s_XXXXXX", path);
79 struct tempfile *f = NULL;
80 int ret = -1;
81 struct update_info_ctx uic = {
82 .cur_fp = NULL,
83 .old_fp = NULL,
84 .cur_sb = STRBUF_INIT,
85 .old_sb = STRBUF_INIT
88 safe_create_leading_directories(path);
89 f = mks_tempfile_m(tmp, 0666);
90 if (!f)
91 goto out;
92 uic.cur_fp = fdopen_tempfile(f, "w");
93 if (!uic.cur_fp)
94 goto out;
96 /* no problem on ENOENT and old_fp == NULL, it's stale, now */
97 if (!force)
98 uic.old_fp = fopen_or_warn(path, "r");
101 * uic_printf will compare incremental comparison against old_fp
102 * and mark uic as stale if needed
104 ret = generate(&uic);
105 if (ret)
106 goto out;
108 /* new file may be shorter than the old one, check here */
109 if (!uic_is_stale(&uic)) {
110 struct stat st;
111 long new_len = ftell(uic.cur_fp);
112 int old_fd = fileno(uic.old_fp);
114 if (new_len < 0) {
115 ret = -1;
116 goto out;
118 if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len))
119 uic_mark_stale(&uic);
122 uic.cur_fp = NULL;
124 if (uic_is_stale(&uic)) {
125 if (adjust_shared_perm(get_tempfile_path(f)) < 0)
126 goto out;
127 if (rename_tempfile(&f, path) < 0)
128 goto out;
129 } else {
130 delete_tempfile(&f);
132 ret = 0;
134 out:
135 if (ret) {
136 error_errno("unable to update %s", path);
137 if (f)
138 delete_tempfile(&f);
140 free(tmp);
141 if (uic.old_fp)
142 fclose(uic.old_fp);
143 strbuf_release(&uic.old_sb);
144 strbuf_release(&uic.cur_sb);
145 return ret;
148 static int add_info_ref(const char *path, const struct object_id *oid,
149 int flag UNUSED,
150 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 refs_for_each_ref(get_main_ref_store(the_repository),
173 add_info_ref, uic);
176 static int update_info_refs(int force)
178 char *path = git_pathdup("info/refs");
179 int ret = update_info_file(path, generate_info_refs, force);
180 free(path);
181 return ret;
184 /* packs */
185 static struct pack_info {
186 struct packed_git *p;
187 int old_num;
188 int new_num;
189 } **info;
190 static int num_pack;
192 static struct pack_info *find_pack_by_name(const char *name)
194 int i;
195 for (i = 0; i < num_pack; i++) {
196 struct packed_git *p = info[i]->p;
197 if (!strcmp(pack_basename(p), name))
198 return info[i];
200 return NULL;
203 /* Returns non-zero when we detect that the info in the
204 * old file is useless.
206 static int parse_pack_def(const char *packname, int old_cnt)
208 struct pack_info *i = find_pack_by_name(packname);
209 if (i) {
210 i->old_num = old_cnt;
211 return 0;
213 else {
214 /* The file describes a pack that is no longer here */
215 return 1;
219 /* Returns non-zero when we detect that the info in the
220 * old file is useless.
222 static int read_pack_info_file(const char *infofile)
224 FILE *fp;
225 struct strbuf line = STRBUF_INIT;
226 int old_cnt = 0;
227 int stale = 1;
229 fp = fopen_or_warn(infofile, "r");
230 if (!fp)
231 return 1; /* nonexistent is not an error. */
233 while (strbuf_getline(&line, fp) != EOF) {
234 const char *arg;
236 if (!line.len)
237 continue;
239 if (skip_prefix(line.buf, "P ", &arg)) {
240 /* P name */
241 if (parse_pack_def(arg, old_cnt++))
242 goto out_stale;
243 } else if (line.buf[0] == 'D') {
244 /* we used to emit D but that was misguided. */
245 goto out_stale;
246 } else if (line.buf[0] == 'T') {
247 /* we used to emit T but nobody uses it. */
248 goto out_stale;
249 } else {
250 error("unrecognized: %s", line.buf);
253 stale = 0;
255 out_stale:
256 strbuf_release(&line);
257 fclose(fp);
258 return stale;
261 static int compare_info(const void *a_, const void *b_)
263 struct pack_info *const *a = a_;
264 struct pack_info *const *b = b_;
266 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
267 /* Keep the order in the original */
268 return (*a)->old_num - (*b)->old_num;
269 else if (0 <= (*a)->old_num)
270 /* Only A existed in the original so B is obviously newer */
271 return -1;
272 else if (0 <= (*b)->old_num)
273 /* The other way around. */
274 return 1;
276 /* then it does not matter but at least keep the comparison stable */
277 if ((*a)->p == (*b)->p)
278 return 0;
279 else if ((*a)->p < (*b)->p)
280 return -1;
281 else
282 return 1;
285 static void init_pack_info(const char *infofile, int force)
287 struct packed_git *p;
288 int stale;
289 int i;
290 size_t alloc = 0;
292 for (p = get_all_packs(the_repository); p; p = p->next) {
293 /* we ignore things on alternate path since they are
294 * not available to the pullers in general.
296 if (!p->pack_local || !file_exists(p->pack_name))
297 continue;
299 i = num_pack++;
300 ALLOC_GROW(info, num_pack, alloc);
301 CALLOC_ARRAY(info[i], 1);
302 info[i]->p = p;
303 info[i]->old_num = -1;
306 if (infofile && !force)
307 stale = read_pack_info_file(infofile);
308 else
309 stale = 1;
311 for (i = 0; i < num_pack; i++)
312 if (stale)
313 info[i]->old_num = -1;
315 /* renumber them */
316 QSORT(info, num_pack, compare_info);
317 for (i = 0; i < num_pack; i++)
318 info[i]->new_num = i;
321 static void free_pack_info(void)
323 int i;
324 for (i = 0; i < num_pack; i++)
325 free(info[i]);
326 free(info);
329 static int write_pack_info_file(struct update_info_ctx *uic)
331 int i;
332 for (i = 0; i < num_pack; i++) {
333 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
334 return -1;
336 if (uic_printf(uic, "\n") < 0)
337 return -1;
338 return 0;
341 static int update_info_packs(int force)
343 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
344 int ret;
346 init_pack_info(infofile, force);
347 ret = update_info_file(infofile, write_pack_info_file, force);
348 free_pack_info();
349 free(infofile);
350 return ret;
353 /* public */
354 int update_server_info(int force)
356 /* We would add more dumb-server support files later,
357 * including index of available pack files and their
358 * intended audiences.
360 int errs = 0;
362 errs = errs | update_info_refs(force);
363 errs = errs | update_info_packs(force);
365 /* remove leftover rev-cache file if there is any */
366 unlink_or_warn(git_path("info/rev-cache"));
368 return errs;