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