summaryrefslogtreecommitdiff
path: root/simpleperf/utils.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2016-03-02 13:56:28 -0800
committerYabin Cui <yabinc@google.com>2016-03-02 18:21:14 -0800
commitbe7ec66eaa4f995bd9068637f7c7d5718173922c (patch)
tree3340b9e58baa518f75b60bfabac56532f25a1a26 /simpleperf/utils.cpp
parent5cfed7b49b1ae706265d4181b1d4f0dbb79491f5 (diff)
downloadextras-be7ec66eaa4f995bd9068637f7c7d5718173922c.tar.gz
simpleperf: support building cts test.
1. build cts test libraries. 2. change tests to use tmpfile instead of perf.data. 3. support extracting testdata from cts test file. Bug: 27387280 Change-Id: I7c5db77f3157d586d0c9beb446b247626e7cce36
Diffstat (limited to 'simpleperf/utils.cpp')
-rw-r--r--simpleperf/utils.cpp49
1 files changed, 46 insertions, 3 deletions
diff --git a/simpleperf/utils.cpp b/simpleperf/utils.cpp
index 2e68767a..99e1e982 100644
--- a/simpleperf/utils.cpp
+++ b/simpleperf/utils.cpp
@@ -54,11 +54,15 @@ const char* OneTimeFreeAllocator::AllocateString(const std::string& s) {
return result;
}
-FileHelper::FileHelper() : fd_(-1) {
+
+FileHelper FileHelper::OpenReadOnly(const std::string& filename) {
+ int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
+ return FileHelper(fd);
}
-FileHelper::FileHelper(const std::string& filename) {
- fd_ = TEMP_FAILURE_RETRY(open(filename.c_str(), O_RDONLY | O_BINARY));
+FileHelper FileHelper::OpenWriteOnly(const std::string& filename) {
+ int fd = TEMP_FAILURE_RETRY(open(filename.c_str(), O_WRONLY | O_BINARY | O_CREAT, 0644));
+ return FileHelper(fd);
}
FileHelper::~FileHelper() {
@@ -67,6 +71,21 @@ FileHelper::~FileHelper() {
}
}
+ArchiveHelper::ArchiveHelper(int fd, const std::string& debug_filename) : valid_(false) {
+ int rc = OpenArchiveFd(fd, "", &handle_, false);
+ if (rc == 0) {
+ valid_ = true;
+ } else {
+ LOG(ERROR) << "Failed to open archive " << debug_filename << ": " << ErrorCodeString(rc);
+ }
+}
+
+ArchiveHelper::~ArchiveHelper() {
+ if (valid_) {
+ CloseArchive(handle_);
+ }
+}
+
void PrintIndented(size_t indent, const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
@@ -137,3 +156,27 @@ uint64_t GetFileSize(const std::string& filename) {
}
return 0;
}
+
+bool MkdirWithParents(const std::string& path) {
+ size_t prev_end = 0;
+ while (prev_end < path.size()) {
+ size_t next_end = path.find('/', prev_end + 1);
+ if (next_end == std::string::npos) {
+ break;
+ }
+ std::string dir_path = path.substr(0, next_end);
+ if (!IsDir(dir_path)) {
+#if defined(_WIN32)
+ int ret = mkdir(dir_path.c_str());
+#else
+ int ret = mkdir(dir_path.c_str(), 0755);
+#endif
+ if (ret != 0) {
+ PLOG(ERROR) << "failed to create dir " << dir_path;
+ return false;
+ }
+ }
+ prev_end = next_end;
+ }
+ return true;
+}