blob: 3d27dc8dea333778a9592335ad98f9c4472cb874 [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"
Pavel Roskine05db0f2007-01-09 23:07:11 -050035
brian m. carlson1af64f72018-03-12 02:27:55 +000036/* The maximum size for an object header. */
37#define MAX_HEADER_LEN 32
38
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000039
40#define EMPTY_TREE_SHA1_BIN_LITERAL \
41 "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
42 "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
brian m. carlson13eeedb2018-11-14 04:09:36 +000043#define EMPTY_TREE_SHA256_BIN_LITERAL \
44 "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \
45 "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \
46 "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \
47 "\x53\x21"
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000048
49#define EMPTY_BLOB_SHA1_BIN_LITERAL \
50 "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
51 "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
brian m. carlson13eeedb2018-11-14 04:09:36 +000052#define EMPTY_BLOB_SHA256_BIN_LITERAL \
53 "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \
54 "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \
55 "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \
56 "\x18\x13"
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000057
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000058static const struct object_id empty_tree_oid = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000059 .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
60 .algo = GIT_HASH_SHA1,
Jacob Keller8576fde2016-08-31 16:27:18 -070061};
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000062static const struct object_id empty_blob_oid = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000063 .hash = EMPTY_BLOB_SHA1_BIN_LITERAL,
64 .algo = GIT_HASH_SHA1,
Jacob Keller8576fde2016-08-31 16:27:18 -070065};
brian m. carlson14228442021-04-26 01:02:56 +000066static const struct object_id null_oid_sha1 = {
67 .hash = {0},
68 .algo = GIT_HASH_SHA1,
69};
brian m. carlson13eeedb2018-11-14 04:09:36 +000070static const struct object_id empty_tree_oid_sha256 = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000071 .hash = EMPTY_TREE_SHA256_BIN_LITERAL,
72 .algo = GIT_HASH_SHA256,
brian m. carlson13eeedb2018-11-14 04:09:36 +000073};
74static const struct object_id empty_blob_oid_sha256 = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000075 .hash = EMPTY_BLOB_SHA256_BIN_LITERAL,
76 .algo = GIT_HASH_SHA256,
brian m. carlson13eeedb2018-11-14 04:09:36 +000077};
brian m. carlson14228442021-04-26 01:02:56 +000078static const struct object_id null_oid_sha256 = {
79 .hash = {0},
80 .algo = GIT_HASH_SHA256,
81};
Junio C Hamano88cd6212005-09-30 14:02:47 -070082
brian m. carlsonac73ced2018-02-01 02:18:38 +000083static void git_hash_sha1_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000084{
brian m. carlsonac73ced2018-02-01 02:18:38 +000085 git_SHA1_Init(&ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000086}
87
brian m. carlson768e30e2020-02-22 20:17:27 +000088static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src)
89{
90 git_SHA1_Clone(&dst->sha1, &src->sha1);
91}
92
brian m. carlsonac73ced2018-02-01 02:18:38 +000093static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000094{
brian m. carlsonac73ced2018-02-01 02:18:38 +000095 git_SHA1_Update(&ctx->sha1, data, len);
brian m. carlsonf50e7662017-11-12 21:28:52 +000096}
97
brian m. carlsonac73ced2018-02-01 02:18:38 +000098static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000099{
brian m. carlsonac73ced2018-02-01 02:18:38 +0000100 git_SHA1_Final(hash, &ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +0000101}
102
brian m. carlsonab795f02021-04-26 01:02:52 +0000103static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
104{
105 git_SHA1_Final(oid->hash, &ctx->sha1);
106 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000107 oid->algo = GIT_HASH_SHA1;
brian m. carlsonab795f02021-04-26 01:02:52 +0000108}
109
brian m. carlson13eeedb2018-11-14 04:09:36 +0000110
111static void git_hash_sha256_init(git_hash_ctx *ctx)
112{
113 git_SHA256_Init(&ctx->sha256);
114}
115
brian m. carlson768e30e2020-02-22 20:17:27 +0000116static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src)
117{
118 git_SHA256_Clone(&dst->sha256, &src->sha256);
119}
120
brian m. carlson13eeedb2018-11-14 04:09:36 +0000121static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len)
122{
123 git_SHA256_Update(&ctx->sha256, data, len);
124}
125
126static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
127{
128 git_SHA256_Final(hash, &ctx->sha256);
129}
130
brian m. carlsonab795f02021-04-26 01:02:52 +0000131static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
132{
133 git_SHA256_Final(oid->hash, &ctx->sha256);
134 /*
135 * This currently does nothing, so the compiler should optimize it out,
136 * but keep it in case we extend the hash size again.
137 */
138 memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000139 oid->algo = GIT_HASH_SHA256;
brian m. carlsonab795f02021-04-26 01:02:52 +0000140}
141
brian m. carlsonac73ced2018-02-01 02:18:38 +0000142static void git_hash_unknown_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000143{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200144 BUG("trying to init unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000145}
146
brian m. carlson768e30e2020-02-22 20:17:27 +0000147static void git_hash_unknown_clone(git_hash_ctx *dst, const git_hash_ctx *src)
148{
149 BUG("trying to clone unknown hash");
150}
151
brian m. carlsonac73ced2018-02-01 02:18:38 +0000152static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000153{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200154 BUG("trying to update unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000155}
156
brian m. carlsonac73ced2018-02-01 02:18:38 +0000157static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000158{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200159 BUG("trying to finalize unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000160}
161
brian m. carlsonab795f02021-04-26 01:02:52 +0000162static void git_hash_unknown_final_oid(struct object_id *oid, git_hash_ctx *ctx)
163{
164 BUG("trying to finalize unknown hash");
165}
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",
brian m. carlsonf50e7662017-11-12 21:28:52 +0000186 /* "sha1", big-endian */
187 0x73686131,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000188 GIT_SHA1_RAWSZ,
189 GIT_SHA1_HEXSZ,
brian m. carlsona2ce0a72018-11-14 04:09:33 +0000190 GIT_SHA1_BLKSZ,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000191 git_hash_sha1_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000192 git_hash_sha1_clone,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000193 git_hash_sha1_update,
194 git_hash_sha1_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000195 git_hash_sha1_final_oid,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000196 &empty_tree_oid,
197 &empty_blob_oid,
brian m. carlson14228442021-04-26 01:02:56 +0000198 &null_oid_sha1,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000199 },
brian m. carlson13eeedb2018-11-14 04:09:36 +0000200 {
201 "sha256",
202 /* "s256", big-endian */
203 0x73323536,
204 GIT_SHA256_RAWSZ,
205 GIT_SHA256_HEXSZ,
206 GIT_SHA256_BLKSZ,
207 git_hash_sha256_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000208 git_hash_sha256_clone,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000209 git_hash_sha256_update,
210 git_hash_sha256_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000211 git_hash_sha256_final_oid,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000212 &empty_tree_oid_sha256,
213 &empty_blob_oid_sha256,
brian m. carlson14228442021-04-26 01:02:56 +0000214 &null_oid_sha256,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000215 }
brian m. carlsonf50e7662017-11-12 21:28:52 +0000216};
217
brian m. carlson14228442021-04-26 01:02:56 +0000218const struct object_id *null_oid(void)
219{
220 return the_hash_algo->null_oid;
221}
222
brian m. carlsond8a92ce2018-05-02 00:25:54 +0000223const char *empty_tree_oid_hex(void)
224{
225 static char buf[GIT_MAX_HEXSZ + 1];
226 return oid_to_hex_r(buf, the_hash_algo->empty_tree);
227}
228
229const char *empty_blob_oid_hex(void)
230{
231 static char buf[GIT_MAX_HEXSZ + 1];
232 return oid_to_hex_r(buf, the_hash_algo->empty_blob);
233}
234
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000235int hash_algo_by_name(const char *name)
236{
237 int i;
238 if (!name)
239 return GIT_HASH_UNKNOWN;
240 for (i = 1; i < GIT_HASH_NALGOS; i++)
241 if (!strcmp(name, hash_algos[i].name))
242 return i;
243 return GIT_HASH_UNKNOWN;
244}
245
246int hash_algo_by_id(uint32_t format_id)
247{
248 int i;
249 for (i = 1; i < GIT_HASH_NALGOS; i++)
250 if (format_id == hash_algos[i].format_id)
251 return i;
252 return GIT_HASH_UNKNOWN;
253}
254
brian m. carlson95399782019-02-19 00:05:17 +0000255int hash_algo_by_length(int len)
256{
257 int i;
258 for (i = 1; i < GIT_HASH_NALGOS; i++)
259 if (len == hash_algos[i].rawsz)
260 return i;
261 return GIT_HASH_UNKNOWN;
262}
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000263
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700264/*
265 * This is meant to hold a *small* number of objects that you would
Jeff Kingcb1c8d12019-01-07 03:33:52 -0500266 * 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 +0700267 * to write them into the object store (e.g. a browse-only
268 * application).
269 */
270static struct cached_object {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000271 struct object_id oid;
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700272 enum object_type type;
273 void *buf;
274 unsigned long size;
275} *cached_objects;
276static int cached_object_nr, cached_object_alloc;
277
278static struct cached_object empty_tree = {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000279 { EMPTY_TREE_SHA1_BIN_LITERAL },
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700280 OBJ_TREE,
281 "",
282 0
283};
284
brian m. carlson62ba93e2018-05-02 00:26:03 +0000285static struct cached_object *find_cached_object(const struct object_id *oid)
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700286{
287 int i;
288 struct cached_object *co = cached_objects;
289
290 for (i = 0; i < cached_object_nr; i++, co++) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400291 if (oideq(&co->oid, oid))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700292 return co;
293 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400294 if (oideq(oid, the_hash_algo->empty_tree))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700295 return &empty_tree;
296 return NULL;
297}
298
Torsten Bögershausen94729352017-11-16 17:38:28 +0100299
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100300static int get_conv_flags(unsigned flags)
Torsten Bögershausen94729352017-11-16 17:38:28 +0100301{
302 if (flags & HASH_RENORMALIZE)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100303 return CONV_EOL_RENORMALIZE;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100304 else if (flags & HASH_WRITE_OBJECT)
Lars Schneider107642f2018-04-15 20:16:07 +0200305 return global_conv_flags_eol | CONV_WRITE_OBJECT;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100306 else
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100307 return 0;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100308}
309
310
Junio C Hamano90a64642011-03-10 16:02:50 -0800311int mkdir_in_gitdir(const char *path)
312{
313 if (mkdir(path, 0777)) {
314 int saved_errno = errno;
315 struct stat st;
316 struct strbuf sb = STRBUF_INIT;
317
318 if (errno != EEXIST)
319 return -1;
320 /*
321 * Are we looking at a path in a symlinked worktree
322 * whose original repository does not yet have it?
323 * e.g. .git/rr-cache pointing at its original
324 * repository in which the user hasn't performed any
325 * conflict resolution yet?
326 */
327 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
328 strbuf_readlink(&sb, path, st.st_size) ||
329 !is_absolute_path(sb.buf) ||
330 mkdir(sb.buf, 0777)) {
331 strbuf_release(&sb);
332 errno = saved_errno;
333 return -1;
334 }
335 strbuf_release(&sb);
336 }
337 return adjust_shared_perm(path);
338}
339
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300340static enum scld_error safe_create_leading_directories_1(char *path, int share)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700341{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100342 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100343 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800344
Michael Haggerty0be05212014-01-06 14:45:25 +0100345 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100346 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100347 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100348
Michael Haggerty0f527402014-01-19 00:40:44 +0100349 while (*slash && !is_dir_sep(*slash))
350 slash++;
351
352 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700353 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100354
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100355 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100356 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100357 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100358 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700359 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100360
Michael Haggerty0f527402014-01-19 00:40:44 +0100361 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100362 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800363 if (!stat(path, &st)) {
364 /* path exists */
Michael Haggerty204a0472017-01-06 17:22:25 +0100365 if (!S_ISDIR(st.st_mode)) {
366 errno = ENOTDIR;
Michael Haggerty0be05212014-01-06 14:45:25 +0100367 ret = SCLD_EXISTS;
Michael Haggerty204a0472017-01-06 17:22:25 +0100368 }
Michael Haggerty53a39722014-01-06 14:45:19 +0100369 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400370 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100371 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400372 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100373 else if (errno == ENOENT)
374 /*
375 * Either mkdir() failed because
376 * somebody just pruned the containing
377 * directory, or stat() failed because
378 * the file that was in our way was
379 * just removed. Either way, inform
380 * the caller that it might be worth
381 * trying again:
382 */
383 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100384 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100385 ret = SCLD_FAILED;
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300386 } else if (share && adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100387 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800388 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100389 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700390 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100391 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700392}
393
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300394enum scld_error safe_create_leading_directories(char *path)
395{
396 return safe_create_leading_directories_1(path, 1);
397}
398
399enum scld_error safe_create_leading_directories_no_share(char *path)
400{
401 return safe_create_leading_directories_1(path, 0);
402}
403
Michael Haggerty0be05212014-01-06 14:45:25 +0100404enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400405{
Michael Haggerty02944302017-01-06 17:22:24 +0100406 int save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400407 /* path points to cache entries, so xstrdup before messing with it */
408 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100409 enum scld_error result = safe_create_leading_directories(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100410
411 save_errno = errno;
Jeff King8e21d632008-06-25 01:41:34 -0400412 free(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100413 errno = save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400414 return result;
415}
416
Michael Haggerty177978f2017-01-06 17:22:26 +0100417int raceproof_create_file(const char *path, create_file_fn fn, void *cb)
418{
419 /*
420 * The number of times we will try to remove empty directories
421 * in the way of path. This is only 1 because if another
422 * process is racily creating directories that conflict with
423 * us, we don't want to fight against them.
424 */
425 int remove_directories_remaining = 1;
426
427 /*
428 * The number of times that we will try to create the
429 * directories containing path. We are willing to attempt this
430 * more than once, because another process could be trying to
431 * clean up empty directories at the same time as we are
432 * trying to create them.
433 */
434 int create_directories_remaining = 3;
435
436 /* A scratch copy of path, filled lazily if we need it: */
437 struct strbuf path_copy = STRBUF_INIT;
438
439 int ret, save_errno;
440
441 /* Sanity check: */
442 assert(*path);
443
444retry_fn:
445 ret = fn(path, cb);
446 save_errno = errno;
447 if (!ret)
448 goto out;
449
450 if (errno == EISDIR && remove_directories_remaining-- > 0) {
451 /*
452 * A directory is in the way. Maybe it is empty; try
453 * to remove it:
454 */
455 if (!path_copy.len)
456 strbuf_addstr(&path_copy, path);
457
458 if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))
459 goto retry_fn;
460 } else if (errno == ENOENT && create_directories_remaining-- > 0) {
461 /*
462 * Maybe the containing directory didn't exist, or
463 * maybe it was just deleted by a process that is
464 * racing with us to clean up empty directories. Try
465 * to create it:
466 */
467 enum scld_error scld_result;
468
469 if (!path_copy.len)
470 strbuf_addstr(&path_copy, path);
471
472 do {
473 scld_result = safe_create_leading_directories(path_copy.buf);
474 if (scld_result == SCLD_OK)
475 goto retry_fn;
476 } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);
477 }
478
479out:
480 strbuf_release(&path_copy);
481 errno = save_errno;
482 return ret;
483}
484
Jeff King514c5fd2019-01-07 03:35:42 -0500485static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
Junio C Hamanoace15342005-05-07 00:38:04 -0700486{
487 int i;
brian m. carlson94b5e092018-07-16 01:28:07 +0000488 for (i = 0; i < the_hash_algo->rawsz; i++) {
Junio C Hamanoace15342005-05-07 00:38:04 -0700489 static char hex[] = "0123456789abcdef";
Jeff King514c5fd2019-01-07 03:35:42 -0500490 unsigned int val = oid->hash[i];
Jeff Kingf7b77742016-10-03 16:36:09 -0400491 strbuf_addch(buf, hex[val >> 4]);
492 strbuf_addch(buf, hex[val & 0xf]);
Jeff Kingafbba2f2016-10-03 16:35:55 -0400493 if (!i)
Jeff Kingf7b77742016-10-03 16:36:09 -0400494 strbuf_addch(buf, '/');
Junio C Hamanoace15342005-05-07 00:38:04 -0700495 }
496}
497
Jeff Kingf0eaf632018-11-12 09:50:39 -0500498static const char *odb_loose_path(struct object_directory *odb,
499 struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500500 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700501{
Jeff Kingb69fb862018-11-12 09:48:56 -0500502 strbuf_reset(buf);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500503 strbuf_addstr(buf, odb->path);
Christian Couder34498472018-01-18 11:08:54 +0100504 strbuf_addch(buf, '/');
Jeff King514c5fd2019-01-07 03:35:42 -0500505 fill_loose_path(buf, oid);
Jeff King38dbe5f2016-10-03 16:36:04 -0400506 return buf->buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700507}
508
Jeff Kingf3f043a2018-11-12 09:49:35 -0500509const char *loose_object_path(struct repository *r, struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500510 const struct object_id *oid)
Jeff Kingf3f043a2018-11-12 09:49:35 -0500511{
Jeff King514c5fd2019-01-07 03:35:42 -0500512 return odb_loose_path(r->objects->odb, buf, oid);
Jeff Kingf3f043a2018-11-12 09:49:35 -0500513}
514
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700515/*
Jeff King4ea82472016-10-03 16:34:48 -0400516 * Return non-zero iff the path is usable as an alternate object database.
517 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100518static int alt_odb_usable(struct raw_object_store *o,
519 struct strbuf *path,
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000520 const char *normalized_objdir, khiter_t *pos)
Jeff King4ea82472016-10-03 16:34:48 -0400521{
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000522 int r;
Jeff King4ea82472016-10-03 16:34:48 -0400523
524 /* Detect cases where alternate disappeared */
525 if (!is_directory(path->buf)) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200526 error(_("object directory %s does not exist; "
527 "check .git/objects/info/alternates"),
Jeff King4ea82472016-10-03 16:34:48 -0400528 path->buf);
529 return 0;
530 }
531
532 /*
533 * Prevent the common mistake of listing the same
534 * thing twice, or object directory itself.
535 */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000536 if (!o->odb_by_path) {
537 khiter_t p;
Jeff King4ea82472016-10-03 16:34:48 -0400538
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000539 o->odb_by_path = kh_init_odb_path_map();
540 assert(!o->odb->next);
541 p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r);
542 assert(r == 1); /* never used */
543 kh_value(o->odb_by_path, p) = o->odb;
544 }
545 if (fspatheq(path->buf, normalized_objdir))
546 return 0;
547 *pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r);
548 /* r: 0 = exists, 1 = never used, 2 = deleted */
549 return r == 0 ? 0 : 1;
Jeff King4ea82472016-10-03 16:34:48 -0400550}
551
552/*
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700553 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700554 *
555 * The variable alt_odb_list points at the list of struct
Jeff King263db402018-11-12 09:48:47 -0500556 * object_directory. The elements on this list come from
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700557 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
558 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800559 * whose contents is similar to that environment variable but can be
560 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700561 * contains "/the/directory/corresponding/to/.git/objects/...", while
562 * its name points just after the slash at the end of ".git/objects/"
Martin Ågrene5afd442020-12-31 12:56:21 +0100563 * in the example above, and has enough space to hold all hex characters
564 * of the object ID, an extra slash for the first level indirection, and
565 * the terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700566 */
Stefan Beller77f012e2018-03-23 18:21:08 +0100567static void read_info_alternates(struct repository *r,
568 const char *relative_base,
569 int depth);
Eric Wong407532f2021-07-07 23:10:16 +0000570static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100571 const char *relative_base, int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200572{
Jeff King263db402018-11-12 09:48:47 -0500573 struct object_directory *ent;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800574 struct strbuf pathbuf = STRBUF_INIT;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000575 khiter_t pos;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200576
Eric Wong407532f2021-07-07 23:10:16 +0000577 if (!is_absolute_path(entry->buf) && relative_base) {
Brandon Williams4ac90062016-12-12 10:16:55 -0800578 strbuf_realpath(&pathbuf, relative_base, 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800579 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200580 }
Eric Wong407532f2021-07-07 23:10:16 +0000581 strbuf_addbuf(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800582
Jeff King37a95862016-11-07 23:50:17 -0500583 if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200584 error(_("unable to normalize alternate object path: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400585 pathbuf.buf);
586 strbuf_release(&pathbuf);
587 return -1;
588 }
Hui Wang5bdf0a82011-09-07 18:37:47 +0800589
590 /*
591 * The trailing slash after the directory name is given by
592 * this function at the end. Remove duplicates.
593 */
Jeff King4ea82472016-10-03 16:34:48 -0400594 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
595 strbuf_setlen(&pathbuf, pathbuf.len - 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800596
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000597 if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) {
Jeff King4ea82472016-10-03 16:34:48 -0400598 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200599 return -1;
600 }
601
René Scharfeca56dad2021-03-13 17:17:22 +0100602 CALLOC_ARRAY(ent, 1);
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000603 /* pathbuf.buf is already in r->objects->odb_by_path */
604 ent->path = strbuf_detach(&pathbuf, NULL);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200605
606 /* add the alternate entry */
Jeff Kingf0eaf632018-11-12 09:50:39 -0500607 *r->objects->odb_tail = ent;
608 r->objects->odb_tail = &(ent->next);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200609 ent->next = NULL;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000610 assert(r->objects->odb_by_path);
611 kh_value(r->objects->odb_by_path, pos) = ent;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200612
613 /* recursively add alternates */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000614 read_info_alternates(r, ent->path, depth + 1);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200615
Martin Waitzc2f493a2006-05-07 20:19:21 +0200616 return 0;
617}
618
Jeff Kingcf3c6352016-12-12 14:52:22 -0500619static const char *parse_alt_odb_entry(const char *string,
620 int sep,
621 struct strbuf *out)
622{
623 const char *end;
624
625 strbuf_reset(out);
626
627 if (*string == '#') {
628 /* comment; consume up to next separator */
629 end = strchrnul(string, sep);
630 } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
631 /*
632 * quoted path; unquote_c_style has copied the
633 * data for us and set "end". Broken quoting (e.g.,
634 * an entry that doesn't end with a quote) falls
635 * back to the unquoted case below.
636 */
637 } else {
638 /* normal, unquoted path */
639 end = strchrnul(string, sep);
640 strbuf_add(out, string, end - string);
641 }
642
643 if (*end)
644 end++;
645 return end;
646}
647
Stefan Beller77f012e2018-03-23 18:21:08 +0100648static void link_alt_odb_entries(struct repository *r, const char *alt,
649 int sep, const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700650{
Ephrim Khong539e7502014-07-15 13:29:45 +0200651 struct strbuf objdirbuf = STRBUF_INIT;
Jeff Kingcf3c6352016-12-12 14:52:22 -0500652 struct strbuf entry = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200653
Jeff Kingf28e3662017-11-12 10:27:39 +0000654 if (!alt || !*alt)
655 return;
656
Martin Waitzc2f493a2006-05-07 20:19:21 +0200657 if (depth > 5) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200658 error(_("%s: ignoring alternate object stores, nesting too deep"),
Martin Waitzc2f493a2006-05-07 20:19:21 +0200659 relative_base);
660 return;
661 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700662
Jeff Kingf0eaf632018-11-12 09:50:39 -0500663 strbuf_add_absolute_path(&objdirbuf, r->objects->odb->path);
Jeff King670c3592016-10-03 16:34:17 -0400664 if (strbuf_normalize_path(&objdirbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200665 die(_("unable to normalize object directory: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400666 objdirbuf.buf);
Ephrim Khong539e7502014-07-15 13:29:45 +0200667
Jeff Kingcf3c6352016-12-12 14:52:22 -0500668 while (*alt) {
669 alt = parse_alt_odb_entry(alt, sep, &entry);
670 if (!entry.len)
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700671 continue;
Eric Wong407532f2021-07-07 23:10:16 +0000672 link_alt_odb_entry(r, &entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100673 relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700674 }
Jeff Kingcf3c6352016-12-12 14:52:22 -0500675 strbuf_release(&entry);
Ephrim Khong539e7502014-07-15 13:29:45 +0200676 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700677}
678
Stefan Beller77f012e2018-03-23 18:21:08 +0100679static void read_info_alternates(struct repository *r,
680 const char *relative_base,
681 int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700682{
Jeff King5015f012015-08-19 14:12:45 -0400683 char *path;
Jeff Kingdc732bd2017-09-19 15:41:07 -0400684 struct strbuf buf = STRBUF_INIT;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700685
Jeff King5015f012015-08-19 14:12:45 -0400686 path = xstrfmt("%s/info/alternates", relative_base);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400687 if (strbuf_read_file(&buf, path, 1024) < 0) {
Jeff Kingf0f7beb2017-09-19 15:41:10 -0400688 warn_on_fopen_errors(path);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400689 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700690 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700691 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700692
Stefan Beller77f012e2018-03-23 18:21:08 +0100693 link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400694 strbuf_release(&buf);
695 free(path);
Junio C Hamanoace15342005-05-07 00:38:04 -0700696}
697
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400698void add_to_alternates_file(const char *reference)
699{
Martin Ågrenf132a122017-10-05 22:32:03 +0200700 struct lock_file lock = LOCK_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400701 char *alts = git_pathdup("objects/info/alternates");
702 FILE *in, *out;
Martin Ågrenf132a122017-10-05 22:32:03 +0200703 int found = 0;
Jeff King77b9b1d2015-08-10 05:34:46 -0400704
Martin Ågrenf132a122017-10-05 22:32:03 +0200705 hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
706 out = fdopen_lock_file(&lock, "w");
Jeff King77b9b1d2015-08-10 05:34:46 -0400707 if (!out)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200708 die_errno(_("unable to fdopen alternates lockfile"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400709
710 in = fopen(alts, "r");
711 if (in) {
712 struct strbuf line = STRBUF_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400713
Junio C Hamano3f163962015-10-28 13:29:24 -0700714 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400715 if (!strcmp(reference, line.buf)) {
716 found = 1;
717 break;
718 }
719 fprintf_or_die(out, "%s\n", line.buf);
720 }
721
722 strbuf_release(&line);
723 fclose(in);
Jeff King77b9b1d2015-08-10 05:34:46 -0400724 }
725 else if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200726 die_errno(_("unable to read alternates file"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400727
Martin Ågrenf132a122017-10-05 22:32:03 +0200728 if (found) {
729 rollback_lock_file(&lock);
730 } else {
Jeff King77b9b1d2015-08-10 05:34:46 -0400731 fprintf_or_die(out, "%s\n", reference);
Martin Ågrenf132a122017-10-05 22:32:03 +0200732 if (commit_lock_file(&lock))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200733 die_errno(_("unable to move new alternates file into place"));
Jeff Kingf0eaf632018-11-12 09:50:39 -0500734 if (the_repository->objects->loaded_alternates)
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100735 link_alt_odb_entries(the_repository, reference,
736 '\n', NULL, 0);
Jeff King77b9b1d2015-08-10 05:34:46 -0400737 }
738 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400739}
740
Jeff Kinga5b34d22016-10-03 16:35:03 -0400741void add_to_alternates_memory(const char *reference)
742{
743 /*
744 * Make sure alternates are initialized, or else our entry may be
745 * overwritten when they are.
746 */
Stefan Beller0b209032018-03-23 18:21:07 +0100747 prepare_alt_odb(the_repository);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400748
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100749 link_alt_odb_entries(the_repository, reference,
750 '\n', NULL, 0);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400751}
752
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700753/*
754 * Compute the exact path an alternate is at and returns it. In case of
755 * error NULL is returned and the human readable error is added to `err`
Robert P. J. Dayefde7b72018-06-03 10:32:50 -0400756 * `path` may be relative and should point to $GIT_DIR.
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700757 * `err` must not be null.
758 */
759char *compute_alternate_path(const char *path, struct strbuf *err)
760{
761 char *ref_git = NULL;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000762 const char *repo;
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700763 int seen_error = 0;
764
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000765 ref_git = real_pathdup(path, 0);
766 if (!ref_git) {
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700767 seen_error = 1;
768 strbuf_addf(err, _("path '%s' does not exist"), path);
769 goto out;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000770 }
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700771
772 repo = read_gitfile(ref_git);
773 if (!repo)
774 repo = read_gitfile(mkpath("%s/.git", ref_git));
775 if (repo) {
776 free(ref_git);
777 ref_git = xstrdup(repo);
778 }
779
780 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
781 char *ref_git_git = mkpathdup("%s/.git", ref_git);
782 free(ref_git);
783 ref_git = ref_git_git;
784 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
785 struct strbuf sb = STRBUF_INIT;
786 seen_error = 1;
787 if (get_common_dir(&sb, ref_git)) {
788 strbuf_addf(err,
789 _("reference repository '%s' as a linked "
790 "checkout is not supported yet."),
791 path);
792 goto out;
793 }
794
795 strbuf_addf(err, _("reference repository '%s' is not a "
796 "local repository."), path);
797 goto out;
798 }
799
800 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
801 strbuf_addf(err, _("reference repository '%s' is shallow"),
802 path);
803 seen_error = 1;
804 goto out;
805 }
806
807 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
808 strbuf_addf(err,
809 _("reference repository '%s' is grafted"),
810 path);
811 seen_error = 1;
812 goto out;
813 }
814
815out:
816 if (seen_error) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000817 FREE_AND_NULL(ref_git);
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700818 }
819
820 return ref_git;
821}
822
Jeff King709dfa62019-07-01 09:17:40 -0400823static void fill_alternate_refs_command(struct child_process *cmd,
824 const char *repo_path)
825{
826 const char *value;
827
828 if (!git_config_get_value("core.alternateRefsCommand", &value)) {
829 cmd->use_shell = 1;
830
Jeff Kingc972bf42020-07-28 16:25:12 -0400831 strvec_push(&cmd->args, value);
832 strvec_push(&cmd->args, repo_path);
Jeff King709dfa62019-07-01 09:17:40 -0400833 } else {
834 cmd->git_cmd = 1;
835
Jeff Kingc972bf42020-07-28 16:25:12 -0400836 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
837 strvec_push(&cmd->args, "for-each-ref");
838 strvec_push(&cmd->args, "--format=%(objectname)");
Jeff King709dfa62019-07-01 09:17:40 -0400839
840 if (!git_config_get_value("core.alternateRefsPrefixes", &value)) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400841 strvec_push(&cmd->args, "--");
842 strvec_split(&cmd->args, value);
Jeff King709dfa62019-07-01 09:17:40 -0400843 }
844 }
845
846 cmd->env = local_repo_env;
847 cmd->out = -1;
848}
849
850static void read_alternate_refs(const char *path,
851 alternate_ref_fn *cb,
852 void *data)
853{
854 struct child_process cmd = CHILD_PROCESS_INIT;
855 struct strbuf line = STRBUF_INIT;
856 FILE *fh;
857
858 fill_alternate_refs_command(&cmd, path);
859
860 if (start_command(&cmd))
861 return;
862
863 fh = xfdopen(cmd.out, "r");
864 while (strbuf_getline_lf(&line, fh) != EOF) {
865 struct object_id oid;
866 const char *p;
867
868 if (parse_oid_hex(line.buf, &oid, &p) || *p) {
869 warning(_("invalid line while parsing alternate refs: %s"),
870 line.buf);
871 break;
872 }
873
874 cb(&oid, data);
875 }
876
877 fclose(fh);
878 finish_command(&cmd);
René Scharfe86ad3ea2019-08-07 13:15:25 +0200879 strbuf_release(&line);
Jeff King709dfa62019-07-01 09:17:40 -0400880}
881
882struct alternate_refs_data {
883 alternate_ref_fn *fn;
884 void *data;
885};
886
887static int refs_from_alternate_cb(struct object_directory *e,
888 void *data)
889{
890 struct strbuf path = STRBUF_INIT;
891 size_t base_len;
892 struct alternate_refs_data *cb = data;
893
894 if (!strbuf_realpath(&path, e->path, 0))
895 goto out;
896 if (!strbuf_strip_suffix(&path, "/objects"))
897 goto out;
898 base_len = path.len;
899
900 /* Is this a git repository with refs? */
901 strbuf_addstr(&path, "/refs");
902 if (!is_directory(path.buf))
903 goto out;
904 strbuf_setlen(&path, base_len);
905
906 read_alternate_refs(path.buf, cb->fn, cb->data);
907
908out:
909 strbuf_release(&path);
910 return 0;
911}
912
913void for_each_alternate_ref(alternate_ref_fn fn, void *data)
914{
915 struct alternate_refs_data cb;
916 cb.fn = fn;
917 cb.data = data;
918 foreach_alt_odb(refs_from_alternate_cb, &cb);
919}
920
Jeff Kingfe1b2262014-10-15 18:33:13 -0400921int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700922{
Jeff King263db402018-11-12 09:48:47 -0500923 struct object_directory *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400924 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700925
Stefan Beller0b209032018-03-23 18:21:07 +0100926 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500927 for (ent = the_repository->objects->odb->next; ent; ent = ent->next) {
Jeff Kingfe1b2262014-10-15 18:33:13 -0400928 r = fn(ent, cb);
929 if (r)
930 break;
931 }
932 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700933}
934
Stefan Beller13068bf2018-03-23 18:21:09 +0100935void prepare_alt_odb(struct repository *r)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200936{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500937 if (r->objects->loaded_alternates)
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400938 return;
939
Stefan Beller13068bf2018-03-23 18:21:09 +0100940 link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200941
Jeff Kingf0eaf632018-11-12 09:50:39 -0500942 read_info_alternates(r, r->objects->odb->path, 0);
943 r->objects->loaded_alternates = 1;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200944}
945
Jeff King3096b2e2015-07-08 16:33:52 -0400946/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400947static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700948{
luciano.rocha@booking.com312cd762020-04-14 16:27:26 +0200949 return !utime(fn, NULL);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600950}
Junio C Hamanoace15342005-05-07 00:38:04 -0700951
Jeff King3096b2e2015-07-08 16:33:52 -0400952/*
953 * All of the check_and_freshen functions return 1 if the file exists and was
954 * freshened (if freshening was requested), 0 otherwise. If they return
955 * 0, you should not assume that it is safe to skip a write of the object (it
956 * either does not exist on disk, or has a stale mtime and may be subject to
957 * pruning).
958 */
Christian Couder6a5e6f52017-02-27 19:00:11 +0100959int check_and_freshen_file(const char *fn, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400960{
961 if (access(fn, F_OK))
962 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400963 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400964 return 0;
965 return 1;
966}
967
Jeff Kingf0eaf632018-11-12 09:50:39 -0500968static int check_and_freshen_odb(struct object_directory *odb,
969 const struct object_id *oid,
970 int freshen)
971{
972 static struct strbuf path = STRBUF_INIT;
Jeff King514c5fd2019-01-07 03:35:42 -0500973 odb_loose_path(odb, &path, oid);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500974 return check_and_freshen_file(path.buf, freshen);
975}
976
brian m. carlson6862ebb2018-05-02 00:25:34 +0000977static int check_and_freshen_local(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400978{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500979 return check_and_freshen_odb(the_repository->objects->odb, oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400980}
981
brian m. carlson6862ebb2018-05-02 00:25:34 +0000982static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600983{
Jeff King263db402018-11-12 09:48:47 -0500984 struct object_directory *odb;
Jeff Kingf3f043a2018-11-12 09:49:35 -0500985
Stefan Beller0b209032018-03-23 18:21:07 +0100986 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500987 for (odb = the_repository->objects->odb->next; odb; odb = odb->next) {
988 if (check_and_freshen_odb(odb, oid, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700989 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700990 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700991 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700992}
993
brian m. carlson6862ebb2018-05-02 00:25:34 +0000994static int check_and_freshen(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400995{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000996 return check_and_freshen_local(oid, freshen) ||
997 check_and_freshen_nonlocal(oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400998}
999
brian m. carlson6862ebb2018-05-02 00:25:34 +00001000int has_loose_object_nonlocal(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001001{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001002 return check_and_freshen_nonlocal(oid, 0);
Jeff King33d42212014-10-15 18:42:22 -04001003}
1004
brian m. carlson6862ebb2018-05-02 00:25:34 +00001005static int has_loose_object(const struct object_id *oid)
Brandon Casey0f4dc142008-11-09 23:59:57 -06001006{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001007 return check_and_freshen(oid, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -06001008}
1009
Steffen Prohaska02710222014-08-26 17:23:23 +02001010static void mmap_limit_check(size_t length)
1011{
1012 static size_t limit = 0;
1013 if (!limit) {
1014 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
1015 if (!limit)
1016 limit = SIZE_MAX;
1017 }
1018 if (length > limit)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001019 die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
Steffen Prohaska02710222014-08-26 17:23:23 +02001020 (uintmax_t)length, (uintmax_t)limit);
1021}
1022
Jeff King15708562015-05-28 03:56:15 -04001023void *xmmap_gently(void *start, size_t length,
1024 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001025{
Steffen Prohaska02710222014-08-26 17:23:23 +02001026 void *ret;
1027
1028 mmap_limit_check(length);
1029 ret = mmap(start, length, prot, flags, fd, offset);
Jeff King9827d4c2019-08-12 16:50:21 -04001030 if (ret == MAP_FAILED && !length)
1031 ret = NULL;
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001032 return ret;
1033}
1034
Eric Wongdc059292021-06-30 00:01:32 +00001035const char *mmap_os_err(void)
1036{
1037 static const char blank[] = "";
1038#if defined(__linux__)
1039 if (errno == ENOMEM) {
1040 /* this continues an existing error message: */
1041 static const char enomem[] =
1042", check sys.vm.max_map_count and/or RLIMIT_DATA";
1043 return enomem;
1044 }
1045#endif /* OS-specific bits */
1046 return blank;
1047}
1048
Jeff King15708562015-05-28 03:56:15 -04001049void *xmmap(void *start, size_t length,
1050 int prot, int flags, int fd, off_t offset)
1051{
1052 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
1053 if (ret == MAP_FAILED)
Eric Wongdc059292021-06-30 00:01:32 +00001054 die_errno(_("mmap failed%s"), mmap_os_err());
Jeff King15708562015-05-28 03:56:15 -04001055 return ret;
1056}
1057
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001058/*
1059 * With an in-core object data in "map", rehash it to make sure the
Jeff Kingcb1c8d12019-01-07 03:33:52 -05001060 * object name actually matches "oid" to detect object corruption.
1061 * With "map" == NULL, try reading the object named with "oid" using
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001062 * the streaming interface and rehash it to do the same.
1063 */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001064int check_object_signature(struct repository *r, const struct object_id *oid,
1065 void *map, unsigned long size, const char *type)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001066{
Patryk Obaraf070fac2018-01-28 01:13:13 +01001067 struct object_id real_oid;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001068 enum object_type obj_type;
1069 struct git_istream *st;
brian m. carlson18e25882018-02-01 02:18:41 +00001070 git_hash_ctx c;
brian m. carlson1af64f72018-03-12 02:27:55 +00001071 char hdr[MAX_HEADER_LEN];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001072 int hdrlen;
1073
1074 if (map) {
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001075 hash_object_file(r->hash_algo, map, size, type, &real_oid);
Jeff King9001dc22018-08-28 17:22:48 -04001076 return !oideq(oid, &real_oid) ? -1 : 0;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001077 }
1078
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001079 st = open_istream(r, oid, &obj_type, &size, NULL);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001080 if (!st)
1081 return -1;
1082
1083 /* Generate the header */
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01001084 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001085
1086 /* Sha1.. */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001087 r->hash_algo->init_fn(&c);
1088 r->hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001089 for (;;) {
1090 char buf[1024 * 16];
1091 ssize_t readlen = read_istream(st, buf, sizeof(buf));
1092
Jeff Kingf54fac52013-03-25 16:17:17 -04001093 if (readlen < 0) {
1094 close_istream(st);
1095 return -1;
1096 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001097 if (!readlen)
1098 break;
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001099 r->hash_algo->update_fn(&c, buf, readlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001100 }
brian m. carlson5951bf42021-04-26 01:02:53 +00001101 r->hash_algo->final_oid_fn(&real_oid, &c);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001102 close_istream(st);
Jeff King9001dc22018-08-28 17:22:48 -04001103 return !oideq(oid, &real_oid) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001104}
1105
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001106int git_open_cloexec(const char *name, int flags)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001107{
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001108 int fd;
1109 static int o_cloexec = O_CLOEXEC;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001110
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001111 fd = open(name, flags | o_cloexec);
1112 if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
Lars Schneidercd66ada2016-10-24 20:02:59 +02001113 /* Try again w/o O_CLOEXEC: the kernel might not support it */
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001114 o_cloexec &= ~O_CLOEXEC;
1115 fd = open(name, flags | o_cloexec);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001116 }
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001117
Eric Wong9fb94952017-07-15 18:55:40 +00001118#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001119 {
1120 static int fd_cloexec = FD_CLOEXEC;
1121
1122 if (!o_cloexec && 0 <= fd && fd_cloexec) {
1123 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
Eric Wong9fb94952017-07-15 18:55:40 +00001124 int flags = fcntl(fd, F_GETFD);
1125 if (fcntl(fd, F_SETFD, flags | fd_cloexec))
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001126 fd_cloexec = 0;
1127 }
1128 }
1129#endif
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001130 return fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001131}
1132
Jeff King771e7d52017-01-13 12:54:39 -05001133/*
Jeff King514c5fd2019-01-07 03:35:42 -05001134 * Find "oid" as a loose object in the local repository or in an alternate.
Jeff King771e7d52017-01-13 12:54:39 -05001135 * Returns 0 on success, negative on failure.
1136 *
1137 * The "path" out-parameter will give the path of the object we found (if any).
1138 * Note that it may point to static storage and is only valid until another
Jeff King514c5fd2019-01-07 03:35:42 -05001139 * call to stat_loose_object().
Jeff King771e7d52017-01-13 12:54:39 -05001140 */
Jeff King514c5fd2019-01-07 03:35:42 -05001141static int stat_loose_object(struct repository *r, const struct object_id *oid,
1142 struct stat *st, const char **path)
Jeff King052fe5e2013-07-12 02:30:48 -04001143{
Jeff King263db402018-11-12 09:48:47 -05001144 struct object_directory *odb;
Christian Couderea657732018-01-17 18:54:54 +01001145 static struct strbuf buf = STRBUF_INIT;
Jeff King052fe5e2013-07-12 02:30:48 -04001146
Stefan Bellerd2607fa2018-03-23 18:21:17 +01001147 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001148 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001149 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001150 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -04001151 return 0;
1152 }
1153
1154 return -1;
1155}
1156
Jeff King771e7d52017-01-13 12:54:39 -05001157/*
Jeff King514c5fd2019-01-07 03:35:42 -05001158 * Like stat_loose_object(), but actually open the object and return the
Jeff King771e7d52017-01-13 12:54:39 -05001159 * descriptor. See the caveats on the "path" parameter above.
1160 */
Jeff King514c5fd2019-01-07 03:35:42 -05001161static int open_loose_object(struct repository *r,
1162 const struct object_id *oid, const char **path)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001163{
1164 int fd;
Jeff King263db402018-11-12 09:48:47 -05001165 struct object_directory *odb;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001166 int most_interesting_errno = ENOENT;
Christian Couderea657732018-01-17 18:54:54 +01001167 static struct strbuf buf = STRBUF_INIT;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001168
Stefan Bellerec7283e2018-03-23 18:21:18 +01001169 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001170 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001171 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001172 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001173 if (fd >= 0)
1174 return fd;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001175
Jeff Kingd6c8a052014-05-15 04:54:06 -04001176 if (most_interesting_errno == ENOENT)
1177 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001178 }
Jeff Kingd6c8a052014-05-15 04:54:06 -04001179 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001180 return -1;
1181}
1182
Jeff King61c77112018-11-12 09:54:42 -05001183static int quick_has_loose(struct repository *r,
Jeff Kingd7a24572019-01-07 03:37:29 -05001184 const struct object_id *oid)
Jeff King61c77112018-11-12 09:54:42 -05001185{
Jeff King61c77112018-11-12 09:54:42 -05001186 struct object_directory *odb;
1187
Jeff King61c77112018-11-12 09:54:42 -05001188 prepare_alt_odb(r);
1189 for (odb = r->objects->odb; odb; odb = odb->next) {
Eric Wong92d8ed82021-07-07 23:10:19 +00001190 if (oidtree_contains(odb_loose_cache(odb, oid), oid))
Jeff King61c77112018-11-12 09:54:42 -05001191 return 1;
1192 }
1193 return 0;
1194}
1195
Jeff Kingf6371f92017-01-13 12:58:16 -05001196/*
1197 * Map the loose object at "path" if it is not NULL, or the path found by
Jeff King514c5fd2019-01-07 03:35:42 -05001198 * searching for a loose object named "oid".
Jeff Kingf6371f92017-01-13 12:58:16 -05001199 */
Jeff King514c5fd2019-01-07 03:35:42 -05001200static void *map_loose_object_1(struct repository *r, const char *path,
1201 const struct object_id *oid, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001202{
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001203 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -07001204 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -07001205
Jeff Kingf6371f92017-01-13 12:58:16 -05001206 if (path)
1207 fd = git_open(path);
1208 else
Jeff King514c5fd2019-01-07 03:35:42 -05001209 fd = open_loose_object(r, oid, &path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001210 map = NULL;
1211 if (fd >= 0) {
1212 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001213
Linus Torvalds44d1c192008-06-14 11:32:37 -07001214 if (!fstat(fd, &st)) {
1215 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001216 if (!*size) {
1217 /* mmap() is forbidden on empty files */
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001218 error(_("object file %s is empty"), path);
René Scharfe68819252019-01-07 17:48:02 +01001219 close(fd);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001220 return NULL;
1221 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001222 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -07001223 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001224 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001225 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001226 return map;
1227}
1228
Jeff King514c5fd2019-01-07 03:35:42 -05001229void *map_loose_object(struct repository *r,
1230 const struct object_id *oid,
1231 unsigned long *size)
Jeff Kingf6371f92017-01-13 12:58:16 -05001232{
Jeff King514c5fd2019-01-07 03:35:42 -05001233 return map_loose_object_1(r, NULL, oid, size);
Jeff Kingf6371f92017-01-13 12:58:16 -05001234}
1235
Jeff King00a77602019-01-07 03:37:02 -05001236static int unpack_loose_short_header(git_zstream *stream,
1237 unsigned char *map, unsigned long mapsize,
1238 void *buffer, unsigned long bufsiz)
Linus Torvaldsc4483572005-06-01 17:54:59 -07001239{
Matheus Tavares31877c92020-01-15 23:39:53 -03001240 int ret;
1241
Linus Torvaldsc4483572005-06-01 17:54:59 -07001242 /* Get the data stream */
1243 memset(stream, 0, sizeof(*stream));
1244 stream->next_in = map;
1245 stream->avail_in = mapsize;
1246 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -07001247 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -07001248
Linus Torvalds39c68542009-01-07 19:54:47 -08001249 git_inflate_init(stream);
Matheus Tavares31877c92020-01-15 23:39:53 -03001250 obj_read_unlock();
1251 ret = git_inflate(stream, 0);
1252 obj_read_lock();
1253
1254 return ret;
Linus Torvaldsc4483572005-06-01 17:54:59 -07001255}
1256
Jeff King00a77602019-01-07 03:37:02 -05001257int unpack_loose_header(git_zstream *stream,
1258 unsigned char *map, unsigned long mapsize,
1259 void *buffer, unsigned long bufsiz)
Junio C Hamanod21f8422016-09-25 21:29:04 -07001260{
Jeff King00a77602019-01-07 03:37:02 -05001261 int status = unpack_loose_short_header(stream, map, mapsize,
1262 buffer, bufsiz);
Junio C Hamanod21f8422016-09-25 21:29:04 -07001263
1264 if (status < Z_OK)
1265 return status;
1266
1267 /* Make sure we have the terminating NUL */
1268 if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1269 return -1;
1270 return 0;
1271}
1272
Jeff King00a77602019-01-07 03:37:02 -05001273static int unpack_loose_header_to_strbuf(git_zstream *stream, unsigned char *map,
1274 unsigned long mapsize, void *buffer,
1275 unsigned long bufsiz, struct strbuf *header)
Karthik Nayak46f03442015-05-03 19:59:59 +05301276{
1277 int status;
1278
Jeff King00a77602019-01-07 03:37:02 -05001279 status = unpack_loose_short_header(stream, map, mapsize, buffer, bufsiz);
Junio C Hamanod21f8422016-09-25 21:29:04 -07001280 if (status < Z_OK)
1281 return -1;
Karthik Nayak46f03442015-05-03 19:59:59 +05301282
1283 /*
1284 * Check if entire header is unpacked in the first iteration.
1285 */
1286 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1287 return 0;
1288
1289 /*
1290 * buffer[0..bufsiz] was not large enough. Copy the partial
1291 * result out to header, and then append the result of further
1292 * reading the stream.
1293 */
1294 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1295 stream->next_out = buffer;
1296 stream->avail_out = bufsiz;
1297
1298 do {
Matheus Tavares31877c92020-01-15 23:39:53 -03001299 obj_read_unlock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301300 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001301 obj_read_lock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301302 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1303 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1304 return 0;
1305 stream->next_out = buffer;
1306 stream->avail_out = bufsiz;
1307 } while (status != Z_STREAM_END);
1308 return -1;
1309}
1310
Jeff King00a77602019-01-07 03:37:02 -05001311static void *unpack_loose_rest(git_zstream *stream,
1312 void *buffer, unsigned long size,
1313 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001314{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001315 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001316 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001317 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001318 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001319
Linus Torvalds93821bd2006-07-11 12:48:08 -07001320 n = stream->total_out - bytes;
1321 if (n > size)
1322 n = size;
1323 memcpy(buf, (char *) buffer + bytes, n);
1324 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001325 if (bytes <= size) {
1326 /*
1327 * The above condition must be (bytes <= size), not
1328 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001329 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001330 * the input zlib stream may have bytes that express
1331 * "this concludes the stream", and we *do* want to
1332 * eat that input.
1333 *
1334 * Otherwise we would not be able to test that we
1335 * consumed all the input to reach the expected size;
1336 * we also want to check that zlib tells us that all
1337 * went well with status == Z_STREAM_END at the end.
1338 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001339 stream->next_out = buf + bytes;
1340 stream->avail_out = size - bytes;
Matheus Tavares31877c92020-01-15 23:39:53 -03001341 while (status == Z_OK) {
1342 obj_read_unlock();
Linus Torvalds39c68542009-01-07 19:54:47 -08001343 status = git_inflate(stream, Z_FINISH);
Matheus Tavares31877c92020-01-15 23:39:53 -03001344 obj_read_lock();
1345 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001346 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001347 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001348 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001349 return buf;
1350 }
1351
1352 if (status < 0)
Jeff King00a77602019-01-07 03:37:02 -05001353 error(_("corrupt loose object '%s'"), oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001354 else if (stream->avail_in)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001355 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05001356 oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001357 free(buf);
1358 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001359}
1360
Linus Torvalds5180cac2005-06-02 07:57:25 -07001361/*
1362 * We used to just use "sscanf()", but that's actually way
1363 * too permissive for what we want to check. So do an anal
1364 * object header parse by hand.
1365 */
Jeff King00a77602019-01-07 03:37:02 -05001366static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
1367 unsigned int flags)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001368{
Karthik Nayak46f03442015-05-03 19:59:59 +05301369 const char *type_buf = hdr;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001370 unsigned long size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301371 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001372
1373 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301374 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001375 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001376 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001377 for (;;) {
1378 char c = *hdr++;
Junio C Hamanod21f8422016-09-25 21:29:04 -07001379 if (!c)
1380 return -1;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001381 if (c == ' ')
1382 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301383 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001384 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301385
1386 type = type_from_string_gently(type_buf, type_len, 1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001387 if (oi->type_name)
1388 strbuf_add(oi->type_name, type_buf, type_len);
Karthik Nayak46f03442015-05-03 19:59:59 +05301389 /*
1390 * Set type to 0 if its an unknown object and
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001391 * we're obtaining the type using '--allow-unknown-type'
Karthik Nayak46f03442015-05-03 19:59:59 +05301392 * option.
1393 */
Jonathan Tan19fc5e82017-06-21 17:40:18 -07001394 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
Karthik Nayak46f03442015-05-03 19:59:59 +05301395 type = 0;
1396 else if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001397 die(_("invalid object type"));
Karthik Nayak46f03442015-05-03 19:59:59 +05301398 if (oi->typep)
1399 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001400
1401 /*
1402 * The length must follow immediately, and be in canonical
1403 * decimal format (ie "010" is not valid).
1404 */
1405 size = *hdr++ - '0';
1406 if (size > 9)
1407 return -1;
1408 if (size) {
1409 for (;;) {
1410 unsigned long c = *hdr - '0';
1411 if (c > 9)
1412 break;
1413 hdr++;
1414 size = size * 10 + c;
1415 }
1416 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301417
1418 if (oi->sizep)
1419 *oi->sizep = size;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001420
1421 /*
1422 * The length must be followed by a zero byte
1423 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301424 return *hdr ? -1 : type;
1425}
1426
Jeff King00a77602019-01-07 03:37:02 -05001427int parse_loose_header(const char *hdr, unsigned long *sizep)
Karthik Nayak46f03442015-05-03 19:59:59 +05301428{
Jeff King27b5c1a2016-08-11 05:24:35 -04001429 struct object_info oi = OBJECT_INFO_INIT;
Karthik Nayak46f03442015-05-03 19:59:59 +05301430
1431 oi.sizep = sizep;
Jeff King00a77602019-01-07 03:37:02 -05001432 return parse_loose_header_extended(hdr, &oi, 0);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001433}
1434
Jeff King514c5fd2019-01-07 03:35:42 -05001435static int loose_object_info(struct repository *r,
1436 const struct object_id *oid,
1437 struct object_info *oi, int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001438{
Karthik Nayak46f03442015-05-03 19:59:59 +05301439 int status = 0;
1440 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001441 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001442 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00001443 char hdr[MAX_HEADER_LEN];
Karthik Nayak46f03442015-05-03 19:59:59 +05301444 struct strbuf hdrbuf = STRBUF_INIT;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001445 unsigned long size_scratch;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001446
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001447 if (oi->delta_base_oid)
1448 oidclr(oi->delta_base_oid);
Jeff King5d642e72013-12-21 09:24:20 -05001449
Jeff King052fe5e2013-07-12 02:30:48 -04001450 /*
1451 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001452 * need to look inside the object at all. Note that we
1453 * do not optimize out the stat call, even if the
1454 * caller doesn't care about the disk-size, since our
1455 * return value implicitly indicates whether the
1456 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04001457 */
Brandon Williams6ca32f42018-02-14 10:59:23 -08001458 if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
Jeff King771e7d52017-01-13 12:54:39 -05001459 const char *path;
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001460 struct stat st;
Jeff King61c77112018-11-12 09:54:42 -05001461 if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK))
Jeff Kingd7a24572019-01-07 03:37:29 -05001462 return quick_has_loose(r, oid) ? 0 : -1;
Jeff King514c5fd2019-01-07 03:35:42 -05001463 if (stat_loose_object(r, oid, &st, &path) < 0)
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001464 return -1;
1465 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04001466 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04001467 return 0;
1468 }
1469
Jeff King514c5fd2019-01-07 03:35:42 -05001470 map = map_loose_object(r, oid, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001471 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02001472 return -1;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001473
1474 if (!oi->sizep)
1475 oi->sizep = &size_scratch;
1476
Jeff King23c339c2013-07-12 02:37:53 -04001477 if (oi->disk_sizep)
1478 *oi->disk_sizep = mapsize;
Jonathan Tan19fc5e82017-06-21 17:40:18 -07001479 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
Jeff King00a77602019-01-07 03:37:02 -05001480 if (unpack_loose_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001481 status = error(_("unable to unpack %s header with --allow-unknown-type"),
Jeff King514c5fd2019-01-07 03:35:42 -05001482 oid_to_hex(oid));
Jeff King00a77602019-01-07 03:37:02 -05001483 } else if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001484 status = error(_("unable to unpack %s header"),
Jeff King514c5fd2019-01-07 03:35:42 -05001485 oid_to_hex(oid));
Karthik Nayak46f03442015-05-03 19:59:59 +05301486 if (status < 0)
1487 ; /* Do nothing */
1488 else if (hdrbuf.len) {
Jeff King00a77602019-01-07 03:37:02 -05001489 if ((status = parse_loose_header_extended(hdrbuf.buf, oi, flags)) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001490 status = error(_("unable to parse %s header with --allow-unknown-type"),
Jeff King514c5fd2019-01-07 03:35:42 -05001491 oid_to_hex(oid));
Jeff King00a77602019-01-07 03:37:02 -05001492 } else if ((status = parse_loose_header_extended(hdr, oi, flags)) < 0)
Jeff King514c5fd2019-01-07 03:35:42 -05001493 status = error(_("unable to parse %s header"), oid_to_hex(oid));
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001494
Jeff Kingb3ea7dd2017-10-05 01:59:52 -04001495 if (status >= 0 && oi->contentp) {
Jeff King00a77602019-01-07 03:37:02 -05001496 *oi->contentp = unpack_loose_rest(&stream, hdr,
1497 *oi->sizep, oid);
Jeff Kingb3ea7dd2017-10-05 01:59:52 -04001498 if (!*oi->contentp) {
1499 git_inflate_end(&stream);
1500 status = -1;
1501 }
1502 } else
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001503 git_inflate_end(&stream);
1504
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001505 munmap(map, mapsize);
Karthik Nayak46f03442015-05-03 19:59:59 +05301506 if (status && oi->typep)
Jeff King23c339c2013-07-12 02:37:53 -04001507 *oi->typep = status;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001508 if (oi->sizep == &size_scratch)
1509 oi->sizep = NULL;
Karthik Nayak46f03442015-05-03 19:59:59 +05301510 strbuf_release(&hdrbuf);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001511 oi->whence = OI_LOOSE;
Jeff King93cff9a2017-04-01 04:05:21 -04001512 return (status < 0) ? status : 0;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001513}
1514
Matheus Tavares31877c92020-01-15 23:39:53 -03001515int obj_read_use_lock = 0;
1516pthread_mutex_t obj_read_mutex;
1517
1518void enable_obj_read_lock(void)
1519{
1520 if (obj_read_use_lock)
1521 return;
1522
1523 obj_read_use_lock = 1;
1524 init_recursive_mutex(&obj_read_mutex);
1525}
1526
1527void disable_obj_read_lock(void)
1528{
1529 if (!obj_read_use_lock)
1530 return;
1531
1532 obj_read_use_lock = 0;
1533 pthread_mutex_destroy(&obj_read_mutex);
1534}
1535
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001536int fetch_if_missing = 1;
1537
Matheus Tavares31877c92020-01-15 23:39:53 -03001538static int do_oid_object_info_extended(struct repository *r,
1539 const struct object_id *oid,
1540 struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001541{
Jonathan Tancd585e22017-06-21 17:40:23 -07001542 static struct object_info blank_oi = OBJECT_INFO_INIT;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001543 struct cached_object *co;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001544 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04001545 int rtype;
brian m. carlsonb383a132018-03-12 02:27:54 +00001546 const struct object_id *real = oid;
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001547 int already_retried = 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001548
Matheus Tavares31877c92020-01-15 23:39:53 -03001549
brian m. carlsonb383a132018-03-12 02:27:54 +00001550 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
Stefan Beller9d983542018-04-25 11:21:06 -07001551 real = lookup_replace_object(r, oid);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001552
brian m. carlsonb383a132018-03-12 02:27:54 +00001553 if (is_null_oid(real))
Jeff King87b5e232017-11-21 18:17:39 -05001554 return -1;
1555
Jonathan Tancd585e22017-06-21 17:40:23 -07001556 if (!oi)
1557 oi = &blank_oi;
1558
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001559 co = find_cached_object(real);
1560 if (co) {
1561 if (oi->typep)
1562 *(oi->typep) = co->type;
1563 if (oi->sizep)
1564 *(oi->sizep) = co->size;
1565 if (oi->disk_sizep)
1566 *(oi->disk_sizep) = 0;
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001567 if (oi->delta_base_oid)
1568 oidclr(oi->delta_base_oid);
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001569 if (oi->type_name)
1570 strbuf_addstr(oi->type_name, type_name(co->type));
1571 if (oi->contentp)
1572 *oi->contentp = xmemdupz(co->buf, co->size);
1573 oi->whence = OI_CACHED;
1574 return 0;
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07001575 }
1576
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001577 while (1) {
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001578 if (find_pack_entry(r, real, &e))
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001579 break;
1580
Takuto Ikuta024aa462018-03-14 15:32:42 +09001581 if (flags & OBJECT_INFO_IGNORE_LOOSE)
1582 return -1;
1583
Steven Grimmddd63e62008-08-05 13:08:41 -07001584 /* Most likely it's a loose object. */
Jeff King514c5fd2019-01-07 03:35:42 -05001585 if (!loose_object_info(r, real, oi, flags))
Jeff King5b086402013-07-12 02:34:57 -04001586 return 0;
Steven Grimmddd63e62008-08-05 13:08:41 -07001587
1588 /* Not a loose object; someone else may have just packed it. */
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001589 if (!(flags & OBJECT_INFO_QUICK)) {
Stefan Beller9d983542018-04-25 11:21:06 -07001590 reprepare_packed_git(r);
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001591 if (find_pack_entry(r, real, &e))
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001592 break;
1593 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001594
1595 /* Check if it is a missing object */
Jonathan Tanef830cc2021-06-17 10:13:26 -07001596 if (fetch_if_missing && repo_has_promisor_remote(r) &&
1597 !already_retried &&
Derrick Stolee31f52562019-05-28 08:19:07 -07001598 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001599 /*
Christian Couderb14ed5a2019-06-25 15:40:31 +02001600 * TODO Investigate checking promisor_remote_get_direct()
1601 * TODO return value and stopping on error here.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001602 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001603 promisor_remote_get_direct(r, real, 1);
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001604 already_retried = 1;
1605 continue;
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001606 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001607
1608 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001609 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001610
Jonathan Tancd585e22017-06-21 17:40:23 -07001611 if (oi == &blank_oi)
1612 /*
1613 * We know that the caller doesn't actually need the
1614 * information below, so return early.
1615 */
1616 return 0;
Stefan Beller9d983542018-04-25 11:21:06 -07001617 rtype = packed_object_info(r, e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04001618 if (rtype < 0) {
brian m. carlsonb383a132018-03-12 02:27:54 +00001619 mark_bad_packed_object(e.p, real->hash);
Matheus Tavares31877c92020-01-15 23:39:53 -03001620 return do_oid_object_info_extended(r, real, oi, 0);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001621 } else if (oi->whence == OI_PACKED) {
Junio C Hamano9a490592011-05-12 15:51:38 -07001622 oi->u.packed.offset = e.offset;
1623 oi->u.packed.pack = e.p;
1624 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
1625 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001626 }
1627
Jeff King5b086402013-07-12 02:34:57 -04001628 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001629}
1630
Matheus Tavares31877c92020-01-15 23:39:53 -03001631int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1632 struct object_info *oi, unsigned flags)
1633{
1634 int ret;
1635 obj_read_lock();
1636 ret = do_oid_object_info_extended(r, oid, oi, flags);
1637 obj_read_unlock();
1638 return ret;
1639}
1640
1641
Christian Couder3fc0dca2013-10-27 00:34:30 +02001642/* returns enum object_type or negative */
Stefan Beller9d983542018-04-25 11:21:06 -07001643int oid_object_info(struct repository *r,
1644 const struct object_id *oid,
1645 unsigned long *sizep)
Junio C Hamano9a490592011-05-12 15:51:38 -07001646{
Jeff King5b086402013-07-12 02:34:57 -04001647 enum object_type type;
Jeff King27b5c1a2016-08-11 05:24:35 -04001648 struct object_info oi = OBJECT_INFO_INIT;
Junio C Hamano9a490592011-05-12 15:51:38 -07001649
Jeff King5b086402013-07-12 02:34:57 -04001650 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001651 oi.sizep = sizep;
Stefan Beller9d983542018-04-25 11:21:06 -07001652 if (oid_object_info_extended(r, oid, &oi,
1653 OBJECT_INFO_LOOKUP_REPLACE) < 0)
Jeff King5b086402013-07-12 02:34:57 -04001654 return -1;
1655 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001656}
1657
Stefan Beller1b9b5c62018-10-16 16:35:32 -07001658static void *read_object(struct repository *r,
Junio C Hamanocba595a2019-02-06 22:05:27 -08001659 const struct object_id *oid, enum object_type *type,
Jonathan Tanf1d81302017-08-18 15:20:30 -07001660 unsigned long *size)
1661{
1662 struct object_info oi = OBJECT_INFO_INIT;
1663 void *content;
1664 oi.typep = type;
1665 oi.sizep = size;
1666 oi.contentp = &content;
1667
Junio C Hamanocba595a2019-02-06 22:05:27 -08001668 if (oid_object_info_extended(r, oid, &oi, 0) < 0)
Jonathan Tanf1d81302017-08-18 15:20:30 -07001669 return NULL;
1670 return content;
1671}
1672
Patryk Obara829e5c32018-01-28 01:13:11 +01001673int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1674 struct object_id *oid)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001675{
1676 struct cached_object *co;
1677
Matheus Tavares2dcde202020-01-30 17:32:22 -03001678 hash_object_file(the_hash_algo, buf, len, type_name(type), oid);
Jonathan Tana64d2aa2020-07-21 15:50:20 -07001679 if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
1680 find_cached_object(oid))
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001681 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04001682 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001683 co = &cached_objects[cached_object_nr++];
1684 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001685 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08001686 co->buf = xmalloc(len);
1687 memcpy(co->buf, buf, len);
brian m. carlson62ba93e2018-05-02 00:26:03 +00001688 oidcpy(&co->oid, oid);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001689 return 0;
1690}
1691
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001692/*
1693 * This function dies on corrupt objects; the callers who want to
1694 * deal with them should arrange to call read_object() and give error
1695 * messages themselves.
1696 */
Stefan Bellera3b72c82018-11-13 16:12:46 -08001697void *read_object_file_extended(struct repository *r,
1698 const struct object_id *oid,
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001699 enum object_type *type,
1700 unsigned long *size,
1701 int lookup_replace)
Nicolas Pitreac939102008-07-14 21:46:48 -04001702{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001703 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001704 const struct packed_git *p;
Jeff King771e7d52017-01-13 12:54:39 -05001705 const char *path;
1706 struct stat st;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -07001707 const struct object_id *repl = lookup_replace ?
Stefan Bellera3b72c82018-11-13 16:12:46 -08001708 lookup_replace_object(r, oid) : oid;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001709
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001710 errno = 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08001711 data = read_object(r, repl, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07001712 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001713 return data;
Christian Couder68095572009-01-23 10:06:53 +01001714
Matheus Tavares31877c92020-01-15 23:39:53 -03001715 obj_read_lock();
Björn Steinbrink25f3af32011-01-20 21:12:20 +01001716 if (errno && errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001717 die_errno(_("failed to read object %s"), oid_to_hex(oid));
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001718
Christian Couder68095572009-01-23 10:06:53 +01001719 /* die if we replaced an object with one that does not exist */
brian m. carlsonb383a132018-03-12 02:27:54 +00001720 if (repl != oid)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001721 die(_("replacement %s not found for %s"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001722 oid_to_hex(repl), oid_to_hex(oid));
Christian Couder68095572009-01-23 10:06:53 +01001723
Junio C Hamanocba595a2019-02-06 22:05:27 -08001724 if (!stat_loose_object(r, repl, &st, &path))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001725 die(_("loose object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001726 oid_to_hex(repl), path);
Christian Couder68095572009-01-23 10:06:53 +01001727
Stefan Bellera3b72c82018-11-13 16:12:46 -08001728 if ((p = has_packed_and_bad(r, repl->hash)) != NULL)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001729 die(_("packed object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001730 oid_to_hex(repl), p->pack_name);
Matheus Tavares31877c92020-01-15 23:39:53 -03001731 obj_read_unlock();
Christian Couderf5552ae2009-01-23 10:07:01 +01001732
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001733 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001734}
1735
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001736void *read_object_with_reference(struct repository *r,
1737 const struct object_id *oid,
Nicolas Pitre21666f12007-02-26 14:55:59 -05001738 const char *required_type_name,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001739 unsigned long *size,
brian m. carlson02f05472018-03-12 02:27:52 +00001740 struct object_id *actual_oid_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001741{
Nicolas Pitre21666f12007-02-26 14:55:59 -05001742 enum object_type type, required_type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001743 void *buffer;
1744 unsigned long isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001745 struct object_id actual_oid;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001746
Nicolas Pitre21666f12007-02-26 14:55:59 -05001747 required_type = type_from_string(required_type_name);
brian m. carlson02f05472018-03-12 02:27:52 +00001748 oidcpy(&actual_oid, oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001749 while (1) {
1750 int ref_length = -1;
1751 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001752
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001753 buffer = repo_read_object_file(r, &actual_oid, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001754 if (!buffer)
1755 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001756 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001757 *size = isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001758 if (actual_oid_return)
1759 oidcpy(actual_oid_return, &actual_oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001760 return buffer;
1761 }
1762 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001763 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001764 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05001765 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001766 ref_type = "object ";
1767 else {
1768 free(buffer);
1769 return NULL;
1770 }
1771 ref_length = strlen(ref_type);
1772
brian m. carlson94b5e092018-07-16 01:28:07 +00001773 if (ref_length + the_hash_algo->hexsz > isize ||
Martin Koegler50974ec2008-02-18 21:47:52 +01001774 memcmp(buffer, ref_type, ref_length) ||
brian m. carlson02f05472018-03-12 02:27:52 +00001775 get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001776 free(buffer);
1777 return NULL;
1778 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001779 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001780 /* Now we have the ID of the referred-to object in
brian m. carlson02f05472018-03-12 02:27:52 +00001781 * actual_oid. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001782 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001783}
1784
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001785static void write_object_file_prepare(const struct git_hash_algo *algo,
1786 const void *buf, unsigned long len,
Patryk Obaraa09c9852018-01-28 01:13:19 +01001787 const char *type, struct object_id *oid,
1788 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001789{
brian m. carlson18e25882018-02-01 02:18:41 +00001790 git_hash_ctx c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001791
1792 /* Generate the header */
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01001793 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001794
1795 /* Sha1.. */
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001796 algo->init_fn(&c);
1797 algo->update_fn(&c, hdr, *hdrlen);
1798 algo->update_fn(&c, buf, len);
brian m. carlson5951bf42021-04-26 01:02:53 +00001799 algo->final_oid_fn(oid, &c);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001800}
1801
Linus Torvalds230f1322005-10-08 15:54:01 -07001802/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001803 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07001804 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07001805int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001806{
Thomas Raste32c0a92008-09-19 00:24:46 +02001807 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001808
Johannes Schindelin348df162009-04-28 00:32:25 +02001809 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001810 goto try_rename;
1811 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02001812 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001813
1814 /*
1815 * Coda hack - coda doesn't like cross-directory links,
1816 * so we fall back to a rename, which will mean that it
1817 * won't be able to check collisions, but that's not a
1818 * big deal.
1819 *
1820 * The same holds for FAT formatted media.
1821 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07001822 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001823 * left to unlink.
1824 */
1825 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001826 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001827 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07001828 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001829 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001830 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001831 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07001832 if (ret) {
1833 if (ret != EEXIST) {
Jeff King2c319882019-01-07 03:39:33 -05001834 return error_errno(_("unable to write file %s"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001835 }
1836 /* FIXME!!! Collision check here ? */
1837 }
1838
Junio C Hamano3be1f182009-03-27 23:14:39 -07001839out:
Matthieu Moy5256b002010-02-22 23:32:16 +01001840 if (adjust_shared_perm(filename))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001841 return error(_("unable to set permission to '%s'"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001842 return 0;
1843}
1844
Linus Torvalds4d548152006-05-24 08:30:54 -07001845static int write_buffer(int fd, const void *buf, size_t len)
1846{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08001847 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001848 return error_errno(_("file write error"));
Linus Torvalds4d548152006-05-24 08:30:54 -07001849 return 0;
1850}
1851
Matheus Tavares2dcde202020-01-30 17:32:22 -03001852int hash_object_file(const struct git_hash_algo *algo, const void *buf,
1853 unsigned long len, const char *type,
Patryk Obaraf070fac2018-01-28 01:13:13 +01001854 struct object_id *oid)
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001855{
brian m. carlson1af64f72018-03-12 02:27:55 +00001856 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001857 int hdrlen = sizeof(hdr);
Matheus Tavares2dcde202020-01-30 17:32:22 -03001858 write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001859 return 0;
1860}
1861
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001862/* Finalize a file on disk, and close it. */
Jeff King514c5fd2019-01-07 03:35:42 -05001863static void close_loose_object(int fd)
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001864{
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -07001865 if (fsync_object_files)
Jeff King76011352019-01-07 03:39:24 -05001866 fsync_or_die(fd, "loose object file");
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001867 if (close(fd) != 0)
Jeff King76011352019-01-07 03:39:24 -05001868 die_errno(_("error when closing loose object file"));
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001869}
1870
Linus Torvalds5723fe72008-06-14 10:50:12 -07001871/* Size of directory component, including the ending '/' */
1872static inline int directory_size(const char *filename)
1873{
1874 const char *s = strrchr(filename, '/');
1875 if (!s)
1876 return 0;
1877 return s - filename + 1;
1878}
1879
1880/*
1881 * This creates a temporary file in the same directory as the final
1882 * 'filename'
1883 *
1884 * We want to avoid cross-directory filename renames, because those
1885 * can have problems on various filesystems (FAT, NFS, Coda).
1886 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001887static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07001888{
1889 int fd, dirlen = directory_size(filename);
1890
Jeff Kingd4b3d112015-09-24 17:07:49 -04001891 strbuf_reset(tmp);
1892 strbuf_add(tmp, filename, dirlen);
1893 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
1894 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05001895 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04001896 /*
1897 * Make sure the directory exists; note that the contents
1898 * of the buffer are undefined after mkstemp returns an
1899 * error, so we have to rewrite the whole buffer from
1900 * scratch.
1901 */
1902 strbuf_reset(tmp);
1903 strbuf_add(tmp, filename, dirlen - 1);
1904 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01001905 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001906 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07001907 return -1;
1908
1909 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001910 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
1911 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07001912 }
1913 return fd;
1914}
1915
Patryk Obara3fc72812018-01-28 01:13:21 +01001916static int write_loose_object(const struct object_id *oid, char *hdr,
1917 int hdrlen, const void *buf, unsigned long len,
1918 time_t mtime)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001919{
Jeff King915308b2009-01-29 00:56:34 -05001920 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001921 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001922 git_zstream stream;
brian m. carlson18e25882018-02-01 02:18:41 +00001923 git_hash_ctx c;
Patryk Obara3fc72812018-01-28 01:13:21 +01001924 struct object_id parano_oid;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001925 static struct strbuf tmp_file = STRBUF_INIT;
Christian Couderea657732018-01-17 18:54:54 +01001926 static struct strbuf filename = STRBUF_INIT;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001927
Jeff King514c5fd2019-01-07 03:35:42 -05001928 loose_object_path(the_repository, &filename, oid);
Christian Couderea657732018-01-17 18:54:54 +01001929
1930 fd = create_tmpfile(&tmp_file, filename.buf);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001931 if (fd < 0) {
Sam Vilain35243572008-11-14 20:19:34 +13001932 if (errno == EACCES)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001933 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01001934 else
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001935 return error_errno(_("unable to create temporary file"));
Linus Torvaldsaac17942005-05-03 11:46:16 -07001936 }
1937
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001938 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001939 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001940 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001941 stream.avail_out = sizeof(compressed);
brian m. carlson18e25882018-02-01 02:18:41 +00001942 the_hash_algo->init_fn(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001943
1944 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05001945 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001946 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001947 while (git_deflate(&stream, 0) == Z_OK)
1948 ; /* nothing */
brian m. carlson18e25882018-02-01 02:18:41 +00001949 the_hash_algo->update_fn(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001950
1951 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04001952 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001953 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001954 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05001955 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001956 ret = git_deflate(&stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00001957 the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001958 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
Jeff King76011352019-01-07 03:39:24 -05001959 die(_("unable to write loose object file"));
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001960 stream.next_out = compressed;
1961 stream.avail_out = sizeof(compressed);
1962 } while (ret == Z_OK);
1963
Linus Torvaldsac54c272007-03-20 11:38:34 -07001964 if (ret != Z_STREAM_END)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001965 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001966 ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001967 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07001968 if (ret != Z_OK)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001969 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001970 ret);
brian m. carlson5951bf42021-04-26 01:02:53 +00001971 the_hash_algo->final_oid_fn(&parano_oid, &c);
Jeff King9001dc22018-08-28 17:22:48 -04001972 if (!oideq(oid, &parano_oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001973 die(_("confused by unstable object source data for %s"),
Patryk Obara3fc72812018-01-28 01:13:21 +01001974 oid_to_hex(oid));
Linus Torvaldsac54c272007-03-20 11:38:34 -07001975
Jeff King514c5fd2019-01-07 03:35:42 -05001976 close_loose_object(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001977
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001978 if (mtime) {
1979 struct utimbuf utb;
1980 utb.actime = mtime;
1981 utb.modtime = mtime;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001982 if (utime(tmp_file.buf, &utb) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001983 warning_errno(_("failed utime() on %s"), tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001984 }
1985
Christian Couderea657732018-01-17 18:54:54 +01001986 return finalize_object_file(tmp_file.buf, filename.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001987}
Daniel Barkalow8237b182005-04-23 18:47:23 -07001988
brian m. carlson6862ebb2018-05-02 00:25:34 +00001989static int freshen_loose_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001990{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001991 return check_and_freshen(oid, 1);
Jeff King33d42212014-10-15 18:42:22 -04001992}
1993
brian m. carlson6862ebb2018-05-02 00:25:34 +00001994static int freshen_packed_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001995{
1996 struct pack_entry e;
brian m. carlson544443c2018-05-02 00:25:35 +00001997 if (!find_pack_entry(the_repository, oid, &e))
Jeff Kingee1c6c32015-04-20 15:55:00 -04001998 return 0;
1999 if (e.p->freshened)
2000 return 1;
2001 if (!freshen_file(e.p->pack_name))
2002 return 0;
2003 e.p->freshened = 1;
2004 return 1;
Jeff King33d42212014-10-15 18:42:22 -04002005}
2006
Patryk Obaraa09c9852018-01-28 01:13:19 +01002007int write_object_file(const void *buf, unsigned long len, const char *type,
2008 struct object_id *oid)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002009{
brian m. carlson1af64f72018-03-12 02:27:55 +00002010 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04002011 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002012
2013 /* Normally if we have it in the pack then we do not bother writing
2014 * it out into .git/objects/??/?{38} file.
2015 */
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002016 write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
2017 &hdrlen);
brian m. carlson6862ebb2018-05-02 00:25:34 +00002018 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002019 return 0;
Patryk Obara3fc72812018-01-28 01:13:21 +01002020 return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002021}
2022
Patryk Obara1752cbb2018-01-28 01:13:22 +01002023int hash_object_file_literally(const void *buf, unsigned long len,
2024 const char *type, struct object_id *oid,
2025 unsigned flags)
Eric Sunshine0c3db672015-05-04 03:25:15 -04002026{
2027 char *header;
2028 int hdrlen, status = 0;
2029
2030 /* type string, SP, %lu of the length plus NUL must fit this */
brian m. carlson1af64f72018-03-12 02:27:55 +00002031 hdrlen = strlen(type) + MAX_HEADER_LEN;
Jeff Kingef1286d2015-09-24 17:06:42 -04002032 header = xmalloc(hdrlen);
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002033 write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
2034 &hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002035
2036 if (!(flags & HASH_WRITE_OBJECT))
2037 goto cleanup;
brian m. carlson6862ebb2018-05-02 00:25:34 +00002038 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Eric Sunshine0c3db672015-05-04 03:25:15 -04002039 goto cleanup;
Patryk Obara3fc72812018-01-28 01:13:21 +01002040 status = write_loose_object(oid, header, hdrlen, buf, len, 0);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002041
2042cleanup:
2043 free(header);
2044 return status;
2045}
2046
Patryk Obara4bdb70a2018-01-28 01:13:20 +01002047int force_object_loose(const struct object_id *oid, time_t mtime)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002048{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002049 void *buf;
2050 unsigned long len;
2051 enum object_type type;
brian m. carlson1af64f72018-03-12 02:27:55 +00002052 char hdr[MAX_HEADER_LEN];
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002053 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002054 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002055
brian m. carlson6862ebb2018-05-02 00:25:34 +00002056 if (has_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002057 return 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08002058 buf = read_object(the_repository, oid, &type, &len);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002059 if (!buf)
Jeff King2c319882019-01-07 03:39:33 -05002060 return error(_("cannot read object for %s"), oid_to_hex(oid));
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01002061 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
Patryk Obara3fc72812018-01-28 01:13:21 +01002062 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002063 free(buf);
2064
2065 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002066}
2067
Jonathan Tan1d8d9cb2020-08-05 16:06:49 -07002068int has_object(struct repository *r, const struct object_id *oid,
2069 unsigned flags)
2070{
2071 int quick = !(flags & HAS_OBJECT_RECHECK_PACKED);
2072 unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT |
2073 (quick ? OBJECT_INFO_QUICK : 0);
2074
2075 if (!startup_info->have_repository)
2076 return 0;
2077 return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0;
2078}
2079
Junio C Hamanocba595a2019-02-06 22:05:27 -08002080int repo_has_object_file_with_flags(struct repository *r,
2081 const struct object_id *oid, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07002082{
Jonathan Nieder3e8b7d32017-04-11 15:47:13 -07002083 if (!startup_info->have_repository)
2084 return 0;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08002085 return oid_object_info_extended(r, oid, NULL, flags) >= 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07002086}
Junio C Hamano74400e72005-05-01 23:45:49 -07002087
Stefan Beller9b45f492018-11-13 16:12:48 -08002088int repo_has_object_file(struct repository *r,
2089 const struct object_id *oid)
brian m. carlsonb419aa22015-11-10 02:22:19 +00002090{
Junio C Hamanocba595a2019-02-06 22:05:27 -08002091 return repo_has_object_file_with_flags(r, oid, 0);
Jeff King5827a032016-10-13 12:53:44 -04002092}
2093
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002094static void check_tree(const void *buf, size_t size)
2095{
2096 struct tree_desc desc;
2097 struct name_entry entry;
2098
2099 init_tree_desc(&desc, buf, size);
2100 while (tree_entry(&desc, &entry))
2101 /* do nothing
2102 * tree_entry() will die() on malformed entries */
2103 ;
2104}
2105
2106static void check_commit(const void *buf, size_t size)
2107{
2108 struct commit c;
2109 memset(&c, 0, sizeof(c));
Stefan Beller08f4f442018-06-28 18:22:00 -07002110 if (parse_commit_buffer(the_repository, &c, buf, size, 0))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002111 die(_("corrupt commit"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002112}
2113
2114static void check_tag(const void *buf, size_t size)
2115{
2116 struct tag t;
2117 memset(&t, 0, sizeof(t));
Stefan Beller0e740fe2018-06-28 18:22:04 -07002118 if (parse_tag_buffer(the_repository, &t, buf, size))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002119 die(_("corrupt tag"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002120}
2121
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002122static int index_mem(struct index_state *istate,
2123 struct object_id *oid, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002124 enum object_type type,
2125 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02002126{
Linus Torvalds6c510be2007-02-13 11:07:23 -08002127 int ret, re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002128 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07002129
Bryan Larsen7672db22005-07-08 16:51:55 -07002130 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08002131 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08002132
2133 /*
2134 * Convert blobs to git internal format
2135 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002136 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002137 struct strbuf nbuf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002138 if (convert_to_git(istate, path, buf, size, &nbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002139 get_conv_flags(flags))) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002140 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08002141 re_allocated = 1;
2142 }
2143 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002144 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002145 if (type == OBJ_TREE)
2146 check_tree(buf, size);
2147 if (type == OBJ_COMMIT)
2148 check_commit(buf, size);
2149 if (type == OBJ_TAG)
2150 check_tag(buf, size);
2151 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08002152
Bryan Larsen7672db22005-07-08 16:51:55 -07002153 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08002154 ret = write_object_file(buf, size, type_name(type), oid);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02002155 else
Matheus Tavares2dcde202020-01-30 17:32:22 -03002156 ret = hash_object_file(the_hash_algo, buf, size,
2157 type_name(type), oid);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002158 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08002159 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002160 return ret;
2161}
2162
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002163static int index_stream_convert_blob(struct index_state *istate,
2164 struct object_id *oid,
2165 int fd,
2166 const char *path,
2167 unsigned flags)
Steffen Prohaska9035d752014-08-26 17:23:25 +02002168{
2169 int ret;
2170 const int write_object = flags & HASH_WRITE_OBJECT;
2171 struct strbuf sbuf = STRBUF_INIT;
2172
2173 assert(path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002174 assert(would_convert_to_git_filter_fd(istate, path));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002175
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002176 convert_to_git_filter_fd(istate, path, fd, &sbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002177 get_conv_flags(flags));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002178
2179 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08002180 ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
Patryk Obaraa09c9852018-01-28 01:13:19 +01002181 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002182 else
Matheus Tavares2dcde202020-01-30 17:32:22 -03002183 ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
2184 type_name(OBJ_BLOB), oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002185 strbuf_release(&sbuf);
2186 return ret;
2187}
2188
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002189static int index_pipe(struct index_state *istate, struct object_id *oid,
2190 int fd, enum object_type type,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002191 const char *path, unsigned flags)
2192{
2193 struct strbuf sbuf = STRBUF_INIT;
2194 int ret;
2195
2196 if (strbuf_read(&sbuf, fd, 4096) >= 0)
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002197 ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002198 else
2199 ret = -1;
2200 strbuf_release(&sbuf);
2201 return ret;
2202}
2203
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002204#define SMALL_FILE_SIZE (32*1024)
2205
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002206static int index_core(struct index_state *istate,
2207 struct object_id *oid, int fd, size_t size,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002208 enum object_type type, const char *path,
2209 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002210{
2211 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002212
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002213 if (!size) {
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002214 ret = index_mem(istate, oid, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002215 } else if (size <= SMALL_FILE_SIZE) {
2216 char *buf = xmalloc(size);
Jeff King90dca672017-09-27 02:01:07 -04002217 ssize_t read_result = read_in_full(fd, buf, size);
2218 if (read_result < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002219 ret = error_errno(_("read error while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002220 path ? path : "<unknown>");
2221 else if (read_result != size)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002222 ret = error(_("short read while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002223 path ? path : "<unknown>");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002224 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002225 ret = index_mem(istate, oid, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002226 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002227 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002228 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002229 ret = index_mem(istate, oid, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002230 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002231 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002232 return ret;
2233}
2234
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002235/*
Junio C Hamano568508e2011-10-28 14:48:40 -07002236 * This creates one packfile per large blob unless bulk-checkin
2237 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002238 *
2239 * This also bypasses the usual "convert-to-git" dance, and that is on
2240 * purpose. We could write a streaming version of the converting
2241 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05002242 * (or equivalent in-core API described above). However, that is
2243 * somewhat complicated, as we do not know the size of the filter
2244 * result, which we need to know beforehand when writing a git object.
2245 * Since the primary motivation for trying to stream from the working
2246 * tree file and to avoid mmaping it in core is to deal with large
2247 * binary blobs, they generally do not want to get any conversion, and
2248 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002249 */
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002250static int index_stream(struct object_id *oid, int fd, size_t size,
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002251 enum object_type type, const char *path,
2252 unsigned flags)
2253{
brian m. carlson68ee6df2018-03-12 02:27:21 +00002254 return index_bulk_checkin(oid, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002255}
2256
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002257int index_fd(struct index_state *istate, struct object_id *oid,
2258 int fd, struct stat *st,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002259 enum object_type type, const char *path, unsigned flags)
2260{
2261 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002262
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002263 /*
2264 * Call xsize_t() only when needed to avoid potentially unnecessary
2265 * die() for large files.
2266 */
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002267 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path))
2268 ret = index_stream_convert_blob(istate, oid, fd, path, flags);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002269 else if (!S_ISREG(st->st_mode))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002270 ret = index_pipe(istate, oid, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002271 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002272 (path && would_convert_to_git(istate, path)))
2273 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
2274 type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002275 else
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002276 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002277 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002278 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002279 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07002280}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002281
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002282int index_path(struct index_state *istate, struct object_id *oid,
2283 const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002284{
2285 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002286 struct strbuf sb = STRBUF_INIT;
Rene Scharfeea8e0292017-08-30 20:00:29 +02002287 int rc = 0;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002288
2289 switch (st->st_mode & S_IFMT) {
2290 case S_IFREG:
2291 fd = open(path, O_RDONLY);
2292 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002293 return error_errno("open(\"%s\")", path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002294 if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002295 return error(_("%s: failed to insert into database"),
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002296 path);
2297 break;
2298 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002299 if (strbuf_readlink(&sb, path, st->st_size))
2300 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002301 if (!(flags & HASH_WRITE_OBJECT))
Matheus Tavares2dcde202020-01-30 17:32:22 -03002302 hash_object_file(the_hash_algo, sb.buf, sb.len,
2303 blob_type, oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +01002304 else if (write_object_file(sb.buf, sb.len, blob_type, oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002305 rc = error(_("%s: failed to insert into database"), path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002306 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002307 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07002308 case S_IFDIR:
brian m. carlsona98e6102017-10-15 22:07:07 +00002309 return resolve_gitlink_ref(path, "HEAD", oid);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002310 default:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002311 return error(_("%s: unsupported file type"), path);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002312 }
Rene Scharfeea8e0292017-08-30 20:00:29 +02002313 return rc;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002314}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002315
2316int read_pack_header(int fd, struct pack_header *header)
2317{
Jeff Kingf48ecd32017-09-13 14:47:22 -04002318 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
Heikki Orsilac697ad12008-05-03 16:27:26 +03002319 /* "eof before pack header was fully read" */
2320 return PH_ERROR_EOF;
2321
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002322 if (header->hdr_signature != htonl(PACK_SIGNATURE))
2323 /* "protocol error (pack signature mismatch detected)" */
2324 return PH_ERROR_PACK_SIGNATURE;
2325 if (!pack_version_ok(header->hdr_version))
2326 /* "protocol error (pack version unsupported)" */
2327 return PH_ERROR_PROTOCOL;
2328 return 0;
2329}
Jeff King40d52ff2010-04-01 20:05:23 -04002330
brian m. carlsone816caa2018-03-12 02:27:42 +00002331void assert_oid_type(const struct object_id *oid, enum object_type expect)
Jeff King40d52ff2010-04-01 20:05:23 -04002332{
Stefan Beller0df8e962018-04-25 11:20:59 -07002333 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff King40d52ff2010-04-01 20:05:23 -04002334 if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002335 die(_("%s is not a valid object"), oid_to_hex(oid));
Jeff King40d52ff2010-04-01 20:05:23 -04002336 if (type != expect)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002337 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002338 type_name(expect));
Jeff King40d52ff2010-04-01 20:05:23 -04002339}
Jeff King27e1e222014-10-15 18:38:55 -04002340
René Scharfe70c49052017-06-24 16:09:39 +02002341int for_each_file_in_obj_subdir(unsigned int subdir_nr,
René Scharfecc817ca2017-06-22 20:19:48 +02002342 struct strbuf *path,
2343 each_loose_object_fn obj_cb,
2344 each_loose_cruft_fn cruft_cb,
2345 each_loose_subdir_fn subdir_cb,
2346 void *data)
Jeff King27e1e222014-10-15 18:38:55 -04002347{
René Scharfe0375f472017-06-24 14:12:30 +02002348 size_t origlen, baselen;
2349 DIR *dir;
Jeff King27e1e222014-10-15 18:38:55 -04002350 struct dirent *de;
2351 int r = 0;
René Scharfe62a24c82017-10-31 14:50:06 +01002352 struct object_id oid;
Jeff King27e1e222014-10-15 18:38:55 -04002353
René Scharfe70c49052017-06-24 16:09:39 +02002354 if (subdir_nr > 0xff)
2355 BUG("invalid loose object subdirectory: %x", subdir_nr);
2356
René Scharfe0375f472017-06-24 14:12:30 +02002357 origlen = path->len;
2358 strbuf_complete(path, '/');
2359 strbuf_addf(path, "%02x", subdir_nr);
René Scharfe0375f472017-06-24 14:12:30 +02002360
2361 dir = opendir(path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04002362 if (!dir) {
René Scharfe0375f472017-06-24 14:12:30 +02002363 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002364 r = error_errno(_("unable to open %s"), path->buf);
René Scharfe0375f472017-06-24 14:12:30 +02002365 strbuf_setlen(path, origlen);
2366 return r;
Jeff King27e1e222014-10-15 18:38:55 -04002367 }
2368
René Scharfe62a24c82017-10-31 14:50:06 +01002369 oid.hash[0] = subdir_nr;
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002370 strbuf_addch(path, '/');
2371 baselen = path->len;
René Scharfe62a24c82017-10-31 14:50:06 +01002372
Elijah Newrenb548f0f2021-05-12 17:28:22 +00002373 while ((de = readdir_skip_dot_and_dotdot(dir))) {
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002374 size_t namelen;
Jeff King27e1e222014-10-15 18:38:55 -04002375
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002376 namelen = strlen(de->d_name);
Jeff King27e1e222014-10-15 18:38:55 -04002377 strbuf_setlen(path, baselen);
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002378 strbuf_add(path, de->d_name, namelen);
brian m. carlson94b5e092018-07-16 01:28:07 +00002379 if (namelen == the_hash_algo->hexsz - 2 &&
René Scharfe62a24c82017-10-31 14:50:06 +01002380 !hex_to_bytes(oid.hash + 1, de->d_name,
brian m. carlson94b5e092018-07-16 01:28:07 +00002381 the_hash_algo->rawsz - 1)) {
brian m. carlson5a6dce72021-04-26 01:02:55 +00002382 oid_set_algo(&oid, the_hash_algo);
René Scharfe62a24c82017-10-31 14:50:06 +01002383 if (obj_cb) {
2384 r = obj_cb(&oid, path->buf, data);
2385 if (r)
2386 break;
Jeff King27e1e222014-10-15 18:38:55 -04002387 }
René Scharfe62a24c82017-10-31 14:50:06 +01002388 continue;
Jeff King27e1e222014-10-15 18:38:55 -04002389 }
2390
2391 if (cruft_cb) {
2392 r = cruft_cb(de->d_name, path->buf, data);
2393 if (r)
2394 break;
2395 }
2396 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02002397 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04002398
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002399 strbuf_setlen(path, baselen - 1);
Jeff King27e1e222014-10-15 18:38:55 -04002400 if (!r && subdir_cb)
2401 r = subdir_cb(subdir_nr, path->buf, data);
2402
René Scharfe0375f472017-06-24 14:12:30 +02002403 strbuf_setlen(path, origlen);
2404
Jeff King27e1e222014-10-15 18:38:55 -04002405 return r;
2406}
2407
Jeff Kinge6f875e2015-02-08 20:13:22 -05002408int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04002409 each_loose_object_fn obj_cb,
2410 each_loose_cruft_fn cruft_cb,
2411 each_loose_subdir_fn subdir_cb,
2412 void *data)
2413{
Jeff King27e1e222014-10-15 18:38:55 -04002414 int r = 0;
2415 int i;
2416
Jeff King27e1e222014-10-15 18:38:55 -04002417 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05002418 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04002419 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002420 if (r)
2421 break;
2422 }
2423
Jeff Kinge6f875e2015-02-08 20:13:22 -05002424 return r;
2425}
2426
2427int for_each_loose_file_in_objdir(const char *path,
2428 each_loose_object_fn obj_cb,
2429 each_loose_cruft_fn cruft_cb,
2430 each_loose_subdir_fn subdir_cb,
2431 void *data)
2432{
2433 struct strbuf buf = STRBUF_INIT;
2434 int r;
2435
2436 strbuf_addstr(&buf, path);
2437 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
2438 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002439 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05002440
Jeff King27e1e222014-10-15 18:38:55 -04002441 return r;
2442}
Jeff King660c8892014-10-15 18:41:21 -04002443
Jeff Kinga7ff6f52018-08-10 19:09:44 -04002444int for_each_loose_object(each_loose_object_fn cb, void *data,
2445 enum for_each_object_flags flags)
Jeff King660c8892014-10-15 18:41:21 -04002446{
Jeff Kingf0eaf632018-11-12 09:50:39 -05002447 struct object_directory *odb;
Jeff King660c8892014-10-15 18:41:21 -04002448
Jeff Kingf0eaf632018-11-12 09:50:39 -05002449 prepare_alt_odb(the_repository);
2450 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
2451 int r = for_each_loose_file_in_objdir(odb->path, cb, NULL,
2452 NULL, data);
2453 if (r)
2454 return r;
Jeff King660c8892014-10-15 18:41:21 -04002455
Jeff Kingf0eaf632018-11-12 09:50:39 -05002456 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
2457 break;
2458 }
Jeff King1385bb72015-03-27 07:32:41 -04002459
Jeff Kingf0eaf632018-11-12 09:50:39 -05002460 return 0;
Jeff King660c8892014-10-15 18:41:21 -04002461}
2462
Jeff King3a2e0822018-11-12 09:50:56 -05002463static int append_loose_object(const struct object_id *oid, const char *path,
2464 void *data)
2465{
Eric Wong92d8ed82021-07-07 23:10:19 +00002466 oidtree_insert(data, oid);
Jeff King3a2e0822018-11-12 09:50:56 -05002467 return 0;
2468}
2469
Eric Wong92d8ed82021-07-07 23:10:19 +00002470struct oidtree *odb_loose_cache(struct object_directory *odb,
René Scharfe0000d652019-01-06 17:45:30 +01002471 const struct object_id *oid)
2472{
2473 int subdir_nr = oid->hash[0];
Jeff King3a2e0822018-11-12 09:50:56 -05002474 struct strbuf buf = STRBUF_INIT;
Eric Wong33f379e2021-07-07 23:10:17 +00002475 size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]);
2476 size_t word_index = subdir_nr / word_bits;
2477 size_t mask = 1 << (subdir_nr % word_bits);
2478 uint32_t *bitmap;
Jeff King3a2e0822018-11-12 09:50:56 -05002479
2480 if (subdir_nr < 0 ||
Eric Wong33f379e2021-07-07 23:10:17 +00002481 subdir_nr >= bitsizeof(odb->loose_objects_subdir_seen))
Jeff King3a2e0822018-11-12 09:50:56 -05002482 BUG("subdir_nr out of range");
2483
Eric Wong33f379e2021-07-07 23:10:17 +00002484 bitmap = &odb->loose_objects_subdir_seen[word_index];
2485 if (*bitmap & mask)
Eric Wong92d8ed82021-07-07 23:10:19 +00002486 return odb->loose_objects_cache;
2487 if (!odb->loose_objects_cache) {
2488 ALLOC_ARRAY(odb->loose_objects_cache, 1);
2489 oidtree_init(odb->loose_objects_cache);
2490 }
Jeff King3a2e0822018-11-12 09:50:56 -05002491 strbuf_addstr(&buf, odb->path);
2492 for_each_file_in_obj_subdir(subdir_nr, &buf,
2493 append_loose_object,
2494 NULL, NULL,
Eric Wong92d8ed82021-07-07 23:10:19 +00002495 odb->loose_objects_cache);
Eric Wong33f379e2021-07-07 23:10:17 +00002496 *bitmap |= mask;
Jeff King7317aa72018-11-22 12:53:00 -05002497 strbuf_release(&buf);
Eric Wong92d8ed82021-07-07 23:10:19 +00002498 return odb->loose_objects_cache;
Jeff King660c8892014-10-15 18:41:21 -04002499}
2500
René Scharfed4e19e52019-01-06 17:45:39 +01002501void odb_clear_loose_cache(struct object_directory *odb)
2502{
Eric Wong92d8ed82021-07-07 23:10:19 +00002503 oidtree_clear(odb->loose_objects_cache);
2504 FREE_AND_NULL(odb->loose_objects_cache);
René Scharfed4e19e52019-01-06 17:45:39 +01002505 memset(&odb->loose_objects_subdir_seen, 0,
2506 sizeof(odb->loose_objects_subdir_seen));
2507}
2508
Jeff King00a77602019-01-07 03:37:02 -05002509static int check_stream_oid(git_zstream *stream,
2510 const char *hdr,
2511 unsigned long size,
2512 const char *path,
2513 const struct object_id *expected_oid)
Jeff Kingf6371f92017-01-13 12:58:16 -05002514{
brian m. carlson18e25882018-02-01 02:18:41 +00002515 git_hash_ctx c;
Jeff King00a77602019-01-07 03:37:02 -05002516 struct object_id real_oid;
Jeff Kingf6371f92017-01-13 12:58:16 -05002517 unsigned char buf[4096];
2518 unsigned long total_read;
2519 int status = Z_OK;
2520
brian m. carlson18e25882018-02-01 02:18:41 +00002521 the_hash_algo->init_fn(&c);
2522 the_hash_algo->update_fn(&c, hdr, stream->total_out);
Jeff Kingf6371f92017-01-13 12:58:16 -05002523
2524 /*
2525 * We already read some bytes into hdr, but the ones up to the NUL
2526 * do not count against the object's content size.
2527 */
2528 total_read = stream->total_out - strlen(hdr) - 1;
2529
2530 /*
2531 * This size comparison must be "<=" to read the final zlib packets;
Jeff King00a77602019-01-07 03:37:02 -05002532 * see the comment in unpack_loose_rest for details.
Jeff Kingf6371f92017-01-13 12:58:16 -05002533 */
2534 while (total_read <= size &&
Junio C Hamano18ad13e2018-10-31 13:12:12 +09002535 (status == Z_OK ||
2536 (status == Z_BUF_ERROR && !stream->avail_out))) {
Jeff Kingf6371f92017-01-13 12:58:16 -05002537 stream->next_out = buf;
2538 stream->avail_out = sizeof(buf);
2539 if (size - total_read < stream->avail_out)
2540 stream->avail_out = size - total_read;
2541 status = git_inflate(stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00002542 the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
Jeff Kingf6371f92017-01-13 12:58:16 -05002543 total_read += stream->next_out - buf;
2544 }
2545 git_inflate_end(stream);
2546
2547 if (status != Z_STREAM_END) {
Jeff King00a77602019-01-07 03:37:02 -05002548 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002549 return -1;
2550 }
Jeff Kingcce044d2017-01-13 13:00:25 -05002551 if (stream->avail_in) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002552 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05002553 oid_to_hex(expected_oid));
Jeff Kingcce044d2017-01-13 13:00:25 -05002554 return -1;
2555 }
Jeff Kingf6371f92017-01-13 12:58:16 -05002556
brian m. carlson5951bf42021-04-26 01:02:53 +00002557 the_hash_algo->final_oid_fn(&real_oid, &c);
Jeff King00a77602019-01-07 03:37:02 -05002558 if (!oideq(expected_oid, &real_oid)) {
Jeff King01f8d592019-01-07 03:40:34 -05002559 error(_("hash mismatch for %s (expected %s)"), path,
Jeff King00a77602019-01-07 03:37:02 -05002560 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002561 return -1;
2562 }
2563
2564 return 0;
2565}
2566
2567int read_loose_object(const char *path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002568 const struct object_id *expected_oid,
Jeff Kingf6371f92017-01-13 12:58:16 -05002569 enum object_type *type,
2570 unsigned long *size,
2571 void **contents)
2572{
2573 int ret = -1;
Jeff Kingf6371f92017-01-13 12:58:16 -05002574 void *map = NULL;
2575 unsigned long mapsize;
2576 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00002577 char hdr[MAX_HEADER_LEN];
Jeff Kingf6371f92017-01-13 12:58:16 -05002578
2579 *contents = NULL;
2580
Jeff King514c5fd2019-01-07 03:35:42 -05002581 map = map_loose_object_1(the_repository, path, NULL, &mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002582 if (!map) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002583 error_errno(_("unable to mmap %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002584 goto out;
2585 }
2586
Jeff King00a77602019-01-07 03:37:02 -05002587 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002588 error(_("unable to unpack header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002589 goto out;
2590 }
2591
Jeff King00a77602019-01-07 03:37:02 -05002592 *type = parse_loose_header(hdr, size);
Jeff Kingf6371f92017-01-13 12:58:16 -05002593 if (*type < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002594 error(_("unable to parse header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002595 git_inflate_end(&stream);
2596 goto out;
2597 }
2598
Jeff King7ac4f3a2018-05-02 15:44:51 -04002599 if (*type == OBJ_BLOB && *size > big_file_threshold) {
Jeff King00a77602019-01-07 03:37:02 -05002600 if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
Jeff Kingf6371f92017-01-13 12:58:16 -05002601 goto out;
2602 } else {
Jeff King00a77602019-01-07 03:37:02 -05002603 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
Jeff Kingf6371f92017-01-13 12:58:16 -05002604 if (!*contents) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002605 error(_("unable to unpack contents of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002606 git_inflate_end(&stream);
2607 goto out;
2608 }
Matheus Tavaresb98d1882020-01-30 17:32:23 -03002609 if (check_object_signature(the_repository, expected_oid,
2610 *contents, *size,
2611 type_name(*type))) {
Jeff King01f8d592019-01-07 03:40:34 -05002612 error(_("hash mismatch for %s (expected %s)"), path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002613 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002614 free(*contents);
2615 goto out;
2616 }
2617 }
2618
2619 ret = 0; /* everything checks out */
2620
2621out:
2622 if (map)
2623 munmap(map, mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002624 return ret;
2625}