Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9efdfdb6 authored by Yabin Cui's avatar Yabin Cui Committed by android-build-merger
Browse files

Merge "base: add API to remove file if it exists." am: aef26bb1

am: f79a5f51

* commit 'f79a5f51':
  base: add API to remove file if it exists.
parents 7fd384bf f79a5f51
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -149,5 +149,32 @@ bool WriteFully(int fd, const void* data, size_t byte_count) {
  return true;
}

bool RemoveFileIfExists(const std::string& path, std::string* err) {
  struct stat st;
#if defined(_WIN32)
  //TODO: Windows version can't handle symbol link correctly.
  int result = stat(path.c_str(), &st);
  bool file_type_removable = (result == 0 && S_ISREG(st.st_mode));
#else
  int result = lstat(path.c_str(), &st);
  bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)));
#endif
  if (result == 0) {
    if (!file_type_removable) {
      if (err != nullptr) {
        *err = "is not a regular or symbol link file";
      }
      return false;
    }
    if (unlink(path.c_str()) == -1) {
      if (err != nullptr) {
        *err = strerror(errno);
      }
      return false;
    }
  }
  return true;
}

}  // namespace base
}  // namespace android
+14 −0
Original line number Diff line number Diff line
@@ -96,3 +96,17 @@ TEST(file, WriteFully) {
  s.resize(1024);
  ASSERT_FALSE(android::base::ReadFully(tf.fd, &s[0], s.size()));
}

TEST(file, RemoveFileIfExist) {
  TemporaryFile tf;
  ASSERT_TRUE(tf.fd != -1);
  close(tf.fd);
  tf.fd = -1;
  std::string err;
  ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path, &err)) << err;
  ASSERT_TRUE(android::base::RemoveFileIfExists(tf.path));
  TemporaryDir td;
  ASSERT_FALSE(android::base::RemoveFileIfExists(td.path));
  ASSERT_FALSE(android::base::RemoveFileIfExists(td.path, &err));
  ASSERT_EQ("is not a regular or symbol link file", err);
}
+2 −0
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ bool WriteStringToFile(const std::string& content, const std::string& path,
bool ReadFully(int fd, void* data, size_t byte_count);
bool WriteFully(int fd, const void* data, size_t byte_count);

bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);

}  // namespace base
}  // namespace android