blob: 8be57f48de738a7f61de8b18e10a41e2007ac045 [file] [log] [blame]
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
Martin Ågrene5afd442020-12-31 12:56:21 +01006 * This handles basic git object files - packing, unpacking,
Linus Torvalds0fcfd162005-04-18 13:04:43 -07007 * creation etc.
8 */
Linus Torvalds0fcfd162005-04-18 13:04:43 -07009#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Michael Haggerty6eac50d2012-11-05 09:41:22 +010011#include "string-list.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020012#include "lockfile.h"
Junio C Hamano1f688552005-06-27 03:35:33 -070013#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070014#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020015#include "blob.h"
16#include "commit.h"
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -070017#include "run-command.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020018#include "tag.h"
19#include "tree.h"
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +070020#include "tree-walk.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070021#include "refs.h"
Nicolas Pitre70f5d5d2008-02-28 00:25:19 -050022#include "pack-revindex.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010023#include "hash-lookup.h"
Junio C Hamano568508e2011-10-28 14:48:40 -070024#include "bulk-checkin.h"
Stefan Beller031dc922018-03-23 18:20:57 +010025#include "repository.h"
Stefan Beller47f351e2018-04-11 17:21:06 -070026#include "replace-object.h"
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +070027#include "streaming.h"
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070028#include "dir.h"
Jeff King12d95ef2016-08-22 17:59:42 -040029#include "list.h"
René Scharfec4c6eff2016-09-13 19:54:42 +020030#include "mergesort.h"
Jeff Kingcf3c6352016-12-12 14:52:22 -050031#include "quote.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070032#include "packfile.h"
Stefan Beller90c62152018-03-23 18:20:55 +010033#include "object-store.h"
Christian Couderb14ed5a2019-06-25 15:40:31 +020034#include "promisor-remote.h"
Jonathan Tana35e03d2021-08-16 14:09:51 -070035#include "submodule.h"
Pavel Roskine05db0f2007-01-09 23:07:11 -050036
brian m. carlson1af64f72018-03-12 02:27:55 +000037/* The maximum size for an object header. */
38#define MAX_HEADER_LEN 32
39
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000040
41#define EMPTY_TREE_SHA1_BIN_LITERAL \
42 "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
43 "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
brian m. carlson13eeedb2018-11-14 04:09:36 +000044#define EMPTY_TREE_SHA256_BIN_LITERAL \
45 "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \
46 "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \
47 "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \
48 "\x53\x21"
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000049
50#define EMPTY_BLOB_SHA1_BIN_LITERAL \
51 "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
52 "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
brian m. carlson13eeedb2018-11-14 04:09:36 +000053#define EMPTY_BLOB_SHA256_BIN_LITERAL \
54 "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \
55 "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \
56 "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \
57 "\x18\x13"
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000058
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000059static const struct object_id empty_tree_oid = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000060 .hash = EMPTY_TREE_SHA1_BIN_LITERAL,
61 .algo = GIT_HASH_SHA1,
Jacob Keller8576fde2016-08-31 16:27:18 -070062};
brian m. carlsone1ccd7e2018-05-02 00:26:07 +000063static const struct object_id empty_blob_oid = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000064 .hash = EMPTY_BLOB_SHA1_BIN_LITERAL,
65 .algo = GIT_HASH_SHA1,
Jacob Keller8576fde2016-08-31 16:27:18 -070066};
brian m. carlson14228442021-04-26 01:02:56 +000067static const struct object_id null_oid_sha1 = {
68 .hash = {0},
69 .algo = GIT_HASH_SHA1,
70};
brian m. carlson13eeedb2018-11-14 04:09:36 +000071static const struct object_id empty_tree_oid_sha256 = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000072 .hash = EMPTY_TREE_SHA256_BIN_LITERAL,
73 .algo = GIT_HASH_SHA256,
brian m. carlson13eeedb2018-11-14 04:09:36 +000074};
75static const struct object_id empty_blob_oid_sha256 = {
brian m. carlson5a6dce72021-04-26 01:02:55 +000076 .hash = EMPTY_BLOB_SHA256_BIN_LITERAL,
77 .algo = GIT_HASH_SHA256,
brian m. carlson13eeedb2018-11-14 04:09:36 +000078};
brian m. carlson14228442021-04-26 01:02:56 +000079static const struct object_id null_oid_sha256 = {
80 .hash = {0},
81 .algo = GIT_HASH_SHA256,
82};
Junio C Hamano88cd6212005-09-30 14:02:47 -070083
brian m. carlsonac73ced2018-02-01 02:18:38 +000084static void git_hash_sha1_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000085{
brian m. carlsonac73ced2018-02-01 02:18:38 +000086 git_SHA1_Init(&ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000087}
88
brian m. carlson768e30e2020-02-22 20:17:27 +000089static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src)
90{
91 git_SHA1_Clone(&dst->sha1, &src->sha1);
92}
93
brian m. carlsonac73ced2018-02-01 02:18:38 +000094static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000095{
brian m. carlsonac73ced2018-02-01 02:18:38 +000096 git_SHA1_Update(&ctx->sha1, data, len);
brian m. carlsonf50e7662017-11-12 21:28:52 +000097}
98
brian m. carlsonac73ced2018-02-01 02:18:38 +000099static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000100{
brian m. carlsonac73ced2018-02-01 02:18:38 +0000101 git_SHA1_Final(hash, &ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +0000102}
103
brian m. carlsonab795f02021-04-26 01:02:52 +0000104static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx)
105{
106 git_SHA1_Final(oid->hash, &ctx->sha1);
107 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000108 oid->algo = GIT_HASH_SHA1;
brian m. carlsonab795f02021-04-26 01:02:52 +0000109}
110
brian m. carlson13eeedb2018-11-14 04:09:36 +0000111
112static void git_hash_sha256_init(git_hash_ctx *ctx)
113{
114 git_SHA256_Init(&ctx->sha256);
115}
116
brian m. carlson768e30e2020-02-22 20:17:27 +0000117static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src)
118{
119 git_SHA256_Clone(&dst->sha256, &src->sha256);
120}
121
brian m. carlson13eeedb2018-11-14 04:09:36 +0000122static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len)
123{
124 git_SHA256_Update(&ctx->sha256, data, len);
125}
126
127static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
128{
129 git_SHA256_Final(hash, &ctx->sha256);
130}
131
brian m. carlsonab795f02021-04-26 01:02:52 +0000132static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx)
133{
134 git_SHA256_Final(oid->hash, &ctx->sha256);
135 /*
136 * This currently does nothing, so the compiler should optimize it out,
137 * but keep it in case we extend the hash size again.
138 */
139 memset(oid->hash + GIT_SHA256_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA256_RAWSZ);
brian m. carlson5a6dce72021-04-26 01:02:55 +0000140 oid->algo = GIT_HASH_SHA256;
brian m. carlsonab795f02021-04-26 01:02:52 +0000141}
142
brian m. carlsonac73ced2018-02-01 02:18:38 +0000143static void git_hash_unknown_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000144{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200145 BUG("trying to init unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000146}
147
brian m. carlson768e30e2020-02-22 20:17:27 +0000148static void git_hash_unknown_clone(git_hash_ctx *dst, const git_hash_ctx *src)
149{
150 BUG("trying to clone unknown hash");
151}
152
brian m. carlsonac73ced2018-02-01 02:18:38 +0000153static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000154{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200155 BUG("trying to update unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000156}
157
brian m. carlsonac73ced2018-02-01 02:18:38 +0000158static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +0000159{
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200160 BUG("trying to finalize unknown hash");
brian m. carlsonf50e7662017-11-12 21:28:52 +0000161}
162
brian m. carlsonab795f02021-04-26 01:02:52 +0000163static void git_hash_unknown_final_oid(struct object_id *oid, git_hash_ctx *ctx)
164{
165 BUG("trying to finalize unknown hash");
166}
167
brian m. carlsonf50e7662017-11-12 21:28:52 +0000168const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
169 {
170 NULL,
171 0x00000000,
172 0,
173 0,
brian m. carlsona2ce0a72018-11-14 04:09:33 +0000174 0,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000175 git_hash_unknown_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000176 git_hash_unknown_clone,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000177 git_hash_unknown_update,
178 git_hash_unknown_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000179 git_hash_unknown_final_oid,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000180 NULL,
181 NULL,
brian m. carlson14228442021-04-26 01:02:56 +0000182 NULL,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000183 },
184 {
brian m. carlson1ccf07c2018-10-22 02:43:31 +0000185 "sha1",
Han-Wen Nienhuys27f37962021-08-30 14:57:37 +0000186 GIT_SHA1_FORMAT_ID,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000187 GIT_SHA1_RAWSZ,
188 GIT_SHA1_HEXSZ,
brian m. carlsona2ce0a72018-11-14 04:09:33 +0000189 GIT_SHA1_BLKSZ,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000190 git_hash_sha1_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000191 git_hash_sha1_clone,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000192 git_hash_sha1_update,
193 git_hash_sha1_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000194 git_hash_sha1_final_oid,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000195 &empty_tree_oid,
196 &empty_blob_oid,
brian m. carlson14228442021-04-26 01:02:56 +0000197 &null_oid_sha1,
brian m. carlsonf50e7662017-11-12 21:28:52 +0000198 },
brian m. carlson13eeedb2018-11-14 04:09:36 +0000199 {
200 "sha256",
Han-Wen Nienhuys27f37962021-08-30 14:57:37 +0000201 GIT_SHA256_FORMAT_ID,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000202 GIT_SHA256_RAWSZ,
203 GIT_SHA256_HEXSZ,
204 GIT_SHA256_BLKSZ,
205 git_hash_sha256_init,
brian m. carlson768e30e2020-02-22 20:17:27 +0000206 git_hash_sha256_clone,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000207 git_hash_sha256_update,
208 git_hash_sha256_final,
brian m. carlsonab795f02021-04-26 01:02:52 +0000209 git_hash_sha256_final_oid,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000210 &empty_tree_oid_sha256,
211 &empty_blob_oid_sha256,
brian m. carlson14228442021-04-26 01:02:56 +0000212 &null_oid_sha256,
brian m. carlson13eeedb2018-11-14 04:09:36 +0000213 }
brian m. carlsonf50e7662017-11-12 21:28:52 +0000214};
215
brian m. carlson14228442021-04-26 01:02:56 +0000216const struct object_id *null_oid(void)
217{
218 return the_hash_algo->null_oid;
219}
220
brian m. carlsond8a92ce2018-05-02 00:25:54 +0000221const char *empty_tree_oid_hex(void)
222{
223 static char buf[GIT_MAX_HEXSZ + 1];
224 return oid_to_hex_r(buf, the_hash_algo->empty_tree);
225}
226
227const char *empty_blob_oid_hex(void)
228{
229 static char buf[GIT_MAX_HEXSZ + 1];
230 return oid_to_hex_r(buf, the_hash_algo->empty_blob);
231}
232
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000233int hash_algo_by_name(const char *name)
234{
235 int i;
236 if (!name)
237 return GIT_HASH_UNKNOWN;
238 for (i = 1; i < GIT_HASH_NALGOS; i++)
239 if (!strcmp(name, hash_algos[i].name))
240 return i;
241 return GIT_HASH_UNKNOWN;
242}
243
244int hash_algo_by_id(uint32_t format_id)
245{
246 int i;
247 for (i = 1; i < GIT_HASH_NALGOS; i++)
248 if (format_id == hash_algos[i].format_id)
249 return i;
250 return GIT_HASH_UNKNOWN;
251}
252
brian m. carlson95399782019-02-19 00:05:17 +0000253int hash_algo_by_length(int len)
254{
255 int i;
256 for (i = 1; i < GIT_HASH_NALGOS; i++)
257 if (len == hash_algos[i].rawsz)
258 return i;
259 return GIT_HASH_UNKNOWN;
260}
brian m. carlson2f90b9d2018-10-22 02:43:32 +0000261
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700262/*
263 * This is meant to hold a *small* number of objects that you would
Jeff Kingcb1c8d12019-01-07 03:33:52 -0500264 * want read_object_file() to be able to return, but yet you do not want
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700265 * to write them into the object store (e.g. a browse-only
266 * application).
267 */
268static struct cached_object {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000269 struct object_id oid;
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700270 enum object_type type;
271 void *buf;
272 unsigned long size;
273} *cached_objects;
274static int cached_object_nr, cached_object_alloc;
275
276static struct cached_object empty_tree = {
brian m. carlson62ba93e2018-05-02 00:26:03 +0000277 { EMPTY_TREE_SHA1_BIN_LITERAL },
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700278 OBJ_TREE,
279 "",
280 0
281};
282
brian m. carlson62ba93e2018-05-02 00:26:03 +0000283static struct cached_object *find_cached_object(const struct object_id *oid)
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700284{
285 int i;
286 struct cached_object *co = cached_objects;
287
288 for (i = 0; i < cached_object_nr; i++, co++) {
Jeff King4a7e27e2018-08-28 17:22:40 -0400289 if (oideq(&co->oid, oid))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700290 return co;
291 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400292 if (oideq(oid, the_hash_algo->empty_tree))
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700293 return &empty_tree;
294 return NULL;
295}
296
Torsten Bögershausen94729352017-11-16 17:38:28 +0100297
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100298static int get_conv_flags(unsigned flags)
Torsten Bögershausen94729352017-11-16 17:38:28 +0100299{
300 if (flags & HASH_RENORMALIZE)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100301 return CONV_EOL_RENORMALIZE;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100302 else if (flags & HASH_WRITE_OBJECT)
Lars Schneider107642f2018-04-15 20:16:07 +0200303 return global_conv_flags_eol | CONV_WRITE_OBJECT;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100304 else
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100305 return 0;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100306}
307
308
Junio C Hamano90a64642011-03-10 16:02:50 -0800309int mkdir_in_gitdir(const char *path)
310{
311 if (mkdir(path, 0777)) {
312 int saved_errno = errno;
313 struct stat st;
314 struct strbuf sb = STRBUF_INIT;
315
316 if (errno != EEXIST)
317 return -1;
318 /*
319 * Are we looking at a path in a symlinked worktree
320 * whose original repository does not yet have it?
321 * e.g. .git/rr-cache pointing at its original
322 * repository in which the user hasn't performed any
323 * conflict resolution yet?
324 */
325 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
326 strbuf_readlink(&sb, path, st.st_size) ||
327 !is_absolute_path(sb.buf) ||
328 mkdir(sb.buf, 0777)) {
329 strbuf_release(&sb);
330 errno = saved_errno;
331 return -1;
332 }
333 strbuf_release(&sb);
334 }
335 return adjust_shared_perm(path);
336}
337
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300338static enum scld_error safe_create_leading_directories_1(char *path, int share)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700339{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100340 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100341 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800342
Michael Haggerty0be05212014-01-06 14:45:25 +0100343 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100344 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100345 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100346
Michael Haggerty0f527402014-01-19 00:40:44 +0100347 while (*slash && !is_dir_sep(*slash))
348 slash++;
349
350 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700351 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100352
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100353 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100354 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100355 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100356 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700357 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100358
Michael Haggerty0f527402014-01-19 00:40:44 +0100359 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100360 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800361 if (!stat(path, &st)) {
362 /* path exists */
Michael Haggerty204a0472017-01-06 17:22:25 +0100363 if (!S_ISDIR(st.st_mode)) {
364 errno = ENOTDIR;
Michael Haggerty0be05212014-01-06 14:45:25 +0100365 ret = SCLD_EXISTS;
Michael Haggerty204a0472017-01-06 17:22:25 +0100366 }
Michael Haggerty53a39722014-01-06 14:45:19 +0100367 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400368 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100369 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400370 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100371 else if (errno == ENOENT)
372 /*
373 * Either mkdir() failed because
374 * somebody just pruned the containing
375 * directory, or stat() failed because
376 * the file that was in our way was
377 * just removed. Either way, inform
378 * the caller that it might be worth
379 * trying again:
380 */
381 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100382 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100383 ret = SCLD_FAILED;
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300384 } else if (share && adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100385 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800386 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100387 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700388 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100389 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700390}
391
Matheus Tavareseb3c0272020-12-01 20:45:04 -0300392enum scld_error safe_create_leading_directories(char *path)
393{
394 return safe_create_leading_directories_1(path, 1);
395}
396
397enum scld_error safe_create_leading_directories_no_share(char *path)
398{
399 return safe_create_leading_directories_1(path, 0);
400}
401
Michael Haggerty0be05212014-01-06 14:45:25 +0100402enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400403{
Michael Haggerty02944302017-01-06 17:22:24 +0100404 int save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400405 /* path points to cache entries, so xstrdup before messing with it */
406 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100407 enum scld_error result = safe_create_leading_directories(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100408
409 save_errno = errno;
Jeff King8e21d632008-06-25 01:41:34 -0400410 free(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100411 errno = save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400412 return result;
413}
414
Jeff King514c5fd2019-01-07 03:35:42 -0500415static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
Junio C Hamanoace15342005-05-07 00:38:04 -0700416{
417 int i;
brian m. carlson94b5e092018-07-16 01:28:07 +0000418 for (i = 0; i < the_hash_algo->rawsz; i++) {
Junio C Hamanoace15342005-05-07 00:38:04 -0700419 static char hex[] = "0123456789abcdef";
Jeff King514c5fd2019-01-07 03:35:42 -0500420 unsigned int val = oid->hash[i];
Jeff Kingf7b77742016-10-03 16:36:09 -0400421 strbuf_addch(buf, hex[val >> 4]);
422 strbuf_addch(buf, hex[val & 0xf]);
Jeff Kingafbba2f2016-10-03 16:35:55 -0400423 if (!i)
Jeff Kingf7b77742016-10-03 16:36:09 -0400424 strbuf_addch(buf, '/');
Junio C Hamanoace15342005-05-07 00:38:04 -0700425 }
426}
427
Jeff Kingf0eaf632018-11-12 09:50:39 -0500428static const char *odb_loose_path(struct object_directory *odb,
429 struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500430 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700431{
Jeff Kingb69fb862018-11-12 09:48:56 -0500432 strbuf_reset(buf);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500433 strbuf_addstr(buf, odb->path);
Christian Couder34498472018-01-18 11:08:54 +0100434 strbuf_addch(buf, '/');
Jeff King514c5fd2019-01-07 03:35:42 -0500435 fill_loose_path(buf, oid);
Jeff King38dbe5f2016-10-03 16:36:04 -0400436 return buf->buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700437}
438
Jeff Kingf3f043a2018-11-12 09:49:35 -0500439const char *loose_object_path(struct repository *r, struct strbuf *buf,
Jeff King514c5fd2019-01-07 03:35:42 -0500440 const struct object_id *oid)
Jeff Kingf3f043a2018-11-12 09:49:35 -0500441{
Jeff King514c5fd2019-01-07 03:35:42 -0500442 return odb_loose_path(r->objects->odb, buf, oid);
Jeff Kingf3f043a2018-11-12 09:49:35 -0500443}
444
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700445/*
Jeff King4ea82472016-10-03 16:34:48 -0400446 * Return non-zero iff the path is usable as an alternate object database.
447 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100448static int alt_odb_usable(struct raw_object_store *o,
449 struct strbuf *path,
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000450 const char *normalized_objdir, khiter_t *pos)
Jeff King4ea82472016-10-03 16:34:48 -0400451{
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000452 int r;
Jeff King4ea82472016-10-03 16:34:48 -0400453
454 /* Detect cases where alternate disappeared */
455 if (!is_directory(path->buf)) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200456 error(_("object directory %s does not exist; "
457 "check .git/objects/info/alternates"),
Jeff King4ea82472016-10-03 16:34:48 -0400458 path->buf);
459 return 0;
460 }
461
462 /*
463 * Prevent the common mistake of listing the same
464 * thing twice, or object directory itself.
465 */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000466 if (!o->odb_by_path) {
467 khiter_t p;
Jeff King4ea82472016-10-03 16:34:48 -0400468
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000469 o->odb_by_path = kh_init_odb_path_map();
470 assert(!o->odb->next);
471 p = kh_put_odb_path_map(o->odb_by_path, o->odb->path, &r);
472 assert(r == 1); /* never used */
473 kh_value(o->odb_by_path, p) = o->odb;
474 }
475 if (fspatheq(path->buf, normalized_objdir))
476 return 0;
477 *pos = kh_put_odb_path_map(o->odb_by_path, path->buf, &r);
478 /* r: 0 = exists, 1 = never used, 2 = deleted */
479 return r == 0 ? 0 : 1;
Jeff King4ea82472016-10-03 16:34:48 -0400480}
481
482/*
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700483 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700484 *
485 * The variable alt_odb_list points at the list of struct
Jeff King263db402018-11-12 09:48:47 -0500486 * object_directory. The elements on this list come from
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700487 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
488 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800489 * whose contents is similar to that environment variable but can be
490 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700491 * contains "/the/directory/corresponding/to/.git/objects/...", while
492 * its name points just after the slash at the end of ".git/objects/"
Martin Ågrene5afd442020-12-31 12:56:21 +0100493 * in the example above, and has enough space to hold all hex characters
494 * of the object ID, an extra slash for the first level indirection, and
495 * the terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700496 */
Stefan Beller77f012e2018-03-23 18:21:08 +0100497static void read_info_alternates(struct repository *r,
498 const char *relative_base,
499 int depth);
Eric Wong407532f2021-07-07 23:10:16 +0000500static int link_alt_odb_entry(struct repository *r, const struct strbuf *entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100501 const char *relative_base, int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200502{
Jeff King263db402018-11-12 09:48:47 -0500503 struct object_directory *ent;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800504 struct strbuf pathbuf = STRBUF_INIT;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000505 khiter_t pos;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200506
Eric Wong407532f2021-07-07 23:10:16 +0000507 if (!is_absolute_path(entry->buf) && relative_base) {
Brandon Williams4ac90062016-12-12 10:16:55 -0800508 strbuf_realpath(&pathbuf, relative_base, 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800509 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200510 }
Eric Wong407532f2021-07-07 23:10:16 +0000511 strbuf_addbuf(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800512
Jeff King37a95862016-11-07 23:50:17 -0500513 if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200514 error(_("unable to normalize alternate object path: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400515 pathbuf.buf);
516 strbuf_release(&pathbuf);
517 return -1;
518 }
Hui Wang5bdf0a82011-09-07 18:37:47 +0800519
520 /*
521 * The trailing slash after the directory name is given by
522 * this function at the end. Remove duplicates.
523 */
Jeff King4ea82472016-10-03 16:34:48 -0400524 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
525 strbuf_setlen(&pathbuf, pathbuf.len - 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800526
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000527 if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir, &pos)) {
Jeff King4ea82472016-10-03 16:34:48 -0400528 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200529 return -1;
530 }
531
René Scharfeca56dad2021-03-13 17:17:22 +0100532 CALLOC_ARRAY(ent, 1);
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000533 /* pathbuf.buf is already in r->objects->odb_by_path */
534 ent->path = strbuf_detach(&pathbuf, NULL);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200535
536 /* add the alternate entry */
Jeff Kingf0eaf632018-11-12 09:50:39 -0500537 *r->objects->odb_tail = ent;
538 r->objects->odb_tail = &(ent->next);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200539 ent->next = NULL;
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000540 assert(r->objects->odb_by_path);
541 kh_value(r->objects->odb_by_path, pos) = ent;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200542
543 /* recursively add alternates */
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000544 read_info_alternates(r, ent->path, depth + 1);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200545
Martin Waitzc2f493a2006-05-07 20:19:21 +0200546 return 0;
547}
548
Jeff Kingcf3c6352016-12-12 14:52:22 -0500549static const char *parse_alt_odb_entry(const char *string,
550 int sep,
551 struct strbuf *out)
552{
553 const char *end;
554
555 strbuf_reset(out);
556
557 if (*string == '#') {
558 /* comment; consume up to next separator */
559 end = strchrnul(string, sep);
560 } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
561 /*
562 * quoted path; unquote_c_style has copied the
563 * data for us and set "end". Broken quoting (e.g.,
564 * an entry that doesn't end with a quote) falls
565 * back to the unquoted case below.
566 */
567 } else {
568 /* normal, unquoted path */
569 end = strchrnul(string, sep);
570 strbuf_add(out, string, end - string);
571 }
572
573 if (*end)
574 end++;
575 return end;
576}
577
Stefan Beller77f012e2018-03-23 18:21:08 +0100578static void link_alt_odb_entries(struct repository *r, const char *alt,
579 int sep, const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700580{
Ephrim Khong539e7502014-07-15 13:29:45 +0200581 struct strbuf objdirbuf = STRBUF_INIT;
Jeff Kingcf3c6352016-12-12 14:52:22 -0500582 struct strbuf entry = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200583
Jeff Kingf28e3662017-11-12 10:27:39 +0000584 if (!alt || !*alt)
585 return;
586
Martin Waitzc2f493a2006-05-07 20:19:21 +0200587 if (depth > 5) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200588 error(_("%s: ignoring alternate object stores, nesting too deep"),
Martin Waitzc2f493a2006-05-07 20:19:21 +0200589 relative_base);
590 return;
591 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700592
Jeff Kingf0eaf632018-11-12 09:50:39 -0500593 strbuf_add_absolute_path(&objdirbuf, r->objects->odb->path);
Jeff King670c3592016-10-03 16:34:17 -0400594 if (strbuf_normalize_path(&objdirbuf) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200595 die(_("unable to normalize object directory: %s"),
Jeff King670c3592016-10-03 16:34:17 -0400596 objdirbuf.buf);
Ephrim Khong539e7502014-07-15 13:29:45 +0200597
Jeff Kingcf3c6352016-12-12 14:52:22 -0500598 while (*alt) {
599 alt = parse_alt_odb_entry(alt, sep, &entry);
600 if (!entry.len)
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700601 continue;
Eric Wong407532f2021-07-07 23:10:16 +0000602 link_alt_odb_entry(r, &entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100603 relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700604 }
Jeff Kingcf3c6352016-12-12 14:52:22 -0500605 strbuf_release(&entry);
Ephrim Khong539e7502014-07-15 13:29:45 +0200606 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700607}
608
Stefan Beller77f012e2018-03-23 18:21:08 +0100609static void read_info_alternates(struct repository *r,
610 const char *relative_base,
611 int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700612{
Jeff King5015f012015-08-19 14:12:45 -0400613 char *path;
Jeff Kingdc732bd2017-09-19 15:41:07 -0400614 struct strbuf buf = STRBUF_INIT;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700615
Jeff King5015f012015-08-19 14:12:45 -0400616 path = xstrfmt("%s/info/alternates", relative_base);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400617 if (strbuf_read_file(&buf, path, 1024) < 0) {
Jeff Kingf0f7beb2017-09-19 15:41:10 -0400618 warn_on_fopen_errors(path);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400619 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700620 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700621 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700622
Stefan Beller77f012e2018-03-23 18:21:08 +0100623 link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400624 strbuf_release(&buf);
625 free(path);
Junio C Hamanoace15342005-05-07 00:38:04 -0700626}
627
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400628void add_to_alternates_file(const char *reference)
629{
Martin Ågrenf132a122017-10-05 22:32:03 +0200630 struct lock_file lock = LOCK_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400631 char *alts = git_pathdup("objects/info/alternates");
632 FILE *in, *out;
Martin Ågrenf132a122017-10-05 22:32:03 +0200633 int found = 0;
Jeff King77b9b1d2015-08-10 05:34:46 -0400634
Martin Ågrenf132a122017-10-05 22:32:03 +0200635 hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
636 out = fdopen_lock_file(&lock, "w");
Jeff King77b9b1d2015-08-10 05:34:46 -0400637 if (!out)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200638 die_errno(_("unable to fdopen alternates lockfile"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400639
640 in = fopen(alts, "r");
641 if (in) {
642 struct strbuf line = STRBUF_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400643
Junio C Hamano3f163962015-10-28 13:29:24 -0700644 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400645 if (!strcmp(reference, line.buf)) {
646 found = 1;
647 break;
648 }
649 fprintf_or_die(out, "%s\n", line.buf);
650 }
651
652 strbuf_release(&line);
653 fclose(in);
Jeff King77b9b1d2015-08-10 05:34:46 -0400654 }
655 else if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200656 die_errno(_("unable to read alternates file"));
Jeff King77b9b1d2015-08-10 05:34:46 -0400657
Martin Ågrenf132a122017-10-05 22:32:03 +0200658 if (found) {
659 rollback_lock_file(&lock);
660 } else {
Jeff King77b9b1d2015-08-10 05:34:46 -0400661 fprintf_or_die(out, "%s\n", reference);
Martin Ågrenf132a122017-10-05 22:32:03 +0200662 if (commit_lock_file(&lock))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +0200663 die_errno(_("unable to move new alternates file into place"));
Jeff Kingf0eaf632018-11-12 09:50:39 -0500664 if (the_repository->objects->loaded_alternates)
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100665 link_alt_odb_entries(the_repository, reference,
666 '\n', NULL, 0);
Jeff King77b9b1d2015-08-10 05:34:46 -0400667 }
668 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400669}
670
Jeff Kinga5b34d22016-10-03 16:35:03 -0400671void add_to_alternates_memory(const char *reference)
672{
673 /*
674 * Make sure alternates are initialized, or else our entry may be
675 * overwritten when they are.
676 */
Stefan Beller0b209032018-03-23 18:21:07 +0100677 prepare_alt_odb(the_repository);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400678
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100679 link_alt_odb_entries(the_repository, reference,
680 '\n', NULL, 0);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400681}
682
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000683struct object_directory *set_temporary_primary_odb(const char *dir, int will_destroy)
684{
685 struct object_directory *new_odb;
686
687 /*
688 * Make sure alternates are initialized, or else our entry may be
689 * overwritten when they are.
690 */
691 prepare_alt_odb(the_repository);
692
693 /*
694 * Make a new primary odb and link the old primary ODB in as an
695 * alternate
696 */
697 new_odb = xcalloc(1, sizeof(*new_odb));
698 new_odb->path = xstrdup(dir);
Neeraj Singhecd81df2021-12-06 22:05:05 +0000699
700 /*
701 * Disable ref updates while a temporary odb is active, since
702 * the objects in the database may roll back.
703 */
704 new_odb->disable_ref_updates = 1;
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000705 new_odb->will_destroy = will_destroy;
706 new_odb->next = the_repository->objects->odb;
707 the_repository->objects->odb = new_odb;
708 return new_odb->next;
709}
710
711void restore_primary_odb(struct object_directory *restore_odb, const char *old_path)
712{
713 struct object_directory *cur_odb = the_repository->objects->odb;
714
715 if (strcmp(old_path, cur_odb->path))
716 BUG("expected %s as primary object store; found %s",
717 old_path, cur_odb->path);
718
719 if (cur_odb->next != restore_odb)
720 BUG("we expect the old primary object store to be the first alternate");
721
722 the_repository->objects->odb = restore_odb;
723 free_object_directory(cur_odb);
724}
725
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700726/*
727 * Compute the exact path an alternate is at and returns it. In case of
728 * error NULL is returned and the human readable error is added to `err`
Robert P. J. Dayefde7b72018-06-03 10:32:50 -0400729 * `path` may be relative and should point to $GIT_DIR.
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700730 * `err` must not be null.
731 */
732char *compute_alternate_path(const char *path, struct strbuf *err)
733{
734 char *ref_git = NULL;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000735 const char *repo;
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700736 int seen_error = 0;
737
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000738 ref_git = real_pathdup(path, 0);
739 if (!ref_git) {
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700740 seen_error = 1;
741 strbuf_addf(err, _("path '%s' does not exist"), path);
742 goto out;
Alexandr Miloslavskiy4530a852020-03-10 13:11:23 +0000743 }
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700744
745 repo = read_gitfile(ref_git);
746 if (!repo)
747 repo = read_gitfile(mkpath("%s/.git", ref_git));
748 if (repo) {
749 free(ref_git);
750 ref_git = xstrdup(repo);
751 }
752
753 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
754 char *ref_git_git = mkpathdup("%s/.git", ref_git);
755 free(ref_git);
756 ref_git = ref_git_git;
757 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
758 struct strbuf sb = STRBUF_INIT;
759 seen_error = 1;
760 if (get_common_dir(&sb, ref_git)) {
761 strbuf_addf(err,
762 _("reference repository '%s' as a linked "
763 "checkout is not supported yet."),
764 path);
765 goto out;
766 }
767
768 strbuf_addf(err, _("reference repository '%s' is not a "
769 "local repository."), path);
770 goto out;
771 }
772
773 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
774 strbuf_addf(err, _("reference repository '%s' is shallow"),
775 path);
776 seen_error = 1;
777 goto out;
778 }
779
780 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
781 strbuf_addf(err,
782 _("reference repository '%s' is grafted"),
783 path);
784 seen_error = 1;
785 goto out;
786 }
787
788out:
789 if (seen_error) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000790 FREE_AND_NULL(ref_git);
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700791 }
792
793 return ref_git;
794}
795
Taylor Blauf57a7392021-09-01 16:34:01 -0400796struct object_directory *find_odb(struct repository *r, const char *obj_dir)
797{
798 struct object_directory *odb;
799 char *obj_dir_real = real_pathdup(obj_dir, 1);
800 struct strbuf odb_path_real = STRBUF_INIT;
801
802 prepare_alt_odb(r);
803 for (odb = r->objects->odb; odb; odb = odb->next) {
804 strbuf_realpath(&odb_path_real, odb->path, 1);
805 if (!strcmp(obj_dir_real, odb_path_real.buf))
806 break;
807 }
808
809 free(obj_dir_real);
810 strbuf_release(&odb_path_real);
811
812 if (!odb)
813 die(_("could not find object directory matching %s"), obj_dir);
814 return odb;
815}
816
Jeff King709dfa62019-07-01 09:17:40 -0400817static void fill_alternate_refs_command(struct child_process *cmd,
818 const char *repo_path)
819{
820 const char *value;
821
822 if (!git_config_get_value("core.alternateRefsCommand", &value)) {
823 cmd->use_shell = 1;
824
Jeff Kingc972bf42020-07-28 16:25:12 -0400825 strvec_push(&cmd->args, value);
826 strvec_push(&cmd->args, repo_path);
Jeff King709dfa62019-07-01 09:17:40 -0400827 } else {
828 cmd->git_cmd = 1;
829
Jeff Kingc972bf42020-07-28 16:25:12 -0400830 strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
831 strvec_push(&cmd->args, "for-each-ref");
832 strvec_push(&cmd->args, "--format=%(objectname)");
Jeff King709dfa62019-07-01 09:17:40 -0400833
834 if (!git_config_get_value("core.alternateRefsPrefixes", &value)) {
Jeff Kingc972bf42020-07-28 16:25:12 -0400835 strvec_push(&cmd->args, "--");
836 strvec_split(&cmd->args, value);
Jeff King709dfa62019-07-01 09:17:40 -0400837 }
838 }
839
Ævar Arnfjörð Bjarmasonc7c4bde2021-11-25 23:52:24 +0100840 strvec_pushv(&cmd->env_array, (const char **)local_repo_env);
Jeff King709dfa62019-07-01 09:17:40 -0400841 cmd->out = -1;
842}
843
844static void read_alternate_refs(const char *path,
845 alternate_ref_fn *cb,
846 void *data)
847{
848 struct child_process cmd = CHILD_PROCESS_INIT;
849 struct strbuf line = STRBUF_INIT;
850 FILE *fh;
851
852 fill_alternate_refs_command(&cmd, path);
853
854 if (start_command(&cmd))
855 return;
856
857 fh = xfdopen(cmd.out, "r");
858 while (strbuf_getline_lf(&line, fh) != EOF) {
859 struct object_id oid;
860 const char *p;
861
862 if (parse_oid_hex(line.buf, &oid, &p) || *p) {
863 warning(_("invalid line while parsing alternate refs: %s"),
864 line.buf);
865 break;
866 }
867
868 cb(&oid, data);
869 }
870
871 fclose(fh);
872 finish_command(&cmd);
René Scharfe86ad3ea2019-08-07 13:15:25 +0200873 strbuf_release(&line);
Jeff King709dfa62019-07-01 09:17:40 -0400874}
875
876struct alternate_refs_data {
877 alternate_ref_fn *fn;
878 void *data;
879};
880
881static int refs_from_alternate_cb(struct object_directory *e,
882 void *data)
883{
884 struct strbuf path = STRBUF_INIT;
885 size_t base_len;
886 struct alternate_refs_data *cb = data;
887
888 if (!strbuf_realpath(&path, e->path, 0))
889 goto out;
890 if (!strbuf_strip_suffix(&path, "/objects"))
891 goto out;
892 base_len = path.len;
893
894 /* Is this a git repository with refs? */
895 strbuf_addstr(&path, "/refs");
896 if (!is_directory(path.buf))
897 goto out;
898 strbuf_setlen(&path, base_len);
899
900 read_alternate_refs(path.buf, cb->fn, cb->data);
901
902out:
903 strbuf_release(&path);
904 return 0;
905}
906
907void for_each_alternate_ref(alternate_ref_fn fn, void *data)
908{
909 struct alternate_refs_data cb;
910 cb.fn = fn;
911 cb.data = data;
912 foreach_alt_odb(refs_from_alternate_cb, &cb);
913}
914
Jeff Kingfe1b2262014-10-15 18:33:13 -0400915int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700916{
Jeff King263db402018-11-12 09:48:47 -0500917 struct object_directory *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400918 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700919
Stefan Beller0b209032018-03-23 18:21:07 +0100920 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500921 for (ent = the_repository->objects->odb->next; ent; ent = ent->next) {
Jeff Kingfe1b2262014-10-15 18:33:13 -0400922 r = fn(ent, cb);
923 if (r)
924 break;
925 }
926 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700927}
928
Stefan Beller13068bf2018-03-23 18:21:09 +0100929void prepare_alt_odb(struct repository *r)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200930{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500931 if (r->objects->loaded_alternates)
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400932 return;
933
Stefan Beller13068bf2018-03-23 18:21:09 +0100934 link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200935
Jeff Kingf0eaf632018-11-12 09:50:39 -0500936 read_info_alternates(r, r->objects->odb->path, 0);
937 r->objects->loaded_alternates = 1;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200938}
939
Jeff King3096b2e2015-07-08 16:33:52 -0400940/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400941static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700942{
luciano.rocha@booking.com312cd762020-04-14 16:27:26 +0200943 return !utime(fn, NULL);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600944}
Junio C Hamanoace15342005-05-07 00:38:04 -0700945
Jeff King3096b2e2015-07-08 16:33:52 -0400946/*
947 * All of the check_and_freshen functions return 1 if the file exists and was
948 * freshened (if freshening was requested), 0 otherwise. If they return
949 * 0, you should not assume that it is safe to skip a write of the object (it
950 * either does not exist on disk, or has a stale mtime and may be subject to
951 * pruning).
952 */
Christian Couder6a5e6f52017-02-27 19:00:11 +0100953int check_and_freshen_file(const char *fn, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400954{
955 if (access(fn, F_OK))
956 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400957 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400958 return 0;
959 return 1;
960}
961
Jeff Kingf0eaf632018-11-12 09:50:39 -0500962static int check_and_freshen_odb(struct object_directory *odb,
963 const struct object_id *oid,
964 int freshen)
965{
966 static struct strbuf path = STRBUF_INIT;
Jeff King514c5fd2019-01-07 03:35:42 -0500967 odb_loose_path(odb, &path, oid);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500968 return check_and_freshen_file(path.buf, freshen);
969}
970
brian m. carlson6862ebb2018-05-02 00:25:34 +0000971static int check_and_freshen_local(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400972{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500973 return check_and_freshen_odb(the_repository->objects->odb, oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400974}
975
brian m. carlson6862ebb2018-05-02 00:25:34 +0000976static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600977{
Jeff King263db402018-11-12 09:48:47 -0500978 struct object_directory *odb;
Jeff Kingf3f043a2018-11-12 09:49:35 -0500979
Stefan Beller0b209032018-03-23 18:21:07 +0100980 prepare_alt_odb(the_repository);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500981 for (odb = the_repository->objects->odb->next; odb; odb = odb->next) {
982 if (check_and_freshen_odb(odb, oid, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700983 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700984 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700985 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700986}
987
brian m. carlson6862ebb2018-05-02 00:25:34 +0000988static int check_and_freshen(const struct object_id *oid, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400989{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000990 return check_and_freshen_local(oid, freshen) ||
991 check_and_freshen_nonlocal(oid, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400992}
993
brian m. carlson6862ebb2018-05-02 00:25:34 +0000994int has_loose_object_nonlocal(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -0400995{
brian m. carlson6862ebb2018-05-02 00:25:34 +0000996 return check_and_freshen_nonlocal(oid, 0);
Jeff King33d42212014-10-15 18:42:22 -0400997}
998
brian m. carlson6862ebb2018-05-02 00:25:34 +0000999static int has_loose_object(const struct object_id *oid)
Brandon Casey0f4dc142008-11-09 23:59:57 -06001000{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001001 return check_and_freshen(oid, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -06001002}
1003
Steffen Prohaska02710222014-08-26 17:23:23 +02001004static void mmap_limit_check(size_t length)
1005{
1006 static size_t limit = 0;
1007 if (!limit) {
1008 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
1009 if (!limit)
1010 limit = SIZE_MAX;
1011 }
1012 if (length > limit)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001013 die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
Steffen Prohaska02710222014-08-26 17:23:23 +02001014 (uintmax_t)length, (uintmax_t)limit);
1015}
1016
Jeff King15708562015-05-28 03:56:15 -04001017void *xmmap_gently(void *start, size_t length,
1018 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001019{
Steffen Prohaska02710222014-08-26 17:23:23 +02001020 void *ret;
1021
1022 mmap_limit_check(length);
1023 ret = mmap(start, length, prot, flags, fd, offset);
Jeff King9827d4c2019-08-12 16:50:21 -04001024 if (ret == MAP_FAILED && !length)
1025 ret = NULL;
Jonathan Nieder58ecbd52010-11-06 06:44:11 -05001026 return ret;
1027}
1028
Eric Wongdc059292021-06-30 00:01:32 +00001029const char *mmap_os_err(void)
1030{
1031 static const char blank[] = "";
1032#if defined(__linux__)
1033 if (errno == ENOMEM) {
1034 /* this continues an existing error message: */
1035 static const char enomem[] =
1036", check sys.vm.max_map_count and/or RLIMIT_DATA";
1037 return enomem;
1038 }
1039#endif /* OS-specific bits */
1040 return blank;
1041}
1042
Jeff King15708562015-05-28 03:56:15 -04001043void *xmmap(void *start, size_t length,
1044 int prot, int flags, int fd, off_t offset)
1045{
1046 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
1047 if (ret == MAP_FAILED)
Eric Wongdc059292021-06-30 00:01:32 +00001048 die_errno(_("mmap failed%s"), mmap_os_err());
Jeff King15708562015-05-28 03:56:15 -04001049 return ret;
1050}
1051
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001052/*
1053 * With an in-core object data in "map", rehash it to make sure the
Jeff Kingcb1c8d12019-01-07 03:33:52 -05001054 * object name actually matches "oid" to detect object corruption.
1055 * With "map" == NULL, try reading the object named with "oid" using
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001056 * the streaming interface and rehash it to do the same.
1057 */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001058int check_object_signature(struct repository *r, const struct object_id *oid,
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001059 void *map, unsigned long size, const char *type,
1060 struct object_id *real_oidp)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001061{
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001062 struct object_id tmp;
1063 struct object_id *real_oid = real_oidp ? real_oidp : &tmp;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001064 enum object_type obj_type;
1065 struct git_istream *st;
brian m. carlson18e25882018-02-01 02:18:41 +00001066 git_hash_ctx c;
brian m. carlson1af64f72018-03-12 02:27:55 +00001067 char hdr[MAX_HEADER_LEN];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001068 int hdrlen;
1069
1070 if (map) {
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001071 hash_object_file(r->hash_algo, map, size, type, real_oid);
1072 return !oideq(oid, real_oid) ? -1 : 0;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001073 }
1074
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001075 st = open_istream(r, oid, &obj_type, &size, NULL);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001076 if (!st)
1077 return -1;
1078
1079 /* Generate the header */
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01001080 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 +07001081
1082 /* Sha1.. */
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001083 r->hash_algo->init_fn(&c);
1084 r->hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001085 for (;;) {
1086 char buf[1024 * 16];
1087 ssize_t readlen = read_istream(st, buf, sizeof(buf));
1088
Jeff Kingf54fac52013-03-25 16:17:17 -04001089 if (readlen < 0) {
1090 close_istream(st);
1091 return -1;
1092 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001093 if (!readlen)
1094 break;
Matheus Tavaresb98d1882020-01-30 17:32:23 -03001095 r->hash_algo->update_fn(&c, buf, readlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001096 }
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001097 r->hash_algo->final_oid_fn(real_oid, &c);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +07001098 close_istream(st);
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02001099 return !oideq(oid, real_oid) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001100}
1101
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001102int git_open_cloexec(const char *name, int flags)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001103{
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001104 int fd;
1105 static int o_cloexec = O_CLOEXEC;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001106
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001107 fd = open(name, flags | o_cloexec);
1108 if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
Lars Schneidercd66ada2016-10-24 20:02:59 +02001109 /* Try again w/o O_CLOEXEC: the kernel might not support it */
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001110 o_cloexec &= ~O_CLOEXEC;
1111 fd = open(name, flags | o_cloexec);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001112 }
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001113
Eric Wong9fb94952017-07-15 18:55:40 +00001114#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001115 {
1116 static int fd_cloexec = FD_CLOEXEC;
1117
1118 if (!o_cloexec && 0 <= fd && fd_cloexec) {
1119 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
Eric Wong9fb94952017-07-15 18:55:40 +00001120 int flags = fcntl(fd, F_GETFD);
1121 if (fcntl(fd, F_SETFD, flags | fd_cloexec))
Junio C Hamano1e3001a2016-10-31 10:41:41 -07001122 fd_cloexec = 0;
1123 }
1124 }
1125#endif
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -07001126 return fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001127}
1128
Jeff King771e7d52017-01-13 12:54:39 -05001129/*
Jeff King514c5fd2019-01-07 03:35:42 -05001130 * Find "oid" as a loose object in the local repository or in an alternate.
Jeff King771e7d52017-01-13 12:54:39 -05001131 * Returns 0 on success, negative on failure.
1132 *
1133 * The "path" out-parameter will give the path of the object we found (if any).
1134 * Note that it may point to static storage and is only valid until another
Jeff King514c5fd2019-01-07 03:35:42 -05001135 * call to stat_loose_object().
Jeff King771e7d52017-01-13 12:54:39 -05001136 */
Jeff King514c5fd2019-01-07 03:35:42 -05001137static int stat_loose_object(struct repository *r, const struct object_id *oid,
1138 struct stat *st, const char **path)
Jeff King052fe5e2013-07-12 02:30:48 -04001139{
Jeff King263db402018-11-12 09:48:47 -05001140 struct object_directory *odb;
Christian Couderea657732018-01-17 18:54:54 +01001141 static struct strbuf buf = STRBUF_INIT;
Jeff King052fe5e2013-07-12 02:30:48 -04001142
Stefan Bellerd2607fa2018-03-23 18:21:17 +01001143 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001144 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001145 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001146 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -04001147 return 0;
1148 }
1149
1150 return -1;
1151}
1152
Jeff King771e7d52017-01-13 12:54:39 -05001153/*
Jeff King514c5fd2019-01-07 03:35:42 -05001154 * Like stat_loose_object(), but actually open the object and return the
Jeff King771e7d52017-01-13 12:54:39 -05001155 * descriptor. See the caveats on the "path" parameter above.
1156 */
Jeff King514c5fd2019-01-07 03:35:42 -05001157static int open_loose_object(struct repository *r,
1158 const struct object_id *oid, const char **path)
Linus Torvalds44d1c192008-06-14 11:32:37 -07001159{
1160 int fd;
Jeff King263db402018-11-12 09:48:47 -05001161 struct object_directory *odb;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001162 int most_interesting_errno = ENOENT;
Christian Couderea657732018-01-17 18:54:54 +01001163 static struct strbuf buf = STRBUF_INIT;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001164
Stefan Bellerec7283e2018-03-23 18:21:18 +01001165 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -05001166 for (odb = r->objects->odb; odb; odb = odb->next) {
Jeff King514c5fd2019-01-07 03:35:42 -05001167 *path = odb_loose_path(odb, &buf, oid);
Jeff King771e7d52017-01-13 12:54:39 -05001168 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001169 if (fd >= 0)
1170 return fd;
Jeff Kingf0eaf632018-11-12 09:50:39 -05001171
Jeff Kingd6c8a052014-05-15 04:54:06 -04001172 if (most_interesting_errno == ENOENT)
1173 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001174 }
Jeff Kingd6c8a052014-05-15 04:54:06 -04001175 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -07001176 return -1;
1177}
1178
Jeff King61c77112018-11-12 09:54:42 -05001179static int quick_has_loose(struct repository *r,
Jeff Kingd7a24572019-01-07 03:37:29 -05001180 const struct object_id *oid)
Jeff King61c77112018-11-12 09:54:42 -05001181{
Jeff King61c77112018-11-12 09:54:42 -05001182 struct object_directory *odb;
1183
Jeff King61c77112018-11-12 09:54:42 -05001184 prepare_alt_odb(r);
1185 for (odb = r->objects->odb; odb; odb = odb->next) {
Eric Wong92d8ed82021-07-07 23:10:19 +00001186 if (oidtree_contains(odb_loose_cache(odb, oid), oid))
Jeff King61c77112018-11-12 09:54:42 -05001187 return 1;
1188 }
1189 return 0;
1190}
1191
Jeff Kingf6371f92017-01-13 12:58:16 -05001192/*
1193 * Map the loose object at "path" if it is not NULL, or the path found by
Jeff King514c5fd2019-01-07 03:35:42 -05001194 * searching for a loose object named "oid".
Jeff Kingf6371f92017-01-13 12:58:16 -05001195 */
Jeff King514c5fd2019-01-07 03:35:42 -05001196static void *map_loose_object_1(struct repository *r, const char *path,
1197 const struct object_id *oid, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001198{
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001199 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -07001200 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -07001201
Jeff Kingf6371f92017-01-13 12:58:16 -05001202 if (path)
1203 fd = git_open(path);
1204 else
Jeff King514c5fd2019-01-07 03:35:42 -05001205 fd = open_loose_object(r, oid, &path);
Linus Torvalds44d1c192008-06-14 11:32:37 -07001206 map = NULL;
1207 if (fd >= 0) {
1208 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001209
Linus Torvalds44d1c192008-06-14 11:32:37 -07001210 if (!fstat(fd, &st)) {
1211 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001212 if (!*size) {
1213 /* mmap() is forbidden on empty files */
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001214 error(_("object file %s is empty"), path);
René Scharfe68819252019-01-07 17:48:02 +01001215 close(fd);
Matthieu Moy33e42de2012-02-06 17:24:52 +01001216 return NULL;
1217 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001218 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -07001219 }
Linus Torvalds44d1c192008-06-14 11:32:37 -07001220 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001221 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001222 return map;
1223}
1224
Jeff King514c5fd2019-01-07 03:35:42 -05001225void *map_loose_object(struct repository *r,
1226 const struct object_id *oid,
1227 unsigned long *size)
Jeff Kingf6371f92017-01-13 12:58:16 -05001228{
Jeff King514c5fd2019-01-07 03:35:42 -05001229 return map_loose_object_1(r, NULL, oid, size);
Jeff Kingf6371f92017-01-13 12:58:16 -05001230}
1231
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001232enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
1233 unsigned char *map,
1234 unsigned long mapsize,
1235 void *buffer,
1236 unsigned long bufsiz,
1237 struct strbuf *header)
Linus Torvaldsc4483572005-06-01 17:54:59 -07001238{
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001239 int status;
Matheus Tavares31877c92020-01-15 23:39:53 -03001240
Linus Torvaldsc4483572005-06-01 17:54:59 -07001241 /* Get the data stream */
1242 memset(stream, 0, sizeof(*stream));
1243 stream->next_in = map;
1244 stream->avail_in = mapsize;
1245 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -07001246 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -07001247
Linus Torvalds39c68542009-01-07 19:54:47 -08001248 git_inflate_init(stream);
Matheus Tavares31877c92020-01-15 23:39:53 -03001249 obj_read_unlock();
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001250 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001251 obj_read_lock();
Junio C Hamanod21f8422016-09-25 21:29:04 -07001252 if (status < Z_OK)
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001253 return ULHR_BAD;
Karthik Nayak46f03442015-05-03 19:59:59 +05301254
1255 /*
1256 * Check if entire header is unpacked in the first iteration.
1257 */
1258 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001259 return ULHR_OK;
Karthik Nayak46f03442015-05-03 19:59:59 +05301260
1261 /*
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001262 * We have a header longer than MAX_HEADER_LEN. The "header"
1263 * here is only non-NULL when we run "cat-file
1264 * --allow-unknown-type".
1265 */
1266 if (!header)
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001267 return ULHR_TOO_LONG;
Karthik Nayak46f03442015-05-03 19:59:59 +05301268
1269 /*
1270 * buffer[0..bufsiz] was not large enough. Copy the partial
1271 * result out to header, and then append the result of further
1272 * reading the stream.
1273 */
1274 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1275 stream->next_out = buffer;
1276 stream->avail_out = bufsiz;
1277
1278 do {
Matheus Tavares31877c92020-01-15 23:39:53 -03001279 obj_read_unlock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301280 status = git_inflate(stream, 0);
Matheus Tavares31877c92020-01-15 23:39:53 -03001281 obj_read_lock();
Karthik Nayak46f03442015-05-03 19:59:59 +05301282 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1283 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1284 return 0;
1285 stream->next_out = buffer;
1286 stream->avail_out = bufsiz;
1287 } while (status != Z_STREAM_END);
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001288 return ULHR_TOO_LONG;
Karthik Nayak46f03442015-05-03 19:59:59 +05301289}
1290
Jeff King00a77602019-01-07 03:37:02 -05001291static void *unpack_loose_rest(git_zstream *stream,
1292 void *buffer, unsigned long size,
1293 const struct object_id *oid)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001294{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001295 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001296 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001297 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001298 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001299
Linus Torvalds93821bd2006-07-11 12:48:08 -07001300 n = stream->total_out - bytes;
1301 if (n > size)
1302 n = size;
1303 memcpy(buf, (char *) buffer + bytes, n);
1304 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001305 if (bytes <= size) {
1306 /*
1307 * The above condition must be (bytes <= size), not
1308 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001309 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001310 * the input zlib stream may have bytes that express
1311 * "this concludes the stream", and we *do* want to
1312 * eat that input.
1313 *
1314 * Otherwise we would not be able to test that we
1315 * consumed all the input to reach the expected size;
1316 * we also want to check that zlib tells us that all
1317 * went well with status == Z_STREAM_END at the end.
1318 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001319 stream->next_out = buf + bytes;
1320 stream->avail_out = size - bytes;
Matheus Tavares31877c92020-01-15 23:39:53 -03001321 while (status == Z_OK) {
1322 obj_read_unlock();
Linus Torvalds39c68542009-01-07 19:54:47 -08001323 status = git_inflate(stream, Z_FINISH);
Matheus Tavares31877c92020-01-15 23:39:53 -03001324 obj_read_lock();
1325 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001326 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001327 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001328 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001329 return buf;
1330 }
1331
1332 if (status < 0)
Jeff King00a77602019-01-07 03:37:02 -05001333 error(_("corrupt loose object '%s'"), oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001334 else if (stream->avail_in)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001335 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05001336 oid_to_hex(oid));
Junio C Hamano7efbff72007-03-05 00:21:37 -08001337 free(buf);
1338 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001339}
1340
Linus Torvalds5180cac2005-06-02 07:57:25 -07001341/*
1342 * We used to just use "sscanf()", but that's actually way
1343 * too permissive for what we want to check. So do an anal
1344 * object header parse by hand.
1345 */
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001346int parse_loose_header(const char *hdr, struct object_info *oi)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001347{
Karthik Nayak46f03442015-05-03 19:59:59 +05301348 const char *type_buf = hdr;
Matt Cooperd6a09e72021-11-02 15:46:10 +00001349 size_t size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301350 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001351
1352 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301353 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001354 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001355 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001356 for (;;) {
1357 char c = *hdr++;
Junio C Hamanod21f8422016-09-25 21:29:04 -07001358 if (!c)
1359 return -1;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001360 if (c == ' ')
1361 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301362 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001363 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301364
1365 type = type_from_string_gently(type_buf, type_len, 1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001366 if (oi->type_name)
1367 strbuf_add(oi->type_name, type_buf, type_len);
Karthik Nayak46f03442015-05-03 19:59:59 +05301368 if (oi->typep)
1369 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001370
1371 /*
1372 * The length must follow immediately, and be in canonical
1373 * decimal format (ie "010" is not valid).
1374 */
1375 size = *hdr++ - '0';
1376 if (size > 9)
1377 return -1;
1378 if (size) {
1379 for (;;) {
1380 unsigned long c = *hdr - '0';
1381 if (c > 9)
1382 break;
1383 hdr++;
Matt Cooperd6a09e72021-11-02 15:46:10 +00001384 size = st_add(st_mult(size, 10), c);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001385 }
1386 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301387
1388 if (oi->sizep)
Matt Cooperd6a09e72021-11-02 15:46:10 +00001389 *oi->sizep = cast_size_t_to_ulong(size);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001390
1391 /*
1392 * The length must be followed by a zero byte
1393 */
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001394 if (*hdr)
1395 return -1;
Karthik Nayak46f03442015-05-03 19:59:59 +05301396
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001397 /*
1398 * The format is valid, but the type may still be bogus. The
1399 * Caller needs to check its oi->typep.
1400 */
1401 return 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001402}
1403
Jeff King514c5fd2019-01-07 03:35:42 -05001404static int loose_object_info(struct repository *r,
1405 const struct object_id *oid,
1406 struct object_info *oi, int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001407{
Karthik Nayak46f03442015-05-03 19:59:59 +05301408 int status = 0;
1409 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001410 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001411 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00001412 char hdr[MAX_HEADER_LEN];
Karthik Nayak46f03442015-05-03 19:59:59 +05301413 struct strbuf hdrbuf = STRBUF_INIT;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001414 unsigned long size_scratch;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001415 enum object_type type_scratch;
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001416 int allow_unknown = flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001417
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001418 if (oi->delta_base_oid)
1419 oidclr(oi->delta_base_oid);
Jeff King5d642e72013-12-21 09:24:20 -05001420
Jeff King052fe5e2013-07-12 02:30:48 -04001421 /*
1422 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001423 * need to look inside the object at all. Note that we
1424 * do not optimize out the stat call, even if the
1425 * caller doesn't care about the disk-size, since our
1426 * return value implicitly indicates whether the
1427 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04001428 */
Brandon Williams6ca32f42018-02-14 10:59:23 -08001429 if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
Jeff King771e7d52017-01-13 12:54:39 -05001430 const char *path;
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001431 struct stat st;
Jeff King61c77112018-11-12 09:54:42 -05001432 if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK))
Jeff Kingd7a24572019-01-07 03:37:29 -05001433 return quick_has_loose(r, oid) ? 0 : -1;
Jeff King514c5fd2019-01-07 03:35:42 -05001434 if (stat_loose_object(r, oid, &st, &path) < 0)
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001435 return -1;
1436 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04001437 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04001438 return 0;
1439 }
1440
Jeff King514c5fd2019-01-07 03:35:42 -05001441 map = map_loose_object(r, oid, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001442 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02001443 return -1;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001444
1445 if (!oi->sizep)
1446 oi->sizep = &size_scratch;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001447 if (!oi->typep)
1448 oi->typep = &type_scratch;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001449
Jeff King23c339c2013-07-12 02:37:53 -04001450 if (oi->disk_sizep)
1451 *oi->disk_sizep = mapsize;
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02001452
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001453 switch (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
1454 allow_unknown ? &hdrbuf : NULL)) {
1455 case ULHR_OK:
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001456 if (parse_loose_header(hdrbuf.len ? hdrbuf.buf : hdr, oi) < 0)
1457 status = error(_("unable to parse %s header"), oid_to_hex(oid));
1458 else if (!allow_unknown && *oi->typep < 0)
1459 die(_("invalid object type"));
1460
1461 if (!oi->contentp)
1462 break;
1463 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
1464 if (*oi->contentp)
1465 goto cleanup;
1466
1467 status = -1;
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001468 break;
1469 case ULHR_BAD:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001470 status = error(_("unable to unpack %s header"),
Jeff King514c5fd2019-01-07 03:35:42 -05001471 oid_to_hex(oid));
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001472 break;
Ævar Arnfjörð Bjarmason5848fb12021-10-01 11:16:50 +02001473 case ULHR_TOO_LONG:
1474 status = error(_("header for %s too long, exceeds %d bytes"),
1475 oid_to_hex(oid), MAX_HEADER_LEN);
1476 break;
Ævar Arnfjörð Bjarmason3b6a8db2021-10-01 11:16:49 +02001477 }
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001478
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001479 git_inflate_end(&stream);
1480cleanup:
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001481 munmap(map, mapsize);
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001482 if (oi->sizep == &size_scratch)
1483 oi->sizep = NULL;
Karthik Nayak46f03442015-05-03 19:59:59 +05301484 strbuf_release(&hdrbuf);
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001485 if (oi->typep == &type_scratch)
1486 oi->typep = NULL;
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001487 oi->whence = OI_LOOSE;
Ævar Arnfjörð Bjarmasondccb32b2021-10-01 11:16:51 +02001488 return status;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001489}
1490
Matheus Tavares31877c92020-01-15 23:39:53 -03001491int obj_read_use_lock = 0;
1492pthread_mutex_t obj_read_mutex;
1493
1494void enable_obj_read_lock(void)
1495{
1496 if (obj_read_use_lock)
1497 return;
1498
1499 obj_read_use_lock = 1;
1500 init_recursive_mutex(&obj_read_mutex);
1501}
1502
1503void disable_obj_read_lock(void)
1504{
1505 if (!obj_read_use_lock)
1506 return;
1507
1508 obj_read_use_lock = 0;
1509 pthread_mutex_destroy(&obj_read_mutex);
1510}
1511
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001512int fetch_if_missing = 1;
1513
Matheus Tavares31877c92020-01-15 23:39:53 -03001514static int do_oid_object_info_extended(struct repository *r,
1515 const struct object_id *oid,
1516 struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001517{
Jonathan Tancd585e22017-06-21 17:40:23 -07001518 static struct object_info blank_oi = OBJECT_INFO_INIT;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001519 struct cached_object *co;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001520 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04001521 int rtype;
brian m. carlsonb383a132018-03-12 02:27:54 +00001522 const struct object_id *real = oid;
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001523 int already_retried = 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001524
Matheus Tavares31877c92020-01-15 23:39:53 -03001525
brian m. carlsonb383a132018-03-12 02:27:54 +00001526 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
Stefan Beller9d983542018-04-25 11:21:06 -07001527 real = lookup_replace_object(r, oid);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001528
brian m. carlsonb383a132018-03-12 02:27:54 +00001529 if (is_null_oid(real))
Jeff King87b5e232017-11-21 18:17:39 -05001530 return -1;
1531
Jonathan Tancd585e22017-06-21 17:40:23 -07001532 if (!oi)
1533 oi = &blank_oi;
1534
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001535 co = find_cached_object(real);
1536 if (co) {
1537 if (oi->typep)
1538 *(oi->typep) = co->type;
1539 if (oi->sizep)
1540 *(oi->sizep) = co->size;
1541 if (oi->disk_sizep)
1542 *(oi->disk_sizep) = 0;
Jeff Kingb99b6bc2020-02-23 23:36:56 -05001543 if (oi->delta_base_oid)
1544 oidclr(oi->delta_base_oid);
Jonathan Tan9c8a2942020-01-02 12:16:30 -08001545 if (oi->type_name)
1546 strbuf_addstr(oi->type_name, type_name(co->type));
1547 if (oi->contentp)
1548 *oi->contentp = xmemdupz(co->buf, co->size);
1549 oi->whence = OI_CACHED;
1550 return 0;
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07001551 }
1552
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001553 while (1) {
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001554 if (find_pack_entry(r, real, &e))
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001555 break;
1556
Takuto Ikuta024aa462018-03-14 15:32:42 +09001557 if (flags & OBJECT_INFO_IGNORE_LOOSE)
1558 return -1;
1559
Steven Grimmddd63e62008-08-05 13:08:41 -07001560 /* Most likely it's a loose object. */
Jeff King514c5fd2019-01-07 03:35:42 -05001561 if (!loose_object_info(r, real, oi, flags))
Jeff King5b086402013-07-12 02:34:57 -04001562 return 0;
Steven Grimmddd63e62008-08-05 13:08:41 -07001563
1564 /* Not a loose object; someone else may have just packed it. */
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001565 if (!(flags & OBJECT_INFO_QUICK)) {
Stefan Beller9d983542018-04-25 11:21:06 -07001566 reprepare_packed_git(r);
Junio C Hamano42c8ce12018-05-30 14:04:10 +09001567 if (find_pack_entry(r, real, &e))
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001568 break;
1569 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001570
Jonathan Taneef71902021-10-08 14:08:18 -07001571 /*
1572 * If r is the_repository, this might be an attempt at
1573 * accessing a submodule object as if it were in the_repository
1574 * (having called add_submodule_odb() on that submodule's ODB).
1575 * If any such ODBs exist, register them and try again.
1576 */
1577 if (r == the_repository &&
1578 register_all_submodule_odb_as_alternates())
Jonathan Tana35e03d2021-08-16 14:09:51 -07001579 /* We added some alternates; retry */
1580 continue;
1581
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001582 /* Check if it is a missing object */
Jonathan Tanef830cc2021-06-17 10:13:26 -07001583 if (fetch_if_missing && repo_has_promisor_remote(r) &&
1584 !already_retried &&
Derrick Stolee31f52562019-05-28 08:19:07 -07001585 !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001586 /*
Christian Couderb14ed5a2019-06-25 15:40:31 +02001587 * TODO Investigate checking promisor_remote_get_direct()
1588 * TODO return value and stopping on error here.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001589 */
Christian Couderb14ed5a2019-06-25 15:40:31 +02001590 promisor_remote_get_direct(r, real, 1);
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001591 already_retried = 1;
1592 continue;
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001593 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001594
1595 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001596 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001597
Jonathan Tancd585e22017-06-21 17:40:23 -07001598 if (oi == &blank_oi)
1599 /*
1600 * We know that the caller doesn't actually need the
1601 * information below, so return early.
1602 */
1603 return 0;
Stefan Beller9d983542018-04-25 11:21:06 -07001604 rtype = packed_object_info(r, e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04001605 if (rtype < 0) {
René Scharfe751530d2021-09-11 22:40:33 +02001606 mark_bad_packed_object(e.p, real);
Matheus Tavares31877c92020-01-15 23:39:53 -03001607 return do_oid_object_info_extended(r, real, oi, 0);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001608 } else if (oi->whence == OI_PACKED) {
Junio C Hamano9a490592011-05-12 15:51:38 -07001609 oi->u.packed.offset = e.offset;
1610 oi->u.packed.pack = e.p;
1611 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
1612 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001613 }
1614
Jeff King5b086402013-07-12 02:34:57 -04001615 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001616}
1617
Matheus Tavares31877c92020-01-15 23:39:53 -03001618int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1619 struct object_info *oi, unsigned flags)
1620{
1621 int ret;
1622 obj_read_lock();
1623 ret = do_oid_object_info_extended(r, oid, oi, flags);
1624 obj_read_unlock();
1625 return ret;
1626}
1627
1628
Christian Couder3fc0dca2013-10-27 00:34:30 +02001629/* returns enum object_type or negative */
Stefan Beller9d983542018-04-25 11:21:06 -07001630int oid_object_info(struct repository *r,
1631 const struct object_id *oid,
1632 unsigned long *sizep)
Junio C Hamano9a490592011-05-12 15:51:38 -07001633{
Jeff King5b086402013-07-12 02:34:57 -04001634 enum object_type type;
Jeff King27b5c1a2016-08-11 05:24:35 -04001635 struct object_info oi = OBJECT_INFO_INIT;
Junio C Hamano9a490592011-05-12 15:51:38 -07001636
Jeff King5b086402013-07-12 02:34:57 -04001637 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001638 oi.sizep = sizep;
Stefan Beller9d983542018-04-25 11:21:06 -07001639 if (oid_object_info_extended(r, oid, &oi,
1640 OBJECT_INFO_LOOKUP_REPLACE) < 0)
Jeff King5b086402013-07-12 02:34:57 -04001641 return -1;
1642 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001643}
1644
Stefan Beller1b9b5c62018-10-16 16:35:32 -07001645static void *read_object(struct repository *r,
Junio C Hamanocba595a2019-02-06 22:05:27 -08001646 const struct object_id *oid, enum object_type *type,
Jonathan Tanf1d81302017-08-18 15:20:30 -07001647 unsigned long *size)
1648{
1649 struct object_info oi = OBJECT_INFO_INIT;
1650 void *content;
1651 oi.typep = type;
1652 oi.sizep = size;
1653 oi.contentp = &content;
1654
Junio C Hamanocba595a2019-02-06 22:05:27 -08001655 if (oid_object_info_extended(r, oid, &oi, 0) < 0)
Jonathan Tanf1d81302017-08-18 15:20:30 -07001656 return NULL;
1657 return content;
1658}
1659
Patryk Obara829e5c32018-01-28 01:13:11 +01001660int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1661 struct object_id *oid)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001662{
1663 struct cached_object *co;
1664
Matheus Tavares2dcde202020-01-30 17:32:22 -03001665 hash_object_file(the_hash_algo, buf, len, type_name(type), oid);
Jonathan Tana64d2aa2020-07-21 15:50:20 -07001666 if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
1667 find_cached_object(oid))
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001668 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04001669 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001670 co = &cached_objects[cached_object_nr++];
1671 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001672 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08001673 co->buf = xmalloc(len);
1674 memcpy(co->buf, buf, len);
brian m. carlson62ba93e2018-05-02 00:26:03 +00001675 oidcpy(&co->oid, oid);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001676 return 0;
1677}
1678
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001679/*
1680 * This function dies on corrupt objects; the callers who want to
1681 * deal with them should arrange to call read_object() and give error
1682 * messages themselves.
1683 */
Stefan Bellera3b72c82018-11-13 16:12:46 -08001684void *read_object_file_extended(struct repository *r,
1685 const struct object_id *oid,
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001686 enum object_type *type,
1687 unsigned long *size,
1688 int lookup_replace)
Nicolas Pitreac939102008-07-14 21:46:48 -04001689{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001690 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001691 const struct packed_git *p;
Jeff King771e7d52017-01-13 12:54:39 -05001692 const char *path;
1693 struct stat st;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -07001694 const struct object_id *repl = lookup_replace ?
Stefan Bellera3b72c82018-11-13 16:12:46 -08001695 lookup_replace_object(r, oid) : oid;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001696
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001697 errno = 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08001698 data = read_object(r, repl, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07001699 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001700 return data;
Christian Couder68095572009-01-23 10:06:53 +01001701
Matheus Tavares31877c92020-01-15 23:39:53 -03001702 obj_read_lock();
Björn Steinbrink25f3af32011-01-20 21:12:20 +01001703 if (errno && errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001704 die_errno(_("failed to read object %s"), oid_to_hex(oid));
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001705
Christian Couder68095572009-01-23 10:06:53 +01001706 /* die if we replaced an object with one that does not exist */
brian m. carlsonb383a132018-03-12 02:27:54 +00001707 if (repl != oid)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001708 die(_("replacement %s not found for %s"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001709 oid_to_hex(repl), oid_to_hex(oid));
Christian Couder68095572009-01-23 10:06:53 +01001710
Junio C Hamanocba595a2019-02-06 22:05:27 -08001711 if (!stat_loose_object(r, repl, &st, &path))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001712 die(_("loose object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001713 oid_to_hex(repl), path);
Christian Couder68095572009-01-23 10:06:53 +01001714
René Scharfe7407d732021-09-11 22:42:20 +02001715 if ((p = has_packed_and_bad(r, repl)) != NULL)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001716 die(_("packed object %s (stored in %s) is corrupt"),
brian m. carlsonb383a132018-03-12 02:27:54 +00001717 oid_to_hex(repl), p->pack_name);
Matheus Tavares31877c92020-01-15 23:39:53 -03001718 obj_read_unlock();
Christian Couderf5552ae2009-01-23 10:07:01 +01001719
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001720 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001721}
1722
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001723void *read_object_with_reference(struct repository *r,
1724 const struct object_id *oid,
Nicolas Pitre21666f12007-02-26 14:55:59 -05001725 const char *required_type_name,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001726 unsigned long *size,
brian m. carlson02f05472018-03-12 02:27:52 +00001727 struct object_id *actual_oid_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001728{
Nicolas Pitre21666f12007-02-26 14:55:59 -05001729 enum object_type type, required_type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001730 void *buffer;
1731 unsigned long isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001732 struct object_id actual_oid;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001733
Nicolas Pitre21666f12007-02-26 14:55:59 -05001734 required_type = type_from_string(required_type_name);
brian m. carlson02f05472018-03-12 02:27:52 +00001735 oidcpy(&actual_oid, oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001736 while (1) {
1737 int ref_length = -1;
1738 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001739
Nguyễn Thái Ngọc Duyd3b47052019-06-27 16:28:47 +07001740 buffer = repo_read_object_file(r, &actual_oid, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001741 if (!buffer)
1742 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001743 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001744 *size = isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001745 if (actual_oid_return)
1746 oidcpy(actual_oid_return, &actual_oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001747 return buffer;
1748 }
1749 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001750 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001751 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05001752 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001753 ref_type = "object ";
1754 else {
1755 free(buffer);
1756 return NULL;
1757 }
1758 ref_length = strlen(ref_type);
1759
brian m. carlson94b5e092018-07-16 01:28:07 +00001760 if (ref_length + the_hash_algo->hexsz > isize ||
Martin Koegler50974ec2008-02-18 21:47:52 +01001761 memcmp(buffer, ref_type, ref_length) ||
brian m. carlson02f05472018-03-12 02:27:52 +00001762 get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001763 free(buffer);
1764 return NULL;
1765 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001766 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001767 /* Now we have the ID of the referred-to object in
brian m. carlson02f05472018-03-12 02:27:52 +00001768 * actual_oid. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001769 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001770}
1771
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001772static void write_object_file_prepare(const struct git_hash_algo *algo,
1773 const void *buf, unsigned long len,
Patryk Obaraa09c9852018-01-28 01:13:19 +01001774 const char *type, struct object_id *oid,
1775 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001776{
brian m. carlson18e25882018-02-01 02:18:41 +00001777 git_hash_ctx c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001778
1779 /* Generate the header */
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01001780 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001781
1782 /* Sha1.. */
Matheus Tavares7ad5c442020-01-30 17:32:21 -03001783 algo->init_fn(&c);
1784 algo->update_fn(&c, hdr, *hdrlen);
1785 algo->update_fn(&c, buf, len);
brian m. carlson5951bf42021-04-26 01:02:53 +00001786 algo->final_oid_fn(oid, &c);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001787}
1788
Linus Torvalds230f1322005-10-08 15:54:01 -07001789/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001790 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07001791 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07001792int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001793{
Thomas Raste32c0a92008-09-19 00:24:46 +02001794 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001795
Johannes Schindelin348df162009-04-28 00:32:25 +02001796 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001797 goto try_rename;
1798 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02001799 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001800
1801 /*
1802 * Coda hack - coda doesn't like cross-directory links,
1803 * so we fall back to a rename, which will mean that it
1804 * won't be able to check collisions, but that's not a
1805 * big deal.
1806 *
1807 * The same holds for FAT formatted media.
1808 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07001809 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001810 * left to unlink.
1811 */
1812 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001813 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001814 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07001815 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001816 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001817 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001818 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07001819 if (ret) {
1820 if (ret != EEXIST) {
Jeff King2c319882019-01-07 03:39:33 -05001821 return error_errno(_("unable to write file %s"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001822 }
1823 /* FIXME!!! Collision check here ? */
1824 }
1825
Junio C Hamano3be1f182009-03-27 23:14:39 -07001826out:
Matthieu Moy5256b002010-02-22 23:32:16 +01001827 if (adjust_shared_perm(filename))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001828 return error(_("unable to set permission to '%s'"), filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001829 return 0;
1830}
1831
Linus Torvalds4d548152006-05-24 08:30:54 -07001832static int write_buffer(int fd, const void *buf, size_t len)
1833{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08001834 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001835 return error_errno(_("file write error"));
Linus Torvalds4d548152006-05-24 08:30:54 -07001836 return 0;
1837}
1838
Matheus Tavares2dcde202020-01-30 17:32:22 -03001839int hash_object_file(const struct git_hash_algo *algo, const void *buf,
1840 unsigned long len, const char *type,
Patryk Obaraf070fac2018-01-28 01:13:13 +01001841 struct object_id *oid)
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001842{
brian m. carlson1af64f72018-03-12 02:27:55 +00001843 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001844 int hdrlen = sizeof(hdr);
Matheus Tavares2dcde202020-01-30 17:32:22 -03001845 write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001846 return 0;
1847}
1848
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001849/* Finalize a file on disk, and close it. */
Jeff King514c5fd2019-01-07 03:35:42 -05001850static void close_loose_object(int fd)
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001851{
Neeraj Singhb3cecf42021-12-06 22:05:04 +00001852 if (!the_repository->objects->odb->will_destroy) {
1853 if (fsync_object_files)
1854 fsync_or_die(fd, "loose object file");
1855 }
1856
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001857 if (close(fd) != 0)
Jeff King76011352019-01-07 03:39:24 -05001858 die_errno(_("error when closing loose object file"));
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001859}
1860
Linus Torvalds5723fe72008-06-14 10:50:12 -07001861/* Size of directory component, including the ending '/' */
1862static inline int directory_size(const char *filename)
1863{
1864 const char *s = strrchr(filename, '/');
1865 if (!s)
1866 return 0;
1867 return s - filename + 1;
1868}
1869
1870/*
1871 * This creates a temporary file in the same directory as the final
1872 * 'filename'
1873 *
1874 * We want to avoid cross-directory filename renames, because those
1875 * can have problems on various filesystems (FAT, NFS, Coda).
1876 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001877static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07001878{
1879 int fd, dirlen = directory_size(filename);
1880
Jeff Kingd4b3d112015-09-24 17:07:49 -04001881 strbuf_reset(tmp);
1882 strbuf_add(tmp, filename, dirlen);
1883 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
1884 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05001885 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04001886 /*
1887 * Make sure the directory exists; note that the contents
1888 * of the buffer are undefined after mkstemp returns an
1889 * error, so we have to rewrite the whole buffer from
1890 * scratch.
1891 */
1892 strbuf_reset(tmp);
1893 strbuf_add(tmp, filename, dirlen - 1);
1894 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01001895 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001896 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07001897 return -1;
1898
1899 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001900 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
1901 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07001902 }
1903 return fd;
1904}
1905
Patryk Obara3fc72812018-01-28 01:13:21 +01001906static int write_loose_object(const struct object_id *oid, char *hdr,
1907 int hdrlen, const void *buf, unsigned long len,
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001908 time_t mtime, unsigned flags)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001909{
Jeff King915308b2009-01-29 00:56:34 -05001910 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001911 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001912 git_zstream stream;
brian m. carlson18e25882018-02-01 02:18:41 +00001913 git_hash_ctx c;
Patryk Obara3fc72812018-01-28 01:13:21 +01001914 struct object_id parano_oid;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001915 static struct strbuf tmp_file = STRBUF_INIT;
Christian Couderea657732018-01-17 18:54:54 +01001916 static struct strbuf filename = STRBUF_INIT;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001917
Jeff King514c5fd2019-01-07 03:35:42 -05001918 loose_object_path(the_repository, &filename, oid);
Christian Couderea657732018-01-17 18:54:54 +01001919
1920 fd = create_tmpfile(&tmp_file, filename.buf);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001921 if (fd < 0) {
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001922 if (flags & HASH_SILENT)
1923 return -1;
1924 else if (errno == EACCES)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001925 return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01001926 else
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001927 return error_errno(_("unable to create temporary file"));
Linus Torvaldsaac17942005-05-03 11:46:16 -07001928 }
1929
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001930 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001931 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001932 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001933 stream.avail_out = sizeof(compressed);
brian m. carlson18e25882018-02-01 02:18:41 +00001934 the_hash_algo->init_fn(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001935
1936 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05001937 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001938 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001939 while (git_deflate(&stream, 0) == Z_OK)
1940 ; /* nothing */
brian m. carlson18e25882018-02-01 02:18:41 +00001941 the_hash_algo->update_fn(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001942
1943 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04001944 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001945 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001946 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05001947 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001948 ret = git_deflate(&stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00001949 the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001950 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
Jeff King76011352019-01-07 03:39:24 -05001951 die(_("unable to write loose object file"));
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001952 stream.next_out = compressed;
1953 stream.avail_out = sizeof(compressed);
1954 } while (ret == Z_OK);
1955
Linus Torvaldsac54c272007-03-20 11:38:34 -07001956 if (ret != Z_STREAM_END)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001957 die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001958 ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001959 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07001960 if (ret != Z_OK)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001961 die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
Patryk Obara3fc72812018-01-28 01:13:21 +01001962 ret);
brian m. carlson5951bf42021-04-26 01:02:53 +00001963 the_hash_algo->final_oid_fn(&parano_oid, &c);
Jeff King9001dc22018-08-28 17:22:48 -04001964 if (!oideq(oid, &parano_oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001965 die(_("confused by unstable object source data for %s"),
Patryk Obara3fc72812018-01-28 01:13:21 +01001966 oid_to_hex(oid));
Linus Torvaldsac54c272007-03-20 11:38:34 -07001967
Jeff King514c5fd2019-01-07 03:35:42 -05001968 close_loose_object(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001969
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001970 if (mtime) {
1971 struct utimbuf utb;
1972 utb.actime = mtime;
1973 utb.modtime = mtime;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02001974 if (utime(tmp_file.buf, &utb) < 0 &&
1975 !(flags & HASH_SILENT))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02001976 warning_errno(_("failed utime() on %s"), tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001977 }
1978
Christian Couderea657732018-01-17 18:54:54 +01001979 return finalize_object_file(tmp_file.buf, filename.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001980}
Daniel Barkalow8237b182005-04-23 18:47:23 -07001981
brian m. carlson6862ebb2018-05-02 00:25:34 +00001982static int freshen_loose_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001983{
brian m. carlson6862ebb2018-05-02 00:25:34 +00001984 return check_and_freshen(oid, 1);
Jeff King33d42212014-10-15 18:42:22 -04001985}
1986
brian m. carlson6862ebb2018-05-02 00:25:34 +00001987static int freshen_packed_object(const struct object_id *oid)
Jeff King33d42212014-10-15 18:42:22 -04001988{
1989 struct pack_entry e;
brian m. carlson544443c2018-05-02 00:25:35 +00001990 if (!find_pack_entry(the_repository, oid, &e))
Jeff Kingee1c6c32015-04-20 15:55:00 -04001991 return 0;
1992 if (e.p->freshened)
1993 return 1;
1994 if (!freshen_file(e.p->pack_name))
1995 return 0;
1996 e.p->freshened = 1;
1997 return 1;
Jeff King33d42212014-10-15 18:42:22 -04001998}
1999
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002000int write_object_file_flags(const void *buf, unsigned long len,
2001 const char *type, struct object_id *oid,
2002 unsigned flags)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002003{
brian m. carlson1af64f72018-03-12 02:27:55 +00002004 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04002005 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002006
2007 /* Normally if we have it in the pack then we do not bother writing
2008 * it out into .git/objects/??/?{38} file.
2009 */
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002010 write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
2011 &hdrlen);
brian m. carlson6862ebb2018-05-02 00:25:34 +00002012 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002013 return 0;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002014 return write_loose_object(oid, hdr, hdrlen, buf, len, 0, flags);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002015}
2016
Patryk Obara1752cbb2018-01-28 01:13:22 +01002017int hash_object_file_literally(const void *buf, unsigned long len,
2018 const char *type, struct object_id *oid,
2019 unsigned flags)
Eric Sunshine0c3db672015-05-04 03:25:15 -04002020{
2021 char *header;
2022 int hdrlen, status = 0;
2023
2024 /* type string, SP, %lu of the length plus NUL must fit this */
brian m. carlson1af64f72018-03-12 02:27:55 +00002025 hdrlen = strlen(type) + MAX_HEADER_LEN;
Jeff Kingef1286d2015-09-24 17:06:42 -04002026 header = xmalloc(hdrlen);
Matheus Tavares7ad5c442020-01-30 17:32:21 -03002027 write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
2028 &hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002029
2030 if (!(flags & HASH_WRITE_OBJECT))
2031 goto cleanup;
brian m. carlson6862ebb2018-05-02 00:25:34 +00002032 if (freshen_packed_object(oid) || freshen_loose_object(oid))
Eric Sunshine0c3db672015-05-04 03:25:15 -04002033 goto cleanup;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002034 status = write_loose_object(oid, header, hdrlen, buf, len, 0, 0);
Eric Sunshine0c3db672015-05-04 03:25:15 -04002035
2036cleanup:
2037 free(header);
2038 return status;
2039}
2040
Patryk Obara4bdb70a2018-01-28 01:13:20 +01002041int force_object_loose(const struct object_id *oid, time_t mtime)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002042{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002043 void *buf;
2044 unsigned long len;
2045 enum object_type type;
brian m. carlson1af64f72018-03-12 02:27:55 +00002046 char hdr[MAX_HEADER_LEN];
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002047 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002048 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002049
brian m. carlson6862ebb2018-05-02 00:25:34 +00002050 if (has_loose_object(oid))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002051 return 0;
Junio C Hamanocba595a2019-02-06 22:05:27 -08002052 buf = read_object(the_repository, oid, &type, &len);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002053 if (!buf)
Jeff King2c319882019-01-07 03:39:33 -05002054 return error(_("cannot read object for %s"), oid_to_hex(oid));
Torsten Bögershausenca473ce2018-11-11 08:05:04 +01002055 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
Ævar Arnfjörð Bjarmason4ef91a22021-10-12 16:30:49 +02002056 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime, 0);
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02002057 free(buf);
2058
2059 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04002060}
2061
Jonathan Tan1d8d9cb2020-08-05 16:06:49 -07002062int has_object(struct repository *r, const struct object_id *oid,
2063 unsigned flags)
2064{
2065 int quick = !(flags & HAS_OBJECT_RECHECK_PACKED);
2066 unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT |
2067 (quick ? OBJECT_INFO_QUICK : 0);
2068
2069 if (!startup_info->have_repository)
2070 return 0;
2071 return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0;
2072}
2073
Junio C Hamanocba595a2019-02-06 22:05:27 -08002074int repo_has_object_file_with_flags(struct repository *r,
2075 const struct object_id *oid, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07002076{
Jonathan Nieder3e8b7d32017-04-11 15:47:13 -07002077 if (!startup_info->have_repository)
2078 return 0;
Jonathan Tan9c8a2942020-01-02 12:16:30 -08002079 return oid_object_info_extended(r, oid, NULL, flags) >= 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07002080}
Junio C Hamano74400e72005-05-01 23:45:49 -07002081
Stefan Beller9b45f492018-11-13 16:12:48 -08002082int repo_has_object_file(struct repository *r,
2083 const struct object_id *oid)
brian m. carlsonb419aa22015-11-10 02:22:19 +00002084{
Junio C Hamanocba595a2019-02-06 22:05:27 -08002085 return repo_has_object_file_with_flags(r, oid, 0);
Jeff King5827a032016-10-13 12:53:44 -04002086}
2087
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002088static void check_tree(const void *buf, size_t size)
2089{
2090 struct tree_desc desc;
2091 struct name_entry entry;
2092
2093 init_tree_desc(&desc, buf, size);
2094 while (tree_entry(&desc, &entry))
2095 /* do nothing
2096 * tree_entry() will die() on malformed entries */
2097 ;
2098}
2099
2100static void check_commit(const void *buf, size_t size)
2101{
2102 struct commit c;
2103 memset(&c, 0, sizeof(c));
Stefan Beller08f4f442018-06-28 18:22:00 -07002104 if (parse_commit_buffer(the_repository, &c, buf, size, 0))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002105 die(_("corrupt commit"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002106}
2107
2108static void check_tag(const void *buf, size_t size)
2109{
2110 struct tag t;
2111 memset(&t, 0, sizeof(t));
Stefan Beller0e740fe2018-06-28 18:22:04 -07002112 if (parse_tag_buffer(the_repository, &t, buf, size))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002113 die(_("corrupt tag"));
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002114}
2115
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002116static int index_mem(struct index_state *istate,
2117 struct object_id *oid, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002118 enum object_type type,
2119 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02002120{
Linus Torvalds6c510be2007-02-13 11:07:23 -08002121 int ret, re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002122 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07002123
Bryan Larsen7672db22005-07-08 16:51:55 -07002124 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08002125 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08002126
2127 /*
2128 * Convert blobs to git internal format
2129 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002130 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002131 struct strbuf nbuf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002132 if (convert_to_git(istate, path, buf, size, &nbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002133 get_conv_flags(flags))) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02002134 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08002135 re_allocated = 1;
2136 }
2137 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002138 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07002139 if (type == OBJ_TREE)
2140 check_tree(buf, size);
2141 if (type == OBJ_COMMIT)
2142 check_commit(buf, size);
2143 if (type == OBJ_TAG)
2144 check_tag(buf, size);
2145 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08002146
Bryan Larsen7672db22005-07-08 16:51:55 -07002147 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08002148 ret = write_object_file(buf, size, type_name(type), oid);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02002149 else
Matheus Tavares2dcde202020-01-30 17:32:22 -03002150 ret = hash_object_file(the_hash_algo, buf, size,
2151 type_name(type), oid);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002152 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08002153 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002154 return ret;
2155}
2156
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002157static int index_stream_convert_blob(struct index_state *istate,
2158 struct object_id *oid,
2159 int fd,
2160 const char *path,
2161 unsigned flags)
Steffen Prohaska9035d752014-08-26 17:23:25 +02002162{
2163 int ret;
2164 const int write_object = flags & HASH_WRITE_OBJECT;
2165 struct strbuf sbuf = STRBUF_INIT;
2166
2167 assert(path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002168 assert(would_convert_to_git_filter_fd(istate, path));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002169
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002170 convert_to_git_filter_fd(istate, path, fd, &sbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01002171 get_conv_flags(flags));
Steffen Prohaska9035d752014-08-26 17:23:25 +02002172
2173 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08002174 ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
Patryk Obaraa09c9852018-01-28 01:13:19 +01002175 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002176 else
Matheus Tavares2dcde202020-01-30 17:32:22 -03002177 ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
2178 type_name(OBJ_BLOB), oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002179 strbuf_release(&sbuf);
2180 return ret;
2181}
2182
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002183static int index_pipe(struct index_state *istate, struct object_id *oid,
2184 int fd, enum object_type type,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002185 const char *path, unsigned flags)
2186{
2187 struct strbuf sbuf = STRBUF_INIT;
2188 int ret;
2189
2190 if (strbuf_read(&sbuf, fd, 4096) >= 0)
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002191 ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002192 else
2193 ret = -1;
2194 strbuf_release(&sbuf);
2195 return ret;
2196}
2197
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002198#define SMALL_FILE_SIZE (32*1024)
2199
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002200static int index_core(struct index_state *istate,
2201 struct object_id *oid, int fd, size_t size,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002202 enum object_type type, const char *path,
2203 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002204{
2205 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002206
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002207 if (!size) {
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002208 ret = index_mem(istate, oid, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002209 } else if (size <= SMALL_FILE_SIZE) {
2210 char *buf = xmalloc(size);
Jeff King90dca672017-09-27 02:01:07 -04002211 ssize_t read_result = read_in_full(fd, buf, size);
2212 if (read_result < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002213 ret = error_errno(_("read error while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002214 path ? path : "<unknown>");
2215 else if (read_result != size)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002216 ret = error(_("short read while indexing %s"),
Jeff King90dca672017-09-27 02:01:07 -04002217 path ? path : "<unknown>");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002218 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002219 ret = index_mem(istate, oid, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03002220 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002221 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002222 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002223 ret = index_mem(istate, oid, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002224 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04002225 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002226 return ret;
2227}
2228
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002229/*
Junio C Hamano568508e2011-10-28 14:48:40 -07002230 * This creates one packfile per large blob unless bulk-checkin
2231 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002232 *
2233 * This also bypasses the usual "convert-to-git" dance, and that is on
2234 * purpose. We could write a streaming version of the converting
2235 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05002236 * (or equivalent in-core API described above). However, that is
2237 * somewhat complicated, as we do not know the size of the filter
2238 * result, which we need to know beforehand when writing a git object.
2239 * Since the primary motivation for trying to stream from the working
2240 * tree file and to avoid mmaping it in core is to deal with large
2241 * binary blobs, they generally do not want to get any conversion, and
2242 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002243 */
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002244static int index_stream(struct object_id *oid, int fd, size_t size,
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002245 enum object_type type, const char *path,
2246 unsigned flags)
2247{
brian m. carlson68ee6df2018-03-12 02:27:21 +00002248 return index_bulk_checkin(oid, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002249}
2250
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002251int index_fd(struct index_state *istate, struct object_id *oid,
2252 int fd, struct stat *st,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002253 enum object_type type, const char *path, unsigned flags)
2254{
2255 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07002256
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002257 /*
2258 * Call xsize_t() only when needed to avoid potentially unnecessary
2259 * die() for large files.
2260 */
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002261 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path))
2262 ret = index_stream_convert_blob(istate, oid, fd, path, flags);
Steffen Prohaska9035d752014-08-26 17:23:25 +02002263 else if (!S_ISREG(st->st_mode))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002264 ret = index_pipe(istate, oid, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002265 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002266 (path && would_convert_to_git(istate, path)))
2267 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
2268 type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07002269 else
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02002270 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02002271 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04002272 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07002273 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07002274}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002275
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002276int index_path(struct index_state *istate, struct object_id *oid,
2277 const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002278{
2279 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002280 struct strbuf sb = STRBUF_INIT;
Rene Scharfeea8e0292017-08-30 20:00:29 +02002281 int rc = 0;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002282
2283 switch (st->st_mode & S_IFMT) {
2284 case S_IFREG:
2285 fd = open(path, O_RDONLY);
2286 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002287 return error_errno("open(\"%s\")", path);
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002288 if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002289 return error(_("%s: failed to insert into database"),
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002290 path);
2291 break;
2292 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07002293 if (strbuf_readlink(&sb, path, st->st_size))
2294 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07002295 if (!(flags & HASH_WRITE_OBJECT))
Matheus Tavares2dcde202020-01-30 17:32:22 -03002296 hash_object_file(the_hash_algo, sb.buf, sb.len,
2297 blob_type, oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +01002298 else if (write_object_file(sb.buf, sb.len, blob_type, oid))
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002299 rc = error(_("%s: failed to insert into database"), path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08002300 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002301 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07002302 case S_IFDIR:
brian m. carlsona98e6102017-10-15 22:07:07 +00002303 return resolve_gitlink_ref(path, "HEAD", oid);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002304 default:
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002305 return error(_("%s: unsupported file type"), path);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002306 }
Rene Scharfeea8e0292017-08-30 20:00:29 +02002307 return rc;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07002308}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002309
2310int read_pack_header(int fd, struct pack_header *header)
2311{
Jeff Kingf48ecd32017-09-13 14:47:22 -04002312 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
Heikki Orsilac697ad12008-05-03 16:27:26 +03002313 /* "eof before pack header was fully read" */
2314 return PH_ERROR_EOF;
2315
Junio C Hamanoa69e5422007-01-22 21:55:18 -08002316 if (header->hdr_signature != htonl(PACK_SIGNATURE))
2317 /* "protocol error (pack signature mismatch detected)" */
2318 return PH_ERROR_PACK_SIGNATURE;
2319 if (!pack_version_ok(header->hdr_version))
2320 /* "protocol error (pack version unsupported)" */
2321 return PH_ERROR_PROTOCOL;
2322 return 0;
2323}
Jeff King40d52ff2010-04-01 20:05:23 -04002324
brian m. carlsone816caa2018-03-12 02:27:42 +00002325void assert_oid_type(const struct object_id *oid, enum object_type expect)
Jeff King40d52ff2010-04-01 20:05:23 -04002326{
Stefan Beller0df8e962018-04-25 11:20:59 -07002327 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff King40d52ff2010-04-01 20:05:23 -04002328 if (type < 0)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002329 die(_("%s is not a valid object"), oid_to_hex(oid));
Jeff King40d52ff2010-04-01 20:05:23 -04002330 if (type != expect)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002331 die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002332 type_name(expect));
Jeff King40d52ff2010-04-01 20:05:23 -04002333}
Jeff King27e1e222014-10-15 18:38:55 -04002334
René Scharfe70c49052017-06-24 16:09:39 +02002335int for_each_file_in_obj_subdir(unsigned int subdir_nr,
René Scharfecc817ca2017-06-22 20:19:48 +02002336 struct strbuf *path,
2337 each_loose_object_fn obj_cb,
2338 each_loose_cruft_fn cruft_cb,
2339 each_loose_subdir_fn subdir_cb,
2340 void *data)
Jeff King27e1e222014-10-15 18:38:55 -04002341{
René Scharfe0375f472017-06-24 14:12:30 +02002342 size_t origlen, baselen;
2343 DIR *dir;
Jeff King27e1e222014-10-15 18:38:55 -04002344 struct dirent *de;
2345 int r = 0;
René Scharfe62a24c82017-10-31 14:50:06 +01002346 struct object_id oid;
Jeff King27e1e222014-10-15 18:38:55 -04002347
René Scharfe70c49052017-06-24 16:09:39 +02002348 if (subdir_nr > 0xff)
2349 BUG("invalid loose object subdirectory: %x", subdir_nr);
2350
René Scharfe0375f472017-06-24 14:12:30 +02002351 origlen = path->len;
2352 strbuf_complete(path, '/');
2353 strbuf_addf(path, "%02x", subdir_nr);
René Scharfe0375f472017-06-24 14:12:30 +02002354
2355 dir = opendir(path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04002356 if (!dir) {
René Scharfe0375f472017-06-24 14:12:30 +02002357 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002358 r = error_errno(_("unable to open %s"), path->buf);
René Scharfe0375f472017-06-24 14:12:30 +02002359 strbuf_setlen(path, origlen);
2360 return r;
Jeff King27e1e222014-10-15 18:38:55 -04002361 }
2362
René Scharfe62a24c82017-10-31 14:50:06 +01002363 oid.hash[0] = subdir_nr;
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002364 strbuf_addch(path, '/');
2365 baselen = path->len;
René Scharfe62a24c82017-10-31 14:50:06 +01002366
Elijah Newrenb548f0f2021-05-12 17:28:22 +00002367 while ((de = readdir_skip_dot_and_dotdot(dir))) {
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002368 size_t namelen;
Jeff King27e1e222014-10-15 18:38:55 -04002369
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002370 namelen = strlen(de->d_name);
Jeff King27e1e222014-10-15 18:38:55 -04002371 strbuf_setlen(path, baselen);
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002372 strbuf_add(path, de->d_name, namelen);
brian m. carlson94b5e092018-07-16 01:28:07 +00002373 if (namelen == the_hash_algo->hexsz - 2 &&
René Scharfe62a24c82017-10-31 14:50:06 +01002374 !hex_to_bytes(oid.hash + 1, de->d_name,
brian m. carlson94b5e092018-07-16 01:28:07 +00002375 the_hash_algo->rawsz - 1)) {
brian m. carlson5a6dce72021-04-26 01:02:55 +00002376 oid_set_algo(&oid, the_hash_algo);
René Scharfe62a24c82017-10-31 14:50:06 +01002377 if (obj_cb) {
2378 r = obj_cb(&oid, path->buf, data);
2379 if (r)
2380 break;
Jeff King27e1e222014-10-15 18:38:55 -04002381 }
René Scharfe62a24c82017-10-31 14:50:06 +01002382 continue;
Jeff King27e1e222014-10-15 18:38:55 -04002383 }
2384
2385 if (cruft_cb) {
2386 r = cruft_cb(de->d_name, path->buf, data);
2387 if (r)
2388 break;
2389 }
2390 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02002391 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04002392
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002393 strbuf_setlen(path, baselen - 1);
Jeff King27e1e222014-10-15 18:38:55 -04002394 if (!r && subdir_cb)
2395 r = subdir_cb(subdir_nr, path->buf, data);
2396
René Scharfe0375f472017-06-24 14:12:30 +02002397 strbuf_setlen(path, origlen);
2398
Jeff King27e1e222014-10-15 18:38:55 -04002399 return r;
2400}
2401
Jeff Kinge6f875e2015-02-08 20:13:22 -05002402int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04002403 each_loose_object_fn obj_cb,
2404 each_loose_cruft_fn cruft_cb,
2405 each_loose_subdir_fn subdir_cb,
2406 void *data)
2407{
Jeff King27e1e222014-10-15 18:38:55 -04002408 int r = 0;
2409 int i;
2410
Jeff King27e1e222014-10-15 18:38:55 -04002411 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05002412 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04002413 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002414 if (r)
2415 break;
2416 }
2417
Jeff Kinge6f875e2015-02-08 20:13:22 -05002418 return r;
2419}
2420
2421int for_each_loose_file_in_objdir(const char *path,
2422 each_loose_object_fn obj_cb,
2423 each_loose_cruft_fn cruft_cb,
2424 each_loose_subdir_fn subdir_cb,
2425 void *data)
2426{
2427 struct strbuf buf = STRBUF_INIT;
2428 int r;
2429
2430 strbuf_addstr(&buf, path);
2431 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
2432 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002433 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05002434
Jeff King27e1e222014-10-15 18:38:55 -04002435 return r;
2436}
Jeff King660c8892014-10-15 18:41:21 -04002437
Jeff Kinga7ff6f52018-08-10 19:09:44 -04002438int for_each_loose_object(each_loose_object_fn cb, void *data,
2439 enum for_each_object_flags flags)
Jeff King660c8892014-10-15 18:41:21 -04002440{
Jeff Kingf0eaf632018-11-12 09:50:39 -05002441 struct object_directory *odb;
Jeff King660c8892014-10-15 18:41:21 -04002442
Jeff Kingf0eaf632018-11-12 09:50:39 -05002443 prepare_alt_odb(the_repository);
2444 for (odb = the_repository->objects->odb; odb; odb = odb->next) {
2445 int r = for_each_loose_file_in_objdir(odb->path, cb, NULL,
2446 NULL, data);
2447 if (r)
2448 return r;
Jeff King660c8892014-10-15 18:41:21 -04002449
Jeff Kingf0eaf632018-11-12 09:50:39 -05002450 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
2451 break;
2452 }
Jeff King1385bb72015-03-27 07:32:41 -04002453
Jeff Kingf0eaf632018-11-12 09:50:39 -05002454 return 0;
Jeff King660c8892014-10-15 18:41:21 -04002455}
2456
Jeff King3a2e0822018-11-12 09:50:56 -05002457static int append_loose_object(const struct object_id *oid, const char *path,
2458 void *data)
2459{
Eric Wong92d8ed82021-07-07 23:10:19 +00002460 oidtree_insert(data, oid);
Jeff King3a2e0822018-11-12 09:50:56 -05002461 return 0;
2462}
2463
Eric Wong92d8ed82021-07-07 23:10:19 +00002464struct oidtree *odb_loose_cache(struct object_directory *odb,
René Scharfe0000d652019-01-06 17:45:30 +01002465 const struct object_id *oid)
2466{
2467 int subdir_nr = oid->hash[0];
Jeff King3a2e0822018-11-12 09:50:56 -05002468 struct strbuf buf = STRBUF_INIT;
Eric Wong33f379e2021-07-07 23:10:17 +00002469 size_t word_bits = bitsizeof(odb->loose_objects_subdir_seen[0]);
2470 size_t word_index = subdir_nr / word_bits;
Philip Oakley26de1fc2021-12-01 00:29:02 +00002471 size_t mask = (size_t)1u << (subdir_nr % word_bits);
Eric Wong33f379e2021-07-07 23:10:17 +00002472 uint32_t *bitmap;
Jeff King3a2e0822018-11-12 09:50:56 -05002473
2474 if (subdir_nr < 0 ||
Eric Wong33f379e2021-07-07 23:10:17 +00002475 subdir_nr >= bitsizeof(odb->loose_objects_subdir_seen))
Jeff King3a2e0822018-11-12 09:50:56 -05002476 BUG("subdir_nr out of range");
2477
Eric Wong33f379e2021-07-07 23:10:17 +00002478 bitmap = &odb->loose_objects_subdir_seen[word_index];
2479 if (*bitmap & mask)
Eric Wong92d8ed82021-07-07 23:10:19 +00002480 return odb->loose_objects_cache;
2481 if (!odb->loose_objects_cache) {
2482 ALLOC_ARRAY(odb->loose_objects_cache, 1);
2483 oidtree_init(odb->loose_objects_cache);
2484 }
Jeff King3a2e0822018-11-12 09:50:56 -05002485 strbuf_addstr(&buf, odb->path);
2486 for_each_file_in_obj_subdir(subdir_nr, &buf,
2487 append_loose_object,
2488 NULL, NULL,
Eric Wong92d8ed82021-07-07 23:10:19 +00002489 odb->loose_objects_cache);
Eric Wong33f379e2021-07-07 23:10:17 +00002490 *bitmap |= mask;
Jeff King7317aa72018-11-22 12:53:00 -05002491 strbuf_release(&buf);
Eric Wong92d8ed82021-07-07 23:10:19 +00002492 return odb->loose_objects_cache;
Jeff King660c8892014-10-15 18:41:21 -04002493}
2494
René Scharfed4e19e52019-01-06 17:45:39 +01002495void odb_clear_loose_cache(struct object_directory *odb)
2496{
Eric Wong92d8ed82021-07-07 23:10:19 +00002497 oidtree_clear(odb->loose_objects_cache);
2498 FREE_AND_NULL(odb->loose_objects_cache);
René Scharfed4e19e52019-01-06 17:45:39 +01002499 memset(&odb->loose_objects_subdir_seen, 0,
2500 sizeof(odb->loose_objects_subdir_seen));
2501}
2502
Jeff King00a77602019-01-07 03:37:02 -05002503static int check_stream_oid(git_zstream *stream,
2504 const char *hdr,
2505 unsigned long size,
2506 const char *path,
2507 const struct object_id *expected_oid)
Jeff Kingf6371f92017-01-13 12:58:16 -05002508{
brian m. carlson18e25882018-02-01 02:18:41 +00002509 git_hash_ctx c;
Jeff King00a77602019-01-07 03:37:02 -05002510 struct object_id real_oid;
Jeff Kingf6371f92017-01-13 12:58:16 -05002511 unsigned char buf[4096];
2512 unsigned long total_read;
2513 int status = Z_OK;
2514
brian m. carlson18e25882018-02-01 02:18:41 +00002515 the_hash_algo->init_fn(&c);
2516 the_hash_algo->update_fn(&c, hdr, stream->total_out);
Jeff Kingf6371f92017-01-13 12:58:16 -05002517
2518 /*
2519 * We already read some bytes into hdr, but the ones up to the NUL
2520 * do not count against the object's content size.
2521 */
2522 total_read = stream->total_out - strlen(hdr) - 1;
2523
2524 /*
2525 * This size comparison must be "<=" to read the final zlib packets;
Jeff King00a77602019-01-07 03:37:02 -05002526 * see the comment in unpack_loose_rest for details.
Jeff Kingf6371f92017-01-13 12:58:16 -05002527 */
2528 while (total_read <= size &&
Junio C Hamano18ad13e2018-10-31 13:12:12 +09002529 (status == Z_OK ||
2530 (status == Z_BUF_ERROR && !stream->avail_out))) {
Jeff Kingf6371f92017-01-13 12:58:16 -05002531 stream->next_out = buf;
2532 stream->avail_out = sizeof(buf);
2533 if (size - total_read < stream->avail_out)
2534 stream->avail_out = size - total_read;
2535 status = git_inflate(stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00002536 the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
Jeff Kingf6371f92017-01-13 12:58:16 -05002537 total_read += stream->next_out - buf;
2538 }
2539 git_inflate_end(stream);
2540
2541 if (status != Z_STREAM_END) {
Jeff King00a77602019-01-07 03:37:02 -05002542 error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002543 return -1;
2544 }
Jeff Kingcce044d2017-01-13 13:00:25 -05002545 if (stream->avail_in) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002546 error(_("garbage at end of loose object '%s'"),
Jeff King00a77602019-01-07 03:37:02 -05002547 oid_to_hex(expected_oid));
Jeff Kingcce044d2017-01-13 13:00:25 -05002548 return -1;
2549 }
Jeff Kingf6371f92017-01-13 12:58:16 -05002550
brian m. carlson5951bf42021-04-26 01:02:53 +00002551 the_hash_algo->final_oid_fn(&real_oid, &c);
Jeff King00a77602019-01-07 03:37:02 -05002552 if (!oideq(expected_oid, &real_oid)) {
Jeff King01f8d592019-01-07 03:40:34 -05002553 error(_("hash mismatch for %s (expected %s)"), path,
Jeff King00a77602019-01-07 03:37:02 -05002554 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002555 return -1;
2556 }
2557
2558 return 0;
2559}
2560
2561int read_loose_object(const char *path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002562 const struct object_id *expected_oid,
Ævar Arnfjörð Bjarmason96e41f52021-10-01 11:16:53 +02002563 struct object_id *real_oid,
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002564 void **contents,
2565 struct object_info *oi)
Jeff Kingf6371f92017-01-13 12:58:16 -05002566{
2567 int ret = -1;
Jeff Kingf6371f92017-01-13 12:58:16 -05002568 void *map = NULL;
2569 unsigned long mapsize;
2570 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00002571 char hdr[MAX_HEADER_LEN];
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002572 unsigned long *size = oi->sizep;
Jeff Kingf6371f92017-01-13 12:58:16 -05002573
Jeff King514c5fd2019-01-07 03:35:42 -05002574 map = map_loose_object_1(the_repository, path, NULL, &mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002575 if (!map) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002576 error_errno(_("unable to mmap %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002577 goto out;
2578 }
2579
Ævar Arnfjörð Bjarmason01cab972021-10-01 11:16:48 +02002580 if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr),
2581 NULL) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002582 error(_("unable to unpack header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002583 goto out;
2584 }
2585
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002586 if (parse_loose_header(hdr, oi) < 0) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002587 error(_("unable to parse header of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002588 git_inflate_end(&stream);
2589 goto out;
2590 }
2591
Ævar Arnfjörð Bjarmason31deb282021-10-01 11:16:52 +02002592 if (*oi->typep == OBJ_BLOB && *size > big_file_threshold) {
Jeff King00a77602019-01-07 03:37:02 -05002593 if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
Jeff Kingf6371f92017-01-13 12:58:16 -05002594 goto out;
2595 } else {
Jeff King00a77602019-01-07 03:37:02 -05002596 *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
Jeff Kingf6371f92017-01-13 12:58:16 -05002597 if (!*contents) {
Nguyễn Thái Ngọc Duy259328b2018-07-21 09:49:39 +02002598 error(_("unable to unpack contents of %s"), path);
Jeff Kingf6371f92017-01-13 12:58:16 -05002599 git_inflate_end(&stream);
2600 goto out;
2601 }
Matheus Tavaresb98d1882020-01-30 17:32:23 -03002602 if (check_object_signature(the_repository, expected_oid,
Ævar Arnfjörð Bjarmason16235e32021-11-11 06:18:56 +01002603 *contents, *size,
2604 oi->type_name->buf, real_oid))
Jeff Kingf6371f92017-01-13 12:58:16 -05002605 goto out;
Jeff Kingf6371f92017-01-13 12:58:16 -05002606 }
2607
2608 ret = 0; /* everything checks out */
2609
2610out:
2611 if (map)
2612 munmap(map, mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002613 return ret;
2614}