1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "android-base/file.h"
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif
#if defined(_WIN32)
#include <direct.h>
#include <windows.h>
#define O_NOFOLLOW 0
#define OS_PATH_SEPARATOR '\\'
#else
#define OS_PATH_SEPARATOR '/'
#endif
#include "android-base/logging.h" // and must be after windows.h for ERROR
#include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
#include "android-base/unique_fd.h"
#include "android-base/utf8.h"
namespace {
#ifdef _WIN32
static int mkstemp(char* name_template, size_t size_in_chars) {
std::wstring path;
CHECK(android::base::UTF8ToWide(name_template, &path))
<< "path can't be converted to wchar: " << name_template;
if (_wmktemp_s(path.data(), path.size() + 1) != 0) {
return -1;
}
// Use open() to match the close() that TemporaryFile's destructor does.
// Use O_BINARY to match base file APIs.
int fd = _wopen(path.c_str(), O_CREAT | O_EXCL | O_RDWR | O_BINARY, S_IRUSR | S_IWUSR);
if (fd < 0) {
return -1;
}
std::string path_utf8;
CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8";
CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0)
<< "utf8 path can't be assigned back to name_template";
return fd;
}
static char* mkdtemp(char* name_template, size_t size_in_chars) {
std::wstring path;
CHECK(android::base::UTF8ToWide(name_template, &path))
<< "path can't be converted to wchar: " << name_template;
if (_wmktemp_s(path.data(), path.size() + 1) != 0) {
return nullptr;
}
if (_wmkdir(path.c_str()) != 0) {
return nullptr;
}
std::string path_utf8;
CHECK(android::base::WideToUTF8(path, &path_utf8)) << "path can't be converted to utf8";
CHECK(strcpy_s(name_template, size_in_chars, path_utf8.c_str()) == 0)
<< "utf8 path can't be assigned back to name_template";
return name_template;
}
#endif
std::string GetSystemTempDir() {
#if defined(__ANDROID__)
const auto* tmpdir = getenv("TMPDIR");
if (tmpdir == nullptr) tmpdir = "/data/local/tmp";
if (access(tmpdir, R_OK | W_OK | X_OK) == 0) {
return tmpdir;
}
// Tests running in app context can't access /data/local/tmp,
// so try current directory if /data/local/tmp is not accessible.
return ".";
#elif defined(_WIN32)
wchar_t tmp_dir_w[MAX_PATH];
DWORD result = GetTempPathW(std::size(tmp_dir_w), tmp_dir_w); // checks TMP env
CHECK_NE(result, 0ul) << "GetTempPathW failed, error: " << GetLastError();
CHECK_LT(result, std::size(tmp_dir_w)) << "path truncated to: " << result;
// GetTempPath() returns a path with a trailing slash, but init()
// does not expect that, so remove it.
if (tmp_dir_w[result - 1] == L'\\') {
tmp_dir_w[result - 1] = L'\0';
}
std::string tmp_dir;
CHECK(android::base::WideToUTF8(tmp_dir_w, &tmp_dir)) << "path can't be converted to utf8";
return tmp_dir;
#else
const auto* tmpdir = getenv("TMPDIR");
if (tmpdir == nullptr) tmpdir = "/tmp";
return tmpdir;
#endif
}
} // namespace
TemporaryFile::TemporaryFile() {
init(GetSystemTempDir());
}
TemporaryFile::TemporaryFile(const std::string& tmp_dir) {
init(tmp_dir);
}
TemporaryFile::~TemporaryFile() {
if (fd != -1) {
close(fd);
}
if (remove_file_) {
unlink(path);
}
}
int TemporaryFile::release() {
int result = fd;
fd = -1;
return result;
}
void TemporaryFile::init(const std::string& tmp_dir) {
snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR);
#if defined(_WIN32)
fd = mkstemp(path, sizeof(path));
#else
fd = mkstemp(path);
#endif
}
TemporaryDir::TemporaryDir() {
init(GetSystemTempDir());
}
TemporaryDir::~TemporaryDir() {
if (!remove_dir_and_contents_) return;
auto callback = [](const char* child, const struct stat*, int file_type, struct FTW*) -> int {
switch (file_type) {
case FTW_D:
case FTW_DP:
case FTW_DNR:
if (rmdir(child) == -1) {
PLOG(ERROR) << "rmdir " << child;
}
break;
case FTW_NS:
default:
if (rmdir(child) != -1) break;
// FALLTHRU (for gcc, lint, pcc, etc; and following for clang)
FALLTHROUGH_INTENDED;
case FTW_F:
case FTW_SL:
case FTW_SLN:
if (unlink(child) == -1) {
PLOG(ERROR) << "unlink " << child;
}
break;
}
return 0;
};
nftw(path, callback, 128, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
}
bool TemporaryDir::init(const std::string& tmp_dir) {
snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR);
#if defined(_WIN32)
return (mkdtemp(path, sizeof(path)) != nullptr);
#else
return (mkdtemp(path) != nullptr);
#endif
}
namespace android {
namespace base {
// Versions of standard library APIs that support UTF-8 strings.
using namespace android::base::utf8;
bool ReadFdToString(borrowed_fd fd, std::string* content) {
content->clear();
// Although original we had small files in mind, this code gets used for
// very large files too, where the std::string growth heuristics might not
// be suitable. https://code.google.com/p/android/issues/detail?id=258500.
struct stat sb;
if (fstat(fd.get(), &sb) != -1 && sb.st_size > 0 && sb.st_size <= SSIZE_MAX) {
// Shrink the string capacity to fit the file, but if the capacity is only
// slightly larger than needed, avoid reallocating. std::string::reserve no
// longer lowers capacity after P0966R1, but it does round the request up a
// small amount (e.g. 8 or 16 bytes).
size_t fd_size = sb.st_size;
if (fd_size > content->capacity()) {
content->reserve(fd_size);
} else if (fd_size < content->capacity() && content->capacity() - fd_size >= 64) {
content->shrink_to_fit();
content->reserve(fd_size);
}
}
char buf[4096] __attribute__((__uninitialized__));
ssize_t n;
while ((n = TEMP_FAILURE_RETRY(read(fd.get(), &buf[0], sizeof(buf)))) > 0) {
content->append(buf, n);
}
return (n == 0) ? true : false;
}
bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) {
content->clear();
int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags)));
if (fd == -1) {
return false;
}
return ReadFdToString(fd, content);
}
bool WriteStringToFd(std::string_view content, borrowed_fd fd) {
const char* p = content.data();
size_t left = content.size();
while (left > 0) {
ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, left));
if (n == -1) {
return false;
}
p += n;
left -= n;
}
return true;
}
static bool CleanUpAfterFailedWrite(const std::string& path) {
// Something went wrong. Let's not leave a corrupt file lying around.
int saved_errno = errno;
unlink(path.c_str());
errno = saved_errno;
return false;
}
#if !defined(_WIN32)
bool WriteStringToFile(const std::string& content, const std::string& path,
mode_t mode, uid_t owner, gid_t group,
bool follow_symlinks) {
int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY |
(follow_symlinks ? 0 : O_NOFOLLOW);
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
if (fd == -1) {
PLOG(ERROR) << "android::WriteStringToFile open failed";
return false;
}
// We do an explicit fchmod here because we assume that the caller really
// meant what they said and doesn't want the umask-influenced mode.
if (fchmod(fd, mode) == -1) {
PLOG(ERROR) << "android::WriteStringToFile fchmod failed";
return CleanUpAfterFailedWrite(path);
}
if (fchown(fd, owner, group) == -1) {
PLOG(ERROR) << "android::WriteStringToFile fchown failed";
return CleanUpAfterFailedWrite(path);
}
if (!WriteStringToFd(content, fd)) {
PLOG(ERROR) << "android::WriteStringToFile write failed";
return CleanUpAfterFailedWrite(path);
}
return true;
}
#endif
bool WriteStringToFile(const std::string& content, const std::string& path,
bool follow_symlinks) {
int flags = O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY |
(follow_symlinks ? 0 : O_NOFOLLOW);
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, 0666)));
if (fd == -1) {
return false;
}
return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path);
}
bool ReadFully(borrowed_fd fd, void* data, size_t byte_count) {
uint8_t* p = reinterpret_cast<uint8_t*>(data);
size_t remaining = byte_count;
while (remaining > 0) {
ssize_t n = TEMP_FAILURE_RETRY(read(fd.get(), p, remaining));
if (n == 0) { // EOF
errno = ENODATA;
return false;
}
if (n == -1) return false;
p += n;
remaining -= n;
}
return true;
}
#if defined(_WIN32)
// Windows implementation of pread. Note that this DOES move the file descriptors read position,
// but it does so atomically.
static ssize_t pread(borrowed_fd fd, void* data, size_t byte_count, off64_t offset) {
DWORD bytes_read;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
overlapped.Offset = static_cast<DWORD>(offset);
overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
if (!ReadFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), data,
static_cast<DWORD>(byte_count), &bytes_read, &overlapped)) {
// In case someone tries to read errno (since this is masquerading as a POSIX call)
errno = EIO;
return -1;
}
return static_cast<ssize_t>(bytes_read);
}
static ssize_t pwrite(borrowed_fd fd, const void* data, size_t byte_count, off64_t offset) {
DWORD bytes_written;
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
overlapped.Offset = static_cast<DWORD>(offset);
overlapped.OffsetHigh = static_cast<DWORD>(offset >> 32);
if (!WriteFile(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), data,
static_cast<DWORD>(byte_count), &bytes_written, &overlapped)) {
// In case someone tries to read errno (since this is masquerading as a POSIX call)
errno = EIO;
return -1;
}
return static_cast<ssize_t>(bytes_written);
}
#endif
bool ReadFullyAtOffset(borrowed_fd fd, void* data, size_t byte_count, off64_t offset) {
uint8_t* p = reinterpret_cast<uint8_t*>(data);
while (byte_count > 0) {
ssize_t n = TEMP_FAILURE_RETRY(pread(fd.get(), p, byte_count, offset));
if (n == 0) { // EOF
errno = ENODATA;
return false;
}
if (n == -1) return false;
p += n;
byte_count -= n;
offset += n;
}
return true;
}
bool WriteFullyAtOffset(borrowed_fd fd, const void* data, size_t byte_count, off64_t offset) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
size_t remaining = byte_count;
while (remaining > 0) {
ssize_t n = TEMP_FAILURE_RETRY(pwrite(fd.get(), p, remaining, offset));
if (n == -1) return false;
p += n;
remaining -= n;
offset += n;
}
return true;
}
bool WriteFully(borrowed_fd fd, const void* data, size_t byte_count) {
const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
size_t remaining = byte_count;
while (remaining > 0) {
ssize_t n = TEMP_FAILURE_RETRY(write(fd.get(), p, remaining));
if (n == -1) return false;
p += n;
remaining -= n;
}
return true;
}
bool RemoveFileIfExists(const std::string& path, std::string* err) {
struct stat st;
#if defined(_WIN32)
// TODO: Windows version can't handle symbolic links 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 == -1) {
if (errno == ENOENT || errno == ENOTDIR) return true;
if (err != nullptr) *err = strerror(errno);
return false;
}
if (result == 0) {
if (!file_type_removable) {
if (err != nullptr) {
*err = "is not a regular file or symbolic link";
}
return false;
}
if (unlink(path.c_str()) == -1) {
if (err != nullptr) {
*err = strerror(errno);
}
return false;
}
}
return true;
}
#if !defined(_WIN32)
bool Readlink(const std::string& path, std::string* result) {
result->clear();
// Most Linux file systems (ext2 and ext4, say) limit symbolic links to
// 4095 bytes. Since we'll copy out into the string anyway, it doesn't
// waste memory to just start there. We add 1 so that we can recognize
// whether it actually fit (rather than being truncated to 4095).
std::vector<char> buf(4095 + 1);
while (true) {
ssize_t size = readlink(path.c_str(), &buf[0], buf.size());
// Unrecoverable error?
if (size == -1) return false;
// It fit! (If size == buf.size(), it may have been truncated.)
if (static_cast<size_t>(size) < buf.size()) {
result->assign(&buf[0], size);
return true;
}
// Double our buffer and try again.
buf.resize(buf.size() * 2);
}
}
#endif
#if !defined(_WIN32)
bool Realpath(const std::string& path, std::string* result) {
result->clear();
// realpath may exit with EINTR. Retry if so.
char* realpath_buf = nullptr;
do {
realpath_buf = realpath(path.c_str(), nullptr);
} while (realpath_buf == nullptr && errno == EINTR);
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;
android::base::Readlink("/proc/self/exe", &path);
return path;
#elif defined(__APPLE__)
char path[PATH_MAX + 1];
uint32_t path_len = sizeof(path);
int rc = _NSGetExecutablePath(path, &path_len);
if (rc < 0) {
std::unique_ptr<char> path_buf(new char[path_len]);
_NSGetExecutablePath(path_buf.get(), &path_len);
return path_buf.get();
}
return path;
#elif defined(_WIN32)
char path[PATH_MAX + 1];
DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1);
if (result == 0 || result == sizeof(path) - 1) return "";
path[PATH_MAX - 1] = 0;
return path;
#elif defined(__EMSCRIPTEN__)
abort();
#else
#include <stdexcept>
throw std::runtime_error(std::string("Unknown OS!"));
#endif
}
std::string GetExecutableDirectory() {
return Dirname(GetExecutablePath());
}
#if defined(_WIN32)
std::string Basename(std::string_view path) {
// TODO: how much of this is actually necessary for mingw?
// Copy path because basename may modify the string passed in.
std::string result(path);
// Use lock because basename() may write to a process global and return a
// pointer to that. Note that this locking strategy only works if all other
// callers to basename in the process also grab this same lock, but its
// better than nothing. Bionic's basename returns a thread-local buffer.
static std::mutex& basename_lock = *new std::mutex();
std::lock_guard<std::mutex> lock(basename_lock);
// Note that if std::string uses copy-on-write strings, &str[0] will cause
// the copy to be made, so there is no chance of us accidentally writing to
// the storage for 'path'.
char* name = basename(&result[0]);
// In case basename returned a pointer to a process global, copy that string
// before leaving the lock.
result.assign(name);
return result;
}
#else
// Copied from bionic so that Basename() below can be portable and thread-safe.
static int _basename_r(const char* path, size_t path_size, char* buffer, size_t buffer_size) {
const char* startp = nullptr;
const char* endp = nullptr;
int len;
int result;
// Empty or NULL string gets treated as ".".
if (path == nullptr || path_size == 0) {
startp = ".";
len = 1;
goto Exit;
}
// Strip trailing slashes.
endp = path + path_size - 1;
while (endp > path && *endp == '/') {
endp--;
}
// All slashes becomes "/".
if (endp == path && *endp == '/') {
startp = "/";
len = 1;
goto Exit;
}
// Find the start of the base.
startp = endp;
while (startp > path && *(startp - 1) != '/') {
startp--;
}
len = endp - startp +1;
Exit:
result = len;
if (buffer == nullptr) {
return result;
}
if (len > static_cast<int>(buffer_size) - 1) {
len = buffer_size - 1;
result = -1;
errno = ERANGE;
}
if (len >= 0) {
memcpy(buffer, startp, len);
buffer[len] = 0;
}
return result;
}
std::string Basename(std::string_view path) {
char buf[PATH_MAX] __attribute__((__uninitialized__));
const auto size = _basename_r(path.data(), path.size(), buf, sizeof(buf));
return size > 0 ? std::string(buf, size) : std::string();
}
#endif
#if defined(_WIN32)
std::string Dirname(std::string_view path) {
// TODO: how much of this is actually necessary for mingw?
// Copy path because dirname may modify the string passed in.
std::string result(path);
// Use lock because dirname() may write to a process global and return a
// pointer to that. Note that this locking strategy only works if all other
// callers to dirname in the process also grab this same lock, but its
// better than nothing. Bionic's dirname returns a thread-local buffer.
static std::mutex& dirname_lock = *new std::mutex();
std::lock_guard<std::mutex> lock(dirname_lock);
// Note that if std::string uses copy-on-write strings, &str[0] will cause
// the copy to be made, so there is no chance of us accidentally writing to
// the storage for 'path'.
char* parent = dirname(&result[0]);
// In case dirname returned a pointer to a process global, copy that string
// before leaving the lock.
result.assign(parent);
return result;
}
#else
// Copied from bionic so that Dirname() below can be portable and thread-safe.
static int _dirname_r(const char* path, size_t path_size, char* buffer, size_t buffer_size) {
const char* endp = nullptr;
int len;
int result;
// Empty or NULL string gets treated as ".".
if (path == nullptr || path_size == 0) {
path = ".";
len = 1;
goto Exit;
}
// Strip trailing slashes.
endp = path + path_size - 1;
while (endp > path && *endp == '/') {
endp--;
}
// Find the start of the dir.
while (endp > path && *endp != '/') {
endp--;
}
// Either the dir is "/" or there are no slashes.
if (endp == path) {
path = (*endp == '/') ? "/" : ".";
len = 1;
goto Exit;
}
do {
endp--;
} while (endp > path && *endp == '/');
len = endp - path + 1;
Exit:
result = len;
if (len + 1 > MAXPATHLEN) {
errno = ENAMETOOLONG;
return -1;
}
if (buffer == nullptr) {
return result;
}
if (len > static_cast<int>(buffer_size) - 1) {
len = buffer_size - 1;
result = -1;
errno = ERANGE;
}
if (len >= 0) {
memcpy(buffer, path, len);
buffer[len] = 0;
}
return result;
}
std::string Dirname(std::string_view path) {
char buf[PATH_MAX] __attribute__((__uninitialized__));
const auto size = _dirname_r(path.data(), path.size(), buf, sizeof(buf));
return size > 0 ? std::string(buf, size) : std::string();
}
#endif
} // namespace base
} // namespace android
|