2015-02-03 09:31:27 +08:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-12-05 14:00:26 +08:00
|
|
|
#include "android-base/file.h"
|
2015-02-03 09:31:27 +08:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-02-24 13:23:05 +08:00
|
|
|
#include <libgen.h>
|
2015-02-03 09:31:27 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2016-08-24 06:53:45 +08:00
|
|
|
#include <unistd.h>
|
2015-02-03 09:31:27 +08:00
|
|
|
|
2016-09-02 03:31:42 +08:00
|
|
|
#include <memory>
|
2017-02-24 13:23:05 +08:00
|
|
|
#include <mutex>
|
2015-03-17 01:08:46 +08:00
|
|
|
#include <string>
|
2016-08-24 06:53:45 +08:00
|
|
|
#include <vector>
|
2015-03-17 01:08:46 +08:00
|
|
|
|
2016-07-29 06:15:28 +08:00
|
|
|
#include "android-base/logging.h"
|
2017-04-06 03:09:17 +08:00
|
|
|
#include "android-base/macros.h" // For TEMP_FAILURE_RETRY on Darwin.
|
|
|
|
#include "android-base/unique_fd.h"
|
2015-12-05 14:00:26 +08:00
|
|
|
#include "android-base/utf8.h"
|
2015-02-04 05:02:57 +08:00
|
|
|
|
2016-09-01 06:07:18 +08:00
|
|
|
#if defined(__APPLE__)
|
2016-09-02 03:31:42 +08:00
|
|
|
#include <mach-o/dyld.h>
|
2016-09-01 06:07:18 +08:00
|
|
|
#endif
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
2017-05-16 08:31:15 +08:00
|
|
|
#define O_CLOEXEC O_NOINHERIT
|
|
|
|
#define O_NOFOLLOW 0
|
2016-09-01 06:07:18 +08:00
|
|
|
#endif
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
namespace android {
|
|
|
|
namespace base {
|
|
|
|
|
2015-11-12 02:02:29 +08:00
|
|
|
// Versions of standard library APIs that support UTF-8 strings.
|
|
|
|
using namespace android::base::utf8;
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
bool ReadFdToString(int fd, std::string* content) {
|
2015-02-07 04:19:48 +08:00
|
|
|
content->clear();
|
|
|
|
|
2017-03-21 10:16:18 +08:00
|
|
|
// 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, &sb) != -1 && sb.st_size > 0) {
|
|
|
|
content->reserve(sb.st_size);
|
|
|
|
}
|
|
|
|
|
2015-02-07 04:19:48 +08:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
ssize_t n;
|
|
|
|
while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
|
|
|
|
content->append(buf, n);
|
|
|
|
}
|
|
|
|
return (n == 0) ? true : false;
|
|
|
|
}
|
|
|
|
|
2016-09-15 07:11:45 +08:00
|
|
|
bool ReadFileToString(const std::string& path, std::string* content, bool follow_symlinks) {
|
2015-02-03 09:31:27 +08:00
|
|
|
content->clear();
|
|
|
|
|
2016-09-15 07:11:45 +08:00
|
|
|
int flags = O_RDONLY | O_CLOEXEC | O_BINARY | (follow_symlinks ? 0 : O_NOFOLLOW);
|
2017-04-06 03:09:17 +08:00
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags)));
|
2015-02-03 09:31:27 +08:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-06 03:09:17 +08:00
|
|
|
return ReadFdToString(fd, content);
|
2015-02-03 09:31:27 +08:00
|
|
|
}
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
bool WriteStringToFd(const std::string& content, int fd) {
|
2015-02-03 09:31:27 +08:00
|
|
|
const char* p = content.data();
|
|
|
|
size_t left = content.size();
|
|
|
|
while (left > 0) {
|
|
|
|
ssize_t n = TEMP_FAILURE_RETRY(write(fd, p, left));
|
|
|
|
if (n == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
p += n;
|
|
|
|
left -= n;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-05 05:19:13 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-05 11:38:28 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-17 01:08:46 +08:00
|
|
|
bool WriteStringToFile(const std::string& content, const std::string& path,
|
2016-09-15 07:11:45 +08:00
|
|
|
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);
|
2017-04-06 03:09:17 +08:00
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, mode)));
|
2015-02-05 05:19:13 +08:00
|
|
|
if (fd == -1) {
|
2016-07-29 06:15:28 +08:00
|
|
|
PLOG(ERROR) << "android::WriteStringToFile open failed";
|
2015-02-05 05:19:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-07 04:19:48 +08:00
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
// 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.
|
2015-02-18 02:16:04 +08:00
|
|
|
if (fchmod(fd, mode) == -1) {
|
2016-07-29 06:15:28 +08:00
|
|
|
PLOG(ERROR) << "android::WriteStringToFile fchmod failed";
|
2015-02-18 02:16:04 +08:00
|
|
|
return CleanUpAfterFailedWrite(path);
|
|
|
|
}
|
|
|
|
if (fchown(fd, owner, group) == -1) {
|
2016-07-29 06:15:28 +08:00
|
|
|
PLOG(ERROR) << "android::WriteStringToFile fchown failed";
|
2015-02-18 02:16:04 +08:00
|
|
|
return CleanUpAfterFailedWrite(path);
|
|
|
|
}
|
|
|
|
if (!WriteStringToFd(content, fd)) {
|
2016-07-29 06:15:28 +08:00
|
|
|
PLOG(ERROR) << "android::WriteStringToFile write failed";
|
2015-02-18 02:16:04 +08:00
|
|
|
return CleanUpAfterFailedWrite(path);
|
|
|
|
}
|
|
|
|
return true;
|
2015-02-05 05:19:13 +08:00
|
|
|
}
|
2015-02-05 11:38:28 +08:00
|
|
|
#endif
|
2015-02-05 05:19:13 +08:00
|
|
|
|
2016-09-15 07:11:45 +08:00
|
|
|
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);
|
2017-05-16 08:31:15 +08:00
|
|
|
android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), flags, 0666)));
|
2015-02-05 05:19:13 +08:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-06 03:09:17 +08:00
|
|
|
return WriteStringToFd(content, fd) || CleanUpAfterFailedWrite(path);
|
2015-02-05 05:19:13 +08:00
|
|
|
}
|
2015-03-17 01:08:46 +08:00
|
|
|
|
2015-04-25 12:57:16 +08:00
|
|
|
bool ReadFully(int 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, p, remaining));
|
|
|
|
if (n <= 0) return false;
|
|
|
|
p += n;
|
|
|
|
remaining -= n;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteFully(int 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, p, remaining));
|
|
|
|
if (n == -1) return false;
|
|
|
|
p += n;
|
|
|
|
remaining -= n;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-30 09:25:54 +08:00
|
|
|
bool RemoveFileIfExists(const std::string& path, std::string* err) {
|
|
|
|
struct stat st;
|
|
|
|
#if defined(_WIN32)
|
|
|
|
//TODO: Windows version can't handle symbol link 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 == 0) {
|
|
|
|
if (!file_type_removable) {
|
|
|
|
if (err != nullptr) {
|
|
|
|
*err = "is not a regular or symbol link file";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (unlink(path.c_str()) == -1) {
|
|
|
|
if (err != nullptr) {
|
|
|
|
*err = strerror(errno);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-24 06:53:45 +08:00
|
|
|
#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
|
|
|
|
|
2016-09-10 01:49:21 +08:00
|
|
|
#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
|
|
|
|
|
2016-09-01 06:07:18 +08:00
|
|
|
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];
|
2016-09-02 03:31:42 +08:00
|
|
|
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();
|
|
|
|
}
|
2016-09-01 06:07:18 +08:00
|
|
|
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;
|
|
|
|
#else
|
|
|
|
#error unknown OS
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-02-24 09:41:56 +08:00
|
|
|
std::string GetExecutableDirectory() {
|
|
|
|
return Dirname(GetExecutablePath());
|
|
|
|
}
|
2017-02-24 13:23:05 +08:00
|
|
|
|
2017-02-24 09:41:56 +08:00
|
|
|
std::string Basename(const std::string& path) {
|
2017-02-24 13:23:05 +08:00
|
|
|
// Copy path because basename may modify the string passed in.
|
|
|
|
std::string result(path);
|
|
|
|
|
|
|
|
#if !defined(__BIONIC__)
|
|
|
|
// 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);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Dirname(const std::string& path) {
|
|
|
|
// Copy path because dirname may modify the string passed in.
|
|
|
|
std::string result(path);
|
|
|
|
|
|
|
|
#if !defined(__BIONIC__)
|
|
|
|
// 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);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-03-17 01:08:46 +08:00
|
|
|
} // namespace base
|
|
|
|
} // namespace android
|