]> git.ipfire.org Git - thirdparty/git.git/blame - server-info.c
sequencer.c: use error_errno()
[thirdparty/git.git] / server-info.c
CommitLineData
8f3f9b09
JH
1#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "commit.h"
b614e3d7 5#include "tag.h"
8f3f9b09 6
d38379ec
JK
7/*
8 * Create the file "path" by writing to a temporary file and renaming
9 * it into place. The contents of the file come from "generate", which
10 * should return non-zero if it encounters an error.
11 */
12static int update_info_file(char *path, int (*generate)(FILE *))
13{
14 char *tmp = mkpathdup("%s_XXXXXX", path);
15 int ret = -1;
16 int fd = -1;
17 FILE *fp = NULL;
18
19 safe_create_leading_directories(path);
d91175b2 20 fd = git_mkstemp_mode(tmp, 0666);
d38379ec
JK
21 if (fd < 0)
22 goto out;
23 fp = fdopen(fd, "w");
24 if (!fp)
25 goto out;
26 ret = generate(fp);
27 if (ret)
28 goto out;
29 if (fclose(fp))
30 goto out;
31 if (adjust_shared_perm(tmp) < 0)
32 goto out;
33 if (rename(tmp, path) < 0)
34 goto out;
35 ret = 0;
36
37out:
38 if (ret) {
39 error("unable to update %s: %s", path, strerror(errno));
40 if (fp)
41 fclose(fp);
42 else if (fd >= 0)
43 close(fd);
44 unlink(tmp);
45 }
46 free(tmp);
47 return ret;
48}
8f3f9b09 49
e2b0bcdf
MH
50static int add_info_ref(const char *path, const struct object_id *oid,
51 int flag, void *cb_data)
8f3f9b09 52{
d38379ec 53 FILE *fp = cb_data;
e2b0bcdf 54 struct object *o = parse_object(oid->hash);
76f8a302
SP
55 if (!o)
56 return -1;
f6b42a81 57
e2b0bcdf 58 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
d38379ec
JK
59 return -1;
60
1974632c 61 if (o->type == OBJ_TAG) {
9534f40b
JH
62 o = deref_tag(o, path, 0);
63 if (o)
d38379ec 64 if (fprintf(fp, "%s %s^{}\n",
f2fd0760 65 oid_to_hex(&o->oid), path) < 0)
d38379ec 66 return -1;
f6b42a81 67 }
8f3f9b09
JH
68 return 0;
69}
70
d38379ec
JK
71static int generate_info_refs(FILE *fp)
72{
e2b0bcdf 73 return for_each_ref(add_info_ref, fp);
d38379ec
JK
74}
75
8f3f9b09
JH
76static int update_info_refs(int force)
77{
d38379ec
JK
78 char *path = git_pathdup("info/refs");
79 int ret = update_info_file(path, generate_info_refs);
80 free(path);
81 return ret;
8f3f9b09
JH
82}
83
84/* packs */
4d8fa916 85static struct pack_info {
8f3f9b09
JH
86 struct packed_git *p;
87 int old_num;
88 int new_num;
89 int nr_alloc;
90 int nr_heads;
91 unsigned char (*head)[20];
8f3f9b09
JH
92} **info;
93static int num_pack;
94static const char *objdir;
95static int objdirlen;
96
8f3f9b09
JH
97static 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;
102 /* skip "/pack/" after ".git/objects" */
103 if (!strcmp(p->pack_name + objdirlen + 6, name))
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 */
112static int parse_pack_def(const char *line, int old_cnt)
113{
114 struct pack_info *i = find_pack_by_name(line + 2);
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;
131 char line[1000];
132 int old_cnt = 0;
133
134 fp = fopen(infofile, "r");
135 if (!fp)
addf88e4 136 return 1; /* nonexistent is not an error. */
8f3f9b09
JH
137
138 while (fgets(line, sizeof(line), fp)) {
139 int len = strlen(line);
872c930d 140 if (len && line[len-1] == '\n')
8ac4838a
JH
141 line[--len] = 0;
142
143 if (!len)
144 continue;
8f3f9b09
JH
145
146 switch (line[0]) {
147 case 'P': /* P name */
148 if (parse_pack_def(line, old_cnt++))
149 goto out_stale;
150 break;
6f42f89c 151 case 'D': /* we used to emit D but that was misguided. */
3e15c67c
JH
152 case 'T': /* we used to emit T but nobody uses it. */
153 goto out_stale;
8f3f9b09
JH
154 default:
155 error("unrecognized: %s", line);
156 break;
157 }
158 }
159 fclose(fp);
160 return 0;
161 out_stale:
162 fclose(fp);
163 return 1;
164}
165
8f3f9b09
JH
166static 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
190static void init_pack_info(const char *infofile, int force)
191{
192 struct packed_git *p;
193 int stale;
194 int i = 0;
8f3f9b09
JH
195
196 objdir = get_object_directory();
197 objdirlen = strlen(objdir);
198
199 prepare_packed_git();
200 for (p = packed_git; p; p = p->next) {
201 /* we ignore things on alternate path since they are
202 * not available to the pullers in general.
203 */
f13d7db4 204 if (!p->pack_local)
8f3f9b09
JH
205 continue;
206 i++;
207 }
208 num_pack = i;
209 info = xcalloc(num_pack, sizeof(struct pack_info *));
210 for (i = 0, p = packed_git; p; p = p->next) {
f13d7db4 211 if (!p->pack_local)
8f3f9b09 212 continue;
6f42f89c 213 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b09
JH
214 info[i]->p = p;
215 info[i]->old_num = -1;
216 i++;
217 }
218
219 if (infofile && !force)
220 stale = read_pack_info_file(infofile);
221 else
222 stale = 1;
223
224 for (i = 0; i < num_pack; i++) {
225 if (stale) {
226 info[i]->old_num = -1;
8f3f9b09
JH
227 info[i]->nr_heads = 0;
228 }
8f3f9b09
JH
229 }
230
6f42f89c 231 /* renumber them */
8f3f9b09
JH
232 qsort(info, num_pack, sizeof(info[0]), compare_info);
233 for (i = 0; i < num_pack; i++)
234 info[i]->new_num = i;
8f3f9b09
JH
235}
236
3907a407
JK
237static void free_pack_info(void)
238{
239 int i;
240 for (i = 0; i < num_pack; i++)
241 free(info[i]);
242 free(info);
243}
244
d38379ec 245static int write_pack_info_file(FILE *fp)
8f3f9b09 246{
3e15c67c 247 int i;
d38379ec
JK
248 for (i = 0; i < num_pack; i++) {
249 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
250 return -1;
251 }
252 if (fputc('\n', fp) == EOF)
253 return -1;
254 return 0;
8f3f9b09
JH
255}
256
257static int update_info_packs(int force)
258{
d38379ec
JK
259 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
260 int ret;
8f3f9b09
JH
261
262 init_pack_info(infofile, force);
d38379ec 263 ret = update_info_file(infofile, write_pack_info_file);
3907a407 264 free_pack_info();
d38379ec
JK
265 free(infofile);
266 return ret;
8f3f9b09
JH
267}
268
8f3f9b09
JH
269/* public */
270int update_server_info(int force)
271{
272 /* We would add more dumb-server support files later,
273 * including index of available pack files and their
274 * intended audiences.
275 */
276 int errs = 0;
277
278 errs = errs | update_info_refs(force);
279 errs = errs | update_info_packs(force);
8f3f9b09 280
6f42f89c 281 /* remove leftover rev-cache file if there is any */
691f1a28 282 unlink_or_warn(git_path("info/rev-cache"));
6f42f89c 283
8f3f9b09
JH
284 return errs;
285}