Merge branch 'es/name-hash-no-trailing-slash-in-dirs'

Clean up the internal of the name-hash mechanism used to work
around case insensitivity on some filesystems to cleanly fix a
long-standing API glitch where the caller of cache_name_exists()
that ask about a directory with a counted string was required to
have '/' at one location past the end of the string.

* es/name-hash-no-trailing-slash-in-dirs:
  dir: revert work-around for retired dangerous behavior
  name-hash: stop storing trailing '/' on paths in index_state.dir_hash
  employ new explicit "exists in index?" API
  name-hash: refactor polymorphic index_name_exists()
This commit is contained in:
Junio C Hamano 2013-10-17 15:55:15 -07:00
commit d6a58b7773
5 changed files with 50 additions and 51 deletions

View File

@ -314,6 +314,8 @@ extern void free_name_hash(struct index_state *istate);
#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL) #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options)) #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
#define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options)) #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
#define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
#define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
#define cache_name_exists(name, namelen, igncase) index_name_exists(&the_index, (name), (namelen), (igncase)) #define cache_name_exists(name, namelen, igncase) index_name_exists(&the_index, (name), (namelen), (igncase))
#define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen)) #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
#define resolve_undo_clear() resolve_undo_clear_index(&the_index) #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
@ -463,6 +465,8 @@ extern int write_index(struct index_state *, int newfd);
extern int discard_index(struct index_state *); extern int discard_index(struct index_state *);
extern int unmerged_index(const struct index_state *); extern int unmerged_index(const struct index_state *);
extern int verify_path(const char *path); extern int verify_path(const char *path);
extern struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen);
extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
extern struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int igncase); extern struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int igncase);
extern int index_name_pos(const struct index_state *, const char *name, int namelen); extern int index_name_pos(const struct index_state *, const char *name, int namelen);
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */ #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */

28
dir.c
View File

@ -860,7 +860,7 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len)
static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len) static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
{ {
if (cache_name_exists(pathname, len, ignore_case)) if (cache_file_exists(pathname, len, ignore_case))
return NULL; return NULL;
ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
@ -885,11 +885,11 @@ enum exist_status {
/* /*
* Do not use the alphabetically sorted index to look up * Do not use the alphabetically sorted index to look up
* the directory name; instead, use the case insensitive * the directory name; instead, use the case insensitive
* name hash. * directory hash.
*/ */
static enum exist_status directory_exists_in_index_icase(const char *dirname, int len) static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
{ {
const struct cache_entry *ce = cache_name_exists(dirname, len + 1, ignore_case); const struct cache_entry *ce = cache_dir_exists(dirname, len);
unsigned char endchar; unsigned char endchar;
if (!ce) if (!ce)
@ -1071,7 +1071,7 @@ static int get_index_dtype(const char *path, int len)
int pos; int pos;
const struct cache_entry *ce; const struct cache_entry *ce;
ce = cache_name_exists(path, len, 0); ce = cache_file_exists(path, len, 0);
if (ce) { if (ce) {
if (!ce_uptodate(ce)) if (!ce_uptodate(ce))
return DT_UNKNOWN; return DT_UNKNOWN;
@ -1131,7 +1131,7 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
int dtype, struct dirent *de) int dtype, struct dirent *de)
{ {
int exclude; int exclude;
int has_path_in_index = !!cache_name_exists(path->buf, path->len, ignore_case); int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);
if (dtype == DT_UNKNOWN) if (dtype == DT_UNKNOWN)
dtype = get_dtype(de, path->buf, path->len); dtype = get_dtype(de, path->buf, path->len);
@ -1160,21 +1160,9 @@ static enum path_treatment treat_one_path(struct dir_struct *dir,
*/ */
if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&
(dtype == DT_DIR) && (dtype == DT_DIR) &&
!has_path_in_index) { !has_path_in_index &&
/* (directory_exists_in_index(path->buf, path->len) == index_nonexistent))
* NEEDSWORK: directory_exists_in_index_icase() return path_none;
* assumes that one byte past the given path is
* readable and has '/', which needs to be fixed, but
* until then, work it around in the caller.
*/
strbuf_addch(path, '/');
if (directory_exists_in_index(path->buf, path->len - 1) ==
index_nonexistent) {
strbuf_setlen(path, path->len - 1);
return path_none;
}
strbuf_setlen(path, path->len - 1);
}
exclude = is_excluded(dir, path->buf, &dtype); exclude = is_excluded(dir, path->buf, &dtype);

View File

@ -58,9 +58,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
{ {
/* /*
* Throw each directory component in the hash for quick lookup * Throw each directory component in the hash for quick lookup
* during a git status. Directory components are stored with their * during a git status. Directory components are stored without their
* closing slash. Despite submodules being a directory, they never * closing slash. Despite submodules being a directory, they never
* reach this point, because they are stored without a closing slash * reach this point, because they are stored
* in index_state.name_hash (as ordinary cache_entries). * in index_state.name_hash (as ordinary cache_entries).
* *
* Note that the cache_entry stored with the dir_entry merely * Note that the cache_entry stored with the dir_entry merely
@ -78,6 +78,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
namelen--; namelen--;
if (namelen <= 0) if (namelen <= 0)
return NULL; return NULL;
namelen--;
/* lookup existing entry for that directory */ /* lookup existing entry for that directory */
dir = find_dir_entry(istate, ce->name, namelen); dir = find_dir_entry(istate, ce->name, namelen);
@ -97,7 +98,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
} }
/* recursively add missing parent directories */ /* recursively add missing parent directories */
dir->parent = hash_dir_entry(istate, ce, namelen - 1); dir->parent = hash_dir_entry(istate, ce, namelen);
} }
return dir; return dir;
} }
@ -222,7 +223,29 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
return slow_same_name(name, namelen, ce->name, len); return slow_same_name(name, namelen, ce->name, len);
} }
struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase) struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen)
{
struct cache_entry *ce;
struct dir_entry *dir;
lazy_init_name_hash(istate);
dir = find_dir_entry(istate, name, namelen);
if (dir && dir->nr)
return dir->ce;
/*
* It might be a submodule. Unlike plain directories, which are stored
* in the dir-hash, submodules are stored in the name-hash, so check
* there, as well.
*/
ce = index_file_exists(istate, name, namelen, 1);
if (ce && S_ISGITLINK(ce->ce_mode))
return ce;
return NULL;
}
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
{ {
unsigned int hash = hash_name(name, namelen); unsigned int hash = hash_name(name, namelen);
struct cache_entry *ce; struct cache_entry *ce;
@ -237,32 +260,16 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na
} }
ce = ce->next; ce = ce->next;
} }
/*
* When looking for a directory (trailing '/'), it might be a
* submodule or a directory. Despite submodules being directories,
* they are stored in the name hash without a closing slash.
* When ignore_case is 1, directories are stored in a separate hash
* table *with* their closing slash.
*
* The side effect of this storage technique is we have need to
* lookup the directory in a separate hash table, and if not found
* remove the slash from name and perform the lookup again without
* the slash. If a match is made, S_ISGITLINK(ce->mode) will be
* true.
*/
if (icase && name[namelen - 1] == '/') {
struct dir_entry *dir = find_dir_entry(istate, name, namelen);
if (dir && dir->nr)
return dir->ce;
ce = index_name_exists(istate, name, namelen - 1, icase);
if (ce && S_ISGITLINK(ce->ce_mode))
return ce;
}
return NULL; return NULL;
} }
struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
{
if (namelen > 0 && name[namelen - 1] == '/')
return index_dir_exists(istate, name, namelen - 1);
return index_file_exists(istate, name, namelen, icase);
}
static int free_dir_entry(void *entry, void *unused) static int free_dir_entry(void *entry, void *unused)
{ {
struct dir_entry *dir = entry; struct dir_entry *dir = entry;

View File

@ -643,7 +643,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
if (*ptr == '/') { if (*ptr == '/') {
struct cache_entry *foundce; struct cache_entry *foundce;
++ptr; ++ptr;
foundce = index_name_exists(istate, ce->name, ptr - ce->name, ignore_case); foundce = index_dir_exists(istate, ce->name, ptr - ce->name - 1);
if (foundce) { if (foundce) {
memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr);
startPtr = ptr; startPtr = ptr;
@ -652,7 +652,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
} }
} }
alias = index_name_exists(istate, ce->name, ce_namelen(ce), ignore_case); alias = index_file_exists(istate, ce->name, ce_namelen(ce), ignore_case);
if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) {
/* Nothing changed, really */ /* Nothing changed, really */
free(ce); free(ce);

View File

@ -1357,7 +1357,7 @@ static int icase_exists(struct unpack_trees_options *o, const char *name, int le
{ {
const struct cache_entry *src; const struct cache_entry *src;
src = index_name_exists(o->src_index, name, len, 1); src = index_file_exists(o->src_index, name, len, 1);
return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE); return src && !ie_match_stat(o->src_index, src, st, CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE);
} }
@ -1403,7 +1403,7 @@ static int check_ok_to_remove(const char *name, int len, int dtype,
* delete this path, which is in a subdirectory that * delete this path, which is in a subdirectory that
* is being replaced with a blob. * is being replaced with a blob.
*/ */
result = index_name_exists(&o->result, name, len, 0); result = index_file_exists(&o->result, name, len, 0);
if (result) { if (result) {
if (result->ce_flags & CE_REMOVE) if (result->ce_flags & CE_REMOVE)
return 0; return 0;