blob: 5ffbf3d4fd46de4242bd1ce00c6b3c2dad17322f [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 *
Martin Ågrene5afd442020-12-31 12:56:21 +01006 * This handles basic git object files - packing, unpacking,
Linus Torvalds0fcfd162005-04-18 13:04:43 -07007 * 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"
Martin Ågrenbc626922020-12-31 12:56:23 +010023#include "hash-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"
Stefan Beller90c62152018-03-23 18:20:55 +010033#include "object-store.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020034#include "promisor-remote.h"
Jonathan Tana35e03d2021-08-16 14:09:51 -070035#include "submodule.h"
Pavel Roskine05db0f2007-01-09 23:07:11 -050036
brian m. carlson1af64f72018-03-12 02:27:55 +000037/* The maximum size for an object header. */
38#define MAX_HEADER_LEN 32
39
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000040
41#define EMPTY_TREE_SHA1_BIN_LITERAL \
42 "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
43 "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
brian m. carlson13eeedb2018-11-14 04:09:36 +000044#define EMPTY_TREE_SHA256_BIN_LITERAL \
45 "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \
46 "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \
47 "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \
48 "\x53\x21"
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000049
50#define EMPTY_BLOB_SHA1_BIN_LITERAL \
51 "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
52 "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
brian m. carlson13eeedb2018-11-14 04:09:36 +000053#define EMPTY_BLOB_SHA256_BIN_LITERAL \
54 "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \
55 "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \
56 "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \
57 "\x18\x13"
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000058
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000059static const struct object_id empty_tree_oid = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000060 .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
61 .algo = GIT_HASH_SHA1,
Jacob Keller8576fde2016-08-31 16:27:18 -070062};
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000063static const struct object_id empty_blob_oid = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000064 .hash = EMPTY_BLOB_SHA1_BIN_LITERAL,
65 .algo = GIT_HASH_SHA1,
Jacob Keller8576fde2016-08-31 16:27:18 -070066};
brian m. carlson14228442021-04-26 01:02:56 +000067static const struct object_id null_oid_sha1 = {
68 .hash = {0},
69 .algo = GIT_HASH_SHA1,
70};
brian m. carlson13eeedb2018-11-14 04:09:36 +000071static const struct object_id empty_tree_oid_sha256 = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000072 .hash = EMPTY_TREE_SHA256_BIN_LITERAL,
73 .algo = GIT_HASH_SHA256,
brian m. carlson13eeedb2018-11-14 04:09:36 +000074};
75static const struct object_id empty_blob_oid_sha256 = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000076 .hash = EMPTY_BLOB_SHA256_BIN_LITERAL,
77 .algo = GIT_HASH_SHA256,
brian m. carlson13eeedb2018-11-14 04:09:36 +000078};
brian m. carlson14228442021-04-26 01:02:56 +000079static const struct object_id null_oid_sha256 = {
80 .hash = {0},
81 .algo = GIT_HASH_SHA256,
82};
Junio C Hamano88cd6212005-09-30 14:02:47 -070083
brian m. carlsonac73ced2018-02-01 02:18:38 +000084static void git_hash_sha1_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000085{
brian m. carlsonac73ced2018-02-01 02:18:38 +000086 git_SHA1_Init(&ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000087}
88
brian m. carlson768e30e2020-02-22 20:17:27 +000089static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src)
90{
91 git_SHA1_Clone(&dst->sha1, &src->sha1);
92}
93
brian m. carlsonac73ced2018-02-01 02:18:38 +000094static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000095{
brian m. carlsonac73ced2018-02-01 02:18:38 +000096 git_SHA1_Update(&ctx->sha1, data, len);
brian m. carlsonf50e7662017-11-12 21:28:52 +000097}
98
brian m. carlsonac73ced2018-02-01 02:18:38 +000099static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000100{
brian m. carlsonac73ced2018-02-01 02:18:38 +0000101 git_SHA1_Final(hash, &ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +0000102}
103
brian m. carlsonab795f02021-04-26 01:02:52 +0000104static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
105{
106 git_SHA1_Final(oid->hash, &ctx->sha1);
107 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000108 oid->algo = GIT_HASH_SHA1;
brian m. carlsonab795f02021-04-26 01:02:52 +0000109}
110
brian m. carlson13eeedb2018-11-14 04:09:36 +0000111
112static void git_hash_sha256_init(git_hash_ctx *ctx)
113{
114 git_SHA256_Init(&ctx->sha256);
115}
116
brian m. carlson768e30e2020-02-22 20:17:27 +0000117static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src)
118{
119 git_SHA256_Clone(&dst->sha256, &src->sha256);
120}
121
brian m. carlson13eeedb2018-11-14 04:09:36 +0000122static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len)
123{
124 git_SHA256_Update(&ctx->sha256, data, len);
125}
126
127static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
128{
129 git_SHA256_Final(hash, &ctx->sha256);
130}
131
brian m. carlsonab795f02021-04-26 01:02:52 +0000132static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
133{
134 git_SHA256_Final(oid->hash, &ctx->sha256);
135 /*
136 * This currently does nothing, so the compiler should optimize it out,
137 * but keep it in case we extend the hash size again.
138 */
139 memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000140 oid->algo = GIT_HASH_SHA256;
brian m. carlsonab795f02021-04-26 01:02:52 +0000141}
142
brian m. carlsonac73ced2018-02-01 02:18:38 +0000143static void git_hash_unknown_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000144{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200145 BUG("trying to init unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000146}
147
brian m. carlson768e30e2020-02-22 20:17:27 +0000148static void git_hash_unknown_clone(git_hash_ctx *dst, const git_hash_ctx *src)
149{
150 BUG("trying to clone unknown hash");
151}
152
brian m. carlsonac73ced2018-02-01 02:18:38 +0000153static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000154{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200155 BUG("trying to update unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000156}
157
brian m. carlsonac73ced2018-02-01 02:18:38 +0000158static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000159{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200160 BUG("trying to finalize unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000161}
162
brian m. carlsonab795f02021-04-26 01:02:52 +0000163static void git_hash_unknown_final_oid(struct object_id *oid, git_hash_ctx *ctx)
164{
165 BUG("trying to finalize unknown hash");
166}
167
brian m. carlsonf50e7662017-11-12 21:28:52 +0000168const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
169 {
Ævar Arnfjörð Bjarmason0cb98722022-02-24 10:33:01 +0100170 .name = NULL,
171 .format_id = 0x00000000,
172 .rawsz = 0,
173 .hexsz = 0,
174 .blksz = 0,
175 .init_fn = git_hash_unknown_init,
176 .clone_fn = git_hash_unknown_clone,
177 .update_fn = git_hash_unknown_update,
178 .final_fn = git_hash_unknown_final,
179 .final_oid_fn = git_hash_unknown_final_oid,
180 .empty_tree = NULL,
181 .empty_blob = NULL,
182 .null_oid = NULL,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000183 },
184 {
Ævar Arnfjörð Bjarmason0cb98722022-02-24 10:33:01 +0100185 .name = "sha1",
186 .format_id = GIT_SHA1_FORMAT_ID,
187 .rawsz = GIT_SHA1_RAWSZ,
188 .hexsz = GIT_SHA1_HEXSZ,
189 .blksz = GIT_SHA1_BLKSZ,
190 .init_fn = git_hash_sha1_init,
191 .clone_fn = git_hash_sha1_clone,
192 .update_fn = git_hash_sha1_update,
193 .final_fn = git_hash_sha1_final,
194 .final_oid_fn = git_hash_sha1_final_oid,
195 .empty_tree = &empty_tree_oid,
196 .empty_blob = &empty_blob_oid,
197 .null_oid = &null_oid_sha1,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000198 },
brian m. carlson13eeedb2018-11-14 04:09:36 +0000199 {
Ævar Arnfjörð Bjarmason0cb98722022-02-24 10:33:01 +0100200 .name = "sha256",
201 .format_id = GIT_SHA256_FORMAT_ID,
202 .rawsz = GIT_SHA256_RAWSZ,
203 .hexsz = GIT_SHA256_HEXSZ,
204 .blksz = GIT_SHA256_BLKSZ,
205 .init_fn = git_hash_sha256_init,
206 .clone_fn = git_hash_sha256_clone,
207 .update_fn = git_hash_sha256_update,
208 .final_fn = git_hash_sha256_final,
209 .final_oid_fn = git_hash_sha256_final_oid,
210 .empty_tree = &empty_tree_oid_sha256,
211 .empty_blob = &empty_blob_oid_sha256,
212 .null_oid = &null_oid_sha256,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000213 }
brian m. carlsonf50e7662017-11-12 21:28:52 +0000214};
215
brian m. carlson14228442021-04-26 01:02:56 +0000216const struct object_id *null_oid(void)
217{
218 return the_hash_algo->null_oid;
219}
220
brian m. carlsond8a92ce2018-05-02 00:25:54 +0000221const char *empty_tree_oid_hex(void)
222{
223 static char buf[GIT_MAX_HEXSZ + 1];
224 return oid_to_hex_r(buf, the_hash_algo->empty_tree);
225}
226
227const char *empty_blob_oid_hex(void)
228{
229 static char buf[GIT_MAX_HEXSZ + 1];
230 return oid_to_hex_r(buf, the_hash_algo->empty_blob);
231}
232
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000233int hash_algo_by_name(const char *name)
234{
235 int i;
236 if (!name)
237 return GIT_HASH_UNKNOWN;
238 for (i = 1; i < GIT_HASH_NALGOS; i++)
239 if (!strcmp(name, hash_algos[i].name))
240 return i;
241 return GIT_HASH_UNKNOWN;
242}
243
244int hash_algo_by_id(uint32_t format_id)
245{
246 int i;
247 for (i = 1; i < GIT_HASH_NALGOS; i++)
248 if (format_id == hash_algos[i].format_id)
249 return i;
250 return GIT_HASH_UNKNOWN;
251}
252
brian m. carlson95399782019-02-19 00:05:17 +0000253int hash_algo_by_length(int len)
254{
255 int i;
256 for (i = 1; i < GIT_HASH_NALGOS; i++)
257 if (len == hash_algos[i].rawsz)
258 return i;
259 return GIT_HASH_UNKNOWN;
260}
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000261
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700262/*
263 * This is meant to hold a *small* number of objects that you would
Jeff Kingcb1c8d12019-01-07 03:33:52 -0500264 * want read_object_file() to be able to return, but yet you do not want
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700265 * to write them into the object store (e.g. a browse-only
266 * application).
267 */
268static struct cached_object {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000269 struct object_id oid;
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700270 enum object_type type;
271 void *buf;
272 unsigned long size;
273} *cached_objects;
274static int cached_object_nr, cached_object_alloc;
275
276static struct cached_object empty_tree = {
Ævar Arnfjörð Bjarmason50103642022-03-17 18:27:17 +0100277 .oid = {
278 .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
279 },
280 .type = OBJ_TREE,
281 .buf = "",
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700282};
283
brian m. carlson62ba93e2018-05-02 00:26:03 +0000284static struct cached_object *find_cached_object(const struct object_id *oid)
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700285{
286 int i;
287 struct cached_object *co = cached_objects;
288
289 for (i = 0; i < cached_object_nr; i++, co++) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400290 if (oideq(&co->oid, oid))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700291 return co;
292 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400293 if (oideq(oid, the_hash_algo->empty_tree))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700294 return &empty_tree;
295 return NULL;
296}
297
Torsten Bögershausen94729352017-11-16 17:38:28 +0100298
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100299static int get_conv_flags(unsigned flags)
Torsten Bögershausen94729352017-11-16 17:38:28 +0100300{
301 if (flags & HASH_RENORMALIZE)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100302 return CONV_EOL_RENORMALIZE;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100303 else if (flags & HASH_WRITE_OBJECT)
Lars Schneider107642f2018-04-15 20:16:07 +0200304 return global_conv_flags_eol | CONV_WRITE_OBJECT;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100305 else
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100306 return 0;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100307}
308
309
Junio C Hamano90a64642011-03-10 16:02:50 -0800310int mkdir_in_gitdir(const char *path)
311{
312 if (mkdir(path, 0777)) {
313 int saved_errno = errno;
314 struct stat st;
315 struct strbuf sb = STRBUF_INIT;
316
317 if (errno != EEXIST)
318 return -1;
319 /*
320 * Are we looking at a path in a symlinked worktree
321 * whose original repository does not yet have it?
322 * e.g. .git/rr-cache pointing at its original
323 * repository in which the user hasn't performed any
324 * conflict resolution yet?
325 */
326 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
327 strbuf_readlink(&sb, path, st.st_size) ||
328 !is_absolute_path(sb.buf) ||
329 mkdir(sb.buf, 0777)) {
330 strbuf_release(&sb);
331 errno = saved_errno;
332 return -1;
333 }
334 strbuf_release(&sb);
335 }
336 return adjust_shared_perm(path);
337}
338
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300339static enum scld_error safe_create_leading_directories_1(char *path, int share)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700340{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100341 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100342 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800343
Michael Haggerty0be05212014-01-06 14:45:25 +0100344 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100345 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100346 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100347
Michael Haggerty0f527402014-01-19 00:40:44 +0100348 while (*slash && !is_dir_sep(*slash))
349 slash++;
350
351 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700352 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100353
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100354 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100355 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100356 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100357 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700358 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100359
Michael Haggerty0f527402014-01-19 00:40:44 +0100360 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100361 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800362 if (!stat(path, &st)) {
363 /* path exists */
Michael Haggerty204a0472017-01-06 17:22:25 +0100364 if (!S_ISDIR(st.st_mode)) {
365 errno = ENOTDIR;
Michael Haggerty0be05212014-01-06 14:45:25 +0100366 ret = SCLD_EXISTS;
Michael Haggerty204a0472017-01-06 17:22:25 +0100367 }
Michael Haggerty53a39722014-01-06 14:45:19 +0100368 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400369 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100370 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400371 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100372 else if (errno == ENOENT)
373 /*
374 * Either mkdir() failed because
375 * somebody just pruned the containing
376 * directory, or stat() failed because
377 * the file that was in our way was
378 * just removed. Either way, inform
379 * the caller that it might be worth
380 * trying again:
381 */
382 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100383 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100384 ret = SCLD_FAILED;
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300385 } else if (share && adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100386 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800387 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100388 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700389 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100390 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700391}
392
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300393enum scld_error safe_create_leading_directories(char *path)
394{
395 return safe_create_leading_directories_1(path, 1);
396}
397
398enum scld_error safe_create_leading_directories_no_share(char *path)
399{
400 return safe_create_leading_directories_1(path, 0);
401}
402
Michael Haggerty0be05212014-01-06 14:45:25 +0100403enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400404{
Michael Haggerty02944302017-01-06 17:22:24 +0100405 int save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400406 /* path points to cache entries, so xstrdup before messing with it */
407 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100408 enum scld_error result = safe_create_leading_directories(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100409
410 save_errno = errno;
Jeff King8e21d632008-06-25 01:41:34 -0400411 free(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100412 errno = save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400413 return result;
414}
415
Jeff King514c5fd2019-01-07 03:35:42 -0500416static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
Junio C Hamanoace15342005-05-07 00:38:04 -0700417{
418 int i;
brian m. carlson94b5e092018-07-16 01:28:07 +0000419 for (i = 0; i < the_hash_algo->rawsz; i++) {
Junio C Hamanoace15342005-05-07 00:38:04 -0700420 static char hex[] = "0123456789abcdef";
Jeff King514c5fd2019-01-07 03:35:42 -0500421 unsigned int val = oid->hash[i];
Jeff Kingf7b77742016-10-03 16:36:09 -0400422 strbuf_addch(buf, hex[val >> 4]);
423 strbuf_addch(buf, hex[val & 0xf]);
Jeff Kingafbba2f2016-10-03 16:35:55 -0400424 if (!i)
Jeff Kingf7b77742016-10-03 16:36:09 -0400425 strbuf_addch(buf, '/');
Junio C Hamanoace15342005-05-07 00:38:04 -0700426 }
427}
428
Jeff Kingf0eaf632018-11-12 09:50:39 -0500429static const char *odb_loose_path(struct object_directory *odb,
430 struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500431 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700432{
Jeff Kingb69fb862018-11-12 09:48:56 -0500433 strbuf_reset(buf);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500434 strbuf_addstr(buf, odb->path);
Christian Couder34498472018-01-18 11:08:54 +0100435 strbuf_addch(buf, '/');
Jeff King514c5fd2019-01-07 03:35:42 -0500436 fill_loose_path(buf, oid);
Jeff King38dbe5f2016-10-03 16:36:04 -0400437 return buf->buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700438}
439
Jeff Kingf3f043a2018-11-12 09:49:35 -0500440const char *loose_object_path(struct repository *r, struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500441 const struct object_id *oid)
Jeff Kingf3f043a2018-11-12 09:49:35 -0500442{
Jeff King514c5fd2019-01-07 03:35:42 -0500443 return odb_loose_path(r->objects->odb, buf, oid);
Jeff Kingf3f043a2018-11-12 09:49:35 -0500444}
445
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700446/*
Jeff King4ea82472016-10-03 16:34:48 -0400447 * Return non-zero iff the path is usable as an alternate object database.
448 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100449static int alt_odb_usable(struct raw_object_store *o,
450 struct strbuf *path,
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000451 const char *normalized_objdir, khiter_t *pos)
Jeff King4ea82472016-10-03 16:34:48 -0400452{
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000453 int r;
Jeff King4ea82472016-10-03 16:34:48 -0400454
455 /* Detect cases where alternate disappeared */
456 if (!is_directory(path->buf)) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200457 error(_("object directory %s does not exist; "
458 "check .git/objects/info/alternates"),
Jeff King4ea82472016-10-03 16:34:48 -0400459 path->buf);
460 return 0;
461 }
462
463 /*
464 * Prevent the common mistake of listing the same
465 * thing twice, or object directory itself.
466 */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000467 if (!o->odb_by_path) {
468 khiter_t p;
Jeff King4ea82472016-10-03 16:34:48 -0400469
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000470 o->odb_by_path = kh_init_odb_path_map();
471 assert(!o->odb->next);
472 p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r);
473 assert(r == 1); /* never used */
474 kh_value(o->odb_by_path, p) = o->odb;
475 }
476 if (fspatheq(path->buf, normalized_objdir))
477 return 0;
478 *pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r);
479 /* r: 0 = exists, 1 = never used, 2 = deleted */
480 return r == 0 ? 0 : 1;
Jeff King4ea82472016-10-03 16:34:48 -0400481}
482
483/*
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700484 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700485 *
486 * The variable alt_odb_list points at the list of struct
Jeff King263db402018-11-12 09:48:47 -0500487 * object_directory. The elements on this list come from
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700488 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
489 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800490 * whose contents is similar to that environment variable but can be
491 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700492 * contains "/the/directory/corresponding/to/.git/objects/...", while
493 * its name points just after the slash at the end of ".git/objects/"
Martin Ågrene5afd442020-12-31 12:56:21 +0100494 * in the example above, and has enough space to hold all hex characters
495 * of the object ID, an extra slash for the first level indirection, and
496 * the terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700497 */
Stefan Beller77f012e2018-03-23 18:21:08 +0100498static void read_info_alternates(struct repository *r,
499 const char *relative_base,
500 int depth);
Eric Wong407532f2021-07-07 23:10:16 +0000501static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100502 const char *relative_base, int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200503{
Jeff King263db402018-11-12 09:48:47 -0500504 struct object_directory *ent;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800505 struct strbuf pathbuf = STRBUF_INIT;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000506 khiter_t pos;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200507
Eric Wong407532f2021-07-07 23:10:16 +0000508 if (!is_absolute_path(entry->buf) && relative_base) {
Brandon Williams4ac90062016-12-12 10:16:55 -0800509 strbuf_realpath(&pathbuf, relative_base, 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800510 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200511 }
Eric Wong407532f2021-07-07 23:10:16 +0000512 strbuf_addbuf(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800513
Jeff King37a95862016-11-07 23:50:17 -0500514 if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200515 error(_("unable to normalize alternate object path: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400516 pathbuf.buf);
517 strbuf_release(&pathbuf);
518 return -1;
519 }
Hui Wang5bdf0a82011-09-07 18:37:47 +0800520
521 /*
522 * The trailing slash after the directory name is given by
523 * this function at the end. Remove duplicates.
524 */
Jeff King4ea82472016-10-03 16:34:48 -0400525 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
526 strbuf_setlen(&pathbuf, pathbuf.len - 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800527
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000528 if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) {
Jeff King4ea82472016-10-03 16:34:48 -0400529 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200530 return -1;
531 }
532
René Scharfeca56dad2021-03-13 17:17:22 +0100533 CALLOC_ARRAY(ent, 1);
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000534 /* pathbuf.buf is already in r->objects->odb_by_path */
535 ent->path = strbuf_detach(&pathbuf, NULL);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200536
537 /* add the alternate entry */
Jeff Kingf0eaf632018-11-12 09:50:39 -0500538 *r->objects->odb_tail = ent;
539 r->objects->odb_tail = &(ent->next);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200540 ent->next = NULL;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000541 assert(r->objects->odb_by_path);
542 kh_value(r->objects->odb_by_path, pos) = ent;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200543
544 /* recursively add alternates */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000545 read_info_alternates(r, ent->path, depth + 1);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200546
Martin Waitzc2f493a2006-05-07 20:19:21 +0200547 return 0;
548}
549
Jeff Kingcf3c6352016-12-12 14:52:22 -0500550static const char *parse_alt_odb_entry(const char *string,
551 int sep,
552 struct strbuf *out)
553{
554 const char *end;
555
556 strbuf_reset(out);
557
558 if (*string == '#') {
559 /* comment; consume up to next separator */
560 end = strchrnul(string, sep);
561 } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
562 /*
563 * quoted path; unquote_c_style has copied the
564 * data for us and set "end". Broken quoting (e.g.,
565 * an entry that doesn't end with a quote) falls
566 * back to the unquoted case below.
567 */
568 } else {
569 /* normal, unquoted path */
570 end = strchrnul(string, sep);
571 strbuf_add(out, string, end - string);
572 }
573
574 if (*end)
575 end++;
576 return end;
577}
578
Stefan Beller77f012e2018-03-23 18:21:08 +0100579static void link_alt_odb_entries(struct repository *r, const char *alt,
580 int sep, const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700581{
Ephrim Khong539e7502014-07-15 13:29:45 +0200582 struct strbuf objdirbuf = STRBUF_INIT;
Jeff Kingcf3c6352016-12-12 14:52:22 -0500583 struct strbuf entry = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200584
Jeff Kingf28e3662017-11-12 10:27:39 +0000585 if (!alt || !*alt)
586 return;
587
Martin Waitzc2f493a2006-05-07 20:19:21 +0200588 if (depth > 5) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200589 error(_("%s: ignoring alternate object stores, nesting too deep"),
Martin Waitzc2f493a2006-05-07 20:19:21 +0200590 relative_base);
591 return;
592 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700593
Jeff Kingf0eaf632018-11-12 09:50:39 -0500594 strbuf_add_absolute_path(&objdirbuf, r->objects->odb->path);
Jeff King670c3592016-10-03 16:34:17 -0400595 if (strbuf_normalize_path(&objdirbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200596 die(_("unable to normalize object directory: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400597 objdirbuf.buf);
Ephrim Khong539e7502014-07-15 13:29:45 +0200598
Jeff Kingcf3c6352016-12-12 14:52:22 -0500599 while (*alt) {
600 alt = parse_alt_odb_entry(alt, sep, &entry);
601 if (!entry.len)
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700602 continue;
Eric Wong407532f2021-07-07 23:10:16 +0000603 link_alt_odb_entry(r, &entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100604 relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700605 }
Jeff Kingcf3c6352016-12-12 14:52:22 -0500606 strbuf_release(&entry);
Ephrim Khong539e7502014-07-15 13:29:45 +0200607 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700608}
609
Stefan Beller77f012e2018-03-23 18:21:08 +0100610static void read_info_alternates(struct repository *r,
611 const char *relative_base,
612 int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700613{
Jeff King5015f012015-08-19 14:12:45 -0400614 char *path;
Jeff Kingdc732bd2017-09-19 15:41:07 -0400615 struct strbuf buf = STRBUF_INIT;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700616
Jeff King5015f012015-08-19 14:12:45 -0400617 path = xstrfmt("%s/info/alternates", relative_base);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400618 if (strbuf_read_file(&buf, path, 1024) < 0) {
Jeff Kingf0f7beb2017-09-19 15:41:10 -0400619 warn_on_fopen_errors(path);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400620 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700621 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700622 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700623
Stefan Beller77f012e2018-03-23 18:21:08 +0100624 link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400625 strbuf_release(&buf);
626 free(path);
Junio C Hamanoace15342005-05-07 00:38:04 -0700627}
628
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400629void add_to_alternates_file(const char *reference)
630{
Martin Ågrenf132a122017-10-05 22:32:03 +0200631 struct lock_file lock = LOCK_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400632 char *alts = git_pathdup("objects/info/alternates");
633 FILE *in, *out;
Martin Ågrenf132a122017-10-05 22:32:03 +0200634 int found = 0;
Jeff King77b9b1d2015-08-10 05:34:46 -0400635
Martin Ågrenf132a122017-10-05 22:32:03 +0200636 hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
637 out = fdopen_lock_file(&lock, "w");
Jeff King77b9b1d2015-08-10 05:34:46 -0400638 if (!out)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200639 die_errno(_("unable to fdopen alternates lockfile"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400640
641 in = fopen(alts, "r");
642 if (in) {
643 struct strbuf line = STRBUF_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400644
Junio C Hamano3f163962015-10-28 13:29:24 -0700645 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400646 if (!strcmp(reference, line.buf)) {
647 found = 1;
648 break;
649 }
650 fprintf_or_die(out, "%s\n", line.buf);
651 }
652
653 strbuf_release(&line);
654 fclose(in);
Jeff King77b9b1d2015-08-10 05:34:46 -0400655 }
656 else if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200657 die_errno(_("unable to read alternates file"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400658
Martin Ågrenf132a122017-10-05 22:32:03 +0200659 if (found) {
660 rollback_lock_file(&lock);
661 } else {
Jeff King77b9b1d2015-08-10 05:34:46 -0400662 fprintf_or_die(out, "%s\n", reference);
Martin Ågrenf132a122017-10-05 22:32:03 +0200663 if (commit_lock_file(&lock))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200664 die_errno(_("unable to move new alternates file into place"));
Jeff Kingf0eaf632018-11-12 09:50:39 -0500665 if (the_repository->objects->loaded_alternates)
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100666 link_alt_odb_entries(the_repository, reference,
667 '\n', NULL, 0);
Jeff King77b9b1d2015-08-10 05:34:46 -0400668 }
669 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400670}
671
Jeff Kinga5b34d22016-10-03 16:35:03 -0400672void add_to_alternates_memory(const char *reference)
673{
674 /*
675 * Make sure alternates are initialized, or else our entry may be
676 * overwritten when they are.
677 */
Stefan Beller0b209032018-03-23 18:21:07 +0100678 prepare_alt_odb(the_repository);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400679
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100680 link_alt_odb_entries(the_repository, reference,
681 '\n', NULL, 0);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400682}
683
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000684struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy)
685{
686 struct object_directory *new_odb;
687
688 /*
689 * Make sure alternates are initialized, or else our entry may be
690 * overwritten when they are.
691 */
692 prepare_alt_odb(the_repository);
693
694 /*
695 * Make a new primary odb and link the old primary ODB in as an
696 * alternate
697 */
698 new_odb = xcalloc(1, sizeof(*new_odb));
699 new_odb->path = xstrdup(dir);
Neeraj Singhecd81df2021-12-06 22:05:05 +0000700
701 /*
702 * Disable ref updates while a temporary odb is active, since
703 * the objects in the database may roll back.
704 */
705 new_odb->disable_ref_updates = 1;
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000706 new_odb->will_destroy = will_destroy;
707 new_odb->next = the_repository->objects->odb;
708 the_repository->objects->odb = new_odb;
709 return new_odb->next;
710}
711
712void restore_primary_odb(struct object_directory *restore_odb, const char *old_path)
713{
714 struct object_directory *cur_odb = the_repository->objects->odb;
715
716 if (strcmp(old_path, cur_odb->path))
717 BUG("expected %s as primary object store; found %s",
718 old_path, cur_odb->path);
719
720 if (cur_odb->next != restore_odb)
721 BUG("we expect the old primary object store to be the first alternate");
722
723 the_repository->objects->odb = restore_odb;
724 free_object_directory(cur_odb);
725}
726
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700727/*
728 * Compute the exact path an alternate is at and returns it. In case of
729 * error NULL is returned and the human readable error is added to `err`
Robert P. J. Dayefde7b72018-06-03 10:32:50 -0400730 * `path` may be relative and should point to $GIT_DIR.
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700731 * `err` must not be null.
732 */
733char *compute_alternate_path(const char *path, struct strbuf *err)
734{
735 char *ref_git = NULL;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000736 const char *repo;
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700737 int seen_error = 0;
738
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000739 ref_git = real_pathdup(path, 0);
740 if (!ref_git) {
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700741 seen_error = 1;
742 strbuf_addf(err, _("path '%s' does not exist"), path);
743 goto out;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000744 }
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700745
746 repo = read_gitfile(ref_git);
747 if (!repo)
748 repo = read_gitfile(mkpath("%s/.git", ref_git));
749 if (repo) {
750 free(ref_git);
751 ref_git = xstrdup(repo);
752 }
753
754 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
755 char *ref_git_git = mkpathdup("%s/.git", ref_git);
756 free(ref_git);
757 ref_git = ref_git_git;
758 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
759 struct strbuf sb = STRBUF_INIT;
760 seen_error = 1;
761 if (get_common_dir(&sb, ref_git)) {
762 strbuf_addf(err,
763 _("reference repository '%s' as a linked "
764 "checkout is not supported yet."),
765 path);
766 goto out;
767 }
768
769 strbuf_addf(err, _("reference repository '%s' is not a "
770 "local repository."), path);
771 goto out;
772 }
773
774 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
775 strbuf_addf(err, _("reference repository '%s' is shallow"),
776 path);
777 seen_error = 1;
778 goto out;
779 }
780
781 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
782 strbuf_addf(err,
783 _("reference repository '%s' is grafted"),
784 path);
785 seen_error = 1;
786 goto out;
787 }
788
789out:
790 if (seen_error) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000791 FREE_AND_NULL(ref_git);
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700792 }
793
794 return ref_git;
795}
796
Taylor Blauf57a7392021-09-01 16:34:01 -0400797struct object_directory *find_odb(struct repository *r, const char *obj_dir)
798{
799 struct object_directory *odb;
800 char *obj_dir_real = real_pathdup(obj_dir, 1);
801 struct strbuf odb_path_real = STRBUF_INIT;
802
803 prepare_alt_odb(r);
804 for (odb = r->objects->odb; odb; odb = odb->next) {
805 strbuf_realpath(&odb_path_real, odb->path, 1);
806 if (!strcmp(obj_dir_real, odb_path_real.buf))
807 break;
808 }
809
810 free(obj_dir_real);
811 strbuf_release(&odb_path_real);
812
813 if (!odb)
814 die(_("could not find object directory matching %s"), obj_dir);
815 return odb;
816}
817
Jeff King709dfa62019-07-01 09:17:40 -0400818static void fill_alternate_refs_command(struct child_process *cmd,
819 const char *repo_path)
820{
821 const char *value;
822
823 if (!git_config_get_value("core.alternateRefsCommand", &value)) {
824 cmd->use_shell = 1;
825
Jeff Kingc972bf42020-07-28 16:25:12 -0400826 strvec_push(&cmd->args, value);
827 strvec_push(&cmd->args, repo_path);
Jeff King709dfa62019-07-01 09:17:40 -0400828 } else {
829 cmd->git_cmd = 1;
830
Jeff Kingc972bf42020-07-28 16:25:12 -0400831 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
832 strvec_push(&cmd->args, "for-each-ref");
833 strvec_push(&cmd->args, "--format=%(objectname)");
Jeff King709dfa62019-07-01 09:17:40 -0400834
835 if (!git_config_get_value("core.alternateRefsPrefixes", &value)) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400836 strvec_push(&cmd->args, "--");
837 strvec_split(&cmd->args, value);
Jeff King709dfa62019-07-01 09:17:40 -0400838 }
839 }
840
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100841 strvec_pushv(&cmd->env_array, (const char **)local_repo_env);
Jeff King709dfa62019-07-01 09:17:40 -0400842 cmd->out = -1;
843}
844
845static void read_alternate_refs(const char *path,
846 alternate_ref_fn *cb,
847 void *data)
848{
849 struct child_process cmd = CHILD_PROCESS_INIT;
850 struct strbuf line = STRBUF_INIT;
851 FILE *fh;
852
853 fill_alternate_refs_command(&cmd, path);
854
855 if (start_command(&cmd))
856 return;
857
858 fh = xfdopen(cmd.out, "r");
859 while (strbuf_getline_lf(&line, fh) != EOF) {
860 struct object_id oid;
861 const char *p;
862
863 if (parse_oid_hex(line.buf, &oid, &p) || *p) {
864 warning(_("invalid line while parsing alternate refs: %s"),
865 line.buf);
866 break;
867 }
868
869 cb(&oid, data);
870 }
871
872 fclose(fh);
873 finish_command(&cmd);
René Scharfe86ad3ea2019-08-07 13:15:25 +0200874 strbuf_release(&line);
Jeff King709dfa62019-07-01 09:17:40 -0400875}
876
877struct alternate_refs_data {
878 alternate_ref_fn *fn;
879 void *data;
880};
881
882static int refs_from_alternate_cb(struct object_directory *e,
883 void *data)
884{
885 struct strbuf path = STRBUF_INIT;
886 size_t base_len;
887 struct alternate_refs_data *cb = data;
888
889 if (!strbuf_realpath(&path, e->path, 0))
890 goto out;
891 if (!strbuf_strip_suffix(&path, "/objects"))
892 goto out;
893 base_len = path.len;
894
895 /* Is this a git repository with refs? */
896 strbuf_addstr(&path, "/refs");
897 if (!is_directory(path.buf))
898 goto out;
899 strbuf_setlen(&path, base_len);
900
901 read_alternate_refs(path.buf, cb->fn, cb->data);
902
903out:
904 strbuf_release(&path);
905 return 0;
906}
907
908void for_each_alternate_ref(alternate_ref_fn fn, void *data)
909{
910 struct alternate_refs_data cb;
911 cb.fn = fn;
912 cb.data = data;
913 foreach_alt_odb(refs_from_alternate_cb, &cb);
914}
915
Jeff Kingfe1b2262014-10-15 18:33:13 -0400916int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700917{
Jeff King263db402018-11-12 09:48:47 -0500918 struct object_directory *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400919 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700920
Stefan Beller0b209032018-03-23 18:21:07 +0100921 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500922 for (ent = the_repository->objects->odb->next; ent; ent = ent->next) {
Jeff Kingfe1b2262014-10-15 18:33:13 -0400923 r = fn(ent, cb);
924 if (r)
925 break;
926 }
927 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700928}
929
Stefan Beller13068bf2018-03-23 18:21:09 +0100930void prepare_alt_odb(struct repository *r)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200931{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500932 if (r->objects->loaded_alternates)
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400933 return;
934
Stefan Beller13068bf2018-03-23 18:21:09 +0100935 link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200936
Jeff Kingf0eaf632018-11-12 09:50:39 -0500937 read_info_alternates(r, r->objects->odb->path, 0);
938 r->objects->loaded_alternates = 1;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200939}
940
Jeff King3096b2e2015-07-08 16:33:52 -0400941/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400942static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700943{
luciano.rocha@booking.com312cd762020-04-14 16:27:26 +0200944 return !utime(fn, NULL);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600945}
Junio C Hamanoace15342005-05-07 00:38:04 -0700946
Jeff King3096b2e2015-07-08 16:33:52 -0400947/*
948 * All of the check_and_freshen functions return 1 if the file exists and was
949 * freshened (if freshening was requested), 0 otherwise. If they return
950 * 0, you should not assume that it is safe to skip a write of the object (it
951 * either does not exist on disk, or has a stale mtime and may be subject to
952 * pruning).
953 */
Christian Couder6a5e6f52017-02-27 19:00:11 +0100954int check_and_freshen_file(const char *fn, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400955{
956 if (access(fn, F_OK))
957 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400958 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400959 return 0;
960 return 1;
961}
962
Jeff Kingf0eaf632018-11-12 09:50:39 -0500963static int check_and_freshen_odb(struct object_directory *odb,
964 const struct object_id *oid,
965 int freshen)
966{
967 static struct strbuf path = STRBUF_INIT;
Jeff King514c5fd2019-01-07 03:35:42 -0500968 odb_loose_path(odb, &path, oid);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500969 return check_and_freshen_file(path.buf, freshen);
970}
971
brian m. carlson6862ebb2018-05-02 00:25:34 +0000972static int check_and_freshen_local(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400973{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500974 return check_and_freshen_odb(the_repository->objects->odb, oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400975}
976
brian m. carlson6862ebb2018-05-02 00:25:34 +0000977static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600978{
Jeff King263db402018-11-12 09:48:47 -0500979 struct object_directory *odb;
Jeff Kingf3f043a2018-11-12 09:49:35 -0500980
Stefan Beller0b209032018-03-23 18:21:07 +0100981 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500982 for (odb = the_repository->objects->odb->next; odb; odb = odb->next) {
983 if (check_and_freshen_odb(odb, oid, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700984 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700985 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700986 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700987}
988
brian m. carlson6862ebb2018-05-02 00:25:34 +0000989static int check_and_freshen(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400990{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000991 return check_and_freshen_local(oid, freshen) ||
992 check_and_freshen_nonlocal(oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400993}
994
brian m. carlson6862ebb2018-05-02 00:25:34 +0000995int has_loose_object_nonlocal(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -0400996{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000997 return check_and_freshen_nonlocal(oid, 0);
Jeff King33d42212014-10-15 18:42:22 -0400998}
999
brian m. carlson6862ebb2018-05-02 00:25:34 +00001000static int has_loose_object(const struct object_id *oid)
Brandon Casey0f4dc142008-11-09 23:59:57 -06001001{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001002 return check_and_freshen(oid, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -06001003}
1004
Steffen Prohaska02710222014-08-26 17:23:23 +02001005static void mmap_limit_check(size_t length)
1006{
1007 static size_t limit = 0;
1008 if (!limit) {
1009 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
1010 if (!limit)
1011 limit = SIZE_MAX;
1012 }
1013 if (length > limit)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001014 die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
Steffen Prohaska02710222014-08-26 17:23:23 +02001015 (uintmax_t)length, (uintmax_t)limit);
1016}
1017
Jeff King15708562015-05-28 03:56:15 -04001018void *xmmap_gently(void *start, size_t length,
1019 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001020{
Steffen Prohaska02710222014-08-26 17:23:23 +02001021 void *ret;
1022
1023 mmap_limit_check(length);
1024 ret = mmap(start, length, prot, flags, fd, offset);
Jeff King9827d4c2019-08-12 16:50:21 -04001025 if (ret == MAP_FAILED && !length)
1026 ret = NULL;
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001027 return ret;
1028}
1029
Eric Wongdc059292021-06-30 00:01:32 +00001030const char *mmap_os_err(void)
1031{
1032 static const char blank[] = "";
1033#if defined(__linux__)
1034 if (errno == ENOMEM) {
1035 /* this continues an existing error message: */
1036 static const char enomem[] =
1037", check sys.vm.max_map_count and/or RLIMIT_DATA";
1038 return enomem;
1039 }
1040#endif /* OS-specific bits */
1041 return blank;
1042}
1043
Jeff King15708562015-05-28 03:56:15 -04001044void *xmmap(void *start, size_t length,
1045 int prot, int flags, int fd, off_t offset)
1046{
1047 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
1048 if (ret == MAP_FAILED)
Eric Wongdc059292021-06-30 00:01:32 +00001049 die_errno(_("mmap failed%s"), mmap_os_err());
Jeff King15708562015-05-28 03:56:15 -04001050 return ret;
1051}
1052
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001053static int format_object_header_literally(char *str, size_t size,
1054 const char *type, size_t objsize)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001055{
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001056 return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1;
1057}
1058
1059int format_object_header(char *str, size_t size, enum object_type type,
1060 size_t objsize)
1061{
1062 const char *name = type_name(type);
1063
1064 if (!name)
1065 BUG("could not get a type name for 'enum object_type' value %d", type);
1066
1067 return format_object_header_literally(str, size, name, objsize);
1068}
1069
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001070int check_object_signature(struct repository *r, const struct object_id *oid,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01001071 void *buf, unsigned long size,
1072 enum object_type type)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001073{
Ævar Arnfjörð Bjarmason0f156db2022-02-05 00:48:30 +01001074 struct object_id real_oid;
1075
1076 hash_object_file(r->hash_algo, buf, size, type, &real_oid);
1077
1078 return !oideq(oid, &real_oid) ? -1 : 0;
1079}
1080
1081int stream_object_signature(struct repository *r, const struct object_id *oid)
1082{
1083 struct object_id real_oid;
1084 unsigned long size;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001085 enum object_type obj_type;
1086 struct git_istream *st;
brian m. carlson18e25882018-02-01 02:18:41 +00001087 git_hash_ctx c;
brian m. carlson1af64f72018-03-12 02:27:55 +00001088 char hdr[MAX_HEADER_LEN];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001089 int hdrlen;
1090
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001091 st = open_istream(r, oid, &obj_type, &size, NULL);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001092 if (!st)
1093 return -1;
1094
1095 /* Generate the header */
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001096 hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001097
1098 /* Sha1.. */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001099 r->hash_algo->init_fn(&c);
1100 r->hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001101 for (;;) {
1102 char buf[1024 * 16];
1103 ssize_t readlen = read_istream(st, buf, sizeof(buf));
1104
Jeff Kingf54fac52013-03-25 16:17:17 -04001105 if (readlen < 0) {
1106 close_istream(st);
1107 return -1;
1108 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001109 if (!readlen)
1110 break;
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001111 r->hash_algo->update_fn(&c, buf, readlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001112 }
Ævar Arnfjörð Bjarmason0f156db2022-02-05 00:48:30 +01001113 r->hash_algo->final_oid_fn(&real_oid, &c);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001114 close_istream(st);
Ævar Arnfjörð Bjarmason0f156db2022-02-05 00:48:30 +01001115 return !oideq(oid, &real_oid) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001116}
1117
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001118int git_open_cloexec(const char *name, int flags)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001119{
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001120 int fd;
1121 static int o_cloexec = O_CLOEXEC;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001122
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001123 fd = open(name, flags | o_cloexec);
1124 if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
Lars Schneidercd66ada2016-10-24 20:02:59 +02001125 /* Try again w/o O_CLOEXEC: the kernel might not support it */
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001126 o_cloexec &= ~O_CLOEXEC;
1127 fd = open(name, flags | o_cloexec);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001128 }
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001129
Eric Wong9fb94952017-07-15 18:55:40 +00001130#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001131 {
1132 static int fd_cloexec = FD_CLOEXEC;
1133
1134 if (!o_cloexec && 0 <= fd && fd_cloexec) {
1135 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
Eric Wong9fb94952017-07-15 18:55:40 +00001136 int flags = fcntl(fd, F_GETFD);
1137 if (fcntl(fd, F_SETFD, flags | fd_cloexec))
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001138 fd_cloexec = 0;
1139 }
1140 }
1141#endif
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001142 return fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001143}
1144
Jeff King771e7d52017-01-13 12:54:39 -05001145/*
Jeff King514c5fd2019-01-07 03:35:42 -05001146 * Find "oid" as a loose object in the local repository or in an alternate.
Jeff King771e7d52017-01-13 12:54:39 -05001147 * Returns 0 on success, negative on failure.
1148 *
1149 * The "path" out-parameter will give the path of the object we found (if any).
1150 * Note that it may point to static storage and is only valid until another
Jeff King514c5fd2019-01-07 03:35:42 -05001151 * call to stat_loose_object().
Jeff King771e7d52017-01-13 12:54:39 -05001152 */
Jeff King514c5fd2019-01-07 03:35:42 -05001153static int stat_loose_object(struct repository *r, const struct object_id *oid,
1154 struct stat *st, const char **path)
Jeff King052fe5e2013-07-12 02:30:48 -04001155{
Jeff King263db402018-11-12 09:48:47 -05001156 struct object_directory *odb;
Christian Couderea657732018-01-17 18:54:54 +01001157 static struct strbuf buf = STRBUF_INIT;
Jeff King052fe5e2013-07-12 02:30:48 -04001158
Stefan Bellerd2607fa2018-03-23 18:21:17 +01001159 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001160 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001161 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001162 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -04001163 return 0;
1164 }
1165
1166 return -1;
1167}
1168
Jeff King771e7d52017-01-13 12:54:39 -05001169/*
Jeff King514c5fd2019-01-07 03:35:42 -05001170 * Like stat_loose_object(), but actually open the object and return the
Jeff King771e7d52017-01-13 12:54:39 -05001171 * descriptor. See the caveats on the "path" parameter above.
1172 */
Jeff King514c5fd2019-01-07 03:35:42 -05001173static int open_loose_object(struct repository *r,
1174 const struct object_id *oid, const char **path)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001175{
1176 int fd;
Jeff King263db402018-11-12 09:48:47 -05001177 struct object_directory *odb;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001178 int most_interesting_errno = ENOENT;
Christian Couderea657732018-01-17 18:54:54 +01001179 static struct strbuf buf = STRBUF_INIT;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001180
Stefan Bellerec7283e2018-03-23 18:21:18 +01001181 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001182 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001183 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001184 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001185 if (fd >= 0)
1186 return fd;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001187
Jeff Kingd6c8a052014-05-15 04:54:06 -04001188 if (most_interesting_errno == ENOENT)
1189 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001190 }
Jeff Kingd6c8a052014-05-15 04:54:06 -04001191 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001192 return -1;
1193}
1194
Jeff King61c77112018-11-12 09:54:42 -05001195static int quick_has_loose(struct repository *r,
Jeff Kingd7a24572019-01-07 03:37:29 -05001196 const struct object_id *oid)
Jeff King61c77112018-11-12 09:54:42 -05001197{
Jeff King61c77112018-11-12 09:54:42 -05001198 struct object_directory *odb;
1199
Jeff King61c77112018-11-12 09:54:42 -05001200 prepare_alt_odb(r);
1201 for (odb = r->objects->odb; odb; odb = odb->next) {
Eric Wong92d8ed82021-07-07 23:10:19 +00001202 if (oidtree_contains(odb_loose_cache(odb, oid), oid))
Jeff King61c77112018-11-12 09:54:42 -05001203 return 1;
1204 }
1205 return 0;
1206}
1207
Jeff Kingf6371f92017-01-13 12:58:16 -05001208/*
1209 * Map the loose object at "path" if it is not NULL, or the path found by
Jeff King514c5fd2019-01-07 03:35:42 -05001210 * searching for a loose object named "oid".
Jeff Kingf6371f92017-01-13 12:58:16 -05001211 */
Jeff King514c5fd2019-01-07 03:35:42 -05001212static void *map_loose_object_1(struct repository *r, const char *path,
1213 const struct object_id *oid, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001214{
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001215 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -07001216 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -07001217
Jeff Kingf6371f92017-01-13 12:58:16 -05001218 if (path)
1219 fd = git_open(path);
1220 else
Jeff King514c5fd2019-01-07 03:35:42 -05001221 fd = open_loose_object(r, oid, &path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001222 map = NULL;
1223 if (fd >= 0) {
1224 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001225
Linus Torvalds44d1c192008-06-14 11:32:37 -07001226 if (!fstat(fd, &st)) {
1227 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001228 if (!*size) {
1229 /* mmap() is forbidden on empty files */
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001230 error(_("object file %s is empty"), path);
René Scharfe68819252019-01-07 17:48:02 +01001231 close(fd);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001232 return NULL;
1233 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001234 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -07001235 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001236 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001237 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001238 return map;
1239}
1240
Jeff King514c5fd2019-01-07 03:35:42 -05001241void *map_loose_object(struct repository *r,
1242 const struct object_id *oid,
1243 unsigned long *size)
Jeff Kingf6371f92017-01-13 12:58:16 -05001244{
Jeff King514c5fd2019-01-07 03:35:42 -05001245 return map_loose_object_1(r, NULL, oid, size);
Jeff Kingf6371f92017-01-13 12:58:16 -05001246}
1247
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001248enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
1249 unsigned char *map,
1250 unsigned long mapsize,
1251 void *buffer,
1252 unsigned long bufsiz,
1253 struct strbuf *header)
Linus Torvaldsc4483572005-06-01 17:54:59 -07001254{
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001255 int status;
Matheus Tavares31877c92020-01-15 23:39:53 -03001256
Linus Torvaldsc4483572005-06-01 17:54:59 -07001257 /* Get the data stream */
1258 memset(stream, 0, sizeof(*stream));
1259 stream->next_in = map;
1260 stream->avail_in = mapsize;
1261 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -07001262 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -07001263
Linus Torvalds39c68542009-01-07 19:54:47 -08001264 git_inflate_init(stream);
Matheus Tavares31877c92020-01-15 23:39:53 -03001265 obj_read_unlock();
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001266 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001267 obj_read_lock();
Junio C Hamanod21f8422016-09-25 21:29:04 -07001268 if (status < Z_OK)
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001269 return ULHR_BAD;
Karthik Nayak46f03442015-05-03 19:59:59 +05301270
1271 /*
1272 * Check if entire header is unpacked in the first iteration.
1273 */
1274 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001275 return ULHR_OK;
Karthik Nayak46f03442015-05-03 19:59:59 +05301276
1277 /*
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001278 * We have a header longer than MAX_HEADER_LEN. The "header"
1279 * here is only non-NULL when we run "cat-file
1280 * --allow-unknown-type".
1281 */
1282 if (!header)
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001283 return ULHR_TOO_LONG;
Karthik Nayak46f03442015-05-03 19:59:59 +05301284
1285 /*
1286 * buffer[0..bufsiz] was not large enough. Copy the partial
1287 * result out to header, and then append the result of further
1288 * reading the stream.
1289 */
1290 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1291 stream->next_out = buffer;
1292 stream->avail_out = bufsiz;
1293
1294 do {
Matheus Tavares31877c92020-01-15 23:39:53 -03001295 obj_read_unlock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301296 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001297 obj_read_lock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301298 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1299 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1300 return 0;
1301 stream->next_out = buffer;
1302 stream->avail_out = bufsiz;
1303 } while (status != Z_STREAM_END);
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001304 return ULHR_TOO_LONG;
Karthik Nayak46f03442015-05-03 19:59:59 +05301305}
1306
Jeff King00a77602019-01-07 03:37:02 -05001307static void *unpack_loose_rest(git_zstream *stream,
1308 void *buffer, unsigned long size,
1309 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001310{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001311 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001312 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001313 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001314 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001315
Linus Torvalds93821bd2006-07-11 12:48:08 -07001316 n = stream->total_out - bytes;
1317 if (n > size)
1318 n = size;
1319 memcpy(buf, (char *) buffer + bytes, n);
1320 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001321 if (bytes <= size) {
1322 /*
1323 * The above condition must be (bytes <= size), not
1324 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001325 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001326 * the input zlib stream may have bytes that express
1327 * "this concludes the stream", and we *do* want to
1328 * eat that input.
1329 *
1330 * Otherwise we would not be able to test that we
1331 * consumed all the input to reach the expected size;
1332 * we also want to check that zlib tells us that all
1333 * went well with status == Z_STREAM_END at the end.
1334 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001335 stream->next_out = buf + bytes;
1336 stream->avail_out = size - bytes;
Matheus Tavares31877c92020-01-15 23:39:53 -03001337 while (status == Z_OK) {
1338 obj_read_unlock();
Linus Torvalds39c68542009-01-07 19:54:47 -08001339 status = git_inflate(stream, Z_FINISH);
Matheus Tavares31877c92020-01-15 23:39:53 -03001340 obj_read_lock();
1341 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001342 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001343 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001344 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001345 return buf;
1346 }
1347
1348 if (status < 0)
Jeff King00a77602019-01-07 03:37:02 -05001349 error(_("corrupt loose object '%s'"), oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001350 else if (stream->avail_in)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001351 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05001352 oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001353 free(buf);
1354 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001355}
1356
Linus Torvalds5180cac2005-06-02 07:57:25 -07001357/*
1358 * We used to just use "sscanf()", but that's actually way
1359 * too permissive for what we want to check. So do an anal
1360 * object header parse by hand.
1361 */
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001362int parse_loose_header(const char *hdr, struct object_info *oi)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001363{
Karthik Nayak46f03442015-05-03 19:59:59 +05301364 const char *type_buf = hdr;
Matt Cooperd6a09e72021-11-02 15:46:10 +00001365 size_t size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301366 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001367
1368 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301369 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001370 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001371 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001372 for (;;) {
1373 char c = *hdr++;
Junio C Hamanod21f8422016-09-25 21:29:04 -07001374 if (!c)
1375 return -1;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001376 if (c == ' ')
1377 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301378 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001379 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301380
1381 type = type_from_string_gently(type_buf, type_len, 1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001382 if (oi->type_name)
1383 strbuf_add(oi->type_name, type_buf, type_len);
Karthik Nayak46f03442015-05-03 19:59:59 +05301384 if (oi->typep)
1385 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001386
1387 /*
1388 * The length must follow immediately, and be in canonical
1389 * decimal format (ie "010" is not valid).
1390 */
1391 size = *hdr++ - '0';
1392 if (size > 9)
1393 return -1;
1394 if (size) {
1395 for (;;) {
1396 unsigned long c = *hdr - '0';
1397 if (c > 9)
1398 break;
1399 hdr++;
Matt Cooperd6a09e72021-11-02 15:46:10 +00001400 size = st_add(st_mult(size, 10), c);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001401 }
1402 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301403
1404 if (oi->sizep)
Matt Cooperd6a09e72021-11-02 15:46:10 +00001405 *oi->sizep = cast_size_t_to_ulong(size);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001406
1407 /*
1408 * The length must be followed by a zero byte
1409 */
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001410 if (*hdr)
1411 return -1;
Karthik Nayak46f03442015-05-03 19:59:59 +05301412
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001413 /*
1414 * The format is valid, but the type may still be bogus. The
1415 * Caller needs to check its oi->typep.
1416 */
1417 return 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001418}
1419
Jeff King514c5fd2019-01-07 03:35:42 -05001420static int loose_object_info(struct repository *r,
1421 const struct object_id *oid,
1422 struct object_info *oi, int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001423{
Karthik Nayak46f03442015-05-03 19:59:59 +05301424 int status = 0;
1425 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001426 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001427 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00001428 char hdr[MAX_HEADER_LEN];
Karthik Nayak46f03442015-05-03 19:59:59 +05301429 struct strbuf hdrbuf = STRBUF_INIT;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001430 unsigned long size_scratch;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001431 enum object_type type_scratch;
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001432 int allow_unknown = flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001433
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001434 if (oi->delta_base_oid)
1435 oidclr(oi->delta_base_oid);
Jeff King5d642e72013-12-21 09:24:20 -05001436
Jeff King052fe5e2013-07-12 02:30:48 -04001437 /*
1438 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001439 * need to look inside the object at all. Note that we
1440 * do not optimize out the stat call, even if the
1441 * caller doesn't care about the disk-size, since our
1442 * return value implicitly indicates whether the
1443 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04001444 */
Brandon Williams6ca32f42018-02-14 10:59:23 -08001445 if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
Jeff King771e7d52017-01-13 12:54:39 -05001446 const char *path;
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001447 struct stat st;
Jeff King61c77112018-11-12 09:54:42 -05001448 if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK))
Jeff Kingd7a24572019-01-07 03:37:29 -05001449 return quick_has_loose(r, oid) ? 0 : -1;
Jeff King514c5fd2019-01-07 03:35:42 -05001450 if (stat_loose_object(r, oid, &st, &path) < 0)
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001451 return -1;
1452 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04001453 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04001454 return 0;
1455 }
1456
Jeff King514c5fd2019-01-07 03:35:42 -05001457 map = map_loose_object(r, oid, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001458 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02001459 return -1;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001460
1461 if (!oi->sizep)
1462 oi->sizep = &size_scratch;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001463 if (!oi->typep)
1464 oi->typep = &type_scratch;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001465
Jeff King23c339c2013-07-12 02:37:53 -04001466 if (oi->disk_sizep)
1467 *oi->disk_sizep = mapsize;
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001468
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001469 switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
1470 allow_unknown ? &hdrbuf : NULL)) {
1471 case ULHR_OK:
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001472 if (parse_loose_header(hdrbuf.len ? hdrbuf.buf : hdr, oi) < 0)
1473 status = error(_("unable to parse %s header"), oid_to_hex(oid));
1474 else if (!allow_unknown && *oi->typep < 0)
1475 die(_("invalid object type"));
1476
1477 if (!oi->contentp)
1478 break;
1479 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
1480 if (*oi->contentp)
1481 goto cleanup;
1482
1483 status = -1;
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001484 break;
1485 case ULHR_BAD:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001486 status = error(_("unable to unpack %s header"),
Jeff King514c5fd2019-01-07 03:35:42 -05001487 oid_to_hex(oid));
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001488 break;
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001489 case ULHR_TOO_LONG:
1490 status = error(_("header for %s too long, exceeds %d bytes"),
1491 oid_to_hex(oid), MAX_HEADER_LEN);
1492 break;
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001493 }
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001494
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001495 git_inflate_end(&stream);
1496cleanup:
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001497 munmap(map, mapsize);
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001498 if (oi->sizep == &size_scratch)
1499 oi->sizep = NULL;
Karthik Nayak46f03442015-05-03 19:59:59 +05301500 strbuf_release(&hdrbuf);
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001501 if (oi->typep == &type_scratch)
1502 oi->typep = NULL;
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001503 oi->whence = OI_LOOSE;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001504 return status;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001505}
1506
Matheus Tavares31877c92020-01-15 23:39:53 -03001507int obj_read_use_lock = 0;
1508pthread_mutex_t obj_read_mutex;
1509
1510void enable_obj_read_lock(void)
1511{
1512 if (obj_read_use_lock)
1513 return;
1514
1515 obj_read_use_lock = 1;
1516 init_recursive_mutex(&obj_read_mutex);
1517}
1518
1519void disable_obj_read_lock(void)
1520{
1521 if (!obj_read_use_lock)
1522 return;
1523
1524 obj_read_use_lock = 0;
1525 pthread_mutex_destroy(&obj_read_mutex);
1526}
1527
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001528int fetch_if_missing = 1;
1529
Matheus Tavares31877c92020-01-15 23:39:53 -03001530static int do_oid_object_info_extended(struct repository *r,
1531 const struct object_id *oid,
1532 struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001533{
Jonathan Tancd585e22017-06-21 17:40:23 -07001534 static struct object_info blank_oi = OBJECT_INFO_INIT;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001535 struct cached_object *co;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001536 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04001537 int rtype;
brian m. carlsonb383a132018-03-12 02:27:54 +00001538 const struct object_id *real = oid;
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001539 int already_retried = 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001540
Matheus Tavares31877c92020-01-15 23:39:53 -03001541
brian m. carlsonb383a132018-03-12 02:27:54 +00001542 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
Stefan Beller9d983542018-04-25 11:21:06 -07001543 real = lookup_replace_object(r, oid);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001544
brian m. carlsonb383a132018-03-12 02:27:54 +00001545 if (is_null_oid(real))
Jeff King87b5e232017-11-21 18:17:39 -05001546 return -1;
1547
Jonathan Tancd585e22017-06-21 17:40:23 -07001548 if (!oi)
1549 oi = &blank_oi;
1550
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001551 co = find_cached_object(real);
1552 if (co) {
1553 if (oi->typep)
1554 *(oi->typep) = co->type;
1555 if (oi->sizep)
1556 *(oi->sizep) = co->size;
1557 if (oi->disk_sizep)
1558 *(oi->disk_sizep) = 0;
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001559 if (oi->delta_base_oid)
1560 oidclr(oi->delta_base_oid);
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001561 if (oi->type_name)
1562 strbuf_addstr(oi->type_name, type_name(co->type));
1563 if (oi->contentp)
1564 *oi->contentp = xmemdupz(co->buf, co->size);
1565 oi->whence = OI_CACHED;
1566 return 0;
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07001567 }
1568
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001569 while (1) {
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001570 if (find_pack_entry(r, real, &e))
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001571 break;
1572
Takuto Ikuta024aa462018-03-14 15:32:42 +09001573 if (flags & OBJECT_INFO_IGNORE_LOOSE)
1574 return -1;
1575
Steven Grimmddd63e62008-08-05 13:08:41 -07001576 /* Most likely it's a loose object. */
Jeff King514c5fd2019-01-07 03:35:42 -05001577 if (!loose_object_info(r, real, oi, flags))
Jeff King5b086402013-07-12 02:34:57 -04001578 return 0;
Steven Grimmddd63e62008-08-05 13:08:41 -07001579
1580 /* Not a loose object; someone else may have just packed it. */
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001581 if (!(flags & OBJECT_INFO_QUICK)) {
Stefan Beller9d983542018-04-25 11:21:06 -07001582 reprepare_packed_git(r);
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001583 if (find_pack_entry(r, real, &e))
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001584 break;
1585 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001586
Jonathan Taneef71902021-10-08 14:08:18 -07001587 /*
1588 * If r is the_repository, this might be an attempt at
1589 * accessing a submodule object as if it were in the_repository
1590 * (having called add_submodule_odb() on that submodule's ODB).
1591 * If any such ODBs exist, register them and try again.
1592 */
1593 if (r == the_repository &&
1594 register_all_submodule_odb_as_alternates())
Jonathan Tana35e03d2021-08-16 14:09:51 -07001595 /* We added some alternates; retry */
1596 continue;
1597
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001598 /* Check if it is a missing object */
Jonathan Tanef830cc2021-06-17 10:13:26 -07001599 if (fetch_if_missing && repo_has_promisor_remote(r) &&
1600 !already_retried &&
Derrick Stolee31f52562019-05-28 08:19:07 -07001601 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001602 /*
Christian Couderb14ed5a2019-06-25 15:40:31 +02001603 * TODO Investigate checking promisor_remote_get_direct()
1604 * TODO return value and stopping on error here.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001605 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001606 promisor_remote_get_direct(r, real, 1);
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001607 already_retried = 1;
1608 continue;
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001609 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001610
1611 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001612 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001613
Jonathan Tancd585e22017-06-21 17:40:23 -07001614 if (oi == &blank_oi)
1615 /*
1616 * We know that the caller doesn't actually need the
1617 * information below, so return early.
1618 */
1619 return 0;
Stefan Beller9d983542018-04-25 11:21:06 -07001620 rtype = packed_object_info(r, e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04001621 if (rtype < 0) {
René Scharfe751530d2021-09-11 22:40:33 +02001622 mark_bad_packed_object(e.p, real);
Matheus Tavares31877c92020-01-15 23:39:53 -03001623 return do_oid_object_info_extended(r, real, oi, 0);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001624 } else if (oi->whence == OI_PACKED) {
Junio C Hamano9a490592011-05-12 15:51:38 -07001625 oi->u.packed.offset = e.offset;
1626 oi->u.packed.pack = e.p;
1627 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
1628 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001629 }
1630
Jeff King5b086402013-07-12 02:34:57 -04001631 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001632}
1633
Matheus Tavares31877c92020-01-15 23:39:53 -03001634int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1635 struct object_info *oi, unsigned flags)
1636{
1637 int ret;
1638 obj_read_lock();
1639 ret = do_oid_object_info_extended(r, oid, oi, flags);
1640 obj_read_unlock();
1641 return ret;
1642}
1643
1644
Christian Couder3fc0dca2013-10-27 00:34:30 +02001645/* returns enum object_type or negative */
Stefan Beller9d983542018-04-25 11:21:06 -07001646int oid_object_info(struct repository *r,
1647 const struct object_id *oid,
1648 unsigned long *sizep)
Junio C Hamano9a490592011-05-12 15:51:38 -07001649{
Jeff King5b086402013-07-12 02:34:57 -04001650 enum object_type type;
Jeff King27b5c1a2016-08-11 05:24:35 -04001651 struct object_info oi = OBJECT_INFO_INIT;
Junio C Hamano9a490592011-05-12 15:51:38 -07001652
Jeff King5b086402013-07-12 02:34:57 -04001653 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001654 oi.sizep = sizep;
Stefan Beller9d983542018-04-25 11:21:06 -07001655 if (oid_object_info_extended(r, oid, &oi,
1656 OBJECT_INFO_LOOKUP_REPLACE) < 0)
Jeff King5b086402013-07-12 02:34:57 -04001657 return -1;
1658 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001659}
1660
Stefan Beller1b9b5c62018-10-16 16:35:32 -07001661static void *read_object(struct repository *r,
Junio C Hamanocba595a2019-02-06 22:05:27 -08001662 const struct object_id *oid, enum object_type *type,
Jonathan Tanf1d81302017-08-18 15:20:30 -07001663 unsigned long *size)
1664{
1665 struct object_info oi = OBJECT_INFO_INIT;
1666 void *content;
1667 oi.typep = type;
1668 oi.sizep = size;
1669 oi.contentp = &content;
1670
Junio C Hamanocba595a2019-02-06 22:05:27 -08001671 if (oid_object_info_extended(r, oid, &oi, 0) < 0)
Jonathan Tanf1d81302017-08-18 15:20:30 -07001672 return NULL;
1673 return content;
1674}
1675
Patryk Obara829e5c32018-01-28 01:13:11 +01001676int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1677 struct object_id *oid)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001678{
1679 struct cached_object *co;
1680
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01001681 hash_object_file(the_hash_algo, buf, len, type, oid);
Jonathan Tana64d2aa2020-07-21 15:50:20 -07001682 if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
1683 find_cached_object(oid))
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001684 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04001685 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001686 co = &cached_objects[cached_object_nr++];
1687 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001688 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08001689 co->buf = xmalloc(len);
1690 memcpy(co->buf, buf, len);
brian m. carlson62ba93e2018-05-02 00:26:03 +00001691 oidcpy(&co->oid, oid);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001692 return 0;
1693}
1694
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001695/*
1696 * This function dies on corrupt objects; the callers who want to
1697 * deal with them should arrange to call read_object() and give error
1698 * messages themselves.
1699 */
Stefan Bellera3b72c82018-11-13 16:12:46 -08001700void *read_object_file_extended(struct repository *r,
1701 const struct object_id *oid,
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001702 enum object_type *type,
1703 unsigned long *size,
1704 int lookup_replace)
Nicolas Pitreac939102008-07-14 21:46:48 -04001705{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001706 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001707 const struct packed_git *p;
Jeff King771e7d52017-01-13 12:54:39 -05001708 const char *path;
1709 struct stat st;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -07001710 const struct object_id *repl = lookup_replace ?
Stefan Bellera3b72c82018-11-13 16:12:46 -08001711 lookup_replace_object(r, oid) : oid;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001712
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001713 errno = 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08001714 data = read_object(r, repl, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07001715 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001716 return data;
Christian Couder68095572009-01-23 10:06:53 +01001717
Matheus Tavares31877c92020-01-15 23:39:53 -03001718 obj_read_lock();
Björn Steinbrink25f3af32011-01-20 21:12:20 +01001719 if (errno && errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001720 die_errno(_("failed to read object %s"), oid_to_hex(oid));
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001721
Christian Couder68095572009-01-23 10:06:53 +01001722 /* die if we replaced an object with one that does not exist */
brian m. carlsonb383a132018-03-12 02:27:54 +00001723 if (repl != oid)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001724 die(_("replacement %s not found for %s"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001725 oid_to_hex(repl), oid_to_hex(oid));
Christian Couder68095572009-01-23 10:06:53 +01001726
Junio C Hamanocba595a2019-02-06 22:05:27 -08001727 if (!stat_loose_object(r, repl, &st, &path))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001728 die(_("loose object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001729 oid_to_hex(repl), path);
Christian Couder68095572009-01-23 10:06:53 +01001730
René Scharfe7407d732021-09-11 22:42:20 +02001731 if ((p = has_packed_and_bad(r, repl)) != NULL)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001732 die(_("packed object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001733 oid_to_hex(repl), p->pack_name);
Matheus Tavares31877c92020-01-15 23:39:53 -03001734 obj_read_unlock();
Christian Couderf5552ae2009-01-23 10:07:01 +01001735
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001736 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001737}
1738
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001739void *read_object_with_reference(struct repository *r,
1740 const struct object_id *oid,
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +01001741 enum object_type required_type,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001742 unsigned long *size,
brian m. carlson02f05472018-03-12 02:27:52 +00001743 struct object_id *actual_oid_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001744{
Ævar Arnfjörð Bjarmason6aea6ba2022-02-05 00:48:34 +01001745 enum object_type type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001746 void *buffer;
1747 unsigned long isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001748 struct object_id actual_oid;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001749
brian m. carlson02f05472018-03-12 02:27:52 +00001750 oidcpy(&actual_oid, oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001751 while (1) {
1752 int ref_length = -1;
1753 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001754
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001755 buffer = repo_read_object_file(r, &actual_oid, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001756 if (!buffer)
1757 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001758 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001759 *size = isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001760 if (actual_oid_return)
1761 oidcpy(actual_oid_return, &actual_oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001762 return buffer;
1763 }
1764 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001765 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001766 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05001767 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001768 ref_type = "object ";
1769 else {
1770 free(buffer);
1771 return NULL;
1772 }
1773 ref_length = strlen(ref_type);
1774
brian m. carlson94b5e092018-07-16 01:28:07 +00001775 if (ref_length + the_hash_algo->hexsz > isize ||
Martin Koegler50974ec2008-02-18 21:47:52 +01001776 memcmp(buffer, ref_type, ref_length) ||
brian m. carlson02f05472018-03-12 02:27:52 +00001777 get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001778 free(buffer);
1779 return NULL;
1780 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001781 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001782 /* Now we have the ID of the referred-to object in
brian m. carlson02f05472018-03-12 02:27:52 +00001783 * actual_oid. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001784 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001785}
1786
Ævar Arnfjörð Bjarmason2bbb28a2022-02-05 00:48:33 +01001787static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c,
1788 const void *buf, unsigned long len,
1789 struct object_id *oid,
1790 char *hdr, int *hdrlen)
1791{
1792 algo->init_fn(c);
1793 algo->update_fn(c, hdr, *hdrlen);
1794 algo->update_fn(c, buf, len);
1795 algo->final_oid_fn(oid, c);
1796}
1797
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001798static void write_object_file_prepare(const struct git_hash_algo *algo,
1799 const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmason2bbb28a2022-02-05 00:48:33 +01001800 enum object_type type, struct object_id *oid,
1801 char *hdr, int *hdrlen)
1802{
1803 git_hash_ctx c;
1804
1805 /* Generate the header */
1806 *hdrlen = format_object_header(hdr, *hdrlen, type, len);
1807
1808 /* Sha1.. */
1809 hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
1810}
1811
1812static void write_object_file_prepare_literally(const struct git_hash_algo *algo,
1813 const void *buf, unsigned long len,
Patryk Obaraa09c9852018-01-28 01:13:19 +01001814 const char *type, struct object_id *oid,
1815 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001816{
brian m. carlson18e25882018-02-01 02:18:41 +00001817 git_hash_ctx c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001818
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001819 *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
Ævar Arnfjörð Bjarmason2bbb28a2022-02-05 00:48:33 +01001820 hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001821}
1822
Linus Torvalds230f1322005-10-08 15:54:01 -07001823/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001824 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07001825 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07001826int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001827{
Thomas Raste32c0a92008-09-19 00:24:46 +02001828 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001829
Johannes Schindelin348df162009-04-28 00:32:25 +02001830 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001831 goto try_rename;
1832 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02001833 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001834
1835 /*
1836 * Coda hack - coda doesn't like cross-directory links,
1837 * so we fall back to a rename, which will mean that it
1838 * won't be able to check collisions, but that's not a
1839 * big deal.
1840 *
1841 * The same holds for FAT formatted media.
1842 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07001843 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001844 * left to unlink.
1845 */
1846 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001847 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001848 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07001849 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001850 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001851 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001852 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07001853 if (ret) {
1854 if (ret != EEXIST) {
Jeff King2c319882019-01-07 03:39:33 -05001855 return error_errno(_("unable to write file %s"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001856 }
1857 /* FIXME!!! Collision check here ? */
1858 }
1859
Junio C Hamano3be1f182009-03-27 23:14:39 -07001860out:
Matthieu Moy5256b002010-02-22 23:32:16 +01001861 if (adjust_shared_perm(filename))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001862 return error(_("unable to set permission to '%s'"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001863 return 0;
1864}
1865
Linus Torvalds4d548152006-05-24 08:30:54 -07001866static int write_buffer(int fd, const void *buf, size_t len)
1867{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08001868 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001869 return error_errno(_("file write error"));
Linus Torvalds4d548152006-05-24 08:30:54 -07001870 return 0;
1871}
1872
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01001873static void hash_object_file_literally(const struct git_hash_algo *algo,
1874 const void *buf, unsigned long len,
1875 const char *type, struct object_id *oid)
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001876{
brian m. carlson1af64f72018-03-12 02:27:55 +00001877 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001878 int hdrlen = sizeof(hdr);
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01001879
Ævar Arnfjörð Bjarmason2bbb28a2022-02-05 00:48:33 +01001880 write_object_file_prepare_literally(algo, buf, len, type, oid, hdr, &hdrlen);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001881}
1882
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01001883void hash_object_file(const struct git_hash_algo *algo, const void *buf,
1884 unsigned long len, enum object_type type,
1885 struct object_id *oid)
1886{
1887 hash_object_file_literally(algo, buf, len, type_name(type), oid);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001888}
1889
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001890/* Finalize a file on disk, and close it. */
Neeraj Singhc4e707f2022-03-30 18:14:15 +00001891static void close_loose_object(int fd, const char *filename)
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001892{
Neeraj Singh020406e2022-03-10 22:43:21 +00001893 if (the_repository->objects->odb->will_destroy)
1894 goto out;
Neeraj Singhb3cecf42021-12-06 22:05:04 +00001895
Neeraj Singh020406e2022-03-10 22:43:21 +00001896 if (fsync_object_files > 0)
Neeraj Singhc4e707f2022-03-30 18:14:15 +00001897 fsync_or_die(fd, filename);
Neeraj Singh020406e2022-03-10 22:43:21 +00001898 else
1899 fsync_component_or_die(FSYNC_COMPONENT_LOOSE_OBJECT, fd,
Neeraj Singhc4e707f2022-03-30 18:14:15 +00001900 filename);
Neeraj Singh020406e2022-03-10 22:43:21 +00001901
1902out:
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001903 if (close(fd) != 0)
Jeff King76011352019-01-07 03:39:24 -05001904 die_errno(_("error when closing loose object file"));
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001905}
1906
Linus Torvalds5723fe72008-06-14 10:50:12 -07001907/* Size of directory component, including the ending '/' */
1908static inline int directory_size(const char *filename)
1909{
1910 const char *s = strrchr(filename, '/');
1911 if (!s)
1912 return 0;
1913 return s - filename + 1;
1914}
1915
1916/*
1917 * This creates a temporary file in the same directory as the final
1918 * 'filename'
1919 *
1920 * We want to avoid cross-directory filename renames, because those
1921 * can have problems on various filesystems (FAT, NFS, Coda).
1922 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001923static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07001924{
1925 int fd, dirlen = directory_size(filename);
1926
Jeff Kingd4b3d112015-09-24 17:07:49 -04001927 strbuf_reset(tmp);
1928 strbuf_add(tmp, filename, dirlen);
1929 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
1930 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05001931 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04001932 /*
1933 * Make sure the directory exists; note that the contents
1934 * of the buffer are undefined after mkstemp returns an
1935 * error, so we have to rewrite the whole buffer from
1936 * scratch.
1937 */
1938 strbuf_reset(tmp);
1939 strbuf_add(tmp, filename, dirlen - 1);
1940 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01001941 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001942 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07001943 return -1;
1944
1945 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001946 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
1947 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07001948 }
1949 return fd;
1950}
1951
Patryk Obara3fc72812018-01-28 01:13:21 +01001952static int write_loose_object(const struct object_id *oid, char *hdr,
1953 int hdrlen, const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001954 time_t mtime, unsigned flags)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001955{
Jeff King915308b2009-01-29 00:56:34 -05001956 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001957 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001958 git_zstream stream;
brian m. carlson18e25882018-02-01 02:18:41 +00001959 git_hash_ctx c;
Patryk Obara3fc72812018-01-28 01:13:21 +01001960 struct object_id parano_oid;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001961 static struct strbuf tmp_file = STRBUF_INIT;
Christian Couderea657732018-01-17 18:54:54 +01001962 static struct strbuf filename = STRBUF_INIT;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001963
Jeff King514c5fd2019-01-07 03:35:42 -05001964 loose_object_path(the_repository, &filename, oid);
Christian Couderea657732018-01-17 18:54:54 +01001965
1966 fd = create_tmpfile(&tmp_file, filename.buf);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001967 if (fd < 0) {
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001968 if (flags & HASH_SILENT)
1969 return -1;
1970 else if (errno == EACCES)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001971 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01001972 else
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001973 return error_errno(_("unable to create temporary file"));
Linus Torvaldsaac17942005-05-03 11:46:16 -07001974 }
1975
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001976 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001977 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001978 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001979 stream.avail_out = sizeof(compressed);
brian m. carlson18e25882018-02-01 02:18:41 +00001980 the_hash_algo->init_fn(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001981
1982 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05001983 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001984 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001985 while (git_deflate(&stream, 0) == Z_OK)
1986 ; /* nothing */
brian m. carlson18e25882018-02-01 02:18:41 +00001987 the_hash_algo->update_fn(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001988
1989 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04001990 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001991 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001992 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05001993 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001994 ret = git_deflate(&stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00001995 the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001996 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
Jeff King76011352019-01-07 03:39:24 -05001997 die(_("unable to write loose object file"));
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001998 stream.next_out = compressed;
1999 stream.avail_out = sizeof(compressed);
2000 } while (ret == Z_OK);
2001
Linus Torvaldsac54c272007-03-20 11:38:34 -07002002 if (ret != Z_STREAM_END)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002003 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01002004 ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07002005 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07002006 if (ret != Z_OK)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002007 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01002008 ret);
brian m. carlson5951bf42021-04-26 01:02:53 +00002009 the_hash_algo->final_oid_fn(&parano_oid, &c);
Jeff King9001dc22018-08-28 17:22:48 -04002010 if (!oideq(oid, &parano_oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002011 die(_("confused by unstable object source data for %s"),
Patryk Obara3fc72812018-01-28 01:13:21 +01002012 oid_to_hex(oid));
Linus Torvaldsac54c272007-03-20 11:38:34 -07002013
Neeraj Singhc4e707f2022-03-30 18:14:15 +00002014 close_loose_object(fd, tmp_file.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002015
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002016 if (mtime) {
2017 struct utimbuf utb;
2018 utb.actime = mtime;
2019 utb.modtime = mtime;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002020 if (utime(tmp_file.buf, &utb) < 0 &&
2021 !(flags & HASH_SILENT))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002022 warning_errno(_("failed utime() on %s"), tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002023 }
2024
Christian Couderea657732018-01-17 18:54:54 +01002025 return finalize_object_file(tmp_file.buf, filename.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07002026}
Daniel Barkalow8237b182005-04-23 18:47:23 -07002027
brian m. carlson6862ebb2018-05-02 00:25:34 +00002028static int freshen_loose_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04002029{
brian m. carlson6862ebb2018-05-02 00:25:34 +00002030 return check_and_freshen(oid, 1);
Jeff King33d42212014-10-15 18:42:22 -04002031}
2032
brian m. carlson6862ebb2018-05-02 00:25:34 +00002033static int freshen_packed_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04002034{
2035 struct pack_entry e;
brian m. carlson544443c2018-05-02 00:25:35 +00002036 if (!find_pack_entry(the_repository, oid, &e))
Jeff Kingee1c6c32015-04-20 15:55:00 -04002037 return 0;
2038 if (e.p->freshened)
2039 return 1;
2040 if (!freshen_file(e.p->pack_name))
2041 return 0;
2042 e.p->freshened = 1;
2043 return 1;
Jeff King33d42212014-10-15 18:42:22 -04002044}
2045
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002046int write_object_file_flags(const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002047 enum object_type type, struct object_id *oid,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002048 unsigned flags)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002049{
brian m. carlson1af64f72018-03-12 02:27:55 +00002050 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04002051 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002052
2053 /* Normally if we have it in the pack then we do not bother writing
2054 * it out into .git/objects/??/?{38} file.
2055 */
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002056 write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
2057 &hdrlen);
brian m. carlson6862ebb2018-05-02 00:25:34 +00002058 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002059 return 0;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002060 return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002061}
2062
Ævar Arnfjörð Bjarmason0ff7b4f2022-02-05 00:48:31 +01002063int write_object_file_literally(const void *buf, unsigned long len,
2064 const char *type, struct object_id *oid,
2065 unsigned flags)
Eric Sunshine0c3db672015-05-04 03:25:15 -04002066{
2067 char *header;
2068 int hdrlen, status = 0;
2069
2070 /* type string, SP, %lu of the length plus NUL must fit this */
brian m. carlson1af64f72018-03-12 02:27:55 +00002071 hdrlen = strlen(type) + MAX_HEADER_LEN;
Jeff Kingef1286d2015-09-24 17:06:42 -04002072 header = xmalloc(hdrlen);
Ævar Arnfjörð Bjarmason2bbb28a2022-02-05 00:48:33 +01002073 write_object_file_prepare_literally(the_hash_algo, buf, len, type,
2074 oid, header, &hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002075
2076 if (!(flags & HASH_WRITE_OBJECT))
2077 goto cleanup;
brian m. carlson6862ebb2018-05-02 00:25:34 +00002078 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Eric Sunshine0c3db672015-05-04 03:25:15 -04002079 goto cleanup;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002080 status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002081
2082cleanup:
2083 free(header);
2084 return status;
2085}
2086
Patryk Obara4bdb70a2018-01-28 01:13:20 +01002087int force_object_loose(const struct object_id *oid, time_t mtime)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002088{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002089 void *buf;
2090 unsigned long len;
2091 enum object_type type;
brian m. carlson1af64f72018-03-12 02:27:55 +00002092 char hdr[MAX_HEADER_LEN];
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002093 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002094 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002095
brian m. carlson6862ebb2018-05-02 00:25:34 +00002096 if (has_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002097 return 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08002098 buf = read_object(the_repository, oid, &type, &len);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002099 if (!buf)
Jeff King2c319882019-01-07 03:39:33 -05002100 return error(_("cannot read object for %s"), oid_to_hex(oid));
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01002101 hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002102 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002103 free(buf);
2104
2105 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002106}
2107
Jonathan Tan1d8d9cb2020-08-05 16:06:49 -07002108int has_object(struct repository *r, const struct object_id *oid,
2109 unsigned flags)
2110{
2111 int quick = !(flags & HAS_OBJECT_RECHECK_PACKED);
2112 unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT |
2113 (quick ? OBJECT_INFO_QUICK : 0);
2114
2115 if (!startup_info->have_repository)
2116 return 0;
2117 return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0;
2118}
2119
Junio C Hamanocba595a2019-02-06 22:05:27 -08002120int repo_has_object_file_with_flags(struct repository *r,
2121 const struct object_id *oid, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07002122{
Jonathan Nieder3e8b7d32017-04-11 15:47:13 -07002123 if (!startup_info->have_repository)
2124 return 0;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08002125 return oid_object_info_extended(r, oid, NULL, flags) >= 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07002126}
Junio C Hamano74400e72005-05-01 23:45:49 -07002127
Stefan Beller9b45f492018-11-13 16:12:48 -08002128int repo_has_object_file(struct repository *r,
2129 const struct object_id *oid)
brian m. carlsonb419aa22015-11-10 02:22:19 +00002130{
Junio C Hamanocba595a2019-02-06 22:05:27 -08002131 return repo_has_object_file_with_flags(r, oid, 0);
Jeff King5827a032016-10-13 12:53:44 -04002132}
2133
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002134static void check_tree(const void *buf, size_t size)
2135{
2136 struct tree_desc desc;
2137 struct name_entry entry;
2138
2139 init_tree_desc(&desc, buf, size);
2140 while (tree_entry(&desc, &entry))
2141 /* do nothing
2142 * tree_entry() will die() on malformed entries */
2143 ;
2144}
2145
2146static void check_commit(const void *buf, size_t size)
2147{
2148 struct commit c;
2149 memset(&c, 0, sizeof(c));
Stefan Beller08f4f442018-06-28 18:22:00 -07002150 if (parse_commit_buffer(the_repository, &c, buf, size, 0))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002151 die(_("corrupt commit"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002152}
2153
2154static void check_tag(const void *buf, size_t size)
2155{
2156 struct tag t;
2157 memset(&t, 0, sizeof(t));
Stefan Beller0e740fe2018-06-28 18:22:04 -07002158 if (parse_tag_buffer(the_repository, &t, buf, size))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002159 die(_("corrupt tag"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002160}
2161
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002162static int index_mem(struct index_state *istate,
2163 struct object_id *oid, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002164 enum object_type type,
2165 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02002166{
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01002167 int ret = 0;
Ævar Arnfjörð Bjarmasonbbea0dd2022-02-05 00:48:23 +01002168 int re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002169 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07002170
Bryan Larsen7672db22005-07-08 16:51:55 -07002171 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08002172 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08002173
2174 /*
2175 * Convert blobs to git internal format
2176 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002177 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002178 struct strbuf nbuf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002179 if (convert_to_git(istate, path, buf, size, &nbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002180 get_conv_flags(flags))) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002181 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08002182 re_allocated = 1;
2183 }
2184 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002185 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002186 if (type == OBJ_TREE)
2187 check_tree(buf, size);
2188 if (type == OBJ_COMMIT)
2189 check_commit(buf, size);
2190 if (type == OBJ_TAG)
2191 check_tag(buf, size);
2192 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08002193
Bryan Larsen7672db22005-07-08 16:51:55 -07002194 if (write_object)
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002195 ret = write_object_file(buf, size, type, oid);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02002196 else
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01002197 hash_object_file(the_hash_algo, buf, size, type, oid);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002198 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08002199 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002200 return ret;
2201}
2202
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002203static int index_stream_convert_blob(struct index_state *istate,
2204 struct object_id *oid,
2205 int fd,
2206 const char *path,
2207 unsigned flags)
Steffen Prohaska9035d752014-08-26 17:23:25 +02002208{
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01002209 int ret = 0;
Steffen Prohaska9035d752014-08-26 17:23:25 +02002210 const int write_object = flags & HASH_WRITE_OBJECT;
2211 struct strbuf sbuf = STRBUF_INIT;
2212
2213 assert(path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002214 assert(would_convert_to_git_filter_fd(istate, path));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002215
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002216 convert_to_git_filter_fd(istate, path, fd, &sbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002217 get_conv_flags(flags));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002218
2219 if (write_object)
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002220 ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
Patryk Obaraa09c9852018-01-28 01:13:19 +01002221 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002222 else
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01002223 hash_object_file(the_hash_algo, sbuf.buf, sbuf.len, OBJ_BLOB,
2224 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002225 strbuf_release(&sbuf);
2226 return ret;
2227}
2228
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002229static int index_pipe(struct index_state *istate, struct object_id *oid,
2230 int fd, enum object_type type,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002231 const char *path, unsigned flags)
2232{
2233 struct strbuf sbuf = STRBUF_INIT;
2234 int ret;
2235
2236 if (strbuf_read(&sbuf, fd, 4096) >= 0)
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002237 ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002238 else
2239 ret = -1;
2240 strbuf_release(&sbuf);
2241 return ret;
2242}
2243
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002244#define SMALL_FILE_SIZE (32*1024)
2245
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002246static int index_core(struct index_state *istate,
2247 struct object_id *oid, int fd, size_t size,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002248 enum object_type type, const char *path,
2249 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002250{
2251 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002252
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002253 if (!size) {
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002254 ret = index_mem(istate, oid, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002255 } else if (size <= SMALL_FILE_SIZE) {
2256 char *buf = xmalloc(size);
Jeff King90dca672017-09-27 02:01:07 -04002257 ssize_t read_result = read_in_full(fd, buf, size);
2258 if (read_result < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002259 ret = error_errno(_("read error while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002260 path ? path : "<unknown>");
2261 else if (read_result != size)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002262 ret = error(_("short read while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002263 path ? path : "<unknown>");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002264 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002265 ret = index_mem(istate, oid, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002266 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002267 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002268 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002269 ret = index_mem(istate, oid, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002270 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002271 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002272 return ret;
2273}
2274
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002275/*
Junio C Hamano568508e2011-10-28 14:48:40 -07002276 * This creates one packfile per large blob unless bulk-checkin
2277 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002278 *
2279 * This also bypasses the usual "convert-to-git" dance, and that is on
2280 * purpose. We could write a streaming version of the converting
2281 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05002282 * (or equivalent in-core API described above). However, that is
2283 * somewhat complicated, as we do not know the size of the filter
2284 * result, which we need to know beforehand when writing a git object.
2285 * Since the primary motivation for trying to stream from the working
2286 * tree file and to avoid mmaping it in core is to deal with large
2287 * binary blobs, they generally do not want to get any conversion, and
2288 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002289 */
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002290static int index_stream(struct object_id *oid, int fd, size_t size,
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002291 enum object_type type, const char *path,
2292 unsigned flags)
2293{
brian m. carlson68ee6df2018-03-12 02:27:21 +00002294 return index_bulk_checkin(oid, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002295}
2296
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002297int index_fd(struct index_state *istate, struct object_id *oid,
2298 int fd, struct stat *st,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002299 enum object_type type, const char *path, unsigned flags)
2300{
2301 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002302
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002303 /*
2304 * Call xsize_t() only when needed to avoid potentially unnecessary
2305 * die() for large files.
2306 */
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002307 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path))
2308 ret = index_stream_convert_blob(istate, oid, fd, path, flags);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002309 else if (!S_ISREG(st->st_mode))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002310 ret = index_pipe(istate, oid, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002311 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002312 (path && would_convert_to_git(istate, path)))
2313 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
2314 type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002315 else
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002316 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002317 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002318 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002319 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07002320}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002321
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002322int index_path(struct index_state *istate, struct object_id *oid,
2323 const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002324{
2325 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002326 struct strbuf sb = STRBUF_INIT;
Rene Scharfeea8e0292017-08-30 20:00:29 +02002327 int rc = 0;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002328
2329 switch (st->st_mode & S_IFMT) {
2330 case S_IFREG:
2331 fd = open(path, O_RDONLY);
2332 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002333 return error_errno("open(\"%s\")", path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002334 if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002335 return error(_("%s: failed to insert into database"),
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002336 path);
2337 break;
2338 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002339 if (strbuf_readlink(&sb, path, st->st_size))
2340 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002341 if (!(flags & HASH_WRITE_OBJECT))
Matheus Tavares2dcde202020-01-30 17:32:22 -03002342 hash_object_file(the_hash_algo, sb.buf, sb.len,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01002343 OBJ_BLOB, oid);
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002344 else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002345 rc = error(_("%s: failed to insert into database"), path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002346 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002347 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07002348 case S_IFDIR:
brian m. carlsona98e6102017-10-15 22:07:07 +00002349 return resolve_gitlink_ref(path, "HEAD", oid);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002350 default:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002351 return error(_("%s: unsupported file type"), path);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002352 }
Rene Scharfeea8e0292017-08-30 20:00:29 +02002353 return rc;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002354}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002355
2356int read_pack_header(int fd, struct pack_header *header)
2357{
Jeff Kingf48ecd32017-09-13 14:47:22 -04002358 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
Heikki Orsilac697ad12008-05-03 16:27:26 +03002359 /* "eof before pack header was fully read" */
2360 return PH_ERROR_EOF;
2361
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002362 if (header->hdr_signature != htonl(PACK_SIGNATURE))
2363 /* "protocol error (pack signature mismatch detected)" */
2364 return PH_ERROR_PACK_SIGNATURE;
2365 if (!pack_version_ok(header->hdr_version))
2366 /* "protocol error (pack version unsupported)" */
2367 return PH_ERROR_PROTOCOL;
2368 return 0;
2369}
Jeff King40d52ff2010-04-01 20:05:23 -04002370
brian m. carlsone816caa2018-03-12 02:27:42 +00002371void assert_oid_type(const struct object_id *oid, enum object_type expect)
Jeff King40d52ff2010-04-01 20:05:23 -04002372{
Stefan Beller0df8e962018-04-25 11:20:59 -07002373 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff King40d52ff2010-04-01 20:05:23 -04002374 if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002375 die(_("%s is not a valid object"), oid_to_hex(oid));
Jeff King40d52ff2010-04-01 20:05:23 -04002376 if (type != expect)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002377 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002378 type_name(expect));
Jeff King40d52ff2010-04-01 20:05:23 -04002379}
Jeff King27e1e222014-10-15 18:38:55 -04002380
René Scharfe70c49052017-06-24 16:09:39 +02002381int for_each_file_in_obj_subdir(unsigned int subdir_nr,
René Scharfecc817ca2017-06-22 20:19:48 +02002382 struct strbuf *path,
2383 each_loose_object_fn obj_cb,
2384 each_loose_cruft_fn cruft_cb,
2385 each_loose_subdir_fn subdir_cb,
2386 void *data)
Jeff King27e1e222014-10-15 18:38:55 -04002387{
René Scharfe0375f472017-06-24 14:12:30 +02002388 size_t origlen, baselen;
2389 DIR *dir;
Jeff King27e1e222014-10-15 18:38:55 -04002390 struct dirent *de;
2391 int r = 0;
René Scharfe62a24c82017-10-31 14:50:06 +01002392 struct object_id oid;
Jeff King27e1e222014-10-15 18:38:55 -04002393
René Scharfe70c49052017-06-24 16:09:39 +02002394 if (subdir_nr > 0xff)
2395 BUG("invalid loose object subdirectory: %x", subdir_nr);
2396
René Scharfe0375f472017-06-24 14:12:30 +02002397 origlen = path->len;
2398 strbuf_complete(path, '/');
2399 strbuf_addf(path, "%02x", subdir_nr);
René Scharfe0375f472017-06-24 14:12:30 +02002400
2401 dir = opendir(path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04002402 if (!dir) {
René Scharfe0375f472017-06-24 14:12:30 +02002403 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002404 r = error_errno(_("unable to open %s"), path->buf);
René Scharfe0375f472017-06-24 14:12:30 +02002405 strbuf_setlen(path, origlen);
2406 return r;
Jeff King27e1e222014-10-15 18:38:55 -04002407 }
2408
René Scharfe62a24c82017-10-31 14:50:06 +01002409 oid.hash[0] = subdir_nr;
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002410 strbuf_addch(path, '/');
2411 baselen = path->len;
René Scharfe62a24c82017-10-31 14:50:06 +01002412
Elijah Newrenb548f0f2021-05-12 17:28:22 +00002413 while ((de = readdir_skip_dot_and_dotdot(dir))) {
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002414 size_t namelen;
Jeff King27e1e222014-10-15 18:38:55 -04002415
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002416 namelen = strlen(de->d_name);
Jeff King27e1e222014-10-15 18:38:55 -04002417 strbuf_setlen(path, baselen);
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002418 strbuf_add(path, de->d_name, namelen);
brian m. carlson94b5e092018-07-16 01:28:07 +00002419 if (namelen == the_hash_algo->hexsz - 2 &&
René Scharfe62a24c82017-10-31 14:50:06 +01002420 !hex_to_bytes(oid.hash + 1, de->d_name,
brian m. carlson94b5e092018-07-16 01:28:07 +00002421 the_hash_algo->rawsz - 1)) {
brian m. carlson5a6dce72021-04-26 01:02:55 +00002422 oid_set_algo(&oid, the_hash_algo);
René Scharfe62a24c82017-10-31 14:50:06 +01002423 if (obj_cb) {
2424 r = obj_cb(&oid, path->buf, data);
2425 if (r)
2426 break;
Jeff King27e1e222014-10-15 18:38:55 -04002427 }
René Scharfe62a24c82017-10-31 14:50:06 +01002428 continue;
Jeff King27e1e222014-10-15 18:38:55 -04002429 }
2430
2431 if (cruft_cb) {
2432 r = cruft_cb(de->d_name, path->buf, data);
2433 if (r)
2434 break;
2435 }
2436 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02002437 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04002438
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002439 strbuf_setlen(path, baselen - 1);
Jeff King27e1e222014-10-15 18:38:55 -04002440 if (!r && subdir_cb)
2441 r = subdir_cb(subdir_nr, path->buf, data);
2442
René Scharfe0375f472017-06-24 14:12:30 +02002443 strbuf_setlen(path, origlen);
2444
Jeff King27e1e222014-10-15 18:38:55 -04002445 return r;
2446}
2447
Jeff Kinge6f875e2015-02-08 20:13:22 -05002448int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04002449 each_loose_object_fn obj_cb,
2450 each_loose_cruft_fn cruft_cb,
2451 each_loose_subdir_fn subdir_cb,
2452 void *data)
2453{
Jeff King27e1e222014-10-15 18:38:55 -04002454 int r = 0;
2455 int i;
2456
Jeff King27e1e222014-10-15 18:38:55 -04002457 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05002458 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04002459 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002460 if (r)
2461 break;
2462 }
2463
Jeff Kinge6f875e2015-02-08 20:13:22 -05002464 return r;
2465}
2466
2467int for_each_loose_file_in_objdir(const char *path,
2468 each_loose_object_fn obj_cb,
2469 each_loose_cruft_fn cruft_cb,
2470 each_loose_subdir_fn subdir_cb,
2471 void *data)
2472{
2473 struct strbuf buf = STRBUF_INIT;
2474 int r;
2475
2476 strbuf_addstr(&buf, path);
2477 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
2478 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002479 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05002480
Jeff King27e1e222014-10-15 18:38:55 -04002481 return r;
2482}
Jeff King660c8892014-10-15 18:41:21 -04002483
Jeff Kinga7ff6f52018-08-10 19:09:44 -04002484int for_each_loose_object(each_loose_object_fn cb, void *data,
2485 enum for_each_object_flags flags)
Jeff King660c8892014-10-15 18:41:21 -04002486{
Jeff Kingf0eaf632018-11-12 09:50:39 -05002487 struct object_directory *odb;
Jeff King660c8892014-10-15 18:41:21 -04002488
Jeff Kingf0eaf632018-11-12 09:50:39 -05002489 prepare_alt_odb(the_repository);
2490 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
2491 int r = for_each_loose_file_in_objdir(odb->path, cb, NULL,
2492 NULL, data);
2493 if (r)
2494 return r;
Jeff King660c8892014-10-15 18:41:21 -04002495
Jeff Kingf0eaf632018-11-12 09:50:39 -05002496 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
2497 break;
2498 }
Jeff King1385bb72015-03-27 07:32:41 -04002499
Jeff Kingf0eaf632018-11-12 09:50:39 -05002500 return 0;
Jeff King660c8892014-10-15 18:41:21 -04002501}
2502
Jeff King3a2e0822018-11-12 09:50:56 -05002503static int append_loose_object(const struct object_id *oid, const char *path,
2504 void *data)
2505{
Eric Wong92d8ed82021-07-07 23:10:19 +00002506 oidtree_insert(data, oid);
Jeff King3a2e0822018-11-12 09:50:56 -05002507 return 0;
2508}
2509
Eric Wong92d8ed82021-07-07 23:10:19 +00002510struct oidtree *odb_loose_cache(struct object_directory *odb,
René Scharfe0000d652019-01-06 17:45:30 +01002511 const struct object_id *oid)
2512{
2513 int subdir_nr = oid->hash[0];
Jeff King3a2e0822018-11-12 09:50:56 -05002514 struct strbuf buf = STRBUF_INIT;
Eric Wong33f379e2021-07-07 23:10:17 +00002515 size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]);
2516 size_t word_index = subdir_nr / word_bits;
Philip Oakley26de1fc2021-12-01 00:29:02 +00002517 size_t mask = (size_t)1u << (subdir_nr % word_bits);
Eric Wong33f379e2021-07-07 23:10:17 +00002518 uint32_t *bitmap;
Jeff King3a2e0822018-11-12 09:50:56 -05002519
2520 if (subdir_nr < 0 ||
Eric Wong33f379e2021-07-07 23:10:17 +00002521 subdir_nr >= bitsizeof(odb->loose_objects_subdir_seen))
Jeff King3a2e0822018-11-12 09:50:56 -05002522 BUG("subdir_nr out of range");
2523
Eric Wong33f379e2021-07-07 23:10:17 +00002524 bitmap = &odb->loose_objects_subdir_seen[word_index];
2525 if (*bitmap & mask)
Eric Wong92d8ed82021-07-07 23:10:19 +00002526 return odb->loose_objects_cache;
2527 if (!odb->loose_objects_cache) {
2528 ALLOC_ARRAY(odb->loose_objects_cache, 1);
2529 oidtree_init(odb->loose_objects_cache);
2530 }
Jeff King3a2e0822018-11-12 09:50:56 -05002531 strbuf_addstr(&buf, odb->path);
2532 for_each_file_in_obj_subdir(subdir_nr, &buf,
2533 append_loose_object,
2534 NULL, NULL,
Eric Wong92d8ed82021-07-07 23:10:19 +00002535 odb->loose_objects_cache);
Eric Wong33f379e2021-07-07 23:10:17 +00002536 *bitmap |= mask;
Jeff King7317aa72018-11-22 12:53:00 -05002537 strbuf_release(&buf);
Eric Wong92d8ed82021-07-07 23:10:19 +00002538 return odb->loose_objects_cache;
Jeff King660c8892014-10-15 18:41:21 -04002539}
2540
René Scharfed4e19e52019-01-06 17:45:39 +01002541void odb_clear_loose_cache(struct object_directory *odb)
2542{
Eric Wong92d8ed82021-07-07 23:10:19 +00002543 oidtree_clear(odb->loose_objects_cache);
2544 FREE_AND_NULL(odb->loose_objects_cache);
René Scharfed4e19e52019-01-06 17:45:39 +01002545 memset(&odb->loose_objects_subdir_seen, 0,
2546 sizeof(odb->loose_objects_subdir_seen));
2547}
2548
Jeff King00a77602019-01-07 03:37:02 -05002549static int check_stream_oid(git_zstream *stream,
2550 const char *hdr,
2551 unsigned long size,
2552 const char *path,
2553 const struct object_id *expected_oid)
Jeff Kingf6371f92017-01-13 12:58:16 -05002554{
brian m. carlson18e25882018-02-01 02:18:41 +00002555 git_hash_ctx c;
Jeff King00a77602019-01-07 03:37:02 -05002556 struct object_id real_oid;
Jeff Kingf6371f92017-01-13 12:58:16 -05002557 unsigned char buf[4096];
2558 unsigned long total_read;
2559 int status = Z_OK;
2560
brian m. carlson18e25882018-02-01 02:18:41 +00002561 the_hash_algo->init_fn(&c);
2562 the_hash_algo->update_fn(&c, hdr, stream->total_out);
Jeff Kingf6371f92017-01-13 12:58:16 -05002563
2564 /*
2565 * We already read some bytes into hdr, but the ones up to the NUL
2566 * do not count against the object's content size.
2567 */
2568 total_read = stream->total_out - strlen(hdr) - 1;
2569
2570 /*
2571 * This size comparison must be "<=" to read the final zlib packets;
Jeff King00a77602019-01-07 03:37:02 -05002572 * see the comment in unpack_loose_rest for details.
Jeff Kingf6371f92017-01-13 12:58:16 -05002573 */
2574 while (total_read <= size &&
Junio C Hamano18ad13e2018-10-31 13:12:12 +09002575 (status == Z_OK ||
2576 (status == Z_BUF_ERROR && !stream->avail_out))) {
Jeff Kingf6371f92017-01-13 12:58:16 -05002577 stream->next_out = buf;
2578 stream->avail_out = sizeof(buf);
2579 if (size - total_read < stream->avail_out)
2580 stream->avail_out = size - total_read;
2581 status = git_inflate(stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00002582 the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
Jeff Kingf6371f92017-01-13 12:58:16 -05002583 total_read += stream->next_out - buf;
2584 }
2585 git_inflate_end(stream);
2586
2587 if (status != Z_STREAM_END) {
Jeff King00a77602019-01-07 03:37:02 -05002588 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002589 return -1;
2590 }
Jeff Kingcce044d2017-01-13 13:00:25 -05002591 if (stream->avail_in) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002592 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05002593 oid_to_hex(expected_oid));
Jeff Kingcce044d2017-01-13 13:00:25 -05002594 return -1;
2595 }
Jeff Kingf6371f92017-01-13 12:58:16 -05002596
brian m. carlson5951bf42021-04-26 01:02:53 +00002597 the_hash_algo->final_oid_fn(&real_oid, &c);
Jeff King00a77602019-01-07 03:37:02 -05002598 if (!oideq(expected_oid, &real_oid)) {
Jeff King01f8d592019-01-07 03:40:34 -05002599 error(_("hash mismatch for %s (expected %s)"), path,
Jeff King00a77602019-01-07 03:37:02 -05002600 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002601 return -1;
2602 }
2603
2604 return 0;
2605}
2606
2607int read_loose_object(const char *path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002608 const struct object_id *expected_oid,
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02002609 struct object_id *real_oid,
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002610 void **contents,
2611 struct object_info *oi)
Jeff Kingf6371f92017-01-13 12:58:16 -05002612{
2613 int ret = -1;
Jeff Kingf6371f92017-01-13 12:58:16 -05002614 void *map = NULL;
2615 unsigned long mapsize;
2616 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00002617 char hdr[MAX_HEADER_LEN];
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002618 unsigned long *size = oi->sizep;
Jeff Kingf6371f92017-01-13 12:58:16 -05002619
Jeff King514c5fd2019-01-07 03:35:42 -05002620 map = map_loose_object_1(the_repository, path, NULL, &mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002621 if (!map) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002622 error_errno(_("unable to mmap %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002623 goto out;
2624 }
2625
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02002626 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
2627 NULL) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002628 error(_("unable to unpack header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002629 goto out;
2630 }
2631
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002632 if (parse_loose_header(hdr, oi) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002633 error(_("unable to parse header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002634 git_inflate_end(&stream);
2635 goto out;
2636 }
2637
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002638 if (*oi->typep == OBJ_BLOB && *size > big_file_threshold) {
Jeff King00a77602019-01-07 03:37:02 -05002639 if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
Jeff Kingf6371f92017-01-13 12:58:16 -05002640 goto out;
2641 } else {
Jeff King00a77602019-01-07 03:37:02 -05002642 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
Jeff Kingf6371f92017-01-13 12:58:16 -05002643 if (!*contents) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002644 error(_("unable to unpack contents of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002645 git_inflate_end(&stream);
2646 goto out;
2647 }
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01002648 hash_object_file_literally(the_repository->hash_algo,
Ævar Arnfjörð Bjarmason16235e32021-11-11 06:18:56 +01002649 *contents, *size,
Ævar Arnfjörð Bjarmason44439c12022-02-05 00:48:32 +01002650 oi->type_name->buf, real_oid);
Ævar Arnfjörð Bjarmason0f156db2022-02-05 00:48:30 +01002651 if (!oideq(expected_oid, real_oid))
Jeff Kingf6371f92017-01-13 12:58:16 -05002652 goto out;
Jeff Kingf6371f92017-01-13 12:58:16 -05002653 }
2654
2655 ret = 0; /* everything checks out */
2656
2657out:
2658 if (map)
2659 munmap(map, mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002660 return ret;
2661}