blob: dcd6b879acc87c99f35ba1d37abd515c10be269f [file] [log] [blame]
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 *
6 * This handles basic git sha1 object files - packing, unpacking,
7 * creation etc.
8 */
Linus Torvalds0fcfd162005-04-18 13:04:43 -07009#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070010#include "config.h"
Michael Haggerty6eac50d2012-11-05 09:41:22 +010011#include "string-list.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020012#include "lockfile.h"
Junio C Hamano1f688552005-06-27 03:35:33 -070013#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -070014#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020015#include "blob.h"
16#include "commit.h"
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -070017#include "run-command.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020018#include "tag.h"
19#include "tree.h"
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +070020#include "tree-walk.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070021#include "refs.h"
Nicolas Pitre70f5d5d2008-02-28 00:25:19 -050022#include "pack-revindex.h"
Junio C Hamano628522e2007-12-29 02:05:47 -080023#include "sha1-lookup.h"
Junio C Hamano568508e2011-10-28 14:48:40 -070024#include "bulk-checkin.h"
Stefan Beller031dc922018-03-23 18:20:57 +010025#include "repository.h"
Stefan Beller47f351e2018-04-11 17:21:06 -070026#include "replace-object.h"
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +070027#include "streaming.h"
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070028#include "dir.h"
Jeff King12d95ef2016-08-22 17:59:42 -040029#include "list.h"
René Scharfec4c6eff2016-09-13 19:54:42 +020030#include "mergesort.h"
Jeff Kingcf3c6352016-12-12 14:52:22 -050031#include "quote.h"
Jonathan Tan4f39cd82017-08-18 15:20:16 -070032#include "packfile.h"
Jonathan Tan8b4c0102017-12-08 15:27:14 +000033#include "fetch-object.h"
Stefan Beller90c62152018-03-23 18:20:55 +010034#include "object-store.h"
Pavel Roskine05db0f2007-01-09 23:07:11 -050035
brian m. carlson1af64f72018-03-12 02:27:55 +000036/* The maximum size for an object header. */
37#define MAX_HEADER_LEN 32
38
Patryk Obara50c5cd52017-08-18 03:59:35 +020039const unsigned char null_sha1[GIT_MAX_RAWSZ];
brian m. carlson3e56e722015-12-06 22:16:35 +000040const struct object_id null_oid;
Jacob Keller8576fde2016-08-31 16:27:18 -070041const struct object_id empty_tree_oid = {
42 EMPTY_TREE_SHA1_BIN_LITERAL
43};
44const struct object_id empty_blob_oid = {
45 EMPTY_BLOB_SHA1_BIN_LITERAL
46};
Junio C Hamano88cd6212005-09-30 14:02:47 -070047
brian m. carlsonac73ced2018-02-01 02:18:38 +000048static void git_hash_sha1_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000049{
brian m. carlsonac73ced2018-02-01 02:18:38 +000050 git_SHA1_Init(&ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000051}
52
brian m. carlsonac73ced2018-02-01 02:18:38 +000053static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000054{
brian m. carlsonac73ced2018-02-01 02:18:38 +000055 git_SHA1_Update(&ctx->sha1, data, len);
brian m. carlsonf50e7662017-11-12 21:28:52 +000056}
57
brian m. carlsonac73ced2018-02-01 02:18:38 +000058static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000059{
brian m. carlsonac73ced2018-02-01 02:18:38 +000060 git_SHA1_Final(hash, &ctx->sha1);
brian m. carlsonf50e7662017-11-12 21:28:52 +000061}
62
brian m. carlsonac73ced2018-02-01 02:18:38 +000063static void git_hash_unknown_init(git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000064{
65 die("trying to init unknown hash");
66}
67
brian m. carlsonac73ced2018-02-01 02:18:38 +000068static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
brian m. carlsonf50e7662017-11-12 21:28:52 +000069{
70 die("trying to update unknown hash");
71}
72
brian m. carlsonac73ced2018-02-01 02:18:38 +000073static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
brian m. carlsonf50e7662017-11-12 21:28:52 +000074{
75 die("trying to finalize unknown hash");
76}
77
78const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
79 {
80 NULL,
81 0x00000000,
82 0,
83 0,
brian m. carlsonf50e7662017-11-12 21:28:52 +000084 git_hash_unknown_init,
85 git_hash_unknown_update,
86 git_hash_unknown_final,
87 NULL,
88 NULL,
89 },
90 {
91 "sha-1",
92 /* "sha1", big-endian */
93 0x73686131,
brian m. carlsonf50e7662017-11-12 21:28:52 +000094 GIT_SHA1_RAWSZ,
95 GIT_SHA1_HEXSZ,
96 git_hash_sha1_init,
97 git_hash_sha1_update,
98 git_hash_sha1_final,
99 &empty_tree_oid,
100 &empty_blob_oid,
101 },
102};
103
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700104/*
105 * This is meant to hold a *small* number of objects that you would
106 * want read_sha1_file() to be able to return, but yet you do not want
107 * to write them into the object store (e.g. a browse-only
108 * application).
109 */
110static struct cached_object {
111 unsigned char sha1[20];
112 enum object_type type;
113 void *buf;
114 unsigned long size;
115} *cached_objects;
116static int cached_object_nr, cached_object_alloc;
117
118static struct cached_object empty_tree = {
Jonathan Niederdab0d412011-02-07 02:17:27 -0600119 EMPTY_TREE_SHA1_BIN_LITERAL,
Nguyễn Thái Ngọc Duyc597ba82011-02-05 21:03:01 +0700120 OBJ_TREE,
121 "",
122 0
123};
124
125static struct cached_object *find_cached_object(const unsigned char *sha1)
126{
127 int i;
128 struct cached_object *co = cached_objects;
129
130 for (i = 0; i < cached_object_nr; i++, co++) {
131 if (!hashcmp(co->sha1, sha1))
132 return co;
133 }
134 if (!hashcmp(sha1, empty_tree.sha1))
135 return &empty_tree;
136 return NULL;
137}
138
Torsten Bögershausen94729352017-11-16 17:38:28 +0100139
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100140static int get_conv_flags(unsigned flags)
Torsten Bögershausen94729352017-11-16 17:38:28 +0100141{
142 if (flags & HASH_RENORMALIZE)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100143 return CONV_EOL_RENORMALIZE;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100144 else if (flags & HASH_WRITE_OBJECT)
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100145 return global_conv_flags_eol;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100146 else
Torsten Bögershausen8462ff42018-01-13 23:49:31 +0100147 return 0;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100148}
149
150
Junio C Hamano90a64642011-03-10 16:02:50 -0800151int mkdir_in_gitdir(const char *path)
152{
153 if (mkdir(path, 0777)) {
154 int saved_errno = errno;
155 struct stat st;
156 struct strbuf sb = STRBUF_INIT;
157
158 if (errno != EEXIST)
159 return -1;
160 /*
161 * Are we looking at a path in a symlinked worktree
162 * whose original repository does not yet have it?
163 * e.g. .git/rr-cache pointing at its original
164 * repository in which the user hasn't performed any
165 * conflict resolution yet?
166 */
167 if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
168 strbuf_readlink(&sb, path, st.st_size) ||
169 !is_absolute_path(sb.buf) ||
170 mkdir(sb.buf, 0777)) {
171 strbuf_release(&sb);
172 errno = saved_errno;
173 return -1;
174 }
175 strbuf_release(&sb);
176 }
177 return adjust_shared_perm(path);
178}
179
Michael Haggerty0be05212014-01-06 14:45:25 +0100180enum scld_error safe_create_leading_directories(char *path)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700181{
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100182 char *next_component = path + offset_1st_component(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100183 enum scld_error ret = SCLD_OK;
Jason Riedy67d42212006-02-09 17:56:13 -0800184
Michael Haggerty0be05212014-01-06 14:45:25 +0100185 while (ret == SCLD_OK && next_component) {
Michael Haggertyf0502332014-01-06 14:45:20 +0100186 struct stat st;
Michael Haggerty0f527402014-01-19 00:40:44 +0100187 char *slash = next_component, slash_character;
Michael Haggertyf0502332014-01-06 14:45:20 +0100188
Michael Haggerty0f527402014-01-19 00:40:44 +0100189 while (*slash && !is_dir_sep(*slash))
190 slash++;
191
192 if (!*slash)
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700193 break;
Michael Haggertybf10cf72014-01-06 14:45:23 +0100194
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100195 next_component = slash + 1;
Michael Haggerty0f527402014-01-19 00:40:44 +0100196 while (is_dir_sep(*next_component))
Michael Haggertybf10cf72014-01-06 14:45:23 +0100197 next_component++;
Michael Haggerty26c8ae22014-01-06 14:45:22 +0100198 if (!*next_component)
Junio C Hamano5f0bdf52008-09-02 14:10:15 -0700199 break;
Michael Haggerty831651f2014-01-06 14:45:21 +0100200
Michael Haggerty0f527402014-01-19 00:40:44 +0100201 slash_character = *slash;
Michael Haggerty831651f2014-01-06 14:45:21 +0100202 *slash = '\0';
Jason Riedy67d42212006-02-09 17:56:13 -0800203 if (!stat(path, &st)) {
204 /* path exists */
Michael Haggerty204a0472017-01-06 17:22:25 +0100205 if (!S_ISDIR(st.st_mode)) {
206 errno = ENOTDIR;
Michael Haggerty0be05212014-01-06 14:45:25 +0100207 ret = SCLD_EXISTS;
Michael Haggerty204a0472017-01-06 17:22:25 +0100208 }
Michael Haggerty53a39722014-01-06 14:45:19 +0100209 } else if (mkdir(path, 0777)) {
Steven Walter928734d2013-03-17 10:09:27 -0400210 if (errno == EEXIST &&
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100211 !stat(path, &st) && S_ISDIR(st.st_mode))
Steven Walter928734d2013-03-17 10:09:27 -0400212 ; /* somebody created it since we checked */
Michael Haggerty18d37e82014-01-06 14:45:27 +0100213 else if (errno == ENOENT)
214 /*
215 * Either mkdir() failed because
216 * somebody just pruned the containing
217 * directory, or stat() failed because
218 * the file that was in our way was
219 * just removed. Either way, inform
220 * the caller that it might be worth
221 * trying again:
222 */
223 ret = SCLD_VANISHED;
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100224 else
Michael Haggerty0be05212014-01-06 14:45:25 +0100225 ret = SCLD_FAILED;
Michael Haggerty53a39722014-01-06 14:45:19 +0100226 } else if (adjust_shared_perm(path)) {
Michael Haggerty0be05212014-01-06 14:45:25 +0100227 ret = SCLD_PERMS;
Jason Riedy67d42212006-02-09 17:56:13 -0800228 }
Michael Haggerty0f527402014-01-19 00:40:44 +0100229 *slash = slash_character;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700230 }
Michael Haggerty9e6f8852014-01-06 14:45:24 +0100231 return ret;
Junio C Hamanob2cb9422005-07-06 01:11:52 -0700232}
233
Michael Haggerty0be05212014-01-06 14:45:25 +0100234enum scld_error safe_create_leading_directories_const(const char *path)
Jeff King8e21d632008-06-25 01:41:34 -0400235{
Michael Haggerty02944302017-01-06 17:22:24 +0100236 int save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400237 /* path points to cache entries, so xstrdup before messing with it */
238 char *buf = xstrdup(path);
Michael Haggerty0be05212014-01-06 14:45:25 +0100239 enum scld_error result = safe_create_leading_directories(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100240
241 save_errno = errno;
Jeff King8e21d632008-06-25 01:41:34 -0400242 free(buf);
Michael Haggerty02944302017-01-06 17:22:24 +0100243 errno = save_errno;
Jeff King8e21d632008-06-25 01:41:34 -0400244 return result;
245}
246
Michael Haggerty177978f2017-01-06 17:22:26 +0100247int raceproof_create_file(const char *path, create_file_fn fn, void *cb)
248{
249 /*
250 * The number of times we will try to remove empty directories
251 * in the way of path. This is only 1 because if another
252 * process is racily creating directories that conflict with
253 * us, we don't want to fight against them.
254 */
255 int remove_directories_remaining = 1;
256
257 /*
258 * The number of times that we will try to create the
259 * directories containing path. We are willing to attempt this
260 * more than once, because another process could be trying to
261 * clean up empty directories at the same time as we are
262 * trying to create them.
263 */
264 int create_directories_remaining = 3;
265
266 /* A scratch copy of path, filled lazily if we need it: */
267 struct strbuf path_copy = STRBUF_INIT;
268
269 int ret, save_errno;
270
271 /* Sanity check: */
272 assert(*path);
273
274retry_fn:
275 ret = fn(path, cb);
276 save_errno = errno;
277 if (!ret)
278 goto out;
279
280 if (errno == EISDIR && remove_directories_remaining-- > 0) {
281 /*
282 * A directory is in the way. Maybe it is empty; try
283 * to remove it:
284 */
285 if (!path_copy.len)
286 strbuf_addstr(&path_copy, path);
287
288 if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))
289 goto retry_fn;
290 } else if (errno == ENOENT && create_directories_remaining-- > 0) {
291 /*
292 * Maybe the containing directory didn't exist, or
293 * maybe it was just deleted by a process that is
294 * racing with us to clean up empty directories. Try
295 * to create it:
296 */
297 enum scld_error scld_result;
298
299 if (!path_copy.len)
300 strbuf_addstr(&path_copy, path);
301
302 do {
303 scld_result = safe_create_leading_directories(path_copy.buf);
304 if (scld_result == SCLD_OK)
305 goto retry_fn;
306 } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);
307 }
308
309out:
310 strbuf_release(&path_copy);
311 errno = save_errno;
312 return ret;
313}
314
Jeff Kingf7b77742016-10-03 16:36:09 -0400315static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
Junio C Hamanoace15342005-05-07 00:38:04 -0700316{
317 int i;
318 for (i = 0; i < 20; i++) {
319 static char hex[] = "0123456789abcdef";
320 unsigned int val = sha1[i];
Jeff Kingf7b77742016-10-03 16:36:09 -0400321 strbuf_addch(buf, hex[val >> 4]);
322 strbuf_addch(buf, hex[val & 0xf]);
Jeff Kingafbba2f2016-10-03 16:35:55 -0400323 if (!i)
Jeff Kingf7b77742016-10-03 16:36:09 -0400324 strbuf_addch(buf, '/');
Junio C Hamanoace15342005-05-07 00:38:04 -0700325 }
326}
327
Stefan Bellera68377b2018-03-23 18:21:16 +0100328void sha1_file_name(struct repository *r, struct strbuf *buf, const unsigned char *sha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700329{
Stefan Bellera68377b2018-03-23 18:21:16 +0100330 strbuf_addstr(buf, r->objects->objectdir);
Christian Couder34498472018-01-18 11:08:54 +0100331 strbuf_addch(buf, '/');
Christian Couderea657732018-01-17 18:54:54 +0100332 fill_sha1_path(buf, sha1);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700333}
334
Jeff King38dbe5f2016-10-03 16:36:04 -0400335struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
336{
337 strbuf_setlen(&alt->scratch, alt->base_len);
338 return &alt->scratch;
339}
340
Jeff King29ec6af2016-10-03 16:35:43 -0400341static const char *alt_sha1_path(struct alternate_object_database *alt,
342 const unsigned char *sha1)
343{
Jeff King38dbe5f2016-10-03 16:36:04 -0400344 struct strbuf *buf = alt_scratch_buf(alt);
Jeff Kingf7b77742016-10-03 16:36:09 -0400345 fill_sha1_path(buf, sha1);
Jeff King38dbe5f2016-10-03 16:36:04 -0400346 return buf->buf;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700347}
348
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700349/*
Jeff King4ea82472016-10-03 16:34:48 -0400350 * Return non-zero iff the path is usable as an alternate object database.
351 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100352static int alt_odb_usable(struct raw_object_store *o,
353 struct strbuf *path,
354 const char *normalized_objdir)
Jeff King4ea82472016-10-03 16:34:48 -0400355{
356 struct alternate_object_database *alt;
357
358 /* Detect cases where alternate disappeared */
359 if (!is_directory(path->buf)) {
360 error("object directory %s does not exist; "
361 "check .git/objects/info/alternates.",
362 path->buf);
363 return 0;
364 }
365
366 /*
367 * Prevent the common mistake of listing the same
368 * thing twice, or object directory itself.
369 */
Stefan Beller13313fc2018-03-23 18:21:03 +0100370 for (alt = o->alt_odb_list; alt; alt = alt->next) {
Jeff Kingea0fc3b2016-10-03 16:36:26 -0400371 if (!fspathcmp(path->buf, alt->path))
Jeff King4ea82472016-10-03 16:34:48 -0400372 return 0;
373 }
374 if (!fspathcmp(path->buf, normalized_objdir))
375 return 0;
376
377 return 1;
378}
379
380/*
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700381 * Prepare alternate object database registry.
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700382 *
383 * The variable alt_odb_list points at the list of struct
384 * alternate_object_database. The elements on this list come from
385 * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
386 * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
Junio C Hamano1494e032005-12-04 22:48:43 -0800387 * whose contents is similar to that environment variable but can be
388 * LF separated. Its base points at a statically allocated buffer that
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700389 * contains "/the/directory/corresponding/to/.git/objects/...", while
390 * its name points just after the slash at the end of ".git/objects/"
391 * in the example above, and has enough space to hold 40-byte hex
392 * SHA1, an extra slash for the first level indirection, and the
393 * terminating NUL.
Junio C Hamanoddd5d052005-05-08 13:51:13 -0700394 */
Stefan Beller77f012e2018-03-23 18:21:08 +0100395static void read_info_alternates(struct repository *r,
396 const char *relative_base,
397 int depth);
398static int link_alt_odb_entry(struct repository *r, const char *entry,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100399 const char *relative_base, int depth, const char *normalized_objdir)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200400{
Martin Waitzc2f493a2006-05-07 20:19:21 +0200401 struct alternate_object_database *ent;
Hui Wang5bdf0a82011-09-07 18:37:47 +0800402 struct strbuf pathbuf = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200403
Johannes Sixt85dadc32007-11-13 21:05:00 +0100404 if (!is_absolute_path(entry) && relative_base) {
Brandon Williams4ac90062016-12-12 10:16:55 -0800405 strbuf_realpath(&pathbuf, relative_base, 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800406 strbuf_addch(&pathbuf, '/');
Martin Waitzc2f493a2006-05-07 20:19:21 +0200407 }
Michael Haggerty6eac50d2012-11-05 09:41:22 +0100408 strbuf_addstr(&pathbuf, entry);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800409
Jeff King37a95862016-11-07 23:50:17 -0500410 if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
Jeff King670c3592016-10-03 16:34:17 -0400411 error("unable to normalize alternate object path: %s",
412 pathbuf.buf);
413 strbuf_release(&pathbuf);
414 return -1;
415 }
Hui Wang5bdf0a82011-09-07 18:37:47 +0800416
417 /*
418 * The trailing slash after the directory name is given by
419 * this function at the end. Remove duplicates.
420 */
Jeff King4ea82472016-10-03 16:34:48 -0400421 while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
422 strbuf_setlen(&pathbuf, pathbuf.len - 1);
Hui Wang5bdf0a82011-09-07 18:37:47 +0800423
Stefan Beller77f012e2018-03-23 18:21:08 +0100424 if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
Jeff King4ea82472016-10-03 16:34:48 -0400425 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200426 return -1;
427 }
428
Jeff King7f0fa2c2016-10-03 16:35:31 -0400429 ent = alloc_alt_odb(pathbuf.buf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200430
431 /* add the alternate entry */
Stefan Beller77f012e2018-03-23 18:21:08 +0100432 *r->objects->alt_odb_tail = ent;
433 r->objects->alt_odb_tail = &(ent->next);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200434 ent->next = NULL;
435
436 /* recursively add alternates */
Stefan Beller77f012e2018-03-23 18:21:08 +0100437 read_info_alternates(r, pathbuf.buf, depth + 1);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200438
Jeff King4ea82472016-10-03 16:34:48 -0400439 strbuf_release(&pathbuf);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200440 return 0;
441}
442
Jeff Kingcf3c6352016-12-12 14:52:22 -0500443static const char *parse_alt_odb_entry(const char *string,
444 int sep,
445 struct strbuf *out)
446{
447 const char *end;
448
449 strbuf_reset(out);
450
451 if (*string == '#') {
452 /* comment; consume up to next separator */
453 end = strchrnul(string, sep);
454 } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
455 /*
456 * quoted path; unquote_c_style has copied the
457 * data for us and set "end". Broken quoting (e.g.,
458 * an entry that doesn't end with a quote) falls
459 * back to the unquoted case below.
460 */
461 } else {
462 /* normal, unquoted path */
463 end = strchrnul(string, sep);
464 strbuf_add(out, string, end - string);
465 }
466
467 if (*end)
468 end++;
469 return end;
470}
471
Stefan Beller77f012e2018-03-23 18:21:08 +0100472static void link_alt_odb_entries(struct repository *r, const char *alt,
473 int sep, const char *relative_base, int depth)
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700474{
Ephrim Khong539e7502014-07-15 13:29:45 +0200475 struct strbuf objdirbuf = STRBUF_INIT;
Jeff Kingcf3c6352016-12-12 14:52:22 -0500476 struct strbuf entry = STRBUF_INIT;
Martin Waitzc2f493a2006-05-07 20:19:21 +0200477
Jeff Kingf28e3662017-11-12 10:27:39 +0000478 if (!alt || !*alt)
479 return;
480
Martin Waitzc2f493a2006-05-07 20:19:21 +0200481 if (depth > 5) {
482 error("%s: ignoring alternate object stores, nesting too deep.",
483 relative_base);
484 return;
485 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700486
Stefan Beller77f012e2018-03-23 18:21:08 +0100487 strbuf_add_absolute_path(&objdirbuf, r->objects->objectdir);
Jeff King670c3592016-10-03 16:34:17 -0400488 if (strbuf_normalize_path(&objdirbuf) < 0)
489 die("unable to normalize object directory: %s",
490 objdirbuf.buf);
Ephrim Khong539e7502014-07-15 13:29:45 +0200491
Jeff Kingcf3c6352016-12-12 14:52:22 -0500492 while (*alt) {
493 alt = parse_alt_odb_entry(alt, sep, &entry);
494 if (!entry.len)
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700495 continue;
Stefan Beller77f012e2018-03-23 18:21:08 +0100496 link_alt_odb_entry(r, entry.buf,
Stefan Bellercfc62fc2018-03-23 18:21:04 +0100497 relative_base, depth, objdirbuf.buf);
Junio C Hamano9577e7e2005-08-16 18:22:05 -0700498 }
Jeff Kingcf3c6352016-12-12 14:52:22 -0500499 strbuf_release(&entry);
Ephrim Khong539e7502014-07-15 13:29:45 +0200500 strbuf_release(&objdirbuf);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700501}
502
Stefan Beller77f012e2018-03-23 18:21:08 +0100503static void read_info_alternates(struct repository *r,
504 const char *relative_base,
505 int depth)
Junio C Hamanoace15342005-05-07 00:38:04 -0700506{
Jeff King5015f012015-08-19 14:12:45 -0400507 char *path;
Jeff Kingdc732bd2017-09-19 15:41:07 -0400508 struct strbuf buf = STRBUF_INIT;
Jason Riedyc7c81b32005-08-23 13:34:07 -0700509
Jeff King5015f012015-08-19 14:12:45 -0400510 path = xstrfmt("%s/info/alternates", relative_base);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400511 if (strbuf_read_file(&buf, path, 1024) < 0) {
Jeff Kingf0f7beb2017-09-19 15:41:10 -0400512 warn_on_fopen_errors(path);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400513 free(path);
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700514 return;
Junio C Hamanoace15342005-05-07 00:38:04 -0700515 }
Junio C Hamanod5a63b92005-08-14 17:25:57 -0700516
Stefan Beller77f012e2018-03-23 18:21:08 +0100517 link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
Jeff Kingdc732bd2017-09-19 15:41:07 -0400518 strbuf_release(&buf);
519 free(path);
Junio C Hamanoace15342005-05-07 00:38:04 -0700520}
521
Jeff King7f0fa2c2016-10-03 16:35:31 -0400522struct alternate_object_database *alloc_alt_odb(const char *dir)
523{
524 struct alternate_object_database *ent;
Jeff King7f0fa2c2016-10-03 16:35:31 -0400525
Jeff King597f9132016-10-03 16:35:51 -0400526 FLEX_ALLOC_STR(ent, path, dir);
Jeff King38dbe5f2016-10-03 16:36:04 -0400527 strbuf_init(&ent->scratch, 0);
528 strbuf_addf(&ent->scratch, "%s/", dir);
529 ent->base_len = ent->scratch.len;
Jeff King7f0fa2c2016-10-03 16:35:31 -0400530
531 return ent;
532}
533
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400534void add_to_alternates_file(const char *reference)
535{
Martin Ågrenf132a122017-10-05 22:32:03 +0200536 struct lock_file lock = LOCK_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400537 char *alts = git_pathdup("objects/info/alternates");
538 FILE *in, *out;
Martin Ågrenf132a122017-10-05 22:32:03 +0200539 int found = 0;
Jeff King77b9b1d2015-08-10 05:34:46 -0400540
Martin Ågrenf132a122017-10-05 22:32:03 +0200541 hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
542 out = fdopen_lock_file(&lock, "w");
Jeff King77b9b1d2015-08-10 05:34:46 -0400543 if (!out)
544 die_errno("unable to fdopen alternates lockfile");
545
546 in = fopen(alts, "r");
547 if (in) {
548 struct strbuf line = STRBUF_INIT;
Jeff King77b9b1d2015-08-10 05:34:46 -0400549
Junio C Hamano3f163962015-10-28 13:29:24 -0700550 while (strbuf_getline(&line, in) != EOF) {
Jeff King77b9b1d2015-08-10 05:34:46 -0400551 if (!strcmp(reference, line.buf)) {
552 found = 1;
553 break;
554 }
555 fprintf_or_die(out, "%s\n", line.buf);
556 }
557
558 strbuf_release(&line);
559 fclose(in);
Jeff King77b9b1d2015-08-10 05:34:46 -0400560 }
561 else if (errno != ENOENT)
562 die_errno("unable to read alternates file");
563
Martin Ågrenf132a122017-10-05 22:32:03 +0200564 if (found) {
565 rollback_lock_file(&lock);
566 } else {
Jeff King77b9b1d2015-08-10 05:34:46 -0400567 fprintf_or_die(out, "%s\n", reference);
Martin Ågrenf132a122017-10-05 22:32:03 +0200568 if (commit_lock_file(&lock))
Jeff King77b9b1d2015-08-10 05:34:46 -0400569 die_errno("unable to move new alternates file into place");
Stefan Beller031dc922018-03-23 18:20:57 +0100570 if (the_repository->objects->alt_odb_tail)
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100571 link_alt_odb_entries(the_repository, reference,
572 '\n', NULL, 0);
Jeff King77b9b1d2015-08-10 05:34:46 -0400573 }
574 free(alts);
Daniel Barkalowbef70b22008-04-17 19:32:30 -0400575}
576
Jeff Kinga5b34d22016-10-03 16:35:03 -0400577void add_to_alternates_memory(const char *reference)
578{
579 /*
580 * Make sure alternates are initialized, or else our entry may be
581 * overwritten when they are.
582 */
Stefan Beller0b209032018-03-23 18:21:07 +0100583 prepare_alt_odb(the_repository);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400584
Stefan Beller93d8d1e2018-03-23 18:21:06 +0100585 link_alt_odb_entries(the_repository, reference,
586 '\n', NULL, 0);
Jeff Kinga5b34d22016-10-03 16:35:03 -0400587}
588
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700589/*
590 * Compute the exact path an alternate is at and returns it. In case of
591 * error NULL is returned and the human readable error is added to `err`
592 * `path` may be relative and should point to $GITDIR.
593 * `err` must not be null.
594 */
595char *compute_alternate_path(const char *path, struct strbuf *err)
596{
597 char *ref_git = NULL;
598 const char *repo, *ref_git_s;
599 int seen_error = 0;
600
601 ref_git_s = real_path_if_valid(path);
602 if (!ref_git_s) {
603 seen_error = 1;
604 strbuf_addf(err, _("path '%s' does not exist"), path);
605 goto out;
606 } else
607 /*
608 * Beware: read_gitfile(), real_path() and mkpath()
609 * return static buffer
610 */
611 ref_git = xstrdup(ref_git_s);
612
613 repo = read_gitfile(ref_git);
614 if (!repo)
615 repo = read_gitfile(mkpath("%s/.git", ref_git));
616 if (repo) {
617 free(ref_git);
618 ref_git = xstrdup(repo);
619 }
620
621 if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
622 char *ref_git_git = mkpathdup("%s/.git", ref_git);
623 free(ref_git);
624 ref_git = ref_git_git;
625 } else if (!is_directory(mkpath("%s/objects", ref_git))) {
626 struct strbuf sb = STRBUF_INIT;
627 seen_error = 1;
628 if (get_common_dir(&sb, ref_git)) {
629 strbuf_addf(err,
630 _("reference repository '%s' as a linked "
631 "checkout is not supported yet."),
632 path);
633 goto out;
634 }
635
636 strbuf_addf(err, _("reference repository '%s' is not a "
637 "local repository."), path);
638 goto out;
639 }
640
641 if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
642 strbuf_addf(err, _("reference repository '%s' is shallow"),
643 path);
644 seen_error = 1;
645 goto out;
646 }
647
648 if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
649 strbuf_addf(err,
650 _("reference repository '%s' is grafted"),
651 path);
652 seen_error = 1;
653 goto out;
654 }
655
656out:
657 if (seen_error) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000658 FREE_AND_NULL(ref_git);
Stefan Beller9eeea7d2016-08-15 14:53:24 -0700659 }
660
661 return ref_git;
662}
663
Jeff Kingfe1b2262014-10-15 18:33:13 -0400664int foreach_alt_odb(alt_odb_fn fn, void *cb)
Junio C Hamanod79796b2008-09-09 01:27:10 -0700665{
666 struct alternate_object_database *ent;
Jeff Kingfe1b2262014-10-15 18:33:13 -0400667 int r = 0;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700668
Stefan Beller0b209032018-03-23 18:21:07 +0100669 prepare_alt_odb(the_repository);
Stefan Beller031dc922018-03-23 18:20:57 +0100670 for (ent = the_repository->objects->alt_odb_list; ent; ent = ent->next) {
Jeff Kingfe1b2262014-10-15 18:33:13 -0400671 r = fn(ent, cb);
672 if (r)
673 break;
674 }
675 return r;
Junio C Hamanod79796b2008-09-09 01:27:10 -0700676}
677
Stefan Beller13068bf2018-03-23 18:21:09 +0100678void prepare_alt_odb(struct repository *r)
Martin Waitzc2f493a2006-05-07 20:19:21 +0200679{
Stefan Beller13068bf2018-03-23 18:21:09 +0100680 if (r->objects->alt_odb_tail)
Shawn O. Pearce7dc24aa2007-05-26 01:24:40 -0400681 return;
682
Stefan Beller13068bf2018-03-23 18:21:09 +0100683 r->objects->alt_odb_tail = &r->objects->alt_odb_list;
684 link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200685
Stefan Beller13068bf2018-03-23 18:21:09 +0100686 read_info_alternates(r, r->objects->objectdir, 0);
Martin Waitzc2f493a2006-05-07 20:19:21 +0200687}
688
Jeff King3096b2e2015-07-08 16:33:52 -0400689/* Returns 1 if we have successfully freshened the file, 0 otherwise. */
Jeff King33d42212014-10-15 18:42:22 -0400690static int freshen_file(const char *fn)
Junio C Hamanoace15342005-05-07 00:38:04 -0700691{
Jeff King33d42212014-10-15 18:42:22 -0400692 struct utimbuf t;
693 t.actime = t.modtime = time(NULL);
694 return !utime(fn, &t);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600695}
Junio C Hamanoace15342005-05-07 00:38:04 -0700696
Jeff King3096b2e2015-07-08 16:33:52 -0400697/*
698 * All of the check_and_freshen functions return 1 if the file exists and was
699 * freshened (if freshening was requested), 0 otherwise. If they return
700 * 0, you should not assume that it is safe to skip a write of the object (it
701 * either does not exist on disk, or has a stale mtime and may be subject to
702 * pruning).
703 */
Christian Couder6a5e6f52017-02-27 19:00:11 +0100704int check_and_freshen_file(const char *fn, int freshen)
Jeff King33d42212014-10-15 18:42:22 -0400705{
706 if (access(fn, F_OK))
707 return 0;
Jeff King3096b2e2015-07-08 16:33:52 -0400708 if (freshen && !freshen_file(fn))
Jeff King33d42212014-10-15 18:42:22 -0400709 return 0;
710 return 1;
711}
712
713static int check_and_freshen_local(const unsigned char *sha1, int freshen)
714{
Christian Couderea657732018-01-17 18:54:54 +0100715 static struct strbuf buf = STRBUF_INIT;
716
717 strbuf_reset(&buf);
Stefan Bellercf78ae42018-03-23 18:21:10 +0100718 sha1_file_name(the_repository, &buf, sha1);
Christian Couderea657732018-01-17 18:54:54 +0100719
720 return check_and_freshen_file(buf.buf, freshen);
Jeff King33d42212014-10-15 18:42:22 -0400721}
722
723static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
Brandon Casey0f4dc142008-11-09 23:59:57 -0600724{
725 struct alternate_object_database *alt;
Stefan Beller0b209032018-03-23 18:21:07 +0100726 prepare_alt_odb(the_repository);
Stefan Beller031dc922018-03-23 18:20:57 +0100727 for (alt = the_repository->objects->alt_odb_list; alt; alt = alt->next) {
Jeff King29ec6af2016-10-03 16:35:43 -0400728 const char *path = alt_sha1_path(alt, sha1);
729 if (check_and_freshen_file(path, freshen))
Linus Torvaldsc529d752008-06-14 11:43:01 -0700730 return 1;
Junio C Hamanoace15342005-05-07 00:38:04 -0700731 }
Linus Torvaldsc529d752008-06-14 11:43:01 -0700732 return 0;
Junio C Hamanoace15342005-05-07 00:38:04 -0700733}
734
Jeff King33d42212014-10-15 18:42:22 -0400735static int check_and_freshen(const unsigned char *sha1, int freshen)
736{
737 return check_and_freshen_local(sha1, freshen) ||
738 check_and_freshen_nonlocal(sha1, freshen);
739}
740
741int has_loose_object_nonlocal(const unsigned char *sha1)
742{
743 return check_and_freshen_nonlocal(sha1, 0);
744}
745
Brandon Casey0f4dc142008-11-09 23:59:57 -0600746static int has_loose_object(const unsigned char *sha1)
747{
Jeff King33d42212014-10-15 18:42:22 -0400748 return check_and_freshen(sha1, 0);
Brandon Casey0f4dc142008-11-09 23:59:57 -0600749}
750
Steffen Prohaska02710222014-08-26 17:23:23 +0200751static void mmap_limit_check(size_t length)
752{
753 static size_t limit = 0;
754 if (!limit) {
755 limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
756 if (!limit)
757 limit = SIZE_MAX;
758 }
759 if (length > limit)
760 die("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX,
761 (uintmax_t)length, (uintmax_t)limit);
762}
763
Jeff King15708562015-05-28 03:56:15 -0400764void *xmmap_gently(void *start, size_t length,
765 int prot, int flags, int fd, off_t offset)
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500766{
Steffen Prohaska02710222014-08-26 17:23:23 +0200767 void *ret;
768
769 mmap_limit_check(length);
770 ret = mmap(start, length, prot, flags, fd, offset);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500771 if (ret == MAP_FAILED) {
772 if (!length)
773 return NULL;
Brandon Casey7c3ecb32013-07-31 12:51:37 -0700774 release_pack_memory(length);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500775 ret = mmap(start, length, prot, flags, fd, offset);
Jonathan Nieder58ecbd52010-11-06 06:44:11 -0500776 }
777 return ret;
778}
779
Jeff King15708562015-05-28 03:56:15 -0400780void *xmmap(void *start, size_t length,
781 int prot, int flags, int fd, off_t offset)
782{
783 void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
784 if (ret == MAP_FAILED)
Junio C Hamano9ca0aaf2015-05-27 13:30:29 -0700785 die_errno("mmap failed");
Jeff King15708562015-05-28 03:56:15 -0400786 return ret;
787}
788
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700789/*
790 * With an in-core object data in "map", rehash it to make sure the
791 * object name actually matches "sha1" to detect object corruption.
792 * With "map" == NULL, try reading the object named with "sha1" using
793 * the streaming interface and rehash it to do the same.
794 */
brian m. carlson17e65452018-03-12 02:27:39 +0000795int check_object_signature(const struct object_id *oid, void *map,
796 unsigned long size, const char *type)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700797{
Patryk Obaraf070fac2018-01-28 01:13:13 +0100798 struct object_id real_oid;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700799 enum object_type obj_type;
800 struct git_istream *st;
brian m. carlson18e25882018-02-01 02:18:41 +0000801 git_hash_ctx c;
brian m. carlson1af64f72018-03-12 02:27:55 +0000802 char hdr[MAX_HEADER_LEN];
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700803 int hdrlen;
804
805 if (map) {
Patryk Obaraf070fac2018-01-28 01:13:13 +0100806 hash_object_file(map, size, type, &real_oid);
brian m. carlson17e65452018-03-12 02:27:39 +0000807 return oidcmp(oid, &real_oid) ? -1 : 0;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700808 }
809
brian m. carlsonef7b5192018-03-12 02:27:40 +0000810 st = open_istream(oid, &obj_type, &size, NULL);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700811 if (!st)
812 return -1;
813
814 /* Generate the header */
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800815 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(obj_type), size) + 1;
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700816
817 /* Sha1.. */
brian m. carlson18e25882018-02-01 02:18:41 +0000818 the_hash_algo->init_fn(&c);
819 the_hash_algo->update_fn(&c, hdr, hdrlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700820 for (;;) {
821 char buf[1024 * 16];
822 ssize_t readlen = read_istream(st, buf, sizeof(buf));
823
Jeff Kingf54fac52013-03-25 16:17:17 -0400824 if (readlen < 0) {
825 close_istream(st);
826 return -1;
827 }
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700828 if (!readlen)
829 break;
brian m. carlson18e25882018-02-01 02:18:41 +0000830 the_hash_algo->update_fn(&c, buf, readlen);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700831 }
brian m. carlson18e25882018-02-01 02:18:41 +0000832 the_hash_algo->final_fn(real_oid.hash, &c);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700833 close_istream(st);
brian m. carlson17e65452018-03-12 02:27:39 +0000834 return oidcmp(oid, &real_oid) ? -1 : 0;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700835}
836
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700837int git_open_cloexec(const char *name, int flags)
Linus Torvalds44d1c192008-06-14 11:32:37 -0700838{
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700839 int fd;
840 static int o_cloexec = O_CLOEXEC;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700841
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700842 fd = open(name, flags | o_cloexec);
843 if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
Lars Schneidercd66ada2016-10-24 20:02:59 +0200844 /* Try again w/o O_CLOEXEC: the kernel might not support it */
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700845 o_cloexec &= ~O_CLOEXEC;
846 fd = open(name, flags | o_cloexec);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700847 }
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700848
Eric Wong9fb94952017-07-15 18:55:40 +0000849#if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700850 {
851 static int fd_cloexec = FD_CLOEXEC;
852
853 if (!o_cloexec && 0 <= fd && fd_cloexec) {
854 /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
Eric Wong9fb94952017-07-15 18:55:40 +0000855 int flags = fcntl(fd, F_GETFD);
856 if (fcntl(fd, F_SETFD, flags | fd_cloexec))
Junio C Hamano1e3001a2016-10-31 10:41:41 -0700857 fd_cloexec = 0;
858 }
859 }
860#endif
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700861 return fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700862}
863
Jeff King771e7d52017-01-13 12:54:39 -0500864/*
865 * Find "sha1" as a loose object in the local repository or in an alternate.
866 * Returns 0 on success, negative on failure.
867 *
868 * The "path" out-parameter will give the path of the object we found (if any).
869 * Note that it may point to static storage and is only valid until another
870 * call to sha1_file_name(), etc.
871 */
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100872static int stat_sha1_file(struct repository *r, const unsigned char *sha1,
873 struct stat *st, const char **path)
Jeff King052fe5e2013-07-12 02:30:48 -0400874{
Jeff King052fe5e2013-07-12 02:30:48 -0400875 struct alternate_object_database *alt;
Christian Couderea657732018-01-17 18:54:54 +0100876 static struct strbuf buf = STRBUF_INIT;
Jeff King052fe5e2013-07-12 02:30:48 -0400877
Christian Couderea657732018-01-17 18:54:54 +0100878 strbuf_reset(&buf);
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100879 sha1_file_name(r, &buf, sha1);
Christian Couderea657732018-01-17 18:54:54 +0100880 *path = buf.buf;
881
Jeff King771e7d52017-01-13 12:54:39 -0500882 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -0400883 return 0;
884
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100885 prepare_alt_odb(r);
Jeff King052fe5e2013-07-12 02:30:48 -0400886 errno = ENOENT;
Stefan Bellerd2607fa2018-03-23 18:21:17 +0100887 for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
Jeff King771e7d52017-01-13 12:54:39 -0500888 *path = alt_sha1_path(alt, sha1);
889 if (!lstat(*path, st))
Jeff King052fe5e2013-07-12 02:30:48 -0400890 return 0;
891 }
892
893 return -1;
894}
895
Jeff King771e7d52017-01-13 12:54:39 -0500896/*
897 * Like stat_sha1_file(), but actually open the object and return the
898 * descriptor. See the caveats on the "path" parameter above.
899 */
Stefan Bellerec7283e2018-03-23 18:21:18 +0100900static int open_sha1_file(struct repository *r,
901 const unsigned char *sha1, const char **path)
Linus Torvalds44d1c192008-06-14 11:32:37 -0700902{
903 int fd;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700904 struct alternate_object_database *alt;
Jeff Kingd6c8a052014-05-15 04:54:06 -0400905 int most_interesting_errno;
Christian Couderea657732018-01-17 18:54:54 +0100906 static struct strbuf buf = STRBUF_INIT;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700907
Christian Couderea657732018-01-17 18:54:54 +0100908 strbuf_reset(&buf);
Stefan Bellerec7283e2018-03-23 18:21:18 +0100909 sha1_file_name(r, &buf, sha1);
Christian Couderea657732018-01-17 18:54:54 +0100910 *path = buf.buf;
911
Jeff King771e7d52017-01-13 12:54:39 -0500912 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700913 if (fd >= 0)
914 return fd;
Jeff Kingd6c8a052014-05-15 04:54:06 -0400915 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700916
Stefan Bellerec7283e2018-03-23 18:21:18 +0100917 prepare_alt_odb(r);
918 for (alt = r->objects->alt_odb_list; alt; alt = alt->next) {
Jeff King771e7d52017-01-13 12:54:39 -0500919 *path = alt_sha1_path(alt, sha1);
920 fd = git_open(*path);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700921 if (fd >= 0)
922 return fd;
Jeff Kingd6c8a052014-05-15 04:54:06 -0400923 if (most_interesting_errno == ENOENT)
924 most_interesting_errno = errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700925 }
Jeff Kingd6c8a052014-05-15 04:54:06 -0400926 errno = most_interesting_errno;
Linus Torvalds44d1c192008-06-14 11:32:37 -0700927 return -1;
928}
929
Jeff Kingf6371f92017-01-13 12:58:16 -0500930/*
931 * Map the loose object at "path" if it is not NULL, or the path found by
932 * searching for a loose object named "sha1".
933 */
Jonathan Nieder1fea63e2018-03-23 18:21:19 +0100934static void *map_sha1_file_1(struct repository *r, const char *path,
935 const unsigned char *sha1, unsigned long *size)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700936{
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700937 void *map;
Linus Torvalds144bde72005-04-23 11:09:32 -0700938 int fd;
Junio C Hamanoace15342005-05-07 00:38:04 -0700939
Jeff Kingf6371f92017-01-13 12:58:16 -0500940 if (path)
941 fd = git_open(path);
942 else
Jonathan Nieder1fea63e2018-03-23 18:21:19 +0100943 fd = open_sha1_file(r, sha1, &path);
Linus Torvalds44d1c192008-06-14 11:32:37 -0700944 map = NULL;
945 if (fd >= 0) {
946 struct stat st;
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700947
Linus Torvalds44d1c192008-06-14 11:32:37 -0700948 if (!fstat(fd, &st)) {
949 *size = xsize_t(st.st_size);
Matthieu Moy33e42de2012-02-06 17:24:52 +0100950 if (!*size) {
951 /* mmap() is forbidden on empty files */
Jeff King771e7d52017-01-13 12:54:39 -0500952 error("object file %s is empty", path);
Matthieu Moy33e42de2012-02-06 17:24:52 +0100953 return NULL;
954 }
Linus Torvalds44d1c192008-06-14 11:32:37 -0700955 map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds144bde72005-04-23 11:09:32 -0700956 }
Linus Torvalds44d1c192008-06-14 11:32:37 -0700957 close(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700958 }
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700959 return map;
960}
961
Stefan Bellerbd27f502018-03-23 18:21:20 +0100962void *map_sha1_file(struct repository *r,
963 const unsigned char *sha1, unsigned long *size)
Jeff Kingf6371f92017-01-13 12:58:16 -0500964{
Stefan Bellerbd27f502018-03-23 18:21:20 +0100965 return map_sha1_file_1(r, NULL, sha1, size);
Jeff Kingf6371f92017-01-13 12:58:16 -0500966}
967
Junio C Hamanod21f8422016-09-25 21:29:04 -0700968static int unpack_sha1_short_header(git_zstream *stream,
969 unsigned char *map, unsigned long mapsize,
970 void *buffer, unsigned long bufsiz)
Linus Torvaldsc4483572005-06-01 17:54:59 -0700971{
972 /* Get the data stream */
973 memset(stream, 0, sizeof(*stream));
974 stream->next_in = map;
975 stream->avail_in = mapsize;
976 stream->next_out = buffer;
Linus Torvalds93821bd2006-07-11 12:48:08 -0700977 stream->avail_out = bufsiz;
Linus Torvaldsc4483572005-06-01 17:54:59 -0700978
Linus Torvalds39c68542009-01-07 19:54:47 -0800979 git_inflate_init(stream);
Junio C Hamanocc5c54e2011-06-08 11:29:01 -0700980 return git_inflate(stream, 0);
Linus Torvaldsc4483572005-06-01 17:54:59 -0700981}
982
Junio C Hamanod21f8422016-09-25 21:29:04 -0700983int unpack_sha1_header(git_zstream *stream,
984 unsigned char *map, unsigned long mapsize,
985 void *buffer, unsigned long bufsiz)
986{
987 int status = unpack_sha1_short_header(stream, map, mapsize,
988 buffer, bufsiz);
989
990 if (status < Z_OK)
991 return status;
992
993 /* Make sure we have the terminating NUL */
994 if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
995 return -1;
996 return 0;
997}
998
Karthik Nayak46f03442015-05-03 19:59:59 +0530999static int unpack_sha1_header_to_strbuf(git_zstream *stream, unsigned char *map,
1000 unsigned long mapsize, void *buffer,
1001 unsigned long bufsiz, struct strbuf *header)
1002{
1003 int status;
1004
Junio C Hamanod21f8422016-09-25 21:29:04 -07001005 status = unpack_sha1_short_header(stream, map, mapsize, buffer, bufsiz);
1006 if (status < Z_OK)
1007 return -1;
Karthik Nayak46f03442015-05-03 19:59:59 +05301008
1009 /*
1010 * Check if entire header is unpacked in the first iteration.
1011 */
1012 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1013 return 0;
1014
1015 /*
1016 * buffer[0..bufsiz] was not large enough. Copy the partial
1017 * result out to header, and then append the result of further
1018 * reading the stream.
1019 */
1020 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1021 stream->next_out = buffer;
1022 stream->avail_out = bufsiz;
1023
1024 do {
1025 status = git_inflate(stream, 0);
1026 strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
1027 if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
1028 return 0;
1029 stream->next_out = buffer;
1030 stream->avail_out = bufsiz;
1031 } while (status != Z_STREAM_END);
1032 return -1;
1033}
1034
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001035static void *unpack_sha1_rest(git_zstream *stream, void *buffer, unsigned long size, const unsigned char *sha1)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001036{
Linus Torvalds5180cac2005-06-02 07:57:25 -07001037 int bytes = strlen(buffer) + 1;
Ilari Liusvaara3aee68a2010-01-26 20:24:14 +02001038 unsigned char *buf = xmallocz(size);
Linus Torvalds93821bd2006-07-11 12:48:08 -07001039 unsigned long n;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001040 int status = Z_OK;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001041
Linus Torvalds93821bd2006-07-11 12:48:08 -07001042 n = stream->total_out - bytes;
1043 if (n > size)
1044 n = size;
1045 memcpy(buf, (char *) buffer + bytes, n);
1046 bytes = n;
Linus Torvalds456cdf62007-03-19 22:49:53 -07001047 if (bytes <= size) {
1048 /*
1049 * The above condition must be (bytes <= size), not
1050 * (bytes < size). In other words, even though we
Junio C Hamanoccf5ace2011-05-15 12:16:03 -07001051 * expect no more output and set avail_out to zero,
Linus Torvalds456cdf62007-03-19 22:49:53 -07001052 * the input zlib stream may have bytes that express
1053 * "this concludes the stream", and we *do* want to
1054 * eat that input.
1055 *
1056 * Otherwise we would not be able to test that we
1057 * consumed all the input to reach the expected size;
1058 * we also want to check that zlib tells us that all
1059 * went well with status == Z_STREAM_END at the end.
1060 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001061 stream->next_out = buf + bytes;
1062 stream->avail_out = size - bytes;
Junio C Hamano7efbff72007-03-05 00:21:37 -08001063 while (status == Z_OK)
Linus Torvalds39c68542009-01-07 19:54:47 -08001064 status = git_inflate(stream, Z_FINISH);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001065 }
Linus Torvalds456cdf62007-03-19 22:49:53 -07001066 if (status == Z_STREAM_END && !stream->avail_in) {
Linus Torvalds39c68542009-01-07 19:54:47 -08001067 git_inflate_end(stream);
Junio C Hamano7efbff72007-03-05 00:21:37 -08001068 return buf;
1069 }
1070
1071 if (status < 0)
1072 error("corrupt loose object '%s'", sha1_to_hex(sha1));
1073 else if (stream->avail_in)
1074 error("garbage at end of loose object '%s'",
1075 sha1_to_hex(sha1));
1076 free(buf);
1077 return NULL;
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001078}
1079
Linus Torvalds5180cac2005-06-02 07:57:25 -07001080/*
1081 * We used to just use "sscanf()", but that's actually way
1082 * too permissive for what we want to check. So do an anal
1083 * object header parse by hand.
1084 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301085static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,
1086 unsigned int flags)
Linus Torvalds5180cac2005-06-02 07:57:25 -07001087{
Karthik Nayak46f03442015-05-03 19:59:59 +05301088 const char *type_buf = hdr;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001089 unsigned long size;
Karthik Nayak46f03442015-05-03 19:59:59 +05301090 int type, type_len = 0;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001091
1092 /*
Karthik Nayak46f03442015-05-03 19:59:59 +05301093 * The type can be of any size but is followed by
Nicolas Pitre21666f12007-02-26 14:55:59 -05001094 * a space.
Linus Torvalds5180cac2005-06-02 07:57:25 -07001095 */
Linus Torvalds5180cac2005-06-02 07:57:25 -07001096 for (;;) {
1097 char c = *hdr++;
Junio C Hamanod21f8422016-09-25 21:29:04 -07001098 if (!c)
1099 return -1;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001100 if (c == ' ')
1101 break;
Karthik Nayak46f03442015-05-03 19:59:59 +05301102 type_len++;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001103 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301104
1105 type = type_from_string_gently(type_buf, type_len, 1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001106 if (oi->type_name)
1107 strbuf_add(oi->type_name, type_buf, type_len);
Karthik Nayak46f03442015-05-03 19:59:59 +05301108 /*
1109 * Set type to 0 if its an unknown object and
Ville Skyttä2e3a16b2016-08-09 11:53:38 +03001110 * we're obtaining the type using '--allow-unknown-type'
Karthik Nayak46f03442015-05-03 19:59:59 +05301111 * option.
1112 */
Jonathan Tan19fc5e82017-06-21 17:40:18 -07001113 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
Karthik Nayak46f03442015-05-03 19:59:59 +05301114 type = 0;
1115 else if (type < 0)
1116 die("invalid object type");
1117 if (oi->typep)
1118 *oi->typep = type;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001119
1120 /*
1121 * The length must follow immediately, and be in canonical
1122 * decimal format (ie "010" is not valid).
1123 */
1124 size = *hdr++ - '0';
1125 if (size > 9)
1126 return -1;
1127 if (size) {
1128 for (;;) {
1129 unsigned long c = *hdr - '0';
1130 if (c > 9)
1131 break;
1132 hdr++;
1133 size = size * 10 + c;
1134 }
1135 }
Karthik Nayak46f03442015-05-03 19:59:59 +05301136
1137 if (oi->sizep)
1138 *oi->sizep = size;
Linus Torvalds5180cac2005-06-02 07:57:25 -07001139
1140 /*
1141 * The length must be followed by a zero byte
1142 */
Karthik Nayak46f03442015-05-03 19:59:59 +05301143 return *hdr ? -1 : type;
1144}
1145
1146int parse_sha1_header(const char *hdr, unsigned long *sizep)
1147{
Jeff King27b5c1a2016-08-11 05:24:35 -04001148 struct object_info oi = OBJECT_INFO_INIT;
Karthik Nayak46f03442015-05-03 19:59:59 +05301149
1150 oi.sizep = sizep;
Jonathan Tan1f0c0d32017-06-21 17:40:19 -07001151 return parse_sha1_header_extended(hdr, &oi, 0);
Linus Torvalds5180cac2005-06-02 07:57:25 -07001152}
1153
Jonathan Nieder4a7c05f2018-03-23 18:21:21 +01001154static int sha1_loose_object_info(struct repository *r,
1155 const unsigned char *sha1,
1156 struct object_info *oi, int flags)
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001157{
Karthik Nayak46f03442015-05-03 19:59:59 +05301158 int status = 0;
1159 unsigned long mapsize;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001160 void *map;
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001161 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00001162 char hdr[MAX_HEADER_LEN];
Karthik Nayak46f03442015-05-03 19:59:59 +05301163 struct strbuf hdrbuf = STRBUF_INIT;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001164 unsigned long size_scratch;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001165
Jeff King5d642e72013-12-21 09:24:20 -05001166 if (oi->delta_base_sha1)
1167 hashclr(oi->delta_base_sha1);
1168
Jeff King052fe5e2013-07-12 02:30:48 -04001169 /*
1170 * If we don't care about type or size, then we don't
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001171 * need to look inside the object at all. Note that we
1172 * do not optimize out the stat call, even if the
1173 * caller doesn't care about the disk-size, since our
1174 * return value implicitly indicates whether the
1175 * object even exists.
Jeff King052fe5e2013-07-12 02:30:48 -04001176 */
Brandon Williams6ca32f42018-02-14 10:59:23 -08001177 if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
Jeff King771e7d52017-01-13 12:54:39 -05001178 const char *path;
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001179 struct stat st;
Jonathan Nieder4a7c05f2018-03-23 18:21:21 +01001180 if (stat_sha1_file(r, sha1, &st, &path) < 0)
Junio C Hamano4ef8d1d2013-11-06 10:00:57 -08001181 return -1;
1182 if (oi->disk_sizep)
Jeff King23c339c2013-07-12 02:37:53 -04001183 *oi->disk_sizep = st.st_size;
Jeff King052fe5e2013-07-12 02:30:48 -04001184 return 0;
1185 }
1186
Jonathan Nieder4a7c05f2018-03-23 18:21:21 +01001187 map = map_sha1_file(r, sha1, &mapsize);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001188 if (!map)
Thomas Rastdbea72a2013-05-30 22:00:22 +02001189 return -1;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001190
1191 if (!oi->sizep)
1192 oi->sizep = &size_scratch;
1193
Jeff King23c339c2013-07-12 02:37:53 -04001194 if (oi->disk_sizep)
1195 *oi->disk_sizep = mapsize;
Jonathan Tan19fc5e82017-06-21 17:40:18 -07001196 if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
Karthik Nayak46f03442015-05-03 19:59:59 +05301197 if (unpack_sha1_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
1198 status = error("unable to unpack %s header with --allow-unknown-type",
1199 sha1_to_hex(sha1));
1200 } else if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
Junio C Hamano36e4d742005-06-27 03:34:06 -07001201 status = error("unable to unpack %s header",
1202 sha1_to_hex(sha1));
Karthik Nayak46f03442015-05-03 19:59:59 +05301203 if (status < 0)
1204 ; /* Do nothing */
1205 else if (hdrbuf.len) {
1206 if ((status = parse_sha1_header_extended(hdrbuf.buf, oi, flags)) < 0)
1207 status = error("unable to parse %s header with --allow-unknown-type",
1208 sha1_to_hex(sha1));
1209 } else if ((status = parse_sha1_header_extended(hdr, oi, flags)) < 0)
Junio C Hamano36e4d742005-06-27 03:34:06 -07001210 status = error("unable to parse %s header", sha1_to_hex(sha1));
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001211
Jeff Kingb3ea7dd2017-10-05 01:59:52 -04001212 if (status >= 0 && oi->contentp) {
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001213 *oi->contentp = unpack_sha1_rest(&stream, hdr,
1214 *oi->sizep, sha1);
Jeff Kingb3ea7dd2017-10-05 01:59:52 -04001215 if (!*oi->contentp) {
1216 git_inflate_end(&stream);
1217 status = -1;
1218 }
1219 } else
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001220 git_inflate_end(&stream);
1221
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001222 munmap(map, mapsize);
Karthik Nayak46f03442015-05-03 19:59:59 +05301223 if (status && oi->typep)
Jeff King23c339c2013-07-12 02:37:53 -04001224 *oi->typep = status;
Jonathan Tanc84a1f32017-06-21 17:40:21 -07001225 if (oi->sizep == &size_scratch)
1226 oi->sizep = NULL;
Karthik Nayak46f03442015-05-03 19:59:59 +05301227 strbuf_release(&hdrbuf);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001228 oi->whence = OI_LOOSE;
Jeff King93cff9a2017-04-01 04:05:21 -04001229 return (status < 0) ? status : 0;
Junio C Hamano65c2e0c2005-06-02 15:20:54 -07001230}
1231
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001232int fetch_if_missing = 1;
1233
Stefan Beller9d983542018-04-25 11:21:06 -07001234int oid_object_info_extended(struct repository *r, const struct object_id *oid,
1235 struct object_info *oi, unsigned flags)
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001236{
Jonathan Tancd585e22017-06-21 17:40:23 -07001237 static struct object_info blank_oi = OBJECT_INFO_INIT;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001238 struct pack_entry e;
Jeff King5b086402013-07-12 02:34:57 -04001239 int rtype;
brian m. carlsonb383a132018-03-12 02:27:54 +00001240 const struct object_id *real = oid;
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001241 int already_retried = 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001242
brian m. carlsonb383a132018-03-12 02:27:54 +00001243 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
Stefan Beller9d983542018-04-25 11:21:06 -07001244 real = lookup_replace_object(r, oid);
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001245
brian m. carlsonb383a132018-03-12 02:27:54 +00001246 if (is_null_oid(real))
Jeff King87b5e232017-11-21 18:17:39 -05001247 return -1;
1248
Jonathan Tancd585e22017-06-21 17:40:23 -07001249 if (!oi)
1250 oi = &blank_oi;
1251
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001252 if (!(flags & OBJECT_INFO_SKIP_CACHED)) {
brian m. carlsonb383a132018-03-12 02:27:54 +00001253 struct cached_object *co = find_cached_object(real->hash);
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001254 if (co) {
1255 if (oi->typep)
1256 *(oi->typep) = co->type;
1257 if (oi->sizep)
1258 *(oi->sizep) = co->size;
1259 if (oi->disk_sizep)
1260 *(oi->disk_sizep) = 0;
1261 if (oi->delta_base_sha1)
1262 hashclr(oi->delta_base_sha1);
Brandon Williams6ca32f42018-02-14 10:59:23 -08001263 if (oi->type_name)
Brandon Williamsdebca9d2018-02-14 10:59:24 -08001264 strbuf_addstr(oi->type_name, type_name(co->type));
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001265 if (oi->contentp)
1266 *oi->contentp = xmemdupz(co->buf, co->size);
1267 oi->whence = OI_CACHED;
1268 return 0;
1269 }
Nguyễn Thái Ngọc Duyc4d99862011-02-05 21:03:02 +07001270 }
1271
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001272 while (1) {
Stefan Beller9d983542018-04-25 11:21:06 -07001273 if (find_pack_entry(r, real->hash, &e))
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001274 break;
1275
Takuto Ikuta024aa462018-03-14 15:32:42 +09001276 if (flags & OBJECT_INFO_IGNORE_LOOSE)
1277 return -1;
1278
Steven Grimmddd63e62008-08-05 13:08:41 -07001279 /* Most likely it's a loose object. */
Stefan Beller9d983542018-04-25 11:21:06 -07001280 if (!sha1_loose_object_info(r, real->hash, oi, flags))
Jeff King5b086402013-07-12 02:34:57 -04001281 return 0;
Steven Grimmddd63e62008-08-05 13:08:41 -07001282
1283 /* Not a loose object; someone else may have just packed it. */
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001284 if (!(flags & OBJECT_INFO_QUICK)) {
Stefan Beller9d983542018-04-25 11:21:06 -07001285 reprepare_packed_git(r);
1286 if (find_pack_entry(r, real->hash, &e))
Jonathan Tan2b7750c2018-03-13 08:30:29 -07001287 break;
1288 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001289
1290 /* Check if it is a missing object */
1291 if (fetch_if_missing && repository_format_partial_clone &&
Stefan Beller9d983542018-04-25 11:21:06 -07001292 !already_retried && r == the_repository) {
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001293 /*
Stefan Beller9d983542018-04-25 11:21:06 -07001294 * TODO Investigate having fetch_object() return
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001295 * TODO error/success and stopping the music here.
Stefan Beller9d983542018-04-25 11:21:06 -07001296 * TODO Pass a repository struct through fetch_object,
1297 * such that arbitrary repositories work.
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001298 */
brian m. carlsonb383a132018-03-12 02:27:54 +00001299 fetch_object(repository_format_partial_clone, real->hash);
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001300 already_retried = 1;
1301 continue;
Jonathan Tandfdd4af2017-06-21 17:40:22 -07001302 }
Jonathan Tan8b4c0102017-12-08 15:27:14 +00001303
1304 return -1;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001305 }
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001306
Jonathan Tancd585e22017-06-21 17:40:23 -07001307 if (oi == &blank_oi)
1308 /*
1309 * We know that the caller doesn't actually need the
1310 * information below, so return early.
1311 */
1312 return 0;
Stefan Beller9d983542018-04-25 11:21:06 -07001313 rtype = packed_object_info(r, e.p, e.offset, oi);
Jeff King412916e2013-07-12 02:32:25 -04001314 if (rtype < 0) {
brian m. carlsonb383a132018-03-12 02:27:54 +00001315 mark_bad_packed_object(e.p, real->hash);
Stefan Beller9d983542018-04-25 11:21:06 -07001316 return oid_object_info_extended(r, real, oi, 0);
Jonathan Tan3ab0fb02017-08-11 13:36:14 -07001317 } else if (oi->whence == OI_PACKED) {
Junio C Hamano9a490592011-05-12 15:51:38 -07001318 oi->u.packed.offset = e.offset;
1319 oi->u.packed.pack = e.p;
1320 oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
1321 rtype == OBJ_OFS_DELTA);
Nicolas Pitre3d77d872008-10-29 19:02:47 -04001322 }
1323
Jeff King5b086402013-07-12 02:34:57 -04001324 return 0;
Johannes Schindelinf0df4ed2006-11-28 00:18:55 +01001325}
1326
Christian Couder3fc0dca2013-10-27 00:34:30 +02001327/* returns enum object_type or negative */
Stefan Beller9d983542018-04-25 11:21:06 -07001328int oid_object_info(struct repository *r,
1329 const struct object_id *oid,
1330 unsigned long *sizep)
Junio C Hamano9a490592011-05-12 15:51:38 -07001331{
Jeff King5b086402013-07-12 02:34:57 -04001332 enum object_type type;
Jeff King27b5c1a2016-08-11 05:24:35 -04001333 struct object_info oi = OBJECT_INFO_INIT;
Junio C Hamano9a490592011-05-12 15:51:38 -07001334
Jeff King5b086402013-07-12 02:34:57 -04001335 oi.typep = &type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001336 oi.sizep = sizep;
Stefan Beller9d983542018-04-25 11:21:06 -07001337 if (oid_object_info_extended(r, oid, &oi,
1338 OBJECT_INFO_LOOKUP_REPLACE) < 0)
Jeff King5b086402013-07-12 02:34:57 -04001339 return -1;
1340 return type;
Junio C Hamano9a490592011-05-12 15:51:38 -07001341}
1342
Jonathan Tanf1d81302017-08-18 15:20:30 -07001343static void *read_object(const unsigned char *sha1, enum object_type *type,
1344 unsigned long *size)
1345{
brian m. carlson7984f232018-03-12 02:27:45 +00001346 struct object_id oid;
Jonathan Tanf1d81302017-08-18 15:20:30 -07001347 struct object_info oi = OBJECT_INFO_INIT;
1348 void *content;
1349 oi.typep = type;
1350 oi.sizep = size;
1351 oi.contentp = &content;
1352
brian m. carlson7984f232018-03-12 02:27:45 +00001353 hashcpy(oid.hash, sha1);
1354
Stefan Beller7ecd8692018-04-25 11:20:58 -07001355 if (oid_object_info_extended(the_repository, &oid, &oi, 0) < 0)
Jonathan Tanf1d81302017-08-18 15:20:30 -07001356 return NULL;
1357 return content;
1358}
1359
Patryk Obara829e5c32018-01-28 01:13:11 +01001360int pretend_object_file(void *buf, unsigned long len, enum object_type type,
1361 struct object_id *oid)
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001362{
1363 struct cached_object *co;
1364
Junio C Hamano169c9c02018-03-06 14:54:07 -08001365 hash_object_file(buf, len, type_name(type), oid);
Patryk Obara829e5c32018-01-28 01:13:11 +01001366 if (has_sha1_file(oid->hash) || find_cached_object(oid->hash))
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001367 return 0;
Dmitry S. Dolzhenkoc7353962014-03-04 02:32:02 +04001368 ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001369 co = &cached_objects[cached_object_nr++];
1370 co->size = len;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001371 co->type = type;
Junio C Hamanoefa13f72007-02-15 17:02:06 -08001372 co->buf = xmalloc(len);
1373 memcpy(co->buf, buf, len);
Patryk Obara829e5c32018-01-28 01:13:11 +01001374 hashcpy(co->sha1, oid->hash);
Junio C Hamanod66b37b2007-02-04 21:42:38 -08001375 return 0;
1376}
1377
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001378/*
1379 * This function dies on corrupt objects; the callers who want to
1380 * deal with them should arrange to call read_object() and give error
1381 * messages themselves.
1382 */
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001383void *read_object_file_extended(const struct object_id *oid,
1384 enum object_type *type,
1385 unsigned long *size,
1386 int lookup_replace)
Nicolas Pitreac939102008-07-14 21:46:48 -04001387{
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001388 void *data;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001389 const struct packed_git *p;
Jeff King771e7d52017-01-13 12:54:39 -05001390 const char *path;
1391 struct stat st;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -07001392 const struct object_id *repl = lookup_replace ?
1393 lookup_replace_object(the_repository, oid) : oid;
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001394
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001395 errno = 0;
brian m. carlsonb383a132018-03-12 02:27:54 +00001396 data = read_object(repl->hash, type, size);
Junio C Hamano4bbf5a22011-05-15 12:54:52 -07001397 if (data)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001398 return data;
Christian Couder68095572009-01-23 10:06:53 +01001399
Björn Steinbrink25f3af32011-01-20 21:12:20 +01001400 if (errno && errno != ENOENT)
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001401 die_errno("failed to read object %s", oid_to_hex(oid));
Junio C Hamano3ba7a062010-10-28 11:13:06 -07001402
Christian Couder68095572009-01-23 10:06:53 +01001403 /* die if we replaced an object with one that does not exist */
brian m. carlsonb383a132018-03-12 02:27:54 +00001404 if (repl != oid)
Christian Couder68095572009-01-23 10:06:53 +01001405 die("replacement %s not found for %s",
brian m. carlsonb383a132018-03-12 02:27:54 +00001406 oid_to_hex(repl), oid_to_hex(oid));
Christian Couder68095572009-01-23 10:06:53 +01001407
Junio C Hamanocf0b1792018-04-11 13:09:55 +09001408 if (!stat_sha1_file(the_repository, repl->hash, &st, &path))
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001409 die("loose object %s (stored in %s) is corrupt",
brian m. carlsonb383a132018-03-12 02:27:54 +00001410 oid_to_hex(repl), path);
Christian Couder68095572009-01-23 10:06:53 +01001411
brian m. carlsonb383a132018-03-12 02:27:54 +00001412 if ((p = has_packed_and_bad(repl->hash)) != NULL)
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001413 die("packed object %s (stored in %s) is corrupt",
brian m. carlsonb383a132018-03-12 02:27:54 +00001414 oid_to_hex(repl), p->pack_name);
Christian Couderf5552ae2009-01-23 10:07:01 +01001415
Junio C Hamanob6c4cec2010-10-28 11:13:06 -07001416 return NULL;
Nicolas Pitreac939102008-07-14 21:46:48 -04001417}
1418
brian m. carlson02f05472018-03-12 02:27:52 +00001419void *read_object_with_reference(const struct object_id *oid,
Nicolas Pitre21666f12007-02-26 14:55:59 -05001420 const char *required_type_name,
Junio C Hamano40469ee2005-04-28 16:42:27 -07001421 unsigned long *size,
brian m. carlson02f05472018-03-12 02:27:52 +00001422 struct object_id *actual_oid_return)
Junio C Hamanof4913f92005-04-20 18:06:49 -07001423{
Nicolas Pitre21666f12007-02-26 14:55:59 -05001424 enum object_type type, required_type;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001425 void *buffer;
1426 unsigned long isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001427 struct object_id actual_oid;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001428
Nicolas Pitre21666f12007-02-26 14:55:59 -05001429 required_type = type_from_string(required_type_name);
brian m. carlson02f05472018-03-12 02:27:52 +00001430 oidcpy(&actual_oid, oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001431 while (1) {
1432 int ref_length = -1;
1433 const char *ref_type = NULL;
Junio C Hamanof4913f92005-04-20 18:06:49 -07001434
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001435 buffer = read_object_file(&actual_oid, &type, &isize);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001436 if (!buffer)
1437 return NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -05001438 if (type == required_type) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001439 *size = isize;
brian m. carlson02f05472018-03-12 02:27:52 +00001440 if (actual_oid_return)
1441 oidcpy(actual_oid_return, &actual_oid);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001442 return buffer;
1443 }
1444 /* Handle references */
Nicolas Pitre21666f12007-02-26 14:55:59 -05001445 else if (type == OBJ_COMMIT)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001446 ref_type = "tree ";
Nicolas Pitre21666f12007-02-26 14:55:59 -05001447 else if (type == OBJ_TAG)
Junio C Hamano40469ee2005-04-28 16:42:27 -07001448 ref_type = "object ";
1449 else {
1450 free(buffer);
1451 return NULL;
1452 }
1453 ref_length = strlen(ref_type);
1454
brian m. carlson02f05472018-03-12 02:27:52 +00001455 if (ref_length + GIT_SHA1_HEXSZ > isize ||
Martin Koegler50974ec2008-02-18 21:47:52 +01001456 memcmp(buffer, ref_type, ref_length) ||
brian m. carlson02f05472018-03-12 02:27:52 +00001457 get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
Junio C Hamano40469ee2005-04-28 16:42:27 -07001458 free(buffer);
1459 return NULL;
1460 }
Sergey Vlasov1cf58e72005-08-08 22:44:43 +04001461 free(buffer);
Junio C Hamano40469ee2005-04-28 16:42:27 -07001462 /* Now we have the ID of the referred-to object in
brian m. carlson02f05472018-03-12 02:27:52 +00001463 * actual_oid. Check again. */
Junio C Hamanof4913f92005-04-20 18:06:49 -07001464 }
Junio C Hamanof4913f92005-04-20 18:06:49 -07001465}
1466
Patryk Obaraa09c9852018-01-28 01:13:19 +01001467static void write_object_file_prepare(const void *buf, unsigned long len,
1468 const char *type, struct object_id *oid,
1469 char *hdr, int *hdrlen)
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001470{
brian m. carlson18e25882018-02-01 02:18:41 +00001471 git_hash_ctx c;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001472
1473 /* Generate the header */
Jeff Kingef1286d2015-09-24 17:06:42 -04001474 *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001475
1476 /* Sha1.. */
brian m. carlson18e25882018-02-01 02:18:41 +00001477 the_hash_algo->init_fn(&c);
1478 the_hash_algo->update_fn(&c, hdr, *hdrlen);
1479 the_hash_algo->update_fn(&c, buf, len);
Junio C Hamano0fd90da2018-02-15 14:55:47 -08001480 the_hash_algo->final_fn(oid->hash, &c);
Junio C Hamanod410c0f2005-06-27 19:03:13 -07001481}
1482
Linus Torvalds230f1322005-10-08 15:54:01 -07001483/*
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001484 * Move the just written object into its final resting place.
Linus Torvalds230f1322005-10-08 15:54:01 -07001485 */
Junio C Hamanocb5add52015-08-07 14:40:24 -07001486int finalize_object_file(const char *tmpfile, const char *filename)
Linus Torvalds230f1322005-10-08 15:54:01 -07001487{
Thomas Raste32c0a92008-09-19 00:24:46 +02001488 int ret = 0;
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001489
Johannes Schindelin348df162009-04-28 00:32:25 +02001490 if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001491 goto try_rename;
1492 else if (link(tmpfile, filename))
Thomas Raste32c0a92008-09-19 00:24:46 +02001493 ret = errno;
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001494
1495 /*
1496 * Coda hack - coda doesn't like cross-directory links,
1497 * so we fall back to a rename, which will mean that it
1498 * won't be able to check collisions, but that's not a
1499 * big deal.
1500 *
1501 * The same holds for FAT formatted media.
1502 *
Junio C Hamano3be1f182009-03-27 23:14:39 -07001503 * When this succeeds, we just return. We have nothing
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001504 * left to unlink.
1505 */
1506 if (ret && ret != EEXIST) {
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +02001507 try_rename:
Linus Torvalds7ebb6fc2005-10-26 10:27:36 -07001508 if (!rename(tmpfile, filename))
Junio C Hamano3be1f182009-03-27 23:14:39 -07001509 goto out;
Johannes Schindelin9e48b382005-10-26 01:41:20 +02001510 ret = errno;
Linus Torvalds230f1322005-10-08 15:54:01 -07001511 }
Alex Riesen691f1a22009-04-29 23:22:56 +02001512 unlink_or_warn(tmpfile);
Linus Torvalds230f1322005-10-08 15:54:01 -07001513 if (ret) {
1514 if (ret != EEXIST) {
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001515 return error_errno("unable to write sha1 filename %s", filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001516 }
1517 /* FIXME!!! Collision check here ? */
1518 }
1519
Junio C Hamano3be1f182009-03-27 23:14:39 -07001520out:
Matthieu Moy5256b002010-02-22 23:32:16 +01001521 if (adjust_shared_perm(filename))
Junio C Hamano5a688fe2009-03-25 16:19:36 -07001522 return error("unable to set permission to '%s'", filename);
Linus Torvalds230f1322005-10-08 15:54:01 -07001523 return 0;
1524}
1525
Linus Torvalds4d548152006-05-24 08:30:54 -07001526static int write_buffer(int fd, const void *buf, size_t len)
1527{
Linus Torvaldsd34cf192007-01-11 20:23:00 -08001528 if (write_in_full(fd, buf, len) < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001529 return error_errno("file write error");
Linus Torvalds4d548152006-05-24 08:30:54 -07001530 return 0;
1531}
1532
Patryk Obaraf070fac2018-01-28 01:13:13 +01001533int hash_object_file(const void *buf, unsigned long len, const char *type,
1534 struct object_id *oid)
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001535{
brian m. carlson1af64f72018-03-12 02:27:55 +00001536 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001537 int hdrlen = sizeof(hdr);
Patryk Obaraa09c9852018-01-28 01:13:19 +01001538 write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001539 return 0;
1540}
1541
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001542/* Finalize a file on disk, and close it. */
1543static void close_sha1_file(int fd)
1544{
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -07001545 if (fsync_object_files)
1546 fsync_or_die(fd, "sha1 file");
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001547 if (close(fd) != 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +02001548 die_errno("error when closing sha1 file");
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001549}
1550
Linus Torvalds5723fe72008-06-14 10:50:12 -07001551/* Size of directory component, including the ending '/' */
1552static inline int directory_size(const char *filename)
1553{
1554 const char *s = strrchr(filename, '/');
1555 if (!s)
1556 return 0;
1557 return s - filename + 1;
1558}
1559
1560/*
1561 * This creates a temporary file in the same directory as the final
1562 * 'filename'
1563 *
1564 * We want to avoid cross-directory filename renames, because those
1565 * can have problems on various filesystems (FAT, NFS, Coda).
1566 */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001567static int create_tmpfile(struct strbuf *tmp, const char *filename)
Linus Torvalds5723fe72008-06-14 10:50:12 -07001568{
1569 int fd, dirlen = directory_size(filename);
1570
Jeff Kingd4b3d112015-09-24 17:07:49 -04001571 strbuf_reset(tmp);
1572 strbuf_add(tmp, filename, dirlen);
1573 strbuf_addstr(tmp, "tmp_obj_XXXXXX");
1574 fd = git_mkstemp_mode(tmp->buf, 0444);
Joey Hesscbacbf42008-11-20 13:56:28 -05001575 if (fd < 0 && dirlen && errno == ENOENT) {
Jeff Kingd4b3d112015-09-24 17:07:49 -04001576 /*
1577 * Make sure the directory exists; note that the contents
1578 * of the buffer are undefined after mkstemp returns an
1579 * error, so we have to rewrite the whole buffer from
1580 * scratch.
1581 */
1582 strbuf_reset(tmp);
1583 strbuf_add(tmp, filename, dirlen - 1);
1584 if (mkdir(tmp->buf, 0777) && errno != EEXIST)
Johan Herlandb2476a62013-10-27 12:35:43 +01001585 return -1;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001586 if (adjust_shared_perm(tmp->buf))
Linus Torvalds5723fe72008-06-14 10:50:12 -07001587 return -1;
1588
1589 /* Try again */
Jeff Kingd4b3d112015-09-24 17:07:49 -04001590 strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
1591 fd = git_mkstemp_mode(tmp->buf, 0444);
Linus Torvalds5723fe72008-06-14 10:50:12 -07001592 }
1593 return fd;
1594}
1595
Patryk Obara3fc72812018-01-28 01:13:21 +01001596static int write_loose_object(const struct object_id *oid, char *hdr,
1597 int hdrlen, const void *buf, unsigned long len,
1598 time_t mtime)
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001599{
Jeff King915308b2009-01-29 00:56:34 -05001600 int fd, ret;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001601 unsigned char compressed[4096];
Junio C Hamanoef49a7a2011-06-10 11:52:15 -07001602 git_zstream stream;
brian m. carlson18e25882018-02-01 02:18:41 +00001603 git_hash_ctx c;
Patryk Obara3fc72812018-01-28 01:13:21 +01001604 struct object_id parano_oid;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001605 static struct strbuf tmp_file = STRBUF_INIT;
Christian Couderea657732018-01-17 18:54:54 +01001606 static struct strbuf filename = STRBUF_INIT;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001607
Christian Couderea657732018-01-17 18:54:54 +01001608 strbuf_reset(&filename);
Junio C Hamanocf0b1792018-04-11 13:09:55 +09001609 sha1_file_name(the_repository, &filename, oid->hash);
Christian Couderea657732018-01-17 18:54:54 +01001610
1611 fd = create_tmpfile(&tmp_file, filename.buf);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001612 if (fd < 0) {
Sam Vilain35243572008-11-14 20:19:34 +13001613 if (errno == EACCES)
Pete Wyckoff82247e92012-04-29 20:28:45 -04001614 return error("insufficient permission for adding an object to repository database %s", get_object_directory());
Petr Baudis916d0812006-11-09 13:52:05 +01001615 else
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001616 return error_errno("unable to create temporary file");
Linus Torvaldsaac17942005-05-03 11:46:16 -07001617 }
1618
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001619 /* Set it up */
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001620 git_deflate_init(&stream, zlib_compression_level);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001621 stream.next_out = compressed;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001622 stream.avail_out = sizeof(compressed);
brian m. carlson18e25882018-02-01 02:18:41 +00001623 the_hash_algo->init_fn(&c);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001624
1625 /* First header.. */
Nicolas Pitred65a16f2007-02-26 14:55:55 -05001626 stream.next_in = (unsigned char *)hdr;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001627 stream.avail_in = hdrlen;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001628 while (git_deflate(&stream, 0) == Z_OK)
1629 ; /* nothing */
brian m. carlson18e25882018-02-01 02:18:41 +00001630 the_hash_algo->update_fn(&c, hdr, hdrlen);
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001631
1632 /* Then the data itself.. */
Jeff Kingc00e6572010-04-01 20:03:18 -04001633 stream.next_in = (void *)buf;
Linus Torvaldsa44c9a52005-04-25 10:19:53 -07001634 stream.avail_in = len;
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001635 do {
Nicolas Pitre748af442010-02-21 15:48:06 -05001636 unsigned char *in0 = stream.next_in;
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001637 ret = git_deflate(&stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00001638 the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
Nicolas Pitre9892beb2010-02-20 23:27:31 -05001639 if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
1640 die("unable to write sha1 file");
1641 stream.next_out = compressed;
1642 stream.avail_out = sizeof(compressed);
1643 } while (ret == Z_OK);
1644
Linus Torvaldsac54c272007-03-20 11:38:34 -07001645 if (ret != Z_STREAM_END)
Patryk Obara3fc72812018-01-28 01:13:21 +01001646 die("unable to deflate new object %s (%d)", oid_to_hex(oid),
1647 ret);
Junio C Hamano55bb5c92011-06-10 10:55:10 -07001648 ret = git_deflate_end_gently(&stream);
Linus Torvaldsac54c272007-03-20 11:38:34 -07001649 if (ret != Z_OK)
Patryk Obara3fc72812018-01-28 01:13:21 +01001650 die("deflateEnd on object %s failed (%d)", oid_to_hex(oid),
1651 ret);
brian m. carlson18e25882018-02-01 02:18:41 +00001652 the_hash_algo->final_fn(parano_oid.hash, &c);
Patryk Obara3fc72812018-01-28 01:13:21 +01001653 if (oidcmp(oid, &parano_oid) != 0)
1654 die("confused by unstable object source data for %s",
1655 oid_to_hex(oid));
Linus Torvaldsac54c272007-03-20 11:38:34 -07001656
Linus Torvaldse9039dd2008-06-10 18:47:18 -07001657 close_sha1_file(fd);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001658
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001659 if (mtime) {
1660 struct utimbuf utb;
1661 utb.actime = mtime;
1662 utb.modtime = mtime;
Jeff Kingd4b3d112015-09-24 17:07:49 -04001663 if (utime(tmp_file.buf, &utb) < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001664 warning_errno("failed utime() on %s", tmp_file.buf);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001665 }
1666
Christian Couderea657732018-01-17 18:54:54 +01001667 return finalize_object_file(tmp_file.buf, filename.buf);
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001668}
Daniel Barkalow8237b182005-04-23 18:47:23 -07001669
Jeff King33d42212014-10-15 18:42:22 -04001670static int freshen_loose_object(const unsigned char *sha1)
1671{
1672 return check_and_freshen(sha1, 1);
1673}
1674
1675static int freshen_packed_object(const unsigned char *sha1)
1676{
1677 struct pack_entry e;
Stefan Beller613b42f22018-03-23 18:45:25 +01001678 if (!find_pack_entry(the_repository, sha1, &e))
Jeff Kingee1c6c32015-04-20 15:55:00 -04001679 return 0;
1680 if (e.p->freshened)
1681 return 1;
1682 if (!freshen_file(e.p->pack_name))
1683 return 0;
1684 e.p->freshened = 1;
1685 return 1;
Jeff King33d42212014-10-15 18:42:22 -04001686}
1687
Patryk Obaraa09c9852018-01-28 01:13:19 +01001688int write_object_file(const void *buf, unsigned long len, const char *type,
1689 struct object_id *oid)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001690{
brian m. carlson1af64f72018-03-12 02:27:55 +00001691 char hdr[MAX_HEADER_LEN];
Jeff Kingef1286d2015-09-24 17:06:42 -04001692 int hdrlen = sizeof(hdr);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001693
1694 /* Normally if we have it in the pack then we do not bother writing
1695 * it out into .git/objects/??/?{38} file.
1696 */
Patryk Obaraa09c9852018-01-28 01:13:19 +01001697 write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
1698 if (freshen_packed_object(oid->hash) || freshen_loose_object(oid->hash))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001699 return 0;
Patryk Obara3fc72812018-01-28 01:13:21 +01001700 return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001701}
1702
Patryk Obara1752cbb2018-01-28 01:13:22 +01001703int hash_object_file_literally(const void *buf, unsigned long len,
1704 const char *type, struct object_id *oid,
1705 unsigned flags)
Eric Sunshine0c3db672015-05-04 03:25:15 -04001706{
1707 char *header;
1708 int hdrlen, status = 0;
1709
1710 /* type string, SP, %lu of the length plus NUL must fit this */
brian m. carlson1af64f72018-03-12 02:27:55 +00001711 hdrlen = strlen(type) + MAX_HEADER_LEN;
Jeff Kingef1286d2015-09-24 17:06:42 -04001712 header = xmalloc(hdrlen);
Patryk Obaraa09c9852018-01-28 01:13:19 +01001713 write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
Eric Sunshine0c3db672015-05-04 03:25:15 -04001714
1715 if (!(flags & HASH_WRITE_OBJECT))
1716 goto cleanup;
Patryk Obarada776112017-08-20 22:09:30 +02001717 if (freshen_packed_object(oid->hash) || freshen_loose_object(oid->hash))
Eric Sunshine0c3db672015-05-04 03:25:15 -04001718 goto cleanup;
Patryk Obara3fc72812018-01-28 01:13:21 +01001719 status = write_loose_object(oid, header, hdrlen, buf, len, 0);
Eric Sunshine0c3db672015-05-04 03:25:15 -04001720
1721cleanup:
1722 free(header);
1723 return status;
1724}
1725
Patryk Obara4bdb70a2018-01-28 01:13:20 +01001726int force_object_loose(const struct object_id *oid, time_t mtime)
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001727{
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001728 void *buf;
1729 unsigned long len;
1730 enum object_type type;
brian m. carlson1af64f72018-03-12 02:27:55 +00001731 char hdr[MAX_HEADER_LEN];
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001732 int hdrlen;
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02001733 int ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001734
Patryk Obara4bdb70a2018-01-28 01:13:20 +01001735 if (has_loose_object(oid->hash))
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001736 return 0;
Patryk Obara4bdb70a2018-01-28 01:13:20 +01001737 buf = read_object(oid->hash, &type, &len);
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001738 if (!buf)
Patryk Obara4bdb70a2018-01-28 01:13:20 +01001739 return error("cannot read sha1_file for %s", oid_to_hex(oid));
Brandon Williamsdebca9d2018-02-14 10:59:24 -08001740 hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), len) + 1;
Patryk Obara3fc72812018-01-28 01:13:21 +01001741 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
Björn Steinbrink1fb23e62008-10-18 02:37:31 +02001742 free(buf);
1743
1744 return ret;
Nicolas Pitrebbac7312008-05-14 01:32:48 -04001745}
1746
Jeff King0eeb0772015-06-09 13:24:37 -04001747int has_sha1_file_with_flags(const unsigned char *sha1, int flags)
Daniel Barkalow8237b182005-04-23 18:47:23 -07001748{
brian m. carlson7984f232018-03-12 02:27:45 +00001749 struct object_id oid;
Jonathan Nieder3e8b7d32017-04-11 15:47:13 -07001750 if (!startup_info->have_repository)
1751 return 0;
brian m. carlson7984f232018-03-12 02:27:45 +00001752 hashcpy(oid.hash, sha1);
Stefan Beller7ecd8692018-04-25 11:20:58 -07001753 return oid_object_info_extended(the_repository, &oid, NULL,
brian m. carlsonabef9022018-03-12 02:27:46 +00001754 flags | OBJECT_INFO_SKIP_CACHED) >= 0;
Daniel Barkalow8237b182005-04-23 18:47:23 -07001755}
Junio C Hamano74400e72005-05-01 23:45:49 -07001756
brian m. carlsonb419aa22015-11-10 02:22:19 +00001757int has_object_file(const struct object_id *oid)
1758{
1759 return has_sha1_file(oid->hash);
1760}
1761
Jeff King5827a032016-10-13 12:53:44 -04001762int has_object_file_with_flags(const struct object_id *oid, int flags)
1763{
1764 return has_sha1_file_with_flags(oid->hash, flags);
1765}
1766
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07001767static void check_tree(const void *buf, size_t size)
1768{
1769 struct tree_desc desc;
1770 struct name_entry entry;
1771
1772 init_tree_desc(&desc, buf, size);
1773 while (tree_entry(&desc, &entry))
1774 /* do nothing
1775 * tree_entry() will die() on malformed entries */
1776 ;
1777}
1778
1779static void check_commit(const void *buf, size_t size)
1780{
1781 struct commit c;
1782 memset(&c, 0, sizeof(c));
1783 if (parse_commit_buffer(&c, buf, size))
1784 die("corrupt commit");
1785}
1786
1787static void check_tag(const void *buf, size_t size)
1788{
1789 struct tag t;
1790 memset(&t, 0, sizeof(t));
1791 if (parse_tag_buffer(&t, buf, size))
1792 die("corrupt tag");
1793}
1794
brian m. carlsonbcd29862017-10-15 22:07:05 +00001795static int index_mem(struct object_id *oid, void *buf, size_t size,
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001796 enum object_type type,
1797 const char *path, unsigned flags)
Björn Engelmanne7332f92006-05-23 20:19:04 +02001798{
Linus Torvalds6c510be2007-02-13 11:07:23 -08001799 int ret, re_allocated = 0;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001800 int write_object = flags & HASH_WRITE_OBJECT;
Junio C Hamano74400e72005-05-01 23:45:49 -07001801
Bryan Larsen7672db22005-07-08 16:51:55 -07001802 if (!type)
Junio C Hamanoedaec3f2007-02-28 11:45:56 -08001803 type = OBJ_BLOB;
Linus Torvalds6c510be2007-02-13 11:07:23 -08001804
1805 /*
1806 * Convert blobs to git internal format
1807 */
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001808 if ((type == OBJ_BLOB) && path) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05001809 struct strbuf nbuf = STRBUF_INIT;
Brandon Williams82b474e2017-06-12 15:13:55 -07001810 if (convert_to_git(&the_index, path, buf, size, &nbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001811 get_conv_flags(flags))) {
Pierre Habouzitb315c5c2007-09-27 12:58:23 +02001812 buf = strbuf_detach(&nbuf, &size);
Linus Torvalds6c510be2007-02-13 11:07:23 -08001813 re_allocated = 1;
1814 }
1815 }
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001816 if (flags & HASH_FORMAT_CHECK) {
Nguyễn Thái Ngọc Duyc879daa2011-02-05 17:52:21 +07001817 if (type == OBJ_TREE)
1818 check_tree(buf, size);
1819 if (type == OBJ_COMMIT)
1820 check_commit(buf, size);
1821 if (type == OBJ_TAG)
1822 check_tag(buf, size);
1823 }
Linus Torvalds6c510be2007-02-13 11:07:23 -08001824
Bryan Larsen7672db22005-07-08 16:51:55 -07001825 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08001826 ret = write_object_file(buf, size, type_name(type), oid);
Rene Scharfeabdc3fc2006-10-14 12:45:36 +02001827 else
Junio C Hamano169c9c02018-03-06 14:54:07 -08001828 ret = hash_object_file(buf, size, type_name(type), oid);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001829 if (re_allocated)
Linus Torvalds6c510be2007-02-13 11:07:23 -08001830 free(buf);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001831 return ret;
1832}
1833
brian m. carlsonbcd29862017-10-15 22:07:05 +00001834static int index_stream_convert_blob(struct object_id *oid, int fd,
Steffen Prohaska9035d752014-08-26 17:23:25 +02001835 const char *path, unsigned flags)
1836{
1837 int ret;
1838 const int write_object = flags & HASH_WRITE_OBJECT;
1839 struct strbuf sbuf = STRBUF_INIT;
1840
1841 assert(path);
1842 assert(would_convert_to_git_filter_fd(path));
1843
Brandon Williamsd6c41c22017-06-12 15:13:54 -07001844 convert_to_git_filter_fd(&the_index, path, fd, &sbuf,
Torsten Bögershausen8462ff42018-01-13 23:49:31 +01001845 get_conv_flags(flags));
Steffen Prohaska9035d752014-08-26 17:23:25 +02001846
1847 if (write_object)
Junio C Hamano169c9c02018-03-06 14:54:07 -08001848 ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
Patryk Obaraa09c9852018-01-28 01:13:19 +01001849 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02001850 else
Junio C Hamano169c9c02018-03-06 14:54:07 -08001851 ret = hash_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
Patryk Obaraf070fac2018-01-28 01:13:13 +01001852 oid);
Steffen Prohaska9035d752014-08-26 17:23:25 +02001853 strbuf_release(&sbuf);
1854 return ret;
1855}
1856
brian m. carlsonbcd29862017-10-15 22:07:05 +00001857static int index_pipe(struct object_id *oid, int fd, enum object_type type,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001858 const char *path, unsigned flags)
1859{
1860 struct strbuf sbuf = STRBUF_INIT;
1861 int ret;
1862
1863 if (strbuf_read(&sbuf, fd, 4096) >= 0)
brian m. carlsonbcd29862017-10-15 22:07:05 +00001864 ret = index_mem(oid, sbuf.buf, sbuf.len, type, path, flags);
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001865 else
1866 ret = -1;
1867 strbuf_release(&sbuf);
1868 return ret;
1869}
1870
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001871#define SMALL_FILE_SIZE (32*1024)
1872
brian m. carlsonbcd29862017-10-15 22:07:05 +00001873static int index_core(struct object_id *oid, int fd, size_t size,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001874 enum object_type type, const char *path,
1875 unsigned flags)
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001876{
1877 int ret;
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001878
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001879 if (!size) {
brian m. carlsonbcd29862017-10-15 22:07:05 +00001880 ret = index_mem(oid, "", size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001881 } else if (size <= SMALL_FILE_SIZE) {
1882 char *buf = xmalloc(size);
Jeff King90dca672017-09-27 02:01:07 -04001883 ssize_t read_result = read_in_full(fd, buf, size);
1884 if (read_result < 0)
1885 ret = error_errno("read error while indexing %s",
1886 path ? path : "<unknown>");
1887 else if (read_result != size)
1888 ret = error("short read while indexing %s",
1889 path ? path : "<unknown>");
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001890 else
brian m. carlsonbcd29862017-10-15 22:07:05 +00001891 ret = index_mem(oid, buf, size, type, path, flags);
Dmitry Potapovea68b0c2010-02-21 09:32:19 +03001892 free(buf);
Dmitry Potapov08bda202010-05-11 01:38:17 +04001893 } else {
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001894 void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
brian m. carlsonbcd29862017-10-15 22:07:05 +00001895 ret = index_mem(oid, buf, size, type, path, flags);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001896 munmap(buf, size);
Dmitry Potapov08bda202010-05-11 01:38:17 +04001897 }
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001898 return ret;
1899}
1900
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001901/*
Junio C Hamano568508e2011-10-28 14:48:40 -07001902 * This creates one packfile per large blob unless bulk-checkin
1903 * machinery is "plugged".
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001904 *
1905 * This also bypasses the usual "convert-to-git" dance, and that is on
1906 * purpose. We could write a streaming version of the converting
1907 * functions and insert that before feeding the data to fast-import
Jeff King4f22b102012-02-24 17:10:17 -05001908 * (or equivalent in-core API described above). However, that is
1909 * somewhat complicated, as we do not know the size of the filter
1910 * result, which we need to know beforehand when writing a git object.
1911 * Since the primary motivation for trying to stream from the working
1912 * tree file and to avoid mmaping it in core is to deal with large
1913 * binary blobs, they generally do not want to get any conversion, and
1914 * callers should avoid this code path when filters are requested.
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001915 */
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02001916static int index_stream(struct object_id *oid, int fd, size_t size,
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001917 enum object_type type, const char *path,
1918 unsigned flags)
1919{
brian m. carlson68ee6df2018-03-12 02:27:21 +00001920 return index_bulk_checkin(oid, fd, size, type, path, flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001921}
1922
Patryk Obarae3506552017-08-20 22:09:29 +02001923int index_fd(struct object_id *oid, int fd, struct stat *st,
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001924 enum object_type type, const char *path, unsigned flags)
1925{
1926 int ret;
Junio C Hamano7b41e1e2011-05-08 01:47:34 -07001927
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001928 /*
1929 * Call xsize_t() only when needed to avoid potentially unnecessary
1930 * die() for large files.
1931 */
Steffen Prohaska9035d752014-08-26 17:23:25 +02001932 if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(path))
brian m. carlsonbcd29862017-10-15 22:07:05 +00001933 ret = index_stream_convert_blob(oid, fd, path, flags);
Steffen Prohaska9035d752014-08-26 17:23:25 +02001934 else if (!S_ISREG(st->st_mode))
brian m. carlsonbcd29862017-10-15 22:07:05 +00001935 ret = index_pipe(oid, fd, type, path, flags);
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001936 else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
Brandon Williams82b474e2017-06-12 15:13:55 -07001937 (path && would_convert_to_git(&the_index, path)))
brian m. carlsonbcd29862017-10-15 22:07:05 +00001938 ret = index_core(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001939 flags);
Junio C Hamano4dd1fbc2011-05-08 01:47:35 -07001940 else
Patryk Obara7d5e1dc2017-08-20 22:09:31 +02001941 ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
Steffen Prohaska9079ab72014-09-21 12:03:26 +02001942 flags);
Dmitry Potapov43df4f82008-08-03 08:39:16 +04001943 close(fd);
Linus Torvaldsaac17942005-05-03 11:46:16 -07001944 return ret;
Junio C Hamano74400e72005-05-01 23:45:49 -07001945}
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001946
Patryk Obara98e019b2017-08-20 22:09:28 +02001947int index_path(struct object_id *oid, const char *path, struct stat *st, unsigned flags)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001948{
1949 int fd;
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08001950 struct strbuf sb = STRBUF_INIT;
Rene Scharfeea8e0292017-08-30 20:00:29 +02001951 int rc = 0;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001952
1953 switch (st->st_mode & S_IFMT) {
1954 case S_IFREG:
1955 fd = open(path, O_RDONLY);
1956 if (fd < 0)
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001957 return error_errno("open(\"%s\")", path);
Patryk Obarae3506552017-08-20 22:09:29 +02001958 if (index_fd(oid, fd, st, OBJ_BLOB, path, flags) < 0)
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001959 return error("%s: failed to insert into database",
1960 path);
1961 break;
1962 case S_IFLNK:
Nguyễn Thái Ngọc Duy7616c6c2016-05-08 16:47:56 +07001963 if (strbuf_readlink(&sb, path, st->st_size))
1964 return error_errno("readlink(\"%s\")", path);
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -07001965 if (!(flags & HASH_WRITE_OBJECT))
Patryk Obaraf070fac2018-01-28 01:13:13 +01001966 hash_object_file(sb.buf, sb.len, blob_type, oid);
Patryk Obaraa09c9852018-01-28 01:13:19 +01001967 else if (write_object_file(sb.buf, sb.len, blob_type, oid))
Rene Scharfeea8e0292017-08-30 20:00:29 +02001968 rc = error("%s: failed to insert into database", path);
Linus Torvaldsb760d3a2008-12-17 09:51:53 -08001969 strbuf_release(&sb);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001970 break;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07001971 case S_IFDIR:
brian m. carlsona98e6102017-10-15 22:07:07 +00001972 return resolve_gitlink_ref(path, "HEAD", oid);
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001973 default:
1974 return error("%s: unsupported file type", path);
1975 }
Rene Scharfeea8e0292017-08-30 20:00:29 +02001976 return rc;
Junio C Hamanoec1fcc12005-10-07 03:42:00 -07001977}
Junio C Hamanoa69e5422007-01-22 21:55:18 -08001978
1979int read_pack_header(int fd, struct pack_header *header)
1980{
Jeff Kingf48ecd32017-09-13 14:47:22 -04001981 if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
Heikki Orsilac697ad12008-05-03 16:27:26 +03001982 /* "eof before pack header was fully read" */
1983 return PH_ERROR_EOF;
1984
Junio C Hamanoa69e5422007-01-22 21:55:18 -08001985 if (header->hdr_signature != htonl(PACK_SIGNATURE))
1986 /* "protocol error (pack signature mismatch detected)" */
1987 return PH_ERROR_PACK_SIGNATURE;
1988 if (!pack_version_ok(header->hdr_version))
1989 /* "protocol error (pack version unsupported)" */
1990 return PH_ERROR_PROTOCOL;
1991 return 0;
1992}
Jeff King40d52ff2010-04-01 20:05:23 -04001993
brian m. carlsone816caa2018-03-12 02:27:42 +00001994void assert_oid_type(const struct object_id *oid, enum object_type expect)
Jeff King40d52ff2010-04-01 20:05:23 -04001995{
Stefan Beller0df8e962018-04-25 11:20:59 -07001996 enum object_type type = oid_object_info(the_repository, oid, NULL);
Jeff King40d52ff2010-04-01 20:05:23 -04001997 if (type < 0)
brian m. carlsone816caa2018-03-12 02:27:42 +00001998 die("%s is not a valid object", oid_to_hex(oid));
Jeff King40d52ff2010-04-01 20:05:23 -04001999 if (type != expect)
brian m. carlsone816caa2018-03-12 02:27:42 +00002000 die("%s is not a valid '%s' object", oid_to_hex(oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002001 type_name(expect));
Jeff King40d52ff2010-04-01 20:05:23 -04002002}
Jeff King27e1e222014-10-15 18:38:55 -04002003
René Scharfe70c49052017-06-24 16:09:39 +02002004int for_each_file_in_obj_subdir(unsigned int subdir_nr,
René Scharfecc817ca2017-06-22 20:19:48 +02002005 struct strbuf *path,
2006 each_loose_object_fn obj_cb,
2007 each_loose_cruft_fn cruft_cb,
2008 each_loose_subdir_fn subdir_cb,
2009 void *data)
Jeff King27e1e222014-10-15 18:38:55 -04002010{
René Scharfe0375f472017-06-24 14:12:30 +02002011 size_t origlen, baselen;
2012 DIR *dir;
Jeff King27e1e222014-10-15 18:38:55 -04002013 struct dirent *de;
2014 int r = 0;
René Scharfe62a24c82017-10-31 14:50:06 +01002015 struct object_id oid;
Jeff King27e1e222014-10-15 18:38:55 -04002016
René Scharfe70c49052017-06-24 16:09:39 +02002017 if (subdir_nr > 0xff)
2018 BUG("invalid loose object subdirectory: %x", subdir_nr);
2019
René Scharfe0375f472017-06-24 14:12:30 +02002020 origlen = path->len;
2021 strbuf_complete(path, '/');
2022 strbuf_addf(path, "%02x", subdir_nr);
René Scharfe0375f472017-06-24 14:12:30 +02002023
2024 dir = opendir(path->buf);
Jeff King27e1e222014-10-15 18:38:55 -04002025 if (!dir) {
René Scharfe0375f472017-06-24 14:12:30 +02002026 if (errno != ENOENT)
2027 r = error_errno("unable to open %s", path->buf);
2028 strbuf_setlen(path, origlen);
2029 return r;
Jeff King27e1e222014-10-15 18:38:55 -04002030 }
2031
René Scharfe62a24c82017-10-31 14:50:06 +01002032 oid.hash[0] = subdir_nr;
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002033 strbuf_addch(path, '/');
2034 baselen = path->len;
René Scharfe62a24c82017-10-31 14:50:06 +01002035
Jeff King27e1e222014-10-15 18:38:55 -04002036 while ((de = readdir(dir))) {
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002037 size_t namelen;
Jeff King27e1e222014-10-15 18:38:55 -04002038 if (is_dot_or_dotdot(de->d_name))
2039 continue;
2040
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002041 namelen = strlen(de->d_name);
Jeff King27e1e222014-10-15 18:38:55 -04002042 strbuf_setlen(path, baselen);
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002043 strbuf_add(path, de->d_name, namelen);
2044 if (namelen == GIT_SHA1_HEXSZ - 2 &&
René Scharfe62a24c82017-10-31 14:50:06 +01002045 !hex_to_bytes(oid.hash + 1, de->d_name,
2046 GIT_SHA1_RAWSZ - 1)) {
2047 if (obj_cb) {
2048 r = obj_cb(&oid, path->buf, data);
2049 if (r)
2050 break;
Jeff King27e1e222014-10-15 18:38:55 -04002051 }
René Scharfe62a24c82017-10-31 14:50:06 +01002052 continue;
Jeff King27e1e222014-10-15 18:38:55 -04002053 }
2054
2055 if (cruft_cb) {
2056 r = cruft_cb(de->d_name, path->buf, data);
2057 if (r)
2058 break;
2059 }
2060 }
Johannes Sixt094c7e62015-08-12 19:43:01 +02002061 closedir(dir);
Jeff King27e1e222014-10-15 18:38:55 -04002062
Derrick Stolee163ee5e2017-12-04 09:06:03 -05002063 strbuf_setlen(path, baselen - 1);
Jeff King27e1e222014-10-15 18:38:55 -04002064 if (!r && subdir_cb)
2065 r = subdir_cb(subdir_nr, path->buf, data);
2066
René Scharfe0375f472017-06-24 14:12:30 +02002067 strbuf_setlen(path, origlen);
2068
Jeff King27e1e222014-10-15 18:38:55 -04002069 return r;
2070}
2071
Jeff Kinge6f875e2015-02-08 20:13:22 -05002072int for_each_loose_file_in_objdir_buf(struct strbuf *path,
Jeff King27e1e222014-10-15 18:38:55 -04002073 each_loose_object_fn obj_cb,
2074 each_loose_cruft_fn cruft_cb,
2075 each_loose_subdir_fn subdir_cb,
2076 void *data)
2077{
Jeff King27e1e222014-10-15 18:38:55 -04002078 int r = 0;
2079 int i;
2080
Jeff King27e1e222014-10-15 18:38:55 -04002081 for (i = 0; i < 256; i++) {
Jeff Kinge6f875e2015-02-08 20:13:22 -05002082 r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
Jeff King27e1e222014-10-15 18:38:55 -04002083 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002084 if (r)
2085 break;
2086 }
2087
Jeff Kinge6f875e2015-02-08 20:13:22 -05002088 return r;
2089}
2090
2091int for_each_loose_file_in_objdir(const char *path,
2092 each_loose_object_fn obj_cb,
2093 each_loose_cruft_fn cruft_cb,
2094 each_loose_subdir_fn subdir_cb,
2095 void *data)
2096{
2097 struct strbuf buf = STRBUF_INIT;
2098 int r;
2099
2100 strbuf_addstr(&buf, path);
2101 r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
2102 subdir_cb, data);
Jeff King27e1e222014-10-15 18:38:55 -04002103 strbuf_release(&buf);
Jeff Kinge6f875e2015-02-08 20:13:22 -05002104
Jeff King27e1e222014-10-15 18:38:55 -04002105 return r;
2106}
Jeff King660c8892014-10-15 18:41:21 -04002107
2108struct loose_alt_odb_data {
2109 each_loose_object_fn *cb;
2110 void *data;
2111};
2112
2113static int loose_from_alt_odb(struct alternate_object_database *alt,
2114 void *vdata)
2115{
2116 struct loose_alt_odb_data *data = vdata;
Jonathon Mahb0a42642015-02-08 20:15:39 -05002117 struct strbuf buf = STRBUF_INIT;
2118 int r;
2119
Jeff King597f9132016-10-03 16:35:51 -04002120 strbuf_addstr(&buf, alt->path);
Jonathon Mahb0a42642015-02-08 20:15:39 -05002121 r = for_each_loose_file_in_objdir_buf(&buf,
2122 data->cb, NULL, NULL,
2123 data->data);
2124 strbuf_release(&buf);
2125 return r;
Jeff King660c8892014-10-15 18:41:21 -04002126}
2127
Jeff King1385bb72015-03-27 07:32:41 -04002128int for_each_loose_object(each_loose_object_fn cb, void *data, unsigned flags)
Jeff King660c8892014-10-15 18:41:21 -04002129{
2130 struct loose_alt_odb_data alt;
2131 int r;
2132
2133 r = for_each_loose_file_in_objdir(get_object_directory(),
2134 cb, NULL, NULL, data);
2135 if (r)
2136 return r;
2137
Jeff King1385bb72015-03-27 07:32:41 -04002138 if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
2139 return 0;
2140
Jeff King660c8892014-10-15 18:41:21 -04002141 alt.cb = cb;
2142 alt.data = data;
2143 return foreach_alt_odb(loose_from_alt_odb, &alt);
2144}
2145
Jeff Kingf6371f92017-01-13 12:58:16 -05002146static int check_stream_sha1(git_zstream *stream,
2147 const char *hdr,
2148 unsigned long size,
2149 const char *path,
2150 const unsigned char *expected_sha1)
2151{
brian m. carlson18e25882018-02-01 02:18:41 +00002152 git_hash_ctx c;
brian m. carlsoncd025992017-03-26 16:01:25 +00002153 unsigned char real_sha1[GIT_MAX_RAWSZ];
Jeff Kingf6371f92017-01-13 12:58:16 -05002154 unsigned char buf[4096];
2155 unsigned long total_read;
2156 int status = Z_OK;
2157
brian m. carlson18e25882018-02-01 02:18:41 +00002158 the_hash_algo->init_fn(&c);
2159 the_hash_algo->update_fn(&c, hdr, stream->total_out);
Jeff Kingf6371f92017-01-13 12:58:16 -05002160
2161 /*
2162 * We already read some bytes into hdr, but the ones up to the NUL
2163 * do not count against the object's content size.
2164 */
2165 total_read = stream->total_out - strlen(hdr) - 1;
2166
2167 /*
2168 * This size comparison must be "<=" to read the final zlib packets;
2169 * see the comment in unpack_sha1_rest for details.
2170 */
2171 while (total_read <= size &&
2172 (status == Z_OK || status == Z_BUF_ERROR)) {
2173 stream->next_out = buf;
2174 stream->avail_out = sizeof(buf);
2175 if (size - total_read < stream->avail_out)
2176 stream->avail_out = size - total_read;
2177 status = git_inflate(stream, Z_FINISH);
brian m. carlson18e25882018-02-01 02:18:41 +00002178 the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
Jeff Kingf6371f92017-01-13 12:58:16 -05002179 total_read += stream->next_out - buf;
2180 }
2181 git_inflate_end(stream);
2182
2183 if (status != Z_STREAM_END) {
2184 error("corrupt loose object '%s'", sha1_to_hex(expected_sha1));
2185 return -1;
2186 }
Jeff Kingcce044d2017-01-13 13:00:25 -05002187 if (stream->avail_in) {
2188 error("garbage at end of loose object '%s'",
2189 sha1_to_hex(expected_sha1));
2190 return -1;
2191 }
Jeff Kingf6371f92017-01-13 12:58:16 -05002192
brian m. carlson18e25882018-02-01 02:18:41 +00002193 the_hash_algo->final_fn(real_sha1, &c);
Jeff Kingf6371f92017-01-13 12:58:16 -05002194 if (hashcmp(expected_sha1, real_sha1)) {
2195 error("sha1 mismatch for %s (expected %s)", path,
2196 sha1_to_hex(expected_sha1));
2197 return -1;
2198 }
2199
2200 return 0;
2201}
2202
2203int read_loose_object(const char *path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002204 const struct object_id *expected_oid,
Jeff Kingf6371f92017-01-13 12:58:16 -05002205 enum object_type *type,
2206 unsigned long *size,
2207 void **contents)
2208{
2209 int ret = -1;
Jeff Kingf6371f92017-01-13 12:58:16 -05002210 void *map = NULL;
2211 unsigned long mapsize;
2212 git_zstream stream;
brian m. carlson1af64f72018-03-12 02:27:55 +00002213 char hdr[MAX_HEADER_LEN];
Jeff Kingf6371f92017-01-13 12:58:16 -05002214
2215 *contents = NULL;
2216
Stefan Beller332295d2018-03-23 18:21:13 +01002217 map = map_sha1_file_1(the_repository, path, NULL, &mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002218 if (!map) {
2219 error_errno("unable to mmap %s", path);
2220 goto out;
2221 }
2222
2223 if (unpack_sha1_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
2224 error("unable to unpack header of %s", path);
2225 goto out;
2226 }
2227
2228 *type = parse_sha1_header(hdr, size);
2229 if (*type < 0) {
2230 error("unable to parse header of %s", path);
2231 git_inflate_end(&stream);
2232 goto out;
2233 }
2234
2235 if (*type == OBJ_BLOB) {
brian m. carlsond61d87b2018-03-12 02:27:38 +00002236 if (check_stream_sha1(&stream, hdr, *size, path, expected_oid->hash) < 0)
Jeff Kingf6371f92017-01-13 12:58:16 -05002237 goto out;
2238 } else {
brian m. carlsond61d87b2018-03-12 02:27:38 +00002239 *contents = unpack_sha1_rest(&stream, hdr, *size, expected_oid->hash);
Jeff Kingf6371f92017-01-13 12:58:16 -05002240 if (!*contents) {
2241 error("unable to unpack contents of %s", path);
2242 git_inflate_end(&stream);
2243 goto out;
2244 }
brian m. carlson17e65452018-03-12 02:27:39 +00002245 if (check_object_signature(expected_oid, *contents,
Brandon Williamsdebca9d2018-02-14 10:59:24 -08002246 *size, type_name(*type))) {
Jeff Kingf6371f92017-01-13 12:58:16 -05002247 error("sha1 mismatch for %s (expected %s)", path,
brian m. carlsond61d87b2018-03-12 02:27:38 +00002248 oid_to_hex(expected_oid));
Jeff Kingf6371f92017-01-13 12:58:16 -05002249 free(*contents);
2250 goto out;
2251 }
2252 }
2253
2254 ret = 0; /* everything checks out */
2255
2256out:
2257 if (map)
2258 munmap(map, mapsize);
Jeff Kingf6371f92017-01-13 12:58:16 -05002259 return ret;
2260}