Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e94b6a

Browse files
committed
iterator: cleanups with symlink dir handling
Perform some error checking when examining symlink directories.
1 parent e9628e7 commit 9e94b6a

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

src/iterator.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#define iterator__has_been_accessed(I) iterator__flag(I,FIRST_ACCESS)
2424
#define iterator__honor_ignores(I) iterator__flag(I,HONOR_IGNORES)
2525
#define iterator__ignore_dot_git(I) iterator__flag(I,IGNORE_DOT_GIT)
26-
#define iterator__symlinksdir(I) iterator__flag(I,INCLUDE_SYMLINK_REFSDIR)
26+
#define iterator__descend_symlinks(I) iterator__flag(I,DESCEND_SYMLINKS)
2727

2828

2929
static void iterator_set_ignore_case(git_iterator *iter, bool ignore_case)
@@ -1492,29 +1492,41 @@ static int filesystem_iterator_current(
14921492
return 0;
14931493
}
14941494

1495-
static int is_directory(
1496-
const filesystem_iterator *iter, const filesystem_iterator_entry *entry)
1495+
static int filesystem_iterator_is_dir(
1496+
bool *is_dir,
1497+
const filesystem_iterator *iter,
1498+
const filesystem_iterator_entry *entry)
14971499
{
1498-
int isdir = 0;
14991500
struct stat st;
1500-
git_buf fullpath;
1501+
git_buf fullpath = GIT_BUF_INIT;
1502+
int error = 0;
15011503

1502-
if (S_ISDIR(entry->st.st_mode))
1503-
return 1;
1504-
if (!iterator__symlinksdir(iter) || !S_ISLNK(entry->st.st_mode))
1505-
return 0;
1504+
if (S_ISDIR(entry->st.st_mode)) {
1505+
*is_dir = 1;
1506+
goto done;
1507+
}
1508+
1509+
if (!iterator__descend_symlinks(iter) || !S_ISLNK(entry->st.st_mode)) {
1510+
*is_dir = 0;
1511+
goto done;
1512+
}
15061513

1507-
git_buf_init(&fullpath, 0);
1508-
git_buf_joinpath(&fullpath, iter->root, entry->path);
1509-
isdir = !p_stat(fullpath.ptr, &st) && S_ISDIR(st.st_mode);
1514+
if ((error = git_buf_joinpath(&fullpath, iter->root, entry->path)) < 0 ||
1515+
(error = p_stat(fullpath.ptr, &st)) < 0)
1516+
goto done;
1517+
1518+
*is_dir = S_ISDIR(st.st_mode);
1519+
1520+
done:
15101521
git_buf_free(&fullpath);
1511-
return isdir;
1522+
return error;
15121523
}
15131524

15141525
static int filesystem_iterator_advance(
15151526
const git_index_entry **out, git_iterator *i)
15161527
{
15171528
filesystem_iterator *iter = (filesystem_iterator *)i;
1529+
bool is_dir;
15181530
int error = 0;
15191531

15201532
iter->base.flags |= GIT_ITERATOR_FIRST_ACCESS;
@@ -1539,7 +1551,10 @@ static int filesystem_iterator_advance(
15391551
entry = frame->entries.contents[frame->next_idx];
15401552
frame->next_idx++;
15411553

1542-
if (is_directory(iter, entry)) {
1554+
if ((error = filesystem_iterator_is_dir(&is_dir, iter, entry)) < 0)
1555+
break;
1556+
1557+
if (is_dir) {
15431558
if (iterator__do_autoexpand(iter)) {
15441559
error = filesystem_iterator_frame_push(iter, entry);
15451560

src/iterator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ typedef enum {
3939
GIT_ITERATOR_DONT_PRECOMPOSE_UNICODE = (1u << 5),
4040
/** include conflicts */
4141
GIT_ITERATOR_INCLUDE_CONFLICTS = (1u << 6),
42-
/** descend into symlinked directories when looking for references */
43-
GIT_ITERATOR_INCLUDE_SYMLINK_REFSDIR = (1u << 7),
42+
/** descend into symlinked directories */
43+
GIT_ITERATOR_DESCEND_SYMLINKS = (1u << 7),
4444
} git_iterator_flag_t;
4545

4646
typedef enum {

src/refdb_fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2035,7 +2035,7 @@ int git_refdb_backend_fs(
20352035
if ((!git_repository__cvar(&t, backend->repo, GIT_CVAR_FSYNCOBJECTFILES) && t) ||
20362036
git_repository__fsync_gitdir)
20372037
backend->fsync = 1;
2038-
backend->iterator_flags |= GIT_ITERATOR_INCLUDE_SYMLINK_REFSDIR;
2038+
backend->iterator_flags |= GIT_ITERATOR_DESCEND_SYMLINKS;
20392039

20402040
backend->parent.exists = &refdb_fs_backend__exists;
20412041
backend->parent.lookup = &refdb_fs_backend__lookup;

0 commit comments

Comments
 (0)