Skip to content

Commit

Permalink
attempt various ways to detect if path is a file or a directory on ma…
Browse files Browse the repository at this point in the history
…cOS (doesn't work yet)
  • Loading branch information
illwieckz committed Jul 8, 2024
1 parent 52f2c08 commit ecd2312
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions crnlib/crn_find_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#elif defined(__GNUC__)
#include <sys/stat.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <dirent.h>
#endif
Expand Down Expand Up @@ -209,21 +210,55 @@ bool find_files::find_internal(const char* pBasepath, const char* pRelpath, cons
bool is_directory = 0;
bool is_file = 0;

if ( ep->d_type != 0 ) {
bool guessed = false;

if (ep->d_type != 0) {
is_directory = (ep->d_type & DT_DIR) != 0;
is_file = (ep->d_type & DT_REG) != 0;
} else {
guessed = true;
}

if (!guessed) {
struct stat s;
if (stat(ep->d_name, &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) {
struct stat s;
int fd = open(ep->d_name, O_RDONLY);
if (fd != -1) {
if (fstat(fd, &s) == 0) {
is_directory = S_ISDIR(s.st_mode);
is_file = S_ISREG(s.st_mode);
guessed = true;
}
}
}

if (!guessed) {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
int fd = open(cwd, O_RDONLY);
if (fd != -1) {
struct stat s;
if (fstatat(fd, ep->d_name, &s, 0) == 0) {
is_directory = S_ISDIR(s.st_mode);
is_file = S_ISREG(s.st_mode);
guessed = true;
}
}
}
}

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

dynamic_string filename(ep->d_name);

if (is_directory) {
Expand Down

0 comments on commit ecd2312

Please sign in to comment.