blob: 0255727fc8c6cbebdd573a84d23a725803c8e3ca [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include <dirent.h>
Mårten Kongstad02751232018-04-27 13:16:32 +020018#include <sys/types.h>
19#include <unistd.h>
20
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010021#include <cerrno>
Mårten Kongstad02751232018-04-27 13:16:32 +020022#include <fstream>
23#include <memory>
24#include <string>
25#include <utility>
26#include <vector>
27
28#include "idmap2/FileUtils.h"
29
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010030namespace android::idmap2::utils {
Mårten Kongstad02751232018-04-27 13:16:32 +020031
32std::unique_ptr<std::vector<std::string>> FindFiles(const std::string& root, bool recurse,
33 const FindFilesPredicate& predicate) {
34 DIR* dir = opendir(root.c_str());
Mårten Kongstadb8779022018-11-29 09:53:17 +010035 if (dir == nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +020036 return nullptr;
37 }
38 std::unique_ptr<std::vector<std::string>> vector(new std::vector<std::string>());
39 struct dirent* dirent;
Mårten Kongstadb8779022018-11-29 09:53:17 +010040 while ((dirent = readdir(dir)) != nullptr) {
Mårten Kongstad02751232018-04-27 13:16:32 +020041 const std::string path = root + "/" + dirent->d_name;
42 if (predicate(dirent->d_type, path)) {
43 vector->push_back(path);
44 }
45 if (recurse && dirent->d_type == DT_DIR && strcmp(dirent->d_name, ".") != 0 &&
46 strcmp(dirent->d_name, "..") != 0) {
47 auto sub_vector = FindFiles(path, recurse, predicate);
48 if (!sub_vector) {
49 closedir(dir);
50 return nullptr;
51 }
52 vector->insert(vector->end(), sub_vector->begin(), sub_vector->end());
53 }
54 }
55 closedir(dir);
56
57 return vector;
58}
59
60std::unique_ptr<std::string> ReadFile(const std::string& path) {
61 std::unique_ptr<std::string> str(new std::string());
62 std::ifstream fin(path);
63 str->append({std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>()});
64 fin.close();
65 return str;
66}
67
68std::unique_ptr<std::string> ReadFile(int fd) {
Mårten Kongstadb8779022018-11-29 09:53:17 +010069 static constexpr const size_t kBufSize = 1024;
70
Mårten Kongstad02751232018-04-27 13:16:32 +020071 std::unique_ptr<std::string> str(new std::string());
Mårten Kongstadb8779022018-11-29 09:53:17 +010072 char buf[kBufSize];
Mårten Kongstad02751232018-04-27 13:16:32 +020073 ssize_t r;
74 while ((r = read(fd, buf, sizeof(buf))) > 0) {
75 str->append(buf, r);
76 }
77 return r == 0 ? std::move(str) : nullptr;
78}
79
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010080} // namespace android::idmap2::utils