object-store: move packed_git and packed_git_mru to object store
[git/git.git] / server-info.c
CommitLineData
8f3f9b09 1#include "cache.h"
a80d72db 2#include "repository.h"
8f3f9b09
JH
3#include "refs.h"
4#include "object.h"
5#include "commit.h"
b614e3d7 6#include "tag.h"
0abe14f6 7#include "packfile.h"
a80d72db 8#include "object-store.h"
8f3f9b09 9
d38379ec
JK
10/*
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.
14 */
15static int update_info_file(char *path, int (*generate)(FILE *))
16{
17 char *tmp = mkpathdup("%s_XXXXXX", path);
18 int ret = -1;
19 int fd = -1;
fa1912c8 20 FILE *fp = NULL, *to_close;
d38379ec
JK
21
22 safe_create_leading_directories(path);
d91175b2 23 fd = git_mkstemp_mode(tmp, 0666);
d38379ec
JK
24 if (fd < 0)
25 goto out;
fa1912c8 26 to_close = fp = fdopen(fd, "w");
d38379ec
JK
27 if (!fp)
28 goto out;
fa1912c8 29 fd = -1;
d38379ec
JK
30 ret = generate(fp);
31 if (ret)
32 goto out;
fa1912c8
RS
33 fp = NULL;
34 if (fclose(to_close))
d38379ec
JK
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;
41
42out:
43 if (ret) {
02382f51 44 error_errno("unable to update %s", path);
d38379ec
JK
45 if (fp)
46 fclose(fp);
47 else if (fd >= 0)
48 close(fd);
49 unlink(tmp);
50 }
51 free(tmp);
52 return ret;
53}
8f3f9b09 54
e2b0bcdf
MH
55static int add_info_ref(const char *path, const struct object_id *oid,
56 int flag, void *cb_data)
8f3f9b09 57{
d38379ec 58 FILE *fp = cb_data;
c251c83d 59 struct object *o = parse_object(oid);
76f8a302
SP
60 if (!o)
61 return -1;
f6b42a81 62
e2b0bcdf 63 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
d38379ec
JK
64 return -1;
65
1974632c 66 if (o->type == OBJ_TAG) {
9534f40b
JH
67 o = deref_tag(o, path, 0);
68 if (o)
d38379ec 69 if (fprintf(fp, "%s %s^{}\n",
f2fd0760 70 oid_to_hex(&o->oid), path) < 0)
d38379ec 71 return -1;
f6b42a81 72 }
8f3f9b09
JH
73 return 0;
74}
75
d38379ec
JK
76static int generate_info_refs(FILE *fp)
77{
e2b0bcdf 78 return for_each_ref(add_info_ref, fp);
d38379ec
JK
79}
80
8f3f9b09
JH
81static int update_info_refs(int force)
82{
d38379ec
JK
83 char *path = git_pathdup("info/refs");
84 int ret = update_info_file(path, generate_info_refs);
85 free(path);
86 return ret;
8f3f9b09
JH
87}
88
89/* packs */
4d8fa916 90static struct pack_info {
8f3f9b09
JH
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];
8f3f9b09
JH
97} **info;
98static int num_pack;
99static const char *objdir;
100static int objdirlen;
101
8f3f9b09
JH
102static struct pack_info *find_pack_by_name(const char *name)
103{
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];
110 }
111 return NULL;
112}
113
8f3f9b09
JH
114/* Returns non-zero when we detect that the info in the
115 * old file is useless.
116 */
117static int parse_pack_def(const char *line, int old_cnt)
118{
119 struct pack_info *i = find_pack_by_name(line + 2);
120 if (i) {
121 i->old_num = old_cnt;
122 return 0;
123 }
124 else {
6f42f89c 125 /* The file describes a pack that is no longer here */
8f3f9b09
JH
126 return 1;
127 }
128}
129
8f3f9b09
JH
130/* Returns non-zero when we detect that the info in the
131 * old file is useless.
132 */
133static int read_pack_info_file(const char *infofile)
134{
135 FILE *fp;
136 char line[1000];
137 int old_cnt = 0;
138
e9d983f1 139 fp = fopen_or_warn(infofile, "r");
8f3f9b09 140 if (!fp)
addf88e4 141 return 1; /* nonexistent is not an error. */
8f3f9b09
JH
142
143 while (fgets(line, sizeof(line), fp)) {
144 int len = strlen(line);
872c930d 145 if (len && line[len-1] == '\n')
8ac4838a
JH
146 line[--len] = 0;
147
148 if (!len)
149 continue;
8f3f9b09
JH
150
151 switch (line[0]) {
152 case 'P': /* P name */
153 if (parse_pack_def(line, old_cnt++))
154 goto out_stale;
155 break;
6f42f89c 156 case 'D': /* we used to emit D but that was misguided. */
3e15c67c
JH
157 case 'T': /* we used to emit T but nobody uses it. */
158 goto out_stale;
8f3f9b09
JH
159 default:
160 error("unrecognized: %s", line);
161 break;
162 }
163 }
164 fclose(fp);
165 return 0;
166 out_stale:
167 fclose(fp);
168 return 1;
169}
170
8f3f9b09
JH
171static int compare_info(const void *a_, const void *b_)
172{
4b25d091
FC
173 struct pack_info *const *a = a_;
174 struct pack_info *const *b = b_;
8f3f9b09
JH
175
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;
185
d5eac498 186 /* then it does not matter but at least keep the comparison stable */
2dee5816
JH
187 if ((*a)->p == (*b)->p)
188 return 0;
189 else if ((*a)->p < (*b)->p)
190 return -1;
191 else
192 return 1;
8f3f9b09
JH
193}
194
195static void init_pack_info(const char *infofile, int force)
196{
197 struct packed_git *p;
198 int stale;
199 int i = 0;
8f3f9b09
JH
200
201 objdir = get_object_directory();
202 objdirlen = strlen(objdir);
203
204 prepare_packed_git();
a80d72db 205 for (p = get_packed_git(the_repository); p; p = p->next) {
8f3f9b09
JH
206 /* we ignore things on alternate path since they are
207 * not available to the pullers in general.
208 */
f13d7db4 209 if (!p->pack_local)
8f3f9b09
JH
210 continue;
211 i++;
212 }
213 num_pack = i;
214 info = xcalloc(num_pack, sizeof(struct pack_info *));
a80d72db 215 for (i = 0, p = get_packed_git(the_repository); p; p = p->next) {
f13d7db4 216 if (!p->pack_local)
8f3f9b09 217 continue;
6f42f89c 218 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b09
JH
219 info[i]->p = p;
220 info[i]->old_num = -1;
221 i++;
222 }
223
224 if (infofile && !force)
225 stale = read_pack_info_file(infofile);
226 else
227 stale = 1;
228
229 for (i = 0; i < num_pack; i++) {
230 if (stale) {
231 info[i]->old_num = -1;
8f3f9b09
JH
232 info[i]->nr_heads = 0;
233 }
8f3f9b09
JH
234 }
235
6f42f89c 236 /* renumber them */
9ed0d8d6 237 QSORT(info, num_pack, compare_info);
8f3f9b09
JH
238 for (i = 0; i < num_pack; i++)
239 info[i]->new_num = i;
8f3f9b09
JH
240}
241
3907a407
JK
242static void free_pack_info(void)
243{
244 int i;
245 for (i = 0; i < num_pack; i++)
246 free(info[i]);
247 free(info);
248}
249
d38379ec 250static int write_pack_info_file(FILE *fp)
8f3f9b09 251{
3e15c67c 252 int i;
d38379ec
JK
253 for (i = 0; i < num_pack; i++) {
254 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
255 return -1;
256 }
257 if (fputc('\n', fp) == EOF)
258 return -1;
259 return 0;
8f3f9b09
JH
260}
261
262static int update_info_packs(int force)
263{
d38379ec
JK
264 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
265 int ret;
8f3f9b09
JH
266
267 init_pack_info(infofile, force);
d38379ec 268 ret = update_info_file(infofile, write_pack_info_file);
3907a407 269 free_pack_info();
d38379ec
JK
270 free(infofile);
271 return ret;
8f3f9b09
JH
272}
273
8f3f9b09
JH
274/* public */
275int update_server_info(int force)
276{
277 /* We would add more dumb-server support files later,
278 * including index of available pack files and their
279 * intended audiences.
280 */
281 int errs = 0;
282
283 errs = errs | update_info_refs(force);
284 errs = errs | update_info_packs(force);
8f3f9b09 285
6f42f89c 286 /* remove leftover rev-cache file if there is any */
691f1a28 287 unlink_or_warn(git_path("info/rev-cache"));
6f42f89c 288
8f3f9b09
JH
289 return errs;
290}