notes.c: use designated initializers for clarity
[git/debian.git] / server-info.c
blob355b6e01a52a0ce25b1a9fdf145749b37085f852
1 #include "cache.h"
2 #include "alloc.h"
3 #include "dir.h"
4 #include "environment.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 "object-store.h"
13 #include "strbuf.h"
14 #include "wrapper.h"
16 struct update_info_ctx {
17 FILE *cur_fp;
18 FILE *old_fp; /* becomes NULL if it differs from cur_fp */
19 struct strbuf cur_sb;
20 struct strbuf old_sb;
23 static void uic_mark_stale(struct update_info_ctx *uic)
25 fclose(uic->old_fp);
26 uic->old_fp = NULL;
29 static int uic_is_stale(const struct update_info_ctx *uic)
31 return uic->old_fp == NULL;
34 __attribute__((format (printf, 2, 3)))
35 static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...)
37 va_list ap;
38 int ret = -1;
40 va_start(ap, fmt);
42 if (uic_is_stale(uic)) {
43 ret = vfprintf(uic->cur_fp, fmt, ap);
44 } else {
45 ssize_t r;
46 struct strbuf *cur = &uic->cur_sb;
47 struct strbuf *old = &uic->old_sb;
49 strbuf_reset(cur);
50 strbuf_vinsertf(cur, 0, fmt, ap);
52 strbuf_reset(old);
53 strbuf_grow(old, cur->len);
54 r = fread(old->buf, 1, cur->len, uic->old_fp);
55 if (r != cur->len || memcmp(old->buf, cur->buf, r))
56 uic_mark_stale(uic);
58 if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len)
59 ret = 0;
62 va_end(ap);
64 return ret;
68 * Create the file "path" by writing to a temporary file and renaming
69 * it into place. The contents of the file come from "generate", which
70 * should return non-zero if it encounters an error.
72 static int update_info_file(char *path,
73 int (*generate)(struct update_info_ctx *),
74 int force)
76 char *tmp = mkpathdup("%s_XXXXXX", path);
77 int ret = -1;
78 int fd = -1;
79 FILE *to_close;
80 struct update_info_ctx uic = {
81 .cur_fp = NULL,
82 .old_fp = NULL,
83 .cur_sb = STRBUF_INIT,
84 .old_sb = STRBUF_INIT
87 safe_create_leading_directories(path);
88 fd = git_mkstemp_mode(tmp, 0666);
89 if (fd < 0)
90 goto out;
91 to_close = uic.cur_fp = fdopen(fd, "w");
92 if (!uic.cur_fp)
93 goto out;
94 fd = -1;
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;
123 if (fclose(to_close))
124 goto out;
126 if (uic_is_stale(&uic)) {
127 if (adjust_shared_perm(tmp) < 0)
128 goto out;
129 if (rename(tmp, path) < 0)
130 goto out;
131 } else {
132 unlink(tmp);
134 ret = 0;
136 out:
137 if (ret) {
138 error_errno("unable to update %s", path);
139 if (uic.cur_fp)
140 fclose(uic.cur_fp);
141 else if (fd >= 0)
142 close(fd);
143 unlink(tmp);
145 free(tmp);
146 if (uic.old_fp)
147 fclose(uic.old_fp);
148 strbuf_release(&uic.old_sb);
149 strbuf_release(&uic.cur_sb);
150 return ret;
153 static int add_info_ref(const char *path, const struct object_id *oid,
154 int flag UNUSED,
155 void *cb_data)
157 struct update_info_ctx *uic = cb_data;
158 struct object *o = parse_object(the_repository, oid);
159 if (!o)
160 return -1;
162 if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0)
163 return -1;
165 if (o->type == OBJ_TAG) {
166 o = deref_tag(the_repository, o, path, 0);
167 if (o)
168 if (uic_printf(uic, "%s %s^{}\n",
169 oid_to_hex(&o->oid), path) < 0)
170 return -1;
172 return 0;
175 static int generate_info_refs(struct update_info_ctx *uic)
177 return for_each_ref(add_info_ref, uic);
180 static int update_info_refs(int force)
182 char *path = git_pathdup("info/refs");
183 int ret = update_info_file(path, generate_info_refs, force);
184 free(path);
185 return ret;
188 /* packs */
189 static struct pack_info {
190 struct packed_git *p;
191 int old_num;
192 int new_num;
193 } **info;
194 static int num_pack;
196 static struct pack_info *find_pack_by_name(const char *name)
198 int i;
199 for (i = 0; i < num_pack; i++) {
200 struct packed_git *p = info[i]->p;
201 if (!strcmp(pack_basename(p), name))
202 return info[i];
204 return NULL;
207 /* Returns non-zero when we detect that the info in the
208 * old file is useless.
210 static int parse_pack_def(const char *packname, int old_cnt)
212 struct pack_info *i = find_pack_by_name(packname);
213 if (i) {
214 i->old_num = old_cnt;
215 return 0;
217 else {
218 /* The file describes a pack that is no longer here */
219 return 1;
223 /* Returns non-zero when we detect that the info in the
224 * old file is useless.
226 static int read_pack_info_file(const char *infofile)
228 FILE *fp;
229 struct strbuf line = STRBUF_INIT;
230 int old_cnt = 0;
231 int stale = 1;
233 fp = fopen_or_warn(infofile, "r");
234 if (!fp)
235 return 1; /* nonexistent is not an error. */
237 while (strbuf_getline(&line, fp) != EOF) {
238 const char *arg;
240 if (!line.len)
241 continue;
243 if (skip_prefix(line.buf, "P ", &arg)) {
244 /* P name */
245 if (parse_pack_def(arg, old_cnt++))
246 goto out_stale;
247 } else if (line.buf[0] == 'D') {
248 /* we used to emit D but that was misguided. */
249 goto out_stale;
250 } else if (line.buf[0] == 'T') {
251 /* we used to emit T but nobody uses it. */
252 goto out_stale;
253 } else {
254 error("unrecognized: %s", line.buf);
257 stale = 0;
259 out_stale:
260 strbuf_release(&line);
261 fclose(fp);
262 return stale;
265 static int compare_info(const void *a_, const void *b_)
267 struct pack_info *const *a = a_;
268 struct pack_info *const *b = b_;
270 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
271 /* Keep the order in the original */
272 return (*a)->old_num - (*b)->old_num;
273 else if (0 <= (*a)->old_num)
274 /* Only A existed in the original so B is obviously newer */
275 return -1;
276 else if (0 <= (*b)->old_num)
277 /* The other way around. */
278 return 1;
280 /* then it does not matter but at least keep the comparison stable */
281 if ((*a)->p == (*b)->p)
282 return 0;
283 else if ((*a)->p < (*b)->p)
284 return -1;
285 else
286 return 1;
289 static void init_pack_info(const char *infofile, int force)
291 struct packed_git *p;
292 int stale;
293 int i;
294 size_t alloc = 0;
296 for (p = get_all_packs(the_repository); p; p = p->next) {
297 /* we ignore things on alternate path since they are
298 * not available to the pullers in general.
300 if (!p->pack_local || !file_exists(p->pack_name))
301 continue;
303 i = num_pack++;
304 ALLOC_GROW(info, num_pack, alloc);
305 CALLOC_ARRAY(info[i], 1);
306 info[i]->p = p;
307 info[i]->old_num = -1;
310 if (infofile && !force)
311 stale = read_pack_info_file(infofile);
312 else
313 stale = 1;
315 for (i = 0; i < num_pack; i++)
316 if (stale)
317 info[i]->old_num = -1;
319 /* renumber them */
320 QSORT(info, num_pack, compare_info);
321 for (i = 0; i < num_pack; i++)
322 info[i]->new_num = i;
325 static void free_pack_info(void)
327 int i;
328 for (i = 0; i < num_pack; i++)
329 free(info[i]);
330 free(info);
333 static int write_pack_info_file(struct update_info_ctx *uic)
335 int i;
336 for (i = 0; i < num_pack; i++) {
337 if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0)
338 return -1;
340 if (uic_printf(uic, "\n") < 0)
341 return -1;
342 return 0;
345 static int update_info_packs(int force)
347 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
348 int ret;
350 init_pack_info(infofile, force);
351 ret = update_info_file(infofile, write_pack_info_file, force);
352 free_pack_info();
353 free(infofile);
354 return ret;
357 /* public */
358 int update_server_info(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;
366 errs = errs | update_info_refs(force);
367 errs = errs | update_info_packs(force);
369 /* remove leftover rev-cache file if there is any */
370 unlink_or_warn(git_path("info/rev-cache"));
372 return errs;