Makefile: quote $INSTLIBDIR when passing it to sed
[git/debian.git] / server-info.c
blob83460ec0d6f10b26719c076639a08a8a0ce803cc
1 #include "cache.h"
2 #include "repository.h"
3 #include "refs.h"
4 #include "object.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "packfile.h"
8 #include "object-store.h"
11 * Create the file "path" by writing to a temporary file and renaming
12 * it into place. The contents of the file come from "generate", which
13 * should return non-zero if it encounters an error.
15 static int update_info_file(char *path, int (*generate)(FILE *))
17 char *tmp = mkpathdup("%s_XXXXXX", path);
18 int ret = -1;
19 int fd = -1;
20 FILE *fp = NULL, *to_close;
22 safe_create_leading_directories(path);
23 fd = git_mkstemp_mode(tmp, 0666);
24 if (fd < 0)
25 goto out;
26 to_close = fp = fdopen(fd, "w");
27 if (!fp)
28 goto out;
29 fd = -1;
30 ret = generate(fp);
31 if (ret)
32 goto out;
33 fp = NULL;
34 if (fclose(to_close))
35 goto out;
36 if (adjust_shared_perm(tmp) < 0)
37 goto out;
38 if (rename(tmp, path) < 0)
39 goto out;
40 ret = 0;
42 out:
43 if (ret) {
44 error_errno("unable to update %s", path);
45 if (fp)
46 fclose(fp);
47 else if (fd >= 0)
48 close(fd);
49 unlink(tmp);
51 free(tmp);
52 return ret;
55 static int add_info_ref(const char *path, const struct object_id *oid,
56 int flag, void *cb_data)
58 FILE *fp = cb_data;
59 struct object *o = parse_object(oid);
60 if (!o)
61 return -1;
63 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
64 return -1;
66 if (o->type == OBJ_TAG) {
67 o = deref_tag(o, path, 0);
68 if (o)
69 if (fprintf(fp, "%s %s^{}\n",
70 oid_to_hex(&o->oid), path) < 0)
71 return -1;
73 return 0;
76 static int generate_info_refs(FILE *fp)
78 return for_each_ref(add_info_ref, fp);
81 static int update_info_refs(int force)
83 char *path = git_pathdup("info/refs");
84 int ret = update_info_file(path, generate_info_refs);
85 free(path);
86 return ret;
89 /* packs */
90 static struct pack_info {
91 struct packed_git *p;
92 int old_num;
93 int new_num;
94 int nr_alloc;
95 int nr_heads;
96 unsigned char (*head)[20];
97 } **info;
98 static int num_pack;
99 static const char *objdir;
100 static int objdirlen;
102 static struct pack_info *find_pack_by_name(const char *name)
104 int i;
105 for (i = 0; i < num_pack; i++) {
106 struct packed_git *p = info[i]->p;
107 /* skip "/pack/" after ".git/objects" */
108 if (!strcmp(p->pack_name + objdirlen + 6, name))
109 return info[i];
111 return NULL;
114 /* Returns non-zero when we detect that the info in the
115 * old file is useless.
117 static int parse_pack_def(const char *line, int old_cnt)
119 struct pack_info *i = find_pack_by_name(line + 2);
120 if (i) {
121 i->old_num = old_cnt;
122 return 0;
124 else {
125 /* The file describes a pack that is no longer here */
126 return 1;
130 /* Returns non-zero when we detect that the info in the
131 * old file is useless.
133 static int read_pack_info_file(const char *infofile)
135 FILE *fp;
136 char line[1000];
137 int old_cnt = 0;
139 fp = fopen_or_warn(infofile, "r");
140 if (!fp)
141 return 1; /* nonexistent is not an error. */
143 while (fgets(line, sizeof(line), fp)) {
144 int len = strlen(line);
145 if (len && line[len-1] == '\n')
146 line[--len] = 0;
148 if (!len)
149 continue;
151 switch (line[0]) {
152 case 'P': /* P name */
153 if (parse_pack_def(line, old_cnt++))
154 goto out_stale;
155 break;
156 case 'D': /* we used to emit D but that was misguided. */
157 case 'T': /* we used to emit T but nobody uses it. */
158 goto out_stale;
159 default:
160 error("unrecognized: %s", line);
161 break;
164 fclose(fp);
165 return 0;
166 out_stale:
167 fclose(fp);
168 return 1;
171 static int compare_info(const void *a_, const void *b_)
173 struct pack_info *const *a = a_;
174 struct pack_info *const *b = b_;
176 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
177 /* Keep the order in the original */
178 return (*a)->old_num - (*b)->old_num;
179 else if (0 <= (*a)->old_num)
180 /* Only A existed in the original so B is obviously newer */
181 return -1;
182 else if (0 <= (*b)->old_num)
183 /* The other way around. */
184 return 1;
186 /* then it does not matter but at least keep the comparison stable */
187 if ((*a)->p == (*b)->p)
188 return 0;
189 else if ((*a)->p < (*b)->p)
190 return -1;
191 else
192 return 1;
195 static void init_pack_info(const char *infofile, int force)
197 struct packed_git *p;
198 int stale;
199 int i = 0;
201 objdir = get_object_directory();
202 objdirlen = strlen(objdir);
204 for (p = get_packed_git(the_repository); p; p = p->next) {
205 /* we ignore things on alternate path since they are
206 * not available to the pullers in general.
208 if (!p->pack_local)
209 continue;
210 i++;
212 num_pack = i;
213 info = xcalloc(num_pack, sizeof(struct pack_info *));
214 for (i = 0, p = get_packed_git(the_repository); p; p = p->next) {
215 if (!p->pack_local)
216 continue;
217 info[i] = xcalloc(1, sizeof(struct pack_info));
218 info[i]->p = p;
219 info[i]->old_num = -1;
220 i++;
223 if (infofile && !force)
224 stale = read_pack_info_file(infofile);
225 else
226 stale = 1;
228 for (i = 0; i < num_pack; i++) {
229 if (stale) {
230 info[i]->old_num = -1;
231 info[i]->nr_heads = 0;
235 /* renumber them */
236 QSORT(info, num_pack, compare_info);
237 for (i = 0; i < num_pack; i++)
238 info[i]->new_num = i;
241 static void free_pack_info(void)
243 int i;
244 for (i = 0; i < num_pack; i++)
245 free(info[i]);
246 free(info);
249 static int write_pack_info_file(FILE *fp)
251 int i;
252 for (i = 0; i < num_pack; i++) {
253 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
254 return -1;
256 if (fputc('\n', fp) == EOF)
257 return -1;
258 return 0;
261 static int update_info_packs(int force)
263 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
264 int ret;
266 init_pack_info(infofile, force);
267 ret = update_info_file(infofile, write_pack_info_file);
268 free_pack_info();
269 free(infofile);
270 return ret;
273 /* public */
274 int update_server_info(int force)
276 /* We would add more dumb-server support files later,
277 * including index of available pack files and their
278 * intended audiences.
280 int errs = 0;
282 errs = errs | update_info_refs(force);
283 errs = errs | update_info_packs(force);
285 /* remove leftover rev-cache file if there is any */
286 unlink_or_warn(git_path("info/rev-cache"));
288 return errs;