git: b13ac6784202 - main - path_test: Verify that operations on unlinked files work

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 18 Apr 2022 21:55:48 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=b13ac678420292f5994b0b6e0f27995b9399268b

commit b13ac678420292f5994b0b6e0f27995b9399268b
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-04-18 21:46:04 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-04-18 21:55:24 +0000

    path_test: Verify that operations on unlinked files work
    
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
---
 tests/sys/file/path_test.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/tests/sys/file/path_test.c b/tests/sys/file/path_test.c
index e8c8410a0bdf..a212325c7db6 100644
--- a/tests/sys/file/path_test.c
+++ b/tests/sys/file/path_test.c
@@ -900,6 +900,38 @@ ATF_TC_BODY(path_unix, tc)
 	CHECKED_CLOSE(pathfd);
 }
 
+/*
+ * Check that we can perform operations using an O_PATH fd for an unlinked file.
+ */
+ATF_TC_WITHOUT_HEAD(path_unlinked);
+ATF_TC_BODY(path_unlinked, tc)
+{
+	char path[PATH_MAX];
+	struct stat sb;
+	int pathfd;
+
+	mktfile(path, "path_rights.XXXXXX");
+
+	pathfd = open(path, O_PATH);
+	ATF_REQUIRE_MSG(pathfd >= 0, FMT_ERR("open"));
+
+	ATF_REQUIRE_MSG(fstatat(pathfd, "", &sb, AT_EMPTY_PATH) == 0,
+	    FMT_ERR("fstatat"));
+	ATF_REQUIRE(sb.st_nlink == 1);
+	ATF_REQUIRE_MSG(fstat(pathfd, &sb) == 0, FMT_ERR("fstat"));
+	ATF_REQUIRE(sb.st_nlink == 1);
+
+	ATF_REQUIRE_MSG(unlink(path) == 0, FMT_ERR("unlink"));
+
+	ATF_REQUIRE_MSG(fstatat(pathfd, "", &sb, AT_EMPTY_PATH) == 0,
+	    FMT_ERR("fstatat"));
+	ATF_REQUIRE(sb.st_nlink == 0);
+	ATF_REQUIRE_MSG(fstat(pathfd, &sb) == 0, FMT_ERR("fstat"));
+	ATF_REQUIRE(sb.st_nlink == 0);
+
+	CHECKED_CLOSE(pathfd);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, path_access);
@@ -922,6 +954,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, path_pipe_fstatat);
 	ATF_TP_ADD_TC(tp, path_rights);
 	ATF_TP_ADD_TC(tp, path_unix);
+	ATF_TP_ADD_TC(tp, path_unlinked);
 
 	return (atf_no_error());
 }