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