blob: 26a6c20b7d420f679d7cceaeba4acdb8ae8c00e7 [file] [log] [blame]
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07001#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "commit.h"
Junio C Hamanob614e3d2005-07-28 14:33:17 -07005#include "tag.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -07006#include "packfile.h"
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07007
Jeff Kingd38379e2014-09-13 16:19:20 -04008/*
9 * Create the file "path" by writing to a temporary file and renaming
10 * it into place. The contents of the file come from "generate", which
11 * should return non-zero if it encounters an error.
12 */
13static int update_info_file(char *path, int (*generate)(FILE *))
14{
15 char *tmp = mkpathdup("%s_XXXXXX", path);
16 int ret = -1;
17 int fd = -1;
René Scharfefa1912c2017-04-16 18:55:58 +020018 FILE *fp = NULL, *to_close;
Jeff Kingd38379e2014-09-13 16:19:20 -040019
20 safe_create_leading_directories(path);
Jeff Kingd91175b2015-01-05 22:50:49 -050021 fd = git_mkstemp_mode(tmp, 0666);
Jeff Kingd38379e2014-09-13 16:19:20 -040022 if (fd < 0)
23 goto out;
René Scharfefa1912c2017-04-16 18:55:58 +020024 to_close = fp = fdopen(fd, "w");
Jeff Kingd38379e2014-09-13 16:19:20 -040025 if (!fp)
26 goto out;
René Scharfefa1912c2017-04-16 18:55:58 +020027 fd = -1;
Jeff Kingd38379e2014-09-13 16:19:20 -040028 ret = generate(fp);
29 if (ret)
30 goto out;
René Scharfefa1912c2017-04-16 18:55:58 +020031 fp = NULL;
32 if (fclose(to_close))
Jeff Kingd38379e2014-09-13 16:19:20 -040033 goto out;
34 if (adjust_shared_perm(tmp) < 0)
35 goto out;
36 if (rename(tmp, path) < 0)
37 goto out;
38 ret = 0;
39
40out:
41 if (ret) {
Nguyễn Thái Ngọc Duy02382f52016-05-08 16:47:55 +070042 error_errno("unable to update %s", path);
Jeff Kingd38379e2014-09-13 16:19:20 -040043 if (fp)
44 fclose(fp);
45 else if (fd >= 0)
46 close(fd);
47 unlink(tmp);
48 }
49 free(tmp);
50 return ret;
51}
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070052
Michael Haggertye2b0bcd2015-05-25 18:39:04 +000053static int add_info_ref(const char *path, const struct object_id *oid,
54 int flag, void *cb_data)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070055{
Jeff Kingd38379e2014-09-13 16:19:20 -040056 FILE *fp = cb_data;
brian m. carlsonc251c832017-05-06 22:10:38 +000057 struct object *o = parse_object(oid);
Shawn O. Pearce76f8a302007-01-31 02:24:44 -050058 if (!o)
59 return -1;
Junio C Hamanof6b42a82005-10-13 18:57:40 -070060
Michael Haggertye2b0bcd2015-05-25 18:39:04 +000061 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
Jeff Kingd38379e2014-09-13 16:19:20 -040062 return -1;
63
Linus Torvalds19746322006-07-11 20:45:31 -070064 if (o->type == OBJ_TAG) {
Junio C Hamano9534f402005-11-02 15:19:13 -080065 o = deref_tag(o, path, 0);
66 if (o)
Jeff Kingd38379e2014-09-13 16:19:20 -040067 if (fprintf(fp, "%s %s^{}\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +000068 oid_to_hex(&o->oid), path) < 0)
Jeff Kingd38379e2014-09-13 16:19:20 -040069 return -1;
Junio C Hamanof6b42a82005-10-13 18:57:40 -070070 }
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070071 return 0;
72}
73
Jeff Kingd38379e2014-09-13 16:19:20 -040074static int generate_info_refs(FILE *fp)
75{
Michael Haggertye2b0bcd2015-05-25 18:39:04 +000076 return for_each_ref(add_info_ref, fp);
Jeff Kingd38379e2014-09-13 16:19:20 -040077}
78
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070079static int update_info_refs(int force)
80{
Jeff Kingd38379e2014-09-13 16:19:20 -040081 char *path = git_pathdup("info/refs");
82 int ret = update_info_file(path, generate_info_refs);
83 free(path);
84 return ret;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070085}
86
87/* packs */
Linus Torvalds4d8fa912005-08-01 12:11:53 -070088static struct pack_info {
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070089 struct packed_git *p;
90 int old_num;
91 int new_num;
92 int nr_alloc;
93 int nr_heads;
94 unsigned char (*head)[20];
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070095} **info;
96static int num_pack;
97static const char *objdir;
98static int objdirlen;
99
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700100static struct pack_info *find_pack_by_name(const char *name)
101{
102 int i;
103 for (i = 0; i < num_pack; i++) {
104 struct packed_git *p = info[i]->p;
105 /* skip "/pack/" after ".git/objects" */
106 if (!strcmp(p->pack_name + objdirlen + 6, name))
107 return info[i];
108 }
109 return NULL;
110}
111
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700112/* Returns non-zero when we detect that the info in the
113 * old file is useless.
114 */
115static int parse_pack_def(const char *line, int old_cnt)
116{
117 struct pack_info *i = find_pack_by_name(line + 2);
118 if (i) {
119 i->old_num = old_cnt;
120 return 0;
121 }
122 else {
Junio C Hamano6f42f892005-12-04 22:52:19 -0800123 /* The file describes a pack that is no longer here */
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700124 return 1;
125 }
126}
127
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700128/* Returns non-zero when we detect that the info in the
129 * old file is useless.
130 */
131static int read_pack_info_file(const char *infofile)
132{
133 FILE *fp;
134 char line[1000];
135 int old_cnt = 0;
136
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700137 fp = fopen_or_warn(infofile, "r");
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700138 if (!fp)
Pavel Roskinaddf88e2006-07-09 03:44:30 -0400139 return 1; /* nonexistent is not an error. */
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700140
141 while (fgets(line, sizeof(line), fp)) {
142 int len = strlen(line);
Jim Meyering872c9302008-01-04 18:37:41 +0100143 if (len && line[len-1] == '\n')
Junio C Hamano8ac48382005-12-21 13:48:47 -0800144 line[--len] = 0;
145
146 if (!len)
147 continue;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700148
149 switch (line[0]) {
150 case 'P': /* P name */
151 if (parse_pack_def(line, old_cnt++))
152 goto out_stale;
153 break;
Junio C Hamano6f42f892005-12-04 22:52:19 -0800154 case 'D': /* we used to emit D but that was misguided. */
Junio C Hamano3e15c672005-12-04 23:12:36 -0800155 case 'T': /* we used to emit T but nobody uses it. */
156 goto out_stale;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700157 default:
158 error("unrecognized: %s", line);
159 break;
160 }
161 }
162 fclose(fp);
163 return 0;
164 out_stale:
165 fclose(fp);
166 return 1;
167}
168
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700169static int compare_info(const void *a_, const void *b_)
170{
Felipe Contreras4b25d092009-05-01 12:06:36 +0300171 struct pack_info *const *a = a_;
172 struct pack_info *const *b = b_;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700173
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
Junio C Hamanod5eac492005-12-04 23:02:54 -0800184 /* then it does not matter but at least keep the comparison stable */
Junio C Hamano2dee5812005-12-08 17:29:11 -0800185 if ((*a)->p == (*b)->p)
186 return 0;
187 else if ((*a)->p < (*b)->p)
188 return -1;
189 else
190 return 1;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700191}
192
193static void init_pack_info(const char *infofile, int force)
194{
195 struct packed_git *p;
196 int stale;
197 int i = 0;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700198
199 objdir = get_object_directory();
200 objdirlen = strlen(objdir);
201
202 prepare_packed_git();
203 for (p = packed_git; p; p = p->next) {
204 /* we ignore things on alternate path since they are
205 * not available to the pullers in general.
206 */
Junio C Hamanof13d7db2005-12-05 10:39:17 -0800207 if (!p->pack_local)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700208 continue;
209 i++;
210 }
211 num_pack = i;
212 info = xcalloc(num_pack, sizeof(struct pack_info *));
213 for (i = 0, p = packed_git; p; p = p->next) {
Junio C Hamanof13d7db2005-12-05 10:39:17 -0800214 if (!p->pack_local)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700215 continue;
Junio C Hamano6f42f892005-12-04 22:52:19 -0800216 info[i] = xcalloc(1, sizeof(struct pack_info));
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700217 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;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700230 info[i]->nr_heads = 0;
231 }
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700232 }
233
Junio C Hamano6f42f892005-12-04 22:52:19 -0800234 /* renumber them */
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200235 QSORT(info, num_pack, compare_info);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700236 for (i = 0; i < num_pack; i++)
237 info[i]->new_num = i;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700238}
239
Jeff King3907a402014-09-13 16:19:38 -0400240static void free_pack_info(void)
241{
242 int i;
243 for (i = 0; i < num_pack; i++)
244 free(info[i]);
245 free(info);
246}
247
Jeff Kingd38379e2014-09-13 16:19:20 -0400248static int write_pack_info_file(FILE *fp)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700249{
Junio C Hamano3e15c672005-12-04 23:12:36 -0800250 int i;
Jeff Kingd38379e2014-09-13 16:19:20 -0400251 for (i = 0; i < num_pack; i++) {
252 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
253 return -1;
254 }
255 if (fputc('\n', fp) == EOF)
256 return -1;
257 return 0;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700258}
259
260static int update_info_packs(int force)
261{
Jeff Kingd38379e2014-09-13 16:19:20 -0400262 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
263 int ret;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700264
265 init_pack_info(infofile, force);
Jeff Kingd38379e2014-09-13 16:19:20 -0400266 ret = update_info_file(infofile, write_pack_info_file);
Jeff King3907a402014-09-13 16:19:38 -0400267 free_pack_info();
Jeff Kingd38379e2014-09-13 16:19:20 -0400268 free(infofile);
269 return ret;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700270}
271
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700272/* public */
273int update_server_info(int force)
274{
275 /* We would add more dumb-server support files later,
276 * including index of available pack files and their
277 * intended audiences.
278 */
279 int errs = 0;
280
281 errs = errs | update_info_refs(force);
282 errs = errs | update_info_packs(force);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700283
Junio C Hamano6f42f892005-12-04 22:52:19 -0800284 /* remove leftover rev-cache file if there is any */
Alex Riesen691f1a22009-04-29 23:22:56 +0200285 unlink_or_warn(git_path("info/rev-cache"));
Junio C Hamano6f42f892005-12-04 22:52:19 -0800286
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700287 return errs;
288}