MÃ¥rten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame^] | 1 | /* |
| 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> |
| 18 | #include <errno.h> |
| 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <fstream> |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "idmap2/FileUtils.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace idmap2 { |
| 32 | namespace utils { |
| 33 | |
| 34 | std::unique_ptr<std::vector<std::string>> FindFiles(const std::string& root, bool recurse, |
| 35 | const FindFilesPredicate& predicate) { |
| 36 | DIR* dir = opendir(root.c_str()); |
| 37 | if (!dir) { |
| 38 | return nullptr; |
| 39 | } |
| 40 | std::unique_ptr<std::vector<std::string>> vector(new std::vector<std::string>()); |
| 41 | struct dirent* dirent; |
| 42 | while ((dirent = readdir(dir))) { |
| 43 | const std::string path = root + "/" + dirent->d_name; |
| 44 | if (predicate(dirent->d_type, path)) { |
| 45 | vector->push_back(path); |
| 46 | } |
| 47 | if (recurse && dirent->d_type == DT_DIR && strcmp(dirent->d_name, ".") != 0 && |
| 48 | strcmp(dirent->d_name, "..") != 0) { |
| 49 | auto sub_vector = FindFiles(path, recurse, predicate); |
| 50 | if (!sub_vector) { |
| 51 | closedir(dir); |
| 52 | return nullptr; |
| 53 | } |
| 54 | vector->insert(vector->end(), sub_vector->begin(), sub_vector->end()); |
| 55 | } |
| 56 | } |
| 57 | closedir(dir); |
| 58 | |
| 59 | return vector; |
| 60 | } |
| 61 | |
| 62 | std::unique_ptr<std::string> ReadFile(const std::string& path) { |
| 63 | std::unique_ptr<std::string> str(new std::string()); |
| 64 | std::ifstream fin(path); |
| 65 | str->append({std::istreambuf_iterator<char>(fin), std::istreambuf_iterator<char>()}); |
| 66 | fin.close(); |
| 67 | return str; |
| 68 | } |
| 69 | |
| 70 | std::unique_ptr<std::string> ReadFile(int fd) { |
| 71 | std::unique_ptr<std::string> str(new std::string()); |
| 72 | char buf[1024]; |
| 73 | 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 | |
| 80 | } // namespace utils |
| 81 | } // namespace idmap2 |
| 82 | } // namespace android |