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
is_dot_or_dotdot(const char * name)26 inline bool is_dot_or_dotdot(const char* name) {
27 return name && name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
28 }
29
30 } // namespace
31
isDirectory(const dirent & entry)32 bool isDirectory(const dirent& entry) {
33 if (entry.d_type == DT_DIR) return true;
34 return false;
35 }
36
addDirectoryEntriesFromLowerFs(DIR * dirp,bool (* const filter)(const dirent &),std::vector<std::shared_ptr<DirectoryEntry>> * directory_entries)37 void addDirectoryEntriesFromLowerFs(DIR* dirp, bool (*const filter)(const dirent&),
38 std::vector<std::shared_ptr<DirectoryEntry>>* directory_entries) {
39 while (1) {
40 errno = 0;
41 const struct dirent* entry = readdir(dirp);
42 if (entry == nullptr) {
43 if (errno) {
44 PLOG(ERROR) << "DEBUG: readdir(): readdir failed with %d" << errno;
45 directory_entries->resize(0);
46 directory_entries->push_back(std::make_shared<DirectoryEntry>("", errno));
47 }
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;
53 if (filter == nullptr || filter(*entry)) {
54 directory_entries->push_back(
55 std::make_shared<DirectoryEntry>(entry->d_name, entry->d_type));
56 }
57 }
58 }
59
60 } // namespace fuse
61 } // namespace mediaprovider
62