blob: 271acf4dd1573538edf163fa2b312fbd8cdd9e72 [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 {
170 NULL,
171 0x00000000,
172 0,
173 0,
brian m. carlsona2ce0a72018-11-14 04:09:33 +0000174 0,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000175 git_hash_unknown_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000176 git_hash_unknown_clone,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000177 git_hash_unknown_update,
178 git_hash_unknown_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000179 git_hash_unknown_final_oid,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000180 NULL,
181 NULL,
brian m. carlson14228442021-04-26 01:02:56 +0000182 NULL,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000183 },
184 {
brian m. carlson1ccf07c2018-10-22 02:43:31 +0000185 "sha1",
Han-Wen Nienhuys27f37962021-08-30 14:57:37 +0000186 GIT_SHA1_FORMAT_ID,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000187 GIT_SHA1_RAWSZ,
188 GIT_SHA1_HEXSZ,
brian m. carlsona2ce0a72018-11-14 04:09:33 +0000189 GIT_SHA1_BLKSZ,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000190 git_hash_sha1_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000191 git_hash_sha1_clone,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000192 git_hash_sha1_update,
193 git_hash_sha1_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000194 git_hash_sha1_final_oid,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000195 &empty_tree_oid,
196 &empty_blob_oid,
brian m. carlson14228442021-04-26 01:02:56 +0000197 &null_oid_sha1,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000198 },
brian m. carlson13eeedb2018-11-14 04:09:36 +0000199 {
200 "sha256",
Han-Wen Nienhuys27f37962021-08-30 14:57:37 +0000201 GIT_SHA256_FORMAT_ID,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000202 GIT_SHA256_RAWSZ,
203 GIT_SHA256_HEXSZ,
204 GIT_SHA256_BLKSZ,
205 git_hash_sha256_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000206 git_hash_sha256_clone,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000207 git_hash_sha256_update,
208 git_hash_sha256_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000209 git_hash_sha256_final_oid,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000210 &empty_tree_oid_sha256,
211 &empty_blob_oid_sha256,
brian m. carlson14228442021-04-26 01:02:56 +0000212 &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 = {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000277 { EMPTY_TREE_SHA1_BIN_LITERAL },
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700278 OBJ_TREE,
279 "",
280 0
281};
282
brian m. carlson62ba93e2018-05-02 00:26:03 +0000283static struct cached_object *find_cached_object(const struct object_id *oid)
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700284{
285 int i;
286 struct cached_object *co = cached_objects;
287
288 for (i = 0; i < cached_object_nr; i++, co++) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400289 if (oideq(&co->oid, oid))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700290 return co;
291 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400292 if (oideq(oid, the_hash_algo->empty_tree))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700293 return &empty_tree;
294 return NULL;
295}
296
Torsten Bögershausen94729352017-11-16 17:38:28 +0100297
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100298static int get_conv_flags(unsigned flags)
Torsten Bögershausen94729352017-11-16 17:38:28 +0100299{
300 if (flags & HASH_RENORMALIZE)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100301 return CONV_EOL_RENORMALIZE;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100302 else if (flags & HASH_WRITE_OBJECT)
Lars Schneider107642f2018-04-15 20:16:07 +0200303 return global_conv_flags_eol | CONV_WRITE_OBJECT;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100304 else
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100305 return 0;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100306}
307
308
Junio C Hamano90a64642011-03-10 16:02:50 -0800309int mkdir_in_gitdir(const char *path)
310{
311 if (mkdir(path, 0777)) {
312 int saved_errno = errno;
313 struct stat st;
314 struct strbuf sb = STRBUF_INIT;
315
316 if (errno != EEXIST)
317 return -1;
318 /*
319 * Are we looking at a path in a symlinked worktree
320 * whose original repository does not yet have it?
321 * e.g. .git/rr-cache pointing at its original
322 * repository in which the user hasn't performed any
323 * conflict resolution yet?
324 */
325 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
326 strbuf_readlink(&sb, path, st.st_size) ||
327 !is_absolute_path(sb.buf) ||
328 mkdir(sb.buf, 0777)) {
329 strbuf_release(&sb);
330 errno = saved_errno;
331 return -1;
332 }
333 strbuf_release(&sb);
334 }
335 return adjust_shared_perm(path);
336}
337
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300338static enum scld_error safe_create_leading_directories_1(char *path, int share)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700339{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100340 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100341 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800342
Michael Haggerty0be05212014-01-06 14:45:25 +0100343 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100344 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100345 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100346
Michael Haggerty0f527402014-01-19 00:40:44 +0100347 while (*slash && !is_dir_sep(*slash))
348 slash++;
349
350 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700351 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100352
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100353 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100354 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100355 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100356 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700357 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100358
Michael Haggerty0f527402014-01-19 00:40:44 +0100359 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100360 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800361 if (!stat(path, &st)) {
362 /* path exists */
Michael Haggerty204a0472017-01-06 17:22:25 +0100363 if (!S_ISDIR(st.st_mode)) {
364 errno = ENOTDIR;
Michael Haggerty0be05212014-01-06 14:45:25 +0100365 ret = SCLD_EXISTS;
Michael Haggerty204a0472017-01-06 17:22:25 +0100366 }
Michael Haggerty53a39722014-01-06 14:45:19 +0100367 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400368 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100369 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400370 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100371 else if (errno == ENOENT)
372 /*
373 * Either mkdir() failed because
374 * somebody just pruned the containing
375 * directory, or stat() failed because
376 * the file that was in our way was
377 * just removed. Either way, inform
378 * the caller that it might be worth
379 * trying again:
380 */
381 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100382 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100383 ret = SCLD_FAILED;
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300384 } else if (share && adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100385 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800386 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100387 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700388 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100389 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700390}
391
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300392enum scld_error safe_create_leading_directories(char *path)
393{
394 return safe_create_leading_directories_1(path, 1);
395}
396
397enum scld_error safe_create_leading_directories_no_share(char *path)
398{
399 return safe_create_leading_directories_1(path, 0);
400}
401
Michael Haggerty0be05212014-01-06 14:45:25 +0100402enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400403{
Michael Haggerty02944302017-01-06 17:22:24 +0100404 int save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400405 /* path points to cache entries, so xstrdup before messing with it */
406 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100407 enum scld_error result = safe_create_leading_directories(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100408
409 save_errno = errno;
Jeff King8e21d632008-06-25 01:41:34 -0400410 free(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100411 errno = save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400412 return result;
413}
414
Jeff King514c5fd2019-01-07 03:35:42 -0500415static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
Junio C Hamanoace15342005-05-07 00:38:04 -0700416{
417 int i;
brian m. carlson94b5e092018-07-16 01:28:07 +0000418 for (i = 0; i < the_hash_algo->rawsz; i++) {
Junio C Hamanoace15342005-05-07 00:38:04 -0700419 static char hex[] = "0123456789abcdef";
Jeff King514c5fd2019-01-07 03:35:42 -0500420 unsigned int val = oid->hash[i];
Jeff Kingf7b77742016-10-03 16:36:09 -0400421 strbuf_addch(buf, hex[val >> 4]);
422 strbuf_addch(buf, hex[val & 0xf]);
Jeff Kingafbba2f2016-10-03 16:35:55 -0400423 if (!i)
Jeff Kingf7b77742016-10-03 16:36:09 -0400424 strbuf_addch(buf, '/');
Junio C Hamanoace15342005-05-07 00:38:04 -0700425 }
426}
427
Jeff Kingf0eaf632018-11-12 09:50:39 -0500428static const char *odb_loose_path(struct object_directory *odb,
429 struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500430 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700431{
Jeff Kingb69fb862018-11-12 09:48:56 -0500432 strbuf_reset(buf);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500433 strbuf_addstr(buf, odb->path);
Christian Couder34498472018-01-18 11:08:54 +0100434 strbuf_addch(buf, '/');
Jeff King514c5fd2019-01-07 03:35:42 -0500435 fill_loose_path(buf, oid);
Jeff King38dbe5f2016-10-03 16:36:04 -0400436 return buf->buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700437}
438
Jeff Kingf3f043a2018-11-12 09:49:35 -0500439const char *loose_object_path(struct repository *r, struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500440 const struct object_id *oid)
Jeff Kingf3f043a2018-11-12 09:49:35 -0500441{
Jeff King514c5fd2019-01-07 03:35:42 -0500442 return odb_loose_path(r->objects->odb, buf, oid);
Jeff Kingf3f043a2018-11-12 09:49:35 -0500443}
444
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700445/*
Jeff King4ea82472016-10-03 16:34:48 -0400446 * Return non-zero iff the path is usable as an alternate object database.
447 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100448static int alt_odb_usable(struct raw_object_store *o,
449 struct strbuf *path,
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000450 const char *normalized_objdir, khiter_t *pos)
Jeff King4ea82472016-10-03 16:34:48 -0400451{
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000452 int r;
Jeff King4ea82472016-10-03 16:34:48 -0400453
454 /* Detect cases where alternate disappeared */
455 if (!is_directory(path->buf)) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200456 error(_("object directory %s does not exist; "
457 "check .git/objects/info/alternates"),
Jeff King4ea82472016-10-03 16:34:48 -0400458 path->buf);
459 return 0;
460 }
461
462 /*
463 * Prevent the common mistake of listing the same
464 * thing twice, or object directory itself.
465 */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000466 if (!o->odb_by_path) {
467 khiter_t p;
Jeff King4ea82472016-10-03 16:34:48 -0400468
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000469 o->odb_by_path = kh_init_odb_path_map();
470 assert(!o->odb->next);
471 p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r);
472 assert(r == 1); /* never used */
473 kh_value(o->odb_by_path, p) = o->odb;
474 }
475 if (fspatheq(path->buf, normalized_objdir))
476 return 0;
477 *pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r);
478 /* r: 0 = exists, 1 = never used, 2 = deleted */
479 return r == 0 ? 0 : 1;
Jeff King4ea82472016-10-03 16:34:48 -0400480}
481
482/*
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700483 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700484 *
485 * The variable alt_odb_list points at the list of struct
Jeff King263db402018-11-12 09:48:47 -0500486 * object_directory. The elements on this list come from
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700487 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
488 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800489 * whose contents is similar to that environment variable but can be
490 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700491 * contains "/the/directory/corresponding/to/.git/objects/...", while
492 * its name points just after the slash at the end of ".git/objects/"
Martin Ågrene5afd442020-12-31 12:56:21 +0100493 * in the example above, and has enough space to hold all hex characters
494 * of the object ID, an extra slash for the first level indirection, and
495 * the terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700496 */
Stefan Beller77f012e2018-03-23 18:21:08 +0100497static void read_info_alternates(struct repository *r,
498 const char *relative_base,
499 int depth);
Eric Wong407532f2021-07-07 23:10:16 +0000500static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100501 const char *relative_base, int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200502{
Jeff King263db402018-11-12 09:48:47 -0500503 struct object_directory *ent;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800504 struct strbuf pathbuf = STRBUF_INIT;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000505 khiter_t pos;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200506
Eric Wong407532f2021-07-07 23:10:16 +0000507 if (!is_absolute_path(entry->buf) && relative_base) {
Brandon Williams4ac90062016-12-12 10:16:55 -0800508 strbuf_realpath(&pathbuf, relative_base, 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800509 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200510 }
Eric Wong407532f2021-07-07 23:10:16 +0000511 strbuf_addbuf(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800512
Jeff King37a95862016-11-07 23:50:17 -0500513 if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200514 error(_("unable to normalize alternate object path: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400515 pathbuf.buf);
516 strbuf_release(&pathbuf);
517 return -1;
518 }
Hui Wang5bdf0a82011-09-07 18:37:47 +0800519
520 /*
521 * The trailing slash after the directory name is given by
522 * this function at the end. Remove duplicates.
523 */
Jeff King4ea82472016-10-03 16:34:48 -0400524 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
525 strbuf_setlen(&pathbuf, pathbuf.len - 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800526
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000527 if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) {
Jeff King4ea82472016-10-03 16:34:48 -0400528 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200529 return -1;
530 }
531
René Scharfeca56dad2021-03-13 17:17:22 +0100532 CALLOC_ARRAY(ent, 1);
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000533 /* pathbuf.buf is already in r->objects->odb_by_path */
534 ent->path = strbuf_detach(&pathbuf, NULL);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200535
536 /* add the alternate entry */
Jeff Kingf0eaf632018-11-12 09:50:39 -0500537 *r->objects->odb_tail = ent;
538 r->objects->odb_tail = &(ent->next);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200539 ent->next = NULL;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000540 assert(r->objects->odb_by_path);
541 kh_value(r->objects->odb_by_path, pos) = ent;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200542
543 /* recursively add alternates */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000544 read_info_alternates(r, ent->path, depth + 1);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200545
Martin Waitzc2f493a2006-05-07 20:19:21 +0200546 return 0;
547}
548
Jeff Kingcf3c6352016-12-12 14:52:22 -0500549static const char *parse_alt_odb_entry(const char *string,
550 int sep,
551 struct strbuf *out)
552{
553 const char *end;
554
555 strbuf_reset(out);
556
557 if (*string == '#') {
558 /* comment; consume up to next separator */
559 end = strchrnul(string, sep);
560 } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
561 /*
562 * quoted path; unquote_c_style has copied the
563 * data for us and set "end". Broken quoting (e.g.,
564 * an entry that doesn't end with a quote) falls
565 * back to the unquoted case below.
566 */
567 } else {
568 /* normal, unquoted path */
569 end = strchrnul(string, sep);
570 strbuf_add(out, string, end - string);
571 }
572
573 if (*end)
574 end++;
575 return end;
576}
577
Stefan Beller77f012e2018-03-23 18:21:08 +0100578static void link_alt_odb_entries(struct repository *r, const char *alt,
579 int sep, const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700580{
Ephrim Khong539e7502014-07-15 13:29:45 +0200581 struct strbuf objdirbuf = STRBUF_INIT;
Jeff Kingcf3c6352016-12-12 14:52:22 -0500582 struct strbuf entry = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200583
Jeff Kingf28e3662017-11-12 10:27:39 +0000584 if (!alt || !*alt)
585 return;
586
Martin Waitzc2f493a2006-05-07 20:19:21 +0200587 if (depth > 5) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200588 error(_("%s: ignoring alternate object stores, nesting too deep"),
Martin Waitzc2f493a2006-05-07 20:19:21 +0200589 relative_base);
590 return;
591 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700592
Jeff Kingf0eaf632018-11-12 09:50:39 -0500593 strbuf_add_absolute_path(&objdirbuf, r->objects->odb->path);
Jeff King670c3592016-10-03 16:34:17 -0400594 if (strbuf_normalize_path(&objdirbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200595 die(_("unable to normalize object directory: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400596 objdirbuf.buf);
Ephrim Khong539e7502014-07-15 13:29:45 +0200597
Jeff Kingcf3c6352016-12-12 14:52:22 -0500598 while (*alt) {
599 alt = parse_alt_odb_entry(alt, sep, &entry);
600 if (!entry.len)
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700601 continue;
Eric Wong407532f2021-07-07 23:10:16 +0000602 link_alt_odb_entry(r, &entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100603 relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700604 }
Jeff Kingcf3c6352016-12-12 14:52:22 -0500605 strbuf_release(&entry);
Ephrim Khong539e7502014-07-15 13:29:45 +0200606 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700607}
608
Stefan Beller77f012e2018-03-23 18:21:08 +0100609static void read_info_alternates(struct repository *r,
610 const char *relative_base,
611 int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700612{
Jeff King5015f012015-08-19 14:12:45 -0400613 char *path;
Jeff Kingdc732bd2017-09-19 15:41:07 -0400614 struct strbuf buf = STRBUF_INIT;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700615
Jeff King5015f012015-08-19 14:12:45 -0400616 path = xstrfmt("%s/info/alternates", relative_base);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400617 if (strbuf_read_file(&buf, path, 1024) < 0) {
Jeff Kingf0f7beb2017-09-19 15:41:10 -0400618 warn_on_fopen_errors(path);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400619 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700620 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700621 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700622
Stefan Beller77f012e2018-03-23 18:21:08 +0100623 link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400624 strbuf_release(&buf);
625 free(path);
Junio C Hamanoace15342005-05-07 00:38:04 -0700626}
627
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400628void add_to_alternates_file(const char *reference)
629{
Martin Ågrenf132a122017-10-05 22:32:03 +0200630 struct lock_file lock = LOCK_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400631 char *alts = git_pathdup("objects/info/alternates");
632 FILE *in, *out;
Martin Ågrenf132a122017-10-05 22:32:03 +0200633 int found = 0;
Jeff King77b9b1d2015-08-10 05:34:46 -0400634
Martin Ågrenf132a122017-10-05 22:32:03 +0200635 hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
636 out = fdopen_lock_file(&lock, "w");
Jeff King77b9b1d2015-08-10 05:34:46 -0400637 if (!out)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200638 die_errno(_("unable to fdopen alternates lockfile"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400639
640 in = fopen(alts, "r");
641 if (in) {
642 struct strbuf line = STRBUF_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400643
Junio C Hamano3f163962015-10-28 13:29:24 -0700644 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400645 if (!strcmp(reference, line.buf)) {
646 found = 1;
647 break;
648 }
649 fprintf_or_die(out, "%s\n", line.buf);
650 }
651
652 strbuf_release(&line);
653 fclose(in);
Jeff King77b9b1d2015-08-10 05:34:46 -0400654 }
655 else if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200656 die_errno(_("unable to read alternates file"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400657
Martin Ågrenf132a122017-10-05 22:32:03 +0200658 if (found) {
659 rollback_lock_file(&lock);
660 } else {
Jeff King77b9b1d2015-08-10 05:34:46 -0400661 fprintf_or_die(out, "%s\n", reference);
Martin Ågrenf132a122017-10-05 22:32:03 +0200662 if (commit_lock_file(&lock))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200663 die_errno(_("unable to move new alternates file into place"));
Jeff Kingf0eaf632018-11-12 09:50:39 -0500664 if (the_repository->objects->loaded_alternates)
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100665 link_alt_odb_entries(the_repository, reference,
666 '\n', NULL, 0);
Jeff King77b9b1d2015-08-10 05:34:46 -0400667 }
668 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400669}
670
Jeff Kinga5b34d22016-10-03 16:35:03 -0400671void add_to_alternates_memory(const char *reference)
672{
673 /*
674 * Make sure alternates are initialized, or else our entry may be
675 * overwritten when they are.
676 */
Stefan Beller0b209032018-03-23 18:21:07 +0100677 prepare_alt_odb(the_repository);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400678
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100679 link_alt_odb_entries(the_repository, reference,
680 '\n', NULL, 0);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400681}
682
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000683struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy)
684{
685 struct object_directory *new_odb;
686
687 /*
688 * Make sure alternates are initialized, or else our entry may be
689 * overwritten when they are.
690 */
691 prepare_alt_odb(the_repository);
692
693 /*
694 * Make a new primary odb and link the old primary ODB in as an
695 * alternate
696 */
697 new_odb = xcalloc(1, sizeof(*new_odb));
698 new_odb->path = xstrdup(dir);
Neeraj Singhecd81df2021-12-06 22:05:05 +0000699
700 /*
701 * Disable ref updates while a temporary odb is active, since
702 * the objects in the database may roll back.
703 */
704 new_odb->disable_ref_updates = 1;
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000705 new_odb->will_destroy = will_destroy;
706 new_odb->next = the_repository->objects->odb;
707 the_repository->objects->odb = new_odb;
708 return new_odb->next;
709}
710
711void restore_primary_odb(struct object_directory *restore_odb, const char *old_path)
712{
713 struct object_directory *cur_odb = the_repository->objects->odb;
714
715 if (strcmp(old_path, cur_odb->path))
716 BUG("expected %s as primary object store; found %s",
717 old_path, cur_odb->path);
718
719 if (cur_odb->next != restore_odb)
720 BUG("we expect the old primary object store to be the first alternate");
721
722 the_repository->objects->odb = restore_odb;
723 free_object_directory(cur_odb);
724}
725
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700726/*
727 * Compute the exact path an alternate is at and returns it. In case of
728 * error NULL is returned and the human readable error is added to `err`
Robert P. J. Dayefde7b72018-06-03 10:32:50 -0400729 * `path` may be relative and should point to $GIT_DIR.
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700730 * `err` must not be null.
731 */
732char *compute_alternate_path(const char *path, struct strbuf *err)
733{
734 char *ref_git = NULL;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000735 const char *repo;
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700736 int seen_error = 0;
737
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000738 ref_git = real_pathdup(path, 0);
739 if (!ref_git) {
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700740 seen_error = 1;
741 strbuf_addf(err, _("path '%s' does not exist"), path);
742 goto out;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000743 }
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700744
745 repo = read_gitfile(ref_git);
746 if (!repo)
747 repo = read_gitfile(mkpath("%s/.git", ref_git));
748 if (repo) {
749 free(ref_git);
750 ref_git = xstrdup(repo);
751 }
752
753 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
754 char *ref_git_git = mkpathdup("%s/.git", ref_git);
755 free(ref_git);
756 ref_git = ref_git_git;
757 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
758 struct strbuf sb = STRBUF_INIT;
759 seen_error = 1;
760 if (get_common_dir(&sb, ref_git)) {
761 strbuf_addf(err,
762 _("reference repository '%s' as a linked "
763 "checkout is not supported yet."),
764 path);
765 goto out;
766 }
767
768 strbuf_addf(err, _("reference repository '%s' is not a "
769 "local repository."), path);
770 goto out;
771 }
772
773 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
774 strbuf_addf(err, _("reference repository '%s' is shallow"),
775 path);
776 seen_error = 1;
777 goto out;
778 }
779
780 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
781 strbuf_addf(err,
782 _("reference repository '%s' is grafted"),
783 path);
784 seen_error = 1;
785 goto out;
786 }
787
788out:
789 if (seen_error) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000790 FREE_AND_NULL(ref_git);
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700791 }
792
793 return ref_git;
794}
795
Taylor Blauf57a7392021-09-01 16:34:01 -0400796struct object_directory *find_odb(struct repository *r, const char *obj_dir)
797{
798 struct object_directory *odb;
799 char *obj_dir_real = real_pathdup(obj_dir, 1);
800 struct strbuf odb_path_real = STRBUF_INIT;
801
802 prepare_alt_odb(r);
803 for (odb = r->objects->odb; odb; odb = odb->next) {
804 strbuf_realpath(&odb_path_real, odb->path, 1);
805 if (!strcmp(obj_dir_real, odb_path_real.buf))
806 break;
807 }
808
809 free(obj_dir_real);
810 strbuf_release(&odb_path_real);
811
812 if (!odb)
813 die(_("could not find object directory matching %s"), obj_dir);
814 return odb;
815}
816
Jeff King709dfa62019-07-01 09:17:40 -0400817static void fill_alternate_refs_command(struct child_process *cmd,
818 const char *repo_path)
819{
820 const char *value;
821
822 if (!git_config_get_value("core.alternateRefsCommand", &value)) {
823 cmd->use_shell = 1;
824
Jeff Kingc972bf42020-07-28 16:25:12 -0400825 strvec_push(&cmd->args, value);
826 strvec_push(&cmd->args, repo_path);
Jeff King709dfa62019-07-01 09:17:40 -0400827 } else {
828 cmd->git_cmd = 1;
829
Jeff Kingc972bf42020-07-28 16:25:12 -0400830 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
831 strvec_push(&cmd->args, "for-each-ref");
832 strvec_push(&cmd->args, "--format=%(objectname)");
Jeff King709dfa62019-07-01 09:17:40 -0400833
834 if (!git_config_get_value("core.alternateRefsPrefixes", &value)) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400835 strvec_push(&cmd->args, "--");
836 strvec_split(&cmd->args, value);
Jeff King709dfa62019-07-01 09:17:40 -0400837 }
838 }
839
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100840 strvec_pushv(&cmd->env_array, (const char **)local_repo_env);
Jeff King709dfa62019-07-01 09:17:40 -0400841 cmd->out = -1;
842}
843
844static void read_alternate_refs(const char *path,
845 alternate_ref_fn *cb,
846 void *data)
847{
848 struct child_process cmd = CHILD_PROCESS_INIT;
849 struct strbuf line = STRBUF_INIT;
850 FILE *fh;
851
852 fill_alternate_refs_command(&cmd, path);
853
854 if (start_command(&cmd))
855 return;
856
857 fh = xfdopen(cmd.out, "r");
858 while (strbuf_getline_lf(&line, fh) != EOF) {
859 struct object_id oid;
860 const char *p;
861
862 if (parse_oid_hex(line.buf, &oid, &p) || *p) {
863 warning(_("invalid line while parsing alternate refs: %s"),
864 line.buf);
865 break;
866 }
867
868 cb(&oid, data);
869 }
870
871 fclose(fh);
872 finish_command(&cmd);
René Scharfe86ad3ea2019-08-07 13:15:25 +0200873 strbuf_release(&line);
Jeff King709dfa62019-07-01 09:17:40 -0400874}
875
876struct alternate_refs_data {
877 alternate_ref_fn *fn;
878 void *data;
879};
880
881static int refs_from_alternate_cb(struct object_directory *e,
882 void *data)
883{
884 struct strbuf path = STRBUF_INIT;
885 size_t base_len;
886 struct alternate_refs_data *cb = data;
887
888 if (!strbuf_realpath(&path, e->path, 0))
889 goto out;
890 if (!strbuf_strip_suffix(&path, "/objects"))
891 goto out;
892 base_len = path.len;
893
894 /* Is this a git repository with refs? */
895 strbuf_addstr(&path, "/refs");
896 if (!is_directory(path.buf))
897 goto out;
898 strbuf_setlen(&path, base_len);
899
900 read_alternate_refs(path.buf, cb->fn, cb->data);
901
902out:
903 strbuf_release(&path);
904 return 0;
905}
906
907void for_each_alternate_ref(alternate_ref_fn fn, void *data)
908{
909 struct alternate_refs_data cb;
910 cb.fn = fn;
911 cb.data = data;
912 foreach_alt_odb(refs_from_alternate_cb, &cb);
913}
914
Jeff Kingfe1b2262014-10-15 18:33:13 -0400915int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700916{
Jeff King263db402018-11-12 09:48:47 -0500917 struct object_directory *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400918 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700919
Stefan Beller0b209032018-03-23 18:21:07 +0100920 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500921 for (ent = the_repository->objects->odb->next; ent; ent = ent->next) {
Jeff Kingfe1b2262014-10-15 18:33:13 -0400922 r = fn(ent, cb);
923 if (r)
924 break;
925 }
926 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700927}
928
Stefan Beller13068bf2018-03-23 18:21:09 +0100929void prepare_alt_odb(struct repository *r)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200930{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500931 if (r->objects->loaded_alternates)
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400932 return;
933
Stefan Beller13068bf2018-03-23 18:21:09 +0100934 link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200935
Jeff Kingf0eaf632018-11-12 09:50:39 -0500936 read_info_alternates(r, r->objects->odb->path, 0);
937 r->objects->loaded_alternates = 1;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200938}
939
Jeff King3096b2e2015-07-08 16:33:52 -0400940/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400941static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700942{
luciano.rocha@booking.com312cd762020-04-14 16:27:26 +0200943 return !utime(fn, NULL);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600944}
Junio C Hamanoace15342005-05-07 00:38:04 -0700945
Jeff King3096b2e2015-07-08 16:33:52 -0400946/*
947 * All of the check_and_freshen functions return 1 if the file exists and was
948 * freshened (if freshening was requested), 0 otherwise. If they return
949 * 0, you should not assume that it is safe to skip a write of the object (it
950 * either does not exist on disk, or has a stale mtime and may be subject to
951 * pruning).
952 */
Christian Couder6a5e6f52017-02-27 19:00:11 +0100953int check_and_freshen_file(const char *fn, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400954{
955 if (access(fn, F_OK))
956 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400957 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400958 return 0;
959 return 1;
960}
961
Jeff Kingf0eaf632018-11-12 09:50:39 -0500962static int check_and_freshen_odb(struct object_directory *odb,
963 const struct object_id *oid,
964 int freshen)
965{
966 static struct strbuf path = STRBUF_INIT;
Jeff King514c5fd2019-01-07 03:35:42 -0500967 odb_loose_path(odb, &path, oid);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500968 return check_and_freshen_file(path.buf, freshen);
969}
970
brian m. carlson6862ebb2018-05-02 00:25:34 +0000971static int check_and_freshen_local(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400972{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500973 return check_and_freshen_odb(the_repository->objects->odb, oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400974}
975
brian m. carlson6862ebb2018-05-02 00:25:34 +0000976static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600977{
Jeff King263db402018-11-12 09:48:47 -0500978 struct object_directory *odb;
Jeff Kingf3f043a2018-11-12 09:49:35 -0500979
Stefan Beller0b209032018-03-23 18:21:07 +0100980 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500981 for (odb = the_repository->objects->odb->next; odb; odb = odb->next) {
982 if (check_and_freshen_odb(odb, oid, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700983 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700984 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700985 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700986}
987
brian m. carlson6862ebb2018-05-02 00:25:34 +0000988static int check_and_freshen(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400989{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000990 return check_and_freshen_local(oid, freshen) ||
991 check_and_freshen_nonlocal(oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400992}
993
brian m. carlson6862ebb2018-05-02 00:25:34 +0000994int has_loose_object_nonlocal(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -0400995{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000996 return check_and_freshen_nonlocal(oid, 0);
Jeff King33d42212014-10-15 18:42:22 -0400997}
998
brian m. carlson6862ebb2018-05-02 00:25:34 +0000999static int has_loose_object(const struct object_id *oid)
Brandon Casey0f4dc142008-11-09 23:59:57 -06001000{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001001 return check_and_freshen(oid, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -06001002}
1003
Steffen Prohaska02710222014-08-26 17:23:23 +02001004static void mmap_limit_check(size_t length)
1005{
1006 static size_t limit = 0;
1007 if (!limit) {
1008 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
1009 if (!limit)
1010 limit = SIZE_MAX;
1011 }
1012 if (length > limit)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001013 die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
Steffen Prohaska02710222014-08-26 17:23:23 +02001014 (uintmax_t)length, (uintmax_t)limit);
1015}
1016
Jeff King15708562015-05-28 03:56:15 -04001017void *xmmap_gently(void *start, size_t length,
1018 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001019{
Steffen Prohaska02710222014-08-26 17:23:23 +02001020 void *ret;
1021
1022 mmap_limit_check(length);
1023 ret = mmap(start, length, prot, flags, fd, offset);
Jeff King9827d4c2019-08-12 16:50:21 -04001024 if (ret == MAP_FAILED && !length)
1025 ret = NULL;
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001026 return ret;
1027}
1028
Eric Wongdc059292021-06-30 00:01:32 +00001029const char *mmap_os_err(void)
1030{
1031 static const char blank[] = "";
1032#if defined(__linux__)
1033 if (errno == ENOMEM) {
1034 /* this continues an existing error message: */
1035 static const char enomem[] =
1036", check sys.vm.max_map_count and/or RLIMIT_DATA";
1037 return enomem;
1038 }
1039#endif /* OS-specific bits */
1040 return blank;
1041}
1042
Jeff King15708562015-05-28 03:56:15 -04001043void *xmmap(void *start, size_t length,
1044 int prot, int flags, int fd, off_t offset)
1045{
1046 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
1047 if (ret == MAP_FAILED)
Eric Wongdc059292021-06-30 00:01:32 +00001048 die_errno(_("mmap failed%s"), mmap_os_err());
Jeff King15708562015-05-28 03:56:15 -04001049 return ret;
1050}
1051
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001052static int format_object_header_literally(char *str, size_t size,
1053 const char *type, size_t objsize)
1054{
1055 return xsnprintf(str, size, "%s %"PRIuMAX, type, (uintmax_t)objsize) + 1;
1056}
1057
1058int format_object_header(char *str, size_t size, enum object_type type,
1059 size_t objsize)
1060{
1061 const char *name = type_name(type);
1062
1063 if (!name)
1064 BUG("could not get a type name for 'enum object_type' value %d", type);
1065
1066 return format_object_header_literally(str, size, name, objsize);
1067}
1068
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001069/*
Ævar Arnfjörð Bjarmason73182b22022-02-05 00:48:27 +01001070 * With in-core object data in "buf", rehash it to make sure the
Jeff Kingcb1c8d12019-01-07 03:33:52 -05001071 * object name actually matches "oid" to detect object corruption.
Ævar Arnfjörð Bjarmason73182b22022-02-05 00:48:27 +01001072 * With "buf" == NULL, try reading the object named with "oid" using
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001073 * the streaming interface and rehash it to do the same.
1074 */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001075int check_object_signature(struct repository *r, const struct object_id *oid,
Ævar Arnfjörð Bjarmason73182b22022-02-05 00:48:27 +01001076 void *buf, unsigned long size, const char *type,
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001077 struct object_id *real_oidp)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001078{
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001079 struct object_id tmp;
1080 struct object_id *real_oid = real_oidp ? real_oidp : &tmp;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001081 enum object_type obj_type;
1082 struct git_istream *st;
brian m. carlson18e25882018-02-01 02:18:41 +00001083 git_hash_ctx c;
brian m. carlson1af64f72018-03-12 02:27:55 +00001084 char hdr[MAX_HEADER_LEN];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001085 int hdrlen;
1086
Ævar Arnfjörð Bjarmason73182b22022-02-05 00:48:27 +01001087 if (buf) {
1088 hash_object_file(r->hash_algo, buf, size, type, real_oid);
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001089 return !oideq(oid, real_oid) ? -1 : 0;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001090 }
1091
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001092 st = open_istream(r, oid, &obj_type, &size, NULL);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001093 if (!st)
1094 return -1;
1095
1096 /* Generate the header */
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001097 hdrlen = format_object_header(hdr, sizeof(hdr), obj_type, size);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001098
1099 /* Sha1.. */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001100 r->hash_algo->init_fn(&c);
1101 r->hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001102 for (;;) {
1103 char buf[1024 * 16];
1104 ssize_t readlen = read_istream(st, buf, sizeof(buf));
1105
Jeff Kingf54fac52013-03-25 16:17:17 -04001106 if (readlen < 0) {
1107 close_istream(st);
1108 return -1;
1109 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001110 if (!readlen)
1111 break;
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001112 r->hash_algo->update_fn(&c, buf, readlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001113 }
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001114 r->hash_algo->final_oid_fn(real_oid, &c);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001115 close_istream(st);
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001116 return !oideq(oid, real_oid) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001117}
1118
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001119int git_open_cloexec(const char *name, int flags)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001120{
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001121 int fd;
1122 static int o_cloexec = O_CLOEXEC;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001123
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001124 fd = open(name, flags | o_cloexec);
1125 if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
Lars Schneidercd66ada2016-10-24 20:02:59 +02001126 /* Try again w/o O_CLOEXEC: the kernel might not support it */
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001127 o_cloexec &= ~O_CLOEXEC;
1128 fd = open(name, flags | o_cloexec);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001129 }
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001130
Eric Wong9fb94952017-07-15 18:55:40 +00001131#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001132 {
1133 static int fd_cloexec = FD_CLOEXEC;
1134
1135 if (!o_cloexec && 0 <= fd && fd_cloexec) {
1136 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
Eric Wong9fb94952017-07-15 18:55:40 +00001137 int flags = fcntl(fd, F_GETFD);
1138 if (fcntl(fd, F_SETFD, flags | fd_cloexec))
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001139 fd_cloexec = 0;
1140 }
1141 }
1142#endif
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001143 return fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001144}
1145
Jeff King771e7d52017-01-13 12:54:39 -05001146/*
Jeff King514c5fd2019-01-07 03:35:42 -05001147 * Find "oid" as a loose object in the local repository or in an alternate.
Jeff King771e7d52017-01-13 12:54:39 -05001148 * Returns 0 on success, negative on failure.
1149 *
1150 * The "path" out-parameter will give the path of the object we found (if any).
1151 * Note that it may point to static storage and is only valid until another
Jeff King514c5fd2019-01-07 03:35:42 -05001152 * call to stat_loose_object().
Jeff King771e7d52017-01-13 12:54:39 -05001153 */
Jeff King514c5fd2019-01-07 03:35:42 -05001154static int stat_loose_object(struct repository *r, const struct object_id *oid,
1155 struct stat *st, const char **path)
Jeff King052fe5e2013-07-12 02:30:48 -04001156{
Jeff King263db402018-11-12 09:48:47 -05001157 struct object_directory *odb;
Christian Couderea657732018-01-17 18:54:54 +01001158 static struct strbuf buf = STRBUF_INIT;
Jeff King052fe5e2013-07-12 02:30:48 -04001159
Stefan Bellerd2607fa2018-03-23 18:21:17 +01001160 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001161 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001162 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001163 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -04001164 return 0;
1165 }
1166
1167 return -1;
1168}
1169
Jeff King771e7d52017-01-13 12:54:39 -05001170/*
Jeff King514c5fd2019-01-07 03:35:42 -05001171 * Like stat_loose_object(), but actually open the object and return the
Jeff King771e7d52017-01-13 12:54:39 -05001172 * descriptor. See the caveats on the "path" parameter above.
1173 */
Jeff King514c5fd2019-01-07 03:35:42 -05001174static int open_loose_object(struct repository *r,
1175 const struct object_id *oid, const char **path)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001176{
1177 int fd;
Jeff King263db402018-11-12 09:48:47 -05001178 struct object_directory *odb;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001179 int most_interesting_errno = ENOENT;
Christian Couderea657732018-01-17 18:54:54 +01001180 static struct strbuf buf = STRBUF_INIT;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001181
Stefan Bellerec7283e2018-03-23 18:21:18 +01001182 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001183 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001184 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001185 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001186 if (fd >= 0)
1187 return fd;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001188
Jeff Kingd6c8a052014-05-15 04:54:06 -04001189 if (most_interesting_errno == ENOENT)
1190 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001191 }
Jeff Kingd6c8a052014-05-15 04:54:06 -04001192 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001193 return -1;
1194}
1195
Jeff King61c77112018-11-12 09:54:42 -05001196static int quick_has_loose(struct repository *r,
Jeff Kingd7a24572019-01-07 03:37:29 -05001197 const struct object_id *oid)
Jeff King61c77112018-11-12 09:54:42 -05001198{
Jeff King61c77112018-11-12 09:54:42 -05001199 struct object_directory *odb;
1200
Jeff King61c77112018-11-12 09:54:42 -05001201 prepare_alt_odb(r);
1202 for (odb = r->objects->odb; odb; odb = odb->next) {
Eric Wong92d8ed82021-07-07 23:10:19 +00001203 if (oidtree_contains(odb_loose_cache(odb, oid), oid))
Jeff King61c77112018-11-12 09:54:42 -05001204 return 1;
1205 }
1206 return 0;
1207}
1208
Jeff Kingf6371f92017-01-13 12:58:16 -05001209/*
1210 * Map the loose object at "path" if it is not NULL, or the path found by
Jeff King514c5fd2019-01-07 03:35:42 -05001211 * searching for a loose object named "oid".
Jeff Kingf6371f92017-01-13 12:58:16 -05001212 */
Jeff King514c5fd2019-01-07 03:35:42 -05001213static void *map_loose_object_1(struct repository *r, const char *path,
1214 const struct object_id *oid, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001215{
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001216 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -07001217 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -07001218
Jeff Kingf6371f92017-01-13 12:58:16 -05001219 if (path)
1220 fd = git_open(path);
1221 else
Jeff King514c5fd2019-01-07 03:35:42 -05001222 fd = open_loose_object(r, oid, &path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001223 map = NULL;
1224 if (fd >= 0) {
1225 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001226
Linus Torvalds44d1c192008-06-14 11:32:37 -07001227 if (!fstat(fd, &st)) {
1228 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001229 if (!*size) {
1230 /* mmap() is forbidden on empty files */
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001231 error(_("object file %s is empty"), path);
René Scharfe68819252019-01-07 17:48:02 +01001232 close(fd);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001233 return NULL;
1234 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001235 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -07001236 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001237 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001238 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001239 return map;
1240}
1241
Jeff King514c5fd2019-01-07 03:35:42 -05001242void *map_loose_object(struct repository *r,
1243 const struct object_id *oid,
1244 unsigned long *size)
Jeff Kingf6371f92017-01-13 12:58:16 -05001245{
Jeff King514c5fd2019-01-07 03:35:42 -05001246 return map_loose_object_1(r, NULL, oid, size);
Jeff Kingf6371f92017-01-13 12:58:16 -05001247}
1248
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001249enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
1250 unsigned char *map,
1251 unsigned long mapsize,
1252 void *buffer,
1253 unsigned long bufsiz,
1254 struct strbuf *header)
Linus Torvaldsc4483572005-06-01 17:54:59 -07001255{
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001256 int status;
Matheus Tavares31877c92020-01-15 23:39:53 -03001257
Linus Torvaldsc4483572005-06-01 17:54:59 -07001258 /* Get the data stream */
1259 memset(stream, 0, sizeof(*stream));
1260 stream->next_in = map;
1261 stream->avail_in = mapsize;
1262 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -07001263 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -07001264
Linus Torvalds39c68542009-01-07 19:54:47 -08001265 git_inflate_init(stream);
Matheus Tavares31877c92020-01-15 23:39:53 -03001266 obj_read_unlock();
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001267 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001268 obj_read_lock();
Junio C Hamanod21f8422016-09-25 21:29:04 -07001269 if (status < Z_OK)
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001270 return ULHR_BAD;
Karthik Nayak46f03442015-05-03 19:59:59 +05301271
1272 /*
1273 * Check if entire header is unpacked in the first iteration.
1274 */
1275 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001276 return ULHR_OK;
Karthik Nayak46f03442015-05-03 19:59:59 +05301277
1278 /*
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001279 * We have a header longer than MAX_HEADER_LEN. The "header"
1280 * here is only non-NULL when we run "cat-file
1281 * --allow-unknown-type".
1282 */
1283 if (!header)
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001284 return ULHR_TOO_LONG;
Karthik Nayak46f03442015-05-03 19:59:59 +05301285
1286 /*
1287 * buffer[0..bufsiz] was not large enough. Copy the partial
1288 * result out to header, and then append the result of further
1289 * reading the stream.
1290 */
1291 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1292 stream->next_out = buffer;
1293 stream->avail_out = bufsiz;
1294
1295 do {
Matheus Tavares31877c92020-01-15 23:39:53 -03001296 obj_read_unlock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301297 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001298 obj_read_lock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301299 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1300 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1301 return 0;
1302 stream->next_out = buffer;
1303 stream->avail_out = bufsiz;
1304 } while (status != Z_STREAM_END);
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001305 return ULHR_TOO_LONG;
Karthik Nayak46f03442015-05-03 19:59:59 +05301306}
1307
Jeff King00a77602019-01-07 03:37:02 -05001308static void *unpack_loose_rest(git_zstream *stream,
1309 void *buffer, unsigned long size,
1310 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001311{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001312 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001313 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001314 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001315 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001316
Linus Torvalds93821bd2006-07-11 12:48:08 -07001317 n = stream->total_out - bytes;
1318 if (n > size)
1319 n = size;
1320 memcpy(buf, (char *) buffer + bytes, n);
1321 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001322 if (bytes <= size) {
1323 /*
1324 * The above condition must be (bytes <= size), not
1325 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001326 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001327 * the input zlib stream may have bytes that express
1328 * "this concludes the stream", and we *do* want to
1329 * eat that input.
1330 *
1331 * Otherwise we would not be able to test that we
1332 * consumed all the input to reach the expected size;
1333 * we also want to check that zlib tells us that all
1334 * went well with status == Z_STREAM_END at the end.
1335 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001336 stream->next_out = buf + bytes;
1337 stream->avail_out = size - bytes;
Matheus Tavares31877c92020-01-15 23:39:53 -03001338 while (status == Z_OK) {
1339 obj_read_unlock();
Linus Torvalds39c68542009-01-07 19:54:47 -08001340 status = git_inflate(stream, Z_FINISH);
Matheus Tavares31877c92020-01-15 23:39:53 -03001341 obj_read_lock();
1342 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001343 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001344 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001345 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001346 return buf;
1347 }
1348
1349 if (status < 0)
Jeff King00a77602019-01-07 03:37:02 -05001350 error(_("corrupt loose object '%s'"), oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001351 else if (stream->avail_in)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001352 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05001353 oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001354 free(buf);
1355 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001356}
1357
Linus Torvalds5180cac2005-06-02 07:57:25 -07001358/*
1359 * We used to just use "sscanf()", but that's actually way
1360 * too permissive for what we want to check. So do an anal
1361 * object header parse by hand.
1362 */
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001363int parse_loose_header(const char *hdr, struct object_info *oi)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001364{
Karthik Nayak46f03442015-05-03 19:59:59 +05301365 const char *type_buf = hdr;
Matt Cooperd6a09e72021-11-02 15:46:10 +00001366 size_t size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301367 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001368
1369 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301370 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001371 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001372 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001373 for (;;) {
1374 char c = *hdr++;
Junio C Hamanod21f8422016-09-25 21:29:04 -07001375 if (!c)
1376 return -1;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001377 if (c == ' ')
1378 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301379 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001380 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301381
1382 type = type_from_string_gently(type_buf, type_len, 1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001383 if (oi->type_name)
1384 strbuf_add(oi->type_name, type_buf, type_len);
Karthik Nayak46f03442015-05-03 19:59:59 +05301385 if (oi->typep)
1386 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001387
1388 /*
1389 * The length must follow immediately, and be in canonical
1390 * decimal format (ie "010" is not valid).
1391 */
1392 size = *hdr++ - '0';
1393 if (size > 9)
1394 return -1;
1395 if (size) {
1396 for (;;) {
1397 unsigned long c = *hdr - '0';
1398 if (c > 9)
1399 break;
1400 hdr++;
Matt Cooperd6a09e72021-11-02 15:46:10 +00001401 size = st_add(st_mult(size, 10), c);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001402 }
1403 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301404
1405 if (oi->sizep)
Matt Cooperd6a09e72021-11-02 15:46:10 +00001406 *oi->sizep = cast_size_t_to_ulong(size);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001407
1408 /*
1409 * The length must be followed by a zero byte
1410 */
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001411 if (*hdr)
1412 return -1;
Karthik Nayak46f03442015-05-03 19:59:59 +05301413
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001414 /*
1415 * The format is valid, but the type may still be bogus. The
1416 * Caller needs to check its oi->typep.
1417 */
1418 return 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001419}
1420
Jeff King514c5fd2019-01-07 03:35:42 -05001421static int loose_object_info(struct repository *r,
1422 const struct object_id *oid,
1423 struct object_info *oi, int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001424{
Karthik Nayak46f03442015-05-03 19:59:59 +05301425 int status = 0;
1426 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001427 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001428 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00001429 char hdr[MAX_HEADER_LEN];
Karthik Nayak46f03442015-05-03 19:59:59 +05301430 struct strbuf hdrbuf = STRBUF_INIT;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001431 unsigned long size_scratch;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001432 enum object_type type_scratch;
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001433 int allow_unknown = flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001434
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001435 if (oi->delta_base_oid)
1436 oidclr(oi->delta_base_oid);
Jeff King5d642e72013-12-21 09:24:20 -05001437
Jeff King052fe5e2013-07-12 02:30:48 -04001438 /*
1439 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001440 * need to look inside the object at all. Note that we
1441 * do not optimize out the stat call, even if the
1442 * caller doesn't care about the disk-size, since our
1443 * return value implicitly indicates whether the
1444 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04001445 */
Brandon Williams6ca32f42018-02-14 10:59:23 -08001446 if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
Jeff King771e7d52017-01-13 12:54:39 -05001447 const char *path;
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001448 struct stat st;
Jeff King61c77112018-11-12 09:54:42 -05001449 if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK))
Jeff Kingd7a24572019-01-07 03:37:29 -05001450 return quick_has_loose(r, oid) ? 0 : -1;
Jeff King514c5fd2019-01-07 03:35:42 -05001451 if (stat_loose_object(r, oid, &st, &path) < 0)
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001452 return -1;
1453 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04001454 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04001455 return 0;
1456 }
1457
Jeff King514c5fd2019-01-07 03:35:42 -05001458 map = map_loose_object(r, oid, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001459 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02001460 return -1;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001461
1462 if (!oi->sizep)
1463 oi->sizep = &size_scratch;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001464 if (!oi->typep)
1465 oi->typep = &type_scratch;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001466
Jeff King23c339c2013-07-12 02:37:53 -04001467 if (oi->disk_sizep)
1468 *oi->disk_sizep = mapsize;
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001469
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001470 switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
1471 allow_unknown ? &hdrbuf : NULL)) {
1472 case ULHR_OK:
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001473 if (parse_loose_header(hdrbuf.len ? hdrbuf.buf : hdr, oi) < 0)
1474 status = error(_("unable to parse %s header"), oid_to_hex(oid));
1475 else if (!allow_unknown && *oi->typep < 0)
1476 die(_("invalid object type"));
1477
1478 if (!oi->contentp)
1479 break;
1480 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
1481 if (*oi->contentp)
1482 goto cleanup;
1483
1484 status = -1;
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001485 break;
1486 case ULHR_BAD:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001487 status = error(_("unable to unpack %s header"),
Jeff King514c5fd2019-01-07 03:35:42 -05001488 oid_to_hex(oid));
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001489 break;
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001490 case ULHR_TOO_LONG:
1491 status = error(_("header for %s too long, exceeds %d bytes"),
1492 oid_to_hex(oid), MAX_HEADER_LEN);
1493 break;
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001494 }
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001495
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001496 git_inflate_end(&stream);
1497cleanup:
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001498 munmap(map, mapsize);
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001499 if (oi->sizep == &size_scratch)
1500 oi->sizep = NULL;
Karthik Nayak46f03442015-05-03 19:59:59 +05301501 strbuf_release(&hdrbuf);
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001502 if (oi->typep == &type_scratch)
1503 oi->typep = NULL;
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001504 oi->whence = OI_LOOSE;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001505 return status;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001506}
1507
Matheus Tavares31877c92020-01-15 23:39:53 -03001508int obj_read_use_lock = 0;
1509pthread_mutex_t obj_read_mutex;
1510
1511void enable_obj_read_lock(void)
1512{
1513 if (obj_read_use_lock)
1514 return;
1515
1516 obj_read_use_lock = 1;
1517 init_recursive_mutex(&obj_read_mutex);
1518}
1519
1520void disable_obj_read_lock(void)
1521{
1522 if (!obj_read_use_lock)
1523 return;
1524
1525 obj_read_use_lock = 0;
1526 pthread_mutex_destroy(&obj_read_mutex);
1527}
1528
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001529int fetch_if_missing = 1;
1530
Matheus Tavares31877c92020-01-15 23:39:53 -03001531static int do_oid_object_info_extended(struct repository *r,
1532 const struct object_id *oid,
1533 struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001534{
Jonathan Tancd585e22017-06-21 17:40:23 -07001535 static struct object_info blank_oi = OBJECT_INFO_INIT;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001536 struct cached_object *co;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001537 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04001538 int rtype;
brian m. carlsonb383a132018-03-12 02:27:54 +00001539 const struct object_id *real = oid;
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001540 int already_retried = 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001541
Matheus Tavares31877c92020-01-15 23:39:53 -03001542
brian m. carlsonb383a132018-03-12 02:27:54 +00001543 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
Stefan Beller9d983542018-04-25 11:21:06 -07001544 real = lookup_replace_object(r, oid);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001545
brian m. carlsonb383a132018-03-12 02:27:54 +00001546 if (is_null_oid(real))
Jeff King87b5e232017-11-21 18:17:39 -05001547 return -1;
1548
Jonathan Tancd585e22017-06-21 17:40:23 -07001549 if (!oi)
1550 oi = &blank_oi;
1551
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001552 co = find_cached_object(real);
1553 if (co) {
1554 if (oi->typep)
1555 *(oi->typep) = co->type;
1556 if (oi->sizep)
1557 *(oi->sizep) = co->size;
1558 if (oi->disk_sizep)
1559 *(oi->disk_sizep) = 0;
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001560 if (oi->delta_base_oid)
1561 oidclr(oi->delta_base_oid);
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001562 if (oi->type_name)
1563 strbuf_addstr(oi->type_name, type_name(co->type));
1564 if (oi->contentp)
1565 *oi->contentp = xmemdupz(co->buf, co->size);
1566 oi->whence = OI_CACHED;
1567 return 0;
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07001568 }
1569
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001570 while (1) {
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001571 if (find_pack_entry(r, real, &e))
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001572 break;
1573
Takuto Ikuta024aa462018-03-14 15:32:42 +09001574 if (flags & OBJECT_INFO_IGNORE_LOOSE)
1575 return -1;
1576
Steven Grimmddd63e62008-08-05 13:08:41 -07001577 /* Most likely it's a loose object. */
Jeff King514c5fd2019-01-07 03:35:42 -05001578 if (!loose_object_info(r, real, oi, flags))
Jeff King5b086402013-07-12 02:34:57 -04001579 return 0;
Steven Grimmddd63e62008-08-05 13:08:41 -07001580
1581 /* Not a loose object; someone else may have just packed it. */
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001582 if (!(flags & OBJECT_INFO_QUICK)) {
Stefan Beller9d983542018-04-25 11:21:06 -07001583 reprepare_packed_git(r);
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001584 if (find_pack_entry(r, real, &e))
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001585 break;
1586 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001587
Jonathan Taneef71902021-10-08 14:08:18 -07001588 /*
1589 * If r is the_repository, this might be an attempt at
1590 * accessing a submodule object as if it were in the_repository
1591 * (having called add_submodule_odb() on that submodule's ODB).
1592 * If any such ODBs exist, register them and try again.
1593 */
1594 if (r == the_repository &&
1595 register_all_submodule_odb_as_alternates())
Jonathan Tana35e03d2021-08-16 14:09:51 -07001596 /* We added some alternates; retry */
1597 continue;
1598
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001599 /* Check if it is a missing object */
Jonathan Tanef830cc2021-06-17 10:13:26 -07001600 if (fetch_if_missing && repo_has_promisor_remote(r) &&
1601 !already_retried &&
Derrick Stolee31f52562019-05-28 08:19:07 -07001602 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001603 /*
Christian Couderb14ed5a2019-06-25 15:40:31 +02001604 * TODO Investigate checking promisor_remote_get_direct()
1605 * TODO return value and stopping on error here.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001606 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001607 promisor_remote_get_direct(r, real, 1);
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001608 already_retried = 1;
1609 continue;
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001610 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001611
1612 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001613 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001614
Jonathan Tancd585e22017-06-21 17:40:23 -07001615 if (oi == &blank_oi)
1616 /*
1617 * We know that the caller doesn't actually need the
1618 * information below, so return early.
1619 */
1620 return 0;
Stefan Beller9d983542018-04-25 11:21:06 -07001621 rtype = packed_object_info(r, e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04001622 if (rtype < 0) {
René Scharfe751530d2021-09-11 22:40:33 +02001623 mark_bad_packed_object(e.p, real);
Matheus Tavares31877c92020-01-15 23:39:53 -03001624 return do_oid_object_info_extended(r, real, oi, 0);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001625 } else if (oi->whence == OI_PACKED) {
Junio C Hamano9a490592011-05-12 15:51:38 -07001626 oi->u.packed.offset = e.offset;
1627 oi->u.packed.pack = e.p;
1628 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
1629 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001630 }
1631
Jeff King5b086402013-07-12 02:34:57 -04001632 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001633}
1634
Matheus Tavares31877c92020-01-15 23:39:53 -03001635int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1636 struct object_info *oi, unsigned flags)
1637{
1638 int ret;
1639 obj_read_lock();
1640 ret = do_oid_object_info_extended(r, oid, oi, flags);
1641 obj_read_unlock();
1642 return ret;
1643}
1644
1645
Christian Couder3fc0dca2013-10-27 00:34:30 +02001646/* returns enum object_type or negative */
Stefan Beller9d983542018-04-25 11:21:06 -07001647int oid_object_info(struct repository *r,
1648 const struct object_id *oid,
1649 unsigned long *sizep)
Junio C Hamano9a490592011-05-12 15:51:38 -07001650{
Jeff King5b086402013-07-12 02:34:57 -04001651 enum object_type type;
Jeff King27b5c1a2016-08-11 05:24:35 -04001652 struct object_info oi = OBJECT_INFO_INIT;
Junio C Hamano9a490592011-05-12 15:51:38 -07001653
Jeff King5b086402013-07-12 02:34:57 -04001654 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001655 oi.sizep = sizep;
Stefan Beller9d983542018-04-25 11:21:06 -07001656 if (oid_object_info_extended(r, oid, &oi,
1657 OBJECT_INFO_LOOKUP_REPLACE) < 0)
Jeff King5b086402013-07-12 02:34:57 -04001658 return -1;
1659 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001660}
1661
Stefan Beller1b9b5c62018-10-16 16:35:32 -07001662static void *read_object(struct repository *r,
Junio C Hamanocba595a2019-02-06 22:05:27 -08001663 const struct object_id *oid, enum object_type *type,
Jonathan Tanf1d81302017-08-18 15:20:30 -07001664 unsigned long *size)
1665{
1666 struct object_info oi = OBJECT_INFO_INIT;
1667 void *content;
1668 oi.typep = type;
1669 oi.sizep = size;
1670 oi.contentp = &content;
1671
Junio C Hamanocba595a2019-02-06 22:05:27 -08001672 if (oid_object_info_extended(r, oid, &oi, 0) < 0)
Jonathan Tanf1d81302017-08-18 15:20:30 -07001673 return NULL;
1674 return content;
1675}
1676
Patryk Obara829e5c32018-01-28 01:13:11 +01001677int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1678 struct object_id *oid)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001679{
1680 struct cached_object *co;
1681
Matheus Tavares2dcde202020-01-30 17:32:22 -03001682 hash_object_file(the_hash_algo, buf, len, type_name(type), oid);
Jonathan Tana64d2aa2020-07-21 15:50:20 -07001683 if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
1684 find_cached_object(oid))
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001685 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04001686 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001687 co = &cached_objects[cached_object_nr++];
1688 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001689 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08001690 co->buf = xmalloc(len);
1691 memcpy(co->buf, buf, len);
brian m. carlson62ba93e2018-05-02 00:26:03 +00001692 oidcpy(&co->oid, oid);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001693 return 0;
1694}
1695
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001696/*
1697 * This function dies on corrupt objects; the callers who want to
1698 * deal with them should arrange to call read_object() and give error
1699 * messages themselves.
1700 */
Stefan Bellera3b72c82018-11-13 16:12:46 -08001701void *read_object_file_extended(struct repository *r,
1702 const struct object_id *oid,
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001703 enum object_type *type,
1704 unsigned long *size,
1705 int lookup_replace)
Nicolas Pitreac939102008-07-14 21:46:48 -04001706{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001707 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001708 const struct packed_git *p;
Jeff King771e7d52017-01-13 12:54:39 -05001709 const char *path;
1710 struct stat st;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -07001711 const struct object_id *repl = lookup_replace ?
Stefan Bellera3b72c82018-11-13 16:12:46 -08001712 lookup_replace_object(r, oid) : oid;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001713
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001714 errno = 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08001715 data = read_object(r, repl, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07001716 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001717 return data;
Christian Couder68095572009-01-23 10:06:53 +01001718
Matheus Tavares31877c92020-01-15 23:39:53 -03001719 obj_read_lock();
Björn Steinbrink25f3af32011-01-20 21:12:20 +01001720 if (errno && errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001721 die_errno(_("failed to read object %s"), oid_to_hex(oid));
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001722
Christian Couder68095572009-01-23 10:06:53 +01001723 /* die if we replaced an object with one that does not exist */
brian m. carlsonb383a132018-03-12 02:27:54 +00001724 if (repl != oid)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001725 die(_("replacement %s not found for %s"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001726 oid_to_hex(repl), oid_to_hex(oid));
Christian Couder68095572009-01-23 10:06:53 +01001727
Junio C Hamanocba595a2019-02-06 22:05:27 -08001728 if (!stat_loose_object(r, repl, &st, &path))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001729 die(_("loose object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001730 oid_to_hex(repl), path);
Christian Couder68095572009-01-23 10:06:53 +01001731
René Scharfe7407d732021-09-11 22:42:20 +02001732 if ((p = has_packed_and_bad(r, repl)) != NULL)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001733 die(_("packed object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001734 oid_to_hex(repl), p->pack_name);
Matheus Tavares31877c92020-01-15 23:39:53 -03001735 obj_read_unlock();
Christian Couderf5552ae2009-01-23 10:07:01 +01001736
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001737 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001738}
1739
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001740void *read_object_with_reference(struct repository *r,
1741 const struct object_id *oid,
Nicolas Pitre21666f12007-02-26 14:55:59 -05001742 const char *required_type_name,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001743 unsigned long *size,
brian m. carlson02f05472018-03-12 02:27:52 +00001744 struct object_id *actual_oid_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001745{
Nicolas Pitre21666f12007-02-26 14:55:59 -05001746 enum object_type type, required_type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001747 void *buffer;
1748 unsigned long isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001749 struct object_id actual_oid;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001750
Nicolas Pitre21666f12007-02-26 14:55:59 -05001751 required_type = type_from_string(required_type_name);
brian m. carlson02f05472018-03-12 02:27:52 +00001752 oidcpy(&actual_oid, oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001753 while (1) {
1754 int ref_length = -1;
1755 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001756
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001757 buffer = repo_read_object_file(r, &actual_oid, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001758 if (!buffer)
1759 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001760 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001761 *size = isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001762 if (actual_oid_return)
1763 oidcpy(actual_oid_return, &actual_oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001764 return buffer;
1765 }
1766 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001767 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001768 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05001769 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001770 ref_type = "object ";
1771 else {
1772 free(buffer);
1773 return NULL;
1774 }
1775 ref_length = strlen(ref_type);
1776
brian m. carlson94b5e092018-07-16 01:28:07 +00001777 if (ref_length + the_hash_algo->hexsz > isize ||
Martin Koegler50974ec2008-02-18 21:47:52 +01001778 memcmp(buffer, ref_type, ref_length) ||
brian m. carlson02f05472018-03-12 02:27:52 +00001779 get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001780 free(buffer);
1781 return NULL;
1782 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001783 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001784 /* Now we have the ID of the referred-to object in
brian m. carlson02f05472018-03-12 02:27:52 +00001785 * actual_oid. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001786 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001787}
1788
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001789static void write_object_file_prepare(const struct git_hash_algo *algo,
1790 const void *buf, unsigned long len,
Patryk Obaraa09c9852018-01-28 01:13:19 +01001791 const char *type, struct object_id *oid,
1792 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001793{
brian m. carlson18e25882018-02-01 02:18:41 +00001794 git_hash_ctx c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001795
1796 /* Generate the header */
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01001797 *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001798
1799 /* Sha1.. */
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001800 algo->init_fn(&c);
1801 algo->update_fn(&c, hdr, *hdrlen);
1802 algo->update_fn(&c, buf, len);
brian m. carlson5951bf42021-04-26 01:02:53 +00001803 algo->final_oid_fn(oid, &c);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001804}
1805
Linus Torvalds230f1322005-10-08 15:54:01 -07001806/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001807 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07001808 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07001809int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001810{
Thomas Raste32c0a92008-09-19 00:24:46 +02001811 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001812
Johannes Schindelin348df162009-04-28 00:32:25 +02001813 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001814 goto try_rename;
1815 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02001816 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001817
1818 /*
1819 * Coda hack - coda doesn't like cross-directory links,
1820 * so we fall back to a rename, which will mean that it
1821 * won't be able to check collisions, but that's not a
1822 * big deal.
1823 *
1824 * The same holds for FAT formatted media.
1825 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07001826 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001827 * left to unlink.
1828 */
1829 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001830 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001831 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07001832 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001833 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001834 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001835 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07001836 if (ret) {
1837 if (ret != EEXIST) {
Jeff King2c319882019-01-07 03:39:33 -05001838 return error_errno(_("unable to write file %s"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001839 }
1840 /* FIXME!!! Collision check here ? */
1841 }
1842
Junio C Hamano3be1f182009-03-27 23:14:39 -07001843out:
Matthieu Moy5256b002010-02-22 23:32:16 +01001844 if (adjust_shared_perm(filename))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001845 return error(_("unable to set permission to '%s'"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001846 return 0;
1847}
1848
Linus Torvalds4d548152006-05-24 08:30:54 -07001849static int write_buffer(int fd, const void *buf, size_t len)
1850{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08001851 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001852 return error_errno(_("file write error"));
Linus Torvalds4d548152006-05-24 08:30:54 -07001853 return 0;
1854}
1855
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01001856void hash_object_file(const struct git_hash_algo *algo, const void *buf,
Matheus Tavares2dcde202020-01-30 17:32:22 -03001857 unsigned long len, const char *type,
Patryk Obaraf070fac2018-01-28 01:13:13 +01001858 struct object_id *oid)
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001859{
brian m. carlson1af64f72018-03-12 02:27:55 +00001860 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001861 int hdrlen = sizeof(hdr);
Matheus Tavares2dcde202020-01-30 17:32:22 -03001862 write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001863}
1864
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001865/* Finalize a file on disk, and close it. */
Jeff King514c5fd2019-01-07 03:35:42 -05001866static void close_loose_object(int fd)
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001867{
Neeraj Singhb3cecf42021-12-06 22:05:04 +00001868 if (!the_repository->objects->odb->will_destroy) {
1869 if (fsync_object_files)
1870 fsync_or_die(fd, "loose object file");
1871 }
1872
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001873 if (close(fd) != 0)
Jeff King76011352019-01-07 03:39:24 -05001874 die_errno(_("error when closing loose object file"));
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001875}
1876
Linus Torvalds5723fe72008-06-14 10:50:12 -07001877/* Size of directory component, including the ending '/' */
1878static inline int directory_size(const char *filename)
1879{
1880 const char *s = strrchr(filename, '/');
1881 if (!s)
1882 return 0;
1883 return s - filename + 1;
1884}
1885
1886/*
1887 * This creates a temporary file in the same directory as the final
1888 * 'filename'
1889 *
1890 * We want to avoid cross-directory filename renames, because those
1891 * can have problems on various filesystems (FAT, NFS, Coda).
1892 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001893static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07001894{
1895 int fd, dirlen = directory_size(filename);
1896
Jeff Kingd4b3d112015-09-24 17:07:49 -04001897 strbuf_reset(tmp);
1898 strbuf_add(tmp, filename, dirlen);
1899 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
1900 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05001901 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04001902 /*
1903 * Make sure the directory exists; note that the contents
1904 * of the buffer are undefined after mkstemp returns an
1905 * error, so we have to rewrite the whole buffer from
1906 * scratch.
1907 */
1908 strbuf_reset(tmp);
1909 strbuf_add(tmp, filename, dirlen - 1);
1910 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01001911 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001912 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07001913 return -1;
1914
1915 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001916 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
1917 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07001918 }
1919 return fd;
1920}
1921
Patryk Obara3fc72812018-01-28 01:13:21 +01001922static int write_loose_object(const struct object_id *oid, char *hdr,
1923 int hdrlen, const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001924 time_t mtime, unsigned flags)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001925{
Jeff King915308b2009-01-29 00:56:34 -05001926 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001927 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001928 git_zstream stream;
brian m. carlson18e25882018-02-01 02:18:41 +00001929 git_hash_ctx c;
Patryk Obara3fc72812018-01-28 01:13:21 +01001930 struct object_id parano_oid;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001931 static struct strbuf tmp_file = STRBUF_INIT;
Christian Couderea657732018-01-17 18:54:54 +01001932 static struct strbuf filename = STRBUF_INIT;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001933
Jeff King514c5fd2019-01-07 03:35:42 -05001934 loose_object_path(the_repository, &filename, oid);
Christian Couderea657732018-01-17 18:54:54 +01001935
1936 fd = create_tmpfile(&tmp_file, filename.buf);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001937 if (fd < 0) {
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001938 if (flags & HASH_SILENT)
1939 return -1;
1940 else if (errno == EACCES)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001941 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01001942 else
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001943 return error_errno(_("unable to create temporary file"));
Linus Torvaldsaac17942005-05-03 11:46:16 -07001944 }
1945
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001946 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001947 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001948 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001949 stream.avail_out = sizeof(compressed);
brian m. carlson18e25882018-02-01 02:18:41 +00001950 the_hash_algo->init_fn(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001951
1952 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05001953 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001954 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001955 while (git_deflate(&stream, 0) == Z_OK)
1956 ; /* nothing */
brian m. carlson18e25882018-02-01 02:18:41 +00001957 the_hash_algo->update_fn(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001958
1959 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04001960 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001961 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001962 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05001963 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001964 ret = git_deflate(&stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00001965 the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001966 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
Jeff King76011352019-01-07 03:39:24 -05001967 die(_("unable to write loose object file"));
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001968 stream.next_out = compressed;
1969 stream.avail_out = sizeof(compressed);
1970 } while (ret == Z_OK);
1971
Linus Torvaldsac54c272007-03-20 11:38:34 -07001972 if (ret != Z_STREAM_END)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001973 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001974 ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001975 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07001976 if (ret != Z_OK)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001977 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001978 ret);
brian m. carlson5951bf42021-04-26 01:02:53 +00001979 the_hash_algo->final_oid_fn(&parano_oid, &c);
Jeff King9001dc22018-08-28 17:22:48 -04001980 if (!oideq(oid, &parano_oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001981 die(_("confused by unstable object source data for %s"),
Patryk Obara3fc72812018-01-28 01:13:21 +01001982 oid_to_hex(oid));
Linus Torvaldsac54c272007-03-20 11:38:34 -07001983
Jeff King514c5fd2019-01-07 03:35:42 -05001984 close_loose_object(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001985
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001986 if (mtime) {
1987 struct utimbuf utb;
1988 utb.actime = mtime;
1989 utb.modtime = mtime;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001990 if (utime(tmp_file.buf, &utb) < 0 &&
1991 !(flags & HASH_SILENT))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001992 warning_errno(_("failed utime() on %s"), tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001993 }
1994
Christian Couderea657732018-01-17 18:54:54 +01001995 return finalize_object_file(tmp_file.buf, filename.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001996}
Daniel Barkalow8237b182005-04-23 18:47:23 -07001997
brian m. carlson6862ebb2018-05-02 00:25:34 +00001998static int freshen_loose_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001999{
brian m. carlson6862ebb2018-05-02 00:25:34 +00002000 return check_and_freshen(oid, 1);
Jeff King33d42212014-10-15 18:42:22 -04002001}
2002
brian m. carlson6862ebb2018-05-02 00:25:34 +00002003static int freshen_packed_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04002004{
2005 struct pack_entry e;
brian m. carlson544443c2018-05-02 00:25:35 +00002006 if (!find_pack_entry(the_repository, oid, &e))
Jeff Kingee1c6c32015-04-20 15:55:00 -04002007 return 0;
2008 if (e.p->freshened)
2009 return 1;
2010 if (!freshen_file(e.p->pack_name))
2011 return 0;
2012 e.p->freshened = 1;
2013 return 1;
Jeff King33d42212014-10-15 18:42:22 -04002014}
2015
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002016int write_object_file_flags(const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002017 enum object_type type, struct object_id *oid,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002018 unsigned flags)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002019{
brian m. carlson1af64f72018-03-12 02:27:55 +00002020 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04002021 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002022
2023 /* Normally if we have it in the pack then we do not bother writing
2024 * it out into .git/objects/??/?{38} file.
2025 */
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002026 write_object_file_prepare(the_hash_algo, buf, len, type_name(type), oid, hdr,
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002027 &hdrlen);
brian m. carlson6862ebb2018-05-02 00:25:34 +00002028 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002029 return 0;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002030 return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002031}
2032
Patryk Obara1752cbb2018-01-28 01:13:22 +01002033int hash_object_file_literally(const void *buf, unsigned long len,
2034 const char *type, struct object_id *oid,
2035 unsigned flags)
Eric Sunshine0c3db672015-05-04 03:25:15 -04002036{
2037 char *header;
2038 int hdrlen, status = 0;
2039
2040 /* type string, SP, %lu of the length plus NUL must fit this */
brian m. carlson1af64f72018-03-12 02:27:55 +00002041 hdrlen = strlen(type) + MAX_HEADER_LEN;
Jeff Kingef1286d2015-09-24 17:06:42 -04002042 header = xmalloc(hdrlen);
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002043 write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
2044 &hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002045
2046 if (!(flags & HASH_WRITE_OBJECT))
2047 goto cleanup;
brian m. carlson6862ebb2018-05-02 00:25:34 +00002048 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Eric Sunshine0c3db672015-05-04 03:25:15 -04002049 goto cleanup;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002050 status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002051
2052cleanup:
2053 free(header);
2054 return status;
2055}
2056
Patryk Obara4bdb70a2018-01-28 01:13:20 +01002057int force_object_loose(const struct object_id *oid, time_t mtime)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002058{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002059 void *buf;
2060 unsigned long len;
2061 enum object_type type;
brian m. carlson1af64f72018-03-12 02:27:55 +00002062 char hdr[MAX_HEADER_LEN];
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002063 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002064 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002065
brian m. carlson6862ebb2018-05-02 00:25:34 +00002066 if (has_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002067 return 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08002068 buf = read_object(the_repository, oid, &type, &len);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002069 if (!buf)
Jeff King2c319882019-01-07 03:39:33 -05002070 return error(_("cannot read object for %s"), oid_to_hex(oid));
Ævar Arnfjörð Bjarmasonb04cdea2022-02-05 00:48:25 +01002071 hdrlen = format_object_header(hdr, sizeof(hdr), type, len);
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002072 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002073 free(buf);
2074
2075 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002076}
2077
Jonathan Tan1d8d9cb2020-08-05 16:06:49 -07002078int has_object(struct repository *r, const struct object_id *oid,
2079 unsigned flags)
2080{
2081 int quick = !(flags & HAS_OBJECT_RECHECK_PACKED);
2082 unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT |
2083 (quick ? OBJECT_INFO_QUICK : 0);
2084
2085 if (!startup_info->have_repository)
2086 return 0;
2087 return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0;
2088}
2089
Junio C Hamanocba595a2019-02-06 22:05:27 -08002090int repo_has_object_file_with_flags(struct repository *r,
2091 const struct object_id *oid, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07002092{
Jonathan Nieder3e8b7d32017-04-11 15:47:13 -07002093 if (!startup_info->have_repository)
2094 return 0;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08002095 return oid_object_info_extended(r, oid, NULL, flags) >= 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07002096}
Junio C Hamano74400e72005-05-01 23:45:49 -07002097
Stefan Beller9b45f492018-11-13 16:12:48 -08002098int repo_has_object_file(struct repository *r,
2099 const struct object_id *oid)
brian m. carlsonb419aa22015-11-10 02:22:19 +00002100{
Junio C Hamanocba595a2019-02-06 22:05:27 -08002101 return repo_has_object_file_with_flags(r, oid, 0);
Jeff King5827a032016-10-13 12:53:44 -04002102}
2103
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002104static void check_tree(const void *buf, size_t size)
2105{
2106 struct tree_desc desc;
2107 struct name_entry entry;
2108
2109 init_tree_desc(&desc, buf, size);
2110 while (tree_entry(&desc, &entry))
2111 /* do nothing
2112 * tree_entry() will die() on malformed entries */
2113 ;
2114}
2115
2116static void check_commit(const void *buf, size_t size)
2117{
2118 struct commit c;
2119 memset(&c, 0, sizeof(c));
Stefan Beller08f4f442018-06-28 18:22:00 -07002120 if (parse_commit_buffer(the_repository, &c, buf, size, 0))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002121 die(_("corrupt commit"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002122}
2123
2124static void check_tag(const void *buf, size_t size)
2125{
2126 struct tag t;
2127 memset(&t, 0, sizeof(t));
Stefan Beller0e740fe2018-06-28 18:22:04 -07002128 if (parse_tag_buffer(the_repository, &t, buf, size))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002129 die(_("corrupt tag"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002130}
2131
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002132static int index_mem(struct index_state *istate,
2133 struct object_id *oid, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002134 enum object_type type,
2135 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02002136{
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01002137 int ret = 0;
Ævar Arnfjörð Bjarmasonbbea0dd2022-02-05 00:48:23 +01002138 int re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002139 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07002140
Bryan Larsen7672db22005-07-08 16:51:55 -07002141 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08002142 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08002143
2144 /*
2145 * Convert blobs to git internal format
2146 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002147 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002148 struct strbuf nbuf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002149 if (convert_to_git(istate, path, buf, size, &nbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002150 get_conv_flags(flags))) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002151 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08002152 re_allocated = 1;
2153 }
2154 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002155 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002156 if (type == OBJ_TREE)
2157 check_tree(buf, size);
2158 if (type == OBJ_COMMIT)
2159 check_commit(buf, size);
2160 if (type == OBJ_TAG)
2161 check_tag(buf, size);
2162 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08002163
Bryan Larsen7672db22005-07-08 16:51:55 -07002164 if (write_object)
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002165 ret = write_object_file(buf, size, type, oid);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02002166 else
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01002167 hash_object_file(the_hash_algo, buf, size, type_name(type),
2168 oid);
2169
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002170 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08002171 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002172 return ret;
2173}
2174
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002175static int index_stream_convert_blob(struct index_state *istate,
2176 struct object_id *oid,
2177 int fd,
2178 const char *path,
2179 unsigned flags)
Steffen Prohaska9035d752014-08-26 17:23:25 +02002180{
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01002181 int ret = 0;
Steffen Prohaska9035d752014-08-26 17:23:25 +02002182 const int write_object = flags & HASH_WRITE_OBJECT;
2183 struct strbuf sbuf = STRBUF_INIT;
2184
2185 assert(path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002186 assert(would_convert_to_git_filter_fd(istate, path));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002187
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002188 convert_to_git_filter_fd(istate, path, fd, &sbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002189 get_conv_flags(flags));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002190
2191 if (write_object)
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002192 ret = write_object_file(sbuf.buf, sbuf.len, OBJ_BLOB,
Patryk Obaraa09c9852018-01-28 01:13:19 +01002193 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002194 else
Ævar Arnfjörð Bjarmason63e05f92022-02-05 00:48:24 +01002195 hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
2196 type_name(OBJ_BLOB), oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002197 strbuf_release(&sbuf);
2198 return ret;
2199}
2200
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002201static int index_pipe(struct index_state *istate, struct object_id *oid,
2202 int fd, enum object_type type,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002203 const char *path, unsigned flags)
2204{
2205 struct strbuf sbuf = STRBUF_INIT;
2206 int ret;
2207
2208 if (strbuf_read(&sbuf, fd, 4096) >= 0)
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002209 ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002210 else
2211 ret = -1;
2212 strbuf_release(&sbuf);
2213 return ret;
2214}
2215
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002216#define SMALL_FILE_SIZE (32*1024)
2217
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002218static int index_core(struct index_state *istate,
2219 struct object_id *oid, int fd, size_t size,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002220 enum object_type type, const char *path,
2221 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002222{
2223 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002224
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002225 if (!size) {
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002226 ret = index_mem(istate, oid, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002227 } else if (size <= SMALL_FILE_SIZE) {
2228 char *buf = xmalloc(size);
Jeff King90dca672017-09-27 02:01:07 -04002229 ssize_t read_result = read_in_full(fd, buf, size);
2230 if (read_result < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002231 ret = error_errno(_("read error while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002232 path ? path : "<unknown>");
2233 else if (read_result != size)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002234 ret = error(_("short read while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002235 path ? path : "<unknown>");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002236 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002237 ret = index_mem(istate, oid, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002238 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002239 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002240 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002241 ret = index_mem(istate, oid, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002242 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002243 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002244 return ret;
2245}
2246
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002247/*
Junio C Hamano568508e2011-10-28 14:48:40 -07002248 * This creates one packfile per large blob unless bulk-checkin
2249 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002250 *
2251 * This also bypasses the usual "convert-to-git" dance, and that is on
2252 * purpose. We could write a streaming version of the converting
2253 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05002254 * (or equivalent in-core API described above). However, that is
2255 * somewhat complicated, as we do not know the size of the filter
2256 * result, which we need to know beforehand when writing a git object.
2257 * Since the primary motivation for trying to stream from the working
2258 * tree file and to avoid mmaping it in core is to deal with large
2259 * binary blobs, they generally do not want to get any conversion, and
2260 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002261 */
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002262static int index_stream(struct object_id *oid, int fd, size_t size,
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002263 enum object_type type, const char *path,
2264 unsigned flags)
2265{
brian m. carlson68ee6df2018-03-12 02:27:21 +00002266 return index_bulk_checkin(oid, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002267}
2268
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002269int index_fd(struct index_state *istate, struct object_id *oid,
2270 int fd, struct stat *st,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002271 enum object_type type, const char *path, unsigned flags)
2272{
2273 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002274
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002275 /*
2276 * Call xsize_t() only when needed to avoid potentially unnecessary
2277 * die() for large files.
2278 */
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002279 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path))
2280 ret = index_stream_convert_blob(istate, oid, fd, path, flags);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002281 else if (!S_ISREG(st->st_mode))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002282 ret = index_pipe(istate, oid, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002283 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002284 (path && would_convert_to_git(istate, path)))
2285 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
2286 type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002287 else
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002288 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002289 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002290 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002291 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07002292}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002293
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002294int index_path(struct index_state *istate, struct object_id *oid,
2295 const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002296{
2297 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002298 struct strbuf sb = STRBUF_INIT;
Rene Scharfeea8e0292017-08-30 20:00:29 +02002299 int rc = 0;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002300
2301 switch (st->st_mode & S_IFMT) {
2302 case S_IFREG:
2303 fd = open(path, O_RDONLY);
2304 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002305 return error_errno("open(\"%s\")", path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002306 if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002307 return error(_("%s: failed to insert into database"),
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002308 path);
2309 break;
2310 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002311 if (strbuf_readlink(&sb, path, st->st_size))
2312 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002313 if (!(flags & HASH_WRITE_OBJECT))
Matheus Tavares2dcde202020-01-30 17:32:22 -03002314 hash_object_file(the_hash_algo, sb.buf, sb.len,
2315 blob_type, oid);
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01002316 else if (write_object_file(sb.buf, sb.len, OBJ_BLOB, oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002317 rc = error(_("%s: failed to insert into database"), path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002318 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002319 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07002320 case S_IFDIR:
brian m. carlsona98e6102017-10-15 22:07:07 +00002321 return resolve_gitlink_ref(path, "HEAD", oid);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002322 default:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002323 return error(_("%s: unsupported file type"), path);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002324 }
Rene Scharfeea8e0292017-08-30 20:00:29 +02002325 return rc;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002326}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002327
2328int read_pack_header(int fd, struct pack_header *header)
2329{
Jeff Kingf48ecd32017-09-13 14:47:22 -04002330 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
Heikki Orsilac697ad12008-05-03 16:27:26 +03002331 /* "eof before pack header was fully read" */
2332 return PH_ERROR_EOF;
2333
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002334 if (header->hdr_signature != htonl(PACK_SIGNATURE))
2335 /* "protocol error (pack signature mismatch detected)" */
2336 return PH_ERROR_PACK_SIGNATURE;
2337 if (!pack_version_ok(header->hdr_version))
2338 /* "protocol error (pack version unsupported)" */
2339 return PH_ERROR_PROTOCOL;
2340 return 0;
2341}
Jeff King40d52ff2010-04-01 20:05:23 -04002342
brian m. carlsone816caa2018-03-12 02:27:42 +00002343void assert_oid_type(const struct object_id *oid, enum object_type expect)
Jeff King40d52ff2010-04-01 20:05:23 -04002344{
Stefan Beller0df8e962018-04-25 11:20:59 -07002345 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff King40d52ff2010-04-01 20:05:23 -04002346 if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002347 die(_("%s is not a valid object"), oid_to_hex(oid));
Jeff King40d52ff2010-04-01 20:05:23 -04002348 if (type != expect)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002349 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002350 type_name(expect));
Jeff King40d52ff2010-04-01 20:05:23 -04002351}
Jeff King27e1e222014-10-15 18:38:55 -04002352
René Scharfe70c49052017-06-24 16:09:39 +02002353int for_each_file_in_obj_subdir(unsigned int subdir_nr,
René Scharfecc817ca2017-06-22 20:19:48 +02002354 struct strbuf *path,
2355 each_loose_object_fn obj_cb,
2356 each_loose_cruft_fn cruft_cb,
2357 each_loose_subdir_fn subdir_cb,
2358 void *data)
Jeff King27e1e222014-10-15 18:38:55 -04002359{
René Scharfe0375f472017-06-24 14:12:30 +02002360 size_t origlen, baselen;
2361 DIR *dir;
Jeff King27e1e222014-10-15 18:38:55 -04002362 struct dirent *de;
2363 int r = 0;
René Scharfe62a24c82017-10-31 14:50:06 +01002364 struct object_id oid;
Jeff King27e1e222014-10-15 18:38:55 -04002365
René Scharfe70c49052017-06-24 16:09:39 +02002366 if (subdir_nr > 0xff)
2367 BUG("invalid loose object subdirectory: %x", subdir_nr);
2368
René Scharfe0375f472017-06-24 14:12:30 +02002369 origlen = path->len;
2370 strbuf_complete(path, '/');
2371 strbuf_addf(path, "%02x", subdir_nr);
René Scharfe0375f472017-06-24 14:12:30 +02002372
2373 dir = opendir(path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04002374 if (!dir) {
René Scharfe0375f472017-06-24 14:12:30 +02002375 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002376 r = error_errno(_("unable to open %s"), path->buf);
René Scharfe0375f472017-06-24 14:12:30 +02002377 strbuf_setlen(path, origlen);
2378 return r;
Jeff King27e1e222014-10-15 18:38:55 -04002379 }
2380
René Scharfe62a24c82017-10-31 14:50:06 +01002381 oid.hash[0] = subdir_nr;
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002382 strbuf_addch(path, '/');
2383 baselen = path->len;
René Scharfe62a24c82017-10-31 14:50:06 +01002384
Elijah Newrenb548f0f2021-05-12 17:28:22 +00002385 while ((de = readdir_skip_dot_and_dotdot(dir))) {
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002386 size_t namelen;
Jeff King27e1e222014-10-15 18:38:55 -04002387
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002388 namelen = strlen(de->d_name);
Jeff King27e1e222014-10-15 18:38:55 -04002389 strbuf_setlen(path, baselen);
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002390 strbuf_add(path, de->d_name, namelen);
brian m. carlson94b5e092018-07-16 01:28:07 +00002391 if (namelen == the_hash_algo->hexsz - 2 &&
René Scharfe62a24c82017-10-31 14:50:06 +01002392 !hex_to_bytes(oid.hash + 1, de->d_name,
brian m. carlson94b5e092018-07-16 01:28:07 +00002393 the_hash_algo->rawsz - 1)) {
brian m. carlson5a6dce72021-04-26 01:02:55 +00002394 oid_set_algo(&oid, the_hash_algo);
René Scharfe62a24c82017-10-31 14:50:06 +01002395 if (obj_cb) {
2396 r = obj_cb(&oid, path->buf, data);
2397 if (r)
2398 break;
Jeff King27e1e222014-10-15 18:38:55 -04002399 }
René Scharfe62a24c82017-10-31 14:50:06 +01002400 continue;
Jeff King27e1e222014-10-15 18:38:55 -04002401 }
2402
2403 if (cruft_cb) {
2404 r = cruft_cb(de->d_name, path->buf, data);
2405 if (r)
2406 break;
2407 }
2408 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02002409 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04002410
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002411 strbuf_setlen(path, baselen - 1);
Jeff King27e1e222014-10-15 18:38:55 -04002412 if (!r && subdir_cb)
2413 r = subdir_cb(subdir_nr, path->buf, data);
2414
René Scharfe0375f472017-06-24 14:12:30 +02002415 strbuf_setlen(path, origlen);
2416
Jeff King27e1e222014-10-15 18:38:55 -04002417 return r;
2418}
2419
Jeff Kinge6f875e2015-02-08 20:13:22 -05002420int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04002421 each_loose_object_fn obj_cb,
2422 each_loose_cruft_fn cruft_cb,
2423 each_loose_subdir_fn subdir_cb,
2424 void *data)
2425{
Jeff King27e1e222014-10-15 18:38:55 -04002426 int r = 0;
2427 int i;
2428
Jeff King27e1e222014-10-15 18:38:55 -04002429 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05002430 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04002431 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002432 if (r)
2433 break;
2434 }
2435
Jeff Kinge6f875e2015-02-08 20:13:22 -05002436 return r;
2437}
2438
2439int for_each_loose_file_in_objdir(const char *path,
2440 each_loose_object_fn obj_cb,
2441 each_loose_cruft_fn cruft_cb,
2442 each_loose_subdir_fn subdir_cb,
2443 void *data)
2444{
2445 struct strbuf buf = STRBUF_INIT;
2446 int r;
2447
2448 strbuf_addstr(&buf, path);
2449 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
2450 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002451 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05002452
Jeff King27e1e222014-10-15 18:38:55 -04002453 return r;
2454}
Jeff King660c8892014-10-15 18:41:21 -04002455
Jeff Kinga7ff6f52018-08-10 19:09:44 -04002456int for_each_loose_object(each_loose_object_fn cb, void *data,
2457 enum for_each_object_flags flags)
Jeff King660c8892014-10-15 18:41:21 -04002458{
Jeff Kingf0eaf632018-11-12 09:50:39 -05002459 struct object_directory *odb;
Jeff King660c8892014-10-15 18:41:21 -04002460
Jeff Kingf0eaf632018-11-12 09:50:39 -05002461 prepare_alt_odb(the_repository);
2462 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
2463 int r = for_each_loose_file_in_objdir(odb->path, cb, NULL,
2464 NULL, data);
2465 if (r)
2466 return r;
Jeff King660c8892014-10-15 18:41:21 -04002467
Jeff Kingf0eaf632018-11-12 09:50:39 -05002468 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
2469 break;
2470 }
Jeff King1385bb72015-03-27 07:32:41 -04002471
Jeff Kingf0eaf632018-11-12 09:50:39 -05002472 return 0;
Jeff King660c8892014-10-15 18:41:21 -04002473}
2474
Jeff King3a2e0822018-11-12 09:50:56 -05002475static int append_loose_object(const struct object_id *oid, const char *path,
2476 void *data)
2477{
Eric Wong92d8ed82021-07-07 23:10:19 +00002478 oidtree_insert(data, oid);
Jeff King3a2e0822018-11-12 09:50:56 -05002479 return 0;
2480}
2481
Eric Wong92d8ed82021-07-07 23:10:19 +00002482struct oidtree *odb_loose_cache(struct object_directory *odb,
René Scharfe0000d652019-01-06 17:45:30 +01002483 const struct object_id *oid)
2484{
2485 int subdir_nr = oid->hash[0];
Jeff King3a2e0822018-11-12 09:50:56 -05002486 struct strbuf buf = STRBUF_INIT;
Eric Wong33f379e2021-07-07 23:10:17 +00002487 size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]);
2488 size_t word_index = subdir_nr / word_bits;
Philip Oakley26de1fc2021-12-01 00:29:02 +00002489 size_t mask = (size_t)1u << (subdir_nr % word_bits);
Eric Wong33f379e2021-07-07 23:10:17 +00002490 uint32_t *bitmap;
Jeff King3a2e0822018-11-12 09:50:56 -05002491
2492 if (subdir_nr < 0 ||
Eric Wong33f379e2021-07-07 23:10:17 +00002493 subdir_nr >= bitsizeof(odb->loose_objects_subdir_seen))
Jeff King3a2e0822018-11-12 09:50:56 -05002494 BUG("subdir_nr out of range");
2495
Eric Wong33f379e2021-07-07 23:10:17 +00002496 bitmap = &odb->loose_objects_subdir_seen[word_index];
2497 if (*bitmap & mask)
Eric Wong92d8ed82021-07-07 23:10:19 +00002498 return odb->loose_objects_cache;
2499 if (!odb->loose_objects_cache) {
2500 ALLOC_ARRAY(odb->loose_objects_cache, 1);
2501 oidtree_init(odb->loose_objects_cache);
2502 }
Jeff King3a2e0822018-11-12 09:50:56 -05002503 strbuf_addstr(&buf, odb->path);
2504 for_each_file_in_obj_subdir(subdir_nr, &buf,
2505 append_loose_object,
2506 NULL, NULL,
Eric Wong92d8ed82021-07-07 23:10:19 +00002507 odb->loose_objects_cache);
Eric Wong33f379e2021-07-07 23:10:17 +00002508 *bitmap |= mask;
Jeff King7317aa72018-11-22 12:53:00 -05002509 strbuf_release(&buf);
Eric Wong92d8ed82021-07-07 23:10:19 +00002510 return odb->loose_objects_cache;
Jeff King660c8892014-10-15 18:41:21 -04002511}
2512
René Scharfed4e19e52019-01-06 17:45:39 +01002513void odb_clear_loose_cache(struct object_directory *odb)
2514{
Eric Wong92d8ed82021-07-07 23:10:19 +00002515 oidtree_clear(odb->loose_objects_cache);
2516 FREE_AND_NULL(odb->loose_objects_cache);
René Scharfed4e19e52019-01-06 17:45:39 +01002517 memset(&odb->loose_objects_subdir_seen, 0,
2518 sizeof(odb->loose_objects_subdir_seen));
2519}
2520
Jeff King00a77602019-01-07 03:37:02 -05002521static int check_stream_oid(git_zstream *stream,
2522 const char *hdr,
2523 unsigned long size,
2524 const char *path,
2525 const struct object_id *expected_oid)
Jeff Kingf6371f92017-01-13 12:58:16 -05002526{
brian m. carlson18e25882018-02-01 02:18:41 +00002527 git_hash_ctx c;
Jeff King00a77602019-01-07 03:37:02 -05002528 struct object_id real_oid;
Jeff Kingf6371f92017-01-13 12:58:16 -05002529 unsigned char buf[4096];
2530 unsigned long total_read;
2531 int status = Z_OK;
2532
brian m. carlson18e25882018-02-01 02:18:41 +00002533 the_hash_algo->init_fn(&c);
2534 the_hash_algo->update_fn(&c, hdr, stream->total_out);
Jeff Kingf6371f92017-01-13 12:58:16 -05002535
2536 /*
2537 * We already read some bytes into hdr, but the ones up to the NUL
2538 * do not count against the object's content size.
2539 */
2540 total_read = stream->total_out - strlen(hdr) - 1;
2541
2542 /*
2543 * This size comparison must be "<=" to read the final zlib packets;
Jeff King00a77602019-01-07 03:37:02 -05002544 * see the comment in unpack_loose_rest for details.
Jeff Kingf6371f92017-01-13 12:58:16 -05002545 */
2546 while (total_read <= size &&
Junio C Hamano18ad13e2018-10-31 13:12:12 +09002547 (status == Z_OK ||
2548 (status == Z_BUF_ERROR && !stream->avail_out))) {
Jeff Kingf6371f92017-01-13 12:58:16 -05002549 stream->next_out = buf;
2550 stream->avail_out = sizeof(buf);
2551 if (size - total_read < stream->avail_out)
2552 stream->avail_out = size - total_read;
2553 status = git_inflate(stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00002554 the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
Jeff Kingf6371f92017-01-13 12:58:16 -05002555 total_read += stream->next_out - buf;
2556 }
2557 git_inflate_end(stream);
2558
2559 if (status != Z_STREAM_END) {
Jeff King00a77602019-01-07 03:37:02 -05002560 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002561 return -1;
2562 }
Jeff Kingcce044d2017-01-13 13:00:25 -05002563 if (stream->avail_in) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002564 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05002565 oid_to_hex(expected_oid));
Jeff Kingcce044d2017-01-13 13:00:25 -05002566 return -1;
2567 }
Jeff Kingf6371f92017-01-13 12:58:16 -05002568
brian m. carlson5951bf42021-04-26 01:02:53 +00002569 the_hash_algo->final_oid_fn(&real_oid, &c);
Jeff King00a77602019-01-07 03:37:02 -05002570 if (!oideq(expected_oid, &real_oid)) {
Jeff King01f8d592019-01-07 03:40:34 -05002571 error(_("hash mismatch for %s (expected %s)"), path,
Jeff King00a77602019-01-07 03:37:02 -05002572 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002573 return -1;
2574 }
2575
2576 return 0;
2577}
2578
2579int read_loose_object(const char *path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002580 const struct object_id *expected_oid,
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02002581 struct object_id *real_oid,
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002582 void **contents,
2583 struct object_info *oi)
Jeff Kingf6371f92017-01-13 12:58:16 -05002584{
2585 int ret = -1;
Jeff Kingf6371f92017-01-13 12:58:16 -05002586 void *map = NULL;
2587 unsigned long mapsize;
2588 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00002589 char hdr[MAX_HEADER_LEN];
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002590 unsigned long *size = oi->sizep;
Jeff Kingf6371f92017-01-13 12:58:16 -05002591
Jeff King514c5fd2019-01-07 03:35:42 -05002592 map = map_loose_object_1(the_repository, path, NULL, &mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002593 if (!map) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002594 error_errno(_("unable to mmap %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002595 goto out;
2596 }
2597
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02002598 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
2599 NULL) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002600 error(_("unable to unpack header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002601 goto out;
2602 }
2603
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002604 if (parse_loose_header(hdr, oi) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002605 error(_("unable to parse header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002606 git_inflate_end(&stream);
2607 goto out;
2608 }
2609
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002610 if (*oi->typep == OBJ_BLOB && *size > big_file_threshold) {
Jeff King00a77602019-01-07 03:37:02 -05002611 if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
Jeff Kingf6371f92017-01-13 12:58:16 -05002612 goto out;
2613 } else {
Jeff King00a77602019-01-07 03:37:02 -05002614 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
Jeff Kingf6371f92017-01-13 12:58:16 -05002615 if (!*contents) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002616 error(_("unable to unpack contents of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002617 git_inflate_end(&stream);
2618 goto out;
2619 }
Matheus Tavaresb98d1882020-01-30 17:32:23 -03002620 if (check_object_signature(the_repository, expected_oid,
Ævar Arnfjörð Bjarmason16235e32021-11-11 06:18:56 +01002621 *contents, *size,
2622 oi->type_name->buf, real_oid))
Jeff Kingf6371f92017-01-13 12:58:16 -05002623 goto out;
Jeff Kingf6371f92017-01-13 12:58:16 -05002624 }
2625
2626 ret = 0; /* everything checks out */
2627
2628out:
2629 if (map)
2630 munmap(map, mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002631 return ret;
2632}