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> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 18 | #include <sys/types.h> |
| 19 | #include <unistd.h> |
| 20 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 21 | #include <cerrno> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 22 | #include <fstream> |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | #include "idmap2/FileUtils.h" |
| 29 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 30 | namespace android::idmap2::utils { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 31 | |
| 32 | std::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 Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 35 | if (dir == nullptr) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 36 | return nullptr; |
| 37 | } |
| 38 | std::unique_ptr<std::vector<std::string>> vector(new std::vector<std::string>()); |
| 39 | struct dirent* dirent; |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 40 | while ((dirent = readdir(dir)) != nullptr) { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 41 | 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 | |
| 60 | std::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 | |
| 68 | std::unique_ptr<std::string> ReadFile(int fd) { |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 69 | static constexpr const size_t kBufSize = 1024; |
| 70 | |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 71 | std::unique_ptr<std::string> str(new std::string()); |
Mårten Kongstad | b877902 | 2018-11-29 09:53:17 +0100 | [diff] [blame] | 72 | char buf[kBufSize]; |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 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 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 80 | } // namespace android::idmap2::utils |