The sixteenth batch
[git/gitster.git] / server-info.c
blob9bb30d9ab71d2231905a031a7e8f173e31585a67
1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
4 #include "dir.h"
5 #include "hex.h"
6 #include "repository.h"
7 #include "refs.h"
8 #include "object.h"
9 #include "commit.h"
10 #include "tag.h"
11 #include "packfile.h"
12 #include "path.h"
13 #include "object-file.h"
14 #include "odb.h"
15 #include "server-info.h"
16 #include "strbuf.h"
17 #include "tempfile.h"
19 struct update_info_ctx {
20 struct repository *repo;
21 FILE *cur_fp;
22 FILE *old_fp; /* becomes NULL if it differs from cur_fp */
23 struct strbuf cur_sb;
24 struct strbuf old_sb;
27 static void uic_mark_stale(struct update_info_ctx *uic)
29 fclose(uic->old_fp);
30 uic->old_fp = NULL;
33 static int uic_is_stale(const struct update_info_ctx *uic)
35 return uic->old_fp == NULL;
38 __attribute__((format (printf, 2, 3)))
39 static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
41 va_list ap;
42 int ret = -1;
44 va_start(ap, fmt);
46 if (uic_is_stale(uic)) {
47 ret = vfprintf(uic->cur_fp, fmt, ap);
48 } else {
49 ssize_t r;
50 struct strbuf *cur = &uic->cur_sb;
51 struct strbuf *old = &uic->old_sb;
53 strbuf_reset(cur);
54 strbuf_vinsertf(cur, 0, fmt, ap);
56 strbuf_reset(old);
57 strbuf_grow(old, cur->len);
58 r = fread(old->buf, 1, cur->len, uic->old_fp);
59 if (r != cur->len || memcmp(old->buf, cur->buf, r))
60 uic_mark_stale(uic);
62 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
63 ret = 0;
66 va_end(ap);
68 return ret;
72 * Create the file "path" by writing to a temporary file and renaming
73 * it into place. The contents of the file come from "generate", which
74 * should return non-zero if it encounters an error.
76 static int update_info_file(struct repository *r, char *path,
77 int (*generate)(struct update_info_ctx *),
78 int force)
80 char *tmp = mkpathdup("%s_XXXXXX", path);
81 struct tempfile *f = NULL;
82 int ret = -1;
83 struct update_info_ctx uic = {
84 .repo = r,
85 .cur_fp = NULL,
86 .old_fp = NULL,
87 .cur_sb = STRBUF_INIT,
88 .old_sb = STRBUF_INIT
91 safe_create_leading_directories(r, path);
92 f = mks_tempfile_m(tmp, 0666);
93 if (!f)
94 goto out;
95 uic.cur_fp = fdopen_tempfile(f, "w");
96 if (!uic.cur_fp)
97 goto out;
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");
104 * uic_printf will compare incremental comparison against old_fp
105 * and mark uic as stale if needed
107 ret = generate(&uic);
108 if (ret)
109 goto out;
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);
117 if (new_len < 0) {
118 ret = -1;
119 goto out;
121 if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len))
122 uic_mark_stale(&uic);
125 uic.cur_fp = NULL;
127 if (uic_is_stale(&uic)) {
128 if (adjust_shared_perm(r, get_tempfile_path(f)) < 0)
129 goto out;
130 if (rename_tempfile(&f, path) < 0)
131 goto out;
132 } else {
133 delete_tempfile(&f);
135 ret = 0;
137 out:
138 if (ret) {
139 error_errno("unable to update %s", path);
140 if (f)
141 delete_tempfile(&f);
143 free(tmp);
144 if (uic.old_fp)
145 fclose(uic.old_fp);
146 strbuf_release(&uic.old_sb);
147 strbuf_release(&uic.cur_sb);
148 return ret;
151 static int add_info_ref(const char *path, const char *referent UNUSED, const struct object_id *oid,
152 int flag UNUSED,
153 void *cb_data)
155 struct update_info_ctx *uic = cb_data;
156 struct object *o = parse_object(uic->repo, oid);
157 if (!o)
158 return -1;
160 if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0)
161 return -1;
163 if (o->type == OBJ_TAG) {
164 o = deref_tag(uic->repo, o, path, 0);
165 if (o)
166 if (uic_printf(uic, "%s %s^{}\n",
167 oid_to_hex(&o->oid), path) < 0)
168 return -1;
170 return 0;
173 static int generate_info_refs(struct update_info_ctx *uic)
175 return refs_for_each_ref(get_main_ref_store(uic->repo),
176 add_info_ref, uic);
179 static int update_info_refs(struct repository *r, int force)
181 char *path = repo_git_path(r, "info/refs");
182 int ret = update_info_file(r, path, generate_info_refs, force);
183 free(path);
184 return ret;
187 /* packs */
188 static struct pack_info {
189 struct packed_git *p;
190 int old_num;
191 int new_num;
192 } **info;
193 static int num_pack;
195 static struct pack_info *find_pack_by_name(const char *name)
197 int i;
198 for (i = 0; i < num_pack; i++) {
199 struct packed_git *p = info[i]->p;
200 if (!strcmp(pack_basename(p), name))
201 return info[i];
203 return NULL;
206 /* Returns non-zero when we detect that the info in the
207 * old file is useless.
209 static int parse_pack_def(const char *packname, int old_cnt)
211 struct pack_info *i = find_pack_by_name(packname);
212 if (i) {
213 i->old_num = old_cnt;
214 return 0;
216 else {
217 /* The file describes a pack that is no longer here */
218 return 1;
222 /* Returns non-zero when we detect that the info in the
223 * old file is useless.
225 static int read_pack_info_file(const char *infofile)
227 FILE *fp;
228 struct strbuf line = STRBUF_INIT;
229 int old_cnt = 0;
230 int stale = 1;
232 fp = fopen_or_warn(infofile, "r");
233 if (!fp)
234 return 1; /* nonexistent is not an error. */
236 while (strbuf_getline(&line, fp) != EOF) {
237 const char *arg;
239 if (!line.len)
240 continue;
242 if (skip_prefix(line.buf, "P ", &arg)) {
243 /* P name */
244 if (parse_pack_def(arg, old_cnt++))
245 goto out_stale;
246 } else if (line.buf[0] == 'D') {
247 /* we used to emit D but that was misguided. */
248 goto out_stale;
249 } else if (line.buf[0] == 'T') {
250 /* we used to emit T but nobody uses it. */
251 goto out_stale;
252 } else {
253 error("unrecognized: %s", line.buf);
256 stale = 0;
258 out_stale:
259 strbuf_release(&line);
260 fclose(fp);
261 return stale;
264 static int compare_info(const void *a_, const void *b_)
266 struct pack_info *const *a = a_;
267 struct pack_info *const *b = b_;
269 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
270 /* Keep the order in the original */
271 return (*a)->old_num - (*b)->old_num;
272 else if (0 <= (*a)->old_num)
273 /* Only A existed in the original so B is obviously newer */
274 return -1;
275 else if (0 <= (*b)->old_num)
276 /* The other way around. */
277 return 1;
279 /* then it does not matter but at least keep the comparison stable */
280 if ((*a)->p == (*b)->p)
281 return 0;
282 else if ((*a)->p < (*b)->p)
283 return -1;
284 else
285 return 1;
288 static void init_pack_info(struct repository *r, const char *infofile, int force)
290 struct packed_git *p;
291 int stale;
292 int i;
293 size_t alloc = 0;
295 for (p = get_all_packs(r); p; p = p->next) {
296 /* we ignore things on alternate path since they are
297 * not available to the pullers in general.
299 if (!p->pack_local || !file_exists(p->pack_name))
300 continue;
302 i = num_pack++;
303 ALLOC_GROW(info, num_pack, alloc);
304 CALLOC_ARRAY(info[i], 1);
305 info[i]->p = p;
306 info[i]->old_num = -1;
309 if (infofile && !force)
310 stale = read_pack_info_file(infofile);
311 else
312 stale = 1;
314 for (i = 0; i < num_pack; i++)
315 if (stale)
316 info[i]->old_num = -1;
318 /* renumber them */
319 QSORT(info, num_pack, compare_info);
320 for (i = 0; i < num_pack; i++)
321 info[i]->new_num = i;
324 static void free_pack_info(void)
326 int i;
327 for (i = 0; i < num_pack; i++)
328 free(info[i]);
329 free(info);
332 static int write_pack_info_file(struct update_info_ctx *uic)
334 int i;
335 for (i = 0; i < num_pack; i++) {
336 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
337 return -1;
339 if (uic_printf(uic, "\n") < 0)
340 return -1;
341 return 0;
344 static int update_info_packs(struct repository *r, int force)
346 char *infofile = mkpathdup("%s/info/packs",
347 repo_get_object_directory(r));
348 int ret;
350 init_pack_info(r, infofile, force);
351 ret = update_info_file(r, infofile, write_pack_info_file, force);
352 free_pack_info();
353 free(infofile);
354 return ret;
357 /* public */
358 int update_server_info(struct repository *r, int force)
360 /* We would add more dumb-server support files later,
361 * including index of available pack files and their
362 * intended audiences.
364 int errs = 0;
365 char *path;
367 errs = errs | update_info_refs(r, force);
368 errs = errs | update_info_packs(r, force);
370 /* remove leftover rev-cache file if there is any */
371 path = repo_git_path(r, "info/rev-cache");
372 unlink_or_warn(path);
373 free(path);
375 return errs;