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