blob: 97b74238483e00c3f07bd5ab0879eb84bf5c8dfa [file] [log] [blame]
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
Linus Torvalds0fcfd162005-04-18 13:04:43 -07009#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Michael Haggerty6eac50d2012-11-05 09:41:22 +010011#include "string-list.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020012#include "lockfile.h"
Junio C Hamano1f688552005-06-27 03:35:33 -070013#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070014#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020015#include "blob.h"
16#include "commit.h"
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -070017#include "run-command.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020018#include "tag.h"
19#include "tree.h"
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +070020#include "tree-walk.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070021#include "refs.h"
Nicolas Pitre70f5d5d2008-02-28 00:25:19 -050022#include "pack-revindex.h"
Junio C Hamano628522e2007-12-29 02:05:47 -080023#include "sha1-lookup.h"
Junio C Hamano568508e2011-10-28 14:48:40 -070024#include "bulk-checkin.h"
Stefan Beller031dc922018-03-23 18:20:57 +010025#include "repository.h"
Stefan Beller47f351e2018-04-11 17:21:06 -070026#include "replace-object.h"
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +070027#include "streaming.h"
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070028#include "dir.h"
Jeff King12d95ef2016-08-22 17:59:42 -040029#include "list.h"
René Scharfec4c6eff2016-09-13 19:54:42 +020030#include "mergesort.h"
Jeff Kingcf3c6352016-12-12 14:52:22 -050031#include "quote.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070032#include "packfile.h"
Jonathan Tan8b4c0102017-12-08 15:27:14 +000033#include "fetch-object.h"
Stefan Beller90c62152018-03-23 18:20:55 +010034#include "object-store.h"
Pavel Roskine05db0f2007-01-09 23:07:11 -050035
brian m. carlson1af64f72018-03-12 02:27:55 +000036/* The maximum size for an object header. */
37#define MAX_HEADER_LEN 32
38
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000039
40#define EMPTY_TREE_SHA1_BIN_LITERAL \
41 "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
42 "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
43
44#define EMPTY_BLOB_SHA1_BIN_LITERAL \
45 "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
46 "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
47
Patryk Obara50c5cd52017-08-18 03:59:35 +020048const unsigned char null_sha1[GIT_MAX_RAWSZ];
brian m. carlson3e56e722015-12-06 22:16:35 +000049const struct object_id null_oid;
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000050static const struct object_id empty_tree_oid = {
Jacob Keller8576fde2016-08-31 16:27:18 -070051 EMPTY_TREE_SHA1_BIN_LITERAL
52};
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000053static const struct object_id empty_blob_oid = {
Jacob Keller8576fde2016-08-31 16:27:18 -070054 EMPTY_BLOB_SHA1_BIN_LITERAL
55};
Junio C Hamano88cd6212005-09-30 14:02:47 -070056
brian m. carlsonac73ced2018-02-01 02:18:38 +000057static void git_hash_sha1_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000058{
brian m. carlsonac73ced2018-02-01 02:18:38 +000059 git_SHA1_Init(&ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000060}
61
brian m. carlsonac73ced2018-02-01 02:18:38 +000062static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000063{
brian m. carlsonac73ced2018-02-01 02:18:38 +000064 git_SHA1_Update(&ctx->sha1, data, len);
brian m. carlsonf50e7662017-11-12 21:28:52 +000065}
66
brian m. carlsonac73ced2018-02-01 02:18:38 +000067static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000068{
brian m. carlsonac73ced2018-02-01 02:18:38 +000069 git_SHA1_Final(hash, &ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000070}
71
brian m. carlsonac73ced2018-02-01 02:18:38 +000072static void git_hash_unknown_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000073{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +020074 BUG("trying to init unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +000075}
76
brian m. carlsonac73ced2018-02-01 02:18:38 +000077static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000078{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +020079 BUG("trying to update unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +000080}
81
brian m. carlsonac73ced2018-02-01 02:18:38 +000082static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000083{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +020084 BUG("trying to finalize unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +000085}
86
87const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
88 {
89 NULL,
90 0x00000000,
91 0,
92 0,
brian m. carlsonf50e7662017-11-12 21:28:52 +000093 git_hash_unknown_init,
94 git_hash_unknown_update,
95 git_hash_unknown_final,
96 NULL,
97 NULL,
98 },
99 {
100 "sha-1",
101 /* "sha1", big-endian */
102 0x73686131,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000103 GIT_SHA1_RAWSZ,
104 GIT_SHA1_HEXSZ,
105 git_hash_sha1_init,
106 git_hash_sha1_update,
107 git_hash_sha1_final,
108 &empty_tree_oid,
109 &empty_blob_oid,
110 },
111};
112
brian m. carlsond8a92ce2018-05-02 00:25:54 +0000113const char *empty_tree_oid_hex(void)
114{
115 static char buf[GIT_MAX_HEXSZ + 1];
116 return oid_to_hex_r(buf, the_hash_algo->empty_tree);
117}
118
119const char *empty_blob_oid_hex(void)
120{
121 static char buf[GIT_MAX_HEXSZ + 1];
122 return oid_to_hex_r(buf, the_hash_algo->empty_blob);
123}
124
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700125/*
126 * This is meant to hold a *small* number of objects that you would
127 * want read_sha1_file() to be able to return, but yet you do not want
128 * to write them into the object store (e.g. a browse-only
129 * application).
130 */
131static struct cached_object {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000132 struct object_id oid;
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700133 enum object_type type;
134 void *buf;
135 unsigned long size;
136} *cached_objects;
137static int cached_object_nr, cached_object_alloc;
138
139static struct cached_object empty_tree = {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000140 { EMPTY_TREE_SHA1_BIN_LITERAL },
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700141 OBJ_TREE,
142 "",
143 0
144};
145
brian m. carlson62ba93e2018-05-02 00:26:03 +0000146static struct cached_object *find_cached_object(const struct object_id *oid)
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700147{
148 int i;
149 struct cached_object *co = cached_objects;
150
151 for (i = 0; i < cached_object_nr; i++, co++) {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000152 if (!oidcmp(&co->oid, oid))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700153 return co;
154 }
brian m. carlson62ba93e2018-05-02 00:26:03 +0000155 if (!oidcmp(oid, the_hash_algo->empty_tree))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700156 return &empty_tree;
157 return NULL;
158}
159
Torsten Bögershausen94729352017-11-16 17:38:28 +0100160
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100161static int get_conv_flags(unsigned flags)
Torsten Bögershausen94729352017-11-16 17:38:28 +0100162{
163 if (flags & HASH_RENORMALIZE)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100164 return CONV_EOL_RENORMALIZE;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100165 else if (flags & HASH_WRITE_OBJECT)
Lars Schneider107642f2018-04-15 20:16:07 +0200166 return global_conv_flags_eol | CONV_WRITE_OBJECT;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100167 else
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100168 return 0;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100169}
170
171
Junio C Hamano90a64642011-03-10 16:02:50 -0800172int mkdir_in_gitdir(const char *path)
173{
174 if (mkdir(path, 0777)) {
175 int saved_errno = errno;
176 struct stat st;
177 struct strbuf sb = STRBUF_INIT;
178
179 if (errno != EEXIST)
180 return -1;
181 /*
182 * Are we looking at a path in a symlinked worktree
183 * whose original repository does not yet have it?
184 * e.g. .git/rr-cache pointing at its original
185 * repository in which the user hasn't performed any
186 * conflict resolution yet?
187 */
188 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
189 strbuf_readlink(&sb, path, st.st_size) ||
190 !is_absolute_path(sb.buf) ||
191 mkdir(sb.buf, 0777)) {
192 strbuf_release(&sb);
193 errno = saved_errno;
194 return -1;
195 }
196 strbuf_release(&sb);
197 }
198 return adjust_shared_perm(path);
199}
200
Michael Haggerty0be05212014-01-06 14:45:25 +0100201enum scld_error safe_create_leading_directories(char *path)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700202{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100203 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100204 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800205
Michael Haggerty0be05212014-01-06 14:45:25 +0100206 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100207 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100208 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100209
Michael Haggerty0f527402014-01-19 00:40:44 +0100210 while (*slash && !is_dir_sep(*slash))
211 slash++;
212
213 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700214 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100215
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100216 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100217 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100218 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100219 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700220 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100221
Michael Haggerty0f527402014-01-19 00:40:44 +0100222 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100223 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800224 if (!stat(path, &st)) {
225 /* path exists */
Michael Haggerty204a0472017-01-06 17:22:25 +0100226 if (!S_ISDIR(st.st_mode)) {
227 errno = ENOTDIR;
Michael Haggerty0be05212014-01-06 14:45:25 +0100228 ret = SCLD_EXISTS;
Michael Haggerty204a0472017-01-06 17:22:25 +0100229 }
Michael Haggerty53a39722014-01-06 14:45:19 +0100230 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400231 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100232 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400233 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100234 else if (errno == ENOENT)
235 /*
236 * Either mkdir() failed because
237 * somebody just pruned the containing
238 * directory, or stat() failed because
239 * the file that was in our way was
240 * just removed. Either way, inform
241 * the caller that it might be worth
242 * trying again:
243 */
244 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100245 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100246 ret = SCLD_FAILED;
Michael Haggerty53a39722014-01-06 14:45:19 +0100247 } else if (adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100248 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800249 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100250 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700251 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100252 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700253}
254
Michael Haggerty0be05212014-01-06 14:45:25 +0100255enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400256{
Michael Haggerty02944302017-01-06 17:22:24 +0100257 int save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400258 /* path points to cache entries, so xstrdup before messing with it */
259 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100260 enum scld_error result = safe_create_leading_directories(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100261
262 save_errno = errno;
Jeff King8e21d632008-06-25 01:41:34 -0400263 free(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100264 errno = save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400265 return result;
266}
267
Michael Haggerty177978f2017-01-06 17:22:26 +0100268int raceproof_create_file(const char *path, create_file_fn fn, void *cb)
269{
270 /*
271 * The number of times we will try to remove empty directories
272 * in the way of path. This is only 1 because if another
273 * process is racily creating directories that conflict with
274 * us, we don't want to fight against them.
275 */
276 int remove_directories_remaining = 1;
277
278 /*
279 * The number of times that we will try to create the
280 * directories containing path. We are willing to attempt this
281 * more than once, because another process could be trying to
282 * clean up empty directories at the same time as we are
283 * trying to create them.
284 */
285 int create_directories_remaining = 3;
286
287 /* A scratch copy of path, filled lazily if we need it: */
288 struct strbuf path_copy = STRBUF_INIT;
289
290 int ret, save_errno;
291
292 /* Sanity check: */
293 assert(*path);
294
295retry_fn:
296 ret = fn(path, cb);
297 save_errno = errno;
298 if (!ret)
299 goto out;
300
301 if (errno == EISDIR && remove_directories_remaining-- > 0) {
302 /*
303 * A directory is in the way. Maybe it is empty; try
304 * to remove it:
305 */
306 if (!path_copy.len)
307 strbuf_addstr(&path_copy, path);
308
309 if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))
310 goto retry_fn;
311 } else if (errno == ENOENT && create_directories_remaining-- > 0) {
312 /*
313 * Maybe the containing directory didn't exist, or
314 * maybe it was just deleted by a process that is
315 * racing with us to clean up empty directories. Try
316 * to create it:
317 */
318 enum scld_error scld_result;
319
320 if (!path_copy.len)
321 strbuf_addstr(&path_copy, path);
322
323 do {
324 scld_result = safe_create_leading_directories(path_copy.buf);
325 if (scld_result == SCLD_OK)
326 goto retry_fn;
327 } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);
328 }
329
330out:
331 strbuf_release(&path_copy);
332 errno = save_errno;
333 return ret;
334}
335
Jeff Kingf7b77742016-10-03 16:36:09 -0400336static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
Junio C Hamanoace15342005-05-07 00:38:04 -0700337{
338 int i;
brian m. carlson94b5e092018-07-16 01:28:07 +0000339 for (i = 0; i < the_hash_algo->rawsz; i++) {
Junio C Hamanoace15342005-05-07 00:38:04 -0700340 static char hex[] = "0123456789abcdef";
341 unsigned int val = sha1[i];
Jeff Kingf7b77742016-10-03 16:36:09 -0400342 strbuf_addch(buf, hex[val >> 4]);
343 strbuf_addch(buf, hex[val & 0xf]);
Jeff Kingafbba2f2016-10-03 16:35:55 -0400344 if (!i)
Jeff Kingf7b77742016-10-03 16:36:09 -0400345 strbuf_addch(buf, '/');
Junio C Hamanoace15342005-05-07 00:38:04 -0700346 }
347}
348
Stefan Bellera68377b2018-03-23 18:21:16 +0100349void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700350{
Stefan Bellera68377b2018-03-23 18:21:16 +0100351 strbuf_addstr(buf, r->objects->objectdir);
Christian Couder34498472018-01-18 11:08:54 +0100352 strbuf_addch(buf, '/');
Christian Couderea657732018-01-17 18:54:54 +0100353 fill_sha1_path(buf, sha1);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700354}
355
Jeff King38dbe5f2016-10-03 16:36:04 -0400356struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
357{
358 strbuf_setlen(&alt->scratch, alt->base_len);
359 return &alt->scratch;
360}
361
Jeff King29ec6af2016-10-03 16:35:43 -0400362static const char *alt_sha1_path(struct alternate_object_database *alt,
363 const unsigned char *sha1)
364{
Jeff King38dbe5f2016-10-03 16:36:04 -0400365 struct strbuf *buf = alt_scratch_buf(alt);
Jeff Kingf7b77742016-10-03 16:36:09 -0400366 fill_sha1_path(buf, sha1);
Jeff King38dbe5f2016-10-03 16:36:04 -0400367 return buf->buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700368}
369
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700370/*
Jeff King4ea82472016-10-03 16:34:48 -0400371 * Return non-zero iff the path is usable as an alternate object database.
372 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100373static int alt_odb_usable(struct raw_object_store *o,
374 struct strbuf *path,
375 const char *normalized_objdir)
Jeff King4ea82472016-10-03 16:34:48 -0400376{
377 struct alternate_object_database *alt;
378
379 /* Detect cases where alternate disappeared */
380 if (!is_directory(path->buf)) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200381 error(_("object directory %s does not exist; "
382 "check .git/objects/info/alternates"),
Jeff King4ea82472016-10-03 16:34:48 -0400383 path->buf);
384 return 0;
385 }
386
387 /*
388 * Prevent the common mistake of listing the same
389 * thing twice, or object directory itself.
390 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100391 for (alt = o->alt_odb_list; alt; alt = alt->next) {
Jeff Kingea0fc3b2016-10-03 16:36:26 -0400392 if (!fspathcmp(path->buf, alt->path))
Jeff King4ea82472016-10-03 16:34:48 -0400393 return 0;
394 }
395 if (!fspathcmp(path->buf, normalized_objdir))
396 return 0;
397
398 return 1;
399}
400
401/*
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700402 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700403 *
404 * The variable alt_odb_list points at the list of struct
405 * alternate_object_database. The elements on this list come from
406 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
407 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800408 * whose contents is similar to that environment variable but can be
409 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700410 * contains "/the/directory/corresponding/to/.git/objects/...", while
411 * its name points just after the slash at the end of ".git/objects/"
412 * in the example above, and has enough space to hold 40-byte hex
413 * SHA1, an extra slash for the first level indirection, and the
414 * terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700415 */
Stefan Beller77f012e2018-03-23 18:21:08 +0100416static void read_info_alternates(struct repository *r,
417 const char *relative_base,
418 int depth);
419static int link_alt_odb_entry(struct repository *r, const char *entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100420 const char *relative_base, int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200421{
Martin Waitzc2f493a2006-05-07 20:19:21 +0200422 struct alternate_object_database *ent;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800423 struct strbuf pathbuf = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200424
Johannes Sixt85dadc32007-11-13 21:05:00 +0100425 if (!is_absolute_path(entry) && relative_base) {
Brandon Williams4ac90062016-12-12 10:16:55 -0800426 strbuf_realpath(&pathbuf, relative_base, 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800427 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200428 }
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100429 strbuf_addstr(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800430
Jeff King37a95862016-11-07 23:50:17 -0500431 if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200432 error(_("unable to normalize alternate object path: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400433 pathbuf.buf);
434 strbuf_release(&pathbuf);
435 return -1;
436 }
Hui Wang5bdf0a82011-09-07 18:37:47 +0800437
438 /*
439 * The trailing slash after the directory name is given by
440 * this function at the end. Remove duplicates.
441 */
Jeff King4ea82472016-10-03 16:34:48 -0400442 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
443 strbuf_setlen(&pathbuf, pathbuf.len - 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800444
Stefan Beller77f012e2018-03-23 18:21:08 +0100445 if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
Jeff King4ea82472016-10-03 16:34:48 -0400446 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200447 return -1;
448 }
449
Jeff King7f0fa2c2016-10-03 16:35:31 -0400450 ent = alloc_alt_odb(pathbuf.buf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200451
452 /* add the alternate entry */
Stefan Beller77f012e2018-03-23 18:21:08 +0100453 *r->objects->alt_odb_tail = ent;
454 r->objects->alt_odb_tail = &(ent->next);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200455 ent->next = NULL;
456
457 /* recursively add alternates */
Stefan Beller77f012e2018-03-23 18:21:08 +0100458 read_info_alternates(r, pathbuf.buf, depth + 1);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200459
Jeff King4ea82472016-10-03 16:34:48 -0400460 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200461 return 0;
462}
463
Jeff Kingcf3c6352016-12-12 14:52:22 -0500464static const char *parse_alt_odb_entry(const char *string,
465 int sep,
466 struct strbuf *out)
467{
468 const char *end;
469
470 strbuf_reset(out);
471
472 if (*string == '#') {
473 /* comment; consume up to next separator */
474 end = strchrnul(string, sep);
475 } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
476 /*
477 * quoted path; unquote_c_style has copied the
478 * data for us and set "end". Broken quoting (e.g.,
479 * an entry that doesn't end with a quote) falls
480 * back to the unquoted case below.
481 */
482 } else {
483 /* normal, unquoted path */
484 end = strchrnul(string, sep);
485 strbuf_add(out, string, end - string);
486 }
487
488 if (*end)
489 end++;
490 return end;
491}
492
Stefan Beller77f012e2018-03-23 18:21:08 +0100493static void link_alt_odb_entries(struct repository *r, const char *alt,
494 int sep, const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700495{
Ephrim Khong539e7502014-07-15 13:29:45 +0200496 struct strbuf objdirbuf = STRBUF_INIT;
Jeff Kingcf3c6352016-12-12 14:52:22 -0500497 struct strbuf entry = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200498
Jeff Kingf28e3662017-11-12 10:27:39 +0000499 if (!alt || !*alt)
500 return;
501
Martin Waitzc2f493a2006-05-07 20:19:21 +0200502 if (depth > 5) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200503 error(_("%s: ignoring alternate object stores, nesting too deep"),
Martin Waitzc2f493a2006-05-07 20:19:21 +0200504 relative_base);
505 return;
506 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700507
Stefan Beller77f012e2018-03-23 18:21:08 +0100508 strbuf_add_absolute_path(&objdirbuf, r->objects->objectdir);
Jeff King670c3592016-10-03 16:34:17 -0400509 if (strbuf_normalize_path(&objdirbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200510 die(_("unable to normalize object directory: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400511 objdirbuf.buf);
Ephrim Khong539e7502014-07-15 13:29:45 +0200512
Jeff Kingcf3c6352016-12-12 14:52:22 -0500513 while (*alt) {
514 alt = parse_alt_odb_entry(alt, sep, &entry);
515 if (!entry.len)
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700516 continue;
Stefan Beller77f012e2018-03-23 18:21:08 +0100517 link_alt_odb_entry(r, entry.buf,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100518 relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700519 }
Jeff Kingcf3c6352016-12-12 14:52:22 -0500520 strbuf_release(&entry);
Ephrim Khong539e7502014-07-15 13:29:45 +0200521 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700522}
523
Stefan Beller77f012e2018-03-23 18:21:08 +0100524static void read_info_alternates(struct repository *r,
525 const char *relative_base,
526 int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700527{
Jeff King5015f012015-08-19 14:12:45 -0400528 char *path;
Jeff Kingdc732bd2017-09-19 15:41:07 -0400529 struct strbuf buf = STRBUF_INIT;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700530
Jeff King5015f012015-08-19 14:12:45 -0400531 path = xstrfmt("%s/info/alternates", relative_base);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400532 if (strbuf_read_file(&buf, path, 1024) < 0) {
Jeff Kingf0f7beb2017-09-19 15:41:10 -0400533 warn_on_fopen_errors(path);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400534 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700535 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700536 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700537
Stefan Beller77f012e2018-03-23 18:21:08 +0100538 link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400539 strbuf_release(&buf);
540 free(path);
Junio C Hamanoace15342005-05-07 00:38:04 -0700541}
542
Jeff King7f0fa2c2016-10-03 16:35:31 -0400543struct alternate_object_database *alloc_alt_odb(const char *dir)
544{
545 struct alternate_object_database *ent;
Jeff King7f0fa2c2016-10-03 16:35:31 -0400546
Jeff King597f9132016-10-03 16:35:51 -0400547 FLEX_ALLOC_STR(ent, path, dir);
Jeff King38dbe5f2016-10-03 16:36:04 -0400548 strbuf_init(&ent->scratch, 0);
549 strbuf_addf(&ent->scratch, "%s/", dir);
550 ent->base_len = ent->scratch.len;
Jeff King7f0fa2c2016-10-03 16:35:31 -0400551
552 return ent;
553}
554
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400555void add_to_alternates_file(const char *reference)
556{
Martin Ågrenf132a122017-10-05 22:32:03 +0200557 struct lock_file lock = LOCK_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400558 char *alts = git_pathdup("objects/info/alternates");
559 FILE *in, *out;
Martin Ågrenf132a122017-10-05 22:32:03 +0200560 int found = 0;
Jeff King77b9b1d2015-08-10 05:34:46 -0400561
Martin Ågrenf132a122017-10-05 22:32:03 +0200562 hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
563 out = fdopen_lock_file(&lock, "w");
Jeff King77b9b1d2015-08-10 05:34:46 -0400564 if (!out)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200565 die_errno(_("unable to fdopen alternates lockfile"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400566
567 in = fopen(alts, "r");
568 if (in) {
569 struct strbuf line = STRBUF_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400570
Junio C Hamano3f163962015-10-28 13:29:24 -0700571 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400572 if (!strcmp(reference, line.buf)) {
573 found = 1;
574 break;
575 }
576 fprintf_or_die(out, "%s\n", line.buf);
577 }
578
579 strbuf_release(&line);
580 fclose(in);
Jeff King77b9b1d2015-08-10 05:34:46 -0400581 }
582 else if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200583 die_errno(_("unable to read alternates file"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400584
Martin Ågrenf132a122017-10-05 22:32:03 +0200585 if (found) {
586 rollback_lock_file(&lock);
587 } else {
Jeff King77b9b1d2015-08-10 05:34:46 -0400588 fprintf_or_die(out, "%s\n", reference);
Martin Ågrenf132a122017-10-05 22:32:03 +0200589 if (commit_lock_file(&lock))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200590 die_errno(_("unable to move new alternates file into place"));
Stefan Beller031dc922018-03-23 18:20:57 +0100591 if (the_repository->objects->alt_odb_tail)
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100592 link_alt_odb_entries(the_repository, reference,
593 '\n', NULL, 0);
Jeff King77b9b1d2015-08-10 05:34:46 -0400594 }
595 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400596}
597
Jeff Kinga5b34d22016-10-03 16:35:03 -0400598void add_to_alternates_memory(const char *reference)
599{
600 /*
601 * Make sure alternates are initialized, or else our entry may be
602 * overwritten when they are.
603 */
Stefan Beller0b209032018-03-23 18:21:07 +0100604 prepare_alt_odb(the_repository);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400605
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100606 link_alt_odb_entries(the_repository, reference,
607 '\n', NULL, 0);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400608}
609
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700610/*
611 * Compute the exact path an alternate is at and returns it. In case of
612 * error NULL is returned and the human readable error is added to `err`
Robert P. J. Dayefde7b72018-06-03 10:32:50 -0400613 * `path` may be relative and should point to $GIT_DIR.
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700614 * `err` must not be null.
615 */
616char *compute_alternate_path(const char *path, struct strbuf *err)
617{
618 char *ref_git = NULL;
619 const char *repo, *ref_git_s;
620 int seen_error = 0;
621
622 ref_git_s = real_path_if_valid(path);
623 if (!ref_git_s) {
624 seen_error = 1;
625 strbuf_addf(err, _("path '%s' does not exist"), path);
626 goto out;
627 } else
628 /*
629 * Beware: read_gitfile(), real_path() and mkpath()
630 * return static buffer
631 */
632 ref_git = xstrdup(ref_git_s);
633
634 repo = read_gitfile(ref_git);
635 if (!repo)
636 repo = read_gitfile(mkpath("%s/.git", ref_git));
637 if (repo) {
638 free(ref_git);
639 ref_git = xstrdup(repo);
640 }
641
642 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
643 char *ref_git_git = mkpathdup("%s/.git", ref_git);
644 free(ref_git);
645 ref_git = ref_git_git;
646 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
647 struct strbuf sb = STRBUF_INIT;
648 seen_error = 1;
649 if (get_common_dir(&sb, ref_git)) {
650 strbuf_addf(err,
651 _("reference repository '%s' as a linked "
652 "checkout is not supported yet."),
653 path);
654 goto out;
655 }
656
657 strbuf_addf(err, _("reference repository '%s' is not a "
658 "local repository."), path);
659 goto out;
660 }
661
662 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
663 strbuf_addf(err, _("reference repository '%s' is shallow"),
664 path);
665 seen_error = 1;
666 goto out;
667 }
668
669 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
670 strbuf_addf(err,
671 _("reference repository '%s' is grafted"),
672 path);
673 seen_error = 1;
674 goto out;
675 }
676
677out:
678 if (seen_error) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000679 FREE_AND_NULL(ref_git);
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700680 }
681
682 return ref_git;
683}
684
Jeff Kingfe1b2262014-10-15 18:33:13 -0400685int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700686{
687 struct alternate_object_database *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400688 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700689
Stefan Beller0b209032018-03-23 18:21:07 +0100690 prepare_alt_odb(the_repository);
Stefan Beller031dc922018-03-23 18:20:57 +0100691 for (ent = the_repository->objects->alt_odb_list; ent; ent = ent->next) {
Jeff Kingfe1b2262014-10-15 18:33:13 -0400692 r = fn(ent, cb);
693 if (r)
694 break;
695 }
696 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700697}
698
Stefan Beller13068bf2018-03-23 18:21:09 +0100699void prepare_alt_odb(struct repository *r)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200700{
Stefan Beller13068bf2018-03-23 18:21:09 +0100701 if (r->objects->alt_odb_tail)
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400702 return;
703
Stefan Beller13068bf2018-03-23 18:21:09 +0100704 r->objects->alt_odb_tail = &r->objects->alt_odb_list;
705 link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200706
Stefan Beller13068bf2018-03-23 18:21:09 +0100707 read_info_alternates(r, r->objects->objectdir, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200708}
709
Jeff King3096b2e2015-07-08 16:33:52 -0400710/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400711static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700712{
Jeff King33d42212014-10-15 18:42:22 -0400713 struct utimbuf t;
714 t.actime = t.modtime = time(NULL);
715 return !utime(fn, &t);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600716}
Junio C Hamanoace15342005-05-07 00:38:04 -0700717
Jeff King3096b2e2015-07-08 16:33:52 -0400718/*
719 * All of the check_and_freshen functions return 1 if the file exists and was
720 * freshened (if freshening was requested), 0 otherwise. If they return
721 * 0, you should not assume that it is safe to skip a write of the object (it
722 * either does not exist on disk, or has a stale mtime and may be subject to
723 * pruning).
724 */
Christian Couder6a5e6f52017-02-27 19:00:11 +0100725int check_and_freshen_file(const char *fn, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400726{
727 if (access(fn, F_OK))
728 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400729 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400730 return 0;
731 return 1;
732}
733
brian m. carlson6862ebb2018-05-02 00:25:34 +0000734static int check_and_freshen_local(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400735{
Christian Couderea657732018-01-17 18:54:54 +0100736 static struct strbuf buf = STRBUF_INIT;
737
738 strbuf_reset(&buf);
brian m. carlson6862ebb2018-05-02 00:25:34 +0000739 sha1_file_name(the_repository, &buf, oid->hash);
Christian Couderea657732018-01-17 18:54:54 +0100740
741 return check_and_freshen_file(buf.buf, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400742}
743
brian m. carlson6862ebb2018-05-02 00:25:34 +0000744static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600745{
746 struct alternate_object_database *alt;
Stefan Beller0b209032018-03-23 18:21:07 +0100747 prepare_alt_odb(the_repository);
Stefan Beller031dc922018-03-23 18:20:57 +0100748 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
brian m. carlson6862ebb2018-05-02 00:25:34 +0000749 const char *path = alt_sha1_path(alt, oid->hash);
Jeff King29ec6af2016-10-03 16:35:43 -0400750 if (check_and_freshen_file(path, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700751 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700752 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700753 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700754}
755
brian m. carlson6862ebb2018-05-02 00:25:34 +0000756static int check_and_freshen(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400757{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000758 return check_and_freshen_local(oid, freshen) ||
759 check_and_freshen_nonlocal(oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400760}
761
brian m. carlson6862ebb2018-05-02 00:25:34 +0000762int has_loose_object_nonlocal(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -0400763{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000764 return check_and_freshen_nonlocal(oid, 0);
Jeff King33d42212014-10-15 18:42:22 -0400765}
766
brian m. carlson6862ebb2018-05-02 00:25:34 +0000767static int has_loose_object(const struct object_id *oid)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600768{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000769 return check_and_freshen(oid, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600770}
771
Steffen Prohaska02710222014-08-26 17:23:23 +0200772static void mmap_limit_check(size_t length)
773{
774 static size_t limit = 0;
775 if (!limit) {
776 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
777 if (!limit)
778 limit = SIZE_MAX;
779 }
780 if (length > limit)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200781 die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
Steffen Prohaska02710222014-08-26 17:23:23 +0200782 (uintmax_t)length, (uintmax_t)limit);
783}
784
Jeff King15708562015-05-28 03:56:15 -0400785void *xmmap_gently(void *start, size_t length,
786 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500787{
Steffen Prohaska02710222014-08-26 17:23:23 +0200788 void *ret;
789
790 mmap_limit_check(length);
791 ret = mmap(start, length, prot, flags, fd, offset);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500792 if (ret == MAP_FAILED) {
793 if (!length)
794 return NULL;
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700795 release_pack_memory(length);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500796 ret = mmap(start, length, prot, flags, fd, offset);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500797 }
798 return ret;
799}
800
Jeff King15708562015-05-28 03:56:15 -0400801void *xmmap(void *start, size_t length,
802 int prot, int flags, int fd, off_t offset)
803{
804 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
805 if (ret == MAP_FAILED)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200806 die_errno(_("mmap failed"));
Jeff King15708562015-05-28 03:56:15 -0400807 return ret;
808}
809
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700810/*
811 * With an in-core object data in "map", rehash it to make sure the
812 * object name actually matches "sha1" to detect object corruption.
813 * With "map" == NULL, try reading the object named with "sha1" using
814 * the streaming interface and rehash it to do the same.
815 */
brian m. carlson17e65452018-03-12 02:27:39 +0000816int check_object_signature(const struct object_id *oid, void *map,
817 unsigned long size, const char *type)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700818{
Patryk Obaraf070fac2018-01-28 01:13:13 +0100819 struct object_id real_oid;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700820 enum object_type obj_type;
821 struct git_istream *st;
brian m. carlson18e25882018-02-01 02:18:41 +0000822 git_hash_ctx c;
brian m. carlson1af64f72018-03-12 02:27:55 +0000823 char hdr[MAX_HEADER_LEN];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700824 int hdrlen;
825
826 if (map) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100827 hash_object_file(map, size, type, &real_oid);
brian m. carlson17e65452018-03-12 02:27:39 +0000828 return oidcmp(oid, &real_oid) ? -1 : 0;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700829 }
830
brian m. carlsonef7b5192018-03-12 02:27:40 +0000831 st = open_istream(oid, &obj_type, &size, NULL);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700832 if (!st)
833 return -1;
834
835 /* Generate the header */
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800836 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(obj_type), size) + 1;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700837
838 /* Sha1.. */
brian m. carlson18e25882018-02-01 02:18:41 +0000839 the_hash_algo->init_fn(&c);
840 the_hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700841 for (;;) {
842 char buf[1024 * 16];
843 ssize_t readlen = read_istream(st, buf, sizeof(buf));
844
Jeff Kingf54fac52013-03-25 16:17:17 -0400845 if (readlen < 0) {
846 close_istream(st);
847 return -1;
848 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700849 if (!readlen)
850 break;
brian m. carlson18e25882018-02-01 02:18:41 +0000851 the_hash_algo->update_fn(&c, buf, readlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700852 }
brian m. carlson18e25882018-02-01 02:18:41 +0000853 the_hash_algo->final_fn(real_oid.hash, &c);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700854 close_istream(st);
brian m. carlson17e65452018-03-12 02:27:39 +0000855 return oidcmp(oid, &real_oid) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700856}
857
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700858int git_open_cloexec(const char *name, int flags)
Linus Torvalds44d1c192008-06-14 11:32:37 -0700859{
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700860 int fd;
861 static int o_cloexec = O_CLOEXEC;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700862
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700863 fd = open(name, flags | o_cloexec);
864 if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
Lars Schneidercd66ada2016-10-24 20:02:59 +0200865 /* Try again w/o O_CLOEXEC: the kernel might not support it */
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700866 o_cloexec &= ~O_CLOEXEC;
867 fd = open(name, flags | o_cloexec);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700868 }
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700869
Eric Wong9fb94952017-07-15 18:55:40 +0000870#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700871 {
872 static int fd_cloexec = FD_CLOEXEC;
873
874 if (!o_cloexec && 0 <= fd && fd_cloexec) {
875 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
Eric Wong9fb94952017-07-15 18:55:40 +0000876 int flags = fcntl(fd, F_GETFD);
877 if (fcntl(fd, F_SETFD, flags | fd_cloexec))
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700878 fd_cloexec = 0;
879 }
880 }
881#endif
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700882 return fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700883}
884
Jeff King771e7d52017-01-13 12:54:39 -0500885/*
886 * Find "sha1" as a loose object in the local repository or in an alternate.
887 * Returns 0 on success, negative on failure.
888 *
889 * The "path" out-parameter will give the path of the object we found (if any).
890 * Note that it may point to static storage and is only valid until another
891 * call to sha1_file_name(), etc.
892 */
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100893static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
894 struct stat *st, const char **path)
Jeff King052fe5e2013-07-12 02:30:48 -0400895{
Jeff King052fe5e2013-07-12 02:30:48 -0400896 struct alternate_object_database *alt;
Christian Couderea657732018-01-17 18:54:54 +0100897 static struct strbuf buf = STRBUF_INIT;
Jeff King052fe5e2013-07-12 02:30:48 -0400898
Christian Couderea657732018-01-17 18:54:54 +0100899 strbuf_reset(&buf);
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100900 sha1_file_name(r, &buf, sha1);
Christian Couderea657732018-01-17 18:54:54 +0100901 *path = buf.buf;
902
Jeff King771e7d52017-01-13 12:54:39 -0500903 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -0400904 return 0;
905
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100906 prepare_alt_odb(r);
Jeff King052fe5e2013-07-12 02:30:48 -0400907 errno = ENOENT;
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100908 for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
Jeff King771e7d52017-01-13 12:54:39 -0500909 *path = alt_sha1_path(alt, sha1);
910 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -0400911 return 0;
912 }
913
914 return -1;
915}
916
Jeff King771e7d52017-01-13 12:54:39 -0500917/*
918 * Like stat_sha1_file(), but actually open the object and return the
919 * descriptor. See the caveats on the "path" parameter above.
920 */
Stefan Bellerec7283e2018-03-23 18:21:18 +0100921static int open_sha1_file(struct repository *r,
922 const unsigned char *sha1, const char **path)
Linus Torvalds44d1c192008-06-14 11:32:37 -0700923{
924 int fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700925 struct alternate_object_database *alt;
Jeff Kingd6c8a052014-05-15 04:54:06 -0400926 int most_interesting_errno;
Christian Couderea657732018-01-17 18:54:54 +0100927 static struct strbuf buf = STRBUF_INIT;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700928
Christian Couderea657732018-01-17 18:54:54 +0100929 strbuf_reset(&buf);
Stefan Bellerec7283e2018-03-23 18:21:18 +0100930 sha1_file_name(r, &buf, sha1);
Christian Couderea657732018-01-17 18:54:54 +0100931 *path = buf.buf;
932
Jeff King771e7d52017-01-13 12:54:39 -0500933 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700934 if (fd >= 0)
935 return fd;
Jeff Kingd6c8a052014-05-15 04:54:06 -0400936 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700937
Stefan Bellerec7283e2018-03-23 18:21:18 +0100938 prepare_alt_odb(r);
939 for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
Jeff King771e7d52017-01-13 12:54:39 -0500940 *path = alt_sha1_path(alt, sha1);
941 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700942 if (fd >= 0)
943 return fd;
Jeff Kingd6c8a052014-05-15 04:54:06 -0400944 if (most_interesting_errno == ENOENT)
945 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700946 }
Jeff Kingd6c8a052014-05-15 04:54:06 -0400947 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700948 return -1;
949}
950
Jeff Kingf6371f92017-01-13 12:58:16 -0500951/*
952 * Map the loose object at "path" if it is not NULL, or the path found by
953 * searching for a loose object named "sha1".
954 */
Jonathan Nieder1fea63e2018-03-23 18:21:19 +0100955static void *map_sha1_file_1(struct repository *r, const char *path,
956 const unsigned char *sha1, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700957{
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700958 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -0700959 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -0700960
Jeff Kingf6371f92017-01-13 12:58:16 -0500961 if (path)
962 fd = git_open(path);
963 else
Jonathan Nieder1fea63e2018-03-23 18:21:19 +0100964 fd = open_sha1_file(r, sha1, &path);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700965 map = NULL;
966 if (fd >= 0) {
967 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700968
Linus Torvalds44d1c192008-06-14 11:32:37 -0700969 if (!fstat(fd, &st)) {
970 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +0100971 if (!*size) {
972 /* mmap() is forbidden on empty files */
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200973 error(_("object file %s is empty"), path);
Matthieu Moy33e42de2012-02-06 17:24:52 +0100974 return NULL;
975 }
Linus Torvalds44d1c192008-06-14 11:32:37 -0700976 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -0700977 }
Linus Torvalds44d1c192008-06-14 11:32:37 -0700978 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700979 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700980 return map;
981}
982
Stefan Bellerbd27f502018-03-23 18:21:20 +0100983void *map_sha1_file(struct repository *r,
984 const unsigned char *sha1, unsigned long *size)
Jeff Kingf6371f92017-01-13 12:58:16 -0500985{
Stefan Bellerbd27f502018-03-23 18:21:20 +0100986 return map_sha1_file_1(r, NULL, sha1, size);
Jeff Kingf6371f92017-01-13 12:58:16 -0500987}
988
Junio C Hamanod21f8422016-09-25 21:29:04 -0700989static int unpack_sha1_short_header(git_zstream *stream,
990 unsigned char *map, unsigned long mapsize,
991 void *buffer, unsigned long bufsiz)
Linus Torvaldsc4483572005-06-01 17:54:59 -0700992{
993 /* Get the data stream */
994 memset(stream, 0, sizeof(*stream));
995 stream->next_in = map;
996 stream->avail_in = mapsize;
997 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -0700998 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -0700999
Linus Torvalds39c68542009-01-07 19:54:47 -08001000 git_inflate_init(stream);
Junio C Hamanocc5c54e2011-06-08 11:29:01 -07001001 return git_inflate(stream, 0);
Linus Torvaldsc4483572005-06-01 17:54:59 -07001002}
1003
Junio C Hamanod21f8422016-09-25 21:29:04 -07001004int unpack_sha1_header(git_zstream *stream,
1005 unsigned char *map, unsigned long mapsize,
1006 void *buffer, unsigned long bufsiz)
1007{
1008 int status = unpack_sha1_short_header(stream, map, mapsize,
1009 buffer, bufsiz);
1010
1011 if (status < Z_OK)
1012 return status;
1013
1014 /* Make sure we have the terminating NUL */
1015 if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1016 return -1;
1017 return 0;
1018}
1019
Karthik Nayak46f03442015-05-03 19:59:59 +05301020static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1021 unsigned long mapsize, void *buffer,
1022 unsigned long bufsiz, struct strbuf *header)
1023{
1024 int status;
1025
Junio C Hamanod21f8422016-09-25 21:29:04 -07001026 status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
1027 if (status < Z_OK)
1028 return -1;
Karthik Nayak46f03442015-05-03 19:59:59 +05301029
1030 /*
1031 * Check if entire header is unpacked in the first iteration.
1032 */
1033 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1034 return 0;
1035
1036 /*
1037 * buffer[0..bufsiz] was not large enough. Copy the partial
1038 * result out to header, and then append the result of further
1039 * reading the stream.
1040 */
1041 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1042 stream->next_out = buffer;
1043 stream->avail_out = bufsiz;
1044
1045 do {
1046 status = git_inflate(stream, 0);
1047 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1048 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1049 return 0;
1050 stream->next_out = buffer;
1051 stream->avail_out = bufsiz;
1052 } while (status != Z_STREAM_END);
1053 return -1;
1054}
1055
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001056static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001057{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001058 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001059 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001060 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001061 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001062
Linus Torvalds93821bd2006-07-11 12:48:08 -07001063 n = stream->total_out - bytes;
1064 if (n > size)
1065 n = size;
1066 memcpy(buf, (char *) buffer + bytes, n);
1067 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001068 if (bytes <= size) {
1069 /*
1070 * The above condition must be (bytes <= size), not
1071 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001072 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001073 * the input zlib stream may have bytes that express
1074 * "this concludes the stream", and we *do* want to
1075 * eat that input.
1076 *
1077 * Otherwise we would not be able to test that we
1078 * consumed all the input to reach the expected size;
1079 * we also want to check that zlib tells us that all
1080 * went well with status == Z_STREAM_END at the end.
1081 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001082 stream->next_out = buf + bytes;
1083 stream->avail_out = size - bytes;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001084 while (status == Z_OK)
Linus Torvalds39c68542009-01-07 19:54:47 -08001085 status = git_inflate(stream, Z_FINISH);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001086 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001087 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001088 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001089 return buf;
1090 }
1091
1092 if (status < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001093 error(_("corrupt loose object '%s'"), sha1_to_hex(sha1));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001094 else if (stream->avail_in)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001095 error(_("garbage at end of loose object '%s'"),
Junio C Hamano7efbff72007-03-05 00:21:37 -08001096 sha1_to_hex(sha1));
1097 free(buf);
1098 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001099}
1100
Linus Torvalds5180cac2005-06-02 07:57:25 -07001101/*
1102 * We used to just use "sscanf()", but that's actually way
1103 * too permissive for what we want to check. So do an anal
1104 * object header parse by hand.
1105 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301106static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1107 unsigned int flags)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001108{
Karthik Nayak46f03442015-05-03 19:59:59 +05301109 const char *type_buf = hdr;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001110 unsigned long size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301111 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001112
1113 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301114 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001115 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001116 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001117 for (;;) {
1118 char c = *hdr++;
Junio C Hamanod21f8422016-09-25 21:29:04 -07001119 if (!c)
1120 return -1;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001121 if (c == ' ')
1122 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301123 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001124 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301125
1126 type = type_from_string_gently(type_buf, type_len, 1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001127 if (oi->type_name)
1128 strbuf_add(oi->type_name, type_buf, type_len);
Karthik Nayak46f03442015-05-03 19:59:59 +05301129 /*
1130 * Set type to 0 if its an unknown object and
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001131 * we're obtaining the type using '--allow-unknown-type'
Karthik Nayak46f03442015-05-03 19:59:59 +05301132 * option.
1133 */
Jonathan Tan19fc5e82017-06-21 17:40:18 -07001134 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
Karthik Nayak46f03442015-05-03 19:59:59 +05301135 type = 0;
1136 else if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001137 die(_("invalid object type"));
Karthik Nayak46f03442015-05-03 19:59:59 +05301138 if (oi->typep)
1139 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001140
1141 /*
1142 * The length must follow immediately, and be in canonical
1143 * decimal format (ie "010" is not valid).
1144 */
1145 size = *hdr++ - '0';
1146 if (size > 9)
1147 return -1;
1148 if (size) {
1149 for (;;) {
1150 unsigned long c = *hdr - '0';
1151 if (c > 9)
1152 break;
1153 hdr++;
1154 size = size * 10 + c;
1155 }
1156 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301157
1158 if (oi->sizep)
1159 *oi->sizep = size;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001160
1161 /*
1162 * The length must be followed by a zero byte
1163 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301164 return *hdr ? -1 : type;
1165}
1166
1167int parse_sha1_header(const char *hdr, unsigned long *sizep)
1168{
Jeff King27b5c1a2016-08-11 05:24:35 -04001169 struct object_info oi = OBJECT_INFO_INIT;
Karthik Nayak46f03442015-05-03 19:59:59 +05301170
1171 oi.sizep = sizep;
Jonathan Tan1f0c0d32017-06-21 17:40:19 -07001172 return parse_sha1_header_extended(hdr, &oi, 0);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001173}
1174
Jonathan Nieder4a7c05f2018-03-23 18:21:21 +01001175static int sha1_loose_object_info(struct repository *r,
1176 const unsigned char *sha1,
1177 struct object_info *oi, int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001178{
Karthik Nayak46f03442015-05-03 19:59:59 +05301179 int status = 0;
1180 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001181 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001182 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00001183 char hdr[MAX_HEADER_LEN];
Karthik Nayak46f03442015-05-03 19:59:59 +05301184 struct strbuf hdrbuf = STRBUF_INIT;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001185 unsigned long size_scratch;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001186
Jeff King5d642e72013-12-21 09:24:20 -05001187 if (oi->delta_base_sha1)
1188 hashclr(oi->delta_base_sha1);
1189
Jeff King052fe5e2013-07-12 02:30:48 -04001190 /*
1191 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001192 * need to look inside the object at all. Note that we
1193 * do not optimize out the stat call, even if the
1194 * caller doesn't care about the disk-size, since our
1195 * return value implicitly indicates whether the
1196 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04001197 */
Brandon Williams6ca32f42018-02-14 10:59:23 -08001198 if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
Jeff King771e7d52017-01-13 12:54:39 -05001199 const char *path;
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001200 struct stat st;
Jonathan Nieder4a7c05f2018-03-23 18:21:21 +01001201 if (stat_sha1_file(r, sha1, &st, &path) < 0)
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001202 return -1;
1203 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04001204 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04001205 return 0;
1206 }
1207
Jonathan Nieder4a7c05f2018-03-23 18:21:21 +01001208 map = map_sha1_file(r, sha1, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001209 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02001210 return -1;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001211
1212 if (!oi->sizep)
1213 oi->sizep = &size_scratch;
1214
Jeff King23c339c2013-07-12 02:37:53 -04001215 if (oi->disk_sizep)
1216 *oi->disk_sizep = mapsize;
Jonathan Tan19fc5e82017-06-21 17:40:18 -07001217 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
Karthik Nayak46f03442015-05-03 19:59:59 +05301218 if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001219 status = error(_("unable to unpack %s header with --allow-unknown-type"),
Karthik Nayak46f03442015-05-03 19:59:59 +05301220 sha1_to_hex(sha1));
1221 } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001222 status = error(_("unable to unpack %s header"),
Junio C Hamano36e4d742005-06-27 03:34:06 -07001223 sha1_to_hex(sha1));
Karthik Nayak46f03442015-05-03 19:59:59 +05301224 if (status < 0)
1225 ; /* Do nothing */
1226 else if (hdrbuf.len) {
1227 if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001228 status = error(_("unable to parse %s header with --allow-unknown-type"),
Karthik Nayak46f03442015-05-03 19:59:59 +05301229 sha1_to_hex(sha1));
1230 } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001231 status = error(_("unable to parse %s header"), sha1_to_hex(sha1));
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001232
Jeff Kingb3ea7dd2017-10-05 01:59:52 -04001233 if (status >= 0 && oi->contentp) {
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001234 *oi->contentp = unpack_sha1_rest(&stream, hdr,
1235 *oi->sizep, sha1);
Jeff Kingb3ea7dd2017-10-05 01:59:52 -04001236 if (!*oi->contentp) {
1237 git_inflate_end(&stream);
1238 status = -1;
1239 }
1240 } else
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001241 git_inflate_end(&stream);
1242
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001243 munmap(map, mapsize);
Karthik Nayak46f03442015-05-03 19:59:59 +05301244 if (status && oi->typep)
Jeff King23c339c2013-07-12 02:37:53 -04001245 *oi->typep = status;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001246 if (oi->sizep == &size_scratch)
1247 oi->sizep = NULL;
Karthik Nayak46f03442015-05-03 19:59:59 +05301248 strbuf_release(&hdrbuf);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001249 oi->whence = OI_LOOSE;
Jeff King93cff9a2017-04-01 04:05:21 -04001250 return (status < 0) ? status : 0;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001251}
1252
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001253int fetch_if_missing = 1;
1254
Stefan Beller9d983542018-04-25 11:21:06 -07001255int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1256 struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001257{
Jonathan Tancd585e22017-06-21 17:40:23 -07001258 static struct object_info blank_oi = OBJECT_INFO_INIT;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001259 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04001260 int rtype;
brian m. carlsonb383a132018-03-12 02:27:54 +00001261 const struct object_id *real = oid;
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001262 int already_retried = 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001263
brian m. carlsonb383a132018-03-12 02:27:54 +00001264 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
Stefan Beller9d983542018-04-25 11:21:06 -07001265 real = lookup_replace_object(r, oid);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001266
brian m. carlsonb383a132018-03-12 02:27:54 +00001267 if (is_null_oid(real))
Jeff King87b5e232017-11-21 18:17:39 -05001268 return -1;
1269
Jonathan Tancd585e22017-06-21 17:40:23 -07001270 if (!oi)
1271 oi = &blank_oi;
1272
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001273 if (!(flags & OBJECT_INFO_SKIP_CACHED)) {
brian m. carlson62ba93e2018-05-02 00:26:03 +00001274 struct cached_object *co = find_cached_object(real);
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001275 if (co) {
1276 if (oi->typep)
1277 *(oi->typep) = co->type;
1278 if (oi->sizep)
1279 *(oi->sizep) = co->size;
1280 if (oi->disk_sizep)
1281 *(oi->disk_sizep) = 0;
1282 if (oi->delta_base_sha1)
1283 hashclr(oi->delta_base_sha1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001284 if (oi->type_name)
Brandon Williamsdebca9d2018-02-14 10:59:24 -08001285 strbuf_addstr(oi->type_name, type_name(co->type));
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001286 if (oi->contentp)
1287 *oi->contentp = xmemdupz(co->buf, co->size);
1288 oi->whence = OI_CACHED;
1289 return 0;
1290 }
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07001291 }
1292
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001293 while (1) {
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001294 if (find_pack_entry(r, real, &e))
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001295 break;
1296
Takuto Ikuta024aa462018-03-14 15:32:42 +09001297 if (flags & OBJECT_INFO_IGNORE_LOOSE)
1298 return -1;
1299
Steven Grimmddd63e62008-08-05 13:08:41 -07001300 /* Most likely it's a loose object. */
Stefan Beller9d983542018-04-25 11:21:06 -07001301 if (!sha1_loose_object_info(r, real->hash, oi, flags))
Jeff King5b086402013-07-12 02:34:57 -04001302 return 0;
Steven Grimmddd63e62008-08-05 13:08:41 -07001303
1304 /* Not a loose object; someone else may have just packed it. */
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001305 if (!(flags & OBJECT_INFO_QUICK)) {
Stefan Beller9d983542018-04-25 11:21:06 -07001306 reprepare_packed_git(r);
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001307 if (find_pack_entry(r, real, &e))
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001308 break;
1309 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001310
1311 /* Check if it is a missing object */
1312 if (fetch_if_missing && repository_format_partial_clone &&
Stefan Beller9d983542018-04-25 11:21:06 -07001313 !already_retried && r == the_repository) {
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001314 /*
Stefan Beller9d983542018-04-25 11:21:06 -07001315 * TODO Investigate having fetch_object() return
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001316 * TODO error/success and stopping the music here.
Stefan Beller9d983542018-04-25 11:21:06 -07001317 * TODO Pass a repository struct through fetch_object,
1318 * such that arbitrary repositories work.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001319 */
brian m. carlsonb383a132018-03-12 02:27:54 +00001320 fetch_object(repository_format_partial_clone, real->hash);
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001321 already_retried = 1;
1322 continue;
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001323 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001324
1325 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001326 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001327
Jonathan Tancd585e22017-06-21 17:40:23 -07001328 if (oi == &blank_oi)
1329 /*
1330 * We know that the caller doesn't actually need the
1331 * information below, so return early.
1332 */
1333 return 0;
Stefan Beller9d983542018-04-25 11:21:06 -07001334 rtype = packed_object_info(r, e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04001335 if (rtype < 0) {
brian m. carlsonb383a132018-03-12 02:27:54 +00001336 mark_bad_packed_object(e.p, real->hash);
Stefan Beller9d983542018-04-25 11:21:06 -07001337 return oid_object_info_extended(r, real, oi, 0);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001338 } else if (oi->whence == OI_PACKED) {
Junio C Hamano9a490592011-05-12 15:51:38 -07001339 oi->u.packed.offset = e.offset;
1340 oi->u.packed.pack = e.p;
1341 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
1342 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001343 }
1344
Jeff King5b086402013-07-12 02:34:57 -04001345 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001346}
1347
Christian Couder3fc0dca2013-10-27 00:34:30 +02001348/* returns enum object_type or negative */
Stefan Beller9d983542018-04-25 11:21:06 -07001349int oid_object_info(struct repository *r,
1350 const struct object_id *oid,
1351 unsigned long *sizep)
Junio C Hamano9a490592011-05-12 15:51:38 -07001352{
Jeff King5b086402013-07-12 02:34:57 -04001353 enum object_type type;
Jeff King27b5c1a2016-08-11 05:24:35 -04001354 struct object_info oi = OBJECT_INFO_INIT;
Junio C Hamano9a490592011-05-12 15:51:38 -07001355
Jeff King5b086402013-07-12 02:34:57 -04001356 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001357 oi.sizep = sizep;
Stefan Beller9d983542018-04-25 11:21:06 -07001358 if (oid_object_info_extended(r, oid, &oi,
1359 OBJECT_INFO_LOOKUP_REPLACE) < 0)
Jeff King5b086402013-07-12 02:34:57 -04001360 return -1;
1361 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001362}
1363
Jonathan Tanf1d81302017-08-18 15:20:30 -07001364static void *read_object(const unsigned char *sha1, enum object_type *type,
1365 unsigned long *size)
1366{
brian m. carlson7984f232018-03-12 02:27:45 +00001367 struct object_id oid;
Jonathan Tanf1d81302017-08-18 15:20:30 -07001368 struct object_info oi = OBJECT_INFO_INIT;
1369 void *content;
1370 oi.typep = type;
1371 oi.sizep = size;
1372 oi.contentp = &content;
1373
brian m. carlson7984f232018-03-12 02:27:45 +00001374 hashcpy(oid.hash, sha1);
1375
Stefan Beller7ecd8692018-04-25 11:20:58 -07001376 if (oid_object_info_extended(the_repository, &oid, &oi, 0) < 0)
Jonathan Tanf1d81302017-08-18 15:20:30 -07001377 return NULL;
1378 return content;
1379}
1380
Patryk Obara829e5c32018-01-28 01:13:11 +01001381int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1382 struct object_id *oid)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001383{
1384 struct cached_object *co;
1385
Junio C Hamano169c9c02018-03-06 14:54:07 -08001386 hash_object_file(buf, len, type_name(type), oid);
brian m. carlson62ba93e2018-05-02 00:26:03 +00001387 if (has_sha1_file(oid->hash) || find_cached_object(oid))
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001388 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04001389 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001390 co = &cached_objects[cached_object_nr++];
1391 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001392 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08001393 co->buf = xmalloc(len);
1394 memcpy(co->buf, buf, len);
brian m. carlson62ba93e2018-05-02 00:26:03 +00001395 oidcpy(&co->oid, oid);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001396 return 0;
1397}
1398
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001399/*
1400 * This function dies on corrupt objects; the callers who want to
1401 * deal with them should arrange to call read_object() and give error
1402 * messages themselves.
1403 */
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001404void *read_object_file_extended(const struct object_id *oid,
1405 enum object_type *type,
1406 unsigned long *size,
1407 int lookup_replace)
Nicolas Pitreac939102008-07-14 21:46:48 -04001408{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001409 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001410 const struct packed_git *p;
Jeff King771e7d52017-01-13 12:54:39 -05001411 const char *path;
1412 struct stat st;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -07001413 const struct object_id *repl = lookup_replace ?
1414 lookup_replace_object(the_repository, oid) : oid;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001415
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001416 errno = 0;
brian m. carlsonb383a132018-03-12 02:27:54 +00001417 data = read_object(repl->hash, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07001418 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001419 return data;
Christian Couder68095572009-01-23 10:06:53 +01001420
Björn Steinbrink25f3af32011-01-20 21:12:20 +01001421 if (errno && errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001422 die_errno(_("failed to read object %s"), oid_to_hex(oid));
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001423
Christian Couder68095572009-01-23 10:06:53 +01001424 /* die if we replaced an object with one that does not exist */
brian m. carlsonb383a132018-03-12 02:27:54 +00001425 if (repl != oid)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001426 die(_("replacement %s not found for %s"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001427 oid_to_hex(repl), oid_to_hex(oid));
Christian Couder68095572009-01-23 10:06:53 +01001428
Junio C Hamanocf0b1792018-04-11 13:09:55 +09001429 if (!stat_sha1_file(the_repository, repl->hash, &st, &path))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001430 die(_("loose object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001431 oid_to_hex(repl), path);
Christian Couder68095572009-01-23 10:06:53 +01001432
brian m. carlsonb383a132018-03-12 02:27:54 +00001433 if ((p = has_packed_and_bad(repl->hash)) != NULL)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001434 die(_("packed object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001435 oid_to_hex(repl), p->pack_name);
Christian Couderf5552ae2009-01-23 10:07:01 +01001436
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001437 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001438}
1439
brian m. carlson02f05472018-03-12 02:27:52 +00001440void *read_object_with_reference(const struct object_id *oid,
Nicolas Pitre21666f12007-02-26 14:55:59 -05001441 const char *required_type_name,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001442 unsigned long *size,
brian m. carlson02f05472018-03-12 02:27:52 +00001443 struct object_id *actual_oid_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001444{
Nicolas Pitre21666f12007-02-26 14:55:59 -05001445 enum object_type type, required_type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001446 void *buffer;
1447 unsigned long isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001448 struct object_id actual_oid;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001449
Nicolas Pitre21666f12007-02-26 14:55:59 -05001450 required_type = type_from_string(required_type_name);
brian m. carlson02f05472018-03-12 02:27:52 +00001451 oidcpy(&actual_oid, oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001452 while (1) {
1453 int ref_length = -1;
1454 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001455
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001456 buffer = read_object_file(&actual_oid, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001457 if (!buffer)
1458 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001459 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001460 *size = isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001461 if (actual_oid_return)
1462 oidcpy(actual_oid_return, &actual_oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001463 return buffer;
1464 }
1465 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001466 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001467 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05001468 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001469 ref_type = "object ";
1470 else {
1471 free(buffer);
1472 return NULL;
1473 }
1474 ref_length = strlen(ref_type);
1475
brian m. carlson94b5e092018-07-16 01:28:07 +00001476 if (ref_length + the_hash_algo->hexsz > isize ||
Martin Koegler50974ec2008-02-18 21:47:52 +01001477 memcmp(buffer, ref_type, ref_length) ||
brian m. carlson02f05472018-03-12 02:27:52 +00001478 get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001479 free(buffer);
1480 return NULL;
1481 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001482 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001483 /* Now we have the ID of the referred-to object in
brian m. carlson02f05472018-03-12 02:27:52 +00001484 * actual_oid. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001485 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001486}
1487
Patryk Obaraa09c9852018-01-28 01:13:19 +01001488static void write_object_file_prepare(const void *buf, unsigned long len,
1489 const char *type, struct object_id *oid,
1490 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001491{
brian m. carlson18e25882018-02-01 02:18:41 +00001492 git_hash_ctx c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001493
1494 /* Generate the header */
Jeff Kingef1286d2015-09-24 17:06:42 -04001495 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001496
1497 /* Sha1.. */
brian m. carlson18e25882018-02-01 02:18:41 +00001498 the_hash_algo->init_fn(&c);
1499 the_hash_algo->update_fn(&c, hdr, *hdrlen);
1500 the_hash_algo->update_fn(&c, buf, len);
Junio C Hamano0fd90da2018-02-15 14:55:47 -08001501 the_hash_algo->final_fn(oid->hash, &c);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001502}
1503
Linus Torvalds230f1322005-10-08 15:54:01 -07001504/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001505 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07001506 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07001507int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001508{
Thomas Raste32c0a92008-09-19 00:24:46 +02001509 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001510
Johannes Schindelin348df162009-04-28 00:32:25 +02001511 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001512 goto try_rename;
1513 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02001514 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001515
1516 /*
1517 * Coda hack - coda doesn't like cross-directory links,
1518 * so we fall back to a rename, which will mean that it
1519 * won't be able to check collisions, but that's not a
1520 * big deal.
1521 *
1522 * The same holds for FAT formatted media.
1523 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07001524 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001525 * left to unlink.
1526 */
1527 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001528 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001529 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07001530 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001531 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001532 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001533 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07001534 if (ret) {
1535 if (ret != EEXIST) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001536 return error_errno(_("unable to write sha1 filename %s"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001537 }
1538 /* FIXME!!! Collision check here ? */
1539 }
1540
Junio C Hamano3be1f182009-03-27 23:14:39 -07001541out:
Matthieu Moy5256b002010-02-22 23:32:16 +01001542 if (adjust_shared_perm(filename))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001543 return error(_("unable to set permission to '%s'"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001544 return 0;
1545}
1546
Linus Torvalds4d548152006-05-24 08:30:54 -07001547static int write_buffer(int fd, const void *buf, size_t len)
1548{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08001549 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001550 return error_errno(_("file write error"));
Linus Torvalds4d548152006-05-24 08:30:54 -07001551 return 0;
1552}
1553
Patryk Obaraf070fac2018-01-28 01:13:13 +01001554int hash_object_file(const void *buf, unsigned long len, const char *type,
1555 struct object_id *oid)
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001556{
brian m. carlson1af64f72018-03-12 02:27:55 +00001557 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001558 int hdrlen = sizeof(hdr);
Patryk Obaraa09c9852018-01-28 01:13:19 +01001559 write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001560 return 0;
1561}
1562
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001563/* Finalize a file on disk, and close it. */
1564static void close_sha1_file(int fd)
1565{
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -07001566 if (fsync_object_files)
1567 fsync_or_die(fd, "sha1 file");
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001568 if (close(fd) != 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001569 die_errno(_("error when closing sha1 file"));
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001570}
1571
Linus Torvalds5723fe72008-06-14 10:50:12 -07001572/* Size of directory component, including the ending '/' */
1573static inline int directory_size(const char *filename)
1574{
1575 const char *s = strrchr(filename, '/');
1576 if (!s)
1577 return 0;
1578 return s - filename + 1;
1579}
1580
1581/*
1582 * This creates a temporary file in the same directory as the final
1583 * 'filename'
1584 *
1585 * We want to avoid cross-directory filename renames, because those
1586 * can have problems on various filesystems (FAT, NFS, Coda).
1587 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001588static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07001589{
1590 int fd, dirlen = directory_size(filename);
1591
Jeff Kingd4b3d112015-09-24 17:07:49 -04001592 strbuf_reset(tmp);
1593 strbuf_add(tmp, filename, dirlen);
1594 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
1595 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05001596 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04001597 /*
1598 * Make sure the directory exists; note that the contents
1599 * of the buffer are undefined after mkstemp returns an
1600 * error, so we have to rewrite the whole buffer from
1601 * scratch.
1602 */
1603 strbuf_reset(tmp);
1604 strbuf_add(tmp, filename, dirlen - 1);
1605 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01001606 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001607 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07001608 return -1;
1609
1610 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001611 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
1612 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07001613 }
1614 return fd;
1615}
1616
Patryk Obara3fc72812018-01-28 01:13:21 +01001617static int write_loose_object(const struct object_id *oid, char *hdr,
1618 int hdrlen, const void *buf, unsigned long len,
1619 time_t mtime)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001620{
Jeff King915308b2009-01-29 00:56:34 -05001621 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001622 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001623 git_zstream stream;
brian m. carlson18e25882018-02-01 02:18:41 +00001624 git_hash_ctx c;
Patryk Obara3fc72812018-01-28 01:13:21 +01001625 struct object_id parano_oid;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001626 static struct strbuf tmp_file = STRBUF_INIT;
Christian Couderea657732018-01-17 18:54:54 +01001627 static struct strbuf filename = STRBUF_INIT;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001628
Christian Couderea657732018-01-17 18:54:54 +01001629 strbuf_reset(&filename);
Junio C Hamanocf0b1792018-04-11 13:09:55 +09001630 sha1_file_name(the_repository, &filename, oid->hash);
Christian Couderea657732018-01-17 18:54:54 +01001631
1632 fd = create_tmpfile(&tmp_file, filename.buf);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001633 if (fd < 0) {
Sam Vilain35243572008-11-14 20:19:34 +13001634 if (errno == EACCES)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001635 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01001636 else
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001637 return error_errno(_("unable to create temporary file"));
Linus Torvaldsaac17942005-05-03 11:46:16 -07001638 }
1639
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001640 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001641 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001642 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001643 stream.avail_out = sizeof(compressed);
brian m. carlson18e25882018-02-01 02:18:41 +00001644 the_hash_algo->init_fn(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001645
1646 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05001647 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001648 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001649 while (git_deflate(&stream, 0) == Z_OK)
1650 ; /* nothing */
brian m. carlson18e25882018-02-01 02:18:41 +00001651 the_hash_algo->update_fn(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001652
1653 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04001654 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001655 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001656 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05001657 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001658 ret = git_deflate(&stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00001659 the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001660 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001661 die(_("unable to write sha1 file"));
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001662 stream.next_out = compressed;
1663 stream.avail_out = sizeof(compressed);
1664 } while (ret == Z_OK);
1665
Linus Torvaldsac54c272007-03-20 11:38:34 -07001666 if (ret != Z_STREAM_END)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001667 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001668 ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001669 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07001670 if (ret != Z_OK)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001671 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001672 ret);
brian m. carlson18e25882018-02-01 02:18:41 +00001673 the_hash_algo->final_fn(parano_oid.hash, &c);
Patryk Obara3fc72812018-01-28 01:13:21 +01001674 if (oidcmp(oid, &parano_oid) != 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001675 die(_("confused by unstable object source data for %s"),
Patryk Obara3fc72812018-01-28 01:13:21 +01001676 oid_to_hex(oid));
Linus Torvaldsac54c272007-03-20 11:38:34 -07001677
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001678 close_sha1_file(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001679
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001680 if (mtime) {
1681 struct utimbuf utb;
1682 utb.actime = mtime;
1683 utb.modtime = mtime;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001684 if (utime(tmp_file.buf, &utb) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001685 warning_errno(_("failed utime() on %s"), tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001686 }
1687
Christian Couderea657732018-01-17 18:54:54 +01001688 return finalize_object_file(tmp_file.buf, filename.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001689}
Daniel Barkalow8237b182005-04-23 18:47:23 -07001690
brian m. carlson6862ebb2018-05-02 00:25:34 +00001691static int freshen_loose_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001692{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001693 return check_and_freshen(oid, 1);
Jeff King33d42212014-10-15 18:42:22 -04001694}
1695
brian m. carlson6862ebb2018-05-02 00:25:34 +00001696static int freshen_packed_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001697{
1698 struct pack_entry e;
brian m. carlson544443c2018-05-02 00:25:35 +00001699 if (!find_pack_entry(the_repository, oid, &e))
Jeff Kingee1c6c32015-04-20 15:55:00 -04001700 return 0;
1701 if (e.p->freshened)
1702 return 1;
1703 if (!freshen_file(e.p->pack_name))
1704 return 0;
1705 e.p->freshened = 1;
1706 return 1;
Jeff King33d42212014-10-15 18:42:22 -04001707}
1708
Patryk Obaraa09c9852018-01-28 01:13:19 +01001709int write_object_file(const void *buf, unsigned long len, const char *type,
1710 struct object_id *oid)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001711{
brian m. carlson1af64f72018-03-12 02:27:55 +00001712 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001713 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001714
1715 /* Normally if we have it in the pack then we do not bother writing
1716 * it out into .git/objects/??/?{38} file.
1717 */
Patryk Obaraa09c9852018-01-28 01:13:19 +01001718 write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
brian m. carlson6862ebb2018-05-02 00:25:34 +00001719 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001720 return 0;
Patryk Obara3fc72812018-01-28 01:13:21 +01001721 return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001722}
1723
Patryk Obara1752cbb2018-01-28 01:13:22 +01001724int hash_object_file_literally(const void *buf, unsigned long len,
1725 const char *type, struct object_id *oid,
1726 unsigned flags)
Eric Sunshine0c3db672015-05-04 03:25:15 -04001727{
1728 char *header;
1729 int hdrlen, status = 0;
1730
1731 /* type string, SP, %lu of the length plus NUL must fit this */
brian m. carlson1af64f72018-03-12 02:27:55 +00001732 hdrlen = strlen(type) + MAX_HEADER_LEN;
Jeff Kingef1286d2015-09-24 17:06:42 -04001733 header = xmalloc(hdrlen);
Patryk Obaraa09c9852018-01-28 01:13:19 +01001734 write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04001735
1736 if (!(flags & HASH_WRITE_OBJECT))
1737 goto cleanup;
brian m. carlson6862ebb2018-05-02 00:25:34 +00001738 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Eric Sunshine0c3db672015-05-04 03:25:15 -04001739 goto cleanup;
Patryk Obara3fc72812018-01-28 01:13:21 +01001740 status = write_loose_object(oid, header, hdrlen, buf, len, 0);
Eric Sunshine0c3db672015-05-04 03:25:15 -04001741
1742cleanup:
1743 free(header);
1744 return status;
1745}
1746
Patryk Obara4bdb70a2018-01-28 01:13:20 +01001747int force_object_loose(const struct object_id *oid, time_t mtime)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001748{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001749 void *buf;
1750 unsigned long len;
1751 enum object_type type;
brian m. carlson1af64f72018-03-12 02:27:55 +00001752 char hdr[MAX_HEADER_LEN];
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001753 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02001754 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001755
brian m. carlson6862ebb2018-05-02 00:25:34 +00001756 if (has_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001757 return 0;
Patryk Obara4bdb70a2018-01-28 01:13:20 +01001758 buf = read_object(oid->hash, &type, &len);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001759 if (!buf)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001760 return error(_("cannot read sha1_file for %s"), oid_to_hex(oid));
Brandon Williamsdebca9d2018-02-14 10:59:24 -08001761 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), len) + 1;
Patryk Obara3fc72812018-01-28 01:13:21 +01001762 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02001763 free(buf);
1764
1765 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001766}
1767
Jeff King0eeb0772015-06-09 13:24:37 -04001768int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07001769{
brian m. carlson7984f232018-03-12 02:27:45 +00001770 struct object_id oid;
Jonathan Nieder3e8b7d32017-04-11 15:47:13 -07001771 if (!startup_info->have_repository)
1772 return 0;
brian m. carlson7984f232018-03-12 02:27:45 +00001773 hashcpy(oid.hash, sha1);
Stefan Beller7ecd8692018-04-25 11:20:58 -07001774 return oid_object_info_extended(the_repository, &oid, NULL,
brian m. carlsonabef9022018-03-12 02:27:46 +00001775 flags | OBJECT_INFO_SKIP_CACHED) >= 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07001776}
Junio C Hamano74400e72005-05-01 23:45:49 -07001777
brian m. carlsonb419aa22015-11-10 02:22:19 +00001778int has_object_file(const struct object_id *oid)
1779{
1780 return has_sha1_file(oid->hash);
1781}
1782
Jeff King5827a032016-10-13 12:53:44 -04001783int has_object_file_with_flags(const struct object_id *oid, int flags)
1784{
1785 return has_sha1_file_with_flags(oid->hash, flags);
1786}
1787
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07001788static void check_tree(const void *buf, size_t size)
1789{
1790 struct tree_desc desc;
1791 struct name_entry entry;
1792
1793 init_tree_desc(&desc, buf, size);
1794 while (tree_entry(&desc, &entry))
1795 /* do nothing
1796 * tree_entry() will die() on malformed entries */
1797 ;
1798}
1799
1800static void check_commit(const void *buf, size_t size)
1801{
1802 struct commit c;
1803 memset(&c, 0, sizeof(c));
Stefan Beller08f4f442018-06-28 18:22:00 -07001804 if (parse_commit_buffer(the_repository, &c, buf, size, 0))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001805 die(_("corrupt commit"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07001806}
1807
1808static void check_tag(const void *buf, size_t size)
1809{
1810 struct tag t;
1811 memset(&t, 0, sizeof(t));
Stefan Beller0e740fe2018-06-28 18:22:04 -07001812 if (parse_tag_buffer(the_repository, &t, buf, size))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001813 die(_("corrupt tag"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07001814}
1815
brian m. carlsonbcd29862017-10-15 22:07:05 +00001816static int index_mem(struct object_id *oid, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001817 enum object_type type,
1818 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02001819{
Linus Torvalds6c510be2007-02-13 11:07:23 -08001820 int ret, re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001821 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07001822
Bryan Larsen7672db22005-07-08 16:51:55 -07001823 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08001824 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08001825
1826 /*
1827 * Convert blobs to git internal format
1828 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001829 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001830 struct strbuf nbuf = STRBUF_INIT;
Brandon Williams82b474e2017-06-12 15:13:55 -07001831 if (convert_to_git(&the_index, path, buf, size, &nbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001832 get_conv_flags(flags))) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02001833 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08001834 re_allocated = 1;
1835 }
1836 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001837 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07001838 if (type == OBJ_TREE)
1839 check_tree(buf, size);
1840 if (type == OBJ_COMMIT)
1841 check_commit(buf, size);
1842 if (type == OBJ_TAG)
1843 check_tag(buf, size);
1844 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08001845
Bryan Larsen7672db22005-07-08 16:51:55 -07001846 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08001847 ret = write_object_file(buf, size, type_name(type), oid);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001848 else
Junio C Hamano169c9c02018-03-06 14:54:07 -08001849 ret = hash_object_file(buf, size, type_name(type), oid);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001850 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08001851 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001852 return ret;
1853}
1854
brian m. carlsonbcd29862017-10-15 22:07:05 +00001855static int index_stream_convert_blob(struct object_id *oid, int fd,
Steffen Prohaska9035d752014-08-26 17:23:25 +02001856 const char *path, unsigned flags)
1857{
1858 int ret;
1859 const int write_object = flags & HASH_WRITE_OBJECT;
1860 struct strbuf sbuf = STRBUF_INIT;
1861
1862 assert(path);
Nguyễn Thái Ngọc Duy7f944e22018-08-13 18:14:21 +02001863 assert(would_convert_to_git_filter_fd(&the_index, path));
Steffen Prohaska9035d752014-08-26 17:23:25 +02001864
Brandon Williamsd6c41c22017-06-12 15:13:54 -07001865 convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001866 get_conv_flags(flags));
Steffen Prohaska9035d752014-08-26 17:23:25 +02001867
1868 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08001869 ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
Patryk Obaraa09c9852018-01-28 01:13:19 +01001870 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02001871 else
Junio C Hamano169c9c02018-03-06 14:54:07 -08001872 ret = hash_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
Patryk Obaraf070fac2018-01-28 01:13:13 +01001873 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02001874 strbuf_release(&sbuf);
1875 return ret;
1876}
1877
brian m. carlsonbcd29862017-10-15 22:07:05 +00001878static int index_pipe(struct object_id *oid, int fd, enum object_type type,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001879 const char *path, unsigned flags)
1880{
1881 struct strbuf sbuf = STRBUF_INIT;
1882 int ret;
1883
1884 if (strbuf_read(&sbuf, fd, 4096) >= 0)
brian m. carlsonbcd29862017-10-15 22:07:05 +00001885 ret = index_mem(oid, sbuf.buf, sbuf.len, type, path, flags);
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001886 else
1887 ret = -1;
1888 strbuf_release(&sbuf);
1889 return ret;
1890}
1891
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001892#define SMALL_FILE_SIZE (32*1024)
1893
brian m. carlsonbcd29862017-10-15 22:07:05 +00001894static int index_core(struct object_id *oid, int fd, size_t size,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001895 enum object_type type, const char *path,
1896 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001897{
1898 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001899
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001900 if (!size) {
brian m. carlsonbcd29862017-10-15 22:07:05 +00001901 ret = index_mem(oid, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001902 } else if (size <= SMALL_FILE_SIZE) {
1903 char *buf = xmalloc(size);
Jeff King90dca672017-09-27 02:01:07 -04001904 ssize_t read_result = read_in_full(fd, buf, size);
1905 if (read_result < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001906 ret = error_errno(_("read error while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04001907 path ? path : "<unknown>");
1908 else if (read_result != size)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001909 ret = error(_("short read while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04001910 path ? path : "<unknown>");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001911 else
brian m. carlsonbcd29862017-10-15 22:07:05 +00001912 ret = index_mem(oid, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001913 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04001914 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001915 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
brian m. carlsonbcd29862017-10-15 22:07:05 +00001916 ret = index_mem(oid, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001917 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04001918 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001919 return ret;
1920}
1921
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001922/*
Junio C Hamano568508e2011-10-28 14:48:40 -07001923 * This creates one packfile per large blob unless bulk-checkin
1924 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001925 *
1926 * This also bypasses the usual "convert-to-git" dance, and that is on
1927 * purpose. We could write a streaming version of the converting
1928 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05001929 * (or equivalent in-core API described above). However, that is
1930 * somewhat complicated, as we do not know the size of the filter
1931 * result, which we need to know beforehand when writing a git object.
1932 * Since the primary motivation for trying to stream from the working
1933 * tree file and to avoid mmaping it in core is to deal with large
1934 * binary blobs, they generally do not want to get any conversion, and
1935 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001936 */
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02001937static int index_stream(struct object_id *oid, int fd, size_t size,
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001938 enum object_type type, const char *path,
1939 unsigned flags)
1940{
brian m. carlson68ee6df2018-03-12 02:27:21 +00001941 return index_bulk_checkin(oid, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001942}
1943
Patryk Obarae3506552017-08-20 22:09:29 +02001944int index_fd(struct object_id *oid, int fd, struct stat *st,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001945 enum object_type type, const char *path, unsigned flags)
1946{
1947 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001948
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001949 /*
1950 * Call xsize_t() only when needed to avoid potentially unnecessary
1951 * die() for large files.
1952 */
Nguyễn Thái Ngọc Duy7f944e22018-08-13 18:14:21 +02001953 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(&the_index, path))
brian m. carlsonbcd29862017-10-15 22:07:05 +00001954 ret = index_stream_convert_blob(oid, fd, path, flags);
Steffen Prohaska9035d752014-08-26 17:23:25 +02001955 else if (!S_ISREG(st->st_mode))
brian m. carlsonbcd29862017-10-15 22:07:05 +00001956 ret = index_pipe(oid, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001957 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Brandon Williams82b474e2017-06-12 15:13:55 -07001958 (path && would_convert_to_git(&the_index, path)))
brian m. carlsonbcd29862017-10-15 22:07:05 +00001959 ret = index_core(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001960 flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001961 else
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02001962 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001963 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001964 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001965 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07001966}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001967
Patryk Obara98e019b2017-08-20 22:09:28 +02001968int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001969{
1970 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08001971 struct strbuf sb = STRBUF_INIT;
Rene Scharfeea8e0292017-08-30 20:00:29 +02001972 int rc = 0;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001973
1974 switch (st->st_mode & S_IFMT) {
1975 case S_IFREG:
1976 fd = open(path, O_RDONLY);
1977 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001978 return error_errno("open(\"%s\")", path);
Patryk Obarae3506552017-08-20 22:09:29 +02001979 if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001980 return error(_("%s: failed to insert into database"),
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001981 path);
1982 break;
1983 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001984 if (strbuf_readlink(&sb, path, st->st_size))
1985 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001986 if (!(flags & HASH_WRITE_OBJECT))
Patryk Obaraf070fac2018-01-28 01:13:13 +01001987 hash_object_file(sb.buf, sb.len, blob_type, oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +01001988 else if (write_object_file(sb.buf, sb.len, blob_type, oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001989 rc = error(_("%s: failed to insert into database"), path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08001990 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001991 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07001992 case S_IFDIR:
brian m. carlsona98e6102017-10-15 22:07:07 +00001993 return resolve_gitlink_ref(path, "HEAD", oid);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001994 default:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001995 return error(_("%s: unsupported file type"), path);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001996 }
Rene Scharfeea8e0292017-08-30 20:00:29 +02001997 return rc;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001998}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08001999
2000int read_pack_header(int fd, struct pack_header *header)
2001{
Jeff Kingf48ecd32017-09-13 14:47:22 -04002002 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
Heikki Orsilac697ad12008-05-03 16:27:26 +03002003 /* "eof before pack header was fully read" */
2004 return PH_ERROR_EOF;
2005
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002006 if (header->hdr_signature != htonl(PACK_SIGNATURE))
2007 /* "protocol error (pack signature mismatch detected)" */
2008 return PH_ERROR_PACK_SIGNATURE;
2009 if (!pack_version_ok(header->hdr_version))
2010 /* "protocol error (pack version unsupported)" */
2011 return PH_ERROR_PROTOCOL;
2012 return 0;
2013}
Jeff King40d52ff2010-04-01 20:05:23 -04002014
brian m. carlsone816caa2018-03-12 02:27:42 +00002015void assert_oid_type(const struct object_id *oid, enum object_type expect)
Jeff King40d52ff2010-04-01 20:05:23 -04002016{
Stefan Beller0df8e962018-04-25 11:20:59 -07002017 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff King40d52ff2010-04-01 20:05:23 -04002018 if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002019 die(_("%s is not a valid object"), oid_to_hex(oid));
Jeff King40d52ff2010-04-01 20:05:23 -04002020 if (type != expect)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002021 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002022 type_name(expect));
Jeff King40d52ff2010-04-01 20:05:23 -04002023}
Jeff King27e1e222014-10-15 18:38:55 -04002024
René Scharfe70c49052017-06-24 16:09:39 +02002025int for_each_file_in_obj_subdir(unsigned int subdir_nr,
René Scharfecc817ca2017-06-22 20:19:48 +02002026 struct strbuf *path,
2027 each_loose_object_fn obj_cb,
2028 each_loose_cruft_fn cruft_cb,
2029 each_loose_subdir_fn subdir_cb,
2030 void *data)
Jeff King27e1e222014-10-15 18:38:55 -04002031{
René Scharfe0375f472017-06-24 14:12:30 +02002032 size_t origlen, baselen;
2033 DIR *dir;
Jeff King27e1e222014-10-15 18:38:55 -04002034 struct dirent *de;
2035 int r = 0;
René Scharfe62a24c82017-10-31 14:50:06 +01002036 struct object_id oid;
Jeff King27e1e222014-10-15 18:38:55 -04002037
René Scharfe70c49052017-06-24 16:09:39 +02002038 if (subdir_nr > 0xff)
2039 BUG("invalid loose object subdirectory: %x", subdir_nr);
2040
René Scharfe0375f472017-06-24 14:12:30 +02002041 origlen = path->len;
2042 strbuf_complete(path, '/');
2043 strbuf_addf(path, "%02x", subdir_nr);
René Scharfe0375f472017-06-24 14:12:30 +02002044
2045 dir = opendir(path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04002046 if (!dir) {
René Scharfe0375f472017-06-24 14:12:30 +02002047 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002048 r = error_errno(_("unable to open %s"), path->buf);
René Scharfe0375f472017-06-24 14:12:30 +02002049 strbuf_setlen(path, origlen);
2050 return r;
Jeff King27e1e222014-10-15 18:38:55 -04002051 }
2052
René Scharfe62a24c82017-10-31 14:50:06 +01002053 oid.hash[0] = subdir_nr;
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002054 strbuf_addch(path, '/');
2055 baselen = path->len;
René Scharfe62a24c82017-10-31 14:50:06 +01002056
Jeff King27e1e222014-10-15 18:38:55 -04002057 while ((de = readdir(dir))) {
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002058 size_t namelen;
Jeff King27e1e222014-10-15 18:38:55 -04002059 if (is_dot_or_dotdot(de->d_name))
2060 continue;
2061
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002062 namelen = strlen(de->d_name);
Jeff King27e1e222014-10-15 18:38:55 -04002063 strbuf_setlen(path, baselen);
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002064 strbuf_add(path, de->d_name, namelen);
brian m. carlson94b5e092018-07-16 01:28:07 +00002065 if (namelen == the_hash_algo->hexsz - 2 &&
René Scharfe62a24c82017-10-31 14:50:06 +01002066 !hex_to_bytes(oid.hash + 1, de->d_name,
brian m. carlson94b5e092018-07-16 01:28:07 +00002067 the_hash_algo->rawsz - 1)) {
René Scharfe62a24c82017-10-31 14:50:06 +01002068 if (obj_cb) {
2069 r = obj_cb(&oid, path->buf, data);
2070 if (r)
2071 break;
Jeff King27e1e222014-10-15 18:38:55 -04002072 }
René Scharfe62a24c82017-10-31 14:50:06 +01002073 continue;
Jeff King27e1e222014-10-15 18:38:55 -04002074 }
2075
2076 if (cruft_cb) {
2077 r = cruft_cb(de->d_name, path->buf, data);
2078 if (r)
2079 break;
2080 }
2081 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02002082 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04002083
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002084 strbuf_setlen(path, baselen - 1);
Jeff King27e1e222014-10-15 18:38:55 -04002085 if (!r && subdir_cb)
2086 r = subdir_cb(subdir_nr, path->buf, data);
2087
René Scharfe0375f472017-06-24 14:12:30 +02002088 strbuf_setlen(path, origlen);
2089
Jeff King27e1e222014-10-15 18:38:55 -04002090 return r;
2091}
2092
Jeff Kinge6f875e2015-02-08 20:13:22 -05002093int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04002094 each_loose_object_fn obj_cb,
2095 each_loose_cruft_fn cruft_cb,
2096 each_loose_subdir_fn subdir_cb,
2097 void *data)
2098{
Jeff King27e1e222014-10-15 18:38:55 -04002099 int r = 0;
2100 int i;
2101
Jeff King27e1e222014-10-15 18:38:55 -04002102 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05002103 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04002104 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002105 if (r)
2106 break;
2107 }
2108
Jeff Kinge6f875e2015-02-08 20:13:22 -05002109 return r;
2110}
2111
2112int for_each_loose_file_in_objdir(const char *path,
2113 each_loose_object_fn obj_cb,
2114 each_loose_cruft_fn cruft_cb,
2115 each_loose_subdir_fn subdir_cb,
2116 void *data)
2117{
2118 struct strbuf buf = STRBUF_INIT;
2119 int r;
2120
2121 strbuf_addstr(&buf, path);
2122 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
2123 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002124 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05002125
Jeff King27e1e222014-10-15 18:38:55 -04002126 return r;
2127}
Jeff King660c8892014-10-15 18:41:21 -04002128
2129struct loose_alt_odb_data {
2130 each_loose_object_fn *cb;
2131 void *data;
2132};
2133
2134static int loose_from_alt_odb(struct alternate_object_database *alt,
2135 void *vdata)
2136{
2137 struct loose_alt_odb_data *data = vdata;
Jonathon Mahb0a42642015-02-08 20:15:39 -05002138 struct strbuf buf = STRBUF_INIT;
2139 int r;
2140
Jeff King597f9132016-10-03 16:35:51 -04002141 strbuf_addstr(&buf, alt->path);
Jonathon Mahb0a42642015-02-08 20:15:39 -05002142 r = for_each_loose_file_in_objdir_buf(&buf,
2143 data->cb, NULL, NULL,
2144 data->data);
2145 strbuf_release(&buf);
2146 return r;
Jeff King660c8892014-10-15 18:41:21 -04002147}
2148
Jeff Kinga7ff6f52018-08-10 19:09:44 -04002149int for_each_loose_object(each_loose_object_fn cb, void *data,
2150 enum for_each_object_flags flags)
Jeff King660c8892014-10-15 18:41:21 -04002151{
2152 struct loose_alt_odb_data alt;
2153 int r;
2154
2155 r = for_each_loose_file_in_objdir(get_object_directory(),
2156 cb, NULL, NULL, data);
2157 if (r)
2158 return r;
2159
Jeff King1385bb72015-03-27 07:32:41 -04002160 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
2161 return 0;
2162
Jeff King660c8892014-10-15 18:41:21 -04002163 alt.cb = cb;
2164 alt.data = data;
2165 return foreach_alt_odb(loose_from_alt_odb, &alt);
2166}
2167
Jeff Kingf6371f92017-01-13 12:58:16 -05002168static int check_stream_sha1(git_zstream *stream,
2169 const char *hdr,
2170 unsigned long size,
2171 const char *path,
2172 const unsigned char *expected_sha1)
2173{
brian m. carlson18e25882018-02-01 02:18:41 +00002174 git_hash_ctx c;
brian m. carlsoncd025992017-03-26 16:01:25 +00002175 unsigned char real_sha1[GIT_MAX_RAWSZ];
Jeff Kingf6371f92017-01-13 12:58:16 -05002176 unsigned char buf[4096];
2177 unsigned long total_read;
2178 int status = Z_OK;
2179
brian m. carlson18e25882018-02-01 02:18:41 +00002180 the_hash_algo->init_fn(&c);
2181 the_hash_algo->update_fn(&c, hdr, stream->total_out);
Jeff Kingf6371f92017-01-13 12:58:16 -05002182
2183 /*
2184 * We already read some bytes into hdr, but the ones up to the NUL
2185 * do not count against the object's content size.
2186 */
2187 total_read = stream->total_out - strlen(hdr) - 1;
2188
2189 /*
2190 * This size comparison must be "<=" to read the final zlib packets;
2191 * see the comment in unpack_sha1_rest for details.
2192 */
2193 while (total_read <= size &&
2194 (status == Z_OK || status == Z_BUF_ERROR)) {
2195 stream->next_out = buf;
2196 stream->avail_out = sizeof(buf);
2197 if (size - total_read < stream->avail_out)
2198 stream->avail_out = size - total_read;
2199 status = git_inflate(stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00002200 the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
Jeff Kingf6371f92017-01-13 12:58:16 -05002201 total_read += stream->next_out - buf;
2202 }
2203 git_inflate_end(stream);
2204
2205 if (status != Z_STREAM_END) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002206 error(_("corrupt loose object '%s'"), sha1_to_hex(expected_sha1));
Jeff Kingf6371f92017-01-13 12:58:16 -05002207 return -1;
2208 }
Jeff Kingcce044d2017-01-13 13:00:25 -05002209 if (stream->avail_in) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002210 error(_("garbage at end of loose object '%s'"),
Jeff Kingcce044d2017-01-13 13:00:25 -05002211 sha1_to_hex(expected_sha1));
2212 return -1;
2213 }
Jeff Kingf6371f92017-01-13 12:58:16 -05002214
brian m. carlson18e25882018-02-01 02:18:41 +00002215 the_hash_algo->final_fn(real_sha1, &c);
Jeff Kingf6371f92017-01-13 12:58:16 -05002216 if (hashcmp(expected_sha1, real_sha1)) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002217 error(_("sha1 mismatch for %s (expected %s)"), path,
Jeff Kingf6371f92017-01-13 12:58:16 -05002218 sha1_to_hex(expected_sha1));
2219 return -1;
2220 }
2221
2222 return 0;
2223}
2224
2225int read_loose_object(const char *path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002226 const struct object_id *expected_oid,
Jeff Kingf6371f92017-01-13 12:58:16 -05002227 enum object_type *type,
2228 unsigned long *size,
2229 void **contents)
2230{
2231 int ret = -1;
Jeff Kingf6371f92017-01-13 12:58:16 -05002232 void *map = NULL;
2233 unsigned long mapsize;
2234 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00002235 char hdr[MAX_HEADER_LEN];
Jeff Kingf6371f92017-01-13 12:58:16 -05002236
2237 *contents = NULL;
2238
Stefan Beller332295d2018-03-23 18:21:13 +01002239 map = map_sha1_file_1(the_repository, path, NULL, &mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002240 if (!map) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002241 error_errno(_("unable to mmap %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002242 goto out;
2243 }
2244
2245 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002246 error(_("unable to unpack header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002247 goto out;
2248 }
2249
2250 *type = parse_sha1_header(hdr, size);
2251 if (*type < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002252 error(_("unable to parse header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002253 git_inflate_end(&stream);
2254 goto out;
2255 }
2256
Jeff King7ac4f3a2018-05-02 15:44:51 -04002257 if (*type == OBJ_BLOB && *size > big_file_threshold) {
brian m. carlsond61d87b2018-03-12 02:27:38 +00002258 if (check_stream_sha1(&stream, hdr, *size, path, expected_oid->hash) < 0)
Jeff Kingf6371f92017-01-13 12:58:16 -05002259 goto out;
2260 } else {
brian m. carlsond61d87b2018-03-12 02:27:38 +00002261 *contents = unpack_sha1_rest(&stream, hdr, *size, expected_oid->hash);
Jeff Kingf6371f92017-01-13 12:58:16 -05002262 if (!*contents) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002263 error(_("unable to unpack contents of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002264 git_inflate_end(&stream);
2265 goto out;
2266 }
brian m. carlson17e65452018-03-12 02:27:39 +00002267 if (check_object_signature(expected_oid, *contents,
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002268 *size, type_name(*type))) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002269 error(_("sha1 mismatch for %s (expected %s)"), path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002270 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002271 free(*contents);
2272 goto out;
2273 }
2274 }
2275
2276 ret = 0; /* everything checks out */
2277
2278out:
2279 if (map)
2280 munmap(map, mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002281 return ret;
2282}