| 1 |
/* |
| 2 |
** 2025-06-05 |
| 3 |
** |
| 4 |
** The author disclaims copyright to this source code. In place of |
| 5 |
** a legal notice, here is a blessing: |
| 6 |
** |
| 7 |
** May you do good and not evil. |
| 8 |
** May you find forgiveness for yourself and forgive others. |
| 9 |
** May you share freely, never taking more than you give. |
| 10 |
** |
| 11 |
************************************************************************* |
| 12 |
** |
| 13 |
** An implementation of opendir(), readdir(), and closedir() for Windows, |
| 14 |
** based on the FindFirstFile(), FindNextFile(), and FindClose() APIs |
| 15 |
** of Win32. |
| 16 |
** |
| 17 |
** #include this file inside any C-code module that needs to use |
| 18 |
** opendir()/readdir()/closedir(). This file is a no-op on non-Windows |
| 19 |
** machines. On Windows, static functions are defined that implement |
| 20 |
** those standard interfaces. |
| 21 |
*/ |
| 22 |
#if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H) |
| 23 |
#define SQLITE_WINDIRENT_H |
| 24 |
|
| 25 |
#ifndef WIN32_LEAN_AND_MEAN |
| 26 |
#define WIN32_LEAN_AND_MEAN |
| 27 |
#endif |
| 28 |
#include <windows.h> |
| 29 |
#include <io.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <limits.h> |
| 34 |
#include <sys/types.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <string.h> |
| 37 |
#ifndef FILENAME_MAX |
| 38 |
# define FILENAME_MAX (260) |
| 39 |
#endif |
| 40 |
#ifndef S_ISREG |
| 41 |
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 42 |
#endif |
| 43 |
#ifndef S_ISDIR |
| 44 |
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 45 |
#endif |
| 46 |
#ifndef S_ISLNK |
| 47 |
#define S_ISLNK(m) (0) |
| 48 |
#endif |
| 49 |
typedef unsigned short mode_t; |
| 50 |
|
| 51 |
/* The dirent object for Windows is abbreviated. The only field really |
| 52 |
** usable by applications is d_name[]. |
| 53 |
*/ |
| 54 |
struct dirent { |
| 55 |
int d_ino; /* Inode number (synthesized) */ |
| 56 |
unsigned d_attributes; /* File attributes */ |
| 57 |
char d_name[FILENAME_MAX]; /* Null-terminated filename */ |
| 58 |
}; |
| 59 |
|
| 60 |
/* The internals of DIR are opaque according to standards. So it |
| 61 |
** does not matter what we put here. */ |
| 62 |
typedef struct DIR DIR; |
| 63 |
struct DIR { |
| 64 |
intptr_t d_handle; /* Handle for findfirst()/findnext() */ |
| 65 |
struct dirent cur; /* Current entry */ |
| 66 |
}; |
| 67 |
|
| 68 |
/* Ignore hidden and system files */ |
| 69 |
#define WindowsFileToIgnore(a) \ |
| 70 |
((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM)) |
| 71 |
|
| 72 |
/* |
| 73 |
** Close a previously opened directory |
| 74 |
*/ |
| 75 |
static int closedir(DIR *pDir){ |
| 76 |
int rc = 0; |
| 77 |
if( pDir==0 ){ |
| 78 |
return EINVAL; |
| 79 |
} |
| 80 |
if( pDir->d_handle!=0 && pDir->d_handle!=(-1) ){ |
| 81 |
rc = _findclose(pDir->d_handle); |
| 82 |
} |
| 83 |
sqlite3_free(pDir); |
| 84 |
return rc; |
| 85 |
} |
| 86 |
|
| 87 |
/* |
| 88 |
** Open a new directory. The directory name should be UTF-8 encoded. |
| 89 |
** appropriate translations happen automatically. |
| 90 |
*/ |
| 91 |
static DIR *opendir(const char *zDirName){ |
| 92 |
DIR *pDir; |
| 93 |
wchar_t *b1; |
| 94 |
sqlite3_int64 sz; |
| 95 |
struct _wfinddata_t data; |
| 96 |
|
| 97 |
pDir = sqlite3_malloc64( sizeof(DIR) ); |
| 98 |
if( pDir==0 ) return 0; |
| 99 |
memset(pDir, 0, sizeof(DIR)); |
| 100 |
memset(&data, 0, sizeof(data)); |
| 101 |
sz = strlen(zDirName); |
| 102 |
b1 = sqlite3_malloc64( (sz+3)*sizeof(b1[0]) ); |
| 103 |
if( b1==0 ){ |
| 104 |
closedir(pDir); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
sz = MultiByteToWideChar(CP_UTF8, 0, zDirName, sz, b1, sz); |
| 108 |
b1[sz++] = '\\'; |
| 109 |
b1[sz++] = '*'; |
| 110 |
b1[sz] = 0; |
| 111 |
if( sz+1>sizeof(data.name)/sizeof(data.name[0]) ){ |
| 112 |
closedir(pDir); |
| 113 |
sqlite3_free(b1); |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
memcpy(data.name, b1, (sz+1)*sizeof(b1[0])); |
| 117 |
sqlite3_free(b1); |
| 118 |
pDir->d_handle = _wfindfirst(data.name, &data); |
| 119 |
if( pDir->d_handle<0 ){ |
| 120 |
closedir(pDir); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
while( WindowsFileToIgnore(data) ){ |
| 124 |
memset(&data, 0, sizeof(data)); |
| 125 |
if( _wfindnext(pDir->d_handle, &data)==-1 ){ |
| 126 |
closedir(pDir); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
} |
| 130 |
pDir->cur.d_ino = 0; |
| 131 |
pDir->cur.d_attributes = data.attrib; |
| 132 |
WideCharToMultiByte(CP_UTF8, 0, data.name, -1, |
| 133 |
pDir->cur.d_name, FILENAME_MAX, 0, 0); |
| 134 |
return pDir; |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
** Read the next entry from a directory. |
| 139 |
** |
| 140 |
** The returned struct-dirent object is managed by DIR. It is only |
| 141 |
** valid until the next readdir() or closedir() call. Only the |
| 142 |
** d_name[] field is meaningful. The d_name[] value has been |
| 143 |
** translated into UTF8. |
| 144 |
*/ |
| 145 |
static struct dirent *readdir(DIR *pDir){ |
| 146 |
struct _wfinddata_t data; |
| 147 |
if( pDir==0 ) return 0; |
| 148 |
if( (pDir->cur.d_ino++)==0 ){ |
| 149 |
return &pDir->cur; |
| 150 |
} |
| 151 |
do{ |
| 152 |
memset(&data, 0, sizeof(data)); |
| 153 |
if( _wfindnext(pDir->d_handle, &data)==-1 ){ |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
}while( WindowsFileToIgnore(data) ); |
| 157 |
pDir->cur.d_attributes = data.attrib; |
| 158 |
WideCharToMultiByte(CP_UTF8, 0, data.name, -1, |
| 159 |
pDir->cur.d_name, FILENAME_MAX, 0, 0); |
| 160 |
return &pDir->cur; |
| 161 |
} |
| 162 |
|
| 163 |
#endif /* defined(_WIN32) && defined(_MSC_VER) */ |