| Linus Torvalds | 8bc9a0c | 2005-04-07 22:16:10 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 8 | struct cache_entry **active_cache = NULL; |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 9 | static time_t index_file_timestamp; |
| Linus Torvalds | ee26752 | 2005-05-06 23:48:43 | [diff] [blame] | 10 | unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 11 | |
| Junio C Hamano | 415e96c | 2005-05-15 21:23:12 | [diff] [blame] | 12 | /* |
| 13 | * This only updates the "non-critical" parts of the directory |
| 14 | * cache, ie the parts that aren't tracked by GIT, and only used |
| 15 | * to validate the cache. |
| 16 | */ |
| 17 | void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) |
| 18 | { |
| 19 | ce->ce_ctime.sec = htonl(st->st_ctime); |
| 20 | ce->ce_mtime.sec = htonl(st->st_mtime); |
| Linus Torvalds | 2cb45e9 | 2005-05-22 22:08:15 | [diff] [blame] | 21 | #ifdef USE_NSEC |
| Junio C Hamano | 415e96c | 2005-05-15 21:23:12 | [diff] [blame] | 22 | ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec); |
| 23 | ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec); |
| 24 | #endif |
| 25 | ce->ce_dev = htonl(st->st_dev); |
| 26 | ce->ce_ino = htonl(st->st_ino); |
| 27 | ce->ce_uid = htonl(st->st_uid); |
| 28 | ce->ce_gid = htonl(st->st_gid); |
| 29 | ce->ce_size = htonl(st->st_size); |
| 30 | } |
| 31 | |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 32 | static int ce_compare_data(struct cache_entry *ce, struct stat *st) |
| 33 | { |
| 34 | int match = -1; |
| 35 | int fd = open(ce->name, O_RDONLY); |
| 36 | |
| 37 | if (fd >= 0) { |
| 38 | unsigned char sha1[20]; |
| 39 | if (!index_fd(sha1, fd, st, 0, NULL)) |
| 40 | match = memcmp(sha1, ce->sha1, 20); |
| 41 | close(fd); |
| 42 | } |
| 43 | return match; |
| 44 | } |
| 45 | |
| 46 | static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size) |
| 47 | { |
| 48 | int match = -1; |
| 49 | char *target; |
| 50 | void *buffer; |
| 51 | unsigned long size; |
| 52 | char type[10]; |
| 53 | int len; |
| 54 | |
| 55 | target = xmalloc(expected_size); |
| 56 | len = readlink(ce->name, target, expected_size); |
| 57 | if (len != expected_size) { |
| 58 | free(target); |
| 59 | return -1; |
| 60 | } |
| 61 | buffer = read_sha1_file(ce->sha1, type, &size); |
| 62 | if (!buffer) { |
| 63 | free(target); |
| 64 | return -1; |
| 65 | } |
| 66 | if (size == expected_size) |
| 67 | match = memcmp(buffer, target, size); |
| 68 | free(buffer); |
| 69 | free(target); |
| 70 | return match; |
| 71 | } |
| 72 | |
| 73 | static int ce_modified_check_fs(struct cache_entry *ce, struct stat *st) |
| 74 | { |
| 75 | switch (st->st_mode & S_IFMT) { |
| 76 | case S_IFREG: |
| 77 | if (ce_compare_data(ce, st)) |
| 78 | return DATA_CHANGED; |
| 79 | break; |
| 80 | case S_IFLNK: |
| 81 | if (ce_compare_link(ce, st->st_size)) |
| 82 | return DATA_CHANGED; |
| 83 | break; |
| 84 | default: |
| 85 | return TYPE_CHANGED; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| Junio C Hamano | 407c8eb | 2005-12-20 20:12:18 | [diff] [blame] | 90 | static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st) |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 91 | { |
| 92 | unsigned int changed = 0; |
| 93 | |
| Kay Sievers | 8ae0a8c | 2005-05-05 12:38:25 | [diff] [blame] | 94 | switch (ntohl(ce->ce_mode) & S_IFMT) { |
| 95 | case S_IFREG: |
| 96 | changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0; |
| Junio C Hamano | 3e09cdf | 2005-10-12 01:45:33 | [diff] [blame] | 97 | /* We consider only the owner x bit to be relevant for |
| 98 | * "mode changes" |
| 99 | */ |
| 100 | if (trust_executable_bit && |
| 101 | (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))) |
| Kay Sievers | ffbe1ad | 2005-05-06 13:45:01 | [diff] [blame] | 102 | changed |= MODE_CHANGED; |
| Kay Sievers | 8ae0a8c | 2005-05-05 12:38:25 | [diff] [blame] | 103 | break; |
| 104 | case S_IFLNK: |
| 105 | changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0; |
| 106 | break; |
| 107 | default: |
| 108 | die("internal error: ce_mode is %o", ntohl(ce->ce_mode)); |
| 109 | } |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 110 | if (ce->ce_mtime.sec != htonl(st->st_mtime)) |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 111 | changed |= MTIME_CHANGED; |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 112 | if (ce->ce_ctime.sec != htonl(st->st_ctime)) |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 113 | changed |= CTIME_CHANGED; |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 114 | |
| Linus Torvalds | 2cb45e9 | 2005-05-22 22:08:15 | [diff] [blame] | 115 | #ifdef USE_NSEC |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 116 | /* |
| 117 | * nsec seems unreliable - not all filesystems support it, so |
| 118 | * as long as it is in the inode cache you get right nsec |
| 119 | * but after it gets flushed, you get zero nsec. |
| 120 | */ |
| Linus Torvalds | 94dfb7f | 2005-04-21 16:58:24 | [diff] [blame] | 121 | if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec)) |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 122 | changed |= MTIME_CHANGED; |
| Linus Torvalds | 94dfb7f | 2005-04-21 16:58:24 | [diff] [blame] | 123 | if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec)) |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 124 | changed |= CTIME_CHANGED; |
| 125 | #endif |
| 126 | |
| 127 | if (ce->ce_uid != htonl(st->st_uid) || |
| 128 | ce->ce_gid != htonl(st->st_gid)) |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 129 | changed |= OWNER_CHANGED; |
| Linus Torvalds | 2cb45e9 | 2005-05-22 22:08:15 | [diff] [blame] | 130 | if (ce->ce_ino != htonl(st->st_ino)) |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 131 | changed |= INODE_CHANGED; |
| Linus Torvalds | 2cb45e9 | 2005-05-22 22:08:15 | [diff] [blame] | 132 | |
| 133 | #ifdef USE_STDEV |
| 134 | /* |
| 135 | * st_dev breaks on network filesystems where different |
| 136 | * clients will have different views of what "device" |
| 137 | * the filesystem is on |
| 138 | */ |
| 139 | if (ce->ce_dev != htonl(st->st_dev)) |
| 140 | changed |= INODE_CHANGED; |
| 141 | #endif |
| 142 | |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 143 | if (ce->ce_size != htonl(st->st_size)) |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 144 | changed |= DATA_CHANGED; |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 145 | |
| Junio C Hamano | 407c8eb | 2005-12-20 20:12:18 | [diff] [blame] | 146 | return changed; |
| 147 | } |
| 148 | |
| 149 | int ce_match_stat(struct cache_entry *ce, struct stat *st) |
| 150 | { |
| 151 | unsigned int changed = ce_match_stat_basic(ce, st); |
| 152 | |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 153 | /* |
| 154 | * Within 1 second of this sequence: |
| 155 | * echo xyzzy >file && git-update-index --add file |
| 156 | * running this command: |
| 157 | * echo frotz >file |
| 158 | * would give a falsely clean cache entry. The mtime and |
| 159 | * length match the cache, and other stat fields do not change. |
| 160 | * |
| 161 | * We could detect this at update-index time (the cache entry |
| 162 | * being registered/updated records the same time as "now") |
| 163 | * and delay the return from git-update-index, but that would |
| 164 | * effectively mean we can make at most one commit per second, |
| 165 | * which is not acceptable. Instead, we check cache entries |
| 166 | * whose mtime are the same as the index file timestamp more |
| 167 | * careful than others. |
| 168 | */ |
| 169 | if (!changed && |
| 170 | index_file_timestamp && |
| 171 | index_file_timestamp <= ntohl(ce->ce_mtime.sec)) |
| 172 | changed |= ce_modified_check_fs(ce, st); |
| 173 | |
| Linus Torvalds | 734aab7 | 2005-04-09 16:48:20 | [diff] [blame] | 174 | return changed; |
| 175 | } |
| 176 | |
| Junio C Hamano | b039189 | 2005-09-19 22:11:15 | [diff] [blame] | 177 | int ce_modified(struct cache_entry *ce, struct stat *st) |
| 178 | { |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 179 | int changed, changed_fs; |
| Junio C Hamano | b039189 | 2005-09-19 22:11:15 | [diff] [blame] | 180 | changed = ce_match_stat(ce, st); |
| 181 | if (!changed) |
| 182 | return 0; |
| Junio C Hamano | b039189 | 2005-09-19 22:11:15 | [diff] [blame] | 183 | /* |
| 184 | * If the mode or type has changed, there's no point in trying |
| 185 | * to refresh the entry - it's not going to match |
| 186 | */ |
| 187 | if (changed & (MODE_CHANGED | TYPE_CHANGED)) |
| 188 | return changed; |
| 189 | |
| 190 | /* Immediately after read-tree or update-index --cacheinfo, |
| 191 | * the length field is zero. For other cases the ce_size |
| 192 | * should match the SHA1 recorded in the index entry. |
| 193 | */ |
| 194 | if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0)) |
| 195 | return changed; |
| 196 | |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 197 | changed_fs = ce_modified_check_fs(ce, st); |
| 198 | if (changed_fs) |
| 199 | return changed | changed_fs; |
| Junio C Hamano | b039189 | 2005-09-19 22:11:15 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
| Linus Torvalds | 958ba6c | 2005-05-20 16:09:18 | [diff] [blame] | 203 | int base_name_compare(const char *name1, int len1, int mode1, |
| 204 | const char *name2, int len2, int mode2) |
| 205 | { |
| 206 | unsigned char c1, c2; |
| 207 | int len = len1 < len2 ? len1 : len2; |
| 208 | int cmp; |
| 209 | |
| 210 | cmp = memcmp(name1, name2, len); |
| 211 | if (cmp) |
| 212 | return cmp; |
| 213 | c1 = name1[len]; |
| 214 | c2 = name2[len]; |
| 215 | if (!c1 && S_ISDIR(mode1)) |
| 216 | c1 = '/'; |
| 217 | if (!c2 && S_ISDIR(mode2)) |
| 218 | c2 = '/'; |
| 219 | return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; |
| 220 | } |
| 221 | |
| Linus Torvalds | 95fd5bf | 2005-04-16 05:51:44 | [diff] [blame] | 222 | int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2) |
| Linus Torvalds | eb38c22 | 2005-04-09 16:26:55 | [diff] [blame] | 223 | { |
| Linus Torvalds | 95fd5bf | 2005-04-16 05:51:44 | [diff] [blame] | 224 | int len1 = flags1 & CE_NAMEMASK; |
| 225 | int len2 = flags2 & CE_NAMEMASK; |
| Linus Torvalds | eb38c22 | 2005-04-09 16:26:55 | [diff] [blame] | 226 | int len = len1 < len2 ? len1 : len2; |
| 227 | int cmp; |
| 228 | |
| 229 | cmp = memcmp(name1, name2, len); |
| 230 | if (cmp) |
| 231 | return cmp; |
| 232 | if (len1 < len2) |
| 233 | return -1; |
| 234 | if (len1 > len2) |
| 235 | return 1; |
| Linus Torvalds | 95fd5bf | 2005-04-16 05:51:44 | [diff] [blame] | 236 | if (flags1 < flags2) |
| 237 | return -1; |
| 238 | if (flags1 > flags2) |
| 239 | return 1; |
| Linus Torvalds | eb38c22 | 2005-04-09 16:26:55 | [diff] [blame] | 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int cache_name_pos(const char *name, int namelen) |
| 244 | { |
| 245 | int first, last; |
| 246 | |
| 247 | first = 0; |
| 248 | last = active_nr; |
| 249 | while (last > first) { |
| 250 | int next = (last + first) >> 1; |
| 251 | struct cache_entry *ce = active_cache[next]; |
| Timo Hirvonen | 972d1bb | 2005-06-07 20:35:56 | [diff] [blame] | 252 | int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags)); |
| Linus Torvalds | eb38c22 | 2005-04-09 16:26:55 | [diff] [blame] | 253 | if (!cmp) |
| Linus Torvalds | 76e7f4e | 2005-04-11 05:06:50 | [diff] [blame] | 254 | return next; |
| Linus Torvalds | eb38c22 | 2005-04-09 16:26:55 | [diff] [blame] | 255 | if (cmp < 0) { |
| 256 | last = next; |
| 257 | continue; |
| 258 | } |
| 259 | first = next+1; |
| 260 | } |
| Linus Torvalds | 76e7f4e | 2005-04-11 05:06:50 | [diff] [blame] | 261 | return -first-1; |
| Linus Torvalds | eb38c22 | 2005-04-09 16:26:55 | [diff] [blame] | 262 | } |
| 263 | |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 264 | /* Remove entry, return true if there are more entries to go.. */ |
| Brad Roberts | dbbce55 | 2005-05-15 02:04:25 | [diff] [blame] | 265 | int remove_cache_entry_at(int pos) |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 266 | { |
| Linus Torvalds | ee26752 | 2005-05-06 23:48:43 | [diff] [blame] | 267 | active_cache_changed = 1; |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 268 | active_nr--; |
| 269 | if (pos >= active_nr) |
| 270 | return 0; |
| 271 | memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *)); |
| 272 | return 1; |
| 273 | } |
| 274 | |
| Junio C Hamano | 6b5ee13 | 2005-09-21 07:00:47 | [diff] [blame] | 275 | int remove_file_from_cache(const char *path) |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 276 | { |
| 277 | int pos = cache_name_pos(path, strlen(path)); |
| Junio C Hamano | c4e3cca | 2005-04-17 16:53:35 | [diff] [blame] | 278 | if (pos < 0) |
| 279 | pos = -pos-1; |
| 280 | while (pos < active_nr && !strcmp(active_cache[pos]->name, path)) |
| Brad Roberts | dbbce55 | 2005-05-15 02:04:25 | [diff] [blame] | 281 | remove_cache_entry_at(pos); |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| Brad Roberts | dbbce55 | 2005-05-15 02:04:25 | [diff] [blame] | 285 | int ce_same_name(struct cache_entry *a, struct cache_entry *b) |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 286 | { |
| 287 | int len = ce_namelen(a); |
| 288 | return ce_namelen(b) == len && !memcmp(a->name, b->name, len); |
| 289 | } |
| 290 | |
| Linus Torvalds | c0fd1f5 | 2005-07-14 23:55:06 | [diff] [blame] | 291 | int ce_path_match(const struct cache_entry *ce, const char **pathspec) |
| 292 | { |
| 293 | const char *match, *name; |
| 294 | int len; |
| 295 | |
| 296 | if (!pathspec) |
| 297 | return 1; |
| 298 | |
| 299 | len = ce_namelen(ce); |
| 300 | name = ce->name; |
| 301 | while ((match = *pathspec++) != NULL) { |
| 302 | int matchlen = strlen(match); |
| 303 | if (matchlen > len) |
| 304 | continue; |
| 305 | if (memcmp(name, match, matchlen)) |
| 306 | continue; |
| 307 | if (matchlen && name[matchlen-1] == '/') |
| 308 | return 1; |
| 309 | if (name[matchlen] == '/' || !name[matchlen]) |
| 310 | return 1; |
| Linus Torvalds | f332726 | 2005-08-17 03:44:32 | [diff] [blame] | 311 | if (!matchlen) |
| 312 | return 1; |
| Linus Torvalds | c0fd1f5 | 2005-07-14 23:55:06 | [diff] [blame] | 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 317 | /* |
| 318 | * Do we have another file that has the beginning components being a |
| 319 | * proper superset of the name we're trying to add? |
| 320 | */ |
| 321 | static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace) |
| 322 | { |
| 323 | int retval = 0; |
| 324 | int len = ce_namelen(ce); |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 325 | int stage = ce_stage(ce); |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 326 | const char *name = ce->name; |
| 327 | |
| 328 | while (pos < active_nr) { |
| 329 | struct cache_entry *p = active_cache[pos++]; |
| 330 | |
| 331 | if (len >= ce_namelen(p)) |
| 332 | break; |
| 333 | if (memcmp(name, p->name, len)) |
| 334 | break; |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 335 | if (ce_stage(p) != stage) |
| 336 | continue; |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 337 | if (p->name[len] != '/') |
| 338 | continue; |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 339 | retval = -1; |
| 340 | if (!ok_to_replace) |
| 341 | break; |
| 342 | remove_cache_entry_at(--pos); |
| 343 | } |
| 344 | return retval; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Do we have another file with a pathname that is a proper |
| 349 | * subset of the name we're trying to add? |
| 350 | */ |
| 351 | static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace) |
| 352 | { |
| 353 | int retval = 0; |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 354 | int stage = ce_stage(ce); |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 355 | const char *name = ce->name; |
| 356 | const char *slash = name + ce_namelen(ce); |
| 357 | |
| 358 | for (;;) { |
| 359 | int len; |
| 360 | |
| 361 | for (;;) { |
| 362 | if (*--slash == '/') |
| 363 | break; |
| 364 | if (slash <= ce->name) |
| 365 | return retval; |
| 366 | } |
| 367 | len = slash - name; |
| 368 | |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 369 | pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage))); |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 370 | if (pos >= 0) { |
| 371 | retval = -1; |
| 372 | if (ok_to_replace) |
| 373 | break; |
| 374 | remove_cache_entry_at(pos); |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Trivial optimization: if we find an entry that |
| 380 | * already matches the sub-directory, then we know |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 381 | * we're ok, and we can exit. |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 382 | */ |
| 383 | pos = -pos-1; |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 384 | while (pos < active_nr) { |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 385 | struct cache_entry *p = active_cache[pos]; |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 386 | if ((ce_namelen(p) <= len) || |
| 387 | (p->name[len] != '/') || |
| 388 | memcmp(p->name, name, len)) |
| 389 | break; /* not our subdirectory */ |
| 390 | if (ce_stage(p) == stage) |
| 391 | /* p is at the same stage as our entry, and |
| 392 | * is a subdirectory of what we are looking |
| 393 | * at, so we cannot have conflicts at our |
| 394 | * level or anything shorter. |
| 395 | */ |
| 396 | return retval; |
| 397 | pos++; |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | return retval; |
| 401 | } |
| 402 | |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 403 | /* We may be in a situation where we already have path/file and path |
| 404 | * is being added, or we already have path and path/file is being |
| 405 | * added. Either one would result in a nonsense tree that has path |
| 406 | * twice when git-write-tree tries to write it out. Prevent it. |
| Junio C Hamano | 192268c | 2005-05-08 04:55:21 | [diff] [blame] | 407 | * |
| 408 | * If ok-to-replace is specified, we remove the conflicting entries |
| 409 | * from the cache so the caller should recompute the insert position. |
| 410 | * When this happens, we return non-zero. |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 411 | */ |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 412 | static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace) |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 413 | { |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 414 | /* |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 415 | * We check if the path is a sub-path of a subsequent pathname |
| 416 | * first, since removing those will not change the position |
| 417 | * in the array |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 418 | */ |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 419 | int retval = has_file_name(ce, pos, ok_to_replace); |
| 420 | /* |
| 421 | * Then check if the path might have a clashing sub-directory |
| 422 | * before it. |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 423 | */ |
| Linus Torvalds | 1267660 | 2005-06-19 03:21:34 | [diff] [blame] | 424 | return retval + has_dir_name(ce, pos, ok_to_replace); |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 425 | } |
| 426 | |
| Junio C Hamano | 192268c | 2005-05-08 04:55:21 | [diff] [blame] | 427 | int add_cache_entry(struct cache_entry *ce, int option) |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 428 | { |
| 429 | int pos; |
| Junio C Hamano | 192268c | 2005-05-08 04:55:21 | [diff] [blame] | 430 | int ok_to_add = option & ADD_CACHE_OK_TO_ADD; |
| 431 | int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; |
| Junio C Hamano | b155725 | 2005-06-25 09:25:29 | [diff] [blame] | 432 | int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; |
| Timo Hirvonen | 972d1bb | 2005-06-07 20:35:56 | [diff] [blame] | 433 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 434 | |
| Junio C Hamano | 3e09cdf | 2005-10-12 01:45:33 | [diff] [blame] | 435 | /* existing match? Just replace it. */ |
| Linus Torvalds | 76e7f4e | 2005-04-11 05:06:50 | [diff] [blame] | 436 | if (pos >= 0) { |
| Linus Torvalds | ee26752 | 2005-05-06 23:48:43 | [diff] [blame] | 437 | active_cache_changed = 1; |
| Linus Torvalds | 76e7f4e | 2005-04-11 05:06:50 | [diff] [blame] | 438 | active_cache[pos] = ce; |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 439 | return 0; |
| 440 | } |
| Linus Torvalds | 76e7f4e | 2005-04-11 05:06:50 | [diff] [blame] | 441 | pos = -pos-1; |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 442 | |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 443 | /* |
| 444 | * Inserting a merged entry ("stage 0") into the index |
| 445 | * will always replace all non-merged entries.. |
| 446 | */ |
| 447 | if (pos < active_nr && ce_stage(ce) == 0) { |
| Brad Roberts | dbbce55 | 2005-05-15 02:04:25 | [diff] [blame] | 448 | while (ce_same_name(active_cache[pos], ce)) { |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 449 | ok_to_add = 1; |
| Brad Roberts | dbbce55 | 2005-05-15 02:04:25 | [diff] [blame] | 450 | if (!remove_cache_entry_at(pos)) |
| Linus Torvalds | 7b937ca | 2005-04-16 19:05:45 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
| Linus Torvalds | 121481a | 2005-04-10 18:32:54 | [diff] [blame] | 455 | if (!ok_to_add) |
| 456 | return -1; |
| 457 | |
| Junio C Hamano | 3e09cdf | 2005-10-12 01:45:33 | [diff] [blame] | 458 | if (!skip_df_check && |
| 459 | check_file_directory_conflict(ce, pos, ok_to_replace)) { |
| Junio C Hamano | 192268c | 2005-05-08 04:55:21 | [diff] [blame] | 460 | if (!ok_to_replace) |
| 461 | return -1; |
| Timo Hirvonen | 972d1bb | 2005-06-07 20:35:56 | [diff] [blame] | 462 | pos = cache_name_pos(ce->name, ntohs(ce->ce_flags)); |
| Junio C Hamano | 192268c | 2005-05-08 04:55:21 | [diff] [blame] | 463 | pos = -pos-1; |
| 464 | } |
| Junio C Hamano | 0f1e4f0 | 2005-05-08 04:48:12 | [diff] [blame] | 465 | |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 466 | /* Make sure the array is big enough .. */ |
| 467 | if (active_nr == active_alloc) { |
| 468 | active_alloc = alloc_nr(active_alloc); |
| Christopher Li | 812666c | 2005-04-26 19:00:58 | [diff] [blame] | 469 | active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *)); |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /* Add it in.. */ |
| 473 | active_nr++; |
| 474 | if (active_nr > pos) |
| 475 | memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce)); |
| 476 | active_cache[pos] = ce; |
| Linus Torvalds | ee26752 | 2005-05-06 23:48:43 | [diff] [blame] | 477 | active_cache_changed = 1; |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 481 | static int verify_hdr(struct cache_header *hdr, unsigned long size) |
| 482 | { |
| 483 | SHA_CTX c; |
| 484 | unsigned char sha1[20]; |
| 485 | |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 486 | if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 487 | return error("bad signature"); |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 488 | if (hdr->hdr_version != htonl(2)) |
| 489 | return error("bad index version"); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 490 | SHA1_Init(&c); |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 491 | SHA1_Update(&c, hdr, size - 20); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 492 | SHA1_Final(sha1, &c); |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 493 | if (memcmp(sha1, (void *)hdr + size - 20, 20)) |
| 494 | return error("bad index file sha1 signature"); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | int read_cache(void) |
| 499 | { |
| 500 | int fd, i; |
| 501 | struct stat st; |
| 502 | unsigned long size, offset; |
| 503 | void *map; |
| 504 | struct cache_header *hdr; |
| 505 | |
| 506 | errno = EBUSY; |
| 507 | if (active_cache) |
| Linus Torvalds | 5d1a5c0 | 2005-10-01 20:24:27 | [diff] [blame] | 508 | return active_nr; |
| 509 | |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 510 | errno = ENOENT; |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 511 | index_file_timestamp = 0; |
| Linus Torvalds | bb233d6 | 2005-04-21 17:55:18 | [diff] [blame] | 512 | fd = open(get_index_file(), O_RDONLY); |
| Linus Torvalds | 5d1a5c0 | 2005-10-01 20:24:27 | [diff] [blame] | 513 | if (fd < 0) { |
| 514 | if (errno == ENOENT) |
| 515 | return 0; |
| 516 | die("index file open failed (%s)", strerror(errno)); |
| 517 | } |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 518 | |
| Linus Torvalds | 19b2860 | 2005-04-08 16:59:28 | [diff] [blame] | 519 | size = 0; // avoid gcc warning |
| Pavel Roskin | e35f982 | 2005-07-29 14:49:14 | [diff] [blame] | 520 | map = MAP_FAILED; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 521 | if (!fstat(fd, &st)) { |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 522 | size = st.st_size; |
| 523 | errno = EINVAL; |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 524 | if (size >= sizeof(struct cache_header) + 20) |
| Linus Torvalds | 520fc24 | 2005-04-27 02:27:27 | [diff] [blame] | 525 | map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 526 | } |
| 527 | close(fd); |
| Pavel Roskin | e35f982 | 2005-07-29 14:49:14 | [diff] [blame] | 528 | if (map == MAP_FAILED) |
| Linus Torvalds | 5d1a5c0 | 2005-10-01 20:24:27 | [diff] [blame] | 529 | die("index file mmap failed (%s)", strerror(errno)); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 530 | |
| 531 | hdr = map; |
| 532 | if (verify_hdr(hdr, size) < 0) |
| 533 | goto unmap; |
| 534 | |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 535 | active_nr = ntohl(hdr->hdr_entries); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 536 | active_alloc = alloc_nr(active_nr); |
| 537 | active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); |
| 538 | |
| 539 | offset = sizeof(*hdr); |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 540 | for (i = 0; i < active_nr; i++) { |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 541 | struct cache_entry *ce = map + offset; |
| 542 | offset = offset + ce_size(ce); |
| 543 | active_cache[i] = ce; |
| 544 | } |
| Junio C Hamano | 29e4d36 | 2005-12-20 08:02:15 | [diff] [blame] | 545 | index_file_timestamp = st.st_mtime; |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 546 | return active_nr; |
| 547 | |
| 548 | unmap: |
| 549 | munmap(map, size); |
| 550 | errno = EINVAL; |
| Linus Torvalds | 5d1a5c0 | 2005-10-01 20:24:27 | [diff] [blame] | 551 | die("index file corrupt"); |
| Linus Torvalds | e83c516 | 2005-04-07 22:13:13 | [diff] [blame] | 552 | } |
| 553 | |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 554 | #define WRITE_BUFFER_SIZE 8192 |
| Brian Gerst | bf0f910 | 2005-05-18 12:14:09 | [diff] [blame] | 555 | static unsigned char write_buffer[WRITE_BUFFER_SIZE]; |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 556 | static unsigned long write_buffer_len; |
| 557 | |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 558 | static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len) |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 559 | { |
| 560 | while (len) { |
| 561 | unsigned int buffered = write_buffer_len; |
| 562 | unsigned int partial = WRITE_BUFFER_SIZE - buffered; |
| 563 | if (partial > len) |
| 564 | partial = len; |
| 565 | memcpy(write_buffer + buffered, data, partial); |
| 566 | buffered += partial; |
| 567 | if (buffered == WRITE_BUFFER_SIZE) { |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 568 | SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE); |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 569 | if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE) |
| 570 | return -1; |
| 571 | buffered = 0; |
| 572 | } |
| 573 | write_buffer_len = buffered; |
| 574 | len -= partial; |
| 575 | data += partial; |
| 576 | } |
| 577 | return 0; |
| 578 | } |
| 579 | |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 580 | static int ce_flush(SHA_CTX *context, int fd) |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 581 | { |
| 582 | unsigned int left = write_buffer_len; |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 583 | |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 584 | if (left) { |
| 585 | write_buffer_len = 0; |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 586 | SHA1_Update(context, write_buffer, left); |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 587 | } |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 588 | |
| Qingning Huo | 2c865d9 | 2005-09-11 13:27:47 | [diff] [blame] | 589 | /* Flush first if not enough space for SHA1 signature */ |
| 590 | if (left + 20 > WRITE_BUFFER_SIZE) { |
| 591 | if (write(fd, write_buffer, left) != left) |
| 592 | return -1; |
| 593 | left = 0; |
| 594 | } |
| 595 | |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 596 | /* Append the SHA1 signature at the end */ |
| 597 | SHA1_Final(write_buffer + left, context); |
| 598 | left += 20; |
| 599 | if (write(fd, write_buffer, left) != left) |
| 600 | return -1; |
| Linus Torvalds | 4990aad | 2005-04-20 19:16:57 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
| Junio C Hamano | 407c8eb | 2005-12-20 20:12:18 | [diff] [blame] | 604 | static void ce_smudge_racily_clean_entry(struct cache_entry *ce) |
| 605 | { |
| 606 | /* |
| 607 | * The only thing we care about in this function is to smudge the |
| 608 | * falsely clean entry due to touch-update-touch race, so we leave |
| 609 | * everything else as they are. We are called for entries whose |
| 610 | * ce_mtime match the index file mtime. |
| 611 | */ |
| 612 | struct stat st; |
| 613 | |
| 614 | if (lstat(ce->name, &st) < 0) |
| 615 | return; |
| 616 | if (ce_match_stat_basic(ce, &st)) |
| 617 | return; |
| 618 | if (ce_modified_check_fs(ce, &st)) { |
| Junio C Hamano | 4b3511b | 2005-12-20 22:18:47 | [diff] [blame] | 619 | /* This is "racily clean"; smudge it. Note that this |
| 620 | * is a tricky code. At first glance, it may appear |
| 621 | * that it can break with this sequence: |
| 622 | * |
| 623 | * $ echo xyzzy >frotz |
| 624 | * $ git-update-index --add frotz |
| 625 | * $ : >frotz |
| 626 | * $ sleep 3 |
| 627 | * $ echo filfre >nitfol |
| 628 | * $ git-update-index --add nitfol |
| 629 | * |
| 630 | * but it does not. Whe the second update-index runs, |
| 631 | * it notices that the entry "frotz" has the same timestamp |
| 632 | * as index, and if we were to smudge it by resetting its |
| 633 | * size to zero here, then the object name recorded |
| 634 | * in index is the 6-byte file but the cached stat information |
| 635 | * becomes zero --- which would then match what we would |
| 636 | * obtain from the filesystem next time we stat("frotz"). |
| 637 | * |
| 638 | * However, the second update-index, before calling |
| 639 | * this function, notices that the cached size is 6 |
| 640 | * bytes and what is on the filesystem is an empty |
| 641 | * file, and never calls us, so the cached size information |
| 642 | * for "frotz" stays 6 which does not match the filesystem. |
| 643 | */ |
| Junio C Hamano | 407c8eb | 2005-12-20 20:12:18 | [diff] [blame] | 644 | ce->ce_size = htonl(0); |
| 645 | } |
| 646 | } |
| 647 | |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 648 | int write_cache(int newfd, struct cache_entry **cache, int entries) |
| 649 | { |
| 650 | SHA_CTX c; |
| 651 | struct cache_header hdr; |
| Junio C Hamano | 025a070 | 2005-06-10 08:32:37 | [diff] [blame] | 652 | int i, removed; |
| 653 | |
| 654 | for (i = removed = 0; i < entries; i++) |
| 655 | if (!cache[i]->ce_mode) |
| 656 | removed++; |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 657 | |
| Linus Torvalds | ccc4feb | 2005-04-15 17:44:27 | [diff] [blame] | 658 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 659 | hdr.hdr_version = htonl(2); |
| Junio C Hamano | 025a070 | 2005-06-10 08:32:37 | [diff] [blame] | 660 | hdr.hdr_entries = htonl(entries - removed); |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 661 | |
| 662 | SHA1_Init(&c); |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 663 | if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0) |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 664 | return -1; |
| 665 | |
| 666 | for (i = 0; i < entries; i++) { |
| 667 | struct cache_entry *ce = cache[i]; |
| Linus Torvalds | aa16021 | 2005-06-09 22:34:04 | [diff] [blame] | 668 | if (!ce->ce_mode) |
| 669 | continue; |
| Junio C Hamano | 407c8eb | 2005-12-20 20:12:18 | [diff] [blame] | 670 | if (index_file_timestamp && |
| 671 | index_file_timestamp <= ntohl(ce->ce_mtime.sec)) |
| 672 | ce_smudge_racily_clean_entry(ce); |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 673 | if (ce_write(&c, newfd, ce, ce_size(ce)) < 0) |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 674 | return -1; |
| 675 | } |
| Linus Torvalds | ca9be05 | 2005-04-20 19:36:41 | [diff] [blame] | 676 | return ce_flush(&c, newfd); |
| Linus Torvalds | 197ee8c | 2005-04-09 19:09:27 | [diff] [blame] | 677 | } |