Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #define LOG_TAG "ReaddirHelper" |
| 17 | |
| 18 | #include "libfuse_jni/ReaddirHelper.h" |
| 19 | #include <android-base/logging.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | namespace mediaprovider { |
| 23 | namespace fuse { |
| 24 | namespace { |
| 25 | |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 26 | inline bool is_dot_or_dotdot(const char* name) { |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 27 | return name && name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')); |
| 28 | } |
| 29 | |
| 30 | } // namespace |
| 31 | |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 32 | bool isDirectory(const dirent& entry) { |
| 33 | if (entry.d_type == DT_DIR) return true; |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | void addDirectoryEntriesFromLowerFs(DIR* dirp, bool (*const filter)(const dirent&), |
| 38 | std::vector<std::shared_ptr<DirectoryEntry>>* directory_entries) { |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 39 | while (1) { |
| 40 | errno = 0; |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 41 | const struct dirent* entry = readdir(dirp); |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 42 | if (entry == nullptr) { |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 43 | if (errno) { |
| 44 | PLOG(ERROR) << "DEBUG: readdir(): readdir failed with %d" << errno; |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 45 | directory_entries->resize(0); |
Sahana Rao | 81ceaf0 | 2019-12-23 12:37:06 +0000 | [diff] [blame] | 46 | directory_entries->push_back(std::make_shared<DirectoryEntry>("", errno)); |
Sahana Rao | 7169344 | 2019-11-13 13:48:07 +0000 | [diff] [blame] | 47 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 48 | break; |
| 49 | } |
| 50 | // Ignore '.' & '..' to maintain consistency with directory entries |
| 51 | // returned by MediaProvider. |
| 52 | if (is_dot_or_dotdot(entry->d_name)) continue; |
Sahana Rao | 8a588e7 | 2019-12-06 11:32:56 +0000 | [diff] [blame] | 53 | if (filter == nullptr || filter(*entry)) { |
| 54 | directory_entries->push_back( |
| 55 | std::make_shared<DirectoryEntry>(entry->d_name, entry->d_type)); |
| 56 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 57 | } |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Sahana Rao | a82bd6a | 2019-10-10 18:10:37 +0100 | [diff] [blame] | 60 | } // namespace fuse |
| 61 | } // namespace mediaprovider |