Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Avoid using dirent.h #2884

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions nntrainer/app_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* @bug No known bugs except for NYI items
*
*/
#include <dirent.h>

#include <filesystem>
#include <iostream>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -421,14 +422,11 @@ AppContext &AppContext::Global() {
}

void AppContext::setWorkingDirectory(const std::string &base) {
DIR *dir = opendir(base.c_str());

if (!dir) {
if (!std::filesystem::is_directory(base)) {
std::stringstream ss;
ss << func_tag << "path is not directory or has no permission: " << base;
throw std::invalid_argument(ss.str().c_str());
}
closedir(dir);

char *ret = getRealpath(base.c_str(), nullptr);

Expand Down Expand Up @@ -576,40 +574,35 @@ int AppContext::registerOptimizer(const std::string &library_path,

std::vector<int>
AppContext::registerPluggableFromDirectory(const std::string &base_path) {
DIR *dir = opendir(base_path.c_str());
const auto directory_exist = std::filesystem::is_directory(base_path);

NNTR_THROW_IF(dir == nullptr, std::invalid_argument)
NNTR_THROW_IF(!directory_exist, std::invalid_argument)
<< func_tag << "failed to open the directory: " << base_path;

struct dirent *entry;
std::vector<int> keys = {};

std::vector<int> keys;
for (const auto &entry : std::filesystem::directory_iterator(base_path)) {
const auto &entry_name = entry.path().string();

while ((entry = readdir(dir)) != NULL) {
if (endswith(entry->d_name, solib_suffix)) {
if (endswith(entry->d_name, layerlib_suffix)) {
if (endswith(entry_name, solib_suffix)) {
if (endswith(entry_name, layerlib_suffix)) {
try {
int key = registerLayer(entry->d_name, base_path);
int key = registerLayer(entry_name, base_path);
keys.emplace_back(key);
} catch (std::exception &e) {
closedir(dir);
throw;
}
} else if (endswith(entry->d_name, optimizerlib_suffix)) {
} else if (endswith(entry_name, optimizerlib_suffix)) {
try {
int key = registerOptimizer(entry->d_name, base_path);
int key = registerOptimizer(entry_name, base_path);
keys.emplace_back(key);
} catch (std::exception &e) {
closedir(dir);
throw;
}
}
}
}

if (dir != NULL)
closedir(dir);

return keys;
}

Expand Down
Loading