blob: 1fd1e7bf19af74614180bb9a9dc8894fbd60f26c [file] [log] [blame]
Sahana Raoa82bd6a2019-10-10 18:10:37 +01001/*
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
22namespace mediaprovider {
23namespace fuse {
24namespace {
25
Sahana Rao8a588e72019-12-06 11:32:56 +000026inline bool is_dot_or_dotdot(const char* name) {
Sahana Raoa82bd6a2019-10-10 18:10:37 +010027 return name && name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'));
28}
29
30} // namespace
31
Sahana Rao8a588e72019-12-06 11:32:56 +000032bool isDirectory(const dirent& entry) {
33 if (entry.d_type == DT_DIR) return true;
34 return false;
35}
36
37void addDirectoryEntriesFromLowerFs(DIR* dirp, bool (*const filter)(const dirent&),
38 std::vector<std::shared_ptr<DirectoryEntry>>* directory_entries) {
Sahana Raoa82bd6a2019-10-10 18:10:37 +010039 while (1) {
40 errno = 0;
Sahana Rao8a588e72019-12-06 11:32:56 +000041 const struct dirent* entry = readdir(dirp);
Sahana Raoa82bd6a2019-10-10 18:10:37 +010042 if (entry == nullptr) {
Sahana Rao71693442019-11-13 13:48:07 +000043 if (errno) {
44 PLOG(ERROR) << "DEBUG: readdir(): readdir failed with %d" << errno;
Sahana Rao8a588e72019-12-06 11:32:56 +000045 directory_entries->resize(0);
Sahana Rao81ceaf02019-12-23 12:37:06 +000046 directory_entries->push_back(std::make_shared<DirectoryEntry>("", errno));
Sahana Rao71693442019-11-13 13:48:07 +000047 }
Sahana Raoa82bd6a2019-10-10 18:10:37 +010048 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 Rao8a588e72019-12-06 11:32:56 +000053 if (filter == nullptr || filter(*entry)) {
54 directory_entries->push_back(
55 std::make_shared<DirectoryEntry>(entry->d_name, entry->d_type));
56 }
Sahana Raoa82bd6a2019-10-10 18:10:37 +010057 }
Sahana Raoa82bd6a2019-10-10 18:10:37 +010058}
59
Sahana Raoa82bd6a2019-10-10 18:10:37 +010060} // namespace fuse
61} // namespace mediaprovider