diff options
-rw-r--r-- | brillo/files/safe_fd.cc | 4 | ||||
-rw-r--r-- | brillo/files/safe_fd.h | 3 | ||||
-rw-r--r-- | brillo/files/safe_fd_test.cc | 21 |
3 files changed, 26 insertions, 2 deletions
diff --git a/brillo/files/safe_fd.cc b/brillo/files/safe_fd.cc index 1b4596f..3823387 100644 --- a/brillo/files/safe_fd.cc +++ b/brillo/files/safe_fd.cc @@ -48,6 +48,10 @@ SafeFD::SafeFDResult OpenPathComponentInternal(int parent_fd, // symlink. It fails with ENXIO when |path| is a FIFO and |flags| is for // writing because of the O_NONBLOCK flag added above. switch (errno) { + case ENOENT: + // Do not write to the log because opening a non-existent file is a + // frequent occurrence. + return MakeErrorResult(SafeFD::Error::kDoesNotExist); case ELOOP: // PLOG prints something along the lines of the symlink depth being too // great which is is misleading so LOG is used instead. diff --git a/brillo/files/safe_fd.h b/brillo/files/safe_fd.h index cb4b46d..cb3612c 100644 --- a/brillo/files/safe_fd.h +++ b/brillo/files/safe_fd.h @@ -52,7 +52,8 @@ class SafeFD { kNoError = 0, kBadArgument, kNotInitialized, // Invalid operation on a SafeFD that was not initialized. - kIOError, // (e.g. called OpenExistingFile on a non-existent file) + kIOError, // Check errno for specific cause. + kDoesNotExist, // The specified path does not exist. kSymlinkDetected, kWrongType, // (e.g. got a directory and expected a file) kWrongUID, diff --git a/brillo/files/safe_fd_test.cc b/brillo/files/safe_fd_test.cc index 878a937..60406fa 100644 --- a/brillo/files/safe_fd_test.cc +++ b/brillo/files/safe_fd_test.cc @@ -27,6 +27,7 @@ std::string to_string(brillo::SafeFD::Error err) { TO_STRING_HELPER(kBadArgument) TO_STRING_HELPER(kNotInitialized) TO_STRING_HELPER(kIOError) + TO_STRING_HELPER(kDoesNotExist) TO_STRING_HELPER(kSymlinkDetected) TO_STRING_HELPER(kWrongType) TO_STRING_HELPER(kWrongUID) @@ -331,7 +332,16 @@ TEST_F(SafeFDTest, OpenExistingFile_NotInitialized) { ASSERT_FALSE(file.first.is_valid()); } +TEST_F(SafeFDTest, OpenExistingFile_DoesNotExist) { + SafeFD::SafeFDResult file = root_.OpenExistingFile(file_path_); + EXPECT_STR_EQ(file.second, SafeFD::Error::kDoesNotExist); + ASSERT_FALSE(file.first.is_valid()); +} + TEST_F(SafeFDTest, OpenExistingFile_IOError) { + ASSERT_TRUE(WriteFile("")); + EXPECT_EQ(chmod(file_path_.value().c_str(), 0000), 0) << strerror(errno); + SafeFD::SafeFDResult file = root_.OpenExistingFile(file_path_); EXPECT_STR_EQ(file.second, SafeFD::Error::kIOError); ASSERT_FALSE(file.first.is_valid()); @@ -366,8 +376,17 @@ TEST_F(SafeFDTest, OpenExistingDir_NotInitialized) { ASSERT_FALSE(dir.first.is_valid()); } +TEST_F(SafeFDTest, OpenExistingDir_DoesNotExist) { + SafeFD::SafeFDResult dir = root_.OpenExistingDir(sub_dir_path_); + EXPECT_STR_EQ(dir.second, SafeFD::Error::kDoesNotExist); + ASSERT_FALSE(dir.first.is_valid()); +} + TEST_F(SafeFDTest, OpenExistingDir_IOError) { - SafeFD::SafeFDResult dir = root_.OpenExistingDir(file_path_); + ASSERT_TRUE(WriteFile("")); + EXPECT_EQ(chmod(sub_dir_path_.value().c_str(), 0000), 0) << strerror(errno); + + SafeFD::SafeFDResult dir = root_.OpenExistingDir(sub_dir_path_); EXPECT_STR_EQ(dir.second, SafeFD::Error::kIOError); ASSERT_FALSE(dir.first.is_valid()); } |