]>
Commit | Line | Data |
---|---|---|
8f3f9b09 | 1 | #include "cache.h" |
a80d72db | 2 | #include "repository.h" |
8f3f9b09 JH |
3 | #include "refs.h" |
4 | #include "object.h" | |
5 | #include "commit.h" | |
b614e3d7 | 6 | #include "tag.h" |
0abe14f6 | 7 | #include "packfile.h" |
a80d72db | 8 | #include "object-store.h" |
8f3f9b09 | 9 | |
d38379ec JK |
10 | /* |
11 | * Create the file "path" by writing to a temporary file and renaming | |
12 | * it into place. The contents of the file come from "generate", which | |
13 | * should return non-zero if it encounters an error. | |
14 | */ | |
15 | static int update_info_file(char *path, int (*generate)(FILE *)) | |
16 | { | |
17 | char *tmp = mkpathdup("%s_XXXXXX", path); | |
18 | int ret = -1; | |
19 | int fd = -1; | |
fa1912c8 | 20 | FILE *fp = NULL, *to_close; |
d38379ec JK |
21 | |
22 | safe_create_leading_directories(path); | |
d91175b2 | 23 | fd = git_mkstemp_mode(tmp, 0666); |
d38379ec JK |
24 | if (fd < 0) |
25 | goto out; | |
fa1912c8 | 26 | to_close = fp = fdopen(fd, "w"); |
d38379ec JK |
27 | if (!fp) |
28 | goto out; | |
fa1912c8 | 29 | fd = -1; |
d38379ec JK |
30 | ret = generate(fp); |
31 | if (ret) | |
32 | goto out; | |
fa1912c8 RS |
33 | fp = NULL; |
34 | if (fclose(to_close)) | |
d38379ec JK |
35 | goto out; |
36 | if (adjust_shared_perm(tmp) < 0) | |
37 | goto out; | |
38 | if (rename(tmp, path) < 0) | |
39 | goto out; | |
40 | ret = 0; | |
41 | ||
42 | out: | |
43 | if (ret) { | |
02382f51 | 44 | error_errno("unable to update %s", path); |
d38379ec JK |
45 | if (fp) |
46 | fclose(fp); | |
47 | else if (fd >= 0) | |
48 | close(fd); | |
49 | unlink(tmp); | |
50 | } | |
51 | free(tmp); | |
52 | return ret; | |
53 | } | |
8f3f9b09 | 54 | |
e2b0bcdf MH |
55 | static int add_info_ref(const char *path, const struct object_id *oid, |
56 | int flag, void *cb_data) | |
8f3f9b09 | 57 | { |
d38379ec | 58 | FILE *fp = cb_data; |
109cd76d | 59 | struct object *o = parse_object(the_repository, oid); |
76f8a302 SP |
60 | if (!o) |
61 | return -1; | |
f6b42a81 | 62 | |
e2b0bcdf | 63 | if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0) |
d38379ec JK |
64 | return -1; |
65 | ||
1974632c | 66 | if (o->type == OBJ_TAG) { |
a74093da | 67 | o = deref_tag(the_repository, o, path, 0); |
9534f40b | 68 | if (o) |
d38379ec | 69 | if (fprintf(fp, "%s %s^{}\n", |
f2fd0760 | 70 | oid_to_hex(&o->oid), path) < 0) |
d38379ec | 71 | return -1; |
f6b42a81 | 72 | } |
8f3f9b09 JH |
73 | return 0; |
74 | } | |
75 | ||
d38379ec JK |
76 | static int generate_info_refs(FILE *fp) |
77 | { | |
e2b0bcdf | 78 | return for_each_ref(add_info_ref, fp); |
d38379ec JK |
79 | } |
80 | ||
b3223761 | 81 | static int update_info_refs(void) |
8f3f9b09 | 82 | { |
d38379ec JK |
83 | char *path = git_pathdup("info/refs"); |
84 | int ret = update_info_file(path, generate_info_refs); | |
85 | free(path); | |
86 | return ret; | |
8f3f9b09 JH |
87 | } |
88 | ||
89 | /* packs */ | |
4d8fa916 | 90 | static struct pack_info { |
8f3f9b09 JH |
91 | struct packed_git *p; |
92 | int old_num; | |
93 | int new_num; | |
8f3f9b09 JH |
94 | } **info; |
95 | static int num_pack; | |
8f3f9b09 | 96 | |
8f3f9b09 JH |
97 | static struct pack_info *find_pack_by_name(const char *name) |
98 | { | |
99 | int i; | |
100 | for (i = 0; i < num_pack; i++) { | |
101 | struct packed_git *p = info[i]->p; | |
b9fb142e | 102 | if (!strcmp(pack_basename(p), name)) |
8f3f9b09 JH |
103 | return info[i]; |
104 | } | |
105 | return NULL; | |
106 | } | |
107 | ||
8f3f9b09 JH |
108 | /* Returns non-zero when we detect that the info in the |
109 | * old file is useless. | |
110 | */ | |
b83a3089 | 111 | static int parse_pack_def(const char *packname, int old_cnt) |
8f3f9b09 | 112 | { |
b83a3089 | 113 | struct pack_info *i = find_pack_by_name(packname); |
8f3f9b09 JH |
114 | if (i) { |
115 | i->old_num = old_cnt; | |
116 | return 0; | |
117 | } | |
118 | else { | |
6f42f89c | 119 | /* The file describes a pack that is no longer here */ |
8f3f9b09 JH |
120 | return 1; |
121 | } | |
122 | } | |
123 | ||
8f3f9b09 JH |
124 | /* Returns non-zero when we detect that the info in the |
125 | * old file is useless. | |
126 | */ | |
127 | static int read_pack_info_file(const char *infofile) | |
128 | { | |
129 | FILE *fp; | |
4ecbd649 | 130 | struct strbuf line = STRBUF_INIT; |
8f3f9b09 | 131 | int old_cnt = 0; |
965cc517 | 132 | int stale = 1; |
8f3f9b09 | 133 | |
e9d983f1 | 134 | fp = fopen_or_warn(infofile, "r"); |
8f3f9b09 | 135 | if (!fp) |
addf88e4 | 136 | return 1; /* nonexistent is not an error. */ |
8f3f9b09 | 137 | |
4ecbd649 | 138 | while (strbuf_getline(&line, fp) != EOF) { |
b83a3089 | 139 | const char *arg; |
8ac4838a | 140 | |
4ecbd649 | 141 | if (!line.len) |
8ac4838a | 142 | continue; |
8f3f9b09 | 143 | |
4ecbd649 | 144 | if (skip_prefix(line.buf, "P ", &arg)) { |
b83a3089 JK |
145 | /* P name */ |
146 | if (parse_pack_def(arg, old_cnt++)) | |
8f3f9b09 | 147 | goto out_stale; |
4ecbd649 | 148 | } else if (line.buf[0] == 'D') { |
b83a3089 | 149 | /* we used to emit D but that was misguided. */ |
3e15c67c | 150 | goto out_stale; |
4ecbd649 | 151 | } else if (line.buf[0] == 'T') { |
b83a3089 JK |
152 | /* we used to emit T but nobody uses it. */ |
153 | goto out_stale; | |
154 | } else { | |
4ecbd649 | 155 | error("unrecognized: %s", line.buf); |
8f3f9b09 JH |
156 | } |
157 | } | |
965cc517 JK |
158 | stale = 0; |
159 | ||
8f3f9b09 | 160 | out_stale: |
4ecbd649 | 161 | strbuf_release(&line); |
8f3f9b09 | 162 | fclose(fp); |
965cc517 | 163 | return stale; |
8f3f9b09 JH |
164 | } |
165 | ||
8f3f9b09 JH |
166 | static int compare_info(const void *a_, const void *b_) |
167 | { | |
4b25d091 FC |
168 | struct pack_info *const *a = a_; |
169 | struct pack_info *const *b = b_; | |
8f3f9b09 JH |
170 | |
171 | if (0 <= (*a)->old_num && 0 <= (*b)->old_num) | |
172 | /* Keep the order in the original */ | |
173 | return (*a)->old_num - (*b)->old_num; | |
174 | else if (0 <= (*a)->old_num) | |
175 | /* Only A existed in the original so B is obviously newer */ | |
176 | return -1; | |
177 | else if (0 <= (*b)->old_num) | |
178 | /* The other way around. */ | |
179 | return 1; | |
180 | ||
d5eac498 | 181 | /* then it does not matter but at least keep the comparison stable */ |
2dee5816 JH |
182 | if ((*a)->p == (*b)->p) |
183 | return 0; | |
184 | else if ((*a)->p < (*b)->p) | |
185 | return -1; | |
186 | else | |
187 | return 1; | |
8f3f9b09 JH |
188 | } |
189 | ||
190 | static void init_pack_info(const char *infofile, int force) | |
191 | { | |
192 | struct packed_git *p; | |
193 | int stale; | |
194 | int i = 0; | |
8f3f9b09 | 195 | |
454ea2e4 | 196 | for (p = get_all_packs(the_repository); p; p = p->next) { |
8f3f9b09 JH |
197 | /* we ignore things on alternate path since they are |
198 | * not available to the pullers in general. | |
199 | */ | |
f13d7db4 | 200 | if (!p->pack_local) |
8f3f9b09 JH |
201 | continue; |
202 | i++; | |
203 | } | |
204 | num_pack = i; | |
205 | info = xcalloc(num_pack, sizeof(struct pack_info *)); | |
454ea2e4 | 206 | for (i = 0, p = get_all_packs(the_repository); p; p = p->next) { |
f13d7db4 | 207 | if (!p->pack_local) |
8f3f9b09 | 208 | continue; |
79bb8b3c | 209 | assert(i < num_pack); |
6f42f89c | 210 | info[i] = xcalloc(1, sizeof(struct pack_info)); |
8f3f9b09 JH |
211 | info[i]->p = p; |
212 | info[i]->old_num = -1; | |
213 | i++; | |
214 | } | |
215 | ||
216 | if (infofile && !force) | |
217 | stale = read_pack_info_file(infofile); | |
218 | else | |
219 | stale = 1; | |
220 | ||
910710bb | 221 | for (i = 0; i < num_pack; i++) |
222 | if (stale) | |
8f3f9b09 | 223 | info[i]->old_num = -1; |
8f3f9b09 | 224 | |
6f42f89c | 225 | /* renumber them */ |
9ed0d8d6 | 226 | QSORT(info, num_pack, compare_info); |
8f3f9b09 JH |
227 | for (i = 0; i < num_pack; i++) |
228 | info[i]->new_num = i; | |
8f3f9b09 JH |
229 | } |
230 | ||
3907a407 JK |
231 | static void free_pack_info(void) |
232 | { | |
233 | int i; | |
234 | for (i = 0; i < num_pack; i++) | |
235 | free(info[i]); | |
236 | free(info); | |
237 | } | |
238 | ||
d38379ec | 239 | static int write_pack_info_file(FILE *fp) |
8f3f9b09 | 240 | { |
3e15c67c | 241 | int i; |
d38379ec | 242 | for (i = 0; i < num_pack; i++) { |
b9fb142e | 243 | if (fprintf(fp, "P %s\n", pack_basename(info[i]->p)) < 0) |
d38379ec JK |
244 | return -1; |
245 | } | |
246 | if (fputc('\n', fp) == EOF) | |
247 | return -1; | |
248 | return 0; | |
8f3f9b09 JH |
249 | } |
250 | ||
251 | static int update_info_packs(int force) | |
252 | { | |
d38379ec JK |
253 | char *infofile = mkpathdup("%s/info/packs", get_object_directory()); |
254 | int ret; | |
8f3f9b09 JH |
255 | |
256 | init_pack_info(infofile, force); | |
d38379ec | 257 | ret = update_info_file(infofile, write_pack_info_file); |
3907a407 | 258 | free_pack_info(); |
d38379ec JK |
259 | free(infofile); |
260 | return ret; | |
8f3f9b09 JH |
261 | } |
262 | ||
8f3f9b09 JH |
263 | /* public */ |
264 | int update_server_info(int force) | |
265 | { | |
266 | /* We would add more dumb-server support files later, | |
267 | * including index of available pack files and their | |
268 | * intended audiences. | |
269 | */ | |
270 | int errs = 0; | |
271 | ||
b3223761 | 272 | errs = errs | update_info_refs(); |
8f3f9b09 | 273 | errs = errs | update_info_packs(force); |
8f3f9b09 | 274 | |
6f42f89c | 275 | /* remove leftover rev-cache file if there is any */ |
691f1a28 | 276 | unlink_or_warn(git_path("info/rev-cache")); |
6f42f89c | 277 | |
8f3f9b09 JH |
278 | return errs; |
279 | } |