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