blob: 97cc2b27f7f102186636d403137176f0a63e4f9e [file] [log] [blame]
Dan Albertaac6b7c2015-03-16 10:08:46 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughesb6351622015-12-04 22:00:26 -080017#include "android-base/file.h"
Dan Albertaac6b7c2015-03-16 10:08:46 -070018
19#include <errno.h>
20#include <fcntl.h>
Mark Salyzynf2e76152018-11-13 13:38:33 -080021#include <ftw.h>
Colin Cross2e732e22017-02-23 21:23:05 -080022#include <libgen.h>
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080023#include <stdio.h>
24#include <stdlib.h>
Florian Mayerc08f3d42018-10-23 15:56:28 +010025#include <string.h>
Dan Albertaac6b7c2015-03-16 10:08:46 -070026#include <sys/stat.h>
27#include <sys/types.h>
Elliott Hughesa634a9a2016-08-23 15:53:45 -070028#include <unistd.h>
Dan Albertaac6b7c2015-03-16 10:08:46 -070029
Josh Gao58668ac2016-09-01 12:31:42 -070030#include <memory>
Colin Cross2e732e22017-02-23 21:23:05 -080031#include <mutex>
Dan Albertaac6b7c2015-03-16 10:08:46 -070032#include <string>
Elliott Hughesa634a9a2016-08-23 15:53:45 -070033#include <vector>
Dan Albertaac6b7c2015-03-16 10:08:46 -070034
Elliott Hughes48f0eb52016-08-31 15:07:18 -070035#if defined(__APPLE__)
Josh Gao58668ac2016-09-01 12:31:42 -070036#include <mach-o/dyld.h>
Elliott Hughes48f0eb52016-08-31 15:07:18 -070037#endif
38#if defined(_WIN32)
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080039#include <direct.h>
Elliott Hughes48f0eb52016-08-31 15:07:18 -070040#include <windows.h>
Elliott Hughes62ecbaa2017-05-15 17:31:15 -070041#define O_NOFOLLOW 0
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080042#define OS_PATH_SEPARATOR '\\'
43#else
44#define OS_PATH_SEPARATOR '/'
Elliott Hughes48f0eb52016-08-31 15:07:18 -070045#endif
46
Mark Salyzynf2e76152018-11-13 13:38:33 -080047#include "android-base/logging.h" // and must be after windows.h for ERROR
48#include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
49#include "android-base/unique_fd.h"
50#include "android-base/utf8.h"
51
Alex Buynytskyyab0407f2019-09-27 11:11:24 -070052namespace {
53
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080054#ifdef _WIN32
Alex Buynytskyyab0407f2019-09-27 11:11:24 -070055static int mkstemp(char* name_template, size_t size_in_chars) {
Alex Buynytskyy19ba0882019-10-02 16:27:22 -070056 std::wstring path;
57 CHECK(android::base::UTF8ToWide(name_template, &path))
58 << "path can't be converted to wchar: " << name_template;
59 if (_wmktemp_s(path.data(), path.size() + 1) != 0) {
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080060 return -1;
61 }
Alex Buynytskyyab0407f2019-09-27 11:11:24 -070062
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080063 // Use open() to match the close() that TemporaryFile's destructor does.
64 // Use O_BINARY to match base file APIs.
Alex Buynytskyy19ba0882019-10-02 16:27:22 -070065 int fd = _wopen(path.c_str(), O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
66 if (fd < 0) {
67 return -1;
68 }
69
70 std::string path_utf8;
71 CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8";
72 CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0)
73 << "utf8 path can't be assigned back to name_template";
74
75 return fd;
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080076}
77
Alex Buynytskyyab0407f2019-09-27 11:11:24 -070078static char* mkdtemp(char* name_template, size_t size_in_chars) {
Alex Buynytskyy19ba0882019-10-02 16:27:22 -070079 std::wstring path;
80 CHECK(android::base::UTF8ToWide(name_template, &path))
81 << "path can't be converted to wchar: " << name_template;
82
83 if (_wmktemp_s(path.data(), path.size() + 1) != 0) {
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080084 return nullptr;
85 }
Alex Buynytskyyab0407f2019-09-27 11:11:24 -070086
Alex Buynytskyy19ba0882019-10-02 16:27:22 -070087 if (_wmkdir(path.c_str()) != 0) {
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080088 return nullptr;
89 }
Alex Buynytskyy19ba0882019-10-02 16:27:22 -070090
91 std::string path_utf8;
92 CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8";
93 CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0)
94 << "utf8 path can't be assigned back to name_template";
95
96 return name_template;
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080097}
98#endif
99
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800100std::string GetSystemTempDir() {
101#if defined(__ANDROID__)
Mark Salyzyn00a68032018-11-13 15:34:38 -0800102 const auto* tmpdir = getenv("TMPDIR");
103 if (tmpdir == nullptr) tmpdir = "/data/local/tmp";
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800104 if (access(tmpdir, R_OK | W_OK | X_OK) == 0) {
105 return tmpdir;
106 }
107 // Tests running in app context can't access /data/local/tmp,
108 // so try current directory if /data/local/tmp is not accessible.
109 return ".";
110#elif defined(_WIN32)
Alex Buynytskyyab0407f2019-09-27 11:11:24 -0700111 wchar_t tmp_dir_w[MAX_PATH];
112 DWORD result = GetTempPathW(std::size(tmp_dir_w), tmp_dir_w); // checks TMP env
113 CHECK_NE(result, 0ul) << "GetTempPathW failed, error: " << GetLastError();
114 CHECK_LT(result, std::size(tmp_dir_w)) << "path truncated to: " << result;
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800115
116 // GetTempPath() returns a path with a trailing slash, but init()
117 // does not expect that, so remove it.
Alex Buynytskyyab0407f2019-09-27 11:11:24 -0700118 if (tmp_dir_w[result - 1] == L'\\') {
119 tmp_dir_w[result - 1] = L'\0';
120 }
121
122 std::string tmp_dir;
123 CHECK(android::base::WideToUTF8(tmp_dir_w, &tmp_dir)) << "path can't be converted to utf8";
124
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800125 return tmp_dir;
126#else
Mark Salyzyn00a68032018-11-13 15:34:38 -0800127 const auto* tmpdir = getenv("TMPDIR");
128 if (tmpdir == nullptr) tmpdir = "/tmp";
129 return tmpdir;
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800130#endif
131}
132
133} // namespace
134
135TemporaryFile::TemporaryFile() {
136 init(GetSystemTempDir());
137}
138
139TemporaryFile::TemporaryFile(const std::string& tmp_dir) {
140 init(tmp_dir);
141}
142
143TemporaryFile::~TemporaryFile() {
144 if (fd != -1) {
145 close(fd);
146 }
147 if (remove_file_) {
148 unlink(path);
149 }
150}
151
152int TemporaryFile::release() {
153 int result = fd;
154 fd = -1;
155 return result;
156}
157
158void TemporaryFile::init(const std::string& tmp_dir) {
159 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR);
Alex Buynytskyyab0407f2019-09-27 11:11:24 -0700160#if defined(_WIN32)
161 fd = mkstemp(path, sizeof(path));
162#else
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800163 fd = mkstemp(path);
Alex Buynytskyyab0407f2019-09-27 11:11:24 -0700164#endif
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800165}
166
167TemporaryDir::TemporaryDir() {
168 init(GetSystemTempDir());
169}
170
171TemporaryDir::~TemporaryDir() {
Mark Salyzynb6f6a202018-11-13 13:38:33 -0800172 if (!remove_dir_and_contents_) return;
173
Mark Salyzynf2e76152018-11-13 13:38:33 -0800174 auto callback = [](const char* child, const struct stat*, int file_type, struct FTW*) -> int {
175 switch (file_type) {
176 case FTW_D:
177 case FTW_DP:
178 case FTW_DNR:
179 if (rmdir(child) == -1) {
180 PLOG(ERROR) << "rmdir " << child;
181 }
182 break;
183 case FTW_NS:
184 default:
185 if (rmdir(child) != -1) break;
186 // FALLTHRU (for gcc, lint, pcc, etc; and following for clang)
187 FALLTHROUGH_INTENDED;
188 case FTW_F:
189 case FTW_SL:
190 case FTW_SLN:
191 if (unlink(child) == -1) {
192 PLOG(ERROR) << "unlink " << child;
193 }
194 break;
195 }
196 return 0;
197 };
198
199 nftw(path, callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800200}
201
202bool TemporaryDir::init(const std::string& tmp_dir) {
203 snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR);
Alex Buynytskyyab0407f2019-09-27 11:11:24 -0700204#if defined(_WIN32)
205 return (mkdtemp(path, sizeof(path)) != nullptr);
206#else
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800207 return (mkdtemp(path) != nullptr);
Alex Buynytskyyab0407f2019-09-27 11:11:24 -0700208#endif
Mark Salyzyn40a4cb42018-11-12 12:29:14 -0800209}
210
Dan Albertaac6b7c2015-03-16 10:08:46 -0700211namespace android {
212namespace base {
213
Elliott Hughes774d7f62015-11-11 18:02:29 +0000214// Versions of standard library APIs that support UTF-8 strings.
215using namespace android::base::utf8;
216
Josh Gaoe4401992019-04-25 14:04:57 -0700217bool ReadFdToString(borrowed_fd fd, std::string* content) {
Dan Albertaac6b7c2015-03-16 10:08:46 -0700218 content->clear();
219
Elliott Hughese9ab7ad2017-03-20 19:16:18 -0700220 // Although original we had small files in mind, this code gets used for
221 // very large files too, where the std::string growth heuristics might not
222 // be suitable. https://code.google.com/p/android/issues/detail?id=258500.
223 struct stat sb;
Josh Gaoe4401992019-04-25 14:04:57 -0700224 if (fstat(fd.get(), &sb) != -1 && sb.st_size > 0) {
Elliott Hughese9ab7ad2017-03-20 19:16:18 -0700225 content->reserve(sb.st_size);
226 }
227
Elliott Hughes18880332020-04-29 14:10:12 -0700228 char buf[BUFSIZ] __attribute__((__uninitialized__));
Dan Albertaac6b7c2015-03-16 10:08:46 -0700229 ssize_t n;
Josh Gaoe4401992019-04-25 14:04:57 -0700230 while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) {
Dan Albertaac6b7c2015-03-16 10:08:46 -0700231 content->append(buf, n);
232 }
233 return (n == 0) ? true : false;
234}
235
Josh Gao8e1f0d82016-09-14 16:11:45 -0700236bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) {
Dan Albertaac6b7c2015-03-16 10:08:46 -0700237 content->clear();
238
Josh Gao8e1f0d82016-09-14 16:11:45 -0700239 int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
Christopher Ferrisbf0929f2017-04-05 12:09:17 -0700240 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags)));
Dan Albertaac6b7c2015-03-16 10:08:46 -0700241 if (fd == -1) {
242 return false;
243 }
Christopher Ferrisbf0929f2017-04-05 12:09:17 -0700244 return ReadFdToString(fd, content);
Dan Albertaac6b7c2015-03-16 10:08:46 -0700245}
246
Josh Gaoe4401992019-04-25 14:04:57 -0700247bool WriteStringToFd(const std::string& content, borrowed_fd fd) {
Dan Albertaac6b7c2015-03-16 10:08:46 -0700248 const char* p = content.data();
249 size_t left = content.size();
250 while (left > 0) {
Josh Gaoe4401992019-04-25 14:04:57 -0700251 ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, left));
Dan Albertaac6b7c2015-03-16 10:08:46 -0700252 if (n == -1) {
253 return false;
254 }
255 p += n;
256 left -= n;
257 }
258 return true;
259}
260
261static bool CleanUpAfterFailedWrite(const std::string& path) {
262 // Something went wrong. Let's not leave a corrupt file lying around.
263 int saved_errno = errno;
264 unlink(path.c_str());
265 errno = saved_errno;
266 return false;
267}
268
269#if !defined(_WIN32)
270bool WriteStringToFile(const std::string& content, const std::string& path,
Josh Gao8e1f0d82016-09-14 16:11:45 -0700271 mode_t mode, uid_t owner, gid_t group,
272 bool follow_symlinks) {
273 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY |
274 (follow_symlinks ? 0 : O_NOFOLLOW);
Christopher Ferrisbf0929f2017-04-05 12:09:17 -0700275 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
Dan Albertaac6b7c2015-03-16 10:08:46 -0700276 if (fd == -1) {
Elliott Hughes7d9a4792016-07-28 15:15:28 -0700277 PLOG(ERROR) << "android::WriteStringToFile open failed";
Dan Albertaac6b7c2015-03-16 10:08:46 -0700278 return false;
279 }
280
281 // We do an explicit fchmod here because we assume that the caller really
282 // meant what they said and doesn't want the umask-influenced mode.
283 if (fchmod(fd, mode) == -1) {
Elliott Hughes7d9a4792016-07-28 15:15:28 -0700284 PLOG(ERROR) << "android::WriteStringToFile fchmod failed";
Dan Albertaac6b7c2015-03-16 10:08:46 -0700285 return CleanUpAfterFailedWrite(path);
286 }
287 if (fchown(fd, owner, group) == -1) {
Elliott Hughes7d9a4792016-07-28 15:15:28 -0700288 PLOG(ERROR) << "android::WriteStringToFile fchown failed";
Dan Albertaac6b7c2015-03-16 10:08:46 -0700289 return CleanUpAfterFailedWrite(path);
290 }
291 if (!WriteStringToFd(content, fd)) {
Elliott Hughes7d9a4792016-07-28 15:15:28 -0700292 PLOG(ERROR) << "android::WriteStringToFile write failed";
Dan Albertaac6b7c2015-03-16 10:08:46 -0700293 return CleanUpAfterFailedWrite(path);
294 }
Dan Albertaac6b7c2015-03-16 10:08:46 -0700295 return true;
296}
297#endif
298
Josh Gao8e1f0d82016-09-14 16:11:45 -0700299bool WriteStringToFile(const std::string& content, const std::string& path,
300 bool follow_symlinks) {
301 int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY |
302 (follow_symlinks ? 0 : O_NOFOLLOW);
Elliott Hughes62ecbaa2017-05-15 17:31:15 -0700303 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, 0666)));
Dan Albertaac6b7c2015-03-16 10:08:46 -0700304 if (fd == -1) {
305 return false;
306 }
Christopher Ferrisbf0929f2017-04-05 12:09:17 -0700307 return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path);
Dan Albertaac6b7c2015-03-16 10:08:46 -0700308}
309
Josh Gaoe4401992019-04-25 14:04:57 -0700310bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) {
Elliott Hughes20abc872015-04-24 21:57:16 -0700311 uint8_t* p = reinterpret_cast<uint8_t*>(data);
312 size_t remaining = byte_count;
313 while (remaining > 0) {
Josh Gaoe4401992019-04-25 14:04:57 -0700314 ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
Elliott Hughes20abc872015-04-24 21:57:16 -0700315 if (n <= 0) return false;
316 p += n;
317 remaining -= n;
318 }
319 return true;
320}
321
Adam Lesinski6d6f9b32017-06-19 10:27:38 -0700322#if defined(_WIN32)
323// Windows implementation of pread. Note that this DOES move the file descriptors read position,
324// but it does so atomically.
Josh Gaoe4401992019-04-25 14:04:57 -0700325static ssize_t pread(borrowed_fd fd, void* data, size_t byte_count, off64_t offset) {
Adam Lesinski6d6f9b32017-06-19 10:27:38 -0700326 DWORD bytes_read;
327 OVERLAPPED overlapped;
328 memset(&overlapped, 0, sizeof(OVERLAPPED));
329 overlapped.Offset = static_cast<DWORD>(offset);
330 overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
Josh Gaoe4401992019-04-25 14:04:57 -0700331 if (!ReadFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), data,
332 static_cast<DWORD>(byte_count), &bytes_read, &overlapped)) {
Adam Lesinski6d6f9b32017-06-19 10:27:38 -0700333 // In case someone tries to read errno (since this is masquerading as a POSIX call)
334 errno = EIO;
335 return -1;
336 }
337 return static_cast<ssize_t>(bytes_read);
338}
339#endif
340
Josh Gaoe4401992019-04-25 14:04:57 -0700341bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset) {
Adam Lesinski6d6f9b32017-06-19 10:27:38 -0700342 uint8_t* p = reinterpret_cast<uint8_t*>(data);
343 while (byte_count > 0) {
Josh Gaoe4401992019-04-25 14:04:57 -0700344 ssize_t n = TEMP_FAILURE_RETRY(pread(fd.get(), p, byte_count, offset));
Adam Lesinski6d6f9b32017-06-19 10:27:38 -0700345 if (n <= 0) return false;
346 p += n;
347 byte_count -= n;
348 offset += n;
349 }
350 return true;
351}
352
Josh Gaoe4401992019-04-25 14:04:57 -0700353bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) {
Elliott Hughes20abc872015-04-24 21:57:16 -0700354 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
355 size_t remaining = byte_count;
356 while (remaining > 0) {
Josh Gaoe4401992019-04-25 14:04:57 -0700357 ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining));
Elliott Hughes20abc872015-04-24 21:57:16 -0700358 if (n == -1) return false;
359 p += n;
360 remaining -= n;
361 }
362 return true;
363}
364
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800365bool RemoveFileIfExists(const std::string& path, std::string* err) {
366 struct stat st;
367#if defined(_WIN32)
liwugang78cea682018-07-11 13:24:49 +0800368 // TODO: Windows version can't handle symbolic links correctly.
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800369 int result = stat(path.c_str(), &st);
370 bool file_type_removable = (result == 0 && S_ISREG(st.st_mode));
371#else
372 int result = lstat(path.c_str(), &st);
373 bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)));
374#endif
liwugang78cea682018-07-11 13:24:49 +0800375 if (result == -1) {
376 if (errno == ENOENT || errno == ENOTDIR) return true;
377 if (err != nullptr) *err = strerror(errno);
378 return false;
379 }
380
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800381 if (result == 0) {
382 if (!file_type_removable) {
383 if (err != nullptr) {
liwugang78cea682018-07-11 13:24:49 +0800384 *err = "is not a regular file or symbolic link";
Yabin Cui8f6a5a02016-01-29 17:25:54 -0800385 }
386 return false;
387 }
388 if (unlink(path.c_str()) == -1) {
389 if (err != nullptr) {
390 *err = strerror(errno);
391 }
392 return false;
393 }
394 }
395 return true;
396}
397
Elliott Hughesa634a9a2016-08-23 15:53:45 -0700398#if !defined(_WIN32)
399bool Readlink(const std::string& path, std::string* result) {
400 result->clear();
401
402 // Most Linux file systems (ext2 and ext4, say) limit symbolic links to
403 // 4095 bytes. Since we'll copy out into the string anyway, it doesn't
404 // waste memory to just start there. We add 1 so that we can recognize
405 // whether it actually fit (rather than being truncated to 4095).
406 std::vector<char> buf(4095 + 1);
407 while (true) {
408 ssize_t size = readlink(path.c_str(), &buf[0], buf.size());
409 // Unrecoverable error?
410 if (size == -1) return false;
411 // It fit! (If size == buf.size(), it may have been truncated.)
412 if (static_cast<size_t>(size) < buf.size()) {
413 result->assign(&buf[0], size);
414 return true;
415 }
416 // Double our buffer and try again.
417 buf.resize(buf.size() * 2);
418 }
419}
420#endif
421
Dimitry Ivanov4edc04f2016-09-09 10:49:21 -0700422#if !defined(_WIN32)
423bool Realpath(const std::string& path, std::string* result) {
424 result->clear();
425
Yifan Hong26fd2342019-03-21 15:20:53 -0700426 // realpath may exit with EINTR. Retry if so.
427 char* realpath_buf = nullptr;
428 do {
429 realpath_buf = realpath(path.c_str(), nullptr);
430 } while (realpath_buf == nullptr && errno == EINTR);
431
Dimitry Ivanov4edc04f2016-09-09 10:49:21 -0700432 if (realpath_buf == nullptr) {
433 return false;
434 }
435 result->assign(realpath_buf);
436 free(realpath_buf);
437 return true;
438}
439#endif
440
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700441std::string GetExecutablePath() {
442#if defined(__linux__)
443 std::string path;
444 android::base::Readlink("/proc/self/exe", &path);
445 return path;
446#elif defined(__APPLE__)
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700447 char path[PATH_MAX + 1];
Josh Gao58668ac2016-09-01 12:31:42 -0700448 uint32_t path_len = sizeof(path);
449 int rc = _NSGetExecutablePath(path, &path_len);
450 if (rc < 0) {
451 std::unique_ptr<char> path_buf(new char[path_len]);
452 _NSGetExecutablePath(path_buf.get(), &path_len);
453 return path_buf.get();
454 }
Elliott Hughes48f0eb52016-08-31 15:07:18 -0700455 return path;
456#elif defined(_WIN32)
457 char path[PATH_MAX + 1];
458 DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1);
459 if (result == 0 || result == sizeof(path) - 1) return "";
460 path[PATH_MAX - 1] = 0;
461 return path;
462#else
463#error unknown OS
464#endif
465}
466
Colin Cross2909be02017-02-23 17:41:56 -0800467std::string GetExecutableDirectory() {
468 return Dirname(GetExecutablePath());
469}
Colin Cross2e732e22017-02-23 21:23:05 -0800470
Colin Cross2909be02017-02-23 17:41:56 -0800471std::string Basename(const std::string& path) {
Colin Cross2e732e22017-02-23 21:23:05 -0800472 // Copy path because basename may modify the string passed in.
473 std::string result(path);
474
475#if !defined(__BIONIC__)
476 // Use lock because basename() may write to a process global and return a
477 // pointer to that. Note that this locking strategy only works if all other
478 // callers to basename in the process also grab this same lock, but its
479 // better than nothing. Bionic's basename returns a thread-local buffer.
480 static std::mutex& basename_lock = *new std::mutex();
481 std::lock_guard<std::mutex> lock(basename_lock);
482#endif
483
484 // Note that if std::string uses copy-on-write strings, &str[0] will cause
485 // the copy to be made, so there is no chance of us accidentally writing to
486 // the storage for 'path'.
487 char* name = basename(&result[0]);
488
489 // In case basename returned a pointer to a process global, copy that string
490 // before leaving the lock.
491 result.assign(name);
492
493 return result;
494}
495
496std::string Dirname(const std::string& path) {
497 // Copy path because dirname may modify the string passed in.
498 std::string result(path);
499
500#if !defined(__BIONIC__)
501 // Use lock because dirname() may write to a process global and return a
502 // pointer to that. Note that this locking strategy only works if all other
503 // callers to dirname in the process also grab this same lock, but its
504 // better than nothing. Bionic's dirname returns a thread-local buffer.
505 static std::mutex& dirname_lock = *new std::mutex();
506 std::lock_guard<std::mutex> lock(dirname_lock);
507#endif
508
509 // Note that if std::string uses copy-on-write strings, &str[0] will cause
510 // the copy to be made, so there is no chance of us accidentally writing to
511 // the storage for 'path'.
512 char* parent = dirname(&result[0]);
513
514 // In case dirname returned a pointer to a process global, copy that string
515 // before leaving the lock.
516 result.assign(parent);
517
518 return result;
519}
520
Dan Albertaac6b7c2015-03-16 10:08:46 -0700521} // namespace base
522} // namespace android