From c0aaebd0a57dc4269cfaf435a4bda327ebd8ccf5 Mon Sep 17 00:00:00 2001 From: Bill Zissimopoulos Date: Sun, 29 Jan 2017 15:45:36 -0800 Subject: [PATCH] tst: passthrough-fuse: windows-posix layer: dirfd --- tst/passthrough-fuse/winposix.c | 43 ++++++++++++++++++++------------- tst/passthrough-fuse/winposix.h | 1 + 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/tst/passthrough-fuse/winposix.c b/tst/passthrough-fuse/winposix.c index 45142585..50803bc1 100644 --- a/tst/passthrough-fuse/winposix.c +++ b/tst/passthrough-fuse/winposix.c @@ -31,7 +31,7 @@ struct _DIR { - HANDLE handle; + HANDLE h, fh; struct dirent dirent; char path[]; }; @@ -330,14 +330,10 @@ int rmdir(const char *path) DIR *opendir(const char *path) { - DWORD FileAttributes = GetFileAttributesA(path); - if (INVALID_FILE_ATTRIBUTES == FileAttributes) + HANDLE h = CreateFileA(path, + FILE_READ_ATTRIBUTES, 0, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); + if (INVALID_HANDLE_VALUE == h) return error0(); - if (0 == (FileAttributes & FILE_ATTRIBUTE_DIRECTORY)) - { - errno = ENOTDIR; - return 0; - } size_t pathlen = strlen(path); if (0 < pathlen && '/' == path[pathlen - 1]) @@ -345,10 +341,14 @@ DIR *opendir(const char *path) DIR *dirp = malloc(sizeof *dirp + pathlen + 3); /* sets errno */ if (0 == dirp) + { + CloseHandle(h); return 0; + } memset(dirp, 0, sizeof *dirp); - dirp->handle = INVALID_HANDLE_VALUE; + dirp->h = h; + dirp->fh = INVALID_HANDLE_VALUE; memcpy(dirp->path, path, pathlen); dirp->path[pathlen + 0] = '/'; dirp->path[pathlen + 1] = '*'; @@ -357,25 +357,33 @@ DIR *opendir(const char *path) return dirp; } +int dirfd(DIR *dirp) +{ + return (int)(intptr_t)dirp->h; +} + void rewinddir(DIR *dirp) { - if (INVALID_HANDLE_VALUE != dirp->handle) - FindClose(dirp->handle); + if (INVALID_HANDLE_VALUE != dirp->fh) + { + FindClose(dirp->fh); + dirp->fh = INVALID_HANDLE_VALUE; + } } struct dirent *readdir(DIR *dirp) { WIN32_FIND_DATAA FindData; - if (INVALID_HANDLE_VALUE != dirp->handle) + if (INVALID_HANDLE_VALUE == dirp->fh) { - dirp->handle = FindFirstFileA(dirp->path, &FindData); - if (INVALID_HANDLE_VALUE == dirp) + dirp->fh = FindFirstFileA(dirp->path, &FindData); + if (INVALID_HANDLE_VALUE == dirp->fh) return error0(); } else { - if (!FindNextFileA(dirp->handle, &FindData)) + if (!FindNextFileA(dirp->fh, &FindData)) return error0(); } @@ -386,9 +394,10 @@ struct dirent *readdir(DIR *dirp) int closedir(DIR *dirp) { - if (INVALID_HANDLE_VALUE != dirp->handle) - FindClose(dirp->handle); + if (INVALID_HANDLE_VALUE != dirp->fh) + FindClose(dirp->fh); + CloseHandle(dirp->h); free(dirp); return 0; diff --git a/tst/passthrough-fuse/winposix.h b/tst/passthrough-fuse/winposix.h index 52a5f226..0952f7a6 100644 --- a/tst/passthrough-fuse/winposix.h +++ b/tst/passthrough-fuse/winposix.h @@ -58,6 +58,7 @@ int mkdir(const char *path, fuse_mode_t mode); int rmdir(const char *path); DIR *opendir(const char *path); +int dirfd(DIR *dirp); void rewinddir(DIR *dirp); struct dirent *readdir(DIR *dirp); int closedir(DIR *dirp);