server-info: drop nr_alloc struct member
[git] / server-info.c
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"
9
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  */
15 static 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;
20         FILE *fp = NULL, *to_close;
21
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;
41
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);
50         }
51         free(tmp);
52         return ret;
53 }
54
55 static int add_info_ref(const char *path, const struct object_id *oid,
56                         int flag, void *cb_data)
57 {
58         FILE *fp = cb_data;
59         struct object *o = parse_object(the_repository, oid);
60         if (!o)
61                 return -1;
62
63         if (fprintf(fp, "%s     %s\n", oid_to_hex(oid), path) < 0)
64                 return -1;
65
66         if (o->type == OBJ_TAG) {
67                 o = deref_tag(the_repository, o, path, 0);
68                 if (o)
69                         if (fprintf(fp, "%s     %s^{}\n",
70                                 oid_to_hex(&o->oid), path) < 0)
71                                 return -1;
72         }
73         return 0;
74 }
75
76 static int generate_info_refs(FILE *fp)
77 {
78         return for_each_ref(add_info_ref, fp);
79 }
80
81 static int update_info_refs(int force)
82 {
83         char *path = git_pathdup("info/refs");
84         int ret = update_info_file(path, generate_info_refs);
85         free(path);
86         return ret;
87 }
88
89 /* packs */
90 static struct pack_info {
91         struct packed_git *p;
92         int old_num;
93         int new_num;
94 } **info;
95 static int num_pack;
96 static const char *objdir;
97 static int objdirlen;
98
99 static struct pack_info *find_pack_by_name(const char *name)
100 {
101         int i;
102         for (i = 0; i < num_pack; i++) {
103                 struct packed_git *p = info[i]->p;
104                 /* skip "/pack/" after ".git/objects" */
105                 if (!strcmp(p->pack_name + objdirlen + 6, name))
106                         return info[i];
107         }
108         return NULL;
109 }
110
111 /* Returns non-zero when we detect that the info in the
112  * old file is useless.
113  */
114 static int parse_pack_def(const char *packname, int old_cnt)
115 {
116         struct pack_info *i = find_pack_by_name(packname);
117         if (i) {
118                 i->old_num = old_cnt;
119                 return 0;
120         }
121         else {
122                 /* The file describes a pack that is no longer here */
123                 return 1;
124         }
125 }
126
127 /* Returns non-zero when we detect that the info in the
128  * old file is useless.
129  */
130 static int read_pack_info_file(const char *infofile)
131 {
132         FILE *fp;
133         struct strbuf line = STRBUF_INIT;
134         int old_cnt = 0;
135         int stale = 1;
136
137         fp = fopen_or_warn(infofile, "r");
138         if (!fp)
139                 return 1; /* nonexistent is not an error. */
140
141         while (strbuf_getline(&line, fp) != EOF) {
142                 const char *arg;
143
144                 if (!line.len)
145                         continue;
146
147                 if (skip_prefix(line.buf, "P ", &arg)) {
148                         /* P name */
149                         if (parse_pack_def(arg, old_cnt++))
150                                 goto out_stale;
151                 } else if (line.buf[0] == 'D') {
152                         /* we used to emit D but that was misguided. */
153                         goto out_stale;
154                 } else if (line.buf[0] == 'T') {
155                         /* we used to emit T but nobody uses it. */
156                         goto out_stale;
157                 } else {
158                         error("unrecognized: %s", line.buf);
159                 }
160         }
161         stale = 0;
162
163  out_stale:
164         strbuf_release(&line);
165         fclose(fp);
166         return stale;
167 }
168
169 static int compare_info(const void *a_, const void *b_)
170 {
171         struct pack_info *const *a = a_;
172         struct pack_info *const *b = b_;
173
174         if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
175                 /* Keep the order in the original */
176                 return (*a)->old_num - (*b)->old_num;
177         else if (0 <= (*a)->old_num)
178                 /* Only A existed in the original so B is obviously newer */
179                 return -1;
180         else if (0 <= (*b)->old_num)
181                 /* The other way around. */
182                 return 1;
183
184         /* then it does not matter but at least keep the comparison stable */
185         if ((*a)->p == (*b)->p)
186                 return 0;
187         else if ((*a)->p < (*b)->p)
188                 return -1;
189         else
190                 return 1;
191 }
192
193 static void init_pack_info(const char *infofile, int force)
194 {
195         struct packed_git *p;
196         int stale;
197         int i = 0;
198
199         objdir = get_object_directory();
200         objdirlen = strlen(objdir);
201
202         for (p = get_all_packs(the_repository); p; p = p->next) {
203                 /* we ignore things on alternate path since they are
204                  * not available to the pullers in general.
205                  */
206                 if (!p->pack_local)
207                         continue;
208                 i++;
209         }
210         num_pack = i;
211         info = xcalloc(num_pack, sizeof(struct pack_info *));
212         for (i = 0, p = get_all_packs(the_repository); p; p = p->next) {
213                 if (!p->pack_local)
214                         continue;
215                 assert(i < num_pack);
216                 info[i] = xcalloc(1, sizeof(struct pack_info));
217                 info[i]->p = p;
218                 info[i]->old_num = -1;
219                 i++;
220         }
221
222         if (infofile && !force)
223                 stale = read_pack_info_file(infofile);
224         else
225                 stale = 1;
226
227         for (i = 0; i < num_pack; i++)
228                 if (stale)
229                         info[i]->old_num = -1;
230
231         /* renumber them */
232         QSORT(info, num_pack, compare_info);
233         for (i = 0; i < num_pack; i++)
234                 info[i]->new_num = i;
235 }
236
237 static void free_pack_info(void)
238 {
239         int i;
240         for (i = 0; i < num_pack; i++)
241                 free(info[i]);
242         free(info);
243 }
244
245 static int write_pack_info_file(FILE *fp)
246 {
247         int i;
248         for (i = 0; i < num_pack; i++) {
249                 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
250                         return -1;
251         }
252         if (fputc('\n', fp) == EOF)
253                 return -1;
254         return 0;
255 }
256
257 static int update_info_packs(int force)
258 {
259         char *infofile = mkpathdup("%s/info/packs", get_object_directory());
260         int ret;
261
262         init_pack_info(infofile, force);
263         ret = update_info_file(infofile, write_pack_info_file);
264         free_pack_info();
265         free(infofile);
266         return ret;
267 }
268
269 /* public */
270 int update_server_info(int force)
271 {
272         /* We would add more dumb-server support files later,
273          * including index of available pack files and their
274          * intended audiences.
275          */
276         int errs = 0;
277
278         errs = errs | update_info_refs(force);
279         errs = errs | update_info_packs(force);
280
281         /* remove leftover rev-cache file if there is any */
282         unlink_or_warn(git_path("info/rev-cache"));
283
284         return errs;
285 }