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