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