2 #include "repository.h"
8 #include "object-store.h"
11 struct update_info_ctx {
13 FILE *old_fp; /* becomes NULL if it differs from cur_fp */
18 static void uic_mark_stale(struct update_info_ctx *uic)
24 static int uic_is_stale(const struct update_info_ctx *uic)
26 return uic->old_fp == NULL;
29 static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
36 if (uic_is_stale(uic)) {
37 ret = vfprintf(uic->cur_fp, fmt, ap);
40 struct strbuf *cur = &uic->cur_sb;
41 struct strbuf *old = &uic->old_sb;
44 strbuf_vinsertf(cur, 0, fmt, ap);
47 strbuf_grow(old, cur->len);
48 r = fread(old->buf, 1, cur->len, uic->old_fp);
49 if (r != cur->len || memcmp(old->buf, cur->buf, r))
52 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
62 * Create the file "path" by writing to a temporary file and renaming
63 * it into place. The contents of the file come from "generate", which
64 * should return non-zero if it encounters an error.
66 static int update_info_file(char *path,
67 int (*generate)(struct update_info_ctx *),
70 char *tmp = mkpathdup("%s_XXXXXX", path);
74 struct update_info_ctx uic = {
77 .cur_sb = STRBUF_INIT,
81 safe_create_leading_directories(path);
82 fd = git_mkstemp_mode(tmp, 0666);
85 to_close = uic.cur_fp = fdopen(fd, "w");
90 /* no problem on ENOENT and old_fp == NULL, it's stale, now */
92 uic.old_fp = fopen_or_warn(path, "r");
95 * uic_printf will compare incremental comparison aginst old_fp
96 * and mark uic as stale if needed
102 /* new file may be shorter than the old one, check here */
103 if (!uic_is_stale(&uic)) {
105 long new_len = ftell(uic.cur_fp);
106 int old_fd = fileno(uic.old_fp);
112 if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len))
113 uic_mark_stale(&uic);
117 if (fclose(to_close))
120 if (uic_is_stale(&uic)) {
121 if (adjust_shared_perm(tmp) < 0)
123 if (rename(tmp, path) < 0)
132 error_errno("unable to update %s", path);
142 strbuf_release(&uic.old_sb);
143 strbuf_release(&uic.cur_sb);
147 static int add_info_ref(const char *path, const struct object_id *oid,
148 int flag, void *cb_data)
150 struct update_info_ctx *uic = cb_data;
151 struct object *o = parse_object(the_repository, oid);
155 if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0)
158 if (o->type == OBJ_TAG) {
159 o = deref_tag(the_repository, o, path, 0);
161 if (uic_printf(uic, "%s %s^{}\n",
162 oid_to_hex(&o->oid), path) < 0)
168 static int generate_info_refs(struct update_info_ctx *uic)
170 return for_each_ref(add_info_ref, uic);
173 static int update_info_refs(int force)
175 char *path = git_pathdup("info/refs");
176 int ret = update_info_file(path, generate_info_refs, force);
182 static struct pack_info {
183 struct packed_git *p;
189 static struct pack_info *find_pack_by_name(const char *name)
192 for (i = 0; i < num_pack; i++) {
193 struct packed_git *p = info[i]->p;
194 if (!strcmp(pack_basename(p), name))
200 /* Returns non-zero when we detect that the info in the
201 * old file is useless.
203 static int parse_pack_def(const char *packname, int old_cnt)
205 struct pack_info *i = find_pack_by_name(packname);
207 i->old_num = old_cnt;
211 /* The file describes a pack that is no longer here */
216 /* Returns non-zero when we detect that the info in the
217 * old file is useless.
219 static int read_pack_info_file(const char *infofile)
222 struct strbuf line = STRBUF_INIT;
226 fp = fopen_or_warn(infofile, "r");
228 return 1; /* nonexistent is not an error. */
230 while (strbuf_getline(&line, fp) != EOF) {
236 if (skip_prefix(line.buf, "P ", &arg)) {
238 if (parse_pack_def(arg, old_cnt++))
240 } else if (line.buf[0] == 'D') {
241 /* we used to emit D but that was misguided. */
243 } else if (line.buf[0] == 'T') {
244 /* we used to emit T but nobody uses it. */
247 error("unrecognized: %s", line.buf);
253 strbuf_release(&line);
258 static int compare_info(const void *a_, const void *b_)
260 struct pack_info *const *a = a_;
261 struct pack_info *const *b = b_;
263 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
264 /* Keep the order in the original */
265 return (*a)->old_num - (*b)->old_num;
266 else if (0 <= (*a)->old_num)
267 /* Only A existed in the original so B is obviously newer */
269 else if (0 <= (*b)->old_num)
270 /* The other way around. */
273 /* then it does not matter but at least keep the comparison stable */
274 if ((*a)->p == (*b)->p)
276 else if ((*a)->p < (*b)->p)
282 static void init_pack_info(const char *infofile, int force)
284 struct packed_git *p;
288 for (p = get_all_packs(the_repository); p; p = p->next) {
289 /* we ignore things on alternate path since they are
290 * not available to the pullers in general.
297 info = xcalloc(num_pack, sizeof(struct pack_info *));
298 for (i = 0, p = get_all_packs(the_repository); p; p = p->next) {
301 assert(i < num_pack);
302 info[i] = xcalloc(1, sizeof(struct pack_info));
304 info[i]->old_num = -1;
308 if (infofile && !force)
309 stale = read_pack_info_file(infofile);
313 for (i = 0; i < num_pack; i++)
315 info[i]->old_num = -1;
318 QSORT(info, num_pack, compare_info);
319 for (i = 0; i < num_pack; i++)
320 info[i]->new_num = i;
323 static void free_pack_info(void)
326 for (i = 0; i < num_pack; i++)
331 static int write_pack_info_file(struct update_info_ctx *uic)
334 for (i = 0; i < num_pack; i++) {
335 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
338 if (uic_printf(uic, "\n") < 0)
343 static int update_info_packs(int force)
345 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
348 init_pack_info(infofile, force);
349 ret = update_info_file(infofile, write_pack_info_file, force);
356 int update_server_info(int force)
358 /* We would add more dumb-server support files later,
359 * including index of available pack files and their
360 * intended audiences.
364 errs = errs | update_info_refs(force);
365 errs = errs | update_info_packs(force);
367 /* remove leftover rev-cache file if there is any */
368 unlink_or_warn(git_path("info/rev-cache"));