Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 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 "Utils.h" |
| 18 | |
| 19 | #include <err.h> |
| 20 | #include <fts.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | std::string getWorkingDir() { |
| 28 | char buf[PATH_MAX]; |
| 29 | if (!getcwd(buf, sizeof(buf))) { |
| 30 | err(1, "getcwd failed"); |
| 31 | } |
| 32 | return buf; |
| 33 | } |
| 34 | |
| 35 | std::vector<std::string> collectFiles(const std::string& directory) { |
| 36 | std::vector<std::string> files; |
| 37 | |
| 38 | char* dir_argv[2] = { const_cast<char*>(directory.c_str()), nullptr }; |
| 39 | FTS* fts = fts_open(dir_argv, FTS_LOGICAL | FTS_NOCHDIR, nullptr); |
| 40 | |
| 41 | if (!fts) { |
| 42 | err(1, "failed to open directory '%s'", directory.c_str()); |
| 43 | } |
| 44 | |
| 45 | FTSENT* ent; |
| 46 | while ((ent = fts_read(fts))) { |
| 47 | if (ent->fts_info & (FTS_D | FTS_DP)) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | files.push_back(ent->fts_path); |
| 52 | } |
| 53 | |
| 54 | fts_close(fts); |
| 55 | return files; |
| 56 | } |