|
| 1 | +// Copyright (c) 2017-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "dali/operators/reader/loader/discover_files.h" |
| 16 | +#include <dirent.h> |
| 17 | +#include <errno.h> |
| 18 | +#include <fnmatch.h> |
| 19 | +#include <glob.h> |
| 20 | +#include <sys/stat.h> |
| 21 | +#include <algorithm> |
| 22 | +#include <cstring> |
| 23 | +#include <filesystem> |
| 24 | +#include <optional> |
| 25 | +#include <string> |
| 26 | +#include <unordered_map> |
| 27 | +#include <utility> |
| 28 | +#include <vector> |
| 29 | +#include "dali/core/call_at_exit.h" |
| 30 | +#include "dali/core/error_handling.h" |
| 31 | +#include "dali/operators/reader/loader/filesystem.h" |
| 32 | +#include "dali/operators/reader/loader/utils.h" |
| 33 | + |
| 34 | +namespace dali { |
| 35 | + |
| 36 | +std::vector<std::string> list_subdirectories(const std::string &parent_dir, |
| 37 | + const std::vector<std::string> dir_filters = {}, |
| 38 | + bool case_sensitive_filter = true) { |
| 39 | + // open the root |
| 40 | + DIR *dir = opendir(parent_dir.c_str()); |
| 41 | + DALI_ENFORCE(dir != nullptr, make_string("Failed to open ", parent_dir)); |
| 42 | + auto cleanup = AtScopeExit([&dir] { |
| 43 | + closedir(dir); |
| 44 | + }); |
| 45 | + |
| 46 | + struct dirent *entry; |
| 47 | + std::vector<std::string> subdirs; |
| 48 | + |
| 49 | + while ((entry = readdir(dir))) { |
| 50 | + struct stat s; |
| 51 | + std::string entry_name(entry->d_name); |
| 52 | + std::string full_path = filesystem::join_path(parent_dir, entry_name); |
| 53 | + int ret = stat(full_path.c_str(), &s); |
| 54 | + DALI_ENFORCE(ret == 0, "Could not access " + full_path + " during directory traversal."); |
| 55 | + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) |
| 56 | + continue; |
| 57 | + if (S_ISDIR(s.st_mode)) { |
| 58 | + if (dir_filters.empty()) { |
| 59 | + subdirs.push_back(entry_name); |
| 60 | + } else { |
| 61 | + for (auto &filter : dir_filters) { |
| 62 | + if (fnmatch(filter.c_str(), entry_name.c_str(), |
| 63 | + case_sensitive_filter ? 0 : FNM_CASEFOLD) == 0) { |
| 64 | + subdirs.push_back(entry_name); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + // sort directories to preserve class alphabetic order, as readdir could |
| 71 | + // return unordered dir list. Otherwise file reader for training and validation |
| 72 | + // could return directories with the same names in completely different order |
| 73 | + std::sort(subdirs.begin(), subdirs.end()); |
| 74 | + return subdirs; |
| 75 | +} |
| 76 | + |
| 77 | +std::vector<std::string> list_files(const std::string &parent_dir, |
| 78 | + const std::vector<std::string> filters = {}, |
| 79 | + bool case_sensitive_filter = true) { |
| 80 | + DIR *dir = opendir(parent_dir.c_str()); |
| 81 | + DALI_ENFORCE(dir != nullptr, make_string("Failed to open ", parent_dir)); |
| 82 | + auto cleanup = AtScopeExit([&dir] { |
| 83 | + closedir(dir); |
| 84 | + }); |
| 85 | + |
| 86 | + dirent *entry; |
| 87 | + std::vector<std::string> files; |
| 88 | + while ((entry = readdir(dir))) { |
| 89 | +#ifdef _DIRENT_HAVE_D_TYPE |
| 90 | + /* |
| 91 | + * we support only regular files and symlinks, if FS returns DT_UNKNOWN |
| 92 | + * it doesn't mean anything and let us validate filename itself |
| 93 | + */ |
| 94 | + if (entry->d_type != DT_REG && entry->d_type != DT_LNK && entry->d_type != DT_UNKNOWN) { |
| 95 | + continue; |
| 96 | + } |
| 97 | +#endif |
| 98 | + std::string fname(entry->d_name); |
| 99 | + for (auto &filter : filters) { |
| 100 | + if (fnmatch(filter.c_str(), fname.c_str(), case_sensitive_filter ? 0 : FNM_CASEFOLD) == 0) { |
| 101 | + files.push_back(fname); |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + std::sort(files.begin(), files.end()); |
| 107 | + return files; |
| 108 | +} |
| 109 | + |
| 110 | +std::vector<FileLabelEntry> discover_files(const std::string &file_root, |
| 111 | + const FileDiscoveryOptions &opts) { |
| 112 | + bool is_s3 = starts_with(file_root, "s3://"); |
| 113 | + if (is_s3) { |
| 114 | + DALI_FAIL("This version of DALI was not built with AWS S3 storage support."); |
| 115 | + } |
| 116 | + |
| 117 | + std::vector<std::string> subdirs; |
| 118 | + subdirs = list_subdirectories(file_root, opts.dir_filters, opts.case_sensitive_filter); |
| 119 | + std::vector<FileLabelEntry> entries; |
| 120 | + auto process_dir = [&](const std::string &rel_dirpath, std::optional<int> label = {}) { |
| 121 | + auto full_dirpath = filesystem::join_path(file_root, rel_dirpath); |
| 122 | + auto tmp_files = list_files(full_dirpath, opts.file_filters, opts.case_sensitive_filter); |
| 123 | + for (const auto &f : tmp_files) { |
| 124 | + entries.push_back({filesystem::join_path(rel_dirpath, f), label}); |
| 125 | + } |
| 126 | + }; |
| 127 | + |
| 128 | + // if we are in "label_from_subdir" mode, we need a subdir to infer the label, therefore we don't |
| 129 | + // visit the current directory |
| 130 | + if (!opts.label_from_subdir) { |
| 131 | + process_dir("."); |
| 132 | + } |
| 133 | + for (unsigned dir_idx = 0; dir_idx < subdirs.size(); ++dir_idx) { |
| 134 | + process_dir(subdirs[dir_idx], |
| 135 | + opts.label_from_subdir ? std::optional<int>{dir_idx} : std::nullopt); |
| 136 | + } |
| 137 | + size_t total_dir_count = opts.label_from_subdir ? subdirs.size() : subdirs.size() + 1; |
| 138 | + LOG_LINE << "read " << entries.size() << " files from " << total_dir_count << "directories\n"; |
| 139 | + return entries; |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace dali |
0 commit comments