aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAllen Webb <allenwebb@google.com>2019-07-30 16:48:20 -0700
committerchrome-bot <chrome-bot@chromium.org>2019-08-14 05:15:47 -0700
commitc77587921646bdfc4286680333d1ece701f18987 (patch)
treed6d5c7537da9254a1e942a50c66caf49b647ec15
parentd23247f9f9dee9834c5551f33b376780227a4418 (diff)
downloadplatform_external_libbrillo-c77587921646bdfc4286680333d1ece701f18987.tar.gz
platform_external_libbrillo-c77587921646bdfc4286680333d1ece701f18987.tar.bz2
platform_external_libbrillo-c77587921646bdfc4286680333d1ece701f18987.zip
libbrillo: Add SafeFD::Error::kDoesNotExist.
BUG=chromium:977388 TEST=FEATURES=test emerge-${BOARD} libbrillo Change-Id: Idbff7a7c1bf8f30134b00c0bb6bc762de72635d6 Reviewed-on: https://chromium-review.googlesource.com/1726746 Tested-by: Allen Webb <allenwebb@google.com> Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org> Reviewed-by: Mike Frysinger <vapier@chromium.org> Reviewed-by: Jorge Lucangeli Obes <jorgelo@chromium.org> Reviewed-by: Mattias Nissler <mnissler@chromium.org> Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2 Cr-Mirrored-Commit: f4214a5032b781973bae3ed8519bb93dd6106dfc
-rw-r--r--brillo/files/safe_fd.cc4
-rw-r--r--brillo/files/safe_fd.h3
-rw-r--r--brillo/files/safe_fd_test.cc21
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());
}