libwinpr-file: make simplest FindFirstFile test case pass
authorMarc-André Moreau <marcandre.moreau@gmail.com>
Sat, 3 Nov 2012 00:58:58 +0000 (20:58 -0400)
committerMarc-André Moreau <marcandre.moreau@gmail.com>
Sat, 3 Nov 2012 00:58:58 +0000 (20:58 -0400)
channels/drive/client/drive_file.c
winpr/include/winpr/file.h
winpr/libwinpr/file/file.c
winpr/libwinpr/file/test/TestFileFindFirstFile.c

index 01991d6..bc53842 100644 (file)
@@ -49,6 +49,7 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
index 1f205e7..fe655ed 100644 (file)
@@ -24,6 +24,7 @@
 #include <winpr/wtypes.h>
 
 #include <winpr/io.h>
+#include <winpr/error.h>
 
 #ifndef _WIN32
 
index b725634..b4df997 100644 (file)
 #include <unistd.h>
 #endif
 
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 /**
  * api-ms-win-core-file-l1-2-0.dll:
  * 
 
 #ifndef _WIN32
 
+#include <time.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <sys/stat.h>
+
+#ifdef ANDROID
+#include <sys/vfs.h>
+#else
+#include <sys/statvfs.h>
+#endif
+
 HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
 {
@@ -267,8 +285,62 @@ BOOL UnlockFileEx(HANDLE hFile, DWORD dwReserved, DWORD nNumberOfBytesToUnlockLo
 
 HANDLE FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
 {
+       char* p;
+       int index;
+       int length;
+       DIR* pDir;
+       LPSTR path;
+       LPSTR pattern;
+       struct stat fileStat;
+       struct dirent* pDirent;
+
        ZeroMemory(lpFindFileData, sizeof(LPWIN32_FIND_DATAA));
 
+       /* Separate lpFileName into path and pattern components */
+
+       p = strrchr(lpFileName, '/'); /* TODO: portability */
+
+       index = (p - lpFileName);
+       length = (p - lpFileName);
+       path = (LPSTR) malloc(length + 1);
+       CopyMemory(path, lpFileName, length);
+       path[length] = '\0';
+
+       length = strlen(lpFileName) - index;
+       pattern = (LPSTR) malloc(length + 1);
+       CopyMemory(pattern, &lpFileName[index + 1], length);
+       pattern[length] = '\0';
+
+       /* Check if the path is a directory */
+
+       if (lstat(path, &fileStat) < 0)
+               return INVALID_HANDLE_VALUE; /* stat error */
+
+       if (S_ISDIR(fileStat.st_mode) == 0)
+               return INVALID_HANDLE_VALUE; /* not a directory */
+
+       /* Open directory for reading */
+
+       pDir = opendir(path);
+
+       if (!pDir)
+               return ERROR_FILE_NOT_FOUND;
+
+       while ((pDirent = readdir(pDir)) != NULL)
+       {
+               if ((strcmp(pDirent->d_name, ".") == 0) || (strcmp(pDirent->d_name, "..") == 0))
+               {
+                       /* skip "." and ".." */
+                       continue;
+               }
+
+               if (strcmp(pDirent->d_name, pattern) == 0)
+               {
+                       strcpy(lpFindFileData->cFileName, pDirent->d_name);
+                       return (HANDLE) 1;
+               }
+       }
+
        return NULL;
 }
 
index ff39da9..d6f027a 100644 (file)
@@ -41,7 +41,7 @@ int TestFileFindFirstFile(int argc, char* argv[])
 
        if (hFind == INVALID_HANDLE_VALUE)
        {
-               _tprintf(_T("FindFirstFile failure: %s\n"), FilePath);
+               _tprintf(_T("FindFirstFile failure: %s (%d)\n"), FilePath, hFind);
                return -1;
        }