--- parser3/src/main/pa_dir.C 2017/02/06 16:17:12 1.28 +++ parser3/src/main/pa_dir.C 2026/11/27 21:35:11 1.43 @@ -1,16 +1,18 @@ /** @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.28 2017/02/06 16:17:12 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 @@ -31,12 +33,16 @@ 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=FindFirstFileW((const wchar_t *)utf16mask, &_ffblk->stat); - return _ffblk->handle==INVALID_HANDLE_VALUE; + return !pa_set_errno(_ffblk->handle!=INVALID_HANDLE_VALUE); // errno as opendir sets it } bool findnext(struct ffblk *_ffblk) { @@ -55,6 +61,11 @@ bool ffblk::is_dir(bool) { 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 = stat.nFileSizeLow; @@ -90,9 +101,8 @@ bool findnext(struct ffblk *_ffblk) { 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; - + 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; @@ -107,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)); @@ -116,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; }