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.
30 #include <android-base/file.h>
31 #include <android-base/logging.h>
33 void OneTimeFreeAllocator::Clear() {
42 const char* OneTimeFreeAllocator::AllocateString(const std::string& s) {
43 size_t size = s.size() + 1;
44 if (cur_ + size > end_) {
45 size_t alloc_size = std::max(size, unit_size_);
46 char* p = new char[alloc_size];
49 end_ = p + alloc_size;
51 strcpy(cur_, s.c_str());
52 const char* result = cur_;
58 FileHelper FileHelper::OpenReadOnly(const std::string& filename) {
59 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
60 return FileHelper(fd);
63 FileHelper FileHelper::OpenWriteOnly(const std::string& filename) {
64 int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
65 return FileHelper(fd);
68 FileHelper::~FileHelper() {
74 ArchiveHelper::ArchiveHelper(int fd, const std::string& debug_filename) : valid_(false) {
75 int rc = OpenArchiveFd(fd, "", &handle_, false);
79 LOG(ERROR) << "Failed to open archive " << debug_filename << ": " << ErrorCodeString(rc);
83 ArchiveHelper::~ArchiveHelper() {
85 CloseArchive(handle_);
89 void PrintIndented(size_t indent, const char* fmt, ...) {
92 printf("%*s", static_cast<int>(indent * 2), "");
97 bool IsPowerOfTwo(uint64_t value) {
98 return (value != 0 && ((value & (value - 1)) == 0));
101 void GetEntriesInDir(const std::string& dirpath, std::vector<std::string>* files,
102 std::vector<std::string>* subdirs) {
103 if (files != nullptr) {
106 if (subdirs != nullptr) {
109 DIR* dir = opendir(dirpath.c_str());
110 if (dir == nullptr) {
111 PLOG(DEBUG) << "can't open dir " << dirpath;
115 while ((entry = readdir(dir)) != nullptr) {
116 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
119 if (IsDir(dirpath + std::string("/") + entry->d_name)) {
120 if (subdirs != nullptr) {
121 subdirs->push_back(entry->d_name);
124 if (files != nullptr) {
125 files->push_back(entry->d_name);
132 bool IsDir(const std::string& dirpath) {
134 if (stat(dirpath.c_str(), &st) == 0) {
135 if (S_ISDIR(st.st_mode)) {
142 bool IsRegularFile(const std::string& filename) {
144 if (stat(filename.c_str(), &st) == 0) {
145 if (S_ISREG(st.st_mode)) {
152 uint64_t GetFileSize(const std::string& filename) {
154 if (stat(filename.c_str(), &st) == 0) {
155 return static_cast<uint64_t>(st.st_size);
160 bool MkdirWithParents(const std::string& path) {
162 while (prev_end < path.size()) {
163 size_t next_end = path.find('/', prev_end + 1);
164 if (next_end == std::string::npos) {
167 std::string dir_path = path.substr(0, next_end);
168 if (!IsDir(dir_path)) {
170 int ret = mkdir(dir_path.c_str());
172 int ret = mkdir(dir_path.c_str(), 0755);
175 PLOG(ERROR) << "failed to create dir " << dir_path;