diff options
author | Dimitry Ivanov | 2016-09-09 12:49:21 -0500 |
---|---|---|
committer | Dimitry Ivanov | 2017-03-16 00:20:50 -0500 |
commit | 840b6019c035eb3abeadfb2dc8abac38692a829b (patch) | |
tree | 63fe419b95c073f43eeabe77f159186e61f789b7 | |
parent | e7b335b4712bc650c93b5b5777a6f8b1d85ad83f (diff) | |
download | platform-system-core-840b6019c035eb3abeadfb2dc8abac38692a829b.tar.gz platform-system-core-840b6019c035eb3abeadfb2dc8abac38692a829b.tar.xz platform-system-core-840b6019c035eb3abeadfb2dc8abac38692a829b.zip |
Add android::base::Realpath.
Bug: http://b/31396973
Test: libbase_test on host and device
Change-Id: I1e5f15c76227ec1c2128baa38eb454d347987703
-rw-r--r-- | base/file.cpp | 14 | ||||
-rw-r--r-- | base/file_test.cpp | 32 | ||||
-rw-r--r-- | base/include/android-base/file.h | 1 |
3 files changed, 47 insertions, 0 deletions
diff --git a/base/file.cpp b/base/file.cpp index 81b04d74e..378a405bc 100644 --- a/base/file.cpp +++ b/base/file.cpp | |||
@@ -212,6 +212,20 @@ bool Readlink(const std::string& path, std::string* result) { | |||
212 | } | 212 | } |
213 | #endif | 213 | #endif |
214 | 214 | ||
215 | #if !defined(_WIN32) | ||
216 | bool Realpath(const std::string& path, std::string* result) { | ||
217 | result->clear(); | ||
218 | |||
219 | char* realpath_buf = realpath(path.c_str(), nullptr); | ||
220 | if (realpath_buf == nullptr) { | ||
221 | return false; | ||
222 | } | ||
223 | result->assign(realpath_buf); | ||
224 | free(realpath_buf); | ||
225 | return true; | ||
226 | } | ||
227 | #endif | ||
228 | |||
215 | std::string GetExecutablePath() { | 229 | std::string GetExecutablePath() { |
216 | #if defined(__linux__) | 230 | #if defined(__linux__) |
217 | std::string path; | 231 | std::string path; |
diff --git a/base/file_test.cpp b/base/file_test.cpp index 102132626..266131ecf 100644 --- a/base/file_test.cpp +++ b/base/file_test.cpp | |||
@@ -159,6 +159,38 @@ TEST(file, Readlink) { | |||
159 | #endif | 159 | #endif |
160 | } | 160 | } |
161 | 161 | ||
162 | TEST(file, Realpath) { | ||
163 | #if !defined(_WIN32) | ||
164 | TemporaryDir td; | ||
165 | std::string basename = android::base::Basename(td.path); | ||
166 | std::string dir_name = android::base::Dirname(td.path); | ||
167 | std::string base_dir_name = android::base::Basename(dir_name); | ||
168 | |||
169 | { | ||
170 | std::string path = dir_name + "/../" + base_dir_name + "/" + basename; | ||
171 | std::string result; | ||
172 | ASSERT_TRUE(android::base::Realpath(path, &result)); | ||
173 | ASSERT_EQ(td.path, result); | ||
174 | } | ||
175 | |||
176 | { | ||
177 | std::string path = std::string(td.path) + "/.."; | ||
178 | std::string result; | ||
179 | ASSERT_TRUE(android::base::Realpath(path, &result)); | ||
180 | ASSERT_EQ(dir_name, result); | ||
181 | } | ||
182 | |||
183 | { | ||
184 | errno = 0; | ||
185 | std::string path = std::string(td.path) + "/foo.noent"; | ||
186 | std::string result = "wrong"; | ||
187 | ASSERT_TRUE(!android::base::Realpath(path, &result)); | ||
188 | ASSERT_TRUE(result.empty()); | ||
189 | ASSERT_EQ(ENOENT, errno); | ||
190 | } | ||
191 | #endif | ||
192 | } | ||
193 | |||
162 | TEST(file, GetExecutableDirectory) { | 194 | TEST(file, GetExecutableDirectory) { |
163 | std::string path = android::base::GetExecutableDirectory(); | 195 | std::string path = android::base::GetExecutableDirectory(); |
164 | ASSERT_NE("", path); | 196 | ASSERT_NE("", path); |
diff --git a/base/include/android-base/file.h b/base/include/android-base/file.h index 33d1ab320..651f52962 100644 --- a/base/include/android-base/file.h +++ b/base/include/android-base/file.h | |||
@@ -47,6 +47,7 @@ bool WriteFully(int fd, const void* data, size_t byte_count); | |||
47 | bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); | 47 | bool RemoveFileIfExists(const std::string& path, std::string* err = nullptr); |
48 | 48 | ||
49 | #if !defined(_WIN32) | 49 | #if !defined(_WIN32) |
50 | bool Realpath(const std::string& path, std::string* result); | ||
50 | bool Readlink(const std::string& path, std::string* result); | 51 | bool Readlink(const std::string& path, std::string* result); |
51 | #endif | 52 | #endif |
52 | 53 | ||