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"
10
11/*
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.
15 */
16static int update_info_file(char *path, int (*generate)(FILE *))
17{
18 char *tmp = mkpathdup("%s_XXXXXX", path);
19 int ret = -1;
20 int fd = -1;
21 FILE *fp = NULL, *to_close;
22
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;
42
43out:
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);
51 }
52 free(tmp);
53 return ret;
54}
55
56static int add_info_ref(const char *path, const struct object_id *oid,
57 int flag, void *cb_data)
58{
59 FILE *fp = cb_data;
60 struct object *o = parse_object(the_repository, oid);
61 if (!o)
62 return -1;
63
64 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
65 return -1;
66
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;
73 }
74 return 0;
75}
76
77static int generate_info_refs(FILE *fp)
78{
79 return for_each_ref(add_info_ref, fp);
80}
81
82static int update_info_refs(void)
83{
84 char *path = git_pathdup("info/refs");
85 int ret = update_info_file(path, generate_info_refs);
86 free(path);
87 return ret;
88}
89
90/* packs */
91static struct pack_info {
92 struct packed_git *p;
93 int old_num;
94 int new_num;
95} **info;
96static int num_pack;
97
98static struct pack_info *find_pack_by_name(const char *name)
99{
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];
105 }
106 return NULL;
107}
108
109/* Returns non-zero when we detect that the info in the
110 * old file is useless.
111 */
112static int parse_pack_def(const char *packname, int old_cnt)
113{
114 struct pack_info *i = find_pack_by_name(packname);
115 if (i) {
116 i->old_num = old_cnt;
117 return 0;
118 }
119 else {
120 /* The file describes a pack that is no longer here */
121 return 1;
122 }
123}
124
125/* Returns non-zero when we detect that the info in the
126 * old file is useless.
127 */
128static int read_pack_info_file(const char *infofile)
129{
130 FILE *fp;
131 struct strbuf line = STRBUF_INIT;
132 int old_cnt = 0;
133 int stale = 1;
134
135 fp = fopen_or_warn(infofile, "r");
136 if (!fp)
137 return 1; /* nonexistent is not an error. */
138
139 while (strbuf_getline(&line, fp) != EOF) {
140 const char *arg;
141
142 if (!line.len)
143 continue;
144
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);
157 }
158 }
159 stale = 0;
160
161 out_stale:
162 strbuf_release(&line);
163 fclose(fp);
164 return stale;
165}
166
167static int compare_info(const void *a_, const void *b_)
168{
169 struct pack_info *const *a = a_;
170 struct pack_info *const *b = b_;
171
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;
181
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;
189}
190
191static void init_pack_info(const char *infofile, int force)
192{
193 struct packed_git *p;
194 int stale;
195 int i;
196 size_t alloc = 0;
197
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.
201 */
202 if (!p->pack_local || !file_exists(p->pack_name))
203 continue;
204
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;
210 }
211
212 if (infofile && !force)
213 stale = read_pack_info_file(infofile);
214 else
215 stale = 1;
216
217 for (i = 0; i < num_pack; i++)
218 if (stale)
219 info[i]->old_num = -1;
220
221 /* renumber them */
222 QSORT(info, num_pack, compare_info);
223 for (i = 0; i < num_pack; i++)
224 info[i]->new_num = i;
225}
226
227static void free_pack_info(void)
228{
229 int i;
230 for (i = 0; i < num_pack; i++)
231 free(info[i]);
232 free(info);
233}
234
235static int write_pack_info_file(FILE *fp)
236{
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;
241 }
242 if (fputc('\n', fp) == EOF)
243 return -1;
244 return 0;
245}
246
247static int update_info_packs(int force)
248{
249 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
250 int ret;
251
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;
257}
258
259/* public */
260int update_server_info(int force)
261{
262 /* We would add more dumb-server support files later,
263 * including index of available pack files and their
264 * intended audiences.
265 */
266 int errs = 0;
267
268 errs = errs | update_info_refs();
269 errs = errs | update_info_packs(force);
270
271 /* remove leftover rev-cache file if there is any */
272 unlink_or_warn(git_path("info/rev-cache"));
273
274 return errs;
275}