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