diff options
author | stijn <stijn@ignitron.net> | 2018-10-04 12:46:06 +0200 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2018-10-05 17:14:33 +1000 |
commit | 02ca8d4674773ed6258eb435beaf0004e04a56ac (patch) | |
tree | 7102ad846fb33c8d8796237d831b57382780c8b9 | |
parent | 397ee7c00e67ba4bd2da5d93187c47725f55157b (diff) |
windows/msvc: Implement file/directory type query.
Add some more POSIX compatibility by adding a d_type field to the
dirent structure and defining corresponding macros so listdir_next
in the unix' port modos.c can use it, end result being uos.ilistdir
now reports the file type.
-rw-r--r-- | ports/windows/msvc/dirent.c | 15 | ||||
-rw-r--r-- | ports/windows/msvc/dirent.h | 6 |
2 files changed, 21 insertions, 0 deletions
diff --git a/ports/windows/msvc/dirent.c b/ports/windows/msvc/dirent.c index ceee666c1..053a3cdf0 100644 --- a/ports/windows/msvc/dirent.c +++ b/ports/windows/msvc/dirent.c @@ -25,6 +25,7 @@ */ #include "dirent.h" +#include "extmod/vfs.h" #include <errno.h> #include <Windows.h> @@ -96,8 +97,22 @@ struct dirent *readdir(DIR *dir) { // first pass d_name is NULL so use result from FindFirstFile in opendir, else use FindNextFile if (!dir->result.d_name || FindNextFile(dir->findHandle, &dir->findData)) { dir->result.d_name = dir->findData.cFileName; + dir->result.d_type = dir->findData.dwFileAttributes; return &dir->result; } return NULL; } + +int dttoif(int d_type) { + if (d_type == INVALID_FILE_ATTRIBUTES) { + return 0; + } + // Could be a couple of things (symlink, junction, ...) and non-trivial to + // figure out so just report it as unknown. Should we ever want this then + // the proper code can be found in msvc's std::filesystem implementation. + if (d_type & FILE_ATTRIBUTE_REPARSE_POINT) { + return 0; + } + return (d_type & FILE_ATTRIBUTE_DIRECTORY) ? MP_S_IFDIR : MP_S_IFREG; +} diff --git a/ports/windows/msvc/dirent.h b/ports/windows/msvc/dirent.h index fca06785a..2ad88b3e0 100644 --- a/ports/windows/msvc/dirent.h +++ b/ports/windows/msvc/dirent.h @@ -31,18 +31,24 @@ // for ino_t #include <sys/types.h> +#define _DIRENT_HAVE_D_TYPE (1) +#define DTTOIF dttoif + // opaque DIR structure typedef struct DIR DIR; // the dirent structure // d_ino is always 0 - if ever needed use GetFileInformationByHandle +// d_type can be converted using DTTOIF, into 0 (unknown) or MP_S_IFDIR or MP_S_IFREG typedef struct dirent { ino_t d_ino; + int d_type; char *d_name; } dirent; DIR *opendir(const char *name); int closedir(DIR *dir); struct dirent *readdir(DIR *dir); +int dttoif(int d_type); #endif // MICROPY_INCLUDED_WINDOWS_MSVC_DIRENT_H |