OLD | NEW |
1 /* | 1 /* |
2 ** 2015 November 30 | 2 ** 2015 November 30 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** This file contains code to implement most of the opendir() family of | 12 ** This file contains code to implement most of the opendir() family of |
13 ** POSIX functions on Win32 using the MSVCRT. | 13 ** POSIX functions on Win32 using the MSVCRT. |
14 */ | 14 */ |
15 | 15 |
16 #if defined(_WIN32) && defined(_MSC_VER) | 16 #if defined(_WIN32) && defined(_MSC_VER) |
17 | 17 |
18 #include "test_windirent.h" | 18 #include "test_windirent.h" |
19 | 19 |
20 /* | 20 /* |
| 21 ** Implementation of the POSIX getenv() function using the Win32 API. |
| 22 ** This function is not thread-safe. |
| 23 */ |
| 24 const char *windirent_getenv( |
| 25 const char *name |
| 26 ){ |
| 27 static char value[32768]; /* Maximum length, per MSDN */ |
| 28 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */ |
| 29 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */ |
| 30 |
| 31 memset(value, 0, sizeof(value)); |
| 32 dwRet = GetEnvironmentVariableA(name, value, dwSize); |
| 33 if( dwRet==0 || dwRet>dwSize ){ |
| 34 /* |
| 35 ** The function call to GetEnvironmentVariableA() failed -OR- |
| 36 ** the buffer is not large enough. Either way, return NULL. |
| 37 */ |
| 38 return 0; |
| 39 }else{ |
| 40 /* |
| 41 ** The function call to GetEnvironmentVariableA() succeeded |
| 42 ** -AND- the buffer contains the entire value. |
| 43 */ |
| 44 return value; |
| 45 } |
| 46 } |
| 47 |
| 48 /* |
21 ** Implementation of the POSIX opendir() function using the MSVCRT. | 49 ** Implementation of the POSIX opendir() function using the MSVCRT. |
22 */ | 50 */ |
23 LPDIR opendir( | 51 LPDIR opendir( |
24 const char *dirname | 52 const char *dirname |
25 ){ | 53 ){ |
26 struct _finddata_t data; | 54 struct _finddata_t data; |
27 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR)); | 55 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR)); |
28 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]); | 56 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]); |
29 | 57 |
30 if( dirp==NULL ) return NULL; | 58 if( dirp==NULL ) return NULL; |
31 memset(dirp, 0, sizeof(DIR)); | 59 memset(dirp, 0, sizeof(DIR)); |
32 | 60 |
33 /* TODO: Remove this if Unix-style root paths are not used. */ | 61 /* TODO: Remove this if Unix-style root paths are not used. */ |
34 if( sqlite3_stricmp(dirname, "/")==0 ){ | 62 if( sqlite3_stricmp(dirname, "/")==0 ){ |
35 dirname = getenv("SystemDrive"); | 63 dirname = windirent_getenv("SystemDrive"); |
36 } | 64 } |
37 | 65 |
| 66 memset(&data, 0, sizeof(struct _finddata_t)); |
38 _snprintf(data.name, namesize, "%s\\*", dirname); | 67 _snprintf(data.name, namesize, "%s\\*", dirname); |
39 dirp->d_handle = _findfirst(data.name, &data); | 68 dirp->d_handle = _findfirst(data.name, &data); |
40 | 69 |
41 if( dirp->d_handle==BAD_INTPTR_T ){ | 70 if( dirp->d_handle==BAD_INTPTR_T ){ |
42 closedir(dirp); | 71 closedir(dirp); |
43 return NULL; | 72 return NULL; |
44 } | 73 } |
45 | 74 |
46 /* TODO: Remove this block to allow hidden and system files. */ | 75 /* TODO: Remove this block to allow hidden and/or system files. */ |
47 if( data.attrib&_A_HIDDEN || data.attrib&_A_SYSTEM ){ | 76 if( is_filtered(data) ){ |
| 77 next: |
| 78 |
| 79 memset(&data, 0, sizeof(struct _finddata_t)); |
48 if( _findnext(dirp->d_handle, &data)==-1 ){ | 80 if( _findnext(dirp->d_handle, &data)==-1 ){ |
49 closedir(dirp); | 81 closedir(dirp); |
50 return NULL; | 82 return NULL; |
51 } | 83 } |
| 84 |
| 85 /* TODO: Remove this block to allow hidden and/or system files. */ |
| 86 if( is_filtered(data) ) goto next; |
52 } | 87 } |
53 | 88 |
54 dirp->d_first.d_attributes = data.attrib; | 89 dirp->d_first.d_attributes = data.attrib; |
55 strncpy(dirp->d_first.d_name, data.name, NAME_MAX); | 90 strncpy(dirp->d_first.d_name, data.name, NAME_MAX); |
56 dirp->d_first.d_name[NAME_MAX] = '\0'; | 91 dirp->d_first.d_name[NAME_MAX] = '\0'; |
57 | 92 |
58 return dirp; | 93 return dirp; |
59 } | 94 } |
60 | 95 |
61 /* | 96 /* |
62 ** Implementation of the POSIX readdir() function using the MSVCRT. | 97 ** Implementation of the POSIX readdir() function using the MSVCRT. |
63 */ | 98 */ |
64 LPDIRENT readdir( | 99 LPDIRENT readdir( |
65 LPDIR dirp | 100 LPDIR dirp |
66 ){ | 101 ){ |
67 struct _finddata_t data; | 102 struct _finddata_t data; |
68 | 103 |
69 if( dirp==NULL ) return NULL; | 104 if( dirp==NULL ) return NULL; |
70 | 105 |
71 if( dirp->d_first.d_ino==0 ){ | 106 if( dirp->d_first.d_ino==0 ){ |
72 dirp->d_first.d_ino++; | 107 dirp->d_first.d_ino++; |
73 dirp->d_next.d_ino++; | 108 dirp->d_next.d_ino++; |
74 | 109 |
75 return &dirp->d_first; | 110 return &dirp->d_first; |
76 } | 111 } |
77 | 112 |
78 next: | 113 next: |
79 | 114 |
| 115 memset(&data, 0, sizeof(struct _finddata_t)); |
80 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL; | 116 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL; |
81 | 117 |
82 /* TODO: Remove this block to allow hidden and system files. */ | 118 /* TODO: Remove this block to allow hidden and/or system files. */ |
83 if( data.attrib&_A_HIDDEN ) goto next; | 119 if( is_filtered(data) ) goto next; |
84 if( data.attrib&_A_SYSTEM ) goto next; | |
85 | 120 |
86 dirp->d_next.d_ino++; | 121 dirp->d_next.d_ino++; |
87 dirp->d_next.d_attributes = data.attrib; | 122 dirp->d_next.d_attributes = data.attrib; |
88 strncpy(dirp->d_next.d_name, data.name, NAME_MAX); | 123 strncpy(dirp->d_next.d_name, data.name, NAME_MAX); |
89 dirp->d_next.d_name[NAME_MAX] = '\0'; | 124 dirp->d_next.d_name[NAME_MAX] = '\0'; |
90 | 125 |
91 return &dirp->d_next; | 126 return &dirp->d_next; |
92 } | 127 } |
93 | 128 |
94 /* | 129 /* |
(...skipping 16 matching lines...) Expand all Loading... |
111 entry->d_attributes = dirp->d_first.d_attributes; | 146 entry->d_attributes = dirp->d_first.d_attributes; |
112 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX); | 147 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX); |
113 entry->d_name[NAME_MAX] = '\0'; | 148 entry->d_name[NAME_MAX] = '\0'; |
114 | 149 |
115 *result = entry; | 150 *result = entry; |
116 return 0; | 151 return 0; |
117 } | 152 } |
118 | 153 |
119 next: | 154 next: |
120 | 155 |
| 156 memset(&data, 0, sizeof(struct _finddata_t)); |
121 if( _findnext(dirp->d_handle, &data)==-1 ){ | 157 if( _findnext(dirp->d_handle, &data)==-1 ){ |
122 *result = NULL; | 158 *result = NULL; |
123 return ENOENT; | 159 return ENOENT; |
124 } | 160 } |
125 | 161 |
126 /* TODO: Remove this block to allow hidden and system files. */ | 162 /* TODO: Remove this block to allow hidden and/or system files. */ |
127 if( data.attrib&_A_HIDDEN ) goto next; | 163 if( is_filtered(data) ) goto next; |
128 if( data.attrib&_A_SYSTEM ) goto next; | |
129 | 164 |
130 entry->d_ino = (ino_t)-1; /* not available */ | 165 entry->d_ino = (ino_t)-1; /* not available */ |
131 entry->d_attributes = data.attrib; | 166 entry->d_attributes = data.attrib; |
132 strncpy(entry->d_name, data.name, NAME_MAX); | 167 strncpy(entry->d_name, data.name, NAME_MAX); |
133 entry->d_name[NAME_MAX] = '\0'; | 168 entry->d_name[NAME_MAX] = '\0'; |
134 | 169 |
135 *result = entry; | 170 *result = entry; |
136 return 0; | 171 return 0; |
137 } | 172 } |
138 | 173 |
139 /* | 174 /* |
140 ** Implementation of the POSIX closedir() function using the MSVCRT. | 175 ** Implementation of the POSIX closedir() function using the MSVCRT. |
141 */ | 176 */ |
142 INT closedir( | 177 INT closedir( |
143 LPDIR dirp | 178 LPDIR dirp |
144 ){ | 179 ){ |
145 INT result = 0; | 180 INT result = 0; |
146 | 181 |
147 if( dirp==NULL ) return EINVAL; | 182 if( dirp==NULL ) return EINVAL; |
148 | 183 |
149 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ | 184 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ |
150 result = _findclose(dirp->d_handle); | 185 result = _findclose(dirp->d_handle); |
151 } | 186 } |
152 | 187 |
153 sqlite3_free(dirp); | 188 sqlite3_free(dirp); |
154 return result; | 189 return result; |
155 } | 190 } |
156 | 191 |
157 #endif /* defined(WIN32) && defined(_MSC_VER) */ | 192 #endif /* defined(WIN32) && defined(_MSC_VER) */ |
OLD | NEW |