Skip to content

Commit

Permalink
crnlib: correct alternate code to test if a path is a directory
Browse files Browse the repository at this point in the history
The alternate code was testing for the file name in the current directory.
  • Loading branch information
illwieckz committed Jul 8, 2024
1 parent c6a5d46 commit 62b5939
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions crnlib/crn_find_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,22 +206,39 @@ bool find_files::find_internal(const char* pBasepath, const char* pRelpath, cons
if ((strcmp(ep->d_name, ".") == 0) || (strcmp(ep->d_name, "..") == 0))
continue;

bool is_directory = 0;
bool is_file = 0;
bool is_directory = false;
bool is_file = false;
bool guessed = false;

if ( ep->d_type != 0 ) {
/* This is the faster guess as it doesn't require any
extra IO as everything is already read. */
if (ep->d_type != DT_UNKNOWN) {
is_directory = (ep->d_type & DT_DIR) != 0;
is_file = (ep->d_type & DT_REG) != 0;
} else {
guessed = true;
}

/* Not all filesystems implement d_type which is optional,
especially network file systems and non-native ones.
See https://github.com/DaemonEngine/crunch/issues/37 */
if (!guessed) {
dynamic_string filepath = pathname + dynamic_string("/") + dynamic_string(ep->d_name);
struct stat s;
if (stat(ep->d_name, &s) == 0) {
if (stat(filepath.get_ptr(), &s) == 0) {
is_directory = S_ISDIR(s.st_mode);
is_file = S_ISREG(s.st_mode);
guessed = true;
}
else {
console::error("Cannot detect if given path is a file or a directory");
return false;
}
}

if (!guessed) {
console::error("Cannot detect if the given path is a file or a directory");
return false;
}

if (!is_file && !is_directory) {
console::error("The given path is not a file neither a directory");
return false;
}

dynamic_string filename(ep->d_name);
Expand Down

0 comments on commit 62b5939

Please sign in to comment.