git-multimail: update to release 1.2.0
[git.git] / server-info.c
blobc82e9ee3964a17b23809c28d7cd14961e3e61d87
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "commit.h"
5 #include "tag.h"
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.
12 static int update_info_file(char *path, int (*generate)(FILE *))
14 char *tmp = mkpathdup("%s_XXXXXX", path);
15 int ret = -1;
16 int fd = -1;
17 FILE *fp = NULL;
19 safe_create_leading_directories(path);
20 fd = git_mkstemp_mode(tmp, 0666);
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;
37 out:
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);
46 free(tmp);
47 return ret;
50 static int add_info_ref(const char *path, const struct object_id *oid,
51 int flag, void *cb_data)
53 FILE *fp = cb_data;
54 struct object *o = parse_object(oid->hash);
55 if (!o)
56 return -1;
58 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
59 return -1;
61 if (o->type == OBJ_TAG) {
62 o = deref_tag(o, path, 0);
63 if (o)
64 if (fprintf(fp, "%s %s^{}\n",
65 sha1_to_hex(o->sha1), path) < 0)
66 return -1;
68 return 0;
71 static int generate_info_refs(FILE *fp)
73 return for_each_ref(add_info_ref, fp);
76 static int update_info_refs(int force)
78 char *path = git_pathdup("info/refs");
79 int ret = update_info_file(path, generate_info_refs);
80 free(path);
81 return ret;
84 /* packs */
85 static struct pack_info {
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];
92 } **info;
93 static int num_pack;
94 static const char *objdir;
95 static int objdirlen;
97 static struct pack_info *find_pack_by_name(const char *name)
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];
106 return NULL;
109 /* Returns non-zero when we detect that the info in the
110 * old file is useless.
112 static int parse_pack_def(const char *line, int old_cnt)
114 struct pack_info *i = find_pack_by_name(line + 2);
115 if (i) {
116 i->old_num = old_cnt;
117 return 0;
119 else {
120 /* The file describes a pack that is no longer here */
121 return 1;
125 /* Returns non-zero when we detect that the info in the
126 * old file is useless.
128 static int read_pack_info_file(const char *infofile)
130 FILE *fp;
131 char line[1000];
132 int old_cnt = 0;
134 fp = fopen(infofile, "r");
135 if (!fp)
136 return 1; /* nonexistent is not an error. */
138 while (fgets(line, sizeof(line), fp)) {
139 int len = strlen(line);
140 if (len && line[len-1] == '\n')
141 line[--len] = 0;
143 if (!len)
144 continue;
146 switch (line[0]) {
147 case 'P': /* P name */
148 if (parse_pack_def(line, old_cnt++))
149 goto out_stale;
150 break;
151 case 'D': /* we used to emit D but that was misguided. */
152 case 'T': /* we used to emit T but nobody uses it. */
153 goto out_stale;
154 default:
155 error("unrecognized: %s", line);
156 break;
159 fclose(fp);
160 return 0;
161 out_stale:
162 fclose(fp);
163 return 1;
166 static int compare_info(const void *a_, const void *b_)
168 struct pack_info *const *a = a_;
169 struct pack_info *const *b = b_;
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;
181 /* then it does not matter but at least keep the comparison stable */
182 if ((*a)->p == (*b)->p)
183 return 0;
184 else if ((*a)->p < (*b)->p)
185 return -1;
186 else
187 return 1;
190 static void init_pack_info(const char *infofile, int force)
192 struct packed_git *p;
193 int stale;
194 int i = 0;
196 objdir = get_object_directory();
197 objdirlen = strlen(objdir);
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.
204 if (!p->pack_local)
205 continue;
206 i++;
208 num_pack = i;
209 info = xcalloc(num_pack, sizeof(struct pack_info *));
210 for (i = 0, p = packed_git; p; p = p->next) {
211 if (!p->pack_local)
212 continue;
213 info[i] = xcalloc(1, sizeof(struct pack_info));
214 info[i]->p = p;
215 info[i]->old_num = -1;
216 i++;
219 if (infofile && !force)
220 stale = read_pack_info_file(infofile);
221 else
222 stale = 1;
224 for (i = 0; i < num_pack; i++) {
225 if (stale) {
226 info[i]->old_num = -1;
227 info[i]->nr_heads = 0;
231 /* renumber them */
232 qsort(info, num_pack, sizeof(info[0]), compare_info);
233 for (i = 0; i < num_pack; i++)
234 info[i]->new_num = i;
237 static void free_pack_info(void)
239 int i;
240 for (i = 0; i < num_pack; i++)
241 free(info[i]);
242 free(info);
245 static int write_pack_info_file(FILE *fp)
247 int i;
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;
252 if (fputc('\n', fp) == EOF)
253 return -1;
254 return 0;
257 static int update_info_packs(int force)
259 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
260 int ret;
262 init_pack_info(infofile, force);
263 ret = update_info_file(infofile, write_pack_info_file);
264 free_pack_info();
265 free(infofile);
266 return ret;
269 /* public */
270 int update_server_info(int force)
272 /* We would add more dumb-server support files later,
273 * including index of available pack files and their
274 * intended audiences.
276 int errs = 0;
278 errs = errs | update_info_refs(force);
279 errs = errs | update_info_packs(force);
281 /* remove leftover rev-cache file if there is any */
282 unlink_or_warn(git_path("info/rev-cache"));
284 return errs;