blob: 7ce6dcd67b7bef92f79bc24c319b2e46150980a3 [file] [log] [blame]
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07001#include "cache.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +01002#include "repository.h"
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07003#include "refs.h"
4#include "object.h"
5#include "commit.h"
Junio C Hamanob614e3d2005-07-28 14:33:17 -07006#include "tag.h"
Jonathan Tan0abe14f2017-08-18 15:20:26 -07007#include "packfile.h"
Stefan Bellera80d72d2018-03-23 18:20:59 +01008#include "object-store.h"
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07009
Jeff Kingd38379e2014-09-13 16:19:20 -040010/*
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;
René Scharfefa1912c2017-04-16 18:55:58 +020020 FILE *fp = NULL, *to_close;
Jeff Kingd38379e2014-09-13 16:19:20 -040021
22 safe_create_leading_directories(path);
Jeff Kingd91175b2015-01-05 22:50:49 -050023 fd = git_mkstemp_mode(tmp, 0666);
Jeff Kingd38379e2014-09-13 16:19:20 -040024 if (fd < 0)
25 goto out;
René Scharfefa1912c2017-04-16 18:55:58 +020026 to_close = fp = fdopen(fd, "w");
Jeff Kingd38379e2014-09-13 16:19:20 -040027 if (!fp)
28 goto out;
René Scharfefa1912c2017-04-16 18:55:58 +020029 fd = -1;
Jeff Kingd38379e2014-09-13 16:19:20 -040030 ret = generate(fp);
31 if (ret)
32 goto out;
René Scharfefa1912c2017-04-16 18:55:58 +020033 fp = NULL;
34 if (fclose(to_close))
Jeff Kingd38379e2014-09-13 16:19:20 -040035 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) {
Nguyễn Thái Ngọc Duy02382f52016-05-08 16:47:55 +070044 error_errno("unable to update %s", path);
Jeff Kingd38379e2014-09-13 16:19:20 -040045 if (fp)
46 fclose(fp);
47 else if (fd >= 0)
48 close(fd);
49 unlink(tmp);
50 }
51 free(tmp);
52 return ret;
53}
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070054
Michael Haggertye2b0bcd2015-05-25 18:39:04 +000055static int add_info_ref(const char *path, const struct object_id *oid,
56 int flag, void *cb_data)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070057{
Jeff Kingd38379e2014-09-13 16:19:20 -040058 FILE *fp = cb_data;
brian m. carlsonc251c832017-05-06 22:10:38 +000059 struct object *o = parse_object(oid);
Shawn O. Pearce76f8a302007-01-31 02:24:44 -050060 if (!o)
61 return -1;
Junio C Hamanof6b42a82005-10-13 18:57:40 -070062
Michael Haggertye2b0bcd2015-05-25 18:39:04 +000063 if (fprintf(fp, "%s %s\n", oid_to_hex(oid), path) < 0)
Jeff Kingd38379e2014-09-13 16:19:20 -040064 return -1;
65
Linus Torvalds19746322006-07-11 20:45:31 -070066 if (o->type == OBJ_TAG) {
Junio C Hamano9534f402005-11-02 15:19:13 -080067 o = deref_tag(o, path, 0);
68 if (o)
Jeff Kingd38379e2014-09-13 16:19:20 -040069 if (fprintf(fp, "%s %s^{}\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +000070 oid_to_hex(&o->oid), path) < 0)
Jeff Kingd38379e2014-09-13 16:19:20 -040071 return -1;
Junio C Hamanof6b42a82005-10-13 18:57:40 -070072 }
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070073 return 0;
74}
75
Jeff Kingd38379e2014-09-13 16:19:20 -040076static int generate_info_refs(FILE *fp)
77{
Michael Haggertye2b0bcd2015-05-25 18:39:04 +000078 return for_each_ref(add_info_ref, fp);
Jeff Kingd38379e2014-09-13 16:19:20 -040079}
80
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070081static int update_info_refs(int force)
82{
Jeff Kingd38379e2014-09-13 16:19:20 -040083 char *path = git_pathdup("info/refs");
84 int ret = update_info_file(path, generate_info_refs);
85 free(path);
86 return ret;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070087}
88
89/* packs */
Linus Torvalds4d8fa912005-08-01 12:11:53 -070090static struct pack_info {
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070091 struct packed_git *p;
92 int old_num;
93 int new_num;
94 int nr_alloc;
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
Stefan Bellera80d72d2018-03-23 18:20:59 +0100202 for (p = get_packed_git(the_repository); p; p = p->next) {
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700203 /* we ignore things on alternate path since they are
204 * not available to the pullers in general.
205 */
Junio C Hamanof13d7db2005-12-05 10:39:17 -0800206 if (!p->pack_local)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700207 continue;
208 i++;
209 }
210 num_pack = i;
211 info = xcalloc(num_pack, sizeof(struct pack_info *));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100212 for (i = 0, p = get_packed_git(the_repository); p; p = p->next) {
Junio C Hamanof13d7db2005-12-05 10:39:17 -0800213 if (!p->pack_local)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700214 continue;
Junio C Hamano6f42f892005-12-04 22:52:19 -0800215 info[i] = xcalloc(1, sizeof(struct pack_info));
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700216 info[i]->p = p;
217 info[i]->old_num = -1;
218 i++;
219 }
220
221 if (infofile && !force)
222 stale = read_pack_info_file(infofile);
223 else
224 stale = 1;
225
brian m. carlson910710b2018-05-02 00:25:30 +0000226 for (i = 0; i < num_pack; i++)
227 if (stale)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700228 info[i]->old_num = -1;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700229
Junio C Hamano6f42f892005-12-04 22:52:19 -0800230 /* renumber them */
René Scharfe9ed0d8d2016-09-29 17:27:31 +0200231 QSORT(info, num_pack, compare_info);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700232 for (i = 0; i < num_pack; i++)
233 info[i]->new_num = i;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700234}
235
Jeff King3907a402014-09-13 16:19:38 -0400236static void free_pack_info(void)
237{
238 int i;
239 for (i = 0; i < num_pack; i++)
240 free(info[i]);
241 free(info);
242}
243
Jeff Kingd38379e2014-09-13 16:19:20 -0400244static int write_pack_info_file(FILE *fp)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700245{
Junio C Hamano3e15c672005-12-04 23:12:36 -0800246 int i;
Jeff Kingd38379e2014-09-13 16:19:20 -0400247 for (i = 0; i < num_pack; i++) {
248 if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
249 return -1;
250 }
251 if (fputc('\n', fp) == EOF)
252 return -1;
253 return 0;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700254}
255
256static int update_info_packs(int force)
257{
Jeff Kingd38379e2014-09-13 16:19:20 -0400258 char *infofile = mkpathdup("%s/info/packs", get_object_directory());
259 int ret;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700260
261 init_pack_info(infofile, force);
Jeff Kingd38379e2014-09-13 16:19:20 -0400262 ret = update_info_file(infofile, write_pack_info_file);
Jeff King3907a402014-09-13 16:19:38 -0400263 free_pack_info();
Jeff Kingd38379e2014-09-13 16:19:20 -0400264 free(infofile);
265 return ret;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700266}
267
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700268/* public */
269int update_server_info(int force)
270{
271 /* We would add more dumb-server support files later,
272 * including index of available pack files and their
273 * intended audiences.
274 */
275 int errs = 0;
276
277 errs = errs | update_info_refs(force);
278 errs = errs | update_info_packs(force);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700279
Junio C Hamano6f42f892005-12-04 22:52:19 -0800280 /* remove leftover rev-cache file if there is any */
Alex Riesen691f1a22009-04-29 23:22:56 +0200281 unlink_or_warn(git_path("info/rev-cache"));
Junio C Hamano6f42f892005-12-04 22:52:19 -0800282
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700283 return errs;
284}