blob: 38d67faf708d7b88f208f10dad23f893dd16f587 [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 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07006#define NO_THE_INDEX_COMPATIBILITY_MACROS
Linus Torvaldse83c5162005-04-07 15:13:13 -07007#include "cache.h"
Michael Haggertyf6ecc622015-08-10 11:47:45 +02008#include "tempfile.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02009#include "lockfile.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070010#include "cache-tree.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070011#include "refs.h"
Alexandre Julliardd6168132007-08-11 23:59:01 +020012#include "dir.h"
Junio C Hamano041aee32008-07-21 01:24:17 -070013#include "tree.h"
14#include "commit.h"
Junio C Hamano39425812008-08-21 01:44:53 -070015#include "blob.h"
Junio C Hamanocfc57892009-12-25 00:30:51 -080016#include "resolve-undo.h"
Junio C Hamano6c9cd162012-04-03 15:53:15 -070017#include "strbuf.h"
18#include "varint.h"
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070019#include "split-index.h"
Jeff Kinga42643a2014-12-15 18:15:20 -050020#include "utf8.h"
Junio C Hamanobad68ec2006-04-24 21:18:58 -070021
Thomas Gummererb60e1882012-07-11 11:22:37 +020022/* Mask for the name length in ce_flags in the on-disk index */
23
24#define CE_NAMEMASK (0x0fff)
25
Junio C Hamanobad68ec2006-04-24 21:18:58 -070026/* Index extensions.
27 *
28 * The first letter should be 'A'..'Z' for extensions that are not
29 * necessary for a correct operation (i.e. optimization data).
30 * When new extensions are added that _needs_ to be understood in
31 * order to correctly interpret the index file, pick character that
32 * is outside the range, to cause the reader to abort.
33 */
34
35#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
36#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
Shawn O. Pearceb659b492010-02-02 07:33:28 -080037#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070038#define CACHE_EXT_LINK 0x6c696e6b /* "link" */
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +070039#define CACHE_EXT_UNTRACKED 0x554E5452 /* "UNTR" */
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070040
41/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
Nguyễn Thái Ngọc Duye0cf0d72014-06-13 19:19:37 +070042#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +070043 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
Nguyễn Thái Ngọc Duy1bbb3db2015-03-08 17:12:39 +070044 SPLIT_INDEX_ORDERED | UNTRACKED_CHANGED)
Linus Torvaldse83c5162005-04-07 15:13:13 -070045
Junio C Hamano228e94f2007-04-01 18:14:06 -070046struct index_state the_index;
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +070047static const char *alternate_index_output;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -070048
Junio C Hamano9cb76b82008-01-22 23:01:13 -080049static void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
50{
51 istate->cache[nr] = ce;
Linus Torvalds96872bc2008-03-21 13:16:24 -070052 add_name_hash(istate, ce);
Junio C Hamano9cb76b82008-01-22 23:01:13 -080053}
54
Linus Torvaldscf558702008-01-22 18:41:14 -080055static void replace_index_entry(struct index_state *istate, int nr, struct cache_entry *ce)
56{
57 struct cache_entry *old = istate->cache[nr];
58
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +070059 replace_index_entry_in_base(istate, old, ce);
Karsten Blees20926782013-02-28 00:57:48 +010060 remove_name_hash(istate, old);
Karsten Blees5699d172013-11-14 20:24:37 +010061 free(old);
Linus Torvaldsa22c6372008-02-22 20:37:40 -080062 set_index_entry(istate, nr, ce);
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +070063 ce->ce_flags |= CE_UPDATE_IN_BASE;
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +070064 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvaldscf558702008-01-22 18:41:14 -080065}
66
Petr Baudis81dc2302008-07-21 02:25:56 +020067void rename_index_entry_at(struct index_state *istate, int nr, const char *new_name)
68{
69 struct cache_entry *old = istate->cache[nr], *new;
70 int namelen = strlen(new_name);
71
72 new = xmalloc(cache_entry_size(namelen));
73 copy_cache_entry(new, old);
Karsten Blees419a5972013-11-14 20:22:27 +010074 new->ce_flags &= ~CE_HASHED;
Thomas Gummererb60e1882012-07-11 11:22:37 +020075 new->ce_namelen = namelen;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +070076 new->index = 0;
Petr Baudis81dc2302008-07-21 02:25:56 +020077 memcpy(new->name, new_name, namelen + 1);
78
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +070079 cache_tree_invalidate_path(istate, old->name);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +070080 untracked_cache_remove_from_index(istate, old->name);
Petr Baudis81dc2302008-07-21 02:25:56 +020081 remove_index_entry_at(istate, nr);
82 add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
83}
84
Michael Haggertyc21d39d2013-06-20 10:37:50 +020085void fill_stat_data(struct stat_data *sd, struct stat *st)
86{
87 sd->sd_ctime.sec = (unsigned int)st->st_ctime;
88 sd->sd_mtime.sec = (unsigned int)st->st_mtime;
89 sd->sd_ctime.nsec = ST_CTIME_NSEC(*st);
90 sd->sd_mtime.nsec = ST_MTIME_NSEC(*st);
91 sd->sd_dev = st->st_dev;
92 sd->sd_ino = st->st_ino;
93 sd->sd_uid = st->st_uid;
94 sd->sd_gid = st->st_gid;
95 sd->sd_size = st->st_size;
96}
97
98int match_stat_data(const struct stat_data *sd, struct stat *st)
99{
100 int changed = 0;
101
102 if (sd->sd_mtime.sec != (unsigned int)st->st_mtime)
103 changed |= MTIME_CHANGED;
104 if (trust_ctime && check_stat &&
105 sd->sd_ctime.sec != (unsigned int)st->st_ctime)
106 changed |= CTIME_CHANGED;
107
108#ifdef USE_NSEC
109 if (check_stat && sd->sd_mtime.nsec != ST_MTIME_NSEC(*st))
110 changed |= MTIME_CHANGED;
111 if (trust_ctime && check_stat &&
112 sd->sd_ctime.nsec != ST_CTIME_NSEC(*st))
113 changed |= CTIME_CHANGED;
114#endif
115
116 if (check_stat) {
117 if (sd->sd_uid != (unsigned int) st->st_uid ||
118 sd->sd_gid != (unsigned int) st->st_gid)
119 changed |= OWNER_CHANGED;
120 if (sd->sd_ino != (unsigned int) st->st_ino)
121 changed |= INODE_CHANGED;
122 }
123
124#ifdef USE_STDEV
125 /*
126 * st_dev breaks on network filesystems where different
127 * clients will have different views of what "device"
128 * the filesystem is on
129 */
130 if (check_stat && sd->sd_dev != (unsigned int) st->st_dev)
131 changed |= INODE_CHANGED;
132#endif
133
134 if (sd->sd_size != (unsigned int) st->st_size)
135 changed |= DATA_CHANGED;
136
137 return changed;
138}
139
Junio C Hamano415e96c2005-05-15 14:23:12 -0700140/*
141 * This only updates the "non-critical" parts of the directory
142 * cache, ie the parts that aren't tracked by GIT, and only used
143 * to validate the cache.
144 */
145void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
146{
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200147 fill_stat_data(&ce->ce_stat_data, st);
Junio C Hamano5f730762006-02-08 21:15:24 -0800148
149 if (assume_unchanged)
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800150 ce->ce_flags |= CE_VALID;
Junio C Hamanoeadb5832008-01-18 23:45:24 -0800151
152 if (S_ISREG(st->st_mode))
153 ce_mark_uptodate(ce);
Junio C Hamano415e96c2005-05-15 14:23:12 -0700154}
155
René Scharfe21a6b9f2013-06-02 17:46:52 +0200156static int ce_compare_data(const struct cache_entry *ce, struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800157{
158 int match = -1;
159 int fd = open(ce->name, O_RDONLY);
160
161 if (fd >= 0) {
162 unsigned char sha1[20];
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -0700163 if (!index_fd(sha1, fd, st, OBJ_BLOB, ce->name, 0))
brian m. carlson99d1a982016-09-05 20:07:52 +0000164 match = hashcmp(sha1, ce->oid.hash);
Linus Torvalds7f8508e2006-07-31 09:55:15 -0700165 /* index_fd() closed the file descriptor already */
Junio C Hamano29e4d362005-12-20 00:02:15 -0800166 }
167 return match;
168}
169
René Scharfe21a6b9f2013-06-02 17:46:52 +0200170static int ce_compare_link(const struct cache_entry *ce, size_t expected_size)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800171{
172 int match = -1;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800173 void *buffer;
174 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500175 enum object_type type;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800176 struct strbuf sb = STRBUF_INIT;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800177
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800178 if (strbuf_readlink(&sb, ce->name, expected_size))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800179 return -1;
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800180
brian m. carlson99d1a982016-09-05 20:07:52 +0000181 buffer = read_sha1_file(ce->oid.hash, &type, &size);
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800182 if (buffer) {
183 if (size == sb.len)
184 match = memcmp(buffer, sb.buf, size);
185 free(buffer);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800186 }
Linus Torvaldsa60272b2008-12-17 09:47:27 -0800187 strbuf_release(&sb);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800188 return match;
189}
190
René Scharfe21a6b9f2013-06-02 17:46:52 +0200191static int ce_compare_gitlink(const struct cache_entry *ce)
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700192{
193 unsigned char sha1[20];
194
195 /*
196 * We don't actually require that the .git directory
Martin Waitz302b9282007-05-21 22:08:28 +0200197 * under GITLINK directory be a valid git directory. It
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700198 * might even be missing (in case nobody populated that
199 * sub-project).
200 *
201 * If so, we consider it always to match.
202 */
203 if (resolve_gitlink_ref(ce->name, "HEAD", sha1) < 0)
204 return 0;
brian m. carlson99d1a982016-09-05 20:07:52 +0000205 return hashcmp(sha1, ce->oid.hash);
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700206}
207
René Scharfe21a6b9f2013-06-02 17:46:52 +0200208static int ce_modified_check_fs(const struct cache_entry *ce, struct stat *st)
Junio C Hamano29e4d362005-12-20 00:02:15 -0800209{
210 switch (st->st_mode & S_IFMT) {
211 case S_IFREG:
212 if (ce_compare_data(ce, st))
213 return DATA_CHANGED;
214 break;
215 case S_IFLNK:
Shawn O. Pearcedc49cd72007-03-06 20:44:37 -0500216 if (ce_compare_link(ce, xsize_t(st->st_size)))
Junio C Hamano29e4d362005-12-20 00:02:15 -0800217 return DATA_CHANGED;
218 break;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700219 case S_IFDIR:
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800220 if (S_ISGITLINK(ce->ce_mode))
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700221 return ce_compare_gitlink(ce) ? DATA_CHANGED : 0;
Junio C Hamano29e4d362005-12-20 00:02:15 -0800222 default:
223 return TYPE_CHANGED;
224 }
225 return 0;
226}
227
René Scharfe21a6b9f2013-06-02 17:46:52 +0200228static int ce_match_stat_basic(const struct cache_entry *ce, struct stat *st)
Linus Torvalds734aab72005-04-09 09:48:20 -0700229{
230 unsigned int changed = 0;
231
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800232 if (ce->ce_flags & CE_REMOVE)
233 return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED;
234
235 switch (ce->ce_mode & S_IFMT) {
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200236 case S_IFREG:
237 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
Junio C Hamano3e09cdf2005-10-11 18:45:33 -0700238 /* We consider only the owner x bit to be relevant for
239 * "mode changes"
240 */
241 if (trust_executable_bit &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800242 (0100 & (ce->ce_mode ^ st->st_mode)))
Kay Sieversffbe1ad2005-05-06 15:45:01 +0200243 changed |= MODE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200244 break;
245 case S_IFLNK:
Johannes Sixt78a8d642007-03-02 22:11:30 +0100246 if (!S_ISLNK(st->st_mode) &&
247 (has_symlinks || !S_ISREG(st->st_mode)))
248 changed |= TYPE_CHANGED;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200249 break;
Martin Waitz302b9282007-05-21 22:08:28 +0200250 case S_IFGITLINK:
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700251 /* We ignore most of the st_xxx fields for gitlinks */
Linus Torvaldsf35a6d32007-04-09 21:20:29 -0700252 if (!S_ISDIR(st->st_mode))
253 changed |= TYPE_CHANGED;
254 else if (ce_compare_gitlink(ce))
255 changed |= DATA_CHANGED;
Linus Torvaldsa8ee75b2007-04-13 09:24:13 -0700256 return changed;
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200257 default:
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800258 die("internal error: ce_mode is %o", ce->ce_mode);
Kay Sievers8ae0a8c2005-05-05 14:38:25 +0200259 }
Linus Torvaldsccc4feb2005-04-15 10:44:27 -0700260
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200261 changed |= match_stat_data(&ce->ce_stat_data, st);
Junio C Hamano29e4d362005-12-20 00:02:15 -0800262
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700263 /* Racily smudged entry? */
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200264 if (!ce->ce_stat_data.sd_size) {
brian m. carlson99d1a982016-09-05 20:07:52 +0000265 if (!is_empty_blob_sha1(ce->oid.hash))
Linus Torvaldsf49c2c22008-06-10 10:44:43 -0700266 changed |= DATA_CHANGED;
267 }
268
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800269 return changed;
270}
271
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700272static int is_racy_stat(const struct index_state *istate,
273 const struct stat_data *sd)
274{
275 return (istate->timestamp.sec &&
276#ifdef USE_NSEC
277 /* nanosecond timestamped files can also be racy! */
278 (istate->timestamp.sec < sd->sd_mtime.sec ||
279 (istate->timestamp.sec == sd->sd_mtime.sec &&
280 istate->timestamp.nsec <= sd->sd_mtime.nsec))
281#else
282 istate->timestamp.sec <= sd->sd_mtime.sec
283#endif
284 );
285}
286
René Scharfe21a6b9f2013-06-02 17:46:52 +0200287static int is_racy_timestamp(const struct index_state *istate,
288 const struct cache_entry *ce)
Junio C Hamano6d91da62008-01-21 00:44:50 -0800289{
Junio C Hamano050288d2008-05-03 17:24:28 -0700290 return (!S_ISGITLINK(ce->ce_mode) &&
Nguyễn Thái Ngọc Duy2bb4cda2015-03-08 17:12:36 +0700291 is_racy_stat(istate, &ce->ce_stat_data));
Junio C Hamano6d91da62008-01-21 00:44:50 -0800292}
293
Nguyễn Thái Ngọc Duyed4efab2015-03-08 17:12:37 +0700294int match_stat_data_racy(const struct index_state *istate,
295 const struct stat_data *sd, struct stat *st)
296{
297 if (is_racy_stat(istate, sd))
298 return MTIME_CHANGED;
299 return match_stat_data(sd, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800300}
301
Linus Torvaldsd1f128b2008-03-06 12:46:09 -0800302int ie_match_stat(const struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200303 const struct cache_entry *ce, struct stat *st,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800304 unsigned int options)
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800305{
Junio C Hamano5f730762006-02-08 21:15:24 -0800306 unsigned int changed;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800307 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700308 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800309 int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;
Junio C Hamano5f730762006-02-08 21:15:24 -0800310
311 /*
312 * If it's marked as always valid in the index, it's
313 * valid whatever the checked-out copy says.
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700314 *
315 * skip-worktree has the same effect with higher precedence
Junio C Hamano5f730762006-02-08 21:15:24 -0800316 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700317 if (!ignore_skip_worktree && ce_skip_worktree(ce))
318 return 0;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800319 if (!ignore_valid && (ce->ce_flags & CE_VALID))
Junio C Hamano5f730762006-02-08 21:15:24 -0800320 return 0;
321
Junio C Hamano331fcb52008-11-28 19:56:34 -0800322 /*
323 * Intent-to-add entries have not been added, so the index entry
324 * by definition never matches what is in the work tree until it
325 * actually gets added.
326 */
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +0700327 if (ce_intent_to_add(ce))
Junio C Hamano331fcb52008-11-28 19:56:34 -0800328 return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED;
329
Junio C Hamano5f730762006-02-08 21:15:24 -0800330 changed = ce_match_stat_basic(ce, st);
Junio C Hamano407c8eb2005-12-20 12:12:18 -0800331
Junio C Hamano29e4d362005-12-20 00:02:15 -0800332 /*
333 * Within 1 second of this sequence:
334 * echo xyzzy >file && git-update-index --add file
335 * running this command:
336 * echo frotz >file
337 * would give a falsely clean cache entry. The mtime and
338 * length match the cache, and other stat fields do not change.
339 *
340 * We could detect this at update-index time (the cache entry
341 * being registered/updated records the same time as "now")
342 * and delay the return from git-update-index, but that would
343 * effectively mean we can make at most one commit per second,
344 * which is not acceptable. Instead, we check cache entries
345 * whose mtime are the same as the index file timestamp more
Junio C Hamano5f730762006-02-08 21:15:24 -0800346 * carefully than others.
Junio C Hamano29e4d362005-12-20 00:02:15 -0800347 */
Junio C Hamano6d91da62008-01-21 00:44:50 -0800348 if (!changed && is_racy_timestamp(istate, ce)) {
Junio C Hamano42f77402006-08-15 21:38:07 -0700349 if (assume_racy_is_modified)
350 changed |= DATA_CHANGED;
351 else
352 changed |= ce_modified_check_fs(ce, st);
353 }
Junio C Hamano29e4d362005-12-20 00:02:15 -0800354
Linus Torvalds734aab72005-04-09 09:48:20 -0700355 return changed;
356}
357
Linus Torvaldsd1f128b2008-03-06 12:46:09 -0800358int ie_modified(const struct index_state *istate,
René Scharfe21a6b9f2013-06-02 17:46:52 +0200359 const struct cache_entry *ce,
360 struct stat *st, unsigned int options)
Junio C Hamanob0391892005-09-19 15:11:15 -0700361{
Junio C Hamano29e4d362005-12-20 00:02:15 -0800362 int changed, changed_fs;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -0800363
364 changed = ie_match_stat(istate, ce, st, options);
Junio C Hamanob0391892005-09-19 15:11:15 -0700365 if (!changed)
366 return 0;
Junio C Hamanob0391892005-09-19 15:11:15 -0700367 /*
368 * If the mode or type has changed, there's no point in trying
369 * to refresh the entry - it's not going to match
370 */
371 if (changed & (MODE_CHANGED | TYPE_CHANGED))
372 return changed;
373
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700374 /*
375 * Immediately after read-tree or update-index --cacheinfo,
376 * the length field is zero, as we have never even read the
377 * lstat(2) information once, and we cannot trust DATA_CHANGED
378 * returned by ie_match_stat() which in turn was returned by
379 * ce_match_stat_basic() to signal that the filesize of the
380 * blob changed. We have to actually go to the filesystem to
381 * see if the contents match, and if so, should answer "unchanged".
382 *
383 * The logic does not apply to gitlinks, as ce_match_stat_basic()
384 * already has checked the actual HEAD from the filesystem in the
385 * subproject. If ie_match_stat() already said it is different,
386 * then we know it is.
Junio C Hamanob0391892005-09-19 15:11:15 -0700387 */
Junio C Hamanoc70115b2008-07-29 01:13:44 -0700388 if ((changed & DATA_CHANGED) &&
Michael Haggertyc21d39d2013-06-20 10:37:50 +0200389 (S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size != 0))
Junio C Hamanob0391892005-09-19 15:11:15 -0700390 return changed;
391
Junio C Hamano29e4d362005-12-20 00:02:15 -0800392 changed_fs = ce_modified_check_fs(ce, st);
393 if (changed_fs)
394 return changed | changed_fs;
Junio C Hamanob0391892005-09-19 15:11:15 -0700395 return 0;
396}
397
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700398int base_name_compare(const char *name1, int len1, int mode1,
399 const char *name2, int len2, int mode2)
400{
401 unsigned char c1, c2;
402 int len = len1 < len2 ? len1 : len2;
403 int cmp;
404
405 cmp = memcmp(name1, name2, len);
406 if (cmp)
407 return cmp;
408 c1 = name1[len];
409 c2 = name2[len];
Linus Torvalds1833a922007-04-11 14:39:12 -0700410 if (!c1 && S_ISDIR(mode1))
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700411 c1 = '/';
Linus Torvalds1833a922007-04-11 14:39:12 -0700412 if (!c2 && S_ISDIR(mode2))
Linus Torvalds958ba6c2005-05-20 09:09:18 -0700413 c2 = '/';
414 return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
415}
416
Linus Torvalds0ab9e1e2008-03-05 18:25:10 -0800417/*
418 * df_name_compare() is identical to base_name_compare(), except it
419 * compares conflicting directory/file entries as equal. Note that
420 * while a directory name compares as equal to a regular file, they
421 * then individually compare _differently_ to a filename that has
422 * a dot after the basename (because '\0' < '.' < '/').
423 *
424 * This is used by routines that want to traverse the git namespace
425 * but then handle conflicting entries together when possible.
426 */
427int df_name_compare(const char *name1, int len1, int mode1,
428 const char *name2, int len2, int mode2)
429{
430 int len = len1 < len2 ? len1 : len2, cmp;
431 unsigned char c1, c2;
432
433 cmp = memcmp(name1, name2, len);
434 if (cmp)
435 return cmp;
436 /* Directories and files compare equal (same length, same name) */
437 if (len1 == len2)
438 return 0;
439 c1 = name1[len];
440 if (!c1 && S_ISDIR(mode1))
441 c1 = '/';
442 c2 = name2[len];
443 if (!c2 && S_ISDIR(mode2))
444 c2 = '/';
445 if (c1 == '/' && !c2)
446 return 0;
447 if (c2 == '/' && !c1)
448 return 0;
449 return c1 - c2;
450}
451
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700452int name_compare(const char *name1, size_t len1, const char *name2, size_t len2)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700453{
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700454 size_t min_len = (len1 < len2) ? len1 : len2;
455 int cmp = memcmp(name1, name2, min_len);
Linus Torvaldseb38c222005-04-09 09:26:55 -0700456 if (cmp)
457 return cmp;
458 if (len1 < len2)
459 return -1;
460 if (len1 > len2)
461 return 1;
Jeremiah Mahlerccdd4a02014-06-19 19:06:44 -0700462 return 0;
463}
464
465int cache_name_stage_compare(const char *name1, int len1, int stage1, const char *name2, int len2, int stage2)
466{
467 int cmp;
468
469 cmp = name_compare(name1, len1, name2, len2);
470 if (cmp)
471 return cmp;
Junio C Hamano5f730762006-02-08 21:15:24 -0800472
Thomas Gummererb60e1882012-07-11 11:22:37 +0200473 if (stage1 < stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700474 return -1;
Thomas Gummererb60e1882012-07-11 11:22:37 +0200475 if (stage1 > stage2)
Linus Torvalds95fd5bf2005-04-15 22:51:44 -0700476 return 1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700477 return 0;
478}
479
Junio C Hamano357e9c62012-09-15 22:44:31 -0700480static int index_name_stage_pos(const struct index_state *istate, const char *name, int namelen, int stage)
Linus Torvaldseb38c222005-04-09 09:26:55 -0700481{
482 int first, last;
483
484 first = 0;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700485 last = istate->cache_nr;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700486 while (last > first) {
487 int next = (last + first) >> 1;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700488 struct cache_entry *ce = istate->cache[next];
Thomas Gummererb60e1882012-07-11 11:22:37 +0200489 int cmp = cache_name_stage_compare(name, namelen, stage, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvaldseb38c222005-04-09 09:26:55 -0700490 if (!cmp)
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700491 return next;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700492 if (cmp < 0) {
493 last = next;
494 continue;
495 }
496 first = next+1;
497 }
Linus Torvalds76e7f4e2005-04-10 22:06:50 -0700498 return -first-1;
Linus Torvaldseb38c222005-04-09 09:26:55 -0700499}
500
Thomas Gummererb60e1882012-07-11 11:22:37 +0200501int index_name_pos(const struct index_state *istate, const char *name, int namelen)
502{
503 return index_name_stage_pos(istate, name, namelen, 0);
504}
505
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700506/* Remove entry, return true if there are more entries to go.. */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700507int remove_index_entry_at(struct index_state *istate, int pos)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700508{
Linus Torvaldscf558702008-01-22 18:41:14 -0800509 struct cache_entry *ce = istate->cache[pos];
510
Junio C Hamanocfc57892009-12-25 00:30:51 -0800511 record_resolve_undo(istate, ce);
Karsten Blees20926782013-02-28 00:57:48 +0100512 remove_name_hash(istate, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700513 save_or_free_index_entry(istate, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700514 istate->cache_changed |= CE_ENTRY_REMOVED;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700515 istate->cache_nr--;
516 if (pos >= istate->cache_nr)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700517 return 0;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700518 memmove(istate->cache + pos,
519 istate->cache + pos + 1,
520 (istate->cache_nr - pos) * sizeof(struct cache_entry *));
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700521 return 1;
522}
523
Kjetil Barvik36419c82009-02-18 23:18:03 +0100524/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +0200525 * Remove all cache entries marked for removal, that is where
Kjetil Barvik36419c82009-02-18 23:18:03 +0100526 * CE_REMOVE is set in ce_flags. This is much more effective than
527 * calling remove_index_entry_at() for each entry to be removed.
528 */
529void remove_marked_cache_entries(struct index_state *istate)
530{
531 struct cache_entry **ce_array = istate->cache;
532 unsigned int i, j;
533
534 for (i = j = 0; i < istate->cache_nr; i++) {
Karsten Blees5699d172013-11-14 20:24:37 +0100535 if (ce_array[i]->ce_flags & CE_REMOVE) {
Karsten Blees20926782013-02-28 00:57:48 +0100536 remove_name_hash(istate, ce_array[i]);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700537 save_or_free_index_entry(istate, ce_array[i]);
Karsten Blees5699d172013-11-14 20:24:37 +0100538 }
Kjetil Barvik36419c82009-02-18 23:18:03 +0100539 else
540 ce_array[j++] = ce_array[i];
541 }
Nguyễn Thái Ngọc Duyad837d92014-06-13 19:19:26 +0700542 if (j == istate->cache_nr)
543 return;
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +0700544 istate->cache_changed |= CE_ENTRY_REMOVED;
Kjetil Barvik36419c82009-02-18 23:18:03 +0100545 istate->cache_nr = j;
546}
547
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700548int remove_file_from_index(struct index_state *istate, const char *path)
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700549{
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700550 int pos = index_name_pos(istate, path, strlen(path));
Junio C Hamanoc4e3cca2005-04-17 09:53:35 -0700551 if (pos < 0)
552 pos = -pos-1;
Nguyễn Thái Ngọc Duya5400ef2014-06-13 19:19:31 +0700553 cache_tree_invalidate_path(istate, path);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +0700554 untracked_cache_remove_from_index(istate, path);
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700555 while (pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path))
556 remove_index_entry_at(istate, pos);
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700557 return 0;
558}
559
Johannes Schindelin20314272007-06-29 18:32:46 +0100560static int compare_name(struct cache_entry *ce, const char *path, int namelen)
561{
562 return namelen != ce_namelen(ce) || memcmp(path, ce->name, namelen);
563}
564
565static int index_name_pos_also_unmerged(struct index_state *istate,
566 const char *path, int namelen)
567{
568 int pos = index_name_pos(istate, path, namelen);
569 struct cache_entry *ce;
570
571 if (pos >= 0)
572 return pos;
573
574 /* maybe unmerged? */
575 pos = -1 - pos;
576 if (pos >= istate->cache_nr ||
577 compare_name((ce = istate->cache[pos]), path, namelen))
578 return -1;
579
580 /* order of preference: stage 2, 1, 3 */
581 if (ce_stage(ce) == 1 && pos + 1 < istate->cache_nr &&
582 ce_stage((ce = istate->cache[pos + 1])) == 2 &&
583 !compare_name(ce, path, namelen))
584 pos++;
585 return pos;
586}
587
Linus Torvalds11029522008-03-22 14:22:44 -0700588static int different_name(struct cache_entry *ce, struct cache_entry *alias)
589{
590 int len = ce_namelen(ce);
591 return ce_namelen(alias) != len || memcmp(ce->name, alias->name, len);
592}
593
594/*
595 * If we add a filename that aliases in the cache, we will use the
596 * name that we already have - but we don't want to update the same
597 * alias twice, because that implies that there were actually two
598 * different files with aliasing names!
599 *
600 * So we use the CE_ADDED flag to verify that the alias was an old
601 * one before we accept it as
602 */
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700603static struct cache_entry *create_alias_ce(struct index_state *istate,
604 struct cache_entry *ce,
605 struct cache_entry *alias)
Linus Torvalds11029522008-03-22 14:22:44 -0700606{
607 int len;
608 struct cache_entry *new;
609
610 if (alias->ce_flags & CE_ADDED)
611 die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name);
612
613 /* Ok, create the new entry using the name of the existing alias */
614 len = ce_namelen(alias);
615 new = xcalloc(1, cache_entry_size(len));
616 memcpy(new->name, alias->name, len);
617 copy_cache_entry(new, ce);
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700618 save_or_free_index_entry(istate, ce);
Linus Torvalds11029522008-03-22 14:22:44 -0700619 return new;
620}
621
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700622void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
Junio C Hamano39425812008-08-21 01:44:53 -0700623{
624 unsigned char sha1[20];
625 if (write_sha1_file("", 0, blob_type, sha1))
626 die("cannot create an empty blob in the object database");
brian m. carlson99d1a982016-09-05 20:07:52 +0000627 hashcpy(ce->oid.hash, sha1);
Junio C Hamano39425812008-08-21 01:44:53 -0700628}
629
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100630int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200631{
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700632 int size, namelen, was_same;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700633 mode_t st_mode = st->st_mode;
Linus Torvalds68355502008-03-22 13:19:49 -0700634 struct cache_entry *ce, *alias;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +0700635 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 -0700636 int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND);
637 int pretend = flags & ADD_CACHE_PRETEND;
Junio C Hamano39425812008-08-21 01:44:53 -0700638 int intent_only = flags & ADD_CACHE_INTENT;
639 int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|
640 (intent_only ? ADD_CACHE_NEW_ONLY : 0));
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200641
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700642 if (!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode))
Alex Riesen960b8ad2008-05-12 19:57:45 +0200643 return error("%s: can only add regular files, symbolic links or git-directories", path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200644
645 namelen = strlen(path);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700646 if (S_ISDIR(st_mode)) {
Linus Torvalds09595252007-04-11 14:49:44 -0700647 while (namelen && path[namelen-1] == '/')
648 namelen--;
649 }
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200650 size = cache_entry_size(namelen);
651 ce = xcalloc(1, size);
652 memcpy(ce->name, path, namelen);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200653 ce->ce_namelen = namelen;
Junio C Hamano39425812008-08-21 01:44:53 -0700654 if (!intent_only)
655 fill_stat_cache_info(ce, st);
Junio C Hamano388b2ac2008-11-28 19:55:25 -0800656 else
657 ce->ce_flags |= CE_INTENT_TO_ADD;
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200658
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100659
660 if (trust_executable_bit && has_symlinks) {
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700661 ce->ce_mode = create_ce_mode(st_mode);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100662 } else {
Johannes Sixt78a8d642007-03-02 22:11:30 +0100663 /* If there is an existing entry, pick the mode bits and type
664 * from it, otherwise assume unexecutable regular file.
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200665 */
Junio C Hamano185c9752007-02-16 22:43:48 -0800666 struct cache_entry *ent;
Johannes Schindelin20314272007-06-29 18:32:46 +0100667 int pos = index_name_pos_also_unmerged(istate, path, namelen);
Junio C Hamano185c9752007-02-16 22:43:48 -0800668
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700669 ent = (0 <= pos) ? istate->cache[pos] : NULL;
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700670 ce->ce_mode = ce_mode_from_stat(ent, st_mode);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200671 }
672
Joshua Jensendc1ae702010-10-03 09:56:45 +0000673 /* When core.ignorecase=true, determine if a directory of the same name but differing
674 * case already exists within the Git repository. If it does, ensure the directory
675 * case of the file being added to the repository matches (is folded into) the existing
676 * entry's directory case.
677 */
678 if (ignore_case) {
David Turner41284eb2015-10-21 13:54:11 -0400679 adjust_dirname_case(istate, ce->name);
Joshua Jensendc1ae702010-10-03 09:56:45 +0000680 }
681
Eric Sunshineebbd7432013-09-17 03:06:15 -0400682 alias = index_file_exists(istate, ce->name, ce_namelen(ce), ignore_case);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700683 if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) {
Junio C Hamano0781b8a2007-07-30 17:12:58 -0700684 /* Nothing changed, really */
Junio C Hamano125fd982010-01-24 00:10:20 -0800685 if (!S_ISGITLINK(alias->ce_mode))
686 ce_mark_uptodate(alias);
Linus Torvalds11029522008-03-22 14:22:44 -0700687 alias->ce_flags |= CE_ADDED;
Stefan Beller2d9426b2015-03-20 17:28:00 -0700688
689 free(ce);
Junio C Hamano0781b8a2007-07-30 17:12:58 -0700690 return 0;
691 }
Junio C Hamano39425812008-08-21 01:44:53 -0700692 if (!intent_only) {
brian m. carlson99d1a982016-09-05 20:07:52 +0000693 if (index_path(ce->oid.hash, path, st, HASH_WRITE_OBJECT)) {
Stefan Beller2d9426b2015-03-20 17:28:00 -0700694 free(ce);
Junio C Hamano39425812008-08-21 01:44:53 -0700695 return error("unable to index file %s", path);
Stefan Beller2d9426b2015-03-20 17:28:00 -0700696 }
Junio C Hamano39425812008-08-21 01:44:53 -0700697 } else
Nguyễn Thái Ngọc Duyb4b313f2014-02-04 09:20:09 +0700698 set_object_name_for_intent_to_add_entry(ce);
Junio C Hamano39425812008-08-21 01:44:53 -0700699
Linus Torvalds11029522008-03-22 14:22:44 -0700700 if (ignore_case && alias && different_name(ce, alias))
Nguyễn Thái Ngọc Duy045113a2014-06-13 19:19:38 +0700701 ce = create_alias_ce(istate, ce, alias);
Linus Torvalds11029522008-03-22 14:22:44 -0700702 ce->ce_flags |= CE_ADDED;
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700703
Junio C Hamano3bf0dd12008-07-16 18:48:58 -0700704 /* It was suspected to be racily clean, but it turns out to be Ok */
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700705 was_same = (alias &&
706 !ce_stage(alias) &&
brian m. carlson99d1a982016-09-05 20:07:52 +0000707 !oidcmp(&alias->oid, &ce->oid) &&
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700708 ce->ce_mode == alias->ce_mode);
709
710 if (pretend)
Junio C Hamano067178e2015-03-23 10:58:00 -0700711 free(ce);
712 else if (add_index_entry(istate, ce, add_option)) {
713 free(ce);
714 return error("unable to add %s to index", path);
715 }
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700716 if (verbose && !was_same)
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200717 printf("add '%s'\n", path);
Johannes Schindelin11be42a2006-07-26 03:52:35 +0200718 return 0;
719}
720
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100721int add_file_to_index(struct index_state *istate, const char *path, int flags)
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700722{
723 struct stat st;
724 if (lstat(path, &st))
Thomas Rastd824cbb2009-06-27 17:58:46 +0200725 die_errno("unable to stat '%s'", path);
Thomas Gummerer610d55a2016-09-14 22:07:47 +0100726 return add_to_index(istate, path, &st, flags);
Linus Torvaldsd177cab2008-05-09 09:11:43 -0700727}
728
Carlos Rica6640f882007-09-11 05:17:28 +0200729struct cache_entry *make_cache_entry(unsigned int mode,
730 const unsigned char *sha1, const char *path, int stage,
Brad King25762722014-01-27 09:45:08 -0500731 unsigned int refresh_options)
Carlos Rica6640f882007-09-11 05:17:28 +0200732{
733 int size, len;
Stefan Bellerbc1c2ca2015-02-17 10:06:14 -0800734 struct cache_entry *ce, *ret;
Carlos Rica6640f882007-09-11 05:17:28 +0200735
Dmitry Potapov7e7abea2008-10-11 20:39:37 +0400736 if (!verify_path(path)) {
737 error("Invalid path '%s'", path);
Carlos Rica6640f882007-09-11 05:17:28 +0200738 return NULL;
Dmitry Potapov7e7abea2008-10-11 20:39:37 +0400739 }
Carlos Rica6640f882007-09-11 05:17:28 +0200740
741 len = strlen(path);
742 size = cache_entry_size(len);
743 ce = xcalloc(1, size);
744
brian m. carlson99d1a982016-09-05 20:07:52 +0000745 hashcpy(ce->oid.hash, sha1);
Carlos Rica6640f882007-09-11 05:17:28 +0200746 memcpy(ce->name, path, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +0200747 ce->ce_flags = create_ce_flags(stage);
748 ce->ce_namelen = len;
Carlos Rica6640f882007-09-11 05:17:28 +0200749 ce->ce_mode = create_ce_mode(mode);
750
Stefan Bellerbc1c2ca2015-02-17 10:06:14 -0800751 ret = refresh_cache_entry(ce, refresh_options);
Stefan Beller915e44c2015-03-23 10:57:11 -0700752 if (ret != ce)
Stefan Bellerbc1c2ca2015-02-17 10:06:14 -0800753 free(ce);
Stefan Beller915e44c2015-03-23 10:57:11 -0700754 return ret;
Carlos Rica6640f882007-09-11 05:17:28 +0200755}
756
Thomas Gummererd9d70962016-09-14 22:07:46 +0100757/*
758 * Chmod an index entry with either +x or -x.
759 *
760 * Returns -1 if the chmod for the particular cache entry failed (if it's
761 * not a regular file), -2 if an invalid flip argument is passed in, 0
762 * otherwise.
763 */
764int chmod_index_entry(struct index_state *istate, struct cache_entry *ce,
765 char flip)
766{
767 if (!S_ISREG(ce->ce_mode))
768 return -1;
769 switch (flip) {
770 case '+':
771 ce->ce_mode |= 0111;
772 break;
773 case '-':
774 ce->ce_mode &= ~0111;
775 break;
776 default:
777 return -2;
778 }
779 cache_tree_invalidate_path(istate, ce->name);
780 ce->ce_flags |= CE_UPDATE_IN_BASE;
781 istate->cache_changed |= CE_ENTRY_CHANGED;
782
783 return 0;
784}
785
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700786int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Linus Torvalds7b937ca2005-04-16 12:05:45 -0700787{
788 int len = ce_namelen(a);
789 return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
790}
791
Linus Torvalds12676602005-06-18 20:21:34 -0700792/*
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700793 * We fundamentally don't like some paths: we don't want
794 * dot or dot-dot anywhere, and for obvious reasons don't
795 * want to recurse into ".git" either.
796 *
797 * Also, we don't want double slashes or slashes at the
798 * end that can make pathnames ambiguous.
799 */
800static int verify_dotfile(const char *rest)
801{
802 /*
803 * The first character was '.', but that
804 * has already been discarded, we now test
805 * the rest.
806 */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200807
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700808 /* "." is not allowed */
Theo Niessinke0f530f2011-06-08 14:04:41 +0200809 if (*rest == '\0' || is_dir_sep(*rest))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700810 return 0;
811
Theo Niessinke0f530f2011-06-08 14:04:41 +0200812 switch (*rest) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700813 /*
814 * ".git" followed by NUL or slash is bad. This
815 * shares the path end test with the ".." case.
816 */
817 case 'g':
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500818 case 'G':
819 if (rest[1] != 'i' && rest[1] != 'I')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700820 break;
Jeff Kingcc2fc7c2014-11-24 13:39:12 -0500821 if (rest[2] != 't' && rest[2] != 'T')
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700822 break;
823 rest += 2;
824 /* fallthrough */
825 case '.':
Theo Niessinke0f530f2011-06-08 14:04:41 +0200826 if (rest[1] == '\0' || is_dir_sep(rest[1]))
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700827 return 0;
828 }
829 return 1;
830}
831
832int verify_path(const char *path)
833{
834 char c;
835
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200836 if (has_dos_drive_prefix(path))
837 return 0;
838
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700839 goto inside;
840 for (;;) {
841 if (!c)
842 return 1;
Erik Faye-Lund56948cb2011-05-27 18:00:40 +0200843 if (is_dir_sep(c)) {
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700844inside:
Jeff Kinga42643a2014-12-15 18:15:20 -0500845 if (protect_hfs && is_hfs_dotgit(path))
846 return 0;
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +0100847 if (protect_ntfs && is_ntfs_dotgit(path))
848 return 0;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700849 c = *path++;
Junio C Hamano3bdf09c2011-06-06 20:49:06 -0700850 if ((c == '.' && !verify_dotfile(path)) ||
851 is_dir_sep(c) || c == '\0')
852 return 0;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -0700853 }
854 c = *path++;
855 }
856}
857
858/*
Linus Torvalds12676602005-06-18 20:21:34 -0700859 * Do we have another file that has the beginning components being a
860 * proper superset of the name we're trying to add?
861 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700862static int has_file_name(struct index_state *istate,
863 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -0700864{
865 int retval = 0;
866 int len = ce_namelen(ce);
Junio C Hamanob1557252005-06-25 02:25:29 -0700867 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -0700868 const char *name = ce->name;
869
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700870 while (pos < istate->cache_nr) {
871 struct cache_entry *p = istate->cache[pos++];
Linus Torvalds12676602005-06-18 20:21:34 -0700872
873 if (len >= ce_namelen(p))
874 break;
875 if (memcmp(name, p->name, len))
876 break;
Junio C Hamanob1557252005-06-25 02:25:29 -0700877 if (ce_stage(p) != stage)
878 continue;
Linus Torvalds12676602005-06-18 20:21:34 -0700879 if (p->name[len] != '/')
880 continue;
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800881 if (p->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700882 continue;
Linus Torvalds12676602005-06-18 20:21:34 -0700883 retval = -1;
884 if (!ok_to_replace)
885 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700886 remove_index_entry_at(istate, --pos);
Linus Torvalds12676602005-06-18 20:21:34 -0700887 }
888 return retval;
889}
890
891/*
892 * Do we have another file with a pathname that is a proper
893 * subset of the name we're trying to add?
894 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700895static int has_dir_name(struct index_state *istate,
896 const struct cache_entry *ce, int pos, int ok_to_replace)
Linus Torvalds12676602005-06-18 20:21:34 -0700897{
898 int retval = 0;
Junio C Hamanob1557252005-06-25 02:25:29 -0700899 int stage = ce_stage(ce);
Linus Torvalds12676602005-06-18 20:21:34 -0700900 const char *name = ce->name;
901 const char *slash = name + ce_namelen(ce);
902
903 for (;;) {
904 int len;
905
906 for (;;) {
907 if (*--slash == '/')
908 break;
909 if (slash <= ce->name)
910 return retval;
911 }
912 len = slash - name;
913
Thomas Gummererb60e1882012-07-11 11:22:37 +0200914 pos = index_name_stage_pos(istate, name, len, stage);
Linus Torvalds12676602005-06-18 20:21:34 -0700915 if (pos >= 0) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700916 /*
917 * Found one, but not so fast. This could
918 * be a marker that says "I was here, but
919 * I am being removed". Such an entry is
920 * not a part of the resulting tree, and
921 * it is Ok to have a directory at the same
922 * path.
923 */
Junio C Hamano077c48d2008-01-22 21:24:21 -0800924 if (!(istate->cache[pos]->ce_flags & CE_REMOVE)) {
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700925 retval = -1;
926 if (!ok_to_replace)
927 break;
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700928 remove_index_entry_at(istate, pos);
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700929 continue;
930 }
Linus Torvalds12676602005-06-18 20:21:34 -0700931 }
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700932 else
933 pos = -pos-1;
Linus Torvalds12676602005-06-18 20:21:34 -0700934
935 /*
936 * Trivial optimization: if we find an entry that
937 * already matches the sub-directory, then we know
Junio C Hamanob1557252005-06-25 02:25:29 -0700938 * we're ok, and we can exit.
Linus Torvalds12676602005-06-18 20:21:34 -0700939 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700940 while (pos < istate->cache_nr) {
941 struct cache_entry *p = istate->cache[pos];
Junio C Hamanob1557252005-06-25 02:25:29 -0700942 if ((ce_namelen(p) <= len) ||
943 (p->name[len] != '/') ||
944 memcmp(p->name, name, len))
945 break; /* not our subdirectory */
Junio C Hamano077c48d2008-01-22 21:24:21 -0800946 if (ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE))
947 /*
948 * p is at the same stage as our entry, and
Junio C Hamanob1557252005-06-25 02:25:29 -0700949 * is a subdirectory of what we are looking
950 * at, so we cannot have conflicts at our
951 * level or anything shorter.
952 */
953 return retval;
954 pos++;
Linus Torvalds12676602005-06-18 20:21:34 -0700955 }
956 }
957 return retval;
958}
959
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700960/* We may be in a situation where we already have path/file and path
961 * is being added, or we already have path and path/file is being
962 * added. Either one would result in a nonsense tree that has path
963 * twice when git-write-tree tries to write it out. Prevent it.
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700964 *
Junio C Hamano192268c2005-05-07 21:55:21 -0700965 * If ok-to-replace is specified, we remove the conflicting entries
966 * from the cache so the caller should recompute the insert position.
967 * When this happens, we return non-zero.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700968 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700969static int check_file_directory_conflict(struct index_state *istate,
970 const struct cache_entry *ce,
971 int pos, int ok_to_replace)
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700972{
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700973 int retval;
974
975 /*
976 * When ce is an "I am going away" entry, we allow it to be added
977 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -0800978 if (ce->ce_flags & CE_REMOVE)
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700979 return 0;
980
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700981 /*
Linus Torvalds12676602005-06-18 20:21:34 -0700982 * We check if the path is a sub-path of a subsequent pathname
983 * first, since removing those will not change the position
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700984 * in the array.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700985 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700986 retval = has_file_name(istate, ce, pos, ok_to_replace);
Junio C Hamano21cd8d02007-03-30 01:55:37 -0700987
Linus Torvalds12676602005-06-18 20:21:34 -0700988 /*
989 * Then check if the path might have a clashing sub-directory
990 * before it.
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700991 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -0700992 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
Junio C Hamano0f1e4f02005-05-07 21:48:12 -0700993}
994
Junio C Hamanoaf3785d2007-08-09 13:42:50 -0700995static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
Linus Torvalds197ee8c2005-04-09 12:09:27 -0700996{
997 int pos;
Junio C Hamano192268c2005-05-07 21:55:21 -0700998 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
999 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
Junio C Hamanob1557252005-06-25 02:25:29 -07001000 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
Junio C Hamano39425812008-08-21 01:44:53 -07001001 int new_only = option & ADD_CACHE_NEW_ONLY;
Junio C Hamano5f730762006-02-08 21:15:24 -08001002
Nguyễn Thái Ngọc Duyce7c6142014-06-13 19:19:42 +07001003 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1004 cache_tree_invalidate_path(istate, ce->name);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001005 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001006
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001007 /* existing match? Just replace it. */
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001008 if (pos >= 0) {
Junio C Hamano39425812008-08-21 01:44:53 -07001009 if (!new_only)
1010 replace_index_entry(istate, pos, ce);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001011 return 0;
1012 }
Linus Torvalds76e7f4e2005-04-10 22:06:50 -07001013 pos = -pos-1;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001014
Nguyễn Thái Ngọc Duyffcc9ba2015-06-07 17:40:52 +07001015 if (!(option & ADD_CACHE_KEEP_CACHE_TREE))
1016 untracked_cache_add_to_index(istate, ce->name);
Nguyễn Thái Ngọc Duye9313712015-03-08 17:12:35 +07001017
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001018 /*
1019 * Inserting a merged entry ("stage 0") into the index
1020 * will always replace all non-merged entries..
1021 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001022 if (pos < istate->cache_nr && ce_stage(ce) == 0) {
1023 while (ce_same_name(istate->cache[pos], ce)) {
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001024 ok_to_add = 1;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001025 if (!remove_index_entry_at(istate, pos))
Linus Torvalds7b937ca2005-04-16 12:05:45 -07001026 break;
1027 }
1028 }
1029
Linus Torvalds121481a2005-04-10 11:32:54 -07001030 if (!ok_to_add)
1031 return -1;
Linus Torvalds8dcf39c2006-05-18 12:07:31 -07001032 if (!verify_path(ce->name))
Dmitry Potapov7e7abea2008-10-11 20:39:37 +04001033 return error("Invalid path '%s'", ce->name);
Linus Torvalds121481a2005-04-10 11:32:54 -07001034
Junio C Hamano3e09cdf2005-10-11 18:45:33 -07001035 if (!skip_df_check &&
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001036 check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {
Junio C Hamano192268c2005-05-07 21:55:21 -07001037 if (!ok_to_replace)
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001038 return error("'%s' appears as both a file and as a directory",
1039 ce->name);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001040 pos = index_name_stage_pos(istate, ce->name, ce_namelen(ce), ce_stage(ce));
Junio C Hamano192268c2005-05-07 21:55:21 -07001041 pos = -pos-1;
1042 }
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001043 return pos + 1;
1044}
1045
1046int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
1047{
1048 int pos;
1049
1050 if (option & ADD_CACHE_JUST_APPEND)
1051 pos = istate->cache_nr;
1052 else {
1053 int ret;
1054 ret = add_index_entry_with_check(istate, ce, option);
1055 if (ret <= 0)
1056 return ret;
1057 pos = ret - 1;
1058 }
Junio C Hamano0f1e4f02005-05-07 21:48:12 -07001059
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001060 /* Make sure the array is big enough .. */
Dmitry S. Dolzhenko999f5662014-03-04 02:32:01 +04001061 ALLOC_GROW(istate->cache, istate->cache_nr + 1, istate->cache_alloc);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001062
1063 /* Add it in.. */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001064 istate->cache_nr++;
Junio C Hamanoaf3785d2007-08-09 13:42:50 -07001065 if (istate->cache_nr > pos + 1)
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001066 memmove(istate->cache + pos + 1,
1067 istate->cache + pos,
1068 (istate->cache_nr - pos - 1) * sizeof(ce));
Linus Torvaldscf558702008-01-22 18:41:14 -08001069 set_index_entry(istate, pos, ce);
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001070 istate->cache_changed |= CE_ENTRY_ADDED;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07001071 return 0;
1072}
1073
Linus Torvalds405e5b22006-05-19 09:56:35 -07001074/*
1075 * "refresh" does not calculate a new sha1 file or bring the
1076 * cache up-to-date for mode/content changes. But what it
1077 * _does_ do is to "re-match" the stat information of a file
1078 * with the cache, so that you can refresh the cache for a
1079 * file that hasn't been changed but where the stat entry is
1080 * out of date.
1081 *
1082 * For example, you'd want to do this after doing a "git-read-tree",
1083 * to link up the stat cache details with the proper files.
1084 */
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001085static struct cache_entry *refresh_cache_ent(struct index_state *istate,
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001086 struct cache_entry *ce,
Jeff Kingd05e6972011-11-18 06:11:08 -05001087 unsigned int options, int *err,
1088 int *changed_ret)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001089{
1090 struct stat st;
1091 struct cache_entry *updated;
1092 int changed, size;
Brad King25762722014-01-27 09:45:08 -05001093 int refresh = options & CE_MATCH_REFRESH;
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001094 int ignore_valid = options & CE_MATCH_IGNORE_VALID;
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001095 int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;
Brad King2e2e7ec2014-01-27 09:45:07 -05001096 int ignore_missing = options & CE_MATCH_IGNORE_MISSING;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001097
Brad King25762722014-01-27 09:45:08 -05001098 if (!refresh || ce_uptodate(ce))
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001099 return ce;
1100
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001101 /*
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001102 * CE_VALID or CE_SKIP_WORKTREE means the user promised us
1103 * that the change to the work tree does not matter and told
1104 * us not to worry.
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001105 */
Nguyễn Thái Ngọc Duy56cac482009-12-14 18:43:58 +07001106 if (!ignore_skip_worktree && ce_skip_worktree(ce)) {
1107 ce_mark_uptodate(ce);
1108 return ce;
1109 }
Marius Storm-Olsenaa9349d2008-05-30 14:38:35 +02001110 if (!ignore_valid && (ce->ce_flags & CE_VALID)) {
1111 ce_mark_uptodate(ce);
1112 return ce;
1113 }
1114
René Scharfeccad42d2014-08-09 19:43:29 +02001115 if (has_symlink_leading_path(ce->name, ce_namelen(ce))) {
1116 if (ignore_missing)
1117 return ce;
1118 if (err)
1119 *err = ENOENT;
1120 return NULL;
1121 }
1122
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001123 if (lstat(ce->name, &st) < 0) {
Brad King2e2e7ec2014-01-27 09:45:07 -05001124 if (ignore_missing && errno == ENOENT)
1125 return ce;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001126 if (err)
1127 *err = errno;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001128 return NULL;
1129 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001130
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001131 changed = ie_match_stat(istate, ce, &st, options);
Jeff Kingd05e6972011-11-18 06:11:08 -05001132 if (changed_ret)
1133 *changed_ret = changed;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001134 if (!changed) {
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001135 /*
1136 * The path is unchanged. If we were told to ignore
1137 * valid bit, then we did the actual stat check and
1138 * found that the entry is unmodified. If the entry
1139 * is not marked VALID, this is the place to mark it
1140 * valid again, under "assume unchanged" mode.
1141 */
1142 if (ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001143 !(ce->ce_flags & CE_VALID))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001144 ; /* mark this one VALID again */
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001145 else {
1146 /*
1147 * We do not mark the index itself "modified"
1148 * because CE_UPTODATE flag is in-core only;
1149 * we are not going to write this change out.
1150 */
Junio C Hamano125fd982010-01-24 00:10:20 -08001151 if (!S_ISGITLINK(ce->ce_mode))
1152 ce_mark_uptodate(ce);
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001153 return ce;
Junio C Hamanoeadb5832008-01-18 23:45:24 -08001154 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001155 }
1156
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001157 if (ie_modified(istate, ce, &st, options)) {
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001158 if (err)
1159 *err = EINVAL;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001160 return NULL;
1161 }
Linus Torvalds405e5b22006-05-19 09:56:35 -07001162
1163 size = ce_size(ce);
1164 updated = xmalloc(size);
1165 memcpy(updated, ce, size);
1166 fill_stat_cache_info(updated, &st);
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001167 /*
1168 * If ignore_valid is not set, we should leave CE_VALID bit
1169 * alone. Otherwise, paths marked with --no-assume-unchanged
1170 * (i.e. things to be edited) will reacquire CE_VALID bit
1171 * automatically, which is not really what we want.
Linus Torvalds405e5b22006-05-19 09:56:35 -07001172 */
Junio C Hamano4bd5b7d2007-11-10 00:15:03 -08001173 if (!ignore_valid && assume_unchanged &&
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001174 !(ce->ce_flags & CE_VALID))
1175 updated->ce_flags &= ~CE_VALID;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001176
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001177 /* istate->cache_changed is updated in the caller */
Linus Torvalds405e5b22006-05-19 09:56:35 -07001178 return updated;
1179}
1180
Matthieu Moy3deffc52009-08-21 10:57:59 +02001181static void show_file(const char * fmt, const char * name, int in_porcelain,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001182 int * first, const char *header_msg)
Matthieu Moy3deffc52009-08-21 10:57:59 +02001183{
1184 if (in_porcelain && *first && header_msg) {
1185 printf("%s\n", header_msg);
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -07001186 *first = 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001187 }
1188 printf(fmt, name);
1189}
1190
Nguyễn Thái Ngọc Duy9b2d6142013-07-14 15:35:54 +07001191int refresh_index(struct index_state *istate, unsigned int flags,
1192 const struct pathspec *pathspec,
Jonathan Nieder046613c2011-02-22 22:43:23 +00001193 char *seen, const char *header_msg)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001194{
1195 int i;
1196 int has_errors = 0;
1197 int really = (flags & REFRESH_REALLY) != 0;
1198 int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
1199 int quiet = (flags & REFRESH_QUIET) != 0;
1200 int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001201 int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) != 0;
Matthieu Moy3deffc52009-08-21 10:57:59 +02001202 int first = 1;
1203 int in_porcelain = (flags & REFRESH_IN_PORCELAIN);
Brad King25762722014-01-27 09:45:08 -05001204 unsigned int options = (CE_MATCH_REFRESH |
1205 (really ? CE_MATCH_IGNORE_VALID : 0) |
Brad King2e2e7ec2014-01-27 09:45:07 -05001206 (not_new ? CE_MATCH_IGNORE_MISSING : 0));
Jeff King4bd4e732011-11-18 06:11:28 -05001207 const char *modified_fmt;
Jeff King73b7eae2011-11-18 06:13:08 -05001208 const char *deleted_fmt;
1209 const char *typechange_fmt;
1210 const char *added_fmt;
Jeff King4bd4e732011-11-18 06:11:28 -05001211 const char *unmerged_fmt;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001212
Jeff King4bd4e732011-11-18 06:11:28 -05001213 modified_fmt = (in_porcelain ? "M\t%s\n" : "%s: needs update\n");
Jeff King73b7eae2011-11-18 06:13:08 -05001214 deleted_fmt = (in_porcelain ? "D\t%s\n" : "%s: needs update\n");
1215 typechange_fmt = (in_porcelain ? "T\t%s\n" : "%s needs update\n");
1216 added_fmt = (in_porcelain ? "A\t%s\n" : "%s needs update\n");
Jeff King4bd4e732011-11-18 06:11:28 -05001217 unmerged_fmt = (in_porcelain ? "U\t%s\n" : "%s: needs merge\n");
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001218 for (i = 0; i < istate->cache_nr; i++) {
Linus Torvalds405e5b22006-05-19 09:56:35 -07001219 struct cache_entry *ce, *new;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001220 int cache_errno = 0;
Jeff King73b7eae2011-11-18 06:13:08 -05001221 int changed = 0;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001222 int filtered = 0;
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001223
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001224 ce = istate->cache[i];
Johannes Schindelin5fdeacb2008-05-14 18:03:45 +01001225 if (ignore_submodules && S_ISGITLINK(ce->ce_mode))
1226 continue;
1227
Nguyễn Thái Ngọc Duy429bb402014-01-24 20:40:28 +07001228 if (pathspec && !ce_path_match(ce, pathspec, seen))
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001229 filtered = 1;
1230
Linus Torvalds405e5b22006-05-19 09:56:35 -07001231 if (ce_stage(ce)) {
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001232 while ((i < istate->cache_nr) &&
1233 ! strcmp(istate->cache[i]->name, ce->name))
Linus Torvalds405e5b22006-05-19 09:56:35 -07001234 i++;
1235 i--;
1236 if (allow_unmerged)
1237 continue;
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001238 if (!filtered)
1239 show_file(unmerged_fmt, ce->name, in_porcelain,
1240 &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001241 has_errors = 1;
1242 continue;
1243 }
1244
Junio C Hamano3d1f1482012-02-17 10:11:05 -08001245 if (filtered)
Alexandre Julliardd6168132007-08-11 23:59:01 +02001246 continue;
1247
Jeff King73b7eae2011-11-18 06:13:08 -05001248 new = refresh_cache_ent(istate, ce, options, &cache_errno, &changed);
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001249 if (new == ce)
Linus Torvalds405e5b22006-05-19 09:56:35 -07001250 continue;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001251 if (!new) {
Jeff King73b7eae2011-11-18 06:13:08 -05001252 const char *fmt;
1253
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001254 if (really && cache_errno == EINVAL) {
Linus Torvalds405e5b22006-05-19 09:56:35 -07001255 /* If we are doing --really-refresh that
1256 * means the index is not valid anymore.
1257 */
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001258 ce->ce_flags &= ~CE_VALID;
Nguyễn Thái Ngọc Duy078a58e2014-06-13 19:19:39 +07001259 ce->ce_flags |= CE_UPDATE_IN_BASE;
Nguyễn Thái Ngọc Duye636a7b2014-06-13 19:19:27 +07001260 istate->cache_changed |= CE_ENTRY_CHANGED;
Linus Torvalds405e5b22006-05-19 09:56:35 -07001261 }
1262 if (quiet)
1263 continue;
Jeff King73b7eae2011-11-18 06:13:08 -05001264
1265 if (cache_errno == ENOENT)
1266 fmt = deleted_fmt;
Nguyễn Thái Ngọc Duy895ff3b2015-08-22 08:08:05 +07001267 else if (ce_intent_to_add(ce))
Jeff King73b7eae2011-11-18 06:13:08 -05001268 fmt = added_fmt; /* must be before other checks */
1269 else if (changed & TYPE_CHANGED)
1270 fmt = typechange_fmt;
1271 else
1272 fmt = modified_fmt;
1273 show_file(fmt,
1274 ce->name, in_porcelain, &first, header_msg);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001275 has_errors = 1;
1276 continue;
1277 }
Linus Torvaldscf558702008-01-22 18:41:14 -08001278
1279 replace_index_entry(istate, i, new);
Linus Torvalds405e5b22006-05-19 09:56:35 -07001280 }
1281 return has_errors;
1282}
1283
Junio C Hamano1335d762016-07-08 10:59:15 -07001284struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
Brad King25762722014-01-27 09:45:08 -05001285 unsigned int options)
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001286{
Brad King25762722014-01-27 09:45:08 -05001287 return refresh_cache_ent(&the_index, ce, options, NULL, NULL);
Junio C Hamanoec0cc702007-04-01 21:34:34 -07001288}
1289
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001290
1291/*****************************************************************
1292 * Index File I/O
1293 *****************************************************************/
1294
Junio C Hamano9d227782012-04-04 09:12:43 -07001295#define INDEX_FORMAT_DEFAULT 3
1296
Thomas Gummerer136347d2014-02-23 21:49:57 +01001297static unsigned int get_index_format_default(void)
1298{
1299 char *envversion = getenv("GIT_INDEX_VERSION");
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001300 char *endp;
Tanay Abhrab27a5722014-08-07 09:21:18 -07001301 int value;
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001302 unsigned int version = INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001303
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001304 if (!envversion) {
Tanay Abhrab27a5722014-08-07 09:21:18 -07001305 if (!git_config_get_int("index.version", &value))
1306 version = value;
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001307 if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1308 warning(_("index.version set, but the value is invalid.\n"
Thomas Gummerer136347d2014-02-23 21:49:57 +01001309 "Using version %i"), INDEX_FORMAT_DEFAULT);
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001310 return INDEX_FORMAT_DEFAULT;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001311 }
1312 return version;
1313 }
Thomas Gummerer3c09d682014-02-23 21:49:59 +01001314
1315 version = strtoul(envversion, &endp, 10);
1316 if (*endp ||
1317 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1318 warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1319 "Using version %i"), INDEX_FORMAT_DEFAULT);
1320 version = INDEX_FORMAT_DEFAULT;
1321 }
1322 return version;
Thomas Gummerer136347d2014-02-23 21:49:57 +01001323}
1324
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001325/*
1326 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1327 * Again - this is just a (very strong in practice) heuristic that
1328 * the inode hasn't changed.
1329 *
1330 * We save the fields in big-endian order to allow using the
1331 * index file over NFS transparently.
1332 */
1333struct ondisk_cache_entry {
1334 struct cache_time ctime;
1335 struct cache_time mtime;
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001336 uint32_t dev;
1337 uint32_t ino;
1338 uint32_t mode;
1339 uint32_t uid;
1340 uint32_t gid;
1341 uint32_t size;
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001342 unsigned char sha1[20];
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001343 uint16_t flags;
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001344 char name[FLEX_ARRAY]; /* more */
1345};
1346
1347/*
1348 * This struct is used when CE_EXTENDED bit is 1
1349 * The struct must match ondisk_cache_entry exactly from
1350 * ctime till flags
1351 */
1352struct ondisk_cache_entry_extended {
1353 struct cache_time ctime;
1354 struct cache_time mtime;
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001355 uint32_t dev;
1356 uint32_t ino;
1357 uint32_t mode;
1358 uint32_t uid;
1359 uint32_t gid;
1360 uint32_t size;
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001361 unsigned char sha1[20];
Thomas Gummerer7800c1e2013-08-18 21:41:51 +02001362 uint16_t flags;
1363 uint16_t flags2;
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001364 char name[FLEX_ARRAY]; /* more */
1365};
1366
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001367/* These are only used for v3 or lower */
Junio C Hamanodb3b3132012-04-03 15:53:09 -07001368#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
1369#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)
1370#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)
1371#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \
1372 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
1373 ondisk_cache_entry_size(ce_namelen(ce)))
1374
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001375static int verify_hdr(struct cache_header *hdr, unsigned long size)
Linus Torvaldse83c5162005-04-07 15:13:13 -07001376{
Nicolas Pitre9126f002008-10-01 14:05:20 -04001377 git_SHA_CTX c;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001378 unsigned char sha1[20];
Junio C Hamano0136bac2012-04-03 15:53:12 -07001379 int hdr_version;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001380
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07001381 if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
Linus Torvaldse83c5162005-04-07 15:13:13 -07001382 return error("bad signature");
Junio C Hamano0136bac2012-04-03 15:53:12 -07001383 hdr_version = ntohl(hdr->hdr_version);
Nguyễn Thái Ngọc Duyb82a7b52013-02-22 19:09:24 +07001384 if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
Junio C Hamano0136bac2012-04-03 15:53:12 -07001385 return error("bad index version %d", hdr_version);
Nicolas Pitre9126f002008-10-01 14:05:20 -04001386 git_SHA1_Init(&c);
1387 git_SHA1_Update(&c, hdr, size - 20);
1388 git_SHA1_Final(sha1, &c);
David Rientjesa89fccd2006-08-17 11:54:57 -07001389 if (hashcmp(sha1, (unsigned char *)hdr + size - 20))
Linus Torvaldsca9be052005-04-20 12:36:41 -07001390 return error("bad index file sha1 signature");
Linus Torvaldse83c5162005-04-07 15:13:13 -07001391 return 0;
1392}
1393
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001394static int read_index_extension(struct index_state *istate,
1395 const char *ext, void *data, unsigned long sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001396{
1397 switch (CACHE_EXT(ext)) {
1398 case CACHE_EXT_TREE:
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001399 istate->cache_tree = cache_tree_read(data, sz);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001400 break;
Junio C Hamanocfc57892009-12-25 00:30:51 -08001401 case CACHE_EXT_RESOLVE_UNDO:
1402 istate->resolve_undo = resolve_undo_read(data, sz);
1403 break;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001404 case CACHE_EXT_LINK:
1405 if (read_link_extension(istate, data, sz))
1406 return -1;
1407 break;
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07001408 case CACHE_EXT_UNTRACKED:
1409 istate->untracked = read_untracked_extension(data, sz);
1410 break;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001411 default:
1412 if (*ext < 'A' || 'Z' < *ext)
1413 return error("index uses %.4s extension, which we do not understand",
1414 ext);
1415 fprintf(stderr, "ignoring %.4s extension\n", ext);
1416 break;
1417 }
1418 return 0;
1419}
1420
Michael Haggerty216aab12014-10-01 12:28:41 +02001421int hold_locked_index(struct lock_file *lk, int die_on_error)
1422{
1423 return hold_lock_file_for_update(lk, get_index_file(),
1424 die_on_error
1425 ? LOCK_DIE_ON_ERROR
1426 : 0);
1427}
1428
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001429int read_index(struct index_state *istate)
Linus Torvaldse83c5162005-04-07 15:13:13 -07001430{
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001431 return read_index_from(istate, get_index_file());
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001432}
1433
Junio C Hamano3fc22b52012-04-03 15:53:13 -07001434static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,
1435 unsigned int flags,
1436 const char *name,
1437 size_t len)
1438{
1439 struct cache_entry *ce = xmalloc(cache_entry_size(len));
1440
Jeff Kingc3d8da52014-01-23 16:26:42 -05001441 ce->ce_stat_data.sd_ctime.sec = get_be32(&ondisk->ctime.sec);
1442 ce->ce_stat_data.sd_mtime.sec = get_be32(&ondisk->mtime.sec);
1443 ce->ce_stat_data.sd_ctime.nsec = get_be32(&ondisk->ctime.nsec);
1444 ce->ce_stat_data.sd_mtime.nsec = get_be32(&ondisk->mtime.nsec);
1445 ce->ce_stat_data.sd_dev = get_be32(&ondisk->dev);
1446 ce->ce_stat_data.sd_ino = get_be32(&ondisk->ino);
1447 ce->ce_mode = get_be32(&ondisk->mode);
1448 ce->ce_stat_data.sd_uid = get_be32(&ondisk->uid);
1449 ce->ce_stat_data.sd_gid = get_be32(&ondisk->gid);
1450 ce->ce_stat_data.sd_size = get_be32(&ondisk->size);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001451 ce->ce_flags = flags & ~CE_NAMEMASK;
1452 ce->ce_namelen = len;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001453 ce->index = 0;
brian m. carlson99d1a982016-09-05 20:07:52 +00001454 hashcpy(ce->oid.hash, ondisk->sha1);
Junio C Hamano3fc22b52012-04-03 15:53:13 -07001455 memcpy(ce->name, name, len);
1456 ce->name[len] = '\0';
1457 return ce;
1458}
1459
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001460/*
1461 * Adjacent cache entries tend to share the leading paths, so it makes
1462 * sense to only store the differences in later entries. In the v4
1463 * on-disk format of the index, each on-disk cache entry stores the
1464 * number of bytes to be stripped from the end of the previous name,
1465 * and the bytes to append to the result, to come up with its name.
1466 */
1467static unsigned long expand_name_field(struct strbuf *name, const char *cp_)
1468{
1469 const unsigned char *ep, *cp = (const unsigned char *)cp_;
1470 size_t len = decode_varint(&cp);
1471
1472 if (name->len < len)
1473 die("malformed name field in the index");
1474 strbuf_remove(name, name->len - len, len);
1475 for (ep = cp; *ep; ep++)
1476 ; /* find the end */
1477 strbuf_add(name, cp, ep - cp);
1478 return (const char *)ep + 1 - cp_;
1479}
1480
Junio C Hamano936f53d2012-04-03 15:53:11 -07001481static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001482 unsigned long *ent_size,
1483 struct strbuf *previous_name)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001484{
René Scharfedebed2a2011-10-24 23:59:14 +02001485 struct cache_entry *ce;
Junio C Hamano7fec10b2008-01-18 23:42:00 -08001486 size_t len;
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07001487 const char *name;
René Scharfedebed2a2011-10-24 23:59:14 +02001488 unsigned int flags;
1489
1490 /* On-disk flags are just 16 bits */
Jeff Kingc3d8da52014-01-23 16:26:42 -05001491 flags = get_be16(&ondisk->flags);
René Scharfedebed2a2011-10-24 23:59:14 +02001492 len = flags & CE_NAMEMASK;
1493
1494 if (flags & CE_EXTENDED) {
1495 struct ondisk_cache_entry_extended *ondisk2;
1496 int extended_flags;
1497 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
Jeff Kingc3d8da52014-01-23 16:26:42 -05001498 extended_flags = get_be16(&ondisk2->flags2) << 16;
René Scharfedebed2a2011-10-24 23:59:14 +02001499 /* We do not yet understand any bit out of CE_EXTENDED_FLAGS */
1500 if (extended_flags & ~CE_EXTENDED_FLAGS)
1501 die("Unknown index entry format %08x", extended_flags);
1502 flags |= extended_flags;
1503 name = ondisk2->name;
1504 }
1505 else
1506 name = ondisk->name;
1507
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001508 if (!previous_name) {
1509 /* v3 and earlier */
1510 if (len == CE_NAMEMASK)
1511 len = strlen(name);
1512 ce = cache_entry_from_ondisk(ondisk, flags, name, len);
René Scharfedebed2a2011-10-24 23:59:14 +02001513
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001514 *ent_size = ondisk_ce_size(ce);
1515 } else {
1516 unsigned long consumed;
1517 consumed = expand_name_field(previous_name, name);
1518 ce = cache_entry_from_ondisk(ondisk, flags,
1519 previous_name->buf,
1520 previous_name->len);
Junio C Hamano7fec10b2008-01-18 23:42:00 -08001521
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001522 *ent_size = (name - ((char *)ondisk)) + consumed;
1523 }
René Scharfedebed2a2011-10-24 23:59:14 +02001524 return ce;
Linus Torvaldscf558702008-01-22 18:41:14 -08001525}
1526
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001527static void check_ce_order(struct index_state *istate)
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001528{
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001529 unsigned int i;
1530
1531 for (i = 1; i < istate->cache_nr; i++) {
1532 struct cache_entry *ce = istate->cache[i - 1];
1533 struct cache_entry *next_ce = istate->cache[i];
1534 int name_compare = strcmp(ce->name, next_ce->name);
1535
1536 if (0 < name_compare)
1537 die("unordered stage entries in index");
1538 if (!name_compare) {
1539 if (!ce_stage(ce))
1540 die("multiple stage entries for merged file '%s'",
1541 ce->name);
1542 if (ce_stage(ce) > ce_stage(next_ce))
1543 die("unordered stage entries for '%s'",
1544 ce->name);
1545 }
Jaime Soriano Pastor15999d02014-08-29 10:54:41 +02001546 }
1547}
1548
Christian Couder435ec092016-01-27 07:58:05 +01001549static void tweak_untracked_cache(struct index_state *istate)
1550{
1551 switch (git_config_get_untracked_cache()) {
1552 case -1: /* keep: do nothing */
1553 break;
1554 case 0: /* false */
1555 remove_untracked_cache(istate);
1556 break;
1557 case 1: /* true */
1558 add_untracked_cache(istate);
1559 break;
1560 default: /* unknown value: do nothing */
1561 break;
1562 }
1563}
1564
1565static void post_read_index_from(struct index_state *istate)
1566{
1567 check_ce_order(istate);
1568 tweak_untracked_cache(istate);
1569}
1570
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001571/* remember to discard_cache() before reading a different cache! */
Nguyễn Thái Ngọc Duy3e52f702014-06-13 19:19:51 +07001572int do_read_index(struct index_state *istate, const char *path, int must_exist)
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001573{
Linus Torvaldse83c5162005-04-07 15:13:13 -07001574 int fd, i;
1575 struct stat st;
René Scharfedebed2a2011-10-24 23:59:14 +02001576 unsigned long src_offset;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001577 struct cache_header *hdr;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001578 void *mmap;
1579 size_t mmap_size;
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001580 struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001581
Junio C Hamano913e0e92008-08-23 12:57:30 -07001582 if (istate->initialized)
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001583 return istate->cache_nr;
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07001584
Kjetil Barvikfba2f382009-02-19 21:08:29 +01001585 istate->timestamp.sec = 0;
1586 istate->timestamp.nsec = 0;
Johannes Schindelin8fd2cb42006-07-25 21:32:18 -07001587 fd = open(path, O_RDONLY);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07001588 if (fd < 0) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001589 if (!must_exist && errno == ENOENT)
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07001590 return 0;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001591 die_errno("%s: index file open failed", path);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07001592 }
Linus Torvaldse83c5162005-04-07 15:13:13 -07001593
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03001594 if (fstat(fd, &st))
Thomas Rastd824cbb2009-06-27 17:58:46 +02001595 die_errno("cannot stat the open index");
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03001596
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001597 mmap_size = xsize_t(st.st_size);
1598 if (mmap_size < sizeof(struct cache_header) + 20)
Luiz Fernando N. Capitulino3511a372007-04-25 11:18:17 -03001599 die("index file smaller than expected");
1600
Jeff Kinga1293ef2015-05-28 03:54:00 -04001601 mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001602 if (mmap == MAP_FAILED)
Thomas Rast0721c312009-06-27 17:58:47 +02001603 die_errno("unable to map index file");
Nguyễn Thái Ngọc Duy57d84f82012-08-06 18:27:09 +07001604 close(fd);
Linus Torvaldse83c5162005-04-07 15:13:13 -07001605
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001606 hdr = mmap;
1607 if (verify_hdr(hdr, mmap_size) < 0)
Linus Torvaldse83c5162005-04-07 15:13:13 -07001608 goto unmap;
1609
Nguyễn Thái Ngọc Duye93021b2014-06-13 19:19:35 +07001610 hashcpy(istate->sha1, (const unsigned char *)hdr + mmap_size - 20);
Junio C Hamano9d227782012-04-04 09:12:43 -07001611 istate->version = ntohl(hdr->hdr_version);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001612 istate->cache_nr = ntohl(hdr->hdr_entries);
1613 istate->cache_alloc = alloc_nr(istate->cache_nr);
Felipe Contrerasc4aa3162013-05-30 08:56:19 -05001614 istate->cache = xcalloc(istate->cache_alloc, sizeof(*istate->cache));
Junio C Hamano913e0e92008-08-23 12:57:30 -07001615 istate->initialized = 1;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001616
Junio C Hamano9d227782012-04-04 09:12:43 -07001617 if (istate->version == 4)
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001618 previous_name = &previous_name_buf;
1619 else
1620 previous_name = NULL;
1621
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001622 src_offset = sizeof(*hdr);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001623 for (i = 0; i < istate->cache_nr; i++) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001624 struct ondisk_cache_entry *disk_ce;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001625 struct cache_entry *ce;
Junio C Hamano936f53d2012-04-03 15:53:11 -07001626 unsigned long consumed;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001627
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001628 disk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001629 ce = create_from_disk(disk_ce, &consumed, previous_name);
Linus Torvaldscf558702008-01-22 18:41:14 -08001630 set_index_entry(istate, i, ce);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001631
Junio C Hamano936f53d2012-04-03 15:53:11 -07001632 src_offset += consumed;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001633 }
Junio C Hamano6c9cd162012-04-03 15:53:15 -07001634 strbuf_release(&previous_name_buf);
Kjetil Barvikfba2f382009-02-19 21:08:29 +01001635 istate->timestamp.sec = st.st_mtime;
Kjetil Barvikc06ff492009-03-04 18:47:40 +01001636 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Kjetil Barvikfba2f382009-02-19 21:08:29 +01001637
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001638 while (src_offset <= mmap_size - 20 - 8) {
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001639 /* After an array of active_nr index entries,
1640 * there can be arbitrary number of extended
1641 * sections, each of which is prefixed with
1642 * extension name (4-byte) and section length
1643 * in 4-byte network byte order.
1644 */
Nathaniel W Filardo07cc8ec2009-12-27 01:11:21 -05001645 uint32_t extsize;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001646 memcpy(&extsize, (char *)mmap + src_offset + 4, 4);
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001647 extsize = ntohl(extsize);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001648 if (read_index_extension(istate,
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001649 (const char *) mmap + src_offset,
1650 (char *) mmap + src_offset + 8,
Florian Forster1d7f1712006-06-18 17:18:09 +02001651 extsize) < 0)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001652 goto unmap;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001653 src_offset += 8;
1654 src_offset += extsize;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001655 }
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001656 munmap(mmap, mmap_size);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001657 return istate->cache_nr;
Linus Torvaldse83c5162005-04-07 15:13:13 -07001658
1659unmap:
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001660 munmap(mmap, mmap_size);
Linus Torvalds5d1a5c02005-10-01 13:24:27 -07001661 die("index file corrupt");
Linus Torvaldse83c5162005-04-07 15:13:13 -07001662}
1663
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001664int read_index_from(struct index_state *istate, const char *path)
1665{
1666 struct split_index *split_index;
1667 int ret;
1668
1669 /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
1670 if (istate->initialized)
1671 return istate->cache_nr;
1672
1673 ret = do_read_index(istate, path, 0);
Christian Couder435ec092016-01-27 07:58:05 +01001674
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001675 split_index = istate->split_index;
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001676 if (!split_index || is_null_sha1(split_index->base_sha1)) {
Christian Couder435ec092016-01-27 07:58:05 +01001677 post_read_index_from(istate);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001678 return ret;
Thomas Gummerer03f15a72015-03-20 22:43:14 +01001679 }
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001680
1681 if (split_index->base)
1682 discard_index(split_index->base);
1683 else
1684 split_index->base = xcalloc(1, sizeof(*split_index->base));
1685 ret = do_read_index(split_index->base,
1686 git_path("sharedindex.%s",
1687 sha1_to_hex(split_index->base_sha1)), 1);
1688 if (hashcmp(split_index->base_sha1, split_index->base->sha1))
1689 die("broken index, expect %s in %s, got %s",
1690 sha1_to_hex(split_index->base_sha1),
1691 git_path("sharedindex.%s",
Stefan Beller6bea53c2015-08-31 11:43:29 -07001692 sha1_to_hex(split_index->base_sha1)),
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001693 sha1_to_hex(split_index->base->sha1));
1694 merge_base_index(istate);
Christian Couder435ec092016-01-27 07:58:05 +01001695 post_read_index_from(istate);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001696 return ret;
1697}
1698
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08001699int is_index_unborn(struct index_state *istate)
1700{
René Scharfedebed2a2011-10-24 23:59:14 +02001701 return (!istate->cache_nr && !istate->timestamp.sec);
Junio C Hamanofa7b3c22008-11-12 11:52:35 -08001702}
1703
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001704int discard_index(struct index_state *istate)
Johannes Schindelin6d297f82006-07-08 18:42:41 +02001705{
René Scharfedebed2a2011-10-24 23:59:14 +02001706 int i;
1707
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001708 for (i = 0; i < istate->cache_nr; i++) {
1709 if (istate->cache[i]->index &&
1710 istate->split_index &&
1711 istate->split_index->base &&
1712 istate->cache[i]->index <= istate->split_index->base->cache_nr &&
1713 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index - 1])
1714 continue;
René Scharfedebed2a2011-10-24 23:59:14 +02001715 free(istate->cache[i]);
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001716 }
Junio C Hamanocfc57892009-12-25 00:30:51 -08001717 resolve_undo_clear_index(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001718 istate->cache_nr = 0;
1719 istate->cache_changed = 0;
Kjetil Barvikfba2f382009-02-19 21:08:29 +01001720 istate->timestamp.sec = 0;
1721 istate->timestamp.nsec = 0;
Karsten Blees20926782013-02-28 00:57:48 +01001722 free_name_hash(istate);
Junio C Hamano4aab5b42007-04-01 23:26:07 -07001723 cache_tree_free(&(istate->cache_tree));
Junio C Hamano913e0e92008-08-23 12:57:30 -07001724 istate->initialized = 0;
René Scharfea0fc4db2013-06-09 19:39:18 +02001725 free(istate->cache);
1726 istate->cache = NULL;
1727 istate->cache_alloc = 0;
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07001728 discard_split_index(istate);
Nguyễn Thái Ngọc Duyf9e6c642015-03-08 17:12:34 +07001729 free_untracked_cache(istate->untracked);
1730 istate->untracked = NULL;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001731 return 0;
Johannes Schindelin6d297f82006-07-08 18:42:41 +02001732}
1733
Linus Torvaldsd1f128b2008-03-06 12:46:09 -08001734int unmerged_index(const struct index_state *istate)
Daniel Barkalow94a57282008-02-07 11:40:13 -05001735{
1736 int i;
1737 for (i = 0; i < istate->cache_nr; i++) {
1738 if (ce_stage(istate->cache[i]))
1739 return 1;
1740 }
1741 return 0;
1742}
1743
Linus Torvalds4990aad2005-04-20 12:16:57 -07001744#define WRITE_BUFFER_SIZE 8192
Brian Gerstbf0f9102005-05-18 08:14:09 -04001745static unsigned char write_buffer[WRITE_BUFFER_SIZE];
Linus Torvalds4990aad2005-04-20 12:16:57 -07001746static unsigned long write_buffer_len;
1747
Nicolas Pitre9126f002008-10-01 14:05:20 -04001748static int ce_write_flush(git_SHA_CTX *context, int fd)
Junio C Hamano6015c282006-08-08 14:47:32 -07001749{
1750 unsigned int buffered = write_buffer_len;
1751 if (buffered) {
Nicolas Pitre9126f002008-10-01 14:05:20 -04001752 git_SHA1_Update(context, write_buffer, buffered);
Andy Whitcroft93822c22007-01-08 15:58:23 +00001753 if (write_in_full(fd, write_buffer, buffered) != buffered)
Junio C Hamano6015c282006-08-08 14:47:32 -07001754 return -1;
1755 write_buffer_len = 0;
1756 }
1757 return 0;
1758}
1759
Nicolas Pitre9126f002008-10-01 14:05:20 -04001760static int ce_write(git_SHA_CTX *context, int fd, void *data, unsigned int len)
Linus Torvalds4990aad2005-04-20 12:16:57 -07001761{
1762 while (len) {
1763 unsigned int buffered = write_buffer_len;
1764 unsigned int partial = WRITE_BUFFER_SIZE - buffered;
1765 if (partial > len)
1766 partial = len;
1767 memcpy(write_buffer + buffered, data, partial);
1768 buffered += partial;
1769 if (buffered == WRITE_BUFFER_SIZE) {
Junio C Hamano6015c282006-08-08 14:47:32 -07001770 write_buffer_len = buffered;
1771 if (ce_write_flush(context, fd))
Linus Torvalds4990aad2005-04-20 12:16:57 -07001772 return -1;
1773 buffered = 0;
1774 }
1775 write_buffer_len = buffered;
1776 len -= partial;
Florian Forster1d7f1712006-06-18 17:18:09 +02001777 data = (char *) data + partial;
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001778 }
1779 return 0;
Linus Torvalds4990aad2005-04-20 12:16:57 -07001780}
1781
Nicolas Pitre9126f002008-10-01 14:05:20 -04001782static int write_index_ext_header(git_SHA_CTX *context, int fd,
Dennis Stosbergac58c7b2006-05-28 21:08:08 +02001783 unsigned int ext, unsigned int sz)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001784{
1785 ext = htonl(ext);
1786 sz = htonl(sz);
David Rientjes968a1d62006-08-14 13:38:14 -07001787 return ((ce_write(context, fd, &ext, 4) < 0) ||
1788 (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07001789}
1790
Nguyễn Thái Ngọc Duye93021b2014-06-13 19:19:35 +07001791static int ce_flush(git_SHA_CTX *context, int fd, unsigned char *sha1)
Linus Torvalds4990aad2005-04-20 12:16:57 -07001792{
1793 unsigned int left = write_buffer_len;
Linus Torvaldsca9be052005-04-20 12:36:41 -07001794
Linus Torvalds4990aad2005-04-20 12:16:57 -07001795 if (left) {
1796 write_buffer_len = 0;
Nicolas Pitre9126f002008-10-01 14:05:20 -04001797 git_SHA1_Update(context, write_buffer, left);
Linus Torvalds4990aad2005-04-20 12:16:57 -07001798 }
Linus Torvaldsca9be052005-04-20 12:36:41 -07001799
Qingning Huo2c865d92005-09-11 14:27:47 +01001800 /* Flush first if not enough space for SHA1 signature */
1801 if (left + 20 > WRITE_BUFFER_SIZE) {
Andy Whitcroft93822c22007-01-08 15:58:23 +00001802 if (write_in_full(fd, write_buffer, left) != left)
Qingning Huo2c865d92005-09-11 14:27:47 +01001803 return -1;
1804 left = 0;
1805 }
1806
Linus Torvaldsca9be052005-04-20 12:36:41 -07001807 /* Append the SHA1 signature at the end */
Nicolas Pitre9126f002008-10-01 14:05:20 -04001808 git_SHA1_Final(write_buffer + left, context);
Nguyễn Thái Ngọc Duye93021b2014-06-13 19:19:35 +07001809 hashcpy(sha1, write_buffer + left);
Linus Torvaldsca9be052005-04-20 12:36:41 -07001810 left += 20;
Andy Whitcroft93822c22007-01-08 15:58:23 +00001811 return (write_in_full(fd, write_buffer, left) != left) ? -1 : 0;
Linus Torvalds4990aad2005-04-20 12:16:57 -07001812}
1813
Junio C Hamano407c8eb2005-12-20 12:12:18 -08001814static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
1815{
1816 /*
1817 * The only thing we care about in this function is to smudge the
1818 * falsely clean entry due to touch-update-touch race, so we leave
1819 * everything else as they are. We are called for entries whose
Michael Haggertyc21d39d2013-06-20 10:37:50 +02001820 * ce_stat_data.sd_mtime match the index file mtime.
Junio C Hamanoc70115b2008-07-29 01:13:44 -07001821 *
1822 * Note that this actually does not do much for gitlinks, for
1823 * which ce_match_stat_basic() always goes to the actual
1824 * contents. The caller checks with is_racy_timestamp() which
1825 * always says "no" for gitlinks, so we are not called for them ;-)
Junio C Hamano407c8eb2005-12-20 12:12:18 -08001826 */
1827 struct stat st;
1828
1829 if (lstat(ce->name, &st) < 0)
1830 return;
1831 if (ce_match_stat_basic(ce, &st))
1832 return;
1833 if (ce_modified_check_fs(ce, &st)) {
Junio C Hamano4b3511b2005-12-20 14:18:47 -08001834 /* This is "racily clean"; smudge it. Note that this
1835 * is a tricky code. At first glance, it may appear
1836 * that it can break with this sequence:
1837 *
1838 * $ echo xyzzy >frotz
1839 * $ git-update-index --add frotz
1840 * $ : >frotz
1841 * $ sleep 3
1842 * $ echo filfre >nitfol
1843 * $ git-update-index --add nitfol
1844 *
Junio C Hamanob7e58b12006-08-05 04:16:02 -07001845 * but it does not. When the second update-index runs,
Junio C Hamano4b3511b2005-12-20 14:18:47 -08001846 * it notices that the entry "frotz" has the same timestamp
1847 * as index, and if we were to smudge it by resetting its
1848 * size to zero here, then the object name recorded
1849 * in index is the 6-byte file but the cached stat information
1850 * becomes zero --- which would then match what we would
Junio C Hamanoa6080a02007-06-07 00:04:01 -07001851 * obtain from the filesystem next time we stat("frotz").
Junio C Hamano4b3511b2005-12-20 14:18:47 -08001852 *
1853 * However, the second update-index, before calling
1854 * this function, notices that the cached size is 6
1855 * bytes and what is on the filesystem is an empty
1856 * file, and never calls us, so the cached size information
1857 * for "frotz" stays 6 which does not match the filesystem.
1858 */
Michael Haggertyc21d39d2013-06-20 10:37:50 +02001859 ce->ce_stat_data.sd_size = 0;
Junio C Hamano407c8eb2005-12-20 12:12:18 -08001860 }
1861}
1862
Junio C Hamanof136f7b2012-04-03 15:53:14 -07001863/* Copy miscellaneous fields but not the name */
1864static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,
1865 struct cache_entry *ce)
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001866{
Thomas Gummererb60e1882012-07-11 11:22:37 +02001867 short flags;
1868
Michael Haggertyc21d39d2013-06-20 10:37:50 +02001869 ondisk->ctime.sec = htonl(ce->ce_stat_data.sd_ctime.sec);
1870 ondisk->mtime.sec = htonl(ce->ce_stat_data.sd_mtime.sec);
1871 ondisk->ctime.nsec = htonl(ce->ce_stat_data.sd_ctime.nsec);
1872 ondisk->mtime.nsec = htonl(ce->ce_stat_data.sd_mtime.nsec);
1873 ondisk->dev = htonl(ce->ce_stat_data.sd_dev);
1874 ondisk->ino = htonl(ce->ce_stat_data.sd_ino);
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001875 ondisk->mode = htonl(ce->ce_mode);
Michael Haggertyc21d39d2013-06-20 10:37:50 +02001876 ondisk->uid = htonl(ce->ce_stat_data.sd_uid);
1877 ondisk->gid = htonl(ce->ce_stat_data.sd_gid);
1878 ondisk->size = htonl(ce->ce_stat_data.sd_size);
brian m. carlson99d1a982016-09-05 20:07:52 +00001879 hashcpy(ondisk->sha1, ce->oid.hash);
Thomas Gummererb60e1882012-07-11 11:22:37 +02001880
Nguyễn Thái Ngọc Duyce51bf02014-06-13 19:19:25 +07001881 flags = ce->ce_flags & ~CE_NAMEMASK;
Thomas Gummererb60e1882012-07-11 11:22:37 +02001882 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK : ce_namelen(ce));
1883 ondisk->flags = htons(flags);
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07001884 if (ce->ce_flags & CE_EXTENDED) {
1885 struct ondisk_cache_entry_extended *ondisk2;
1886 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;
1887 ondisk2->flags2 = htons((ce->ce_flags & CE_EXTENDED_FLAGS) >> 16);
Junio C Hamanof136f7b2012-04-03 15:53:14 -07001888 return ondisk2->name;
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07001889 }
Junio C Hamanof136f7b2012-04-03 15:53:14 -07001890 else {
1891 return ondisk->name;
1892 }
1893}
1894
Junio C Hamano9d227782012-04-04 09:12:43 -07001895static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,
1896 struct strbuf *previous_name)
Junio C Hamanof136f7b2012-04-03 15:53:14 -07001897{
Junio C Hamano9d227782012-04-04 09:12:43 -07001898 int size;
1899 struct ondisk_cache_entry *ondisk;
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07001900 int saved_namelen = saved_namelen; /* compiler workaround */
Junio C Hamanof136f7b2012-04-03 15:53:14 -07001901 char *name;
1902 int result;
1903
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07001904 if (ce->ce_flags & CE_STRIP_NAME) {
1905 saved_namelen = ce_namelen(ce);
1906 ce->ce_namelen = 0;
1907 }
1908
Junio C Hamano9d227782012-04-04 09:12:43 -07001909 if (!previous_name) {
1910 size = ondisk_ce_size(ce);
1911 ondisk = xcalloc(1, size);
1912 name = copy_cache_entry_to_ondisk(ondisk, ce);
1913 memcpy(name, ce->name, ce_namelen(ce));
1914 } else {
1915 int common, to_remove, prefix_size;
1916 unsigned char to_remove_vi[16];
1917 for (common = 0;
1918 (ce->name[common] &&
1919 common < previous_name->len &&
1920 ce->name[common] == previous_name->buf[common]);
1921 common++)
1922 ; /* still matching */
1923 to_remove = previous_name->len - common;
1924 prefix_size = encode_varint(to_remove, to_remove_vi);
1925
1926 if (ce->ce_flags & CE_EXTENDED)
1927 size = offsetof(struct ondisk_cache_entry_extended, name);
1928 else
1929 size = offsetof(struct ondisk_cache_entry, name);
1930 size += prefix_size + (ce_namelen(ce) - common + 1);
1931
1932 ondisk = xcalloc(1, size);
1933 name = copy_cache_entry_to_ondisk(ondisk, ce);
1934 memcpy(name, to_remove_vi, prefix_size);
1935 memcpy(name + prefix_size, ce->name + common, ce_namelen(ce) - common);
1936
1937 strbuf_splice(previous_name, common, to_remove,
1938 ce->name + common, ce_namelen(ce) - common);
1939 }
Nguyễn Thái Ngọc Duyb3c96fb2014-06-13 19:19:43 +07001940 if (ce->ce_flags & CE_STRIP_NAME) {
1941 ce->ce_namelen = saved_namelen;
1942 ce->ce_flags &= ~CE_STRIP_NAME;
1943 }
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001944
Jonathan Nieder59efba62010-08-09 22:28:07 -05001945 result = ce_write(c, fd, ondisk, size);
1946 free(ondisk);
1947 return result;
Linus Torvalds7a51ed62008-01-14 16:03:17 -08001948}
1949
Yiannis Marangos426ddee2014-04-10 21:31:21 +03001950/*
1951 * This function verifies if index_state has the correct sha1 of the
1952 * index file. Don't die if we have any other failure, just return 0.
1953 */
1954static int verify_index_from(const struct index_state *istate, const char *path)
1955{
1956 int fd;
1957 ssize_t n;
1958 struct stat st;
1959 unsigned char sha1[20];
1960
1961 if (!istate->initialized)
1962 return 0;
1963
1964 fd = open(path, O_RDONLY);
1965 if (fd < 0)
1966 return 0;
1967
1968 if (fstat(fd, &st))
1969 goto out;
1970
1971 if (st.st_size < sizeof(struct cache_header) + 20)
1972 goto out;
1973
1974 n = pread_in_full(fd, sha1, 20, st.st_size - 20);
1975 if (n != 20)
1976 goto out;
1977
1978 if (hashcmp(istate->sha1, sha1))
1979 goto out;
1980
1981 close(fd);
1982 return 1;
1983
1984out:
1985 close(fd);
1986 return 0;
1987}
1988
1989static int verify_index(const struct index_state *istate)
1990{
1991 return verify_index_from(istate, get_index_file());
1992}
1993
Junio C Hamano483fbe22011-03-21 10:18:19 -07001994static int has_racy_timestamp(struct index_state *istate)
1995{
1996 int entries = istate->cache_nr;
1997 int i;
1998
1999 for (i = 0; i < entries; i++) {
2000 struct cache_entry *ce = istate->cache[i];
2001 if (is_racy_timestamp(istate, ce))
2002 return 1;
2003 }
2004 return 0;
2005}
2006
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002007/*
Ondřej Bílka98e023d2013-07-29 10:18:21 +02002008 * Opportunistically update the index but do not complain if we can't
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002009 */
2010void update_index_if_able(struct index_state *istate, struct lock_file *lockfile)
2011{
Junio C Hamano483fbe22011-03-21 10:18:19 -07002012 if ((istate->cache_changed || has_racy_timestamp(istate)) &&
Junio C Hamano788cef82014-07-16 11:25:40 -07002013 verify_index(istate) &&
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002014 write_locked_index(istate, lockfile, COMMIT_LOCK))
Junio C Hamanoccdc4ec2011-03-21 10:16:10 -07002015 rollback_lock_file(lockfile);
2016}
2017
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002018static int do_write_index(struct index_state *istate, int newfd,
2019 int strip_extensions)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002020{
Nicolas Pitre9126f002008-10-01 14:05:20 -04002021 git_SHA_CTX c;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002022 struct cache_header hdr;
Junio C Hamano9d227782012-04-04 09:12:43 -07002023 int i, err, removed, extended, hdr_version;
Junio C Hamano4aab5b42007-04-01 23:26:07 -07002024 struct cache_entry **cache = istate->cache;
2025 int entries = istate->cache_nr;
Kjetil Barvike1afca42009-02-23 19:02:57 +01002026 struct stat st;
Junio C Hamano9d227782012-04-04 09:12:43 -07002027 struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
Junio C Hamano025a0702005-06-10 01:32:37 -07002028
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002029 for (i = removed = extended = 0; i < entries; i++) {
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002030 if (cache[i]->ce_flags & CE_REMOVE)
Junio C Hamano025a0702005-06-10 01:32:37 -07002031 removed++;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002032
Nguyễn Thái Ngọc Duy06aaaa02008-10-01 11:04:01 +07002033 /* reduce extended entries if possible */
2034 cache[i]->ce_flags &= ~CE_EXTENDED;
2035 if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) {
2036 extended++;
2037 cache[i]->ce_flags |= CE_EXTENDED;
2038 }
2039 }
2040
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002041 if (!istate->version) {
Thomas Gummerer136347d2014-02-23 21:49:57 +01002042 istate->version = get_index_format_default();
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002043 if (getenv("GIT_TEST_SPLIT_INDEX"))
2044 init_split_index(istate);
2045 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002046
2047 /* demote version 3 to version 2 when the latter suffices */
2048 if (istate->version == 3 || istate->version == 2)
2049 istate->version = extended ? 3 : 2;
2050
2051 hdr_version = istate->version;
2052
Linus Torvaldsccc4feb2005-04-15 10:44:27 -07002053 hdr.hdr_signature = htonl(CACHE_SIGNATURE);
Junio C Hamano9d227782012-04-04 09:12:43 -07002054 hdr.hdr_version = htonl(hdr_version);
Junio C Hamano025a0702005-06-10 01:32:37 -07002055 hdr.hdr_entries = htonl(entries - removed);
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002056
Nicolas Pitre9126f002008-10-01 14:05:20 -04002057 git_SHA1_Init(&c);
Linus Torvaldsca9be052005-04-20 12:36:41 -07002058 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002059 return -1;
2060
Junio C Hamano9d227782012-04-04 09:12:43 -07002061 previous_name = (hdr_version == 4) ? &previous_name_buf : NULL;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002062 for (i = 0; i < entries; i++) {
2063 struct cache_entry *ce = cache[i];
Linus Torvalds7a51ed62008-01-14 16:03:17 -08002064 if (ce->ce_flags & CE_REMOVE)
Linus Torvaldsaa160212005-06-09 15:34:04 -07002065 continue;
Junio C Hamanoe06c43c2008-03-30 09:25:52 -07002066 if (!ce_uptodate(ce) && is_racy_timestamp(istate, ce))
Junio C Hamano407c8eb2005-12-20 12:12:18 -08002067 ce_smudge_racily_clean_entry(ce);
brian m. carlson99d1a982016-09-05 20:07:52 +00002068 if (is_null_oid(&ce->oid)) {
Jeff King83bd7432013-08-27 16:41:12 -04002069 static const char msg[] = "cache entry has null sha1: %s";
2070 static int allow = -1;
2071
2072 if (allow < 0)
2073 allow = git_env_bool("GIT_ALLOW_NULL_SHA1", 0);
2074 if (allow)
2075 warning(msg, ce->name);
2076 else
2077 return error(msg, ce->name);
2078 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002079 if (ce_write_entry(&c, newfd, ce, previous_name) < 0)
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002080 return -1;
2081 }
Junio C Hamano9d227782012-04-04 09:12:43 -07002082 strbuf_release(&previous_name_buf);
Junio C Hamano1af1c2b2006-04-23 16:52:08 -07002083
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002084 /* Write extension data here */
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002085 if (!strip_extensions && istate->split_index) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002086 struct strbuf sb = STRBUF_INIT;
2087
2088 err = write_link_extension(&sb, istate) < 0 ||
2089 write_index_ext_header(&c, newfd, CACHE_EXT_LINK,
2090 sb.len) < 0 ||
2091 ce_write(&c, newfd, sb.buf, sb.len) < 0;
2092 strbuf_release(&sb);
2093 if (err)
2094 return -1;
2095 }
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002096 if (!strip_extensions && istate->cache_tree) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -05002097 struct strbuf sb = STRBUF_INIT;
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02002098
Pierre Habouzit1dffb8f2007-09-25 10:22:44 +02002099 cache_tree_write(&sb, istate->cache_tree);
2100 err = write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) < 0
2101 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
2102 strbuf_release(&sb);
2103 if (err)
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002104 return -1;
Junio C Hamanobad68ec2006-04-24 21:18:58 -07002105 }
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002106 if (!strip_extensions && istate->resolve_undo) {
Junio C Hamanocfc57892009-12-25 00:30:51 -08002107 struct strbuf sb = STRBUF_INIT;
2108
2109 resolve_undo_write(&sb, istate->resolve_undo);
2110 err = write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,
2111 sb.len) < 0
2112 || ce_write(&c, newfd, sb.buf, sb.len) < 0;
2113 strbuf_release(&sb);
2114 if (err)
2115 return -1;
2116 }
Nguyễn Thái Ngọc Duy83c094a2015-03-08 17:12:33 +07002117 if (!strip_extensions && istate->untracked) {
2118 struct strbuf sb = STRBUF_INIT;
2119
2120 write_untracked_extension(&sb, istate->untracked);
2121 err = write_index_ext_header(&c, newfd, CACHE_EXT_UNTRACKED,
2122 sb.len) < 0 ||
2123 ce_write(&c, newfd, sb.buf, sb.len) < 0;
2124 strbuf_release(&sb);
2125 if (err)
2126 return -1;
2127 }
Kjetil Barvike1afca42009-02-23 19:02:57 +01002128
Nguyễn Thái Ngọc Duye93021b2014-06-13 19:19:35 +07002129 if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
Kjetil Barvike1afca42009-02-23 19:02:57 +01002130 return -1;
Kjetil Barvik5bcf1092009-03-15 12:38:55 +01002131 istate->timestamp.sec = (unsigned int)st.st_mtime;
2132 istate->timestamp.nsec = ST_MTIME_NSEC(st);
Kjetil Barvike1afca42009-02-23 19:02:57 +01002133 return 0;
Linus Torvalds197ee8c2005-04-09 12:09:27 -07002134}
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002135
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07002136void set_alternate_index_output(const char *name)
2137{
2138 alternate_index_output = name;
2139}
2140
2141static int commit_locked_index(struct lock_file *lk)
2142{
Michael Haggerty751bace2014-10-01 12:28:36 +02002143 if (alternate_index_output)
2144 return commit_lock_file_to(lk, alternate_index_output);
2145 else
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07002146 return commit_lock_file(lk);
Nguyễn Thái Ngọc Duy626f35c2014-06-13 19:19:24 +07002147}
2148
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002149static int do_write_locked_index(struct index_state *istate, struct lock_file *lock,
2150 unsigned flags)
2151{
Michael Haggertyc99a4c22015-08-10 11:47:38 +02002152 int ret = do_write_index(istate, get_lock_file_fd(lock), 0);
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002153 if (ret)
2154 return ret;
2155 assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
2156 (COMMIT_LOCK | CLOSE_LOCK));
2157 if (flags & COMMIT_LOCK)
2158 return commit_locked_index(lock);
2159 else if (flags & CLOSE_LOCK)
2160 return close_lock_file(lock);
2161 else
2162 return ret;
2163}
2164
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002165static int write_split_index(struct index_state *istate,
2166 struct lock_file *lock,
2167 unsigned flags)
2168{
2169 int ret;
2170 prepare_to_write_split_index(istate);
2171 ret = do_write_locked_index(istate, lock, flags);
2172 finish_writing_split_index(istate);
2173 return ret;
2174}
2175
Michael Haggertyf6ecc622015-08-10 11:47:45 +02002176static struct tempfile temporary_sharedindex;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002177
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07002178static int write_shared_index(struct index_state *istate,
2179 struct lock_file *lock, unsigned flags)
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002180{
2181 struct split_index *si = istate->split_index;
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002182 int fd, ret;
2183
Michael Haggertyf6ecc622015-08-10 11:47:45 +02002184 fd = mks_tempfile(&temporary_sharedindex, git_path("sharedindex_XXXXXX"));
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07002185 if (fd < 0) {
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07002186 hashclr(si->base_sha1);
2187 return do_write_locked_index(istate, lock, flags);
2188 }
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002189 move_cache_to_base_index(istate);
2190 ret = do_write_index(si->base, fd, 1);
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002191 if (ret) {
Michael Haggertyf6ecc622015-08-10 11:47:45 +02002192 delete_tempfile(&temporary_sharedindex);
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002193 return ret;
2194 }
Michael Haggertyf6ecc622015-08-10 11:47:45 +02002195 ret = rename_tempfile(&temporary_sharedindex,
2196 git_path("sharedindex.%s", sha1_to_hex(si->base->sha1)));
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002197 if (!ret)
2198 hashcpy(si->base_sha1, si->base->sha1);
2199 return ret;
2200}
2201
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002202int write_locked_index(struct index_state *istate, struct lock_file *lock,
2203 unsigned flags)
2204{
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002205 struct split_index *si = istate->split_index;
2206
Nguyễn Thái Ngọc Duy5165dd52014-06-13 19:19:47 +07002207 if (!si || alternate_index_output ||
2208 (istate->cache_changed & ~EXTMASK)) {
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002209 if (si)
2210 hashclr(si->base_sha1);
2211 return do_write_locked_index(istate, lock, flags);
2212 }
2213
Nguyễn Thái Ngọc Duyd6e3c182014-06-13 19:19:49 +07002214 if (getenv("GIT_TEST_SPLIT_INDEX")) {
2215 int v = si->base_sha1[0];
2216 if ((v & 15) < 6)
2217 istate->cache_changed |= SPLIT_INDEX_ORDERED;
2218 }
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002219 if (istate->cache_changed & SPLIT_INDEX_ORDERED) {
Nguyễn Thái Ngọc Duya0a96752014-06-13 19:19:45 +07002220 int ret = write_shared_index(istate, lock, flags);
Nguyễn Thái Ngọc Duyc18b80a2014-06-13 19:19:44 +07002221 if (ret)
2222 return ret;
2223 }
2224
Nguyễn Thái Ngọc Duy5fc2fc82014-06-13 19:19:36 +07002225 return write_split_index(istate, lock, flags);
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002226}
2227
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002228/*
2229 * Read the index file that is potentially unmerged into given
Junio C Hamano63e8dc52008-12-05 17:54:11 -08002230 * index_state, dropping any unmerged entries. Returns true if
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002231 * the index is unmerged. Callers who want to refuse to work
2232 * from an unmerged state can call this and check its return value,
2233 * instead of calling read_cache().
2234 */
2235int read_index_unmerged(struct index_state *istate)
2236{
2237 int i;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002238 int unmerged = 0;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002239
2240 read_index(istate);
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002241 for (i = 0; i < istate->cache_nr; i++) {
2242 struct cache_entry *ce = istate->cache[i];
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002243 struct cache_entry *new_ce;
2244 int size, len;
2245
2246 if (!ce_stage(ce))
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002247 continue;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002248 unmerged = 1;
Thomas Gummerer68c4f6a2012-07-06 18:07:30 +02002249 len = ce_namelen(ce);
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002250 size = cache_entry_size(len);
2251 new_ce = xcalloc(1, size);
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002252 memcpy(new_ce->name, ce->name, len);
Thomas Gummererb60e1882012-07-11 11:22:37 +02002253 new_ce->ce_flags = create_ce_flags(0) | CE_CONFLICTED;
2254 new_ce->ce_namelen = len;
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002255 new_ce->ce_mode = ce->ce_mode;
2256 if (add_index_entry(istate, new_ce, 0))
2257 return error("%s: cannot drop to stage #0",
Karsten Blees5699d172013-11-14 20:24:37 +01002258 new_ce->name);
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002259 }
Junio C Hamanod1a43f22008-10-15 16:00:06 -07002260 return unmerged;
Miklos Vajnae46bbcf2008-06-27 18:21:58 +02002261}
Junio C Hamano041aee32008-07-21 01:24:17 -07002262
Jeff King98fa4732008-10-16 11:07:26 -04002263/*
2264 * Returns 1 if the path is an "other" path with respect to
2265 * the index; that is, the path is not mentioned in the index at all,
2266 * either as a file, a directory with some files in the index,
2267 * or as an unmerged entry.
2268 *
2269 * We helpfully remove a trailing "/" from directories so that
2270 * the output of read_directory can be used as-is.
2271 */
2272int index_name_is_other(const struct index_state *istate, const char *name,
2273 int namelen)
2274{
2275 int pos;
2276 if (namelen && name[namelen - 1] == '/')
2277 namelen--;
2278 pos = index_name_pos(istate, name, namelen);
2279 if (0 <= pos)
2280 return 0; /* exact match */
2281 pos = -pos - 1;
2282 if (pos < istate->cache_nr) {
2283 struct cache_entry *ce = istate->cache[pos];
2284 if (ce_namelen(ce) == namelen &&
2285 !memcmp(ce->name, name, namelen))
2286 return 0; /* Yup, this one exists unmerged */
2287 }
2288 return 1;
2289}
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02002290
Lukas Fleischerff366822013-04-13 15:28:31 +02002291void *read_blob_data_from_index(struct index_state *istate, const char *path, unsigned long *size)
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02002292{
2293 int pos, len;
2294 unsigned long sz;
2295 enum object_type type;
2296 void *data;
2297
2298 len = strlen(path);
2299 pos = index_name_pos(istate, path, len);
2300 if (pos < 0) {
2301 /*
2302 * We might be in the middle of a merge, in which
2303 * case we would read stage #2 (ours).
2304 */
2305 int i;
2306 for (i = -pos - 1;
2307 (pos < 0 && i < istate->cache_nr &&
2308 !strcmp(istate->cache[i]->name, path));
2309 i++)
2310 if (ce_stage(istate->cache[i]) == 2)
2311 pos = i;
2312 }
2313 if (pos < 0)
2314 return NULL;
brian m. carlson99d1a982016-09-05 20:07:52 +00002315 data = read_sha1_file(istate->cache[pos]->oid.hash, &type, &sz);
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02002316 if (!data || type != OBJ_BLOB) {
2317 free(data);
2318 return NULL;
2319 }
Lukas Fleischerff366822013-04-13 15:28:31 +02002320 if (size)
2321 *size = sz;
Lukas Fleischer29fb37b2013-04-13 15:28:30 +02002322 return data;
2323}
Michael Haggerty38612532013-06-20 10:37:51 +02002324
2325void stat_validity_clear(struct stat_validity *sv)
2326{
2327 free(sv->sd);
2328 sv->sd = NULL;
2329}
2330
2331int stat_validity_check(struct stat_validity *sv, const char *path)
2332{
2333 struct stat st;
2334
2335 if (stat(path, &st) < 0)
2336 return sv->sd == NULL;
2337 if (!sv->sd)
2338 return 0;
2339 return S_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);
2340}
2341
2342void stat_validity_update(struct stat_validity *sv, int fd)
2343{
2344 struct stat st;
2345
2346 if (fstat(fd, &st) < 0 || !S_ISREG(st.st_mode))
2347 stat_validity_clear(sv);
2348 else {
2349 if (!sv->sd)
2350 sv->sd = xcalloc(1, sizeof(struct stat_data));
2351 fill_stat_data(sv->sd, &st);
2352 }
2353}