clean up name allocation in prepare_revision_walk
[git/mingw/j6t.git] / server-info.c
blob31f4a749fbad73b9f7bc1f1ebf9b11fd743ab374
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 = 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;
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 unsigned char *sha1, int flag, void *cb_data)
52 FILE *fp = cb_data;
53 struct object *o = parse_object(sha1);
54 if (!o)
55 return -1;
57 if (fprintf(fp, "%s %s\n", sha1_to_hex(sha1), path) < 0)
58 return -1;
60 if (o->type == OBJ_TAG) {
61 o = deref_tag(o, path, 0);
62 if (o)
63 if (fprintf(fp, "%s %s^{}\n",
64 sha1_to_hex(o->sha1), path) < 0)
65 return -1;
67 return 0;
70 static int generate_info_refs(FILE *fp)
72 return for_each_ref(add_info_ref, fp);
75 static int update_info_refs(int force)
77 char *path = git_pathdup("info/refs");
78 int ret = update_info_file(path, generate_info_refs);
79 free(path);
80 return ret;
83 /* packs */
84 static struct pack_info {
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];
91 } **info;
92 static int num_pack;
93 static const char *objdir;
94 static int objdirlen;
96 static struct pack_info *find_pack_by_name(const char *name)
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];
105 return NULL;
108 /* Returns non-zero when we detect that the info in the
109 * old file is useless.
111 static int parse_pack_def(const char *line, int old_cnt)
113 struct pack_info *i = find_pack_by_name(line + 2);
114 if (i) {
115 i->old_num = old_cnt;
116 return 0;
118 else {
119 /* The file describes a pack that is no longer here */
120 return 1;
124 /* Returns non-zero when we detect that the info in the
125 * old file is useless.
127 static int read_pack_info_file(const char *infofile)
129 FILE *fp;
130 char line[1000];
131 int old_cnt = 0;
133 fp = fopen(infofile, "r");
134 if (!fp)
135 return 1; /* nonexistent is not an error. */
137 while (fgets(line, sizeof(line), fp)) {
138 int len = strlen(line);
139 if (len && line[len-1] == '\n')
140 line[--len] = 0;
142 if (!len)
143 continue;
145 switch (line[0]) {
146 case 'P': /* P name */
147 if (parse_pack_def(line, old_cnt++))
148 goto out_stale;
149 break;
150 case 'D': /* we used to emit D but that was misguided. */
151 case 'T': /* we used to emit T but nobody uses it. */
152 goto out_stale;
153 default:
154 error("unrecognized: %s", line);
155 break;
158 fclose(fp);
159 return 0;
160 out_stale:
161 fclose(fp);
162 return 1;
165 static int compare_info(const void *a_, const void *b_)
167 struct pack_info *const *a = a_;
168 struct pack_info *const *b = b_;
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;
180 /* then it does not matter but at least keep the comparison stable */
181 if ((*a)->p == (*b)->p)
182 return 0;
183 else if ((*a)->p < (*b)->p)
184 return -1;
185 else
186 return 1;
189 static void init_pack_info(const char *infofile, int force)
191 struct packed_git *p;
192 int stale;
193 int i = 0;
195 objdir = get_object_directory();
196 objdirlen = strlen(objdir);
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.
203 if (!p->pack_local)
204 continue;
205 i++;
207 num_pack = i;
208 info = xcalloc(num_pack, sizeof(struct pack_info *));
209 for (i = 0, p = packed_git; p; p = p->next) {
210 if (!p->pack_local)
211 continue;
212 info[i] = xcalloc(1, sizeof(struct pack_info));
213 info[i]->p = p;
214 info[i]->old_num = -1;
215 i++;
218 if (infofile && !force)
219 stale = read_pack_info_file(infofile);
220 else
221 stale = 1;
223 for (i = 0; i < num_pack; i++) {
224 if (stale) {
225 info[i]->old_num = -1;
226 info[i]->nr_heads = 0;
230 /* renumber them */
231 qsort(info, num_pack, sizeof(info[0]), compare_info);
232 for (i = 0; i < num_pack; i++)
233 info[i]->new_num = i;
236 static void free_pack_info(void)
238 int i;
239 for (i = 0; i < num_pack; i++)
240 free(info[i]);
241 free(info);
244 static int write_pack_info_file(FILE *fp)
246 int i;
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;
251 if (fputc('\n', fp) == EOF)
252 return -1;
253 return 0;
256 static int update_info_packs(int force)
258 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
259 int ret;
261 init_pack_info(infofile, force);
262 ret = update_info_file(infofile, write_pack_info_file);
263 free_pack_info();
264 free(infofile);
265 return ret;
268 /* public */
269 int update_server_info(int force)
271 /* We would add more dumb-server support files later,
272 * including index of available pack files and their
273 * intended audiences.
275 int errs = 0;
277 errs = errs | update_info_refs(force);
278 errs = errs | update_info_packs(force);
280 /* remove leftover rev-cache file if there is any */
281 unlink_or_warn(git_path("info/rev-cache"));
283 return errs;