blob: 8dfdeee93f74cf147ec9321b50480a37bff72f3f [file] [log] [blame]
Linus Torvalds8bc9a0c2005-04-07 15:16:10 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Linus Torvaldse83c5162005-04-07 15:13:13 -07006#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07007#include "config.h"
Elijah Newrencffbfad2018-06-30 18:24:55 -07008#include "diff.h"
9#include "diffcore.h"
Michael Haggertyf6ecc622015-08-10 11:47:45 +020010#include "tempfile.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +020011#include "lockfile.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070012#include "cache-tree.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070013#include "refs.h"
Alexandre Julliardd6168132007-08-11 23:59:01 +020014#include "dir.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070015#include "object-store.h"
Junio C Hamano041aee32008-07-21 01:24:17 -070016#include "tree.h"
17#include "commit.h"
Junio C Hamano39425812008-08-21 01:44:53 -070018#include "blob.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080019#include "resolve-undo.h"
Ben Peart1956ecd2019-02-15 12:59:21 -050020#include "run-command.h"
Junio C Hamano6c9cd162012-04-03 15:53:15 -070021#include "strbuf.h"
22#include "varint.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070023#include "split-index.h"
Jeff Kinga42643a2014-12-15 18:15:20 -050024#include "utf8.h"
Ben Peart883e2482017-09-22 12:35:40 -040025#include "fsmonitor.h"
Ben Peartabb4bb82018-10-10 11:59:36 -040026#include "thread-utils.h"
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +020027#include "progress.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070028
Thomas Gummererb60e1882012-07-11 11:22:37 +020029/* Mask for the name length in ce_flags in the on-disk index */
30
31#define CE_NAMEMASK (0x0fff)
32
Junio C Hamanobad68ec2006-04-24 21:18:58 -070033/* Index extensions.
34 *
35 * The first letter should be 'A'..'Z' for extensions that are not
36 * necessary for a correct operation (i.e. optimization data).
37 * When new extensions are added that _needs_ to be understood in
38 * order to correctly interpret the index file, pick character that
39 * is outside the range, to cause the reader to abort.
40 */
41
42#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
43#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
Shawn O. Pearceb659b492010-02-02 07:33:28 -080044#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070045#define CACHE_EXT_LINK 0x6c696e6b /* "link" */
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +070046#define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
Ben Peart883e2482017-09-22 12:35:40 -040047#define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
Ben Peart3b1d9e02018-10-10 11:59:34 -040048#define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
Ben Peart32550892018-10-10 11:59:37 -040049#define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070050
51/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
Nguyễn Thái Ngọc Duye0cf0d72014-06-13 19:19:37 +070052#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +070053 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
Ben Peart883e2482017-09-22 12:35:40 -040054 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED | FSMONITOR_CHANGED)
Linus Torvaldse83c5162005-04-07 15:13:13 -070055
Jameson Miller8e72d672018-07-02 19:49:37 +000056
57/*
58 * This is an estimate of the pathname length in the index. We use
59 * this for V4 index files to guess the un-deltafied size of the index
60 * in memory because of pathname deltafication. This is not required
61 * for V2/V3 index formats because their pathnames are not compressed.
62 * If the initial amount of memory set aside is not sufficient, the
63 * mem pool will allocate extra memory.
64 */
65#define CACHE_ENTRY_PATH_LENGTH 80
66
67static inline struct cache_entry *mem_pool__ce_alloc(struct mem_pool *mem_pool, size_t len)
68{
69 struct cache_entry *ce;
70 ce = mem_pool_alloc(mem_pool, cache_entry_size(len));
71 ce->mem_pool_allocated = 1;
72 return ce;
73}
74
75static inline struct cache_entry *mem_pool__ce_calloc(struct mem_pool *mem_pool, size_t len)
76{
77 struct cache_entry * ce;
78 ce = mem_pool_calloc(mem_pool, 1, cache_entry_size(len));
79 ce->mem_pool_allocated = 1;
80 return ce;
81}
82
83static struct mem_pool *find_mem_pool(struct index_state *istate)
84{
85 struct mem_pool **pool_ptr;
86
87 if (istate->split_index && istate->split_index->base)
88 pool_ptr = &istate->split_index->base->ce_mem_pool;
89 else
90 pool_ptr = &istate->ce_mem_pool;
91
Elijah Newren44c7e1a2020-08-15 17:37:56 +000092 if (!*pool_ptr) {
93 *pool_ptr = xmalloc(sizeof(**pool_ptr));
94 mem_pool_init(*pool_ptr, 0);
95 }
Jameson Miller8e72d672018-07-02 19:49:37 +000096
97 return *pool_ptr;
98}
99
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +0700100static const char *alternate_index_output;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -0700101
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800102static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
103{
104 istate->cache[nr] = ce;
Linus Torvalds96872bc2008-03-21 13:16:24 -0700105 add_name_hash(istate, ce);
Junio C Hamano9cb76b82008-01-22 23:01:13 -0800106}
107
Linus Torvaldscf558702008-01-22 18:41:14 -0800108static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
109{
110 struct cache_entry *old = istate->cache[nr];
111
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700112 replace_index_entry_in_base(istate, old, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100113 remove_name_hash(istate, old);
Jameson Millera8497352018-07-02 19:49:31 +0000114 discard_cache_entry(old);
Ben Peart0e267b72018-03-15 11:25:20 -0400115 ce->ce_flags &= ~CE_HASHED;
Linus Torvaldsa22c6372008-02-22 20:37:40 -0800116 set_index_entry(istate, nr, ce);
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +0700117 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400118 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700119 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvaldscf558702008-01-22 18:41:14 -0800120}
121
Petr Baudis81dc2302008-07-21 02:25:56 +0200122void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
123{
Brandon Williams285c2e22018-02-14 10:59:45 -0800124 struct cache_entry *old_entry = istate->cache[nr], *new_entry;
Petr Baudis81dc2302008-07-21 02:25:56 +0200125 int namelen = strlen(new_name);
126
Jameson Millera8497352018-07-02 19:49:31 +0000127 new_entry = make_empty_cache_entry(istate, namelen);
Brandon Williams285c2e22018-02-14 10:59:45 -0800128 copy_cache_entry(new_entry, old_entry);
129 new_entry->ce_flags &= ~CE_HASHED;
130 new_entry->ce_namelen = namelen;
131 new_entry->index = 0;
132 memcpy(new_entry->name, new_name, namelen + 1);
Petr Baudis81dc2302008-07-21 02:25:56 +0200133
Brandon Williams285c2e22018-02-14 10:59:45 -0800134 cache_tree_invalidate_path(istate, old_entry->name);
135 untracked_cache_remove_from_index(istate, old_entry->name);
Petr Baudis81dc2302008-07-21 02:25:56 +0200136 remove_index_entry_at(istate, nr);
Brandon Williams285c2e22018-02-14 10:59:45 -0800137 add_index_entry(istate, new_entry, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
Petr Baudis81dc2302008-07-21 02:25:56 +0200138}
139
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200140void fill_stat_data(struct stat_data *sd, struct stat *st)
141{
142 sd->sd_ctime.sec = (unsigned int)st->st_ctime;
143 sd->sd_mtime.sec = (unsigned int)st->st_mtime;
144 sd->sd_ctime.nsec = ST_CTIME_NSEC(*st);
145 sd->sd_mtime.nsec = ST_MTIME_NSEC(*st);
146 sd->sd_dev = st->st_dev;
147 sd->sd_ino = st->st_ino;
148 sd->sd_uid = st->st_uid;
149 sd->sd_gid = st->st_gid;
150 sd->sd_size = st->st_size;
151}
152
153int match_stat_data(const struct stat_data *sd, struct stat *st)
154{
155 int changed = 0;
156
157 if (sd->sd_mtime.sec != (unsigned int)st->st_mtime)
158 changed |= MTIME_CHANGED;
159 if (trust_ctime && check_stat &&
160 sd->sd_ctime.sec != (unsigned int)st->st_ctime)
161 changed |= CTIME_CHANGED;
162
163#ifdef USE_NSEC
164 if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st))
165 changed |= MTIME_CHANGED;
166 if (trust_ctime && check_stat &&
167 sd->sd_ctime.nsec != ST_CTIME_NSEC(*st))
168 changed |= CTIME_CHANGED;
169#endif
170
171 if (check_stat) {
172 if (sd->sd_uid != (unsigned int) st->st_uid ||
173 sd->sd_gid != (unsigned int) st->st_gid)
174 changed |= OWNER_CHANGED;
175 if (sd->sd_ino != (unsigned int) st->st_ino)
176 changed |= INODE_CHANGED;
177 }
178
179#ifdef USE_STDEV
180 /*
181 * st_dev breaks on network filesystems where different
182 * clients will have different views of what "device"
183 * the filesystem is on
184 */
185 if (check_stat && sd->sd_dev != (unsigned int) st->st_dev)
186 changed |= INODE_CHANGED;
187#endif
188
189 if (sd->sd_size != (unsigned int) st->st_size)
190 changed |= DATA_CHANGED;
191
192 return changed;
193}
194
Junio C Hamano415e96c2005-05-15 14:23:12 -0700195/*
196 * This only updates the "non-critical" parts of the directory
197 * cache, ie the parts that aren't tracked by GIT, and only used
198 * to validate the cache.
199 */
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -0700200void fill_stat_cache_info(struct index_state *istate, struct cache_entry *ce, struct stat *st)
Junio C Hamano415e96c2005-05-15 14:23:12 -0700201{
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200202 fill_stat_data(&ce->ce_stat_data, st);
Junio C Hamano5f730762006-02-08 21:15:24 -0800203
204 if (assume_unchanged)
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800205 ce->ce_flags |= CE_VALID;
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800206
Ben Peart883e2482017-09-22 12:35:40 -0400207 if (S_ISREG(st->st_mode)) {
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800208 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -0700209 mark_fsmonitor_valid(istate, ce);
Ben Peart883e2482017-09-22 12:35:40 -0400210 }
Junio C Hamano415e96c2005-05-15 14:23:12 -0700211}
212
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200213static int ce_compare_data(struct index_state *istate,
214 const struct cache_entry *ce,
215 struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800216{
217 int match = -1;
Junio C Hamano1b8ac5e2016-10-28 06:23:07 -0700218 int fd = git_open_cloexec(ce->name, O_RDONLY);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800219
220 if (fd >= 0) {
Patryk Obarabebfecb2017-08-20 22:09:27 +0200221 struct object_id oid;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200222 if (!index_fd(istate, &oid, fd, st, OBJ_BLOB, ce->name, 0))
Jeff King6a29d7b2018-08-28 17:22:59 -0400223 match = !oideq(&oid, &ce->oid);
Linus Torvalds7f8508e2006-07-31 09:55:15 -0700224 /* index_fd() closed the file descriptor already */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800225 }
226 return match;
227}
228
René Scharfe21a6b9f2013-06-02 17:46:52 +0200229static int ce_compare_link(const struct cache_entry *ce, size_t expected_size)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800230{
231 int match = -1;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800232 void *buffer;
233 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500234 enum object_type type;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800235 struct strbuf sb = STRBUF_INIT;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800236
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800237 if (strbuf_readlink(&sb, ce->name, expected_size))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800238 return -1;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800239
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000240 buffer = read_object_file(&ce->oid, &type, &size);
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800241 if (buffer) {
242 if (size == sb.len)
243 match = memcmp(buffer, sb.buf, size);
244 free(buffer);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800245 }
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800246 strbuf_release(&sb);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800247 return match;
248}
249
René Scharfe21a6b9f2013-06-02 17:46:52 +0200250static int ce_compare_gitlink(const struct cache_entry *ce)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700251{
brian m. carlson1053fe82017-10-15 22:07:06 +0000252 struct object_id oid;
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700253
254 /*
255 * We don't actually require that the .git directory
Martin Waitz302b9282007-05-21 22:08:28 +0200256 * under GITLINK directory be a valid git directory. It
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700257 * might even be missing (in case nobody populated that
258 * sub-project).
259 *
260 * If so, we consider it always to match.
261 */
brian m. carlsona98e6102017-10-15 22:07:07 +0000262 if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700263 return 0;
Jeff King6a29d7b2018-08-28 17:22:59 -0400264 return !oideq(&oid, &ce->oid);
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700265}
266
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200267static int ce_modified_check_fs(struct index_state *istate,
268 const struct cache_entry *ce,
269 struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800270{
271 switch (st->st_mode & S_IFMT) {
272 case S_IFREG:
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200273 if (ce_compare_data(istate, ce, st))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800274 return DATA_CHANGED;
275 break;
276 case S_IFLNK:
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500277 if (ce_compare_link(ce, xsize_t(st->st_size)))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800278 return DATA_CHANGED;
279 break;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700280 case S_IFDIR:
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800281 if (S_ISGITLINK(ce->ce_mode))
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700282 return ce_compare_gitlink(ce) ? DATA_CHANGED : 0;
Jeff King1cf01a32017-09-21 02:25:41 -0400283 /* else fallthrough */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800284 default:
285 return TYPE_CHANGED;
286 }
287 return 0;
288}
289
René Scharfe21a6b9f2013-06-02 17:46:52 +0200290static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
Linus Torvalds734aab72005-04-09 09:48:20 -0700291{
292 unsigned int changed = 0;
293
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800294 if (ce->ce_flags & CE_REMOVE)
295 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
296
297 switch (ce->ce_mode & S_IFMT) {
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200298 case S_IFREG:
299 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
Junio C Hamano3e09cdf2005-10-11 18:45:33 -0700300 /* We consider only the owner x bit to be relevant for
301 * "mode changes"
302 */
303 if (trust_executable_bit &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800304 (0100 & (ce->ce_mode ^ st->st_mode)))
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200305 changed |= MODE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200306 break;
307 case S_IFLNK:
Johannes Sixt78a8d642007-03-02 22:11:30 +0100308 if (!S_ISLNK(st->st_mode) &&
309 (has_symlinks || !S_ISREG(st->st_mode)))
310 changed |= TYPE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200311 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200312 case S_IFGITLINK:
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700313 /* We ignore most of the st_xxx fields for gitlinks */
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700314 if (!S_ISDIR(st->st_mode))
315 changed |= TYPE_CHANGED;
316 else if (ce_compare_gitlink(ce))
317 changed |= DATA_CHANGED;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700318 return changed;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200319 default:
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +0100320 BUG("unsupported ce_mode: %o", ce->ce_mode);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200321 }
Linus Torvaldsccc4feb2005-04-15 10:44:27 -0700322
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200323 changed |= match_stat_data(&ce->ce_stat_data, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800324
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700325 /* Racily smudged entry? */
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200326 if (!ce->ce_stat_data.sd_size) {
brian m. carlson99d1a982016-09-05 20:07:52 +0000327 if (!is_empty_blob_sha1(ce->oid.hash))
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700328 changed |= DATA_CHANGED;
329 }
330
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800331 return changed;
332}
333
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700334static int is_racy_stat(const struct index_state *istate,
335 const struct stat_data *sd)
336{
337 return (istate->timestamp.sec &&
338#ifdef USE_NSEC
339 /* nanosecond timestamped files can also be racy! */
340 (istate->timestamp.sec < sd->sd_mtime.sec ||
341 (istate->timestamp.sec == sd->sd_mtime.sec &&
342 istate->timestamp.nsec <= sd->sd_mtime.nsec))
343#else
344 istate->timestamp.sec <= sd->sd_mtime.sec
345#endif
346 );
347}
348
SZEDER Gábor5581a012018-10-11 11:43:09 +0200349int is_racy_timestamp(const struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200350 const struct cache_entry *ce)
Junio C Hamano6d91da62008-01-21 00:44:50 -0800351{
Junio C Hamano050288d2008-05-03 17:24:28 -0700352 return (!S_ISGITLINK(ce->ce_mode) &&
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700353 is_racy_stat(istate, &ce->ce_stat_data));
Junio C Hamano6d91da62008-01-21 00:44:50 -0800354}
355
Nguyễn Thái Ngọc Duyed4efab2015-03-08 17:12:37 +0700356int match_stat_data_racy(const struct index_state *istate,
357 const struct stat_data *sd, struct stat *st)
358{
359 if (is_racy_stat(istate, sd))
360 return MTIME_CHANGED;
361 return match_stat_data(sd, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800362}
363
Ben Peart883e2482017-09-22 12:35:40 -0400364int ie_match_stat(struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200365 const struct cache_entry *ce, struct stat *st,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800366 unsigned int options)
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800367{
Junio C Hamano5f730762006-02-08 21:15:24 -0800368 unsigned int changed;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800369 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700370 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800371 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
Ben Peart883e2482017-09-22 12:35:40 -0400372 int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR;
Junio C Hamano5f730762006-02-08 21:15:24 -0800373
Ben Peart883e2482017-09-22 12:35:40 -0400374 if (!ignore_fsmonitor)
375 refresh_fsmonitor(istate);
Junio C Hamano5f730762006-02-08 21:15:24 -0800376 /*
377 * If it's marked as always valid in the index, it's
378 * valid whatever the checked-out copy says.
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700379 *
380 * skip-worktree has the same effect with higher precedence
Junio C Hamano5f730762006-02-08 21:15:24 -0800381 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700382 if (!ignore_skip_worktree && ce_skip_worktree(ce))
383 return 0;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800384 if (!ignore_valid && (ce->ce_flags & CE_VALID))
Junio C Hamano5f730762006-02-08 21:15:24 -0800385 return 0;
Ben Peart883e2482017-09-22 12:35:40 -0400386 if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID))
387 return 0;
Junio C Hamano5f730762006-02-08 21:15:24 -0800388
Junio C Hamano331fcb52008-11-28 19:56:34 -0800389 /*
390 * Intent-to-add entries have not been added, so the index entry
391 * by definition never matches what is in the work tree until it
392 * actually gets added.
393 */
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700394 if (ce_intent_to_add(ce))
Junio C Hamano331fcb52008-11-28 19:56:34 -0800395 return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
396
Junio C Hamano5f730762006-02-08 21:15:24 -0800397 changed = ce_match_stat_basic(ce, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800398
Junio C Hamano29e4d362005-12-20 00:02:15 -0800399 /*
400 * Within 1 second of this sequence:
401 * echo xyzzy >file && git-update-index --add file
402 * running this command:
403 * echo frotz >file
404 * would give a falsely clean cache entry. The mtime and
405 * length match the cache, and other stat fields do not change.
406 *
407 * We could detect this at update-index time (the cache entry
408 * being registered/updated records the same time as "now")
409 * and delay the return from git-update-index, but that would
410 * effectively mean we can make at most one commit per second,
411 * which is not acceptable. Instead, we check cache entries
412 * whose mtime are the same as the index file timestamp more
Junio C Hamano5f730762006-02-08 21:15:24 -0800413 * carefully than others.
Junio C Hamano29e4d362005-12-20 00:02:15 -0800414 */
Junio C Hamano6d91da62008-01-21 00:44:50 -0800415 if (!changed && is_racy_timestamp(istate, ce)) {
Junio C Hamano42f77402006-08-15 21:38:07 -0700416 if (assume_racy_is_modified)
417 changed |= DATA_CHANGED;
418 else
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200419 changed |= ce_modified_check_fs(istate, ce, st);
Junio C Hamano42f77402006-08-15 21:38:07 -0700420 }
Junio C Hamano29e4d362005-12-20 00:02:15 -0800421
Linus Torvalds734aab72005-04-09 09:48:20 -0700422 return changed;
423}
424
Ben Peart883e2482017-09-22 12:35:40 -0400425int ie_modified(struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200426 const struct cache_entry *ce,
427 struct stat *st, unsigned int options)
Junio C Hamanob0391892005-09-19 15:11:15 -0700428{
Junio C Hamano29e4d362005-12-20 00:02:15 -0800429 int changed, changed_fs;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800430
431 changed = ie_match_stat(istate, ce, st, options);
Junio C Hamanob0391892005-09-19 15:11:15 -0700432 if (!changed)
433 return 0;
Junio C Hamanob0391892005-09-19 15:11:15 -0700434 /*
435 * If the mode or type has changed, there's no point in trying
436 * to refresh the entry - it's not going to match
437 */
438 if (changed & (MODE_CHANGED | TYPE_CHANGED))
439 return changed;
440
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700441 /*
442 * Immediately after read-tree or update-index --cacheinfo,
443 * the length field is zero, as we have never even read the
444 * lstat(2) information once, and we cannot trust DATA_CHANGED
445 * returned by ie_match_stat() which in turn was returned by
446 * ce_match_stat_basic() to signal that the filesize of the
447 * blob changed. We have to actually go to the filesystem to
448 * see if the contents match, and if so, should answer "unchanged".
449 *
450 * The logic does not apply to gitlinks, as ce_match_stat_basic()
451 * already has checked the actual HEAD from the filesystem in the
452 * subproject. If ie_match_stat() already said it is different,
453 * then we know it is.
Junio C Hamanob0391892005-09-19 15:11:15 -0700454 */
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700455 if ((changed & DATA_CHANGED) &&
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200456 (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
Junio C Hamanob0391892005-09-19 15:11:15 -0700457 return changed;
458
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200459 changed_fs = ce_modified_check_fs(istate, ce, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800460 if (changed_fs)
461 return changed | changed_fs;
Junio C Hamanob0391892005-09-19 15:11:15 -0700462 return 0;
463}
464
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700465int base_name_compare(const char *name1, int len1, int mode1,
466 const char *name2, int len2, int mode2)
467{
468 unsigned char c1, c2;
469 int len = len1 < len2 ? len1 : len2;
470 int cmp;
471
472 cmp = memcmp(name1, name2, len);
473 if (cmp)
474 return cmp;
475 c1 = name1[len];
476 c2 = name2[len];
Linus Torvalds1833a922007-04-11 14:39:12 -0700477 if (!c1 && S_ISDIR(mode1))
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700478 c1 = '/';
Linus Torvalds1833a922007-04-11 14:39:12 -0700479 if (!c2 && S_ISDIR(mode2))
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700480 c2 = '/';
481 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
482}
483
Linus Torvalds0ab9e1e2008-03-05 18:25:10 -0800484/*
485 * df_name_compare() is identical to base_name_compare(), except it
486 * compares conflicting directory/file entries as equal. Note that
487 * while a directory name compares as equal to a regular file, they
488 * then individually compare _differently_ to a filename that has
489 * a dot after the basename (because '\0' < '.' < '/').
490 *
491 * This is used by routines that want to traverse the git namespace
492 * but then handle conflicting entries together when possible.
493 */
494int df_name_compare(const char *name1, int len1, int mode1,
495 const char *name2, int len2, int mode2)
496{
497 int len = len1 < len2 ? len1 : len2, cmp;
498 unsigned char c1, c2;
499
500 cmp = memcmp(name1, name2, len);
501 if (cmp)
502 return cmp;
503 /* Directories and files compare equal (same length, same name) */
504 if (len1 == len2)
505 return 0;
506 c1 = name1[len];
507 if (!c1 && S_ISDIR(mode1))
508 c1 = '/';
509 c2 = name2[len];
510 if (!c2 && S_ISDIR(mode2))
511 c2 = '/';
512 if (c1 == '/' && !c2)
513 return 0;
514 if (c2 == '/' && !c1)
515 return 0;
516 return c1 - c2;
517}
518
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700519int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700520{
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700521 size_t min_len = (len1 < len2) ? len1 : len2;
522 int cmp = memcmp(name1, name2, min_len);
Linus Torvaldseb38c222005-04-09 09:26:55 -0700523 if (cmp)
524 return cmp;
525 if (len1 < len2)
526 return -1;
527 if (len1 > len2)
528 return 1;
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700529 return 0;
530}
531
532int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2)
533{
534 int cmp;
535
536 cmp = name_compare(name1, len1, name2, len2);
537 if (cmp)
538 return cmp;
Junio C Hamano5f730762006-02-08 21:15:24 -0800539
Thomas Gummererb60e1882012-07-11 11:22:37 +0200540 if (stage1 < stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700541 return -1;
Thomas Gummererb60e1882012-07-11 11:22:37 +0200542 if (stage1 > stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700543 return 1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700544 return 0;
545}
546
Junio C Hamano357e9c62012-09-15 22:44:31 -0700547static int index_name_stage_pos(const struct index_state *istate, const char *name, int namelen, int stage)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700548{
549 int first, last;
550
551 first = 0;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700552 last = istate->cache_nr;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700553 while (last > first) {
René Scharfe568a05c2019-06-13 19:51:56 +0200554 int next = first + ((last - first) >> 1);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700555 struct cache_entry *ce = istate->cache[next];
Thomas Gummererb60e1882012-07-11 11:22:37 +0200556 int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvaldseb38c222005-04-09 09:26:55 -0700557 if (!cmp)
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700558 return next;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700559 if (cmp < 0) {
560 last = next;
561 continue;
562 }
563 first = next+1;
564 }
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700565 return -first-1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700566}
567
Thomas Gummererb60e1882012-07-11 11:22:37 +0200568int index_name_pos(const struct index_state *istate, const char *name, int namelen)
569{
570 return index_name_stage_pos(istate, name, namelen, 0);
571}
572
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700573int remove_index_entry_at(struct index_state *istate, int pos)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700574{
Linus Torvaldscf558702008-01-22 18:41:14 -0800575 struct cache_entry *ce = istate->cache[pos];
576
Junio C Hamanocfc57892009-12-25 00:30:51 -0800577 record_resolve_undo(istate, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100578 remove_name_hash(istate, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700579 save_or_free_index_entry(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700580 istate->cache_changed |= CE_ENTRY_REMOVED;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700581 istate->cache_nr--;
582 if (pos >= istate->cache_nr)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700583 return 0;
René Scharfef331ab92017-07-15 22:00:45 +0200584 MOVE_ARRAY(istate->cache + pos, istate->cache + pos + 1,
585 istate->cache_nr - pos);
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700586 return 1;
587}
588
Kjetil Barvik36419c82009-02-18 23:18:03 +0100589/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200590 * Remove all cache entries marked for removal, that is where
Kjetil Barvik36419c82009-02-18 23:18:03 +0100591 * CE_REMOVE is set in ce_flags. This is much more effective than
592 * calling remove_index_entry_at() for each entry to be removed.
593 */
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000594void remove_marked_cache_entries(struct index_state *istate, int invalidate)
Kjetil Barvik36419c82009-02-18 23:18:03 +0100595{
596 struct cache_entry **ce_array = istate->cache;
597 unsigned int i, j;
598
599 for (i = j = 0; i < istate->cache_nr; i++) {
Karsten Blees5699d172013-11-14 20:24:37 +0100600 if (ce_array[i]->ce_flags & CE_REMOVE) {
Thomas Gummerer6fdc2052018-12-20 13:48:16 +0000601 if (invalidate) {
602 cache_tree_invalidate_path(istate,
603 ce_array[i]->name);
604 untracked_cache_remove_from_index(istate,
605 ce_array[i]->name);
606 }
Karsten Blees20926782013-02-28 00:57:48 +0100607 remove_name_hash(istate, ce_array[i]);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700608 save_or_free_index_entry(istate, ce_array[i]);
Karsten Blees5699d172013-11-14 20:24:37 +0100609 }
Kjetil Barvik36419c82009-02-18 23:18:03 +0100610 else
611 ce_array[j++] = ce_array[i];
612 }
Nguyễn Thái Ngọc Duyad837d92014-06-13 19:19:26 +0700613 if (j == istate->cache_nr)
614 return;
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700615 istate->cache_changed |= CE_ENTRY_REMOVED;
Kjetil Barvik36419c82009-02-18 23:18:03 +0100616 istate->cache_nr = j;
617}
618
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700619int remove_file_from_index(struct index_state *istate, const char *path)
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700620{
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700621 int pos = index_name_pos(istate, path, strlen(path));
Junio C Hamanoc4e3cca2005-04-17 09:53:35 -0700622 if (pos < 0)
623 pos = -pos-1;
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700624 cache_tree_invalidate_path(istate, path);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +0700625 untracked_cache_remove_from_index(istate, path);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700626 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
627 remove_index_entry_at(istate, pos);
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700628 return 0;
629}
630
Johannes Schindelin20314272007-06-29 18:32:46 +0100631static int compare_name(struct cache_entry *ce, const char *path, int namelen)
632{
633 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
634}
635
636static int index_name_pos_also_unmerged(struct index_state *istate,
637 const char *path, int namelen)
638{
639 int pos = index_name_pos(istate, path, namelen);
640 struct cache_entry *ce;
641
642 if (pos >= 0)
643 return pos;
644
645 /* maybe unmerged? */
646 pos = -1 - pos;
647 if (pos >= istate->cache_nr ||
648 compare_name((ce = istate->cache[pos]), path, namelen))
649 return -1;
650
651 /* order of preference: stage 2, 1, 3 */
652 if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
653 ce_stage((ce = istate->cache[pos + 1])) == 2 &&
654 !compare_name(ce, path, namelen))
655 pos++;
656 return pos;
657}
658
Linus Torvalds11029522008-03-22 14:22:44 -0700659static int different_name(struct cache_entry *ce, struct cache_entry *alias)
660{
661 int len = ce_namelen(ce);
662 return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len);
663}
664
665/*
666 * If we add a filename that aliases in the cache, we will use the
667 * name that we already have - but we don't want to update the same
668 * alias twice, because that implies that there were actually two
669 * different files with aliasing names!
670 *
671 * So we use the CE_ADDED flag to verify that the alias was an old
672 * one before we accept it as
673 */
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700674static struct cache_entry *create_alias_ce(struct index_state *istate,
675 struct cache_entry *ce,
676 struct cache_entry *alias)
Linus Torvalds11029522008-03-22 14:22:44 -0700677{
678 int len;
Brandon Williams285c2e22018-02-14 10:59:45 -0800679 struct cache_entry *new_entry;
Linus Torvalds11029522008-03-22 14:22:44 -0700680
681 if (alias->ce_flags & CE_ADDED)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100682 die(_("will not add file alias '%s' ('%s' already exists in index)"),
683 ce->name, alias->name);
Linus Torvalds11029522008-03-22 14:22:44 -0700684
685 /* Ok, create the new entry using the name of the existing alias */
686 len = ce_namelen(alias);
Jameson Millera8497352018-07-02 19:49:31 +0000687 new_entry = make_empty_cache_entry(istate, len);
Brandon Williams285c2e22018-02-14 10:59:45 -0800688 memcpy(new_entry->name, alias->name, len);
689 copy_cache_entry(new_entry, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700690 save_or_free_index_entry(istate, ce);
Brandon Williams285c2e22018-02-14 10:59:45 -0800691 return new_entry;
Linus Torvalds11029522008-03-22 14:22:44 -0700692}
693
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700694void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
Junio C Hamano39425812008-08-21 01:44:53 -0700695{
Patryk Obaraa09c9852018-01-28 01:13:19 +0100696 struct object_id oid;
697 if (write_object_file("", 0, blob_type, &oid))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100698 die(_("cannot create an empty blob in the object database"));
Patryk Obaraa09c9852018-01-28 01:13:19 +0100699 oidcpy(&ce->oid, &oid);
Junio C Hamano39425812008-08-21 01:44:53 -0700700}
701
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100702int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200703{
Jameson Millera8497352018-07-02 19:49:31 +0000704 int namelen, was_same;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700705 mode_t st_mode = st->st_mode;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100706 struct cache_entry *ce, *alias = NULL;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700707 unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY;
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700708 int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
709 int pretend = flags & ADD_CACHE_PRETEND;
Junio C Hamano39425812008-08-21 01:44:53 -0700710 int intent_only = flags & ADD_CACHE_INTENT;
711 int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
712 (intent_only ? ADD_CACHE_NEW_ONLY : 0));
Jeff King9e5da3d2019-01-17 11:27:11 -0500713 int hash_flags = HASH_WRITE_OBJECT;
Kyle Meyerf937bc22019-04-09 19:07:37 -0400714 struct object_id oid;
Torsten Bögershausen94729352017-11-16 17:38:28 +0100715
Jeff King9e5da3d2019-01-17 11:27:11 -0500716 if (flags & ADD_CACHE_RENORMALIZE)
717 hash_flags |= HASH_RENORMALIZE;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200718
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700719 if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100720 return error(_("%s: can only add regular files, symbolic links or git-directories"), path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200721
722 namelen = strlen(path);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700723 if (S_ISDIR(st_mode)) {
Kyle Meyerf937bc22019-04-09 19:07:37 -0400724 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
725 return error(_("'%s' does not have a commit checked out"), path);
Linus Torvalds09595252007-04-11 14:49:44 -0700726 while (namelen && path[namelen-1] == '/')
727 namelen--;
728 }
Jameson Millera8497352018-07-02 19:49:31 +0000729 ce = make_empty_cache_entry(istate, namelen);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200730 memcpy(ce->name, path, namelen);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200731 ce->ce_namelen = namelen;
Junio C Hamano39425812008-08-21 01:44:53 -0700732 if (!intent_only)
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -0700733 fill_stat_cache_info(istate, ce, st);
Junio C Hamano388b2ac2008-11-28 19:55:25 -0800734 else
735 ce->ce_flags |= CE_INTENT_TO_ADD;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200736
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100737
738 if (trust_executable_bit && has_symlinks) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700739 ce->ce_mode = create_ce_mode(st_mode);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100740 } else {
Johannes Sixt78a8d642007-03-02 22:11:30 +0100741 /* If there is an existing entry, pick the mode bits and type
742 * from it, otherwise assume unexecutable regular file.
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200743 */
Junio C Hamano185c9752007-02-16 22:43:48 -0800744 struct cache_entry *ent;
Johannes Schindelin20314272007-06-29 18:32:46 +0100745 int pos = index_name_pos_also_unmerged(istate, path, namelen);
Junio C Hamano185c9752007-02-16 22:43:48 -0800746
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700747 ent = (0 <= pos) ? istate->cache[pos] : NULL;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700748 ce->ce_mode = ce_mode_from_stat(ent, st_mode);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200749 }
750
Joshua Jensendc1ae702010-10-03 09:56:45 +0000751 /* When core.ignorecase=true, determine if a directory of the same name but differing
752 * case already exists within the Git repository. If it does, ensure the directory
753 * case of the file being added to the repository matches (is folded into) the existing
754 * entry's directory case.
755 */
756 if (ignore_case) {
David Turner41284eb2015-10-21 13:54:11 -0400757 adjust_dirname_case(istate, ce->name);
Joshua Jensendc1ae702010-10-03 09:56:45 +0000758 }
Jeff Kinge2c2a372019-02-06 21:00:22 -0500759 if (!(flags & ADD_CACHE_RENORMALIZE)) {
Torsten Bögershausen94729352017-11-16 17:38:28 +0100760 alias = index_file_exists(istate, ce->name,
761 ce_namelen(ce), ignore_case);
762 if (alias &&
763 !ce_stage(alias) &&
764 !ie_match_stat(istate, alias, st, ce_option)) {
765 /* Nothing changed, really */
766 if (!S_ISGITLINK(alias->ce_mode))
767 ce_mark_uptodate(alias);
768 alias->ce_flags |= CE_ADDED;
Joshua Jensendc1ae702010-10-03 09:56:45 +0000769
Jameson Millera8497352018-07-02 19:49:31 +0000770 discard_cache_entry(ce);
Torsten Bögershausen94729352017-11-16 17:38:28 +0100771 return 0;
772 }
Junio C Hamano0781b8a2007-07-30 17:12:58 -0700773 }
Junio C Hamano39425812008-08-21 01:44:53 -0700774 if (!intent_only) {
Junio C Hamano1c418242019-02-05 14:26:13 -0800775 if (index_path(istate, &ce->oid, path, st, hash_flags)) {
Jameson Millera8497352018-07-02 19:49:31 +0000776 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100777 return error(_("unable to index file '%s'"), path);
Stefan Beller2d9426b2015-03-20 17:28:00 -0700778 }
Junio C Hamano39425812008-08-21 01:44:53 -0700779 } else
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700780 set_object_name_for_intent_to_add_entry(ce);
Junio C Hamano39425812008-08-21 01:44:53 -0700781
Linus Torvalds11029522008-03-22 14:22:44 -0700782 if (ignore_case && alias && different_name(ce, alias))
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700783 ce = create_alias_ce(istate, ce, alias);
Linus Torvalds11029522008-03-22 14:22:44 -0700784 ce->ce_flags |= CE_ADDED;
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700785
Junio C Hamano3bf0dd12008-07-16 18:48:58 -0700786 /* It was suspected to be racily clean, but it turns out to be Ok */
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700787 was_same = (alias &&
788 !ce_stage(alias) &&
Jeff King4a7e27e2018-08-28 17:22:40 -0400789 oideq(&alias->oid, &ce->oid) &&
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700790 ce->ce_mode == alias->ce_mode);
791
792 if (pretend)
Jameson Millera8497352018-07-02 19:49:31 +0000793 discard_cache_entry(ce);
Junio C Hamano067178e2015-03-23 10:58:00 -0700794 else if (add_index_entry(istate, ce, add_option)) {
Jameson Millera8497352018-07-02 19:49:31 +0000795 discard_cache_entry(ce);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100796 return error(_("unable to add '%s' to index"), path);
Junio C Hamano067178e2015-03-23 10:58:00 -0700797 }
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700798 if (verbose && !was_same)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200799 printf("add '%s'\n", path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200800 return 0;
801}
802
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100803int add_file_to_index(struct index_state *istate, const char *path, int flags)
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700804{
805 struct stat st;
806 if (lstat(path, &st))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100807 die_errno(_("unable to stat '%s'"), path);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100808 return add_to_index(istate, path, &st, flags);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700809}
810
Jameson Millera8497352018-07-02 19:49:31 +0000811struct cache_entry *make_empty_cache_entry(struct index_state *istate, size_t len)
Carlos Rica6640f882007-09-11 05:17:28 +0200812{
Jameson Miller8e72d672018-07-02 19:49:37 +0000813 return mem_pool__ce_calloc(find_mem_pool(istate), len);
Jameson Millera8497352018-07-02 19:49:31 +0000814}
815
816struct cache_entry *make_empty_transient_cache_entry(size_t len)
817{
818 return xcalloc(1, cache_entry_size(len));
819}
820
821struct cache_entry *make_cache_entry(struct index_state *istate,
822 unsigned int mode,
Jameson Miller825ed4d2018-07-02 19:49:30 +0000823 const struct object_id *oid,
824 const char *path,
825 int stage,
826 unsigned int refresh_options)
Carlos Rica6640f882007-09-11 05:17:28 +0200827{
Stefan Bellerbc1c2ca2015-02-17 10:06:14 -0800828 struct cache_entry *ce, *ret;
Jameson Millera8497352018-07-02 19:49:31 +0000829 int len;
Carlos Rica6640f882007-09-11 05:17:28 +0200830
Jeff King10ecfa72018-05-04 20:03:35 -0400831 if (!verify_path(path, mode)) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100832 error(_("invalid path '%s'"), path);
Carlos Rica6640f882007-09-11 05:17:28 +0200833 return NULL;
Dmitry Potapov7e7abea2008-10-11 20:39:37 +0400834 }
Carlos Rica6640f882007-09-11 05:17:28 +0200835
836 len = strlen(path);
Jameson Millera8497352018-07-02 19:49:31 +0000837 ce = make_empty_cache_entry(istate, len);
Carlos Rica6640f882007-09-11 05:17:28 +0200838
Jameson Miller825ed4d2018-07-02 19:49:30 +0000839 oidcpy(&ce->oid, oid);
Carlos Rica6640f882007-09-11 05:17:28 +0200840 memcpy(ce->name, path, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200841 ce->ce_flags = create_ce_flags(stage);
842 ce->ce_namelen = len;
Carlos Rica6640f882007-09-11 05:17:28 +0200843 ce->ce_mode = create_ce_mode(mode);
844
Nguyễn Thái Ngọc Duyd7b665c2018-09-21 17:57:25 +0200845 ret = refresh_cache_entry(istate, ce, refresh_options);
Stefan Beller915e44c2015-03-23 10:57:11 -0700846 if (ret != ce)
Jameson Millera8497352018-07-02 19:49:31 +0000847 discard_cache_entry(ce);
Stefan Beller915e44c2015-03-23 10:57:11 -0700848 return ret;
Carlos Rica6640f882007-09-11 05:17:28 +0200849}
850
Jameson Millera8497352018-07-02 19:49:31 +0000851struct cache_entry *make_transient_cache_entry(unsigned int mode, const struct object_id *oid,
852 const char *path, int stage)
853{
854 struct cache_entry *ce;
855 int len;
856
857 if (!verify_path(path, mode)) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +0100858 error(_("invalid path '%s'"), path);
Jameson Millera8497352018-07-02 19:49:31 +0000859 return NULL;
860 }
861
862 len = strlen(path);
863 ce = make_empty_transient_cache_entry(len);
864
865 oidcpy(&ce->oid, oid);
866 memcpy(ce->name, path, len);
867 ce->ce_flags = create_ce_flags(stage);
868 ce->ce_namelen = len;
869 ce->ce_mode = create_ce_mode(mode);
870
871 return ce;
872}
873
Thomas Gummererd9d70962016-09-14 22:07:46 +0100874/*
875 * Chmod an index entry with either +x or -x.
876 *
877 * Returns -1 if the chmod for the particular cache entry failed (if it's
878 * not a regular file), -2 if an invalid flip argument is passed in, 0
879 * otherwise.
880 */
881int chmod_index_entry(struct index_state *istate, struct cache_entry *ce,
882 char flip)
883{
884 if (!S_ISREG(ce->ce_mode))
885 return -1;
886 switch (flip) {
887 case '+':
888 ce->ce_mode |= 0111;
889 break;
890 case '-':
891 ce->ce_mode &= ~0111;
892 break;
893 default:
894 return -2;
895 }
896 cache_tree_invalidate_path(istate, ce->name);
897 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -0400898 mark_fsmonitor_invalid(istate, ce);
Thomas Gummererd9d70962016-09-14 22:07:46 +0100899 istate->cache_changed |= CE_ENTRY_CHANGED;
900
901 return 0;
902}
903
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700904int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700905{
906 int len = ce_namelen(a);
907 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
908}
909
Linus Torvalds12676602005-06-18 20:21:34 -0700910/*
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700911 * We fundamentally don't like some paths: we don't want
912 * dot or dot-dot anywhere, and for obvious reasons don't
913 * want to recurse into ".git" either.
914 *
915 * Also, we don't want double slashes or slashes at the
916 * end that can make pathnames ambiguous.
917 */
Jeff King10ecfa72018-05-04 20:03:35 -0400918static int verify_dotfile(const char *rest, unsigned mode)
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700919{
920 /*
921 * The first character was '.', but that
922 * has already been discarded, we now test
923 * the rest.
924 */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200925
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700926 /* "." is not allowed */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200927 if (*rest == '\0' || is_dir_sep(*rest))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700928 return 0;
929
Theo Niessinke0f530f2011-06-08 14:04:41 +0200930 switch (*rest) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700931 /*
Jeff King641084b2018-05-15 09:56:50 -0400932 * ".git" followed by NUL or slash is bad. Note that we match
933 * case-insensitively here, even if ignore_case is not set.
934 * This outlaws ".GIT" everywhere out of an abundance of caution,
935 * since there's really no good reason to allow it.
Jeff King10ecfa72018-05-04 20:03:35 -0400936 *
937 * Once we've seen ".git", we can also find ".gitmodules", etc (also
938 * case-insensitively).
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700939 */
940 case 'g':
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500941 case 'G':
942 if (rest[1] != 'i' && rest[1] != 'I')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700943 break;
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500944 if (rest[2] != 't' && rest[2] != 'T')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700945 break;
Jeff Kinge19e5e62018-05-13 13:00:23 -0400946 if (rest[3] == '\0' || is_dir_sep(rest[3]))
947 return 0;
Jeff King10ecfa72018-05-04 20:03:35 -0400948 if (S_ISLNK(mode)) {
949 rest += 3;
950 if (skip_iprefix(rest, "modules", &rest) &&
951 (*rest == '\0' || is_dir_sep(*rest)))
952 return 0;
953 }
Jeff Kinge19e5e62018-05-13 13:00:23 -0400954 break;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700955 case '.':
Theo Niessinke0f530f2011-06-08 14:04:41 +0200956 if (rest[1] == '\0' || is_dir_sep(rest[1]))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700957 return 0;
958 }
959 return 1;
960}
961
Jeff King10ecfa72018-05-04 20:03:35 -0400962int verify_path(const char *path, unsigned mode)
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700963{
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000964 char c = 0;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700965
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200966 if (has_dos_drive_prefix(path))
967 return 0;
968
Johannes Schindelind2c84da2019-09-05 13:27:53 +0200969 if (!is_valid_path(path))
970 return 0;
971
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700972 goto inside;
973 for (;;) {
974 if (!c)
975 return 1;
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200976 if (is_dir_sep(c)) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700977inside:
Jeff King10ecfa72018-05-04 20:03:35 -0400978 if (protect_hfs) {
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000979
Jeff King10ecfa72018-05-04 20:03:35 -0400980 if (is_hfs_dotgit(path))
981 return 0;
982 if (S_ISLNK(mode)) {
983 if (is_hfs_dotgitmodules(path))
984 return 0;
985 }
986 }
987 if (protect_ntfs) {
Johannes Schindelin via GitGitGadget49e268e2020-01-09 13:30:34 +0000988#ifdef GIT_WINDOWS_NATIVE
989 if (c == '\\')
990 return 0;
991#endif
Jeff King10ecfa72018-05-04 20:03:35 -0400992 if (is_ntfs_dotgit(path))
993 return 0;
994 if (S_ISLNK(mode)) {
995 if (is_ntfs_dotgitmodules(path))
996 return 0;
997 }
998 }
999
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001000 c = *path++;
Jeff King10ecfa72018-05-04 20:03:35 -04001001 if ((c == '.' && !verify_dotfile(path, mode)) ||
Junio C Hamano3bdf09c2011-06-06 20:49:06 -07001002 is_dir_sep(c) || c == '\0')
1003 return 0;
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001004 } else if (c == '\\' && protect_ntfs) {
1005 if (is_ntfs_dotgit(path))
1006 return 0;
1007 if (S_ISLNK(mode)) {
1008 if (is_ntfs_dotgitmodules(path))
1009 return 0;
1010 }
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001011 }
Johannes Schindelin288a74b2019-09-23 08:58:11 +02001012
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001013 c = *path++;
1014 }
1015}
1016
1017/*
Linus Torvalds12676602005-06-18 20:21:34 -07001018 * Do we have another file that has the beginning components being a
1019 * proper superset of the name we're trying to add?
1020 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001021static int has_file_name(struct index_state *istate,
1022 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -07001023{
1024 int retval = 0;
1025 int len = ce_namelen(ce);
Junio C Hamanob1557252005-06-25 02:25:29 -07001026 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -07001027 const char *name = ce->name;
1028
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001029 while (pos < istate->cache_nr) {
1030 struct cache_entry *p = istate->cache[pos++];
Linus Torvalds12676602005-06-18 20:21:34 -07001031
1032 if (len >= ce_namelen(p))
1033 break;
1034 if (memcmp(name, p->name, len))
1035 break;
Junio C Hamanob1557252005-06-25 02:25:29 -07001036 if (ce_stage(p) != stage)
1037 continue;
Linus Torvalds12676602005-06-18 20:21:34 -07001038 if (p->name[len] != '/')
1039 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001040 if (p->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001041 continue;
Linus Torvalds12676602005-06-18 20:21:34 -07001042 retval = -1;
1043 if (!ok_to_replace)
1044 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001045 remove_index_entry_at(istate, --pos);
Linus Torvalds12676602005-06-18 20:21:34 -07001046 }
1047 return retval;
1048}
1049
Jeff Hostetlera6db3fb2017-04-14 19:12:28 +00001050
1051/*
1052 * Like strcmp(), but also return the offset of the first change.
1053 * If strings are equal, return the length.
1054 */
1055int strcmp_offset(const char *s1, const char *s2, size_t *first_change)
1056{
1057 size_t k;
1058
1059 if (!first_change)
1060 return strcmp(s1, s2);
1061
1062 for (k = 0; s1[k] == s2[k]; k++)
1063 if (s1[k] == '\0')
1064 break;
1065
1066 *first_change = k;
1067 return (unsigned char)s1[k] - (unsigned char)s2[k];
1068}
1069
Linus Torvalds12676602005-06-18 20:21:34 -07001070/*
1071 * Do we have another file with a pathname that is a proper
1072 * subset of the name we're trying to add?
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001073 *
1074 * That is, is there another file in the index with a path
1075 * that matches a sub-directory in the given entry?
Linus Torvalds12676602005-06-18 20:21:34 -07001076 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001077static int has_dir_name(struct index_state *istate,
1078 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -07001079{
1080 int retval = 0;
Junio C Hamanob1557252005-06-25 02:25:29 -07001081 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -07001082 const char *name = ce->name;
1083 const char *slash = name + ce_namelen(ce);
Jeff Hostetler06b6d812017-04-19 17:06:17 +00001084 size_t len_eq_last;
1085 int cmp_last = 0;
1086
1087 /*
1088 * We are frequently called during an iteration on a sorted
1089 * list of pathnames and while building a new index. Therefore,
1090 * there is a high probability that this entry will eventually
1091 * be appended to the index, rather than inserted in the middle.
1092 * If we can confirm that, we can avoid binary searches on the
1093 * components of the pathname.
1094 *
1095 * Compare the entry's full path with the last path in the index.
1096 */
1097 if (istate->cache_nr > 0) {
1098 cmp_last = strcmp_offset(name,
1099 istate->cache[istate->cache_nr - 1]->name,
1100 &len_eq_last);
1101 if (cmp_last > 0) {
1102 if (len_eq_last == 0) {
1103 /*
1104 * The entry sorts AFTER the last one in the
1105 * index and their paths have no common prefix,
1106 * so there cannot be a F/D conflict.
1107 */
1108 return retval;
1109 } else {
1110 /*
1111 * The entry sorts AFTER the last one in the
1112 * index, but has a common prefix. Fall through
1113 * to the loop below to disect the entry's path
1114 * and see where the difference is.
1115 */
1116 }
1117 } else if (cmp_last == 0) {
1118 /*
1119 * The entry exactly matches the last one in the
1120 * index, but because of multiple stage and CE_REMOVE
1121 * items, we fall through and let the regular search
1122 * code handle it.
1123 */
1124 }
1125 }
Linus Torvalds12676602005-06-18 20:21:34 -07001126
1127 for (;;) {
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001128 size_t len;
Linus Torvalds12676602005-06-18 20:21:34 -07001129
1130 for (;;) {
1131 if (*--slash == '/')
1132 break;
1133 if (slash <= ce->name)
1134 return retval;
1135 }
1136 len = slash - name;
1137
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001138 if (cmp_last > 0) {
1139 /*
1140 * (len + 1) is a directory boundary (including
1141 * the trailing slash). And since the loop is
1142 * decrementing "slash", the first iteration is
1143 * the longest directory prefix; subsequent
1144 * iterations consider parent directories.
1145 */
1146
1147 if (len + 1 <= len_eq_last) {
1148 /*
1149 * The directory prefix (including the trailing
1150 * slash) also appears as a prefix in the last
1151 * entry, so the remainder cannot collide (because
1152 * strcmp said the whole path was greater).
1153 *
1154 * EQ: last: xxx/A
1155 * this: xxx/B
1156 *
1157 * LT: last: xxx/file_A
1158 * this: xxx/file_B
1159 */
1160 return retval;
1161 }
1162
1163 if (len > len_eq_last) {
1164 /*
1165 * This part of the directory prefix (excluding
1166 * the trailing slash) is longer than the known
1167 * equal portions, so this sub-directory cannot
1168 * collide with a file.
1169 *
1170 * GT: last: xxxA
1171 * this: xxxB/file
1172 */
1173 return retval;
1174 }
1175
Jeff Hostetlerb986df52017-04-19 17:06:18 +00001176 /*
1177 * This is a possible collision. Fall through and
1178 * let the regular search code handle it.
1179 *
1180 * last: xxx
1181 * this: xxx/file
1182 */
1183 }
1184
Thomas Gummererb60e1882012-07-11 11:22:37 +02001185 pos = index_name_stage_pos(istate, name, len, stage);
Linus Torvalds12676602005-06-18 20:21:34 -07001186 if (pos >= 0) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001187 /*
1188 * Found one, but not so fast. This could
1189 * be a marker that says "I was here, but
1190 * I am being removed". Such an entry is
1191 * not a part of the resulting tree, and
1192 * it is Ok to have a directory at the same
1193 * path.
1194 */
Junio C Hamano077c48d2008-01-22 21:24:21 -08001195 if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001196 retval = -1;
1197 if (!ok_to_replace)
1198 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001199 remove_index_entry_at(istate, pos);
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001200 continue;
1201 }
Linus Torvalds12676602005-06-18 20:21:34 -07001202 }
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001203 else
1204 pos = -pos-1;
Linus Torvalds12676602005-06-18 20:21:34 -07001205
1206 /*
1207 * Trivial optimization: if we find an entry that
1208 * already matches the sub-directory, then we know
Junio C Hamanob1557252005-06-25 02:25:29 -07001209 * we're ok, and we can exit.
Linus Torvalds12676602005-06-18 20:21:34 -07001210 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001211 while (pos < istate->cache_nr) {
1212 struct cache_entry *p = istate->cache[pos];
Junio C Hamanob1557252005-06-25 02:25:29 -07001213 if ((ce_namelen(p) <= len) ||
1214 (p->name[len] != '/') ||
1215 memcmp(p->name, name, len))
1216 break; /* not our subdirectory */
Junio C Hamano077c48d2008-01-22 21:24:21 -08001217 if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE))
1218 /*
1219 * p is at the same stage as our entry, and
Junio C Hamanob1557252005-06-25 02:25:29 -07001220 * is a subdirectory of what we are looking
1221 * at, so we cannot have conflicts at our
1222 * level or anything shorter.
1223 */
1224 return retval;
1225 pos++;
Linus Torvalds12676602005-06-18 20:21:34 -07001226 }
1227 }
1228 return retval;
1229}
1230
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001231/* We may be in a situation where we already have path/file and path
1232 * is being added, or we already have path and path/file is being
1233 * added. Either one would result in a nonsense tree that has path
1234 * twice when git-write-tree tries to write it out. Prevent it.
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001235 *
Junio C Hamano192268c2005-05-07 21:55:21 -07001236 * If ok-to-replace is specified, we remove the conflicting entries
1237 * from the cache so the caller should recompute the insert position.
1238 * When this happens, we return non-zero.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001239 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001240static int check_file_directory_conflict(struct index_state *istate,
1241 const struct cache_entry *ce,
1242 int pos, int ok_to_replace)
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001243{
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001244 int retval;
1245
1246 /*
1247 * When ce is an "I am going away" entry, we allow it to be added
1248 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001249 if (ce->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001250 return 0;
1251
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001252 /*
Linus Torvalds12676602005-06-18 20:21:34 -07001253 * We check if the path is a sub-path of a subsequent pathname
1254 * first, since removing those will not change the position
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001255 * in the array.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001256 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001257 retval = has_file_name(istate, ce, pos, ok_to_replace);
Junio C Hamano21cd8d02007-03-30 01:55:37 -07001258
Linus Torvalds12676602005-06-18 20:21:34 -07001259 /*
1260 * Then check if the path might have a clashing sub-directory
1261 * before it.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001262 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001263 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001264}
1265
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001266static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001267{
1268 int pos;
Junio C Hamano192268c2005-05-07 21:55:21 -07001269 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
1270 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
Junio C Hamanob1557252005-06-25 02:25:29 -07001271 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
Junio C Hamano39425812008-08-21 01:44:53 -07001272 int new_only = option & ADD_CACHE_NEW_ONLY;
Junio C Hamano5f730762006-02-08 21:15:24 -08001273
Nguyễn Thái Ngọc Duyce7c6142014-06-13 19:19:42 +07001274 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1275 cache_tree_invalidate_path(istate, ce->name);
Jeff Hostetlere5494632017-04-19 17:06:16 +00001276
1277 /*
1278 * If this entry's path sorts after the last entry in the index,
1279 * we can avoid searching for it.
1280 */
1281 if (istate->cache_nr > 0 &&
1282 strcmp(ce->name, istate->cache[istate->cache_nr - 1]->name) > 0)
Johannes Schindelinc097b952019-10-04 08:09:26 -07001283 pos = index_pos_to_insert_pos(istate->cache_nr);
Jeff Hostetlere5494632017-04-19 17:06:16 +00001284 else
1285 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001286
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001287 /* existing match? Just replace it. */
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001288 if (pos >= 0) {
Junio C Hamano39425812008-08-21 01:44:53 -07001289 if (!new_only)
1290 replace_index_entry(istate, pos, ce);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001291 return 0;
1292 }
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001293 pos = -pos-1;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001294
Nguyễn Thái Ngọc Duyffcc9ba2015-06-07 17:40:52 +07001295 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1296 untracked_cache_add_to_index(istate, ce->name);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07001297
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001298 /*
1299 * Inserting a merged entry ("stage 0") into the index
1300 * will always replace all non-merged entries..
1301 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001302 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
1303 while (ce_same_name(istate->cache[pos], ce)) {
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001304 ok_to_add = 1;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001305 if (!remove_index_entry_at(istate, pos))
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001306 break;
1307 }
1308 }
1309
Linus Torvalds121481a2005-04-10 11:32:54 -07001310 if (!ok_to_add)
1311 return -1;
Jeff King10ecfa72018-05-04 20:03:35 -04001312 if (!verify_path(ce->name, ce->ce_mode))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001313 return error(_("invalid path '%s'"), ce->name);
Linus Torvalds121481a2005-04-10 11:32:54 -07001314
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001315 if (!skip_df_check &&
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001316 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
Junio C Hamano192268c2005-05-07 21:55:21 -07001317 if (!ok_to_replace)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001318 return error(_("'%s' appears as both a file and as a directory"),
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001319 ce->name);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001320 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
Junio C Hamano192268c2005-05-07 21:55:21 -07001321 pos = -pos-1;
1322 }
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001323 return pos + 1;
1324}
1325
1326int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
1327{
1328 int pos;
1329
1330 if (option & ADD_CACHE_JUST_APPEND)
1331 pos = istate->cache_nr;
1332 else {
1333 int ret;
1334 ret = add_index_entry_with_check(istate, ce, option);
1335 if (ret <= 0)
1336 return ret;
1337 pos = ret - 1;
1338 }
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001339
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001340 /* Make sure the array is big enough .. */
Dmitry S. Dolzhenko999f5662014-03-04 02:32:01 +04001341 ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001342
1343 /* Add it in.. */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001344 istate->cache_nr++;
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001345 if (istate->cache_nr > pos + 1)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +01001346 MOVE_ARRAY(istate->cache + pos + 1, istate->cache + pos,
1347 istate->cache_nr - pos - 1);
Linus Torvaldscf558702008-01-22 18:41:14 -08001348 set_index_entry(istate, pos, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001349 istate->cache_changed |= CE_ENTRY_ADDED;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001350 return 0;
1351}
1352
Linus Torvalds405e5b22006-05-19 09:56:35 -07001353/*
1354 * "refresh" does not calculate a new sha1 file or bring the
1355 * cache up-to-date for mode/content changes. But what it
1356 * _does_ do is to "re-match" the stat information of a file
1357 * with the cache, so that you can refresh the cache for a
1358 * file that hasn't been changed but where the stat entry is
1359 * out of date.
1360 *
1361 * For example, you'd want to do this after doing a "git-read-tree",
1362 * to link up the stat cache details with the proper files.
1363 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001364static struct cache_entry *refresh_cache_ent(struct index_state *istate,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001365 struct cache_entry *ce,
Jeff Kingd05e6972011-11-18 06:11:08 -05001366 unsigned int options, int *err,
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001367 int *changed_ret,
Jeff Hostetler15268d12021-02-03 15:34:46 +00001368 int *t2_did_lstat,
1369 int *t2_did_scan)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001370{
1371 struct stat st;
1372 struct cache_entry *updated;
Jameson Millera8497352018-07-02 19:49:31 +00001373 int changed;
Brad King25762722014-01-27 09:45:08 -05001374 int refresh = options & CE_MATCH_REFRESH;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001375 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001376 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Brad King2e2e7ec2014-01-27 09:45:07 -05001377 int ignore_missing = options & CE_MATCH_IGNORE_MISSING;
Ben Peart883e2482017-09-22 12:35:40 -04001378 int ignore_fsmonitor = options & CE_MATCH_IGNORE_FSMONITOR;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001379
Brad King25762722014-01-27 09:45:08 -05001380 if (!refresh || ce_uptodate(ce))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001381 return ce;
1382
Ben Peart883e2482017-09-22 12:35:40 -04001383 if (!ignore_fsmonitor)
1384 refresh_fsmonitor(istate);
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001385 /*
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001386 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1387 * that the change to the work tree does not matter and told
1388 * us not to worry.
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001389 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001390 if (!ignore_skip_worktree && ce_skip_worktree(ce)) {
1391 ce_mark_uptodate(ce);
1392 return ce;
1393 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001394 if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
1395 ce_mark_uptodate(ce);
1396 return ce;
1397 }
Ben Peart883e2482017-09-22 12:35:40 -04001398 if (!ignore_fsmonitor && (ce->ce_flags & CE_FSMONITOR_VALID)) {
1399 ce_mark_uptodate(ce);
1400 return ce;
1401 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001402
René Scharfeccad42d2014-08-09 19:43:29 +02001403 if (has_symlink_leading_path(ce->name, ce_namelen(ce))) {
1404 if (ignore_missing)
1405 return ce;
1406 if (err)
1407 *err = ENOENT;
1408 return NULL;
1409 }
1410
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001411 if (t2_did_lstat)
1412 *t2_did_lstat = 1;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001413 if (lstat(ce->name, &st) < 0) {
Brad King2e2e7ec2014-01-27 09:45:07 -05001414 if (ignore_missing && errno == ENOENT)
1415 return ce;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001416 if (err)
1417 *err = errno;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001418 return NULL;
1419 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001420
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001421 changed = ie_match_stat(istate, ce, &st, options);
Jeff Kingd05e6972011-11-18 06:11:08 -05001422 if (changed_ret)
1423 *changed_ret = changed;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001424 if (!changed) {
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001425 /*
1426 * The path is unchanged. If we were told to ignore
1427 * valid bit, then we did the actual stat check and
1428 * found that the entry is unmodified. If the entry
1429 * is not marked VALID, this is the place to mark it
1430 * valid again, under "assume unchanged" mode.
1431 */
1432 if (ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001433 !(ce->ce_flags & CE_VALID))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001434 ; /* mark this one VALID again */
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001435 else {
1436 /*
1437 * We do not mark the index itself "modified"
1438 * because CE_UPTODATE flag is in-core only;
1439 * we are not going to write this change out.
1440 */
Ben Peart883e2482017-09-22 12:35:40 -04001441 if (!S_ISGITLINK(ce->ce_mode)) {
Junio C Hamano125fd982010-01-24 00:10:20 -08001442 ce_mark_uptodate(ce);
Johannes Schindelinb5a81692019-05-24 05:23:48 -07001443 mark_fsmonitor_valid(istate, ce);
Ben Peart883e2482017-09-22 12:35:40 -04001444 }
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001445 return ce;
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001446 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001447 }
1448
Jeff Hostetler15268d12021-02-03 15:34:46 +00001449 if (t2_did_scan)
1450 *t2_did_scan = 1;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001451 if (ie_modified(istate, ce, &st, options)) {
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001452 if (err)
1453 *err = EINVAL;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001454 return NULL;
1455 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001456
Jameson Millera8497352018-07-02 19:49:31 +00001457 updated = make_empty_cache_entry(istate, ce_namelen(ce));
Ben Peart0e267b72018-03-15 11:25:20 -04001458 copy_cache_entry(updated, ce);
1459 memcpy(updated->name, ce->name, ce->ce_namelen + 1);
Johannes Schindelind4c0a3a2019-05-24 05:23:47 -07001460 fill_stat_cache_info(istate, updated, &st);
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001461 /*
1462 * If ignore_valid is not set, we should leave CE_VALID bit
1463 * alone. Otherwise, paths marked with --no-assume-unchanged
1464 * (i.e. things to be edited) will reacquire CE_VALID bit
1465 * automatically, which is not really what we want.
Linus Torvalds405e5b22006-05-19 09:56:35 -07001466 */
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001467 if (!ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001468 !(ce->ce_flags & CE_VALID))
1469 updated->ce_flags &= ~CE_VALID;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001470
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001471 /* istate->cache_changed is updated in the caller */
Linus Torvalds405e5b22006-05-19 09:56:35 -07001472 return updated;
1473}
1474
Matthieu Moy3deffc52009-08-21 10:57:59 +02001475static void show_file(const char * fmt, const char * name, int in_porcelain,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001476 int * first, const char *header_msg)
Matthieu Moy3deffc52009-08-21 10:57:59 +02001477{
1478 if (in_porcelain && *first && header_msg) {
1479 printf("%s\n", header_msg);
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -07001480 *first = 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001481 }
1482 printf(fmt, name);
1483}
1484
Thomas Gummerer22184492019-09-11 19:20:25 +01001485int repo_refresh_and_write_index(struct repository *repo,
1486 unsigned int refresh_flags,
1487 unsigned int write_flags,
1488 int gentle,
1489 const struct pathspec *pathspec,
1490 char *seen, const char *header_msg)
1491{
1492 struct lock_file lock_file = LOCK_INIT;
1493 int fd, ret = 0;
1494
1495 fd = repo_hold_locked_index(repo, &lock_file, 0);
1496 if (!gentle && fd < 0)
1497 return -1;
1498 if (refresh_index(repo->index, refresh_flags, pathspec, seen, header_msg))
1499 ret = 1;
1500 if (0 <= fd && write_locked_index(repo->index, &lock_file, COMMIT_LOCK | write_flags))
1501 ret = -1;
1502 return ret;
1503}
1504
1505
Nguyễn Thái Ngọc Duy9b2d6142013-07-14 15:35:54 +07001506int refresh_index(struct index_state *istate, unsigned int flags,
1507 const struct pathspec *pathspec,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001508 char *seen, const char *header_msg)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001509{
1510 int i;
1511 int has_errors = 0;
1512 int really = (flags & REFRESH_REALLY) != 0;
1513 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
1514 int quiet = (flags & REFRESH_QUIET) != 0;
1515 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001516 int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001517 int first = 1;
1518 int in_porcelain = (flags & REFRESH_IN_PORCELAIN);
Brad King25762722014-01-27 09:45:08 -05001519 unsigned int options = (CE_MATCH_REFRESH |
1520 (really ? CE_MATCH_IGNORE_VALID : 0) |
Brad King2e2e7ec2014-01-27 09:45:07 -05001521 (not_new ? CE_MATCH_IGNORE_MISSING : 0));
Jeff King4bd4e732011-11-18 06:11:28 -05001522 const char *modified_fmt;
Jeff King73b7eae2011-11-18 06:13:08 -05001523 const char *deleted_fmt;
1524 const char *typechange_fmt;
1525 const char *added_fmt;
Jeff King4bd4e732011-11-18 06:11:28 -05001526 const char *unmerged_fmt;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001527 struct progress *progress = NULL;
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001528 int t2_sum_lstat = 0;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001529 int t2_sum_scan = 0;
Nguyễn Thái Ngọc Duyae9af122018-09-15 19:56:04 +02001530
1531 if (flags & REFRESH_PROGRESS && isatty(2))
1532 progress = start_delayed_progress(_("Refresh index"),
1533 istate->cache_nr);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001534
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02001535 trace_performance_enter();
Nguyễn Thái Ngọc Duya71806a2018-11-10 06:16:06 +01001536 modified_fmt = in_porcelain ? "M\t%s\n" : "%s: needs update\n";
1537 deleted_fmt = in_porcelain ? "D\t%s\n" : "%s: needs update\n";
1538 typechange_fmt = in_porcelain ? "T\t%s\n" : "%s: needs update\n";
1539 added_fmt = in_porcelain ? "A\t%s\n" : "%s: needs update\n";
1540 unmerged_fmt = in_porcelain ? "U\t%s\n" : "%s: needs merge\n";
Ben Peart99ce7202018-10-29 16:41:59 -04001541 /*
1542 * Use the multi-threaded preload_index() to refresh most of the
1543 * cache entries quickly then in the single threaded loop below,
1544 * we only have to do the special cases that are left.
1545 */
1546 preload_index(istate, pathspec, 0);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001547 trace2_region_enter("index", "refresh", NULL);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001548 for (i = 0; i < istate->cache_nr; i++) {
Brandon Williams285c2e22018-02-14 10:59:45 -08001549 struct cache_entry *ce, *new_entry;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001550 int cache_errno = 0;
Jeff King73b7eae2011-11-18 06:13:08 -05001551 int changed = 0;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001552 int filtered = 0;
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001553 int t2_did_lstat = 0;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001554 int t2_did_scan = 0;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001555
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001556 ce = istate->cache[i];
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001557 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1558 continue;
1559
Nguyễn Thái Ngọc Duyd7b665c2018-09-21 17:57:25 +02001560 if (pathspec && !ce_path_match(istate, ce, pathspec, seen))
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001561 filtered = 1;
1562
Linus Torvalds405e5b22006-05-19 09:56:35 -07001563 if (ce_stage(ce)) {
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001564 while ((i < istate->cache_nr) &&
1565 ! strcmp(istate->cache[i]->name, ce->name))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001566 i++;
1567 i--;
1568 if (allow_unmerged)
1569 continue;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001570 if (!filtered)
1571 show_file(unmerged_fmt, ce->name, in_porcelain,
1572 &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001573 has_errors = 1;
1574 continue;
1575 }
1576
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001577 if (filtered)
Alexandre Julliardd6168132007-08-11 23:59:01 +02001578 continue;
1579
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001580 new_entry = refresh_cache_ent(istate, ce, options,
1581 &cache_errno, &changed,
Jeff Hostetler15268d12021-02-03 15:34:46 +00001582 &t2_did_lstat, &t2_did_scan);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001583 t2_sum_lstat += t2_did_lstat;
Jeff Hostetler15268d12021-02-03 15:34:46 +00001584 t2_sum_scan += t2_did_scan;
Brandon Williams285c2e22018-02-14 10:59:45 -08001585 if (new_entry == ce)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001586 continue;
Ævar Arnfjörð Bjarmasonb7b793d2021-06-07 16:43:22 +02001587 display_progress(progress, i);
Brandon Williams285c2e22018-02-14 10:59:45 -08001588 if (!new_entry) {
Jeff King73b7eae2011-11-18 06:13:08 -05001589 const char *fmt;
1590
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001591 if (really && cache_errno == EINVAL) {
Linus Torvalds405e5b22006-05-19 09:56:35 -07001592 /* If we are doing --really-refresh that
1593 * means the index is not valid anymore.
1594 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001595 ce->ce_flags &= ~CE_VALID;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +07001596 ce->ce_flags |= CE_UPDATE_IN_BASE;
Ben Peart883e2482017-09-22 12:35:40 -04001597 mark_fsmonitor_invalid(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001598 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001599 }
1600 if (quiet)
1601 continue;
Jeff King73b7eae2011-11-18 06:13:08 -05001602
1603 if (cache_errno == ENOENT)
1604 fmt = deleted_fmt;
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +07001605 else if (ce_intent_to_add(ce))
Jeff King73b7eae2011-11-18 06:13:08 -05001606 fmt = added_fmt; /* must be before other checks */
1607 else if (changed & TYPE_CHANGED)
1608 fmt = typechange_fmt;
1609 else
1610 fmt = modified_fmt;
1611 show_file(fmt,
1612 ce->name, in_porcelain, &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001613 has_errors = 1;
1614 continue;
1615 }
Linus Torvaldscf558702008-01-22 18:41:14 -08001616
Brandon Williams285c2e22018-02-14 10:59:45 -08001617 replace_index_entry(istate, i, new_entry);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001618 }
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001619 trace2_data_intmax("index", NULL, "refresh/sum_lstat", t2_sum_lstat);
Jeff Hostetler15268d12021-02-03 15:34:46 +00001620 trace2_data_intmax("index", NULL, "refresh/sum_scan", t2_sum_scan);
Jeff Hostetlera98e0f22021-02-03 15:34:45 +00001621 trace2_region_leave("index", "refresh", NULL);
Ævar Arnfjörð Bjarmasonb7b793d2021-06-07 16:43:22 +02001622 display_progress(progress, istate->cache_nr);
1623 stop_progress(&progress);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02001624 trace_performance_leave("refresh index");
Linus Torvalds405e5b22006-05-19 09:56:35 -07001625 return has_errors;
1626}
1627
Jameson Miller768d7962018-07-02 19:49:29 +00001628struct cache_entry *refresh_cache_entry(struct index_state *istate,
1629 struct cache_entry *ce,
1630 unsigned int options)
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001631{
Jeff Hostetler15268d12021-02-03 15:34:46 +00001632 return refresh_cache_ent(istate, ce, options, NULL, NULL, NULL, NULL);
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001633}
1634
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001635
1636/*****************************************************************
1637 * Index File I/O
1638 *****************************************************************/
1639
Junio C Hamano9d227782012-04-04 09:12:43 -07001640#define INDEX_FORMAT_DEFAULT 3
1641
Derrick Stolee7211b9e2019-08-13 11:37:43 -07001642static unsigned int get_index_format_default(struct repository *r)
Thomas Gummerer136347d2014-02-23 21:49:57 +01001643{
1644 char *envversion = getenv("GIT_INDEX_VERSION");
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001645 char *endp;
1646 unsigned int version = INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001647
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001648 if (!envversion) {
Derrick Stolee7211b9e2019-08-13 11:37:43 -07001649 prepare_repo_settings(r);
1650
1651 if (r->settings.index_version >= 0)
1652 version = r->settings.index_version;
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001653 if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1654 warning(_("index.version set, but the value is invalid.\n"
Thomas Gummerer136347d2014-02-23 21:49:57 +01001655 "Using version %i"), INDEX_FORMAT_DEFAULT);
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001656 return INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001657 }
1658 return version;
1659 }
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001660
1661 version = strtoul(envversion, &endp, 10);
1662 if (*endp ||
1663 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1664 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1665 "Using version %i"), INDEX_FORMAT_DEFAULT);
1666 version = INDEX_FORMAT_DEFAULT;
1667 }
1668 return version;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001669}
1670
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001671/*
1672 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1673 * Again - this is just a (very strong in practice) heuristic that
1674 * the inode hasn't changed.
1675 *
1676 * We save the fields in big-endian order to allow using the
1677 * index file over NFS transparently.
1678 */
1679struct ondisk_cache_entry {
1680 struct cache_time ctime;
1681 struct cache_time mtime;
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001682 uint32_t dev;
1683 uint32_t ino;
1684 uint32_t mode;
1685 uint32_t uid;
1686 uint32_t gid;
1687 uint32_t size;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001688 /*
1689 * unsigned char hash[hashsz];
1690 * uint16_t flags;
1691 * if (flags & CE_EXTENDED)
1692 * uint16_t flags2;
1693 */
1694 unsigned char data[GIT_MAX_RAWSZ + 2 * sizeof(uint16_t)];
1695 char name[FLEX_ARRAY];
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001696};
1697
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001698/* These are only used for v3 or lower */
Kevin Willfordce012de2017-08-21 15:24:32 -06001699#define align_padding_size(size, len) ((size + (len) + 8) & ~7) - (size + len)
brian m. carlson575fa8a2019-02-19 00:05:24 +00001700#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,data) + (len) + 8) & ~7)
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001701#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
brian m. carlson575fa8a2019-02-19 00:05:24 +00001702#define ondisk_data_size(flags, len) (the_hash_algo->rawsz + \
1703 ((flags & CE_EXTENDED) ? 2 : 1) * sizeof(uint16_t) + len)
1704#define ondisk_data_size_max(len) (ondisk_data_size(CE_EXTENDED, len))
1705#define ondisk_ce_size(ce) (ondisk_cache_entry_size(ondisk_data_size((ce)->ce_flags, ce_namelen(ce))))
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001706
Jeff Hostetlera33fc722017-04-14 20:32:21 +00001707/* Allow fsck to force verification of the index checksum. */
1708int verify_index_checksum;
1709
Ben Peart00ec50e2017-10-18 10:27:25 -04001710/* Allow fsck to force verification of the cache entry order. */
1711int verify_ce_order;
1712
Ben Peart371ed0d2018-10-10 11:59:33 -04001713static int verify_hdr(const struct cache_header *hdr, unsigned long size)
Linus Torvaldse83c5162005-04-07 15:13:13 -07001714{
brian m. carlsonaab61352018-02-01 02:18:45 +00001715 git_hash_ctx c;
1716 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano0136bac2012-04-03 15:53:12 -07001717 int hdr_version;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001718
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07001719 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001720 return error(_("bad signature 0x%08x"), hdr->hdr_signature);
Junio C Hamano0136bac2012-04-03 15:53:12 -07001721 hdr_version = ntohl(hdr->hdr_version);
Nguyễn Thái Ngọc Duyb82a7b52013-02-22 19:09:24 +07001722 if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001723 return error(_("bad index version %d"), hdr_version);
Jeff Hostetlera33fc722017-04-14 20:32:21 +00001724
1725 if (!verify_index_checksum)
1726 return 0;
1727
brian m. carlsonaab61352018-02-01 02:18:45 +00001728 the_hash_algo->init_fn(&c);
1729 the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz);
1730 the_hash_algo->final_fn(hash, &c);
Jeff King67947c32018-08-28 17:22:52 -04001731 if (!hasheq(hash, (unsigned char *)hdr + size - the_hash_algo->rawsz))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001732 return error(_("bad index file sha1 signature"));
Linus Torvaldse83c5162005-04-07 15:13:13 -07001733 return 0;
1734}
1735
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001736static int read_index_extension(struct index_state *istate,
Ben Peart371ed0d2018-10-10 11:59:33 -04001737 const char *ext, const char *data, unsigned long sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001738{
1739 switch (CACHE_EXT(ext)) {
1740 case CACHE_EXT_TREE:
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001741 istate->cache_tree = cache_tree_read(data, sz);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001742 break;
Junio C Hamanocfc57892009-12-25 00:30:51 -08001743 case CACHE_EXT_RESOLVE_UNDO:
1744 istate->resolve_undo = resolve_undo_read(data, sz);
1745 break;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001746 case CACHE_EXT_LINK:
1747 if (read_link_extension(istate, data, sz))
1748 return -1;
1749 break;
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07001750 case CACHE_EXT_UNTRACKED:
1751 istate->untracked = read_untracked_extension(data, sz);
1752 break;
Ben Peart883e2482017-09-22 12:35:40 -04001753 case CACHE_EXT_FSMONITOR:
1754 read_fsmonitor_extension(istate, data, sz);
1755 break;
Ben Peart3b1d9e02018-10-10 11:59:34 -04001756 case CACHE_EXT_ENDOFINDEXENTRIES:
Ben Peart32550892018-10-10 11:59:37 -04001757 case CACHE_EXT_INDEXENTRYOFFSETTABLE:
Ben Peart3b1d9e02018-10-10 11:59:34 -04001758 /* already handled in do_read_index() */
1759 break;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001760 default:
1761 if (*ext < 'A' || 'Z' < *ext)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001762 return error(_("index uses %.4s extension, which we do not understand"),
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001763 ext);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001764 fprintf_ln(stderr, _("ignoring %.4s extension"), ext);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001765 break;
1766 }
1767 return 0;
1768}
1769
Ben Peart77ff1122018-10-10 11:59:38 -04001770static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool,
1771 unsigned int version,
Jameson Millera8497352018-07-02 19:49:31 +00001772 struct ondisk_cache_entry *ondisk,
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001773 unsigned long *ent_size,
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001774 const struct cache_entry *previous_ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001775{
René Scharfedebed2a2011-10-24 23:59:14 +02001776 struct cache_entry *ce;
Junio C Hamano7fec10b2008-01-18 23:42:00 -08001777 size_t len;
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07001778 const char *name;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001779 const unsigned hashsz = the_hash_algo->rawsz;
1780 const uint16_t *flagsp = (const uint16_t *)(ondisk->data + hashsz);
René Scharfedebed2a2011-10-24 23:59:14 +02001781 unsigned int flags;
Nguyễn Thái Ngọc Duyf5c4a9a2018-11-03 09:48:49 +01001782 size_t copy_len = 0;
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001783 /*
1784 * Adjacent cache entries tend to share the leading paths, so it makes
1785 * sense to only store the differences in later entries. In the v4
1786 * on-disk format of the index, each on-disk cache entry stores the
1787 * number of bytes to be stripped from the end of the previous name,
1788 * and the bytes to append to the result, to come up with its name.
1789 */
Ben Peart77ff1122018-10-10 11:59:38 -04001790 int expand_name_field = version == 4;
René Scharfedebed2a2011-10-24 23:59:14 +02001791
1792 /* On-disk flags are just 16 bits */
brian m. carlson575fa8a2019-02-19 00:05:24 +00001793 flags = get_be16(flagsp);
René Scharfedebed2a2011-10-24 23:59:14 +02001794 len = flags & CE_NAMEMASK;
1795
1796 if (flags & CE_EXTENDED) {
René Scharfedebed2a2011-10-24 23:59:14 +02001797 int extended_flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001798 extended_flags = get_be16(flagsp + 1) << 16;
René Scharfedebed2a2011-10-24 23:59:14 +02001799 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1800 if (extended_flags & ~CE_EXTENDED_FLAGS)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001801 die(_("unknown index entry format 0x%08x"), extended_flags);
René Scharfedebed2a2011-10-24 23:59:14 +02001802 flags |= extended_flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001803 name = (const char *)(flagsp + 2);
René Scharfedebed2a2011-10-24 23:59:14 +02001804 }
1805 else
brian m. carlson575fa8a2019-02-19 00:05:24 +00001806 name = (const char *)(flagsp + 1);
René Scharfedebed2a2011-10-24 23:59:14 +02001807
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001808 if (expand_name_field) {
1809 const unsigned char *cp = (const unsigned char *)name;
1810 size_t strip_len, previous_len;
René Scharfedebed2a2011-10-24 23:59:14 +02001811
Elijah Newren15beaaa2019-11-05 17:07:23 +00001812 /* If we're at the beginning of a block, ignore the previous name */
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001813 strip_len = decode_varint(&cp);
Ben Peart77ff1122018-10-10 11:59:38 -04001814 if (previous_ce) {
1815 previous_len = previous_ce->ce_namelen;
1816 if (previous_len < strip_len)
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001817 die(_("malformed name field in the index, near path '%s'"),
Ben Peart77ff1122018-10-10 11:59:38 -04001818 previous_ce->name);
1819 copy_len = previous_len - strip_len;
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001820 }
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001821 name = (const char *)cp;
1822 }
1823
1824 if (len == CE_NAMEMASK) {
1825 len = strlen(name);
1826 if (expand_name_field)
1827 len += copy_len;
1828 }
1829
Ben Peart77ff1122018-10-10 11:59:38 -04001830 ce = mem_pool__ce_alloc(ce_mem_pool, len);
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001831
1832 ce->ce_stat_data.sd_ctime.sec = get_be32(&ondisk->ctime.sec);
1833 ce->ce_stat_data.sd_mtime.sec = get_be32(&ondisk->mtime.sec);
1834 ce->ce_stat_data.sd_ctime.nsec = get_be32(&ondisk->ctime.nsec);
1835 ce->ce_stat_data.sd_mtime.nsec = get_be32(&ondisk->mtime.nsec);
1836 ce->ce_stat_data.sd_dev = get_be32(&ondisk->dev);
1837 ce->ce_stat_data.sd_ino = get_be32(&ondisk->ino);
1838 ce->ce_mode = get_be32(&ondisk->mode);
1839 ce->ce_stat_data.sd_uid = get_be32(&ondisk->uid);
1840 ce->ce_stat_data.sd_gid = get_be32(&ondisk->gid);
1841 ce->ce_stat_data.sd_size = get_be32(&ondisk->size);
1842 ce->ce_flags = flags & ~CE_NAMEMASK;
1843 ce->ce_namelen = len;
1844 ce->index = 0;
brian m. carlson575fa8a2019-02-19 00:05:24 +00001845 hashcpy(ce->oid.hash, ondisk->data);
1846 memcpy(ce->name, name, len);
1847 ce->name[len] = '\0';
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001848
1849 if (expand_name_field) {
1850 if (copy_len)
1851 memcpy(ce->name, previous_ce->name, copy_len);
1852 memcpy(ce->name + copy_len, name, len + 1 - copy_len);
1853 *ent_size = (name - ((char *)ondisk)) + len + 1 - copy_len;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001854 } else {
Nguyễn Thái Ngọc Duy252d0792018-09-26 15:54:36 -04001855 memcpy(ce->name, name, len + 1);
1856 *ent_size = ondisk_ce_size(ce);
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001857 }
René Scharfedebed2a2011-10-24 23:59:14 +02001858 return ce;
Linus Torvaldscf558702008-01-22 18:41:14 -08001859}
1860
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001861static void check_ce_order(struct index_state *istate)
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001862{
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001863 unsigned int i;
1864
Ben Peart00ec50e2017-10-18 10:27:25 -04001865 if (!verify_ce_order)
1866 return;
1867
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001868 for (i = 1; i < istate->cache_nr; i++) {
1869 struct cache_entry *ce = istate->cache[i - 1];
1870 struct cache_entry *next_ce = istate->cache[i];
1871 int name_compare = strcmp(ce->name, next_ce->name);
1872
1873 if (0 < name_compare)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001874 die(_("unordered stage entries in index"));
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001875 if (!name_compare) {
1876 if (!ce_stage(ce))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001877 die(_("multiple stage entries for merged file '%s'"),
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001878 ce->name);
1879 if (ce_stage(ce) > ce_stage(next_ce))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01001880 die(_("unordered stage entries for '%s'"),
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001881 ce->name);
1882 }
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001883 }
1884}
1885
Christian Couder435ec092016-01-27 07:58:05 +01001886static void tweak_untracked_cache(struct index_state *istate)
1887{
Derrick Stoleead0fb652019-08-13 11:37:46 -07001888 struct repository *r = the_repository;
1889
1890 prepare_repo_settings(r);
1891
1892 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_REMOVE) {
Christian Couder435ec092016-01-27 07:58:05 +01001893 remove_untracked_cache(istate);
Derrick Stoleead0fb652019-08-13 11:37:46 -07001894 return;
Christian Couder435ec092016-01-27 07:58:05 +01001895 }
Derrick Stoleead0fb652019-08-13 11:37:46 -07001896
1897 if (r->settings.core_untracked_cache == UNTRACKED_CACHE_WRITE)
1898 add_untracked_cache(istate);
Christian Couder435ec092016-01-27 07:58:05 +01001899}
1900
Christian Couder43925312017-02-27 19:00:02 +01001901static void tweak_split_index(struct index_state *istate)
1902{
1903 switch (git_config_get_split_index()) {
1904 case -1: /* unset: do nothing */
1905 break;
1906 case 0: /* false */
1907 remove_split_index(istate);
1908 break;
1909 case 1: /* true */
1910 add_split_index(istate);
1911 break;
1912 default: /* unknown value: do nothing */
1913 break;
1914 }
1915}
1916
Christian Couder435ec092016-01-27 07:58:05 +01001917static void post_read_index_from(struct index_state *istate)
1918{
1919 check_ce_order(istate);
1920 tweak_untracked_cache(istate);
Christian Couder43925312017-02-27 19:00:02 +01001921 tweak_split_index(istate);
Ben Peart883e2482017-09-22 12:35:40 -04001922 tweak_fsmonitor(istate);
Christian Couder435ec092016-01-27 07:58:05 +01001923}
1924
Jameson Miller8e72d672018-07-02 19:49:37 +00001925static size_t estimate_cache_size_from_compressed(unsigned int entries)
1926{
1927 return entries * (sizeof(struct cache_entry) + CACHE_ENTRY_PATH_LENGTH);
1928}
1929
1930static size_t estimate_cache_size(size_t ondisk_size, unsigned int entries)
1931{
1932 long per_entry = sizeof(struct cache_entry) - sizeof(struct ondisk_cache_entry);
1933
1934 /*
1935 * Account for potential alignment differences.
1936 */
Johannes Schindelinc097b952019-10-04 08:09:26 -07001937 per_entry += align_padding_size(per_entry, 0);
Jameson Miller8e72d672018-07-02 19:49:37 +00001938 return ondisk_size + entries * per_entry;
1939}
1940
Ben Peart32550892018-10-10 11:59:37 -04001941struct index_entry_offset
1942{
1943 /* starting byte offset into index file, count of index entries in this block */
1944 int offset, nr;
1945};
1946
1947struct index_entry_offset_table
1948{
1949 int nr;
1950 struct index_entry_offset entries[FLEX_ARRAY];
1951};
1952
Ben Peart32550892018-10-10 11:59:37 -04001953static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset);
1954static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot);
Ben Peart32550892018-10-10 11:59:37 -04001955
Ben Peart3b1d9e02018-10-10 11:59:34 -04001956static size_t read_eoie_extension(const char *mmap, size_t mmap_size);
1957static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset);
1958
Ben Peartabb4bb82018-10-10 11:59:36 -04001959struct load_index_extensions
1960{
Ben Peartabb4bb82018-10-10 11:59:36 -04001961 pthread_t pthread;
Ben Peartabb4bb82018-10-10 11:59:36 -04001962 struct index_state *istate;
1963 const char *mmap;
1964 size_t mmap_size;
1965 unsigned long src_offset;
1966};
1967
1968static void *load_index_extensions(void *_data)
1969{
1970 struct load_index_extensions *p = _data;
1971 unsigned long src_offset = p->src_offset;
1972
1973 while (src_offset <= p->mmap_size - the_hash_algo->rawsz - 8) {
1974 /* After an array of active_nr index entries,
1975 * there can be arbitrary number of extended
1976 * sections, each of which is prefixed with
1977 * extension name (4-byte) and section length
1978 * in 4-byte network byte order.
1979 */
1980 uint32_t extsize = get_be32(p->mmap + src_offset + 4);
1981 if (read_index_extension(p->istate,
1982 p->mmap + src_offset,
1983 p->mmap + src_offset + 8,
1984 extsize) < 0) {
1985 munmap((void *)p->mmap, p->mmap_size);
1986 die(_("index file corrupt"));
1987 }
1988 src_offset += 8;
1989 src_offset += extsize;
1990 }
1991
1992 return NULL;
1993}
1994
Ben Peart32550892018-10-10 11:59:37 -04001995/*
Ben Peart77ff1122018-10-10 11:59:38 -04001996 * A helper function that will load the specified range of cache entries
1997 * from the memory mapped file and add them to the given index.
1998 */
1999static unsigned long load_cache_entry_block(struct index_state *istate,
2000 struct mem_pool *ce_mem_pool, int offset, int nr, const char *mmap,
2001 unsigned long start_offset, const struct cache_entry *previous_ce)
2002{
2003 int i;
2004 unsigned long src_offset = start_offset;
2005
2006 for (i = offset; i < offset + nr; i++) {
2007 struct ondisk_cache_entry *disk_ce;
2008 struct cache_entry *ce;
2009 unsigned long consumed;
2010
2011 disk_ce = (struct ondisk_cache_entry *)(mmap + src_offset);
2012 ce = create_from_disk(ce_mem_pool, istate->version, disk_ce, &consumed, previous_ce);
2013 set_index_entry(istate, i, ce);
2014
2015 src_offset += consumed;
2016 previous_ce = ce;
2017 }
2018 return src_offset - start_offset;
2019}
2020
2021static unsigned long load_all_cache_entries(struct index_state *istate,
2022 const char *mmap, size_t mmap_size, unsigned long src_offset)
2023{
2024 unsigned long consumed;
2025
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002026 istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
Ben Peart77ff1122018-10-10 11:59:38 -04002027 if (istate->version == 4) {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002028 mem_pool_init(istate->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002029 estimate_cache_size_from_compressed(istate->cache_nr));
2030 } else {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002031 mem_pool_init(istate->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002032 estimate_cache_size(mmap_size, istate->cache_nr));
2033 }
2034
2035 consumed = load_cache_entry_block(istate, istate->ce_mem_pool,
2036 0, istate->cache_nr, mmap, src_offset, NULL);
2037 return consumed;
2038}
2039
Ben Peart77ff1122018-10-10 11:59:38 -04002040/*
Ben Peart32550892018-10-10 11:59:37 -04002041 * Mostly randomly chosen maximum thread counts: we
2042 * cap the parallelism to online_cpus() threads, and we want
2043 * to have at least 10000 cache entries per thread for it to
2044 * be worth starting a thread.
2045 */
2046
2047#define THREAD_COST (10000)
2048
Ben Peart77ff1122018-10-10 11:59:38 -04002049struct load_cache_entries_thread_data
2050{
2051 pthread_t pthread;
2052 struct index_state *istate;
2053 struct mem_pool *ce_mem_pool;
2054 int offset;
2055 const char *mmap;
2056 struct index_entry_offset_table *ieot;
2057 int ieot_start; /* starting index into the ieot array */
2058 int ieot_blocks; /* count of ieot entries to process */
2059 unsigned long consumed; /* return # of bytes in index file processed */
2060};
2061
2062/*
2063 * A thread proc to run the load_cache_entries() computation
2064 * across multiple background threads.
2065 */
2066static void *load_cache_entries_thread(void *_data)
2067{
2068 struct load_cache_entries_thread_data *p = _data;
2069 int i;
2070
2071 /* iterate across all ieot blocks assigned to this thread */
2072 for (i = p->ieot_start; i < p->ieot_start + p->ieot_blocks; i++) {
2073 p->consumed += load_cache_entry_block(p->istate, p->ce_mem_pool,
2074 p->offset, p->ieot->entries[i].nr, p->mmap, p->ieot->entries[i].offset, NULL);
2075 p->offset += p->ieot->entries[i].nr;
2076 }
2077 return NULL;
2078}
2079
2080static unsigned long load_cache_entries_threaded(struct index_state *istate, const char *mmap, size_t mmap_size,
Jeff King7bd96312019-05-09 17:29:44 -04002081 int nr_threads, struct index_entry_offset_table *ieot)
Ben Peart77ff1122018-10-10 11:59:38 -04002082{
2083 int i, offset, ieot_blocks, ieot_start, err;
2084 struct load_cache_entries_thread_data *data;
2085 unsigned long consumed = 0;
2086
2087 /* a little sanity checking */
2088 if (istate->name_hash_initialized)
2089 BUG("the name hash isn't thread safe");
2090
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002091 istate->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
2092 mem_pool_init(istate->ce_mem_pool, 0);
Ben Peart77ff1122018-10-10 11:59:38 -04002093
2094 /* ensure we have no more threads than we have blocks to process */
2095 if (nr_threads > ieot->nr)
2096 nr_threads = ieot->nr;
René Scharfeca56dad2021-03-13 17:17:22 +01002097 CALLOC_ARRAY(data, nr_threads);
Ben Peart77ff1122018-10-10 11:59:38 -04002098
2099 offset = ieot_start = 0;
2100 ieot_blocks = DIV_ROUND_UP(ieot->nr, nr_threads);
2101 for (i = 0; i < nr_threads; i++) {
2102 struct load_cache_entries_thread_data *p = &data[i];
2103 int nr, j;
2104
2105 if (ieot_start + ieot_blocks > ieot->nr)
2106 ieot_blocks = ieot->nr - ieot_start;
2107
2108 p->istate = istate;
2109 p->offset = offset;
2110 p->mmap = mmap;
2111 p->ieot = ieot;
2112 p->ieot_start = ieot_start;
2113 p->ieot_blocks = ieot_blocks;
2114
2115 /* create a mem_pool for each thread */
2116 nr = 0;
2117 for (j = p->ieot_start; j < p->ieot_start + p->ieot_blocks; j++)
2118 nr += p->ieot->entries[j].nr;
René Scharfebcd2c5e2020-09-04 19:33:55 +02002119 p->ce_mem_pool = xmalloc(sizeof(*istate->ce_mem_pool));
Ben Peart77ff1122018-10-10 11:59:38 -04002120 if (istate->version == 4) {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002121 mem_pool_init(p->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002122 estimate_cache_size_from_compressed(nr));
2123 } else {
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002124 mem_pool_init(p->ce_mem_pool,
Ben Peart77ff1122018-10-10 11:59:38 -04002125 estimate_cache_size(mmap_size, nr));
2126 }
2127
2128 err = pthread_create(&p->pthread, NULL, load_cache_entries_thread, p);
2129 if (err)
2130 die(_("unable to create load_cache_entries thread: %s"), strerror(err));
2131
2132 /* increment by the number of cache entries in the ieot block being processed */
2133 for (j = 0; j < ieot_blocks; j++)
2134 offset += ieot->entries[ieot_start + j].nr;
2135 ieot_start += ieot_blocks;
2136 }
2137
2138 for (i = 0; i < nr_threads; i++) {
2139 struct load_cache_entries_thread_data *p = &data[i];
2140
2141 err = pthread_join(p->pthread, NULL);
2142 if (err)
2143 die(_("unable to join load_cache_entries thread: %s"), strerror(err));
2144 mem_pool_combine(istate->ce_mem_pool, p->ce_mem_pool);
2145 consumed += p->consumed;
2146 }
2147
2148 free(data);
2149
2150 return consumed;
2151}
Ben Peart77ff1122018-10-10 11:59:38 -04002152
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002153/* remember to discard_cache() before reading a different cache! */
Nguyễn Thái Ngọc Duy3e52f702014-06-13 19:19:51 +07002154int do_read_index(struct index_state *istate, const char *path, int must_exist)
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002155{
Ben Peart77ff1122018-10-10 11:59:38 -04002156 int fd;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002157 struct stat st;
René Scharfedebed2a2011-10-24 23:59:14 +02002158 unsigned long src_offset;
Ben Peart371ed0d2018-10-10 11:59:33 -04002159 const struct cache_header *hdr;
2160 const char *mmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002161 size_t mmap_size;
Ben Peartabb4bb82018-10-10 11:59:36 -04002162 struct load_index_extensions p;
2163 size_t extension_offset = 0;
Ben Peart77ff1122018-10-10 11:59:38 -04002164 int nr_threads, cpus;
2165 struct index_entry_offset_table *ieot = NULL;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002166
Junio C Hamano913e0e92008-08-23 12:57:30 -07002167 if (istate->initialized)
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002168 return istate->cache_nr;
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002169
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002170 istate->timestamp.sec = 0;
2171 istate->timestamp.nsec = 0;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07002172 fd = open(path, O_RDONLY);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002173 if (fd < 0) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002174 if (!must_exist && errno == ENOENT)
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002175 return 0;
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002176 die_errno(_("%s: index file open failed"), path);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07002177 }
Linus Torvaldse83c5162005-04-07 15:13:13 -07002178
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002179 if (fstat(fd, &st))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002180 die_errno(_("%s: cannot stat the open index"), path);
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002181
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002182 mmap_size = xsize_t(st.st_size);
brian m. carlsonaab61352018-02-01 02:18:45 +00002183 if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002184 die(_("%s: index file smaller than expected"), path);
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03002185
Varun Naik02638d12019-07-13 20:01:53 -07002186 mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002187 if (mmap == MAP_FAILED)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002188 die_errno(_("%s: unable to map index file"), path);
Nguyễn Thái Ngọc Duy57d84f82012-08-06 18:27:09 +07002189 close(fd);
Linus Torvaldse83c5162005-04-07 15:13:13 -07002190
Ben Peart371ed0d2018-10-10 11:59:33 -04002191 hdr = (const struct cache_header *)mmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002192 if (verify_hdr(hdr, mmap_size) < 0)
Linus Torvaldse83c5162005-04-07 15:13:13 -07002193 goto unmap;
2194
brian m. carlson75691ea2018-05-02 00:25:44 +00002195 hashcpy(istate->oid.hash, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz);
Junio C Hamano9d227782012-04-04 09:12:43 -07002196 istate->version = ntohl(hdr->hdr_version);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002197 istate->cache_nr = ntohl(hdr->hdr_entries);
2198 istate->cache_alloc = alloc_nr(istate->cache_nr);
René Scharfeca56dad2021-03-13 17:17:22 +01002199 CALLOC_ARRAY(istate->cache, istate->cache_alloc);
Junio C Hamano913e0e92008-08-23 12:57:30 -07002200 istate->initialized = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002201
Ben Peartabb4bb82018-10-10 11:59:36 -04002202 p.istate = istate;
2203 p.mmap = mmap;
2204 p.mmap_size = mmap_size;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07002205
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002206 src_offset = sizeof(*hdr);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002207
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002208 if (git_config_get_index_threads(&nr_threads))
2209 nr_threads = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002210
Ben Peart77ff1122018-10-10 11:59:38 -04002211 /* TODO: does creating more threads than cores help? */
2212 if (!nr_threads) {
2213 nr_threads = istate->cache_nr / THREAD_COST;
2214 cpus = online_cpus();
2215 if (nr_threads > cpus)
2216 nr_threads = cpus;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002217 }
Ben Peartabb4bb82018-10-10 11:59:36 -04002218
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002219 if (!HAVE_THREADS)
2220 nr_threads = 1;
2221
Ben Peartabb4bb82018-10-10 11:59:36 -04002222 if (nr_threads > 1) {
2223 extension_offset = read_eoie_extension(mmap, mmap_size);
2224 if (extension_offset) {
2225 int err;
2226
2227 p.src_offset = extension_offset;
2228 err = pthread_create(&p.pthread, NULL, load_index_extensions, &p);
2229 if (err)
2230 die(_("unable to create load_index_extensions thread: %s"), strerror(err));
2231
2232 nr_threads--;
2233 }
2234 }
Ben Peart77ff1122018-10-10 11:59:38 -04002235
2236 /*
2237 * Locate and read the index entry offset table so that we can use it
2238 * to multi-thread the reading of the cache entries.
2239 */
2240 if (extension_offset && nr_threads > 1)
2241 ieot = read_ieot_extension(mmap, mmap_size, extension_offset);
2242
2243 if (ieot) {
Jeff King7bd96312019-05-09 17:29:44 -04002244 src_offset += load_cache_entries_threaded(istate, mmap, mmap_size, nr_threads, ieot);
Ben Peart77ff1122018-10-10 11:59:38 -04002245 free(ieot);
2246 } else {
2247 src_offset += load_all_cache_entries(istate, mmap, mmap_size, src_offset);
2248 }
Ben Peartabb4bb82018-10-10 11:59:36 -04002249
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002250 istate->timestamp.sec = st.st_mtime;
Kjetil Barvikc06ff492009-03-04 18:47:40 +01002251 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002252
Ben Peartabb4bb82018-10-10 11:59:36 -04002253 /* if we created a thread, join it otherwise load the extensions on the primary thread */
Ben Peartabb4bb82018-10-10 11:59:36 -04002254 if (extension_offset) {
2255 int ret = pthread_join(p.pthread, NULL);
2256 if (ret)
2257 die(_("unable to join load_index_extensions thread: %s"), strerror(ret));
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002258 } else {
Ben Peartabb4bb82018-10-10 11:59:36 -04002259 p.src_offset = src_offset;
2260 load_index_extensions(&p);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002261 }
Ben Peart371ed0d2018-10-10 11:59:33 -04002262 munmap((void *)mmap, mmap_size);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002263
2264 /*
2265 * TODO trace2: replace "the_repository" with the actual repo instance
2266 * that is associated with the given "istate".
2267 */
2268 trace2_data_intmax("index", the_repository, "read/version",
2269 istate->version);
2270 trace2_data_intmax("index", the_repository, "read/cache_nr",
2271 istate->cache_nr);
2272
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002273 return istate->cache_nr;
Linus Torvaldse83c5162005-04-07 15:13:13 -07002274
2275unmap:
Ben Peart371ed0d2018-10-10 11:59:33 -04002276 munmap((void *)mmap, mmap_size);
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002277 die(_("index file corrupt"));
Linus Torvaldse83c5162005-04-07 15:13:13 -07002278}
2279
Christian Couder0d59ffb2017-02-27 19:00:12 +01002280/*
2281 * Signal that the shared index is used by updating its mtime.
2282 *
2283 * This way, shared index can be removed if they have not been used
2284 * for some time.
2285 */
Thomas Gummerera125a222018-01-07 22:30:13 +00002286static void freshen_shared_index(const char *shared_index, int warn)
Christian Couder0d59ffb2017-02-27 19:00:12 +01002287{
Christian Couder0d59ffb2017-02-27 19:00:12 +01002288 if (!check_and_freshen_file(shared_index, 1) && warn)
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002289 warning(_("could not freshen shared index '%s'"), shared_index);
Christian Couder0d59ffb2017-02-27 19:00:12 +01002290}
2291
Thomas Gummerera125a222018-01-07 22:30:13 +00002292int read_index_from(struct index_state *istate, const char *path,
2293 const char *gitdir)
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002294{
2295 struct split_index *split_index;
2296 int ret;
brian m. carlson2182abd2018-05-02 00:25:43 +00002297 char *base_oid_hex;
Thomas Gummerera125a222018-01-07 22:30:13 +00002298 char *base_path;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002299
2300 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
2301 if (istate->initialized)
2302 return istate->cache_nr;
2303
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002304 /*
2305 * TODO trace2: replace "the_repository" with the actual repo instance
2306 * that is associated with the given "istate".
2307 */
2308 trace2_region_enter_printf("index", "do_read_index", the_repository,
2309 "%s", path);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002310 trace_performance_enter();
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002311 ret = do_read_index(istate, path, 0);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002312 trace_performance_leave("read cache %s", path);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002313 trace2_region_leave_printf("index", "do_read_index", the_repository,
2314 "%s", path);
Christian Couder435ec092016-01-27 07:58:05 +01002315
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002316 split_index = istate->split_index;
brian m. carlson2182abd2018-05-02 00:25:43 +00002317 if (!split_index || is_null_oid(&split_index->base_oid)) {
Christian Couder435ec092016-01-27 07:58:05 +01002318 post_read_index_from(istate);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002319 return ret;
Thomas Gummerer03f15a72015-03-20 22:43:14 +01002320 }
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002321
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002322 trace_performance_enter();
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002323 if (split_index->base)
2324 discard_index(split_index->base);
2325 else
René Scharfeca56dad2021-03-13 17:17:22 +01002326 CALLOC_ARRAY(split_index->base, 1);
Christian Couderde6ae5f2017-03-06 10:42:00 +01002327
brian m. carlson2182abd2018-05-02 00:25:43 +00002328 base_oid_hex = oid_to_hex(&split_index->base_oid);
2329 base_path = xstrfmt("%s/sharedindex.%s", gitdir, base_oid_hex);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002330 trace2_region_enter_printf("index", "shared/do_read_index",
2331 the_repository, "%s", base_path);
Christian Couderde6ae5f2017-03-06 10:42:00 +01002332 ret = do_read_index(split_index->base, base_path, 1);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08002333 trace2_region_leave_printf("index", "shared/do_read_index",
2334 the_repository, "%s", base_path);
Jeff King9001dc22018-08-28 17:22:48 -04002335 if (!oideq(&split_index->base_oid, &split_index->base->oid))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01002336 die(_("broken index, expect %s in %s, got %s"),
brian m. carlson2182abd2018-05-02 00:25:43 +00002337 base_oid_hex, base_path,
brian m. carlson75691ea2018-05-02 00:25:44 +00002338 oid_to_hex(&split_index->base->oid));
Christian Couderde6ae5f2017-03-06 10:42:00 +01002339
Thomas Gummerera125a222018-01-07 22:30:13 +00002340 freshen_shared_index(base_path, 0);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002341 merge_base_index(istate);
Christian Couder435ec092016-01-27 07:58:05 +01002342 post_read_index_from(istate);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +02002343 trace_performance_leave("read cache %s", base_path);
Carlo Marcelo Arenas Belónb42ad7d2018-10-20 00:33:34 -07002344 free(base_path);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002345 return ret;
2346}
2347
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08002348int is_index_unborn(struct index_state *istate)
2349{
René Scharfedebed2a2011-10-24 23:59:14 +02002350 return (!istate->cache_nr && !istate->timestamp.sec);
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08002351}
2352
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002353int discard_index(struct index_state *istate)
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002354{
Jameson Miller8e72d672018-07-02 19:49:37 +00002355 /*
2356 * Cache entries in istate->cache[] should have been allocated
2357 * from the memory pool associated with this index, or from an
2358 * associated split_index. There is no need to free individual
Jameson Miller8616a2d2018-07-02 19:49:39 +00002359 * cache entries. validate_cache_entries can detect when this
2360 * assertion does not hold.
Jameson Miller8e72d672018-07-02 19:49:37 +00002361 */
Jameson Miller8616a2d2018-07-02 19:49:39 +00002362 validate_cache_entries(istate);
René Scharfedebed2a2011-10-24 23:59:14 +02002363
Junio C Hamanocfc57892009-12-25 00:30:51 -08002364 resolve_undo_clear_index(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002365 istate->cache_nr = 0;
2366 istate->cache_changed = 0;
Kjetil Barvikfba2f382009-02-19 21:08:29 +01002367 istate->timestamp.sec = 0;
2368 istate->timestamp.nsec = 0;
Karsten Blees20926782013-02-28 00:57:48 +01002369 free_name_hash(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002370 cache_tree_free(&(istate->cache_tree));
Junio C Hamano913e0e92008-08-23 12:57:30 -07002371 istate->initialized = 0;
Johannes Schindelin398a3b02019-05-07 13:10:21 +02002372 istate->fsmonitor_has_run_once = 0;
Johannes Schindelin4abc5782021-03-17 15:30:49 +00002373 FREE_AND_NULL(istate->fsmonitor_last_update);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002374 FREE_AND_NULL(istate->cache);
René Scharfea0fc4db2013-06-09 19:39:18 +02002375 istate->cache_alloc = 0;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002376 discard_split_index(istate);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07002377 free_untracked_cache(istate->untracked);
2378 istate->untracked = NULL;
Jameson Miller8e72d672018-07-02 19:49:37 +00002379
2380 if (istate->ce_mem_pool) {
Jameson Miller8616a2d2018-07-02 19:49:39 +00002381 mem_pool_discard(istate->ce_mem_pool, should_validate_cache_entries());
Elijah Newren44c7e1a2020-08-15 17:37:56 +00002382 FREE_AND_NULL(istate->ce_mem_pool);
Jameson Miller8e72d672018-07-02 19:49:37 +00002383 }
2384
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002385 return 0;
Johannes Schindelin6d297f82006-07-08 18:42:41 +02002386}
2387
Jameson Miller8616a2d2018-07-02 19:49:39 +00002388/*
2389 * Validate the cache entries of this index.
2390 * All cache entries associated with this index
2391 * should have been allocated by the memory pool
2392 * associated with this index, or by a referenced
2393 * split index.
2394 */
2395void validate_cache_entries(const struct index_state *istate)
2396{
2397 int i;
2398
2399 if (!should_validate_cache_entries() ||!istate || !istate->initialized)
2400 return;
2401
2402 for (i = 0; i < istate->cache_nr; i++) {
2403 if (!istate) {
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +01002404 BUG("cache entry is not allocated from expected memory pool");
Jameson Miller8616a2d2018-07-02 19:49:39 +00002405 } else if (!istate->ce_mem_pool ||
2406 !mem_pool_contains(istate->ce_mem_pool, istate->cache[i])) {
2407 if (!istate->split_index ||
2408 !istate->split_index->base ||
2409 !istate->split_index->base->ce_mem_pool ||
2410 !mem_pool_contains(istate->split_index->base->ce_mem_pool, istate->cache[i])) {
Nguyễn Thái Ngọc Duy391408e2018-11-10 06:16:04 +01002411 BUG("cache entry is not allocated from expected memory pool");
Jameson Miller8616a2d2018-07-02 19:49:39 +00002412 }
2413 }
2414 }
2415
2416 if (istate->split_index)
2417 validate_cache_entries(istate->split_index->base);
2418}
2419
Linus Torvaldsd1f128b2008-03-06 12:46:09 -08002420int unmerged_index(const struct index_state *istate)
Daniel Barkalow94a57282008-02-07 11:40:13 -05002421{
2422 int i;
2423 for (i = 0; i < istate->cache_nr; i++) {
2424 if (ce_stage(istate->cache[i]))
2425 return 1;
2426 }
2427 return 0;
2428}
2429
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002430int repo_index_has_changes(struct repository *repo,
2431 struct tree *tree,
2432 struct strbuf *sb)
Elijah Newrencffbfad2018-06-30 18:24:55 -07002433{
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002434 struct index_state *istate = repo->index;
Elijah Newrene1f86942018-06-30 18:25:00 -07002435 struct object_id cmp;
Elijah Newrencffbfad2018-06-30 18:24:55 -07002436 int i;
2437
Elijah Newrene1f86942018-06-30 18:25:00 -07002438 if (tree)
2439 cmp = tree->object.oid;
2440 if (tree || !get_oid_tree("HEAD", &cmp)) {
Elijah Newrencffbfad2018-06-30 18:24:55 -07002441 struct diff_options opt;
2442
Nguyễn Thái Ngọc Duy150fe062019-01-12 09:13:31 +07002443 repo_diff_setup(repo, &opt);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002444 opt.flags.exit_with_status = 1;
2445 if (!sb)
2446 opt.flags.quick = 1;
Elijah Newrene1f86942018-06-30 18:25:00 -07002447 do_diff_cache(&cmp, &opt);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002448 diffcore_std(&opt);
2449 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
2450 if (i)
2451 strbuf_addch(sb, ' ');
2452 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
2453 }
2454 diff_flush(&opt);
2455 return opt.flags.has_changes != 0;
2456 } else {
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002457 for (i = 0; sb && i < istate->cache_nr; i++) {
Elijah Newrencffbfad2018-06-30 18:24:55 -07002458 if (i)
2459 strbuf_addch(sb, ' ');
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002460 strbuf_addstr(sb, istate->cache[i]->name);
Elijah Newrencffbfad2018-06-30 18:24:55 -07002461 }
Elijah Newren1b9fbef2018-06-30 18:24:56 -07002462 return !!istate->cache_nr;
Elijah Newrencffbfad2018-06-30 18:24:55 -07002463 }
2464}
2465
Neeraj Singhf2798942021-02-18 02:48:26 +00002466#define WRITE_BUFFER_SIZE (128 * 1024)
Brian Gerstbf0f9102005-05-18 08:14:09 -04002467static unsigned char write_buffer[WRITE_BUFFER_SIZE];
Linus Torvalds4990aad2005-04-20 12:16:57 -07002468static unsigned long write_buffer_len;
2469
brian m. carlsonaab61352018-02-01 02:18:45 +00002470static int ce_write_flush(git_hash_ctx *context, int fd)
Junio C Hamano6015c282006-08-08 14:47:32 -07002471{
2472 unsigned int buffered = write_buffer_len;
2473 if (buffered) {
brian m. carlsonaab61352018-02-01 02:18:45 +00002474 the_hash_algo->update_fn(context, write_buffer, buffered);
Jeff King06f46f22017-09-13 13:16:03 -04002475 if (write_in_full(fd, write_buffer, buffered) < 0)
Junio C Hamano6015c282006-08-08 14:47:32 -07002476 return -1;
2477 write_buffer_len = 0;
2478 }
2479 return 0;
2480}
2481
brian m. carlsonaab61352018-02-01 02:18:45 +00002482static int ce_write(git_hash_ctx *context, int fd, void *data, unsigned int len)
Linus Torvalds4990aad2005-04-20 12:16:57 -07002483{
2484 while (len) {
2485 unsigned int buffered = write_buffer_len;
2486 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
2487 if (partial > len)
2488 partial = len;
2489 memcpy(write_buffer + buffered, data, partial);
2490 buffered += partial;
2491 if (buffered == WRITE_BUFFER_SIZE) {
Junio C Hamano6015c282006-08-08 14:47:32 -07002492 write_buffer_len = buffered;
2493 if (ce_write_flush(context, fd))
Linus Torvalds4990aad2005-04-20 12:16:57 -07002494 return -1;
2495 buffered = 0;
2496 }
2497 write_buffer_len = buffered;
2498 len -= partial;
Florian Forster1d7f1712006-06-18 17:18:09 +02002499 data = (char *) data + partial;
Junio C Hamanoa6080a02007-06-07 00:04:01 -07002500 }
2501 return 0;
Linus Torvalds4990aad2005-04-20 12:16:57 -07002502}
2503
Ben Peart3b1d9e02018-10-10 11:59:34 -04002504static int write_index_ext_header(git_hash_ctx *context, git_hash_ctx *eoie_context,
2505 int fd, unsigned int ext, unsigned int sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002506{
2507 ext = htonl(ext);
2508 sz = htonl(sz);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002509 if (eoie_context) {
2510 the_hash_algo->update_fn(eoie_context, &ext, 4);
2511 the_hash_algo->update_fn(eoie_context, &sz, 4);
2512 }
David Rientjes968a1d62006-08-14 13:38:14 -07002513 return ((ce_write(context, fd, &ext, 4) < 0) ||
2514 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002515}
2516
brian m. carlsonaab61352018-02-01 02:18:45 +00002517static int ce_flush(git_hash_ctx *context, int fd, unsigned char *hash)
Linus Torvalds4990aad2005-04-20 12:16:57 -07002518{
2519 unsigned int left = write_buffer_len;
Linus Torvaldsca9be052005-04-20 12:36:41 -07002520
Linus Torvalds4990aad2005-04-20 12:16:57 -07002521 if (left) {
2522 write_buffer_len = 0;
brian m. carlsonaab61352018-02-01 02:18:45 +00002523 the_hash_algo->update_fn(context, write_buffer, left);
Linus Torvalds4990aad2005-04-20 12:16:57 -07002524 }
Linus Torvaldsca9be052005-04-20 12:36:41 -07002525
brian m. carlsonaab61352018-02-01 02:18:45 +00002526 /* Flush first if not enough space for hash signature */
2527 if (left + the_hash_algo->rawsz > WRITE_BUFFER_SIZE) {
Jeff King06f46f22017-09-13 13:16:03 -04002528 if (write_in_full(fd, write_buffer, left) < 0)
Qingning Huo2c865d92005-09-11 14:27:47 +01002529 return -1;
2530 left = 0;
2531 }
2532
brian m. carlsonaab61352018-02-01 02:18:45 +00002533 /* Append the hash signature at the end */
2534 the_hash_algo->final_fn(write_buffer + left, context);
2535 hashcpy(hash, write_buffer + left);
2536 left += the_hash_algo->rawsz;
Jeff King06f46f22017-09-13 13:16:03 -04002537 return (write_in_full(fd, write_buffer, left) < 0) ? -1 : 0;
Linus Torvalds4990aad2005-04-20 12:16:57 -07002538}
2539
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002540static void ce_smudge_racily_clean_entry(struct index_state *istate,
2541 struct cache_entry *ce)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002542{
2543 /*
2544 * The only thing we care about in this function is to smudge the
2545 * falsely clean entry due to touch-update-touch race, so we leave
2546 * everything else as they are. We are called for entries whose
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002547 * ce_stat_data.sd_mtime match the index file mtime.
Junio C Hamanoc70115b2008-07-29 01:13:44 -07002548 *
2549 * Note that this actually does not do much for gitlinks, for
2550 * which ce_match_stat_basic() always goes to the actual
2551 * contents. The caller checks with is_racy_timestamp() which
2552 * always says "no" for gitlinks, so we are not called for them ;-)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002553 */
2554 struct stat st;
2555
2556 if (lstat(ce->name, &st) < 0)
2557 return;
2558 if (ce_match_stat_basic(ce, &st))
2559 return;
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002560 if (ce_modified_check_fs(istate, ce, &st)) {
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002561 /* This is "racily clean"; smudge it. Note that this
2562 * is a tricky code. At first glance, it may appear
2563 * that it can break with this sequence:
2564 *
2565 * $ echo xyzzy >frotz
2566 * $ git-update-index --add frotz
2567 * $ : >frotz
2568 * $ sleep 3
2569 * $ echo filfre >nitfol
2570 * $ git-update-index --add nitfol
2571 *
Junio C Hamanob7e58b12006-08-05 04:16:02 -07002572 * but it does not. When the second update-index runs,
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002573 * it notices that the entry "frotz" has the same timestamp
2574 * as index, and if we were to smudge it by resetting its
2575 * size to zero here, then the object name recorded
2576 * in index is the 6-byte file but the cached stat information
2577 * becomes zero --- which would then match what we would
Junio C Hamanoa6080a02007-06-07 00:04:01 -07002578 * obtain from the filesystem next time we stat("frotz").
Junio C Hamano4b3511b2005-12-20 14:18:47 -08002579 *
2580 * However, the second update-index, before calling
2581 * this function, notices that the cached size is 6
2582 * bytes and what is on the filesystem is an empty
2583 * file, and never calls us, so the cached size information
2584 * for "frotz" stays 6 which does not match the filesystem.
2585 */
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002586 ce->ce_stat_data.sd_size = 0;
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002587 }
2588}
2589
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002590/* Copy miscellaneous fields but not the name */
Kevin Willfordce012de2017-08-21 15:24:32 -06002591static void copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002592 struct cache_entry *ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002593{
Thomas Gummererb60e1882012-07-11 11:22:37 +02002594 short flags;
brian m. carlson575fa8a2019-02-19 00:05:24 +00002595 const unsigned hashsz = the_hash_algo->rawsz;
2596 uint16_t *flagsp = (uint16_t *)(ondisk->data + hashsz);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002597
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002598 ondisk->ctime.sec = htonl(ce->ce_stat_data.sd_ctime.sec);
2599 ondisk->mtime.sec = htonl(ce->ce_stat_data.sd_mtime.sec);
2600 ondisk->ctime.nsec = htonl(ce->ce_stat_data.sd_ctime.nsec);
2601 ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec);
2602 ondisk->dev = htonl(ce->ce_stat_data.sd_dev);
2603 ondisk->ino = htonl(ce->ce_stat_data.sd_ino);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002604 ondisk->mode = htonl(ce->ce_mode);
Michael Haggertyc21d39d2013-06-20 10:37:50 +02002605 ondisk->uid = htonl(ce->ce_stat_data.sd_uid);
2606 ondisk->gid = htonl(ce->ce_stat_data.sd_gid);
2607 ondisk->size = htonl(ce->ce_stat_data.sd_size);
brian m. carlson575fa8a2019-02-19 00:05:24 +00002608 hashcpy(ondisk->data, ce->oid.hash);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002609
Nguyễn Thái Ngọc Duyce51bf02014-06-13 19:19:25 +07002610 flags = ce->ce_flags & ~CE_NAMEMASK;
Thomas Gummererb60e1882012-07-11 11:22:37 +02002611 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
brian m. carlson575fa8a2019-02-19 00:05:24 +00002612 flagsp[0] = htons(flags);
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002613 if (ce->ce_flags & CE_EXTENDED) {
brian m. carlson575fa8a2019-02-19 00:05:24 +00002614 flagsp[1] = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16);
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002615 }
2616}
2617
brian m. carlsonaab61352018-02-01 02:18:45 +00002618static int ce_write_entry(git_hash_ctx *c, int fd, struct cache_entry *ce,
Kevin Willfordce012de2017-08-21 15:24:32 -06002619 struct strbuf *previous_name, struct ondisk_cache_entry *ondisk)
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002620{
Junio C Hamano9d227782012-04-04 09:12:43 -07002621 int size;
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002622 int result;
Ramsay Jones00a4b032018-03-19 17:56:11 +00002623 unsigned int saved_namelen;
2624 int stripped_name = 0;
Kevin Willfordce012de2017-08-21 15:24:32 -06002625 static unsigned char padding[8] = { 0x00 };
Junio C Hamanof136f7b2012-04-03 15:53:14 -07002626
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002627 if (ce->ce_flags & CE_STRIP_NAME) {
2628 saved_namelen = ce_namelen(ce);
2629 ce->ce_namelen = 0;
Ramsay Jones00a4b032018-03-19 17:56:11 +00002630 stripped_name = 1;
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002631 }
2632
brian m. carlson575fa8a2019-02-19 00:05:24 +00002633 size = offsetof(struct ondisk_cache_entry,data) + ondisk_data_size(ce->ce_flags, 0);
Kevin Willfordce012de2017-08-21 15:24:32 -06002634
Junio C Hamano9d227782012-04-04 09:12:43 -07002635 if (!previous_name) {
Kevin Willfordce012de2017-08-21 15:24:32 -06002636 int len = ce_namelen(ce);
2637 copy_cache_entry_to_ondisk(ondisk, ce);
2638 result = ce_write(c, fd, ondisk, size);
2639 if (!result)
2640 result = ce_write(c, fd, ce->name, len);
2641 if (!result)
2642 result = ce_write(c, fd, padding, align_padding_size(size, len));
Junio C Hamano9d227782012-04-04 09:12:43 -07002643 } else {
2644 int common, to_remove, prefix_size;
2645 unsigned char to_remove_vi[16];
2646 for (common = 0;
2647 (ce->name[common] &&
2648 common < previous_name->len &&
2649 ce->name[common] == previous_name->buf[common]);
2650 common++)
2651 ; /* still matching */
2652 to_remove = previous_name->len - common;
2653 prefix_size = encode_varint(to_remove, to_remove_vi);
2654
Kevin Willfordce012de2017-08-21 15:24:32 -06002655 copy_cache_entry_to_ondisk(ondisk, ce);
2656 result = ce_write(c, fd, ondisk, size);
2657 if (!result)
2658 result = ce_write(c, fd, to_remove_vi, prefix_size);
2659 if (!result)
Thomas Gummerer0b90b882017-09-07 20:24:12 +01002660 result = ce_write(c, fd, ce->name + common, ce_namelen(ce) - common);
2661 if (!result)
2662 result = ce_write(c, fd, padding, 1);
Junio C Hamano9d227782012-04-04 09:12:43 -07002663
2664 strbuf_splice(previous_name, common, to_remove,
2665 ce->name + common, ce_namelen(ce) - common);
2666 }
Ramsay Jones00a4b032018-03-19 17:56:11 +00002667 if (stripped_name) {
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07002668 ce->ce_namelen = saved_namelen;
2669 ce->ce_flags &= ~CE_STRIP_NAME;
2670 }
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002671
Jonathan Nieder59efba62010-08-09 22:28:07 -05002672 return result;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002673}
2674
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002675/*
2676 * This function verifies if index_state has the correct sha1 of the
2677 * index file. Don't die if we have any other failure, just return 0.
2678 */
2679static int verify_index_from(const struct index_state *istate, const char *path)
2680{
2681 int fd;
2682 ssize_t n;
2683 struct stat st;
brian m. carlsonaab61352018-02-01 02:18:45 +00002684 unsigned char hash[GIT_MAX_RAWSZ];
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002685
2686 if (!istate->initialized)
2687 return 0;
2688
2689 fd = open(path, O_RDONLY);
2690 if (fd < 0)
2691 return 0;
2692
2693 if (fstat(fd, &st))
2694 goto out;
2695
brian m. carlsonaab61352018-02-01 02:18:45 +00002696 if (st.st_size < sizeof(struct cache_header) + the_hash_algo->rawsz)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002697 goto out;
2698
brian m. carlsonaab61352018-02-01 02:18:45 +00002699 n = pread_in_full(fd, hash, the_hash_algo->rawsz, st.st_size - the_hash_algo->rawsz);
2700 if (n != the_hash_algo->rawsz)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002701 goto out;
2702
Jeff King67947c32018-08-28 17:22:52 -04002703 if (!hasheq(istate->oid.hash, hash))
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002704 goto out;
2705
2706 close(fd);
2707 return 1;
2708
2709out:
2710 close(fd);
2711 return 0;
2712}
2713
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002714static int repo_verify_index(struct repository *repo)
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002715{
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002716 return verify_index_from(repo->index, repo->index_file);
Yiannis Marangos426ddee2014-04-10 21:31:21 +03002717}
2718
Junio C Hamano483fbe22011-03-21 10:18:19 -07002719static int has_racy_timestamp(struct index_state *istate)
2720{
2721 int entries = istate->cache_nr;
2722 int i;
2723
2724 for (i = 0; i < entries; i++) {
2725 struct cache_entry *ce = istate->cache[i];
2726 if (is_racy_timestamp(istate, ce))
2727 return 1;
2728 }
2729 return 0;
2730}
2731
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002732void repo_update_index_if_able(struct repository *repo,
2733 struct lock_file *lockfile)
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002734{
Nguyễn Thái Ngọc Duy1b0d9682019-01-12 09:13:27 +07002735 if ((repo->index->cache_changed ||
2736 has_racy_timestamp(repo->index)) &&
2737 repo_verify_index(repo))
2738 write_locked_index(repo->index, lockfile, COMMIT_LOCK);
Martin Ågrenb74c90f2017-10-06 22:12:14 +02002739 else
2740 rollback_lock_file(lockfile);
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002741}
2742
Jonathan Niederd8465502018-11-19 22:11:47 -08002743static int record_eoie(void)
2744{
2745 int val;
2746
2747 if (!git_config_get_bool("index.recordendofindexentries", &val))
2748 return val;
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002749
2750 /*
2751 * As a convenience, the end of index entries extension
2752 * used for threading is written by default if the user
2753 * explicitly requested threaded index reads.
2754 */
2755 return !git_config_get_index_threads(&val) && val != 1;
Jonathan Niederd8465502018-11-19 22:11:47 -08002756}
2757
Jonathan Nieder42916052018-11-19 22:12:22 -08002758static int record_ieot(void)
2759{
2760 int val;
2761
2762 if (!git_config_get_bool("index.recordoffsettable", &val))
2763 return val;
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002764
2765 /*
2766 * As a convenience, the offset table used for threading is
2767 * written by default if the user explicitly requested
2768 * threaded index reads.
2769 */
2770 return !git_config_get_index_threads(&val) && val != 1;
Jonathan Nieder42916052018-11-19 22:12:22 -08002771}
2772
Martin Ågren812d6b02017-10-06 22:12:12 +02002773/*
2774 * On success, `tempfile` is closed. If it is the temporary file
2775 * of a `struct lock_file`, we will therefore effectively perform
2776 * a 'close_lock_file_gently()`. Since that is an implementation
2777 * detail of lockfiles, callers of `do_write_index()` should not
2778 * rely on it.
2779 */
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02002780static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002781 int strip_extensions)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002782{
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +07002783 uint64_t start = getnanotime();
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02002784 int newfd = tempfile->fd;
Ben Peart3b1d9e02018-10-10 11:59:34 -04002785 git_hash_ctx c, eoie_c;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002786 struct cache_header hdr;
Kevin Willfordb50386c2017-08-21 15:24:31 -06002787 int i, err = 0, removed, extended, hdr_version;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002788 struct cache_entry **cache = istate->cache;
2789 int entries = istate->cache_nr;
Kjetil Barvike1afca42009-02-23 19:02:57 +01002790 struct stat st;
brian m. carlson575fa8a2019-02-19 00:05:24 +00002791 struct ondisk_cache_entry ondisk;
Junio C Hamano9d227782012-04-04 09:12:43 -07002792 struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
Thomas Gummerer4bddd982018-01-07 22:30:14 +00002793 int drop_cache_tree = istate->drop_cache_tree;
Ben Peart3b1d9e02018-10-10 11:59:34 -04002794 off_t offset;
Ben Peart77ff1122018-10-10 11:59:38 -04002795 int ieot_entries = 1;
Ben Peart32550892018-10-10 11:59:37 -04002796 struct index_entry_offset_table *ieot = NULL;
2797 int nr, nr_threads;
Junio C Hamano025a0702005-06-10 01:32:37 -07002798
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002799 for (i = removed = extended = 0; i < entries; i++) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002800 if (cache[i]->ce_flags & CE_REMOVE)
Junio C Hamano025a0702005-06-10 01:32:37 -07002801 removed++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002802
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002803 /* reduce extended entries if possible */
2804 cache[i]->ce_flags &= ~CE_EXTENDED;
2805 if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) {
2806 extended++;
2807 cache[i]->ce_flags |= CE_EXTENDED;
2808 }
2809 }
2810
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002811 if (!istate->version) {
Derrick Stolee7211b9e2019-08-13 11:37:43 -07002812 istate->version = get_index_format_default(the_repository);
Nguyễn Thái Ngọc Duy4c2db932018-04-14 17:34:59 +02002813 if (git_env_bool("GIT_TEST_SPLIT_INDEX", 0))
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002814 init_split_index(istate);
2815 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002816
2817 /* demote version 3 to version 2 when the latter suffices */
2818 if (istate->version == 3 || istate->version == 2)
2819 istate->version = extended ? 3 : 2;
2820
2821 hdr_version = istate->version;
2822
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07002823 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
Junio C Hamano9d227782012-04-04 09:12:43 -07002824 hdr.hdr_version = htonl(hdr_version);
Junio C Hamano025a0702005-06-10 01:32:37 -07002825 hdr.hdr_entries = htonl(entries - removed);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002826
brian m. carlsonaab61352018-02-01 02:18:45 +00002827 the_hash_algo->init_fn(&c);
Linus Torvaldsca9be052005-04-20 12:36:41 -07002828 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002829 return -1;
2830
Jonathan Nieder2a9dede2018-11-19 22:14:26 -08002831 if (!HAVE_THREADS || git_config_get_index_threads(&nr_threads))
Nguyễn Thái Ngọc Duy88168b92018-11-03 09:48:48 +01002832 nr_threads = 1;
Nguyễn Thái Ngọc Duy62e5ee82018-11-03 09:48:47 +01002833
Jonathan Nieder42916052018-11-19 22:12:22 -08002834 if (nr_threads != 1 && record_ieot()) {
Ben Peart32550892018-10-10 11:59:37 -04002835 int ieot_blocks, cpus;
2836
2837 /*
2838 * ensure default number of ieot blocks maps evenly to the
2839 * default number of threads that will process them leaving
2840 * room for the thread to load the index extensions.
2841 */
2842 if (!nr_threads) {
2843 ieot_blocks = istate->cache_nr / THREAD_COST;
2844 cpus = online_cpus();
2845 if (ieot_blocks > cpus - 1)
2846 ieot_blocks = cpus - 1;
2847 } else {
2848 ieot_blocks = nr_threads;
Ben Peart77ff1122018-10-10 11:59:38 -04002849 if (ieot_blocks > istate->cache_nr)
2850 ieot_blocks = istate->cache_nr;
Ben Peart32550892018-10-10 11:59:37 -04002851 }
2852
2853 /*
2854 * no reason to write out the IEOT extension if we don't
2855 * have enough blocks to utilize multi-threading
2856 */
2857 if (ieot_blocks > 1) {
2858 ieot = xcalloc(1, sizeof(struct index_entry_offset_table)
2859 + (ieot_blocks * sizeof(struct index_entry_offset)));
Ben Peart77ff1122018-10-10 11:59:38 -04002860 ieot_entries = DIV_ROUND_UP(entries, ieot_blocks);
Ben Peart32550892018-10-10 11:59:37 -04002861 }
2862 }
Ben Peart32550892018-10-10 11:59:37 -04002863
Ben Peart3b1d9e02018-10-10 11:59:34 -04002864 offset = lseek(newfd, 0, SEEK_CUR);
Ben Peart32550892018-10-10 11:59:37 -04002865 if (offset < 0) {
2866 free(ieot);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002867 return -1;
Ben Peart32550892018-10-10 11:59:37 -04002868 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04002869 offset += write_buffer_len;
Ben Peart32550892018-10-10 11:59:37 -04002870 nr = 0;
Junio C Hamano9d227782012-04-04 09:12:43 -07002871 previous_name = (hdr_version == 4) ? &previous_name_buf : NULL;
Kevin Willfordce012de2017-08-21 15:24:32 -06002872
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002873 for (i = 0; i < entries; i++) {
2874 struct cache_entry *ce = cache[i];
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002875 if (ce->ce_flags & CE_REMOVE)
Linus Torvaldsaa160212005-06-09 15:34:04 -07002876 continue;
Junio C Hamanoe06c43c2008-03-30 09:25:52 -07002877 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +02002878 ce_smudge_racily_clean_entry(istate, ce);
brian m. carlson99d1a982016-09-05 20:07:52 +00002879 if (is_null_oid(&ce->oid)) {
Jeff King83bd7432013-08-27 16:41:12 -04002880 static const char msg[] = "cache entry has null sha1: %s";
2881 static int allow = -1;
2882
2883 if (allow < 0)
2884 allow = git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
2885 if (allow)
2886 warning(msg, ce->name);
2887 else
Kevin Willfordb50386c2017-08-21 15:24:31 -06002888 err = error(msg, ce->name);
Jeff Kinga96d3cc2017-04-21 14:46:17 -04002889
2890 drop_cache_tree = 1;
Jeff King83bd7432013-08-27 16:41:12 -04002891 }
Ben Peart77ff1122018-10-10 11:59:38 -04002892 if (ieot && i && (i % ieot_entries == 0)) {
Ben Peart32550892018-10-10 11:59:37 -04002893 ieot->entries[ieot->nr].nr = nr;
2894 ieot->entries[ieot->nr].offset = offset;
2895 ieot->nr++;
2896 /*
2897 * If we have a V4 index, set the first byte to an invalid
2898 * character to ensure there is nothing common with the previous
2899 * entry
2900 */
2901 if (previous_name)
2902 previous_name->buf[0] = 0;
2903 nr = 0;
2904 offset = lseek(newfd, 0, SEEK_CUR);
2905 if (offset < 0) {
2906 free(ieot);
2907 return -1;
2908 }
2909 offset += write_buffer_len;
2910 }
Kevin Willfordce012de2017-08-21 15:24:32 -06002911 if (ce_write_entry(&c, newfd, ce, previous_name, (struct ondisk_cache_entry *)&ondisk) < 0)
Kevin Willfordb50386c2017-08-21 15:24:31 -06002912 err = -1;
2913
2914 if (err)
2915 break;
Ben Peart32550892018-10-10 11:59:37 -04002916 nr++;
2917 }
2918 if (ieot && nr) {
2919 ieot->entries[ieot->nr].nr = nr;
2920 ieot->entries[ieot->nr].offset = offset;
2921 ieot->nr++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002922 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002923 strbuf_release(&previous_name_buf);
Junio C Hamano1af1c2b2006-04-23 16:52:08 -07002924
Ben Peart32550892018-10-10 11:59:37 -04002925 if (err) {
2926 free(ieot);
Kevin Willfordb50386c2017-08-21 15:24:31 -06002927 return err;
Ben Peart32550892018-10-10 11:59:37 -04002928 }
Kevin Willfordb50386c2017-08-21 15:24:31 -06002929
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002930 /* Write extension data here */
Ben Peart3b1d9e02018-10-10 11:59:34 -04002931 offset = lseek(newfd, 0, SEEK_CUR);
Ben Peart32550892018-10-10 11:59:37 -04002932 if (offset < 0) {
2933 free(ieot);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002934 return -1;
Ben Peart32550892018-10-10 11:59:37 -04002935 }
Ben Peart3b1d9e02018-10-10 11:59:34 -04002936 offset += write_buffer_len;
2937 the_hash_algo->init_fn(&eoie_c);
2938
Ben Peart32550892018-10-10 11:59:37 -04002939 /*
2940 * Lets write out CACHE_EXT_INDEXENTRYOFFSETTABLE first so that we
2941 * can minimize the number of extensions we have to scan through to
2942 * find it during load. Write it out regardless of the
2943 * strip_extensions parameter as we need it when loading the shared
2944 * index.
2945 */
Ben Peart32550892018-10-10 11:59:37 -04002946 if (ieot) {
2947 struct strbuf sb = STRBUF_INIT;
2948
2949 write_ieot_extension(&sb, ieot);
2950 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_INDEXENTRYOFFSETTABLE, sb.len) < 0
2951 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
2952 strbuf_release(&sb);
2953 free(ieot);
2954 if (err)
2955 return -1;
2956 }
Ben Peart32550892018-10-10 11:59:37 -04002957
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +07002958 if (!strip_extensions && istate->split_index &&
2959 !is_null_oid(&istate->split_index->base_oid)) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002960 struct strbuf sb = STRBUF_INIT;
2961
2962 err = write_link_extension(&sb, istate) < 0 ||
Ben Peart3b1d9e02018-10-10 11:59:34 -04002963 write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_LINK,
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002964 sb.len) < 0 ||
2965 ce_write(&c, newfd, sb.buf, sb.len) < 0;
2966 strbuf_release(&sb);
2967 if (err)
2968 return -1;
2969 }
Jeff Kinga96d3cc2017-04-21 14:46:17 -04002970 if (!strip_extensions && !drop_cache_tree && istate->cache_tree) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002971 struct strbuf sb = STRBUF_INIT;
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02002972
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02002973 cache_tree_write(&sb, istate->cache_tree);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002974 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_TREE, sb.len) < 0
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02002975 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
2976 strbuf_release(&sb);
2977 if (err)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002978 return -1;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002979 }
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002980 if (!strip_extensions && istate->resolve_undo) {
Junio C Hamanocfc57892009-12-25 00:30:51 -08002981 struct strbuf sb = STRBUF_INIT;
2982
2983 resolve_undo_write(&sb, istate->resolve_undo);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002984 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_RESOLVE_UNDO,
Junio C Hamanocfc57892009-12-25 00:30:51 -08002985 sb.len) < 0
2986 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
2987 strbuf_release(&sb);
2988 if (err)
2989 return -1;
2990 }
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002991 if (!strip_extensions && istate->untracked) {
2992 struct strbuf sb = STRBUF_INIT;
2993
2994 write_untracked_extension(&sb, istate->untracked);
Ben Peart3b1d9e02018-10-10 11:59:34 -04002995 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_UNTRACKED,
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002996 sb.len) < 0 ||
2997 ce_write(&c, newfd, sb.buf, sb.len) < 0;
2998 strbuf_release(&sb);
2999 if (err)
3000 return -1;
3001 }
Ben Peart883e2482017-09-22 12:35:40 -04003002 if (!strip_extensions && istate->fsmonitor_last_update) {
3003 struct strbuf sb = STRBUF_INIT;
3004
3005 write_fsmonitor_extension(&sb, istate);
Ben Peart3b1d9e02018-10-10 11:59:34 -04003006 err = write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_FSMONITOR, sb.len) < 0
3007 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3008 strbuf_release(&sb);
3009 if (err)
3010 return -1;
3011 }
3012
3013 /*
3014 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1
3015 * so that it can be found and processed before all the index entries are
3016 * read. Write it out regardless of the strip_extensions parameter as we need it
3017 * when loading the shared index.
3018 */
Jonathan Niederd8465502018-11-19 22:11:47 -08003019 if (offset && record_eoie()) {
Ben Peart3b1d9e02018-10-10 11:59:34 -04003020 struct strbuf sb = STRBUF_INIT;
3021
3022 write_eoie_extension(&sb, &eoie_c, offset);
3023 err = write_index_ext_header(&c, NULL, newfd, CACHE_EXT_ENDOFINDEXENTRIES, sb.len) < 0
Ben Peart883e2482017-09-22 12:35:40 -04003024 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
3025 strbuf_release(&sb);
3026 if (err)
3027 return -1;
3028 }
Kjetil Barvike1afca42009-02-23 19:02:57 +01003029
brian m. carlson75691ea2018-05-02 00:25:44 +00003030 if (ce_flush(&c, newfd, istate->oid.hash))
Jeff Hostetler9f41c7a2017-04-26 22:05:23 +02003031 return -1;
Jeff King49bd0fc2017-09-05 08:14:30 -04003032 if (close_tempfile_gently(tempfile)) {
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003033 error(_("could not close '%s'"), get_tempfile_path(tempfile));
Jeff King49bd0fc2017-09-05 08:14:30 -04003034 return -1;
3035 }
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003036 if (stat(get_tempfile_path(tempfile), &st))
Kjetil Barvike1afca42009-02-23 19:02:57 +01003037 return -1;
Kjetil Barvik5bcf1092009-03-15 12:38:55 +01003038 istate->timestamp.sec = (unsigned int)st.st_mtime;
3039 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Nguyễn Thái Ngọc Duyca54d9b2018-01-27 19:27:56 +07003040 trace_performance_since(start, "write index, changed mask = %x", istate->cache_changed);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003041
3042 /*
3043 * TODO trace2: replace "the_repository" with the actual repo instance
3044 * that is associated with the given "istate".
3045 */
3046 trace2_data_intmax("index", the_repository, "write/version",
3047 istate->version);
3048 trace2_data_intmax("index", the_repository, "write/cache_nr",
3049 istate->cache_nr);
3050
Kjetil Barvike1afca42009-02-23 19:02:57 +01003051 return 0;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07003052}
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003053
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003054void set_alternate_index_output(const char *name)
3055{
3056 alternate_index_output = name;
3057}
3058
3059static int commit_locked_index(struct lock_file *lk)
3060{
Michael Haggerty751bace2014-10-01 12:28:36 +02003061 if (alternate_index_output)
3062 return commit_lock_file_to(lk, alternate_index_output);
3063 else
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003064 return commit_lock_file(lk);
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07003065}
3066
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003067static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
3068 unsigned flags)
3069{
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003070 int ret;
3071
3072 /*
3073 * TODO trace2: replace "the_repository" with the actual repo instance
3074 * that is associated with the given "istate".
3075 */
3076 trace2_region_enter_printf("index", "do_write_index", the_repository,
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003077 "%s", get_lock_file_path(lock));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003078 ret = do_write_index(istate, lock->tempfile, 0);
3079 trace2_region_leave_printf("index", "do_write_index", the_repository,
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003080 "%s", get_lock_file_path(lock));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003081
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003082 if (ret)
3083 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003084 if (flags & COMMIT_LOCK)
Ben Peart1956ecd2019-02-15 12:59:21 -05003085 ret = commit_locked_index(lock);
3086 else
3087 ret = close_lock_file_gently(lock);
3088
3089 run_hook_le(NULL, "post-index-change",
3090 istate->updated_workdir ? "1" : "0",
3091 istate->updated_skipworktree ? "1" : "0", NULL);
3092 istate->updated_workdir = 0;
3093 istate->updated_skipworktree = 0;
3094
3095 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003096}
3097
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003098static int write_split_index(struct index_state *istate,
3099 struct lock_file *lock,
3100 unsigned flags)
3101{
3102 int ret;
3103 prepare_to_write_split_index(istate);
3104 ret = do_write_locked_index(istate, lock, flags);
3105 finish_writing_split_index(istate);
3106 return ret;
3107}
3108
Christian Couderb9683722017-03-06 10:41:58 +01003109static const char *shared_index_expire = "2.weeks.ago";
3110
3111static unsigned long get_shared_index_expire_date(void)
3112{
3113 static unsigned long shared_index_expire_date;
3114 static int shared_index_expire_date_prepared;
3115
3116 if (!shared_index_expire_date_prepared) {
3117 git_config_get_expiry("splitindex.sharedindexexpire",
3118 &shared_index_expire);
3119 shared_index_expire_date = approxidate(shared_index_expire);
3120 shared_index_expire_date_prepared = 1;
3121 }
3122
3123 return shared_index_expire_date;
3124}
3125
3126static int should_delete_shared_index(const char *shared_index_path)
3127{
3128 struct stat st;
3129 unsigned long expiration;
3130
3131 /* Check timestamp */
3132 expiration = get_shared_index_expire_date();
3133 if (!expiration)
3134 return 0;
3135 if (stat(shared_index_path, &st))
Peter Krefting78bde922017-04-30 23:32:48 +02003136 return error_errno(_("could not stat '%s'"), shared_index_path);
Christian Couderb9683722017-03-06 10:41:58 +01003137 if (st.st_mtime > expiration)
3138 return 0;
3139
3140 return 1;
3141}
3142
3143static int clean_shared_index_files(const char *current_hex)
3144{
3145 struct dirent *de;
3146 DIR *dir = opendir(get_git_dir());
3147
3148 if (!dir)
3149 return error_errno(_("unable to open git dir: %s"), get_git_dir());
3150
3151 while ((de = readdir(dir)) != NULL) {
3152 const char *sha1_hex;
3153 const char *shared_index_path;
3154 if (!skip_prefix(de->d_name, "sharedindex.", &sha1_hex))
3155 continue;
3156 if (!strcmp(sha1_hex, current_hex))
3157 continue;
3158 shared_index_path = git_path("%s", de->d_name);
3159 if (should_delete_shared_index(shared_index_path) > 0 &&
3160 unlink(shared_index_path))
3161 warning_errno(_("unable to unlink: %s"), shared_index_path);
3162 }
3163 closedir(dir);
3164
3165 return 0;
3166}
3167
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07003168static int write_shared_index(struct index_state *istate,
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003169 struct tempfile **temp)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003170{
3171 struct split_index *si = istate->split_index;
Jeff King076aa2c2017-09-05 08:15:08 -04003172 int ret;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003173
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003174 move_cache_to_base_index(istate);
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003175
3176 trace2_region_enter_printf("index", "shared/do_write_index",
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003177 the_repository, "%s", get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003178 ret = do_write_index(si->base, *temp, 1);
Ævar Arnfjörð Bjarmasonc1735422019-05-10 15:37:38 +02003179 trace2_region_leave_printf("index", "shared/do_write_index",
Martin Ågren6a8c89d2021-01-05 20:23:50 +01003180 the_repository, "%s", get_tempfile_path(*temp));
Jeff Hostetler42fee7a2019-02-22 14:25:07 -08003181
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003182 if (ret)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003183 return ret;
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003184 ret = adjust_shared_perm(get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003185 if (ret) {
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01003186 error(_("cannot fix permission bits on '%s'"), get_tempfile_path(*temp));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003187 return ret;
3188 }
Nguyễn Thái Ngọc Duy7db2d082018-01-14 17:18:18 +07003189 ret = rename_tempfile(temp,
brian m. carlson75691ea2018-05-02 00:25:44 +00003190 git_path("sharedindex.%s", oid_to_hex(&si->base->oid)));
Christian Couderb9683722017-03-06 10:41:58 +01003191 if (!ret) {
brian m. carlson75691ea2018-05-02 00:25:44 +00003192 oidcpy(&si->base_oid, &si->base->oid);
3193 clean_shared_index_files(oid_to_hex(&si->base->oid));
Christian Couderb9683722017-03-06 10:41:58 +01003194 }
3195
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003196 return ret;
3197}
3198
Christian Coudere6a1dd72017-02-27 19:00:08 +01003199static const int default_max_percent_split_change = 20;
3200
3201static int too_many_not_shared_entries(struct index_state *istate)
3202{
3203 int i, not_shared = 0;
3204 int max_split = git_config_get_max_percent_split_change();
3205
3206 switch (max_split) {
3207 case -1:
3208 /* not or badly configured: use the default value */
3209 max_split = default_max_percent_split_change;
3210 break;
3211 case 0:
3212 return 1; /* 0% means always write a new shared index */
3213 case 100:
3214 return 0; /* 100% means never write a new shared index */
3215 default:
3216 break; /* just use the configured value */
3217 }
3218
3219 /* Count not shared entries */
3220 for (i = 0; i < istate->cache_nr; i++) {
3221 struct cache_entry *ce = istate->cache[i];
3222 if (!ce->index)
3223 not_shared++;
3224 }
3225
3226 return (int64_t)istate->cache_nr * max_split < (int64_t)not_shared * 100;
3227}
3228
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003229int write_locked_index(struct index_state *istate, struct lock_file *lock,
3230 unsigned flags)
3231{
Christian Couder0d59ffb2017-02-27 19:00:12 +01003232 int new_shared_index, ret;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003233 struct split_index *si = istate->split_index;
3234
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02003235 if (git_env_bool("GIT_TEST_CHECK_CACHE_TREE", 0))
Nguyễn Thái Ngọc Duyc207e9e2018-11-10 06:49:02 +01003236 cache_tree_verify(the_repository, istate);
Nguyễn Thái Ngọc Duy4592e602018-08-18 16:41:28 +02003237
Martin Ågren61000812018-03-01 21:40:20 +01003238 if ((flags & SKIP_IF_UNCHANGED) && !istate->cache_changed) {
3239 if (flags & COMMIT_LOCK)
3240 rollback_lock_file(lock);
3241 return 0;
3242 }
3243
Alex Vandiver3bd28eb2017-11-09 11:58:10 -08003244 if (istate->fsmonitor_last_update)
3245 fill_fsmonitor_bitmap(istate);
3246
Nguyễn Thái Ngọc Duy5165dd52014-06-13 19:19:47 +07003247 if (!si || alternate_index_output ||
3248 (istate->cache_changed & ~EXTMASK)) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003249 if (si)
brian m. carlson2182abd2018-05-02 00:25:43 +00003250 oidclr(&si->base_oid);
Martin Ågrendf60cf52017-10-06 22:12:13 +02003251 ret = do_write_locked_index(istate, lock, flags);
3252 goto out;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07003253 }
3254
Nguyễn Thái Ngọc Duy4c2db932018-04-14 17:34:59 +02003255 if (git_env_bool("GIT_TEST_SPLIT_INDEX", 0)) {
brian m. carlson2182abd2018-05-02 00:25:43 +00003256 int v = si->base_oid.hash[0];
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07003257 if ((v & 15) < 6)
3258 istate->cache_changed |= SPLIT_INDEX_ORDERED;
3259 }
Christian Coudere6a1dd72017-02-27 19:00:08 +01003260 if (too_many_not_shared_entries(istate))
3261 istate->cache_changed |= SPLIT_INDEX_ORDERED;
Christian Couder0d59ffb2017-02-27 19:00:12 +01003262
3263 new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED;
3264
3265 if (new_shared_index) {
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003266 struct tempfile *temp;
3267 int saved_errno;
3268
Ævar Arnfjörð Bjarmasonc9d6c782018-11-18 19:04:29 +00003269 /* Same initial permissions as the main .git/index file */
3270 temp = mks_tempfile_sm(git_path("sharedindex_XXXXXX"), 0, 0666);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003271 if (!temp) {
brian m. carlson2182abd2018-05-02 00:25:43 +00003272 oidclr(&si->base_oid);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003273 ret = do_write_locked_index(istate, lock, flags);
Nguyễn Thái Ngọc Duyef5b3a62018-01-24 16:38:29 +07003274 goto out;
3275 }
3276 ret = write_shared_index(istate, &temp);
Nguyễn Thái Ngọc Duy59f9d2d2018-01-14 17:18:19 +07003277
3278 saved_errno = errno;
3279 if (is_tempfile_active(temp))
3280 delete_tempfile(&temp);
3281 errno = saved_errno;
3282
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003283 if (ret)
Martin Ågrendf60cf52017-10-06 22:12:13 +02003284 goto out;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07003285 }
3286
Christian Couder0d59ffb2017-02-27 19:00:12 +01003287 ret = write_split_index(istate, lock, flags);
3288
3289 /* Freshen the shared index only if the split-index was written */
Nguyễn Thái Ngọc Duy6e37c8e2019-02-13 16:51:29 +07003290 if (!ret && !new_shared_index && !is_null_oid(&si->base_oid)) {
Thomas Gummerera125a222018-01-07 22:30:13 +00003291 const char *shared_index = git_path("sharedindex.%s",
brian m. carlson2182abd2018-05-02 00:25:43 +00003292 oid_to_hex(&si->base_oid));
Thomas Gummerera125a222018-01-07 22:30:13 +00003293 freshen_shared_index(shared_index, 1);
3294 }
Christian Couder0d59ffb2017-02-27 19:00:12 +01003295
Martin Ågrendf60cf52017-10-06 22:12:13 +02003296out:
3297 if (flags & COMMIT_LOCK)
3298 rollback_lock_file(lock);
Christian Couder0d59ffb2017-02-27 19:00:12 +01003299 return ret;
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003300}
3301
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003302/*
3303 * Read the index file that is potentially unmerged into given
Elijah Newrenad376202018-07-31 10:12:05 -07003304 * index_state, dropping any unmerged entries to stage #0 (potentially
3305 * resulting in a path appearing as both a file and a directory in the
3306 * index; the caller is responsible to clear out the extra entries
3307 * before writing the index to a tree). Returns true if the index is
3308 * unmerged. Callers who want to refuse to work from an unmerged
3309 * state can call this and check its return value, instead of calling
3310 * read_cache().
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003311 */
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003312int repo_read_index_unmerged(struct repository *repo)
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003313{
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003314 struct index_state *istate;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003315 int i;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003316 int unmerged = 0;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003317
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07003318 repo_read_index(repo);
3319 istate = repo->index;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003320 for (i = 0; i < istate->cache_nr; i++) {
3321 struct cache_entry *ce = istate->cache[i];
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003322 struct cache_entry *new_ce;
Jameson Millera8497352018-07-02 19:49:31 +00003323 int len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003324
3325 if (!ce_stage(ce))
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003326 continue;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003327 unmerged = 1;
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02003328 len = ce_namelen(ce);
Jameson Millera8497352018-07-02 19:49:31 +00003329 new_ce = make_empty_cache_entry(istate, len);
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003330 memcpy(new_ce->name, ce->name, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +02003331 new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED;
3332 new_ce->ce_namelen = len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003333 new_ce->ce_mode = ce->ce_mode;
Elijah Newrenad376202018-07-31 10:12:05 -07003334 if (add_index_entry(istate, new_ce, ADD_CACHE_SKIP_DFCHECK))
Nguyễn Thái Ngọc Duy9d0a9e92018-11-10 06:16:05 +01003335 return error(_("%s: cannot drop to stage #0"),
Karsten Blees5699d172013-11-14 20:24:37 +01003336 new_ce->name);
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003337 }
Junio C Hamanod1a43f22008-10-15 16:00:06 -07003338 return unmerged;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02003339}
Junio C Hamano041aee32008-07-21 01:24:17 -07003340
Jeff King98fa4732008-10-16 11:07:26 -04003341/*
3342 * Returns 1 if the path is an "other" path with respect to
3343 * the index; that is, the path is not mentioned in the index at all,
3344 * either as a file, a directory with some files in the index,
3345 * or as an unmerged entry.
3346 *
3347 * We helpfully remove a trailing "/" from directories so that
3348 * the output of read_directory can be used as-is.
3349 */
3350int index_name_is_other(const struct index_state *istate, const char *name,
3351 int namelen)
3352{
3353 int pos;
3354 if (namelen && name[namelen - 1] == '/')
3355 namelen--;
3356 pos = index_name_pos(istate, name, namelen);
3357 if (0 <= pos)
3358 return 0; /* exact match */
3359 pos = -pos - 1;
3360 if (pos < istate->cache_nr) {
3361 struct cache_entry *ce = istate->cache[pos];
3362 if (ce_namelen(ce) == namelen &&
3363 !memcmp(ce->name, name, namelen))
3364 return 0; /* Yup, this one exists unmerged */
3365 }
3366 return 1;
3367}
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003368
Brandon Williams87542502017-01-10 12:06:10 -08003369void *read_blob_data_from_index(const struct index_state *istate,
3370 const char *path, unsigned long *size)
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003371{
3372 int pos, len;
3373 unsigned long sz;
3374 enum object_type type;
3375 void *data;
3376
3377 len = strlen(path);
3378 pos = index_name_pos(istate, path, len);
3379 if (pos < 0) {
3380 /*
3381 * We might be in the middle of a merge, in which
3382 * case we would read stage #2 (ours).
3383 */
3384 int i;
3385 for (i = -pos - 1;
3386 (pos < 0 && i < istate->cache_nr &&
3387 !strcmp(istate->cache[i]->name, path));
3388 i++)
3389 if (ce_stage(istate->cache[i]) == 2)
3390 pos = i;
3391 }
3392 if (pos < 0)
3393 return NULL;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00003394 data = read_object_file(&istate->cache[pos]->oid, &type, &sz);
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003395 if (!data || type != OBJ_BLOB) {
3396 free(data);
3397 return NULL;
3398 }
Lukas Fleischerff366822013-04-13 15:28:31 +02003399 if (size)
3400 *size = sz;
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02003401 return data;
3402}
Michael Haggerty38612532013-06-20 10:37:51 +02003403
3404void stat_validity_clear(struct stat_validity *sv)
3405{
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00003406 FREE_AND_NULL(sv->sd);
Michael Haggerty38612532013-06-20 10:37:51 +02003407}
3408
3409int stat_validity_check(struct stat_validity *sv, const char *path)
3410{
3411 struct stat st;
3412
3413 if (stat(path, &st) < 0)
3414 return sv->sd == NULL;
3415 if (!sv->sd)
3416 return 0;
3417 return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
3418}
3419
3420void stat_validity_update(struct stat_validity *sv, int fd)
3421{
3422 struct stat st;
3423
3424 if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
3425 stat_validity_clear(sv);
3426 else {
3427 if (!sv->sd)
René Scharfeca56dad2021-03-13 17:17:22 +01003428 CALLOC_ARRAY(sv->sd, 1);
Michael Haggerty38612532013-06-20 10:37:51 +02003429 fill_stat_data(sv->sd, &st);
3430 }
3431}
David Turneredf3b902017-05-08 11:41:42 +02003432
3433void move_index_extensions(struct index_state *dst, struct index_state *src)
3434{
3435 dst->untracked = src->untracked;
3436 src->untracked = NULL;
Nguyễn Thái Ngọc Duy836ef2b2018-08-18 16:41:26 +02003437 dst->cache_tree = src->cache_tree;
3438 src->cache_tree = NULL;
David Turneredf3b902017-05-08 11:41:42 +02003439}
Jameson Millera8497352018-07-02 19:49:31 +00003440
Jameson Miller8e72d672018-07-02 19:49:37 +00003441struct cache_entry *dup_cache_entry(const struct cache_entry *ce,
3442 struct index_state *istate)
3443{
3444 unsigned int size = ce_size(ce);
3445 int mem_pool_allocated;
3446 struct cache_entry *new_entry = make_empty_cache_entry(istate, ce_namelen(ce));
3447 mem_pool_allocated = new_entry->mem_pool_allocated;
3448
3449 memcpy(new_entry, ce, size);
3450 new_entry->mem_pool_allocated = mem_pool_allocated;
3451 return new_entry;
3452}
3453
Jameson Millera8497352018-07-02 19:49:31 +00003454void discard_cache_entry(struct cache_entry *ce)
3455{
Jameson Miller8616a2d2018-07-02 19:49:39 +00003456 if (ce && should_validate_cache_entries())
3457 memset(ce, 0xCD, cache_entry_size(ce->ce_namelen));
3458
Jameson Miller8e72d672018-07-02 19:49:37 +00003459 if (ce && ce->mem_pool_allocated)
3460 return;
3461
Jameson Millera8497352018-07-02 19:49:31 +00003462 free(ce);
3463}
Jameson Miller8616a2d2018-07-02 19:49:39 +00003464
3465int should_validate_cache_entries(void)
3466{
3467 static int validate_index_cache_entries = -1;
3468
3469 if (validate_index_cache_entries < 0) {
3470 if (getenv("GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES"))
3471 validate_index_cache_entries = 1;
3472 else
3473 validate_index_cache_entries = 0;
3474 }
3475
3476 return validate_index_cache_entries;
3477}
Ben Peart3b1d9e02018-10-10 11:59:34 -04003478
3479#define EOIE_SIZE (4 + GIT_SHA1_RAWSZ) /* <4-byte offset> + <20-byte hash> */
3480#define EOIE_SIZE_WITH_HEADER (4 + 4 + EOIE_SIZE) /* <4-byte signature> + <4-byte length> + EOIE_SIZE */
3481
3482static size_t read_eoie_extension(const char *mmap, size_t mmap_size)
3483{
3484 /*
3485 * The end of index entries (EOIE) extension is guaranteed to be last
3486 * so that it can be found by scanning backwards from the EOF.
3487 *
3488 * "EOIE"
3489 * <4-byte length>
3490 * <4-byte offset>
3491 * <20-byte hash>
3492 */
3493 const char *index, *eoie;
3494 uint32_t extsize;
3495 size_t offset, src_offset;
3496 unsigned char hash[GIT_MAX_RAWSZ];
3497 git_hash_ctx c;
3498
3499 /* ensure we have an index big enough to contain an EOIE extension */
3500 if (mmap_size < sizeof(struct cache_header) + EOIE_SIZE_WITH_HEADER + the_hash_algo->rawsz)
3501 return 0;
3502
3503 /* validate the extension signature */
3504 index = eoie = mmap + mmap_size - EOIE_SIZE_WITH_HEADER - the_hash_algo->rawsz;
3505 if (CACHE_EXT(index) != CACHE_EXT_ENDOFINDEXENTRIES)
3506 return 0;
3507 index += sizeof(uint32_t);
3508
3509 /* validate the extension size */
3510 extsize = get_be32(index);
3511 if (extsize != EOIE_SIZE)
3512 return 0;
3513 index += sizeof(uint32_t);
3514
3515 /*
3516 * Validate the offset we're going to look for the first extension
3517 * signature is after the index header and before the eoie extension.
3518 */
3519 offset = get_be32(index);
3520 if (mmap + offset < mmap + sizeof(struct cache_header))
3521 return 0;
3522 if (mmap + offset >= eoie)
3523 return 0;
3524 index += sizeof(uint32_t);
3525
3526 /*
3527 * The hash is computed over extension types and their sizes (but not
3528 * their contents). E.g. if we have "TREE" extension that is N-bytes
3529 * long, "REUC" extension that is M-bytes long, followed by "EOIE",
3530 * then the hash would be:
3531 *
3532 * SHA-1("TREE" + <binary representation of N> +
3533 * "REUC" + <binary representation of M>)
3534 */
3535 src_offset = offset;
3536 the_hash_algo->init_fn(&c);
3537 while (src_offset < mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER) {
3538 /* After an array of active_nr index entries,
3539 * there can be arbitrary number of extended
3540 * sections, each of which is prefixed with
3541 * extension name (4-byte) and section length
3542 * in 4-byte network byte order.
3543 */
3544 uint32_t extsize;
3545 memcpy(&extsize, mmap + src_offset + 4, 4);
3546 extsize = ntohl(extsize);
3547
3548 /* verify the extension size isn't so large it will wrap around */
3549 if (src_offset + 8 + extsize < src_offset)
3550 return 0;
3551
3552 the_hash_algo->update_fn(&c, mmap + src_offset, 8);
3553
3554 src_offset += 8;
3555 src_offset += extsize;
3556 }
3557 the_hash_algo->final_fn(hash, &c);
3558 if (!hasheq(hash, (const unsigned char *)index))
3559 return 0;
3560
3561 /* Validate that the extension offsets returned us back to the eoie extension. */
3562 if (src_offset != mmap_size - the_hash_algo->rawsz - EOIE_SIZE_WITH_HEADER)
3563 return 0;
3564
3565 return offset;
3566}
3567
3568static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset)
3569{
3570 uint32_t buffer;
3571 unsigned char hash[GIT_MAX_RAWSZ];
3572
3573 /* offset */
3574 put_be32(&buffer, offset);
3575 strbuf_add(sb, &buffer, sizeof(uint32_t));
3576
3577 /* hash */
3578 the_hash_algo->final_fn(hash, eoie_context);
3579 strbuf_add(sb, hash, the_hash_algo->rawsz);
3580}
Ben Peart32550892018-10-10 11:59:37 -04003581
Ben Peart32550892018-10-10 11:59:37 -04003582#define IEOT_VERSION (1)
3583
3584static struct index_entry_offset_table *read_ieot_extension(const char *mmap, size_t mmap_size, size_t offset)
3585{
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003586 const char *index = NULL;
3587 uint32_t extsize, ext_version;
3588 struct index_entry_offset_table *ieot;
3589 int i, nr;
Ben Peart32550892018-10-10 11:59:37 -04003590
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003591 /* find the IEOT extension */
3592 if (!offset)
3593 return NULL;
3594 while (offset <= mmap_size - the_hash_algo->rawsz - 8) {
3595 extsize = get_be32(mmap + offset + 4);
3596 if (CACHE_EXT((mmap + offset)) == CACHE_EXT_INDEXENTRYOFFSETTABLE) {
3597 index = mmap + offset + 4 + 4;
3598 break;
3599 }
3600 offset += 8;
3601 offset += extsize;
3602 }
3603 if (!index)
3604 return NULL;
Ben Peart32550892018-10-10 11:59:37 -04003605
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003606 /* validate the version is IEOT_VERSION */
3607 ext_version = get_be32(index);
3608 if (ext_version != IEOT_VERSION) {
3609 error("invalid IEOT version %d", ext_version);
3610 return NULL;
3611 }
3612 index += sizeof(uint32_t);
Ben Peart32550892018-10-10 11:59:37 -04003613
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003614 /* extension size - version bytes / bytes per entry */
3615 nr = (extsize - sizeof(uint32_t)) / (sizeof(uint32_t) + sizeof(uint32_t));
3616 if (!nr) {
3617 error("invalid number of IEOT entries %d", nr);
3618 return NULL;
3619 }
3620 ieot = xmalloc(sizeof(struct index_entry_offset_table)
3621 + (nr * sizeof(struct index_entry_offset)));
3622 ieot->nr = nr;
3623 for (i = 0; i < nr; i++) {
3624 ieot->entries[i].offset = get_be32(index);
3625 index += sizeof(uint32_t);
3626 ieot->entries[i].nr = get_be32(index);
3627 index += sizeof(uint32_t);
3628 }
Ben Peart32550892018-10-10 11:59:37 -04003629
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003630 return ieot;
Ben Peart32550892018-10-10 11:59:37 -04003631}
3632
3633static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot)
3634{
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003635 uint32_t buffer;
3636 int i;
Ben Peart32550892018-10-10 11:59:37 -04003637
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003638 /* version */
3639 put_be32(&buffer, IEOT_VERSION);
3640 strbuf_add(sb, &buffer, sizeof(uint32_t));
Ben Peart32550892018-10-10 11:59:37 -04003641
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003642 /* ieot */
3643 for (i = 0; i < ieot->nr; i++) {
Ben Peart32550892018-10-10 11:59:37 -04003644
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003645 /* offset */
3646 put_be32(&buffer, ieot->entries[i].offset);
3647 strbuf_add(sb, &buffer, sizeof(uint32_t));
Ben Peart32550892018-10-10 11:59:37 -04003648
Nguyễn Thái Ngọc Duyec36c422018-12-06 16:42:06 +01003649 /* count */
3650 put_be32(&buffer, ieot->entries[i].nr);
3651 strbuf_add(sb, &buffer, sizeof(uint32_t));
3652 }
Ben Peart32550892018-10-10 11:59:37 -04003653}