| 1 | package TUI::StdDlg::FindFirstRec::Win32; | ||
| 2 | # ABSTRACT: A class implementing the behavior of findfirst/findnext for Win32 | ||
| 3 | |||
| 4 | use 5.010; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | our $VERSION = '2.000002'; | ||
| 9 | $VERSION =~ tr/_//d; | ||
| 10 | our $AUTHORITY = 'cpan:BRICKPOOL'; | ||
| 11 | |||
| 12 | require bytes; | ||
| 13 | use Config; | ||
| 14 | use Hash::Util::FieldHash qw( fieldhash ); | ||
| 15 | use Scalar::Util qw( | ||
| 16 | refaddr | ||
| 17 | weaken | ||
| 18 | ); | ||
| 19 | use TUI::toolkit qw( | ||
| 20 | :boolean | ||
| 21 | :utils | ||
| 22 | ); | ||
| 23 | use TUI::toolkit::Types qw( | ||
| 24 | Maybe | ||
| 25 | :is | ||
| 26 | :types | ||
| 27 | ); | ||
| 28 | use Win32; | ||
| 29 | use Win32::API; | ||
| 30 | |||
| 31 | use TUI::StdDlg::Const qw( | ||
| 32 | MAXPATH | ||
| 33 | :_A_ | ||
| 34 | ); | ||
| 35 | |||
| 36 | # We use variables to avoid polluting the namespace when importing Win32 API | ||
| 37 | # functions. | ||
| 38 | my ( | ||
| 39 | $FindFirstFileW, | ||
| 40 | $FindNextFileW, | ||
| 41 | $FindClose, | ||
| 42 | $FileTimeToLocalFileTime, | ||
| 43 | $FileTimeToDosDateTime, | ||
| 44 | $MultiByteToWideChar, | ||
| 45 | $WideCharToMultiByte, | ||
| 46 | ); | ||
| 47 | |||
| 48 | # Load required Windows API functions | ||
| 49 | BEGIN { | ||
| 50 | $FindFirstFileW = Win32::API::More->new( 'kernel32', | ||
| 51 | 'HANDLE FindFirstFileW( | ||
| 52 | LPCWSTR lpFileName, | ||
| 53 | LPVOID lpFindFileData | ||
| 54 | )', | ||
| 55 | ) or die "Import FindFirstFileW failed: $^E"; | ||
| 56 | |||
| 57 | $FindNextFileW = Win32::API::More->new( 'kernel32', | ||
| 58 | 'BOOL FindNextFileW( | ||
| 59 | HANDLE hFindFile, | ||
| 60 | LPVOID lpFindFileData | ||
| 61 | )', | ||
| 62 | ) or die "Import FindNextFileW failed: $^E"; | ||
| 63 | |||
| 64 | $FindClose = Win32::API::More->new( 'kernel32', | ||
| 65 | 'BOOL FindClose( | ||
| 66 | HANDLE hFindFile | ||
| 67 | )', | ||
| 68 | ) or die "Import FindClose failed: $^E"; | ||
| 69 | |||
| 70 | $FileTimeToLocalFileTime = Win32::API::More->new( 'kernel32', | ||
| 71 | 'BOOL FileTimeToLocalFileTime( | ||
| 72 | LPVOID lpFileTime, | ||
| 73 | LPVOID lpLocalFileTime | ||
| 74 | )', | ||
| 75 | ) or die "Import FileTimeToLocalFileTime failed: $^E"; | ||
| 76 | |||
| 77 | $FileTimeToDosDateTime = Win32::API::More->new( 'kernel32', | ||
| 78 | 'BOOL FileTimeToDosDateTime( | ||
| 79 | LPVOID lpFileTime, | ||
| 80 | LPWORD lpFatDate, | ||
| 81 | LPWORD lpFatTime | ||
| 82 | )', | ||
| 83 | ) or die "Import FileTimeToDosDateTime failed: $^E"; | ||
| 84 | |||
| 85 | $MultiByteToWideChar = Win32::API::More->new( 'kernel32', | ||
| 86 | 'int MultiByteToWideChar( | ||
| 87 | UINT CodePage, | ||
| 88 | DWORD dwFlags, | ||
| 89 | LPCSTR lpMultiByteStr, | ||
| 90 | int cbMultiByte, | ||
| 91 | LPWSTR lpWideCharStr, | ||
| 92 | int cchWideChar | ||
| 93 | )' | ||
| 94 | ) or die "Import MultiByteToWideChar: $^E"; | ||
| 95 | |||
| 96 | $WideCharToMultiByte = Win32::API::More->new( 'kernel32', | ||
| 97 | 'int WideCharToMultiByte( | ||
| 98 | UINT CodePage, | ||
| 99 | DWORD dwFlags, | ||
| 100 | LPCWSTR lpWideCharStr, | ||
| 101 | int cchWideChar, | ||
| 102 | LPSTR lpMultiByteStr, | ||
| 103 | int cbMultiByte, | ||
| 104 | LPCSTR lpDefaultChar, | ||
| 105 | LPBOOL lpUsedDefaultChar | ||
| 106 | )' | ||
| 107 | ) or die "Import WideCharToMultiByte: $^E"; | ||
| 108 | } | ||
| 109 | |||
| 110 | PRIVATE: { | ||
| 111 | namespace::sweep->import( -also => [qw( | ||
| 112 | INVALID_HANDLE_VALUE | ||
| 113 | |||
| 114 | CP_UTF8 | ||
| 115 | SPECIAL_BITS | ||
| 116 | |||
| 117 | DWORD_SIZE | ||
| 118 | FILETIME_SIZE | ||
| 119 | WCHAR_SIZE | ||
| 120 | FILENAME_SIZE | ||
| 121 | WIN32_FIND_DATAW_SIZE | ||
| 122 | |||
| 123 | dwFileAttributes | ||
| 124 | ftCreationTime | ||
| 125 | ftLastAccessTime | ||
| 126 | ftLastWriteTime | ||
| 127 | nFileSizeHigh | ||
| 128 | nFileSizeLow | ||
| 129 | dwReserved0 | ||
| 130 | dwReserved1 | ||
| 131 | cFileName | ||
| 132 | cAlternateFileName | ||
| 133 | |||
| 134 | reserved | ||
| 135 | size | ||
| 136 | attrib | ||
| 137 | wr_time | ||
| 138 | wr_date | ||
| 139 | name | ||
| 140 | )] ) if eval { require namespace::sweep }; | ||
| 141 | |||
| 142 | # We cannot import INVALID_HANDLE_VALUE from Win32API::File. | ||
| 143 | # That module defines INVALID_HANDLE_VALUE as a 32-bit value, while | ||
| 144 | # Win32::API::More returns HANDLE values using the native pointer size. | ||
| 145 | # Define the constant using the current pointer size so that HANDLE | ||
| 146 | # comparisons work correctly on both 32-bit and 64-bit Perls. | ||
| 147 | use constant INVALID_HANDLE_VALUE => do { | ||
| 148 | my $t = $Config{ptrsize} == 8 ? 'Q' : 'L'; | ||
| 149 | unpack $t => pack $t => ~0; | ||
| 150 | }; | ||
| 151 | |||
| 152 | use constant CP_UTF8 => 65001; | ||
| 153 | use constant SPECIAL_BITS => _A_SUBDIR | _A_HIDDEN | _A_SYSTEM; | ||
| 154 | |||
| 155 | # WIN32_FIND_DATAW structure | ||
| 156 | # https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-win32_find_dataw | ||
| 157 | use constant DWORD_SIZE => 4; | ||
| 158 | use constant FILETIME_SIZE => 2 * DWORD_SIZE; | ||
| 159 | use constant WCHAR_SIZE => 2; | ||
| 160 | use constant FILENAME_SIZE => MAXPATH * WCHAR_SIZE; | ||
| 161 | use constant WIN32_FIND_DATAW_SIZE => DWORD_SIZE | ||
| 162 | + 3 * FILETIME_SIZE | ||
| 163 | + 4 * DWORD_SIZE | ||
| 164 | + FILENAME_SIZE | ||
| 165 | + 14 * WCHAR_SIZE; | ||
| 166 | |||
| 167 | # WIN32_FIND_DATAW_SIZE offsets | ||
| 168 | use constant { | ||
| 169 | dwFileAttributes => 0, | ||
| 170 | ftCreationTime => 4, | ||
| 171 | ftLastAccessTime => 12, | ||
| 172 | ftLastWriteTime => 20, | ||
| 173 | nFileSizeHigh => 28, | ||
| 174 | nFileSizeLow => 32, | ||
| 175 | dwReserved0 => 36, | ||
| 176 | dwReserved1 => 40, | ||
| 177 | cFileName => 44, | ||
| 178 | cAlternateFileName => 564, | ||
| 179 | }; | ||
| 180 | |||
| 181 | # find_t offsets | ||
| 182 | use constant { | ||
| 183 | reserved => 0, | ||
| 184 | size => 1, | ||
| 185 | attrib => 2, | ||
| 186 | wr_time => 3, | ||
| 187 | wr_date => 4, | ||
| 188 | name => 5, | ||
| 189 | }; | ||
| 190 | } | ||
| 191 | |||
| 192 | # declare global variables | ||
| 193 | fieldhash my %REC_LIST; | ||
| 194 | |||
| 195 | # private attributes | ||
| 196 | our %HAS; BEGIN { | ||
| 197 | %HAS = ( | ||
| 198 | finfo => sub { die 'required' }, # weak_ref => 1 | ||
| 199 | searchAttr => sub { 0 }, | ||
| 200 | hFindFile => sub { INVALID_HANDLE_VALUE }, | ||
| 201 | fileName => sub { '' }, | ||
| 202 | ) | ||
| 203 | } | ||
| 204 | |||
| 205 | # predeclare private methods | ||
| 206 | my ( | ||
| 207 | $open, | ||
| 208 | $close, | ||
| 209 | $setParameters, | ||
| 210 | $attrMatch, | ||
| 211 | $cvtAttr, | ||
| 212 | $cvtTime, | ||
| 213 | ); | ||
| 214 | |||
| 215 | sub allocate { # $rec|undef ($fileinfo, $attrib, $pathname) | ||
| 216 | state $sig = signature( | ||
| 217 | method => 1, | ||
| 218 | pos => [ | ||
| 219 | Maybe[ArrayLike], | ||
| 220 | PositiveOrZeroInt, | ||
| 221 | Str, | ||
| 222 | ], | ||
| 223 | ); | ||
| 224 | my ( $class, $fileinfo, $attrib, $pathname ) = $sig->( @_ ); | ||
| 225 | |||
| 226 | # The findfirst interface based on DOS call 0x4E doesn't provide a | ||
| 227 | # findclose function. The strategy here is the same as in Borland's RTL: | ||
| 228 | # a new object is created and stored internally in %REC_LIST, unless | ||
| 229 | # $fileinfo has already been passed to us before. | ||
| 230 | return undef | ||
| 231 | unless $fileinfo; | ||
| 232 | |||
| 233 | my $r = $REC_LIST{ $fileinfo }; | ||
| 234 | # If $r is defined, we need to close the search handle and reset the | ||
| 235 | # parameters. If $r is undef, we need to create a new FindFirstRec and store | ||
| 236 | # it in the field hash %REC_LIST and the global registry for retrieval. | ||
| 237 | if ( $r ) { | ||
| 238 | $r->$close(); | ||
| 239 | } | ||
| 240 | else { | ||
| 241 | $r = bless { | ||
| 242 | finfo => $fileinfo // $HAS{finfo}->(), | ||
| 243 | searchAttr => $HAS{searchAttr}->(), | ||
| 244 | hFindFile => $HAS{hFindFile}->(), | ||
| 245 | fileName => $HAS{fileName}->(), | ||
| 246 | }, $class; | ||
| 247 | $REC_LIST{ $fileinfo } = $r; | ||
| 248 | weaken $r->{finfo}; | ||
| 249 | } | ||
| 250 | # If pathname is a valid directory, make fileinfo point to the allocated | ||
| 251 | # FindFirstRec. Otherwise, return undef. | ||
| 252 | if ( $r->$setParameters( $attrib, $pathname ) ) { | ||
| 253 | # Connect fileinfo to FindFirstRec for compatibility with the original | ||
| 254 | # findfirst interface. This allows the caller to identify the corresponding | ||
| 255 | # FindFirstRec object using the fileinfo structure. | ||
| 256 | $fileinfo->[reserved] = refaddr $r; | ||
| 257 | return $r; | ||
| 258 | } | ||
| 259 | return undef; | ||
| 260 | } #/ sub allocate | ||
| 261 | |||
| 262 | sub DESTROY { | ||
| 263 | my ( $self ) = @_; | ||
| 264 | assert ( @_ == 1 ); | ||
| 265 | assert ( is_Object $self ); | ||
| 266 | $self->$close(); | ||
| 267 | return; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub get { # $rec|undef ($fileinfo) | ||
| 271 | state $sig = signature( | ||
| 272 | method => 1, | ||
| 273 | pos => [ | ||
| 274 | Maybe[ArrayLike], | ||
| 275 | ], | ||
| 276 | ); | ||
| 277 | my ( $class, $fileinfo ) = $sig->( @_ ); | ||
| 278 | |||
| 279 | return undef | ||
| 280 | unless $fileinfo; | ||
| 281 | return $REC_LIST{ $fileinfo }; | ||
| 282 | } | ||
| 283 | |||
| 284 | sub next { # $bool () | ||
| 285 | state $sig = signature( | ||
| 286 | method => Object, | ||
| 287 | pos => [], | ||
| 288 | ); | ||
| 289 | my ( $self ) = $sig->( @_ ); | ||
| 290 | |||
| 291 | my $findData = "\0" x WIN32_FIND_DATAW_SIZE; | ||
| 292 | while ( 1 ) { | ||
| 293 | if ( $self->{hFindFile} == INVALID_HANDLE_VALUE ) { | ||
| 294 | my $cFileName = "\0" x FILENAME_SIZE; | ||
| 295 | # TODO: Our port currently uses code page 437. We therefore use | ||
| 296 | # GetConsoleOutputCP() to determine the code page for converting the | ||
| 297 | # filename to UTF-16. | ||
| 298 | $MultiByteToWideChar->Call( Win32::GetConsoleOutputCP(), 0, | ||
| 299 | $self->{fileName}, -1, | ||
| 300 | $cFileName, MAXPATH ); | ||
| 301 | $self->{hFindFile} = $FindFirstFileW->Call( $cFileName, $findData ); | ||
| 302 | } | ||
| 303 | elsif ( !$FindNextFileW->Call( $self->{hFindFile}, $findData ) ) { | ||
| 304 | $self->$close(); | ||
| 305 | return false; | ||
| 306 | } | ||
| 307 | |||
| 308 | if ( $self->{hFindFile} != INVALID_HANDLE_VALUE ) { | ||
| 309 | my $cFileName = bytes::substr( $findData, cFileName, FILENAME_SIZE ); | ||
| 310 | my $attr = $self->$cvtAttr( $findData, $cFileName ); | ||
| 311 | if ( $self->$attrMatch( $attr ) ) { | ||
| 312 | # Match found, fill finfo. | ||
| 313 | my $size = unpack 'V' | ||
| 314 | => bytes::substr( $findData, nFileSizeLow, DWORD_SIZE ); | ||
| 315 | $self->{finfo}->[size] = $size; | ||
| 316 | $self->{finfo}->[attrib] = $attr; | ||
| 317 | $self->$cvtTime( $findData, $self->{finfo} ); | ||
| 318 | my $name = "\0" x MAXPATH; | ||
| 319 | # TODO: workaround GetConsoleOutputCP(), see above | ||
| 320 | $WideCharToMultiByte->Call( Win32::GetConsoleOutputCP(), 0, | ||
| 321 | $cFileName, -1, | ||
| 322 | $name, MAXPATH, | ||
| 323 | undef, undef ); | ||
| 324 | $name =~ s/\0+\z//; | ||
| 325 | $self->{finfo}->[name] = $name; | ||
| 326 | return true; | ||
| 327 | } | ||
| 328 | } | ||
| 329 | else { | ||
| 330 | return false; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | $open = sub { # $bool () | ||
| 336 | my ( $self ) = @_; | ||
| 337 | assert ( @_ == 1 ); | ||
| 338 | assert ( is_Object $self ); | ||
| 339 | return true; | ||
| 340 | }; | ||
| 341 | |||
| 342 | $close = sub { # void () | ||
| 343 | my ( $self ) = @_; | ||
| 344 | assert ( @_ == 1 ); | ||
| 345 | assert ( is_Object $self ); | ||
| 346 | assert ( is_Int $self->{hFindFile} ); | ||
| 347 | if ( $self->{hFindFile} != INVALID_HANDLE_VALUE ) { | ||
| 348 | $FindClose->Call( $self->{hFindFile} ); | ||
| 349 | } | ||
| 350 | $self->{hFindFile} = INVALID_HANDLE_VALUE; | ||
| 351 | return; | ||
| 352 | }; | ||
| 353 | |||
| 354 | $setParameters = sub { # $rec|undef ($attrib, $pathname) | ||
| 355 | my ( $self, $attrib, $pathname ) = @_; | ||
| 356 | assert ( @_ == 3 ); | ||
| 357 | assert ( is_Object $self ); | ||
| 358 | assert ( is_PositiveOrZeroInt $attrib ); | ||
| 359 | assert ( is_Str $pathname ); | ||
| 360 | if ( $self->{hFindFile} != INVALID_HANDLE_VALUE ) { | ||
| 361 | $self->$close(); | ||
| 362 | } | ||
| 363 | $self->{fileName} = $pathname; | ||
| 364 | $self->{searchAttr} = $attrib; | ||
| 365 | |||
| 366 | return $self->$open(); | ||
| 367 | }; | ||
| 368 | |||
| 369 | $attrMatch = sub { # $bool ($attrib) | ||
| 370 | my ( $self, $attrib ) = @_; | ||
| 371 | assert ( @_ == 2 ); | ||
| 372 | assert ( is_Object $self ); | ||
| 373 | assert ( is_PositiveOrZeroInt $attrib ); | ||
| 374 | assert ( is_Int $self->{searchAttr} ); | ||
| 375 | return ( ( $self->{searchAttr} & _A_VOLID ) && ( $attrib & _A_VOLID ) ) | ||
| 376 | || !( $attrib & SPECIAL_BITS ) | ||
| 377 | || ( $self->{searchAttr} & $attrib & SPECIAL_BITS ); | ||
| 378 | }; | ||
| 379 | |||
| 380 | $cvtAttr = sub { # $attr ($findData, $filename) | ||
| 381 | my ( $self, $findData, $filename ) = @_; | ||
| 382 | assert ( @_ == 3 ); | ||
| 383 | assert ( is_Object $self ); | ||
| 384 | assert ( is_Str $findData ); | ||
| 385 | assert ( is_Str $filename ); | ||
| 386 | my $attr = unpack 'V' => bytes::substr( $findData, 0, DWORD_SIZE ); | ||
| 387 | if ( $filename && substr( $filename, 0, 1 ) eq '.' ) { | ||
| 388 | $attr |= _A_HIDDEN; | ||
| 389 | } | ||
| 390 | return $attr; | ||
| 391 | }; | ||
| 392 | |||
| 393 | $cvtTime = sub { # void ($findData, $fileinfo) | ||
| 394 | my ( $self, $findData, $fileinfo ) = @_; | ||
| 395 | assert ( @_ == 3 ); | ||
| 396 | assert ( is_Object $self ); | ||
| 397 | assert ( is_Str $findData ); | ||
| 398 | assert ( is_ArrayLike $fileinfo ); | ||
| 399 | my $fileTime = bytes::substr( $findData, ftLastWriteTime, FILETIME_SIZE ); | ||
| 400 | my $localTime = "\0" x FILETIME_SIZE; | ||
| 401 | $FileTimeToLocalFileTime->Call( $fileTime, $localTime ); | ||
| 402 | my $lpFatDate = pack 'S' => 0; | ||
| 403 | my $lpFatTime = pack 'S' => 0; | ||
| 404 | $FileTimeToDosDateTime->Call( $localTime, $lpFatDate, $lpFatTime ); | ||
| 405 | $fileinfo->[wr_date] = unpack 'S' => $lpFatDate; | ||
| 406 | $fileinfo->[wr_time] = unpack 'S' => $lpFatTime; | ||
| 407 | return; | ||
| 408 | }; | ||
| 409 | |||
| 410 | 1 | ||
| 411 | |||
| 412 | __END__ |