2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 #include <android-base/file.h>
32 #include <android-base/logging.h>
34 void OneTimeFreeAllocator::Clear() {
43 const char* OneTimeFreeAllocator::AllocateString(const std::string& s) {
44 size_t size = s.size() + 1;
45 if (cur_ + size > end_) {
46 size_t alloc_size = std::max(size, unit_size_);
47 char* p = new char[alloc_size];
50 end_ = p + alloc_size;
52 strcpy(cur_, s.c_str());
53 const char* result = cur_;
59 FileHelper FileHelper::OpenReadOnly(const std::string& filename) {
60 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
61 return FileHelper(fd);
64 FileHelper FileHelper::OpenWriteOnly(const std::string& filename) {
65 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
66 return FileHelper(fd);
69 FileHelper::~FileHelper() {
75 ArchiveHelper::ArchiveHelper(int fd, const std::string& debug_filename) : valid_(false) {
76 int rc = OpenArchiveFd(fd, "", &handle_, false);
80 LOG(ERROR) << "Failed to open archive " << debug_filename << ": " << ErrorCodeString(rc);
84 ArchiveHelper::~ArchiveHelper() {
86 CloseArchive(handle_);
90 void PrintIndented(size_t indent, const char* fmt, ...) {
93 printf("%*s", static_cast<int>(indent * 2), "");
98 bool IsPowerOfTwo(uint64_t value) {
99 return (value != 0 && ((value & (value - 1)) == 0));
102 void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
103 std::vector<std::string>* subdirs) {
104 if (files != nullptr) {
107 if (subdirs != nullptr) {
110 DIR* dir = opendir(dirpath.c_str());
111 if (dir == nullptr) {
112 PLOG(DEBUG) << "can't open dir " << dirpath;
116 while ((entry = readdir(dir)) != nullptr) {
117 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
120 if (IsDir(dirpath + std::string("/") + entry->d_name)) {
121 if (subdirs != nullptr) {
122 subdirs->push_back(entry->d_name);
125 if (files != nullptr) {
126 files->push_back(entry->d_name);
133 bool IsDir(const std::string& dirpath) {
135 if (stat(dirpath.c_str(), &st) == 0) {
136 if (S_ISDIR(st.st_mode)) {
143 bool IsRegularFile(const std::string& filename) {
145 if (stat(filename.c_str(), &st) == 0) {
146 if (S_ISREG(st.st_mode)) {
153 uint64_t GetFileSize(const std::string& filename) {
155 if (stat(filename.c_str(), &st) == 0) {
156 return static_cast<uint64_t>(st.st_size);
161 bool MkdirWithParents(const std::string& path) {
163 while (prev_end < path.size()) {
164 size_t next_end = path.find('/', prev_end + 1);
165 if (next_end == std::string::npos) {
168 std::string dir_path = path.substr(0, next_end);
169 if (!IsDir(dir_path)) {
171 int ret = mkdir(dir_path.c_str());
173 int ret = mkdir(dir_path.c_str(), 0755);
176 PLOG(ERROR) << "failed to create dir " << dir_path;
185 bool GetLogSeverity(const std::string& name, android::base::LogSeverity* severity) {
186 static std::map<std::string, android::base::LogSeverity> log_severity_map = {
187 {"verbose", android::base::VERBOSE},
188 {"debug", android::base::DEBUG},
189 {"warning", android::base::WARNING},
190 {"error", android::base::ERROR},
191 {"fatal", android::base::FATAL},
193 auto it = log_severity_map.find(name);
194 if (it != log_severity_map.end()) {
195 *severity = it->second;
202 static int is_root = -1;
204 #if defined(__linux__)
205 is_root = (getuid() == 0) ? 1 : 0;