]> git.ipfire.org Git - thirdparty/git.git/blame - server-info.c
t1301: set umask in reflog sharedrepository=group test
[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);
20 fd = mkstemp(tmp);
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
8da19775 50static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
8f3f9b09 51{
d38379ec 52 FILE *fp = cb_data;
f6b42a81 53 struct object *o = parse_object(sha1);
76f8a302
SP
54 if (!o)
55 return -1;
f6b42a81 56
d38379ec
JK
57 if (fprintf(fp, "%s %s\n", sha1_to_hex(sha1), path) < 0)
58 return -1;
59
1974632c 60 if (o->type == OBJ_TAG) {
9534f40b
JH
61 o = deref_tag(o, path, 0);
62 if (o)
d38379ec
JK
63 if (fprintf(fp, "%s %s^{}\n",
64 sha1_to_hex(o->sha1), path) < 0)
65 return -1;
f6b42a81 66 }
8f3f9b09
JH
67 return 0;
68}
69
d38379ec
JK
70static int generate_info_refs(FILE *fp)
71{
72 return for_each_ref(add_info_ref, fp);
73}
74
8f3f9b09
JH
75static int update_info_refs(int force)
76{
d38379ec
JK
77 char *path = git_pathdup("info/refs");
78 int ret = update_info_file(path, generate_info_refs);
79 free(path);
80 return ret;
8f3f9b09
JH
81}
82
83/* packs */
4d8fa916 84static struct pack_info {
8f3f9b09
JH
85 struct packed_git *p;
86 int old_num;
87 int new_num;
88 int nr_alloc;
89 int nr_heads;
90 unsigned char (*head)[20];
8f3f9b09
JH
91} **info;
92static int num_pack;
93static const char *objdir;
94static int objdirlen;
95
8f3f9b09
JH
96static struct pack_info *find_pack_by_name(const char *name)
97{
98 int i;
99 for (i = 0; i < num_pack; i++) {
100 struct packed_git *p = info[i]->p;
101 /* skip "/pack/" after ".git/objects" */
102 if (!strcmp(p->pack_name + objdirlen + 6, name))
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 */
111static int parse_pack_def(const char *line, int old_cnt)
112{
113 struct pack_info *i = find_pack_by_name(line + 2);
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 */
127static int read_pack_info_file(const char *infofile)
128{
129 FILE *fp;
130 char line[1000];
131 int old_cnt = 0;
132
133 fp = fopen(infofile, "r");
134 if (!fp)
addf88e4 135 return 1; /* nonexistent is not an error. */
8f3f9b09
JH
136
137 while (fgets(line, sizeof(line), fp)) {
138 int len = strlen(line);
872c930d 139 if (len && line[len-1] == '\n')
8ac4838a
JH
140 line[--len] = 0;
141
142 if (!len)
143 continue;
8f3f9b09
JH
144
145 switch (line[0]) {
146 case 'P': /* P name */
147 if (parse_pack_def(line, old_cnt++))
148 goto out_stale;
149 break;
6f42f89c 150 case 'D': /* we used to emit D but that was misguided. */
3e15c67c
JH
151 case 'T': /* we used to emit T but nobody uses it. */
152 goto out_stale;
8f3f9b09
JH
153 default:
154 error("unrecognized: %s", line);
155 break;
156 }
157 }
158 fclose(fp);
159 return 0;
160 out_stale:
161 fclose(fp);
162 return 1;
163}
164
8f3f9b09
JH
165static int compare_info(const void *a_, const void *b_)
166{
4b25d091
FC
167 struct pack_info *const *a = a_;
168 struct pack_info *const *b = b_;
8f3f9b09
JH
169
170 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
171 /* Keep the order in the original */
172 return (*a)->old_num - (*b)->old_num;
173 else if (0 <= (*a)->old_num)
174 /* Only A existed in the original so B is obviously newer */
175 return -1;
176 else if (0 <= (*b)->old_num)
177 /* The other way around. */
178 return 1;
179
d5eac498 180 /* then it does not matter but at least keep the comparison stable */
2dee5816
JH
181 if ((*a)->p == (*b)->p)
182 return 0;
183 else if ((*a)->p < (*b)->p)
184 return -1;
185 else
186 return 1;
8f3f9b09
JH
187}
188
189static void init_pack_info(const char *infofile, int force)
190{
191 struct packed_git *p;
192 int stale;
193 int i = 0;
8f3f9b09
JH
194
195 objdir = get_object_directory();
196 objdirlen = strlen(objdir);
197
198 prepare_packed_git();
199 for (p = packed_git; p; p = p->next) {
200 /* we ignore things on alternate path since they are
201 * not available to the pullers in general.
202 */
f13d7db4 203 if (!p->pack_local)
8f3f9b09
JH
204 continue;
205 i++;
206 }
207 num_pack = i;
208 info = xcalloc(num_pack, sizeof(struct pack_info *));
209 for (i = 0, p = packed_git; p; p = p->next) {
f13d7db4 210 if (!p->pack_local)
8f3f9b09 211 continue;
6f42f89c 212 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b09
JH
213 info[i]->p = p;
214 info[i]->old_num = -1;
215 i++;
216 }
217
218 if (infofile && !force)
219 stale = read_pack_info_file(infofile);
220 else
221 stale = 1;
222
223 for (i = 0; i < num_pack; i++) {
224 if (stale) {
225 info[i]->old_num = -1;
8f3f9b09
JH
226 info[i]->nr_heads = 0;
227 }
8f3f9b09
JH
228 }
229
6f42f89c 230 /* renumber them */
8f3f9b09
JH
231 qsort(info, num_pack, sizeof(info[0]), compare_info);
232 for (i = 0; i < num_pack; i++)
233 info[i]->new_num = i;
8f3f9b09
JH
234}
235
3907a407
JK
236static void free_pack_info(void)
237{
238 int i;
239 for (i = 0; i < num_pack; i++)
240 free(info[i]);
241 free(info);
242}
243
d38379ec 244static int write_pack_info_file(FILE *fp)
8f3f9b09 245{
3e15c67c 246 int i;
d38379ec
JK
247 for (i = 0; i < num_pack; i++) {
248 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
249 return -1;
250 }
251 if (fputc('\n', fp) == EOF)
252 return -1;
253 return 0;
8f3f9b09
JH
254}
255
256static int update_info_packs(int force)
257{
d38379ec
JK
258 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
259 int ret;
8f3f9b09
JH
260
261 init_pack_info(infofile, force);
d38379ec 262 ret = update_info_file(infofile, write_pack_info_file);
3907a407 263 free_pack_info();
d38379ec
JK
264 free(infofile);
265 return ret;
8f3f9b09
JH
266}
267
8f3f9b09
JH
268/* public */
269int update_server_info(int force)
270{
271 /* We would add more dumb-server support files later,
272 * including index of available pack files and their
273 * intended audiences.
274 */
275 int errs = 0;
276
277 errs = errs | update_info_refs(force);
278 errs = errs | update_info_packs(force);
8f3f9b09 279
6f42f89c 280 /* remove leftover rev-cache file if there is any */
691f1a28 281 unlink_or_warn(git_path("info/rev-cache"));
6f42f89c 282
8f3f9b09
JH
283 return errs;
284}