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