Merge branch 'sg/fsck-config-in-doc' into maint
[git/raj.git] / server-info.c
blob92187c70db650ee0e4d5ea4bf3435e84a51c44b2
1 #include "cache.h"
2 #include "dir.h"
3 #include "repository.h"
4 #include "refs.h"
5 #include "object.h"
6 #include "commit.h"
7 #include "tag.h"
8 #include "packfile.h"
9 #include "object-store.h"
12 * Create the file "path" by writing to a temporary file and renaming
13 * it into place. The contents of the file come from "generate", which
14 * should return non-zero if it encounters an error.
16 static int update_info_file(char *path, int (*generate)(FILE *))
18 char *tmp = mkpathdup("%s_XXXXXX", path);
19 int ret = -1;
20 int fd = -1;
21 FILE *fp = NULL, *to_close;
23 safe_create_leading_directories(path);
24 fd = git_mkstemp_mode(tmp, 0666);
25 if (fd < 0)
26 goto out;
27 to_close = fp = fdopen(fd, "w");
28 if (!fp)
29 goto out;
30 fd = -1;
31 ret = generate(fp);
32 if (ret)
33 goto out;
34 fp = NULL;
35 if (fclose(to_close))
36 goto out;
37 if (adjust_shared_perm(tmp) < 0)
38 goto out;
39 if (rename(tmp, path) < 0)
40 goto out;
41 ret = 0;
43 out:
44 if (ret) {
45 error_errno("unable to update %s", path);
46 if (fp)
47 fclose(fp);
48 else if (fd >= 0)
49 close(fd);
50 unlink(tmp);
52 free(tmp);
53 return ret;
56 static int add_info_ref(const char *path, const struct object_id *oid,
57 int flag, void *cb_data)
59 FILE *fp = cb_data;
60 struct object *o = parse_object(the_repository, oid);
61 if (!o)
62 return -1;
64 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
65 return -1;
67 if (o->type == OBJ_TAG) {
68 o = deref_tag(the_repository, o, path, 0);
69 if (o)
70 if (fprintf(fp, "%s %s^{}\n",
71 oid_to_hex(&o->oid), path) < 0)
72 return -1;
74 return 0;
77 static int generate_info_refs(FILE *fp)
79 return for_each_ref(add_info_ref, fp);
82 static int update_info_refs(void)
84 char *path = git_pathdup("info/refs");
85 int ret = update_info_file(path, generate_info_refs);
86 free(path);
87 return ret;
90 /* packs */
91 static struct pack_info {
92 struct packed_git *p;
93 int old_num;
94 int new_num;
95 } **info;
96 static int num_pack;
98 static struct pack_info *find_pack_by_name(const char *name)
100 int i;
101 for (i = 0; i < num_pack; i++) {
102 struct packed_git *p = info[i]->p;
103 if (!strcmp(pack_basename(p), 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 *packname, int old_cnt)
114 struct pack_info *i = find_pack_by_name(packname);
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 struct strbuf line = STRBUF_INIT;
132 int old_cnt = 0;
133 int stale = 1;
135 fp = fopen_or_warn(infofile, "r");
136 if (!fp)
137 return 1; /* nonexistent is not an error. */
139 while (strbuf_getline(&line, fp) != EOF) {
140 const char *arg;
142 if (!line.len)
143 continue;
145 if (skip_prefix(line.buf, "P ", &arg)) {
146 /* P name */
147 if (parse_pack_def(arg, old_cnt++))
148 goto out_stale;
149 } else if (line.buf[0] == 'D') {
150 /* we used to emit D but that was misguided. */
151 goto out_stale;
152 } else if (line.buf[0] == 'T') {
153 /* we used to emit T but nobody uses it. */
154 goto out_stale;
155 } else {
156 error("unrecognized: %s", line.buf);
159 stale = 0;
161 out_stale:
162 strbuf_release(&line);
163 fclose(fp);
164 return stale;
167 static int compare_info(const void *a_, const void *b_)
169 struct pack_info *const *a = a_;
170 struct pack_info *const *b = b_;
172 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
173 /* Keep the order in the original */
174 return (*a)->old_num - (*b)->old_num;
175 else if (0 <= (*a)->old_num)
176 /* Only A existed in the original so B is obviously newer */
177 return -1;
178 else if (0 <= (*b)->old_num)
179 /* The other way around. */
180 return 1;
182 /* then it does not matter but at least keep the comparison stable */
183 if ((*a)->p == (*b)->p)
184 return 0;
185 else if ((*a)->p < (*b)->p)
186 return -1;
187 else
188 return 1;
191 static void init_pack_info(const char *infofile, int force)
193 struct packed_git *p;
194 int stale;
195 int i;
196 size_t alloc = 0;
198 for (p = get_all_packs(the_repository); p; p = p->next) {
199 /* we ignore things on alternate path since they are
200 * not available to the pullers in general.
202 if (!p->pack_local || !file_exists(p->pack_name))
203 continue;
205 i = num_pack++;
206 ALLOC_GROW(info, num_pack, alloc);
207 info[i] = xcalloc(1, sizeof(struct pack_info));
208 info[i]->p = p;
209 info[i]->old_num = -1;
212 if (infofile && !force)
213 stale = read_pack_info_file(infofile);
214 else
215 stale = 1;
217 for (i = 0; i < num_pack; i++)
218 if (stale)
219 info[i]->old_num = -1;
221 /* renumber them */
222 QSORT(info, num_pack, compare_info);
223 for (i = 0; i < num_pack; i++)
224 info[i]->new_num = i;
227 static void free_pack_info(void)
229 int i;
230 for (i = 0; i < num_pack; i++)
231 free(info[i]);
232 free(info);
235 static int write_pack_info_file(FILE *fp)
237 int i;
238 for (i = 0; i < num_pack; i++) {
239 if (fprintf(fp, "P %s\n", pack_basename(info[i]->p)) < 0)
240 return -1;
242 if (fputc('\n', fp) == EOF)
243 return -1;
244 return 0;
247 static int update_info_packs(int force)
249 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
250 int ret;
252 init_pack_info(infofile, force);
253 ret = update_info_file(infofile, write_pack_info_file);
254 free_pack_info();
255 free(infofile);
256 return ret;
259 /* public */
260 int update_server_info(int force)
262 /* We would add more dumb-server support files later,
263 * including index of available pack files and their
264 * intended audiences.
266 int errs = 0;
268 errs = errs | update_info_refs();
269 errs = errs | update_info_packs(force);
271 /* remove leftover rev-cache file if there is any */
272 unlink_or_warn(git_path("info/rev-cache"));
274 return errs;