Commit 3b259cbd1afdc96a3c3eb7af5895b310c1ac2a7d

Russell Belfer 2013-11-04T15:47:35

Preserve file error in iterator When the filesystem iterator encounters an error with a file, it returns the error but because of the cleanup code, it was in some cases erasing the error message. This uses the giterr_detach API to make sure that the actual error message is restored after the cleanup code has been run.

diff --git a/src/iterator.c b/src/iterator.c
index c0d7862..369a079 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -991,8 +991,20 @@ static int fs_iterator__expand_dir(fs_iterator *fi)
 		fi->base.start, fi->base.end, &ff->entries);
 
 	if (error < 0) {
+		git_buf     msg = GIT_BUF_INIT;
+		git_error_t errt = giterr_detach(&msg);
+
+		/* these callbacks may clear the error message */
 		fs_iterator__free_frame(ff);
 		fs_iterator__advance_over(NULL, (git_iterator *)fi);
+		/* next time return value we skipped to */
+		fi->base.flags &= ~GIT_ITERATOR_FIRST_ACCESS;
+
+		if (msg.ptr) {
+			giterr_set_str(errt, msg.ptr);
+			git_buf_free(&msg);
+		}
+
 		return error;
 	}
 
diff --git a/tests-clar/repo/iterator.c b/tests-clar/repo/iterator.c
index 1c513e9..56b5185 100644
--- a/tests-clar/repo/iterator.c
+++ b/tests-clar/repo/iterator.c
@@ -926,3 +926,37 @@ void test_repo_iterator__fs2(void)
 	expect_iterator_items(i, 12, expect_base, 12, expect_base);
 	git_iterator_free(i);
 }
+
+void test_repo_iterator__fs_preserves_error(void)
+{
+	git_iterator *i;
+	const git_index_entry *e;
+
+	if (!cl_is_chmod_supported())
+		return;
+
+	g_repo = cl_git_sandbox_init("empty_standard_repo");
+
+	cl_must_pass(p_mkdir("empty_standard_repo/r", 0777));
+	cl_git_mkfile("empty_standard_repo/r/a", "hello");
+	cl_must_pass(p_mkdir("empty_standard_repo/r/b", 0777));
+	cl_git_mkfile("empty_standard_repo/r/b/problem", "not me");
+	cl_must_pass(p_chmod("empty_standard_repo/r/b", 0000));
+	cl_must_pass(p_mkdir("empty_standard_repo/r/c", 0777));
+	cl_git_mkfile("empty_standard_repo/r/d", "final");
+
+	cl_git_pass(git_iterator_for_filesystem(
+		&i, "empty_standard_repo/r", 0, NULL, NULL));
+
+	cl_git_pass(git_iterator_advance(&e, i)); /* a */
+	cl_git_fail(git_iterator_advance(&e, i)); /* b */
+	cl_assert(giterr_last());
+	cl_assert(giterr_last()->message != NULL);
+	/* skip 'c/' empty directory */
+	cl_git_pass(git_iterator_advance(&e, i)); /* d */
+	cl_assert_equal_i(GIT_ITEROVER, git_iterator_advance(&e, i));
+
+	cl_must_pass(p_chmod("empty_standard_repo/r/b", 0777));
+
+	git_iterator_free(i);
+}