Merge "Add android::base::Realpath."
This commit is contained in:
commit
b3fde950f2
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue