--- parser3/src/main/pa_dir.C 2016/12/28 22:50:07 1.27 +++ parser3/src/main/pa_dir.C 2026/11/27 21:35:11 1.43 @@ -1,19 +1,27 @@ /** @file Parser: directory scanning for different OS-es. - Copyright (c) 2000-2015 Art. Lebedev Studio (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #include "pa_common.h" +#include "pa_file.h" #include "pa_dir.h" +#include "pa_os.h" +#include "pa_request.h" +#include "pa_convert_utf.h" -volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.27 2016/12/28 22:50:07 moko Exp $" IDENT_PA_DIR_H; +volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.43 2026/11/27 21:35:11 moko Exp $" IDENT_PA_DIR_H; #ifdef _MSC_VER +const UTF16* pa_utf16_encode(const char* in, Charset& source_charset); +const char* pa_utf16_decode(const UTF16* in, Charset& asked_charset); + #define TICKS_PER_SECOND 10000000ULL #define EPOCH_DIFFERENCE 11644473600ULL + time_t filetime_to_timet(FILETIME const& ft){ ULARGE_INTEGER ull; ull.LowPart = ft.dwLowDateTime; @@ -25,41 +33,56 @@ time_t filetime_to_timet(FILETIME const& bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) { char mask[MAXPATH]; - snprintf(mask, MAXPATH, "%s/*.*", _pathname); + pa_snprintf(mask, MAXPATH, "%s/*.*", _pathname); + + const UTF16* utf16mask=pa_utf16_encode(mask, pa_thread_request().charsets.source()); + if(!utf16mask) { // not UTF-8, there can't be such a directory + errno=ENOENT; + return false; + } - _ffblk->handle=FindFirstFile(mask, (_WIN32_FIND_DATAA *)_ffblk); - return _ffblk->handle==INVALID_HANDLE_VALUE; + _ffblk->handle=FindFirstFileW((const wchar_t *)utf16mask, &_ffblk->stat); + return !pa_set_errno(_ffblk->handle!=INVALID_HANDLE_VALUE); // errno as opendir sets it } bool findnext(struct ffblk *_ffblk) { - return !FindNextFile(_ffblk->handle, (_WIN32_FIND_DATAA *)_ffblk); + return !FindNextFileW(_ffblk->handle, &_ffblk->stat); } void findclose(struct ffblk *_ffblk) { FindClose(_ffblk->handle); } +const char *ffblk::name() { + return pa_utf16_decode((const UTF16*)stat.cFileName, pa_thread_request().charsets.source()); +} + bool ffblk::is_dir(bool) { - return (ff_attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; + return (stat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; +} + +bool ffblk::is_symlink() { + return (stat.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) + && (stat.dwReserved0 == IO_REPARSE_TAG_SYMLINK || stat.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT); // junctions as well, but not the other reparse points [OneDrive, dedup] } double ffblk::size() { ULARGE_INTEGER ull; - ull.LowPart = nFileSizeLow; - ull.HighPart = nFileSizeHigh; + ull.LowPart = stat.nFileSizeLow; + ull.HighPart = stat.nFileSizeHigh; return (double)ull.QuadPart; } time_t ffblk::c_timestamp() { - return filetime_to_timet(ftCreationTime); + return filetime_to_timet(stat.ftCreationTime); } time_t ffblk::m_timestamp() { - return filetime_to_timet(ftLastWriteTime); + return filetime_to_timet(stat.ftLastWriteTime); } time_t ffblk::a_timestamp() { - return filetime_to_timet(ftLastAccessTime); + return filetime_to_timet(stat.ftLastAccessTime); } #else @@ -67,20 +90,19 @@ time_t ffblk::a_timestamp() { bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) { _ffblk->filePath=_pathname; if(!(_ffblk->dir=opendir(_ffblk->filePath))) - return true; + return true; return findnext(_ffblk); } bool findnext(struct ffblk *_ffblk) { - while(true) { - struct dirent *entry=readdir(_ffblk->dir); - if(!entry) - return true; - - strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name)-1); - _ffblk->ff_name[sizeof(_ffblk->ff_name)-1]=0; - + while(true) { + struct dirent *entry=readdir(_ffblk->dir); + if(!entry) + return true; + + pa_strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name)); + #ifdef HAVE_STRUCT_DIRENT_D_TYPE // http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html _ffblk->_d_type=entry->d_type; @@ -95,7 +117,7 @@ void findclose(struct ffblk *_ffblk) { void ffblk::stat_file() { char fileSpec[MAXPATH]; - snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name); + pa_snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name); if(pa_stat(fileSpec, &_st) != 0) { memset(&_st,0,sizeof(_st)); @@ -104,13 +126,25 @@ void ffblk::stat_file() { bool ffblk::is_dir(bool stat) { #ifdef HAVE_STRUCT_DIRENT_D_TYPE - if(!stat && _d_type != DT_UNKNOWN) + if(!stat && _d_type != DT_UNKNOWN && _d_type != DT_LNK) return _d_type == DT_DIR; #endif stat_file(); return S_ISDIR(_st.st_mode) != 0; } +bool ffblk::is_symlink() { +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + if(_d_type != DT_UNKNOWN) + return _d_type == DT_LNK; +#endif + char fileSpec[MAXPATH]; + pa_snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name); + + struct stat st; + return lstat(fileSpec, &st) == 0 && S_ISLNK(st.st_mode) != 0; +} + double ffblk::size() { return (double)_st.st_size; }