]>
Commit | Line | Data |
---|---|---|
623b80be | 1 | #include "git-compat-util.h" |
e941c48d | 2 | #include "dir.h" |
32a8f510 | 3 | #include "environment.h" |
41771fa4 | 4 | #include "hex.h" |
a80d72db | 5 | #include "repository.h" |
8f3f9b09 JH |
6 | #include "refs.h" |
7 | #include "object.h" | |
8 | #include "commit.h" | |
b614e3d7 | 9 | #include "tag.h" |
0abe14f6 | 10 | #include "packfile.h" |
c339932b | 11 | #include "path.h" |
87bed179 | 12 | #include "object-file.h" |
a034e910 | 13 | #include "object-store-ll.h" |
623b80be | 14 | #include "server-info.h" |
f4f476b6 EW |
15 | #include "strbuf.h" |
16 | ||
17 | struct update_info_ctx { | |
18 | FILE *cur_fp; | |
19 | FILE *old_fp; /* becomes NULL if it differs from cur_fp */ | |
20 | struct strbuf cur_sb; | |
21 | struct strbuf old_sb; | |
22 | }; | |
23 | ||
24 | static void uic_mark_stale(struct update_info_ctx *uic) | |
25 | { | |
26 | fclose(uic->old_fp); | |
27 | uic->old_fp = NULL; | |
28 | } | |
29 | ||
30 | static int uic_is_stale(const struct update_info_ctx *uic) | |
31 | { | |
32 | return uic->old_fp == NULL; | |
33 | } | |
34 | ||
48ca53ca | 35 | __attribute__((format (printf, 2, 3))) |
f4f476b6 EW |
36 | static int uic_printf(struct update_info_ctx *uic, const char *fmt, ...) |
37 | { | |
38 | va_list ap; | |
39 | int ret = -1; | |
40 | ||
41 | va_start(ap, fmt); | |
42 | ||
43 | if (uic_is_stale(uic)) { | |
44 | ret = vfprintf(uic->cur_fp, fmt, ap); | |
45 | } else { | |
46 | ssize_t r; | |
47 | struct strbuf *cur = &uic->cur_sb; | |
48 | struct strbuf *old = &uic->old_sb; | |
49 | ||
50 | strbuf_reset(cur); | |
51 | strbuf_vinsertf(cur, 0, fmt, ap); | |
52 | ||
53 | strbuf_reset(old); | |
54 | strbuf_grow(old, cur->len); | |
55 | r = fread(old->buf, 1, cur->len, uic->old_fp); | |
56 | if (r != cur->len || memcmp(old->buf, cur->buf, r)) | |
57 | uic_mark_stale(uic); | |
58 | ||
59 | if (fwrite(cur->buf, 1, cur->len, uic->cur_fp) == cur->len) | |
60 | ret = 0; | |
61 | } | |
62 | ||
63 | va_end(ap); | |
64 | ||
65 | return ret; | |
66 | } | |
8f3f9b09 | 67 | |
d38379ec JK |
68 | /* |
69 | * Create the file "path" by writing to a temporary file and renaming | |
70 | * it into place. The contents of the file come from "generate", which | |
71 | * should return non-zero if it encounters an error. | |
72 | */ | |
f4f476b6 EW |
73 | static int update_info_file(char *path, |
74 | int (*generate)(struct update_info_ctx *), | |
75 | int force) | |
d38379ec JK |
76 | { |
77 | char *tmp = mkpathdup("%s_XXXXXX", path); | |
78 | int ret = -1; | |
79 | int fd = -1; | |
f4f476b6 EW |
80 | FILE *to_close; |
81 | struct update_info_ctx uic = { | |
82 | .cur_fp = NULL, | |
83 | .old_fp = NULL, | |
84 | .cur_sb = STRBUF_INIT, | |
85 | .old_sb = STRBUF_INIT | |
86 | }; | |
d38379ec JK |
87 | |
88 | safe_create_leading_directories(path); | |
d91175b2 | 89 | fd = git_mkstemp_mode(tmp, 0666); |
d38379ec JK |
90 | if (fd < 0) |
91 | goto out; | |
f4f476b6 EW |
92 | to_close = uic.cur_fp = fdopen(fd, "w"); |
93 | if (!uic.cur_fp) | |
d38379ec | 94 | goto out; |
fa1912c8 | 95 | fd = -1; |
f4f476b6 EW |
96 | |
97 | /* no problem on ENOENT and old_fp == NULL, it's stale, now */ | |
98 | if (!force) | |
99 | uic.old_fp = fopen_or_warn(path, "r"); | |
100 | ||
101 | /* | |
15beaaa3 | 102 | * uic_printf will compare incremental comparison against old_fp |
f4f476b6 EW |
103 | * and mark uic as stale if needed |
104 | */ | |
105 | ret = generate(&uic); | |
d38379ec JK |
106 | if (ret) |
107 | goto out; | |
f4f476b6 EW |
108 | |
109 | /* new file may be shorter than the old one, check here */ | |
110 | if (!uic_is_stale(&uic)) { | |
111 | struct stat st; | |
112 | long new_len = ftell(uic.cur_fp); | |
113 | int old_fd = fileno(uic.old_fp); | |
114 | ||
115 | if (new_len < 0) { | |
116 | ret = -1; | |
117 | goto out; | |
118 | } | |
119 | if (fstat(old_fd, &st) || (st.st_size != (size_t)new_len)) | |
120 | uic_mark_stale(&uic); | |
121 | } | |
122 | ||
123 | uic.cur_fp = NULL; | |
fa1912c8 | 124 | if (fclose(to_close)) |
d38379ec | 125 | goto out; |
f4f476b6 EW |
126 | |
127 | if (uic_is_stale(&uic)) { | |
128 | if (adjust_shared_perm(tmp) < 0) | |
129 | goto out; | |
130 | if (rename(tmp, path) < 0) | |
131 | goto out; | |
132 | } else { | |
133 | unlink(tmp); | |
134 | } | |
d38379ec JK |
135 | ret = 0; |
136 | ||
137 | out: | |
138 | if (ret) { | |
02382f51 | 139 | error_errno("unable to update %s", path); |
f4f476b6 EW |
140 | if (uic.cur_fp) |
141 | fclose(uic.cur_fp); | |
d38379ec JK |
142 | else if (fd >= 0) |
143 | close(fd); | |
144 | unlink(tmp); | |
145 | } | |
146 | free(tmp); | |
f4f476b6 EW |
147 | if (uic.old_fp) |
148 | fclose(uic.old_fp); | |
149 | strbuf_release(&uic.old_sb); | |
150 | strbuf_release(&uic.cur_sb); | |
d38379ec JK |
151 | return ret; |
152 | } | |
8f3f9b09 | 153 | |
e2b0bcdf | 154 | static int add_info_ref(const char *path, const struct object_id *oid, |
5cf88fd8 | 155 | int flag UNUSED, |
63e14ee2 | 156 | void *cb_data) |
8f3f9b09 | 157 | { |
f4f476b6 | 158 | struct update_info_ctx *uic = cb_data; |
109cd76d | 159 | struct object *o = parse_object(the_repository, oid); |
76f8a302 SP |
160 | if (!o) |
161 | return -1; | |
f6b42a81 | 162 | |
f4f476b6 | 163 | if (uic_printf(uic, "%s %s\n", oid_to_hex(oid), path) < 0) |
d38379ec JK |
164 | return -1; |
165 | ||
1974632c | 166 | if (o->type == OBJ_TAG) { |
a74093da | 167 | o = deref_tag(the_repository, o, path, 0); |
9534f40b | 168 | if (o) |
f4f476b6 | 169 | if (uic_printf(uic, "%s %s^{}\n", |
f2fd0760 | 170 | oid_to_hex(&o->oid), path) < 0) |
d38379ec | 171 | return -1; |
f6b42a81 | 172 | } |
8f3f9b09 JH |
173 | return 0; |
174 | } | |
175 | ||
f4f476b6 | 176 | static int generate_info_refs(struct update_info_ctx *uic) |
d38379ec | 177 | { |
f4f476b6 | 178 | return for_each_ref(add_info_ref, uic); |
d38379ec JK |
179 | } |
180 | ||
f4f476b6 | 181 | static int update_info_refs(int force) |
8f3f9b09 | 182 | { |
d38379ec | 183 | char *path = git_pathdup("info/refs"); |
f4f476b6 | 184 | int ret = update_info_file(path, generate_info_refs, force); |
d38379ec JK |
185 | free(path); |
186 | return ret; | |
8f3f9b09 JH |
187 | } |
188 | ||
189 | /* packs */ | |
4d8fa916 | 190 | static struct pack_info { |
8f3f9b09 JH |
191 | struct packed_git *p; |
192 | int old_num; | |
193 | int new_num; | |
8f3f9b09 JH |
194 | } **info; |
195 | static int num_pack; | |
8f3f9b09 | 196 | |
8f3f9b09 JH |
197 | static struct pack_info *find_pack_by_name(const char *name) |
198 | { | |
199 | int i; | |
200 | for (i = 0; i < num_pack; i++) { | |
201 | struct packed_git *p = info[i]->p; | |
b9fb142e | 202 | if (!strcmp(pack_basename(p), name)) |
8f3f9b09 JH |
203 | return info[i]; |
204 | } | |
205 | return NULL; | |
206 | } | |
207 | ||
8f3f9b09 JH |
208 | /* Returns non-zero when we detect that the info in the |
209 | * old file is useless. | |
210 | */ | |
b83a3089 | 211 | static int parse_pack_def(const char *packname, int old_cnt) |
8f3f9b09 | 212 | { |
b83a3089 | 213 | struct pack_info *i = find_pack_by_name(packname); |
8f3f9b09 JH |
214 | if (i) { |
215 | i->old_num = old_cnt; | |
216 | return 0; | |
217 | } | |
218 | else { | |
6f42f89c | 219 | /* The file describes a pack that is no longer here */ |
8f3f9b09 JH |
220 | return 1; |
221 | } | |
222 | } | |
223 | ||
8f3f9b09 JH |
224 | /* Returns non-zero when we detect that the info in the |
225 | * old file is useless. | |
226 | */ | |
227 | static int read_pack_info_file(const char *infofile) | |
228 | { | |
229 | FILE *fp; | |
4ecbd649 | 230 | struct strbuf line = STRBUF_INIT; |
8f3f9b09 | 231 | int old_cnt = 0; |
965cc517 | 232 | int stale = 1; |
8f3f9b09 | 233 | |
e9d983f1 | 234 | fp = fopen_or_warn(infofile, "r"); |
8f3f9b09 | 235 | if (!fp) |
addf88e4 | 236 | return 1; /* nonexistent is not an error. */ |
8f3f9b09 | 237 | |
4ecbd649 | 238 | while (strbuf_getline(&line, fp) != EOF) { |
b83a3089 | 239 | const char *arg; |
8ac4838a | 240 | |
4ecbd649 | 241 | if (!line.len) |
8ac4838a | 242 | continue; |
8f3f9b09 | 243 | |
4ecbd649 | 244 | if (skip_prefix(line.buf, "P ", &arg)) { |
b83a3089 JK |
245 | /* P name */ |
246 | if (parse_pack_def(arg, old_cnt++)) | |
8f3f9b09 | 247 | goto out_stale; |
4ecbd649 | 248 | } else if (line.buf[0] == 'D') { |
b83a3089 | 249 | /* we used to emit D but that was misguided. */ |
3e15c67c | 250 | goto out_stale; |
4ecbd649 | 251 | } else if (line.buf[0] == 'T') { |
b83a3089 JK |
252 | /* we used to emit T but nobody uses it. */ |
253 | goto out_stale; | |
254 | } else { | |
4ecbd649 | 255 | error("unrecognized: %s", line.buf); |
8f3f9b09 JH |
256 | } |
257 | } | |
965cc517 JK |
258 | stale = 0; |
259 | ||
8f3f9b09 | 260 | out_stale: |
4ecbd649 | 261 | strbuf_release(&line); |
8f3f9b09 | 262 | fclose(fp); |
965cc517 | 263 | return stale; |
8f3f9b09 JH |
264 | } |
265 | ||
8f3f9b09 JH |
266 | static int compare_info(const void *a_, const void *b_) |
267 | { | |
4b25d091 FC |
268 | struct pack_info *const *a = a_; |
269 | struct pack_info *const *b = b_; | |
8f3f9b09 JH |
270 | |
271 | if (0 <= (*a)->old_num && 0 <= (*b)->old_num) | |
272 | /* Keep the order in the original */ | |
273 | return (*a)->old_num - (*b)->old_num; | |
274 | else if (0 <= (*a)->old_num) | |
275 | /* Only A existed in the original so B is obviously newer */ | |
276 | return -1; | |
277 | else if (0 <= (*b)->old_num) | |
278 | /* The other way around. */ | |
279 | return 1; | |
280 | ||
d5eac498 | 281 | /* then it does not matter but at least keep the comparison stable */ |
2dee5816 JH |
282 | if ((*a)->p == (*b)->p) |
283 | return 0; | |
284 | else if ((*a)->p < (*b)->p) | |
285 | return -1; | |
286 | else | |
287 | return 1; | |
8f3f9b09 JH |
288 | } |
289 | ||
290 | static void init_pack_info(const char *infofile, int force) | |
291 | { | |
292 | struct packed_git *p; | |
293 | int stale; | |
e941c48d EW |
294 | int i; |
295 | size_t alloc = 0; | |
8f3f9b09 | 296 | |
454ea2e4 | 297 | for (p = get_all_packs(the_repository); p; p = p->next) { |
8f3f9b09 JH |
298 | /* we ignore things on alternate path since they are |
299 | * not available to the pullers in general. | |
300 | */ | |
e941c48d | 301 | if (!p->pack_local || !file_exists(p->pack_name)) |
8f3f9b09 | 302 | continue; |
e941c48d EW |
303 | |
304 | i = num_pack++; | |
305 | ALLOC_GROW(info, num_pack, alloc); | |
ca56dadb | 306 | CALLOC_ARRAY(info[i], 1); |
8f3f9b09 JH |
307 | info[i]->p = p; |
308 | info[i]->old_num = -1; | |
8f3f9b09 JH |
309 | } |
310 | ||
311 | if (infofile && !force) | |
312 | stale = read_pack_info_file(infofile); | |
313 | else | |
314 | stale = 1; | |
315 | ||
910710bb | 316 | for (i = 0; i < num_pack; i++) |
317 | if (stale) | |
8f3f9b09 | 318 | info[i]->old_num = -1; |
8f3f9b09 | 319 | |
6f42f89c | 320 | /* renumber them */ |
9ed0d8d6 | 321 | QSORT(info, num_pack, compare_info); |
8f3f9b09 JH |
322 | for (i = 0; i < num_pack; i++) |
323 | info[i]->new_num = i; | |
8f3f9b09 JH |
324 | } |
325 | ||
3907a407 JK |
326 | static void free_pack_info(void) |
327 | { | |
328 | int i; | |
329 | for (i = 0; i < num_pack; i++) | |
330 | free(info[i]); | |
331 | free(info); | |
332 | } | |
333 | ||
f4f476b6 | 334 | static int write_pack_info_file(struct update_info_ctx *uic) |
8f3f9b09 | 335 | { |
3e15c67c | 336 | int i; |
d38379ec | 337 | for (i = 0; i < num_pack; i++) { |
f4f476b6 | 338 | if (uic_printf(uic, "P %s\n", pack_basename(info[i]->p)) < 0) |
d38379ec JK |
339 | return -1; |
340 | } | |
f4f476b6 | 341 | if (uic_printf(uic, "\n") < 0) |
d38379ec JK |
342 | return -1; |
343 | return 0; | |
8f3f9b09 JH |
344 | } |
345 | ||
346 | static int update_info_packs(int force) | |
347 | { | |
d38379ec JK |
348 | char *infofile = mkpathdup("%s/info/packs", get_object_directory()); |
349 | int ret; | |
8f3f9b09 JH |
350 | |
351 | init_pack_info(infofile, force); | |
f4f476b6 | 352 | ret = update_info_file(infofile, write_pack_info_file, force); |
3907a407 | 353 | free_pack_info(); |
d38379ec JK |
354 | free(infofile); |
355 | return ret; | |
8f3f9b09 JH |
356 | } |
357 | ||
8f3f9b09 JH |
358 | /* public */ |
359 | int update_server_info(int force) | |
360 | { | |
361 | /* We would add more dumb-server support files later, | |
362 | * including index of available pack files and their | |
363 | * intended audiences. | |
364 | */ | |
365 | int errs = 0; | |
366 | ||
f4f476b6 | 367 | errs = errs | update_info_refs(force); |
8f3f9b09 | 368 | errs = errs | update_info_packs(force); |
8f3f9b09 | 369 | |
6f42f89c | 370 | /* remove leftover rev-cache file if there is any */ |
691f1a28 | 371 | unlink_or_warn(git_path("info/rev-cache")); |
6f42f89c | 372 | |
8f3f9b09 JH |
373 | return errs; |
374 | } |