Merge "Add android::base::Realpath." am: b3fde950f2

am: 424bdf9ca1

Change-Id: Icccf533964855a9df160b0562ae41d1a896f828a
This commit is contained in:
Dimitry Ivanov 2017-03-16 06:59:37 +00:00 committed by android-build-merger
commit 0c856b8a04
3 changed files with 47 additions and 0 deletions

View file

@ -212,6 +212,20 @@ bool Readlink(const std::string& path, std::string* result) {
}
#endif
#if !defined(_WIN32)
bool Realpath(const std::string& path, std::string* result) {
result->clear();
char* realpath_buf = realpath(path.c_str(), nullptr);
if (realpath_buf == nullptr) {
return false;
}
result->assign(realpath_buf);
free(realpath_buf);
return true;
}
#endif
std::string GetExecutablePath() {
#if defined(__linux__)
std::string path;

View file

@ -159,6 +159,38 @@ TEST(file, Readlink) {
#endif
}
TEST(file, Realpath) {
#if !defined(_WIN32)
TemporaryDir td;
std::string basename = android::base::Basename(td.path);
std::string dir_name = android::base::Dirname(td.path);
std::string base_dir_name = android::base::Basename(dir_name);
{
std::string path = dir_name + "/../" + base_dir_name + "/" + basename;
std::string result;
ASSERT_TRUE(android::base::Realpath(path, &result));
ASSERT_EQ(td.path, result);
}
{
std::string path = std::string(td.path) + "/..";
std::string result;
ASSERT_TRUE(android::base::Realpath(path, &result));
ASSERT_EQ(dir_name, result);
}
{
errno = 0;
std::string path = std::string(td.path) + "/foo.noent";
std::string result = "wrong";
ASSERT_TRUE(!android::base::Realpath(path, &result));
ASSERT_TRUE(result.empty());
ASSERT_EQ(ENOENT, errno);
}
#endif
}
TEST(file, GetExecutableDirectory) {
std::string path = android::base::GetExecutableDirectory();
ASSERT_NE("", path);

View file

@ -47,6 +47,7 @@ bool WriteFully(int fd, const void* data, size_t byte_count);
bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr);
#if !defined(_WIN32)
bool Realpath(const std::string& path, std::string* result);
bool Readlink(const std::string& path, std::string* result);
#endif