8 * Create the file "path" by writing to a temporary file and renaming
9 * it into place. The contents of the file come from "generate", which
10 * should return non-zero if it encounters an error.
12 static int update_info_file(char *path, int (*generate)(FILE *))
14 char *tmp = mkpathdup("%s_XXXXXX", path);
17 FILE *fp = NULL, *to_close;
19 safe_create_leading_directories(path);
20 fd = git_mkstemp_mode(tmp, 0666);
23 to_close = fp = fdopen(fd, "w");
33 if (adjust_shared_perm(tmp) < 0)
35 if (rename(tmp, path) < 0)
41 error_errno("unable to update %s", path);
52 static int add_info_ref(const char *path, const struct object_id *oid,
53 int flag, void *cb_data)
56 struct object *o = parse_object(oid->hash);
60 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
63 if (o->type == OBJ_TAG) {
64 o = deref_tag(o, path, 0);
66 if (fprintf(fp, "%s %s^{}\n",
67 oid_to_hex(&o->oid), path) < 0)
73 static int generate_info_refs(FILE *fp)
75 return for_each_ref(add_info_ref, fp);
78 static int update_info_refs(int force)
80 char *path = git_pathdup("info/refs");
81 int ret = update_info_file(path, generate_info_refs);
87 static struct pack_info {
93 unsigned char (*head)[20];
96 static const char *objdir;
99 static struct pack_info *find_pack_by_name(const char *name)
102 for (i = 0; i < num_pack; i++) {
103 struct packed_git *p = info[i]->p;
104 /* skip "/pack/" after ".git/objects" */
105 if (!strcmp(p->pack_name + objdirlen + 6, name))
111 /* Returns non-zero when we detect that the info in the
112 * old file is useless.
114 static int parse_pack_def(const char *line, int old_cnt)
116 struct pack_info *i = find_pack_by_name(line + 2);
118 i->old_num = old_cnt;
122 /* The file describes a pack that is no longer here */
127 /* Returns non-zero when we detect that the info in the
128 * old file is useless.
130 static int read_pack_info_file(const char *infofile)
136 fp = fopen(infofile, "r");
138 return 1; /* nonexistent is not an error. */
140 while (fgets(line, sizeof(line), fp)) {
141 int len = strlen(line);
142 if (len && line[len-1] == '\n')
149 case 'P': /* P name */
150 if (parse_pack_def(line, old_cnt++))
153 case 'D': /* we used to emit D but that was misguided. */
154 case 'T': /* we used to emit T but nobody uses it. */
157 error("unrecognized: %s", line);
168 static int compare_info(const void *a_, const void *b_)
170 struct pack_info *const *a = a_;
171 struct pack_info *const *b = b_;
173 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
174 /* Keep the order in the original */
175 return (*a)->old_num - (*b)->old_num;
176 else if (0 <= (*a)->old_num)
177 /* Only A existed in the original so B is obviously newer */
179 else if (0 <= (*b)->old_num)
180 /* The other way around. */
183 /* then it does not matter but at least keep the comparison stable */
184 if ((*a)->p == (*b)->p)
186 else if ((*a)->p < (*b)->p)
192 static void init_pack_info(const char *infofile, int force)
194 struct packed_git *p;
198 objdir = get_object_directory();
199 objdirlen = strlen(objdir);
201 prepare_packed_git();
202 for (p = packed_git; p; p = p->next) {
203 /* we ignore things on alternate path since they are
204 * not available to the pullers in general.
211 info = xcalloc(num_pack, sizeof(struct pack_info *));
212 for (i = 0, p = packed_git; p; p = p->next) {
215 info[i] = xcalloc(1, sizeof(struct pack_info));
217 info[i]->old_num = -1;
221 if (infofile && !force)
222 stale = read_pack_info_file(infofile);
226 for (i = 0; i < num_pack; i++) {
228 info[i]->old_num = -1;
229 info[i]->nr_heads = 0;
234 QSORT(info, num_pack, compare_info);
235 for (i = 0; i < num_pack; i++)
236 info[i]->new_num = i;
239 static void free_pack_info(void)
242 for (i = 0; i < num_pack; i++)
247 static int write_pack_info_file(FILE *fp)
250 for (i = 0; i < num_pack; i++) {
251 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
254 if (fputc('\n', fp) == EOF)
259 static int update_info_packs(int force)
261 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
264 init_pack_info(infofile, force);
265 ret = update_info_file(infofile, write_pack_info_file);
272 int update_server_info(int force)
274 /* We would add more dumb-server support files later,
275 * including index of available pack files and their
276 * intended audiences.
280 errs = errs | update_info_refs(force);
281 errs = errs | update_info_packs(force);
283 /* remove leftover rev-cache file if there is any */
284 unlink_or_warn(git_path("info/rev-cache"));