Annotation of parser3/src/main/pa_dir.C, revision 1.43

1.1       paf         1: /** @file
                      2:        Parser: directory scanning for different OS-es.
                      3: 
1.34      moko        4:        Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.32      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.12      paf         6: */
1.1       paf         7: 
1.9       paf         8: #include "pa_common.h"
1.38      moko        9: #include "pa_file.h"
1.39      moko       10: #include "pa_dir.h"
1.40      moko       11: #include "pa_os.h"
1.28      moko       12: #include "pa_request.h"
                     13: #include "pa_convert_utf.h"
1.1       paf        14: 
1.43    ! moko       15: volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.42 2026/10/18 21:05:35 moko Exp $" IDENT_PA_DIR_H;
1.21      moko       16: 
1.24      moko       17: #ifdef _MSC_VER
1.1       paf        18: 
1.28      moko       19: const UTF16* pa_utf16_encode(const char* in, Charset& source_charset);
                     20: const char* pa_utf16_decode(const UTF16* in, Charset& asked_charset);
                     21: 
1.22      moko       22: #define TICKS_PER_SECOND 10000000ULL
                     23: #define EPOCH_DIFFERENCE 11644473600ULL
1.28      moko       24: 
1.22      moko       25: time_t filetime_to_timet(FILETIME const& ft){
                     26:        ULARGE_INTEGER ull;
                     27:        ull.LowPart = ft.dwLowDateTime;
                     28:        ull.HighPart = ft.dwHighDateTime;
                     29:        long long secs=(ull.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE);
                     30:        time_t t =(time_t)secs;
                     31:        return (secs == (long long)t) ? t : 0;
                     32: }
                     33: 
1.16      paf        34: bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) {
1.1       paf        35:        char mask[MAXPATH];
1.42      moko       36:        pa_snprintf(mask, MAXPATH, "%s/*.*", _pathname);
1.1       paf        37: 
1.28      moko       38:        const UTF16* utf16mask=pa_utf16_encode(mask, pa_thread_request().charsets.source());
1.41      moko       39:        if(!utf16mask) { // not UTF-8, there can't be such a directory
                     40:                errno=ENOENT;
                     41:                return false;
                     42:        }
1.28      moko       43: 
                     44:        _ffblk->handle=FindFirstFileW((const wchar_t *)utf16mask, &_ffblk->stat);
1.40      moko       45:        return !pa_set_errno(_ffblk->handle!=INVALID_HANDLE_VALUE); // errno as opendir sets it
1.1       paf        46: }
                     47: 
                     48: bool findnext(struct ffblk *_ffblk) {
1.28      moko       49:        return !FindNextFileW(_ffblk->handle, &_ffblk->stat);
1.22      moko       50: }
1.1       paf        51: 
                     52: void findclose(struct ffblk *_ffblk) {
                     53:        FindClose(_ffblk->handle);
                     54: }
                     55: 
1.28      moko       56: const char *ffblk::name() {
                     57:        return pa_utf16_decode((const UTF16*)stat.cFileName, pa_thread_request().charsets.source());
                     58: }
                     59: 
1.25      moko       60: bool ffblk::is_dir(bool) {
1.28      moko       61:        return (stat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
1.22      moko       62: }
                     63: 
1.43    ! moko       64: bool ffblk::is_symlink() {
        !            65:        return (stat.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
        !            66:                && (stat.dwReserved0 == IO_REPARSE_TAG_SYMLINK || stat.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT); // junctions as well, but not the other reparse points [OneDrive, dedup]
        !            67: }
        !            68: 
1.22      moko       69: double ffblk::size() {
                     70:        ULARGE_INTEGER ull;
1.28      moko       71:        ull.LowPart = stat.nFileSizeLow;
                     72:        ull.HighPart = stat.nFileSizeHigh;
1.22      moko       73:        return (double)ull.QuadPart;
                     74: }
                     75: 
                     76: time_t ffblk::c_timestamp() {
1.28      moko       77:        return filetime_to_timet(stat.ftCreationTime);
1.22      moko       78: }
                     79: 
                     80: time_t ffblk::m_timestamp() {
1.28      moko       81:        return filetime_to_timet(stat.ftLastWriteTime);
1.22      moko       82: }
                     83: 
                     84: time_t ffblk::a_timestamp() {
1.28      moko       85:        return filetime_to_timet(stat.ftLastAccessTime);
1.22      moko       86: }
                     87: 
1.1       paf        88: #else
                     89: 
1.18      paf        90: bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) {
1.23      moko       91:        _ffblk->filePath=_pathname;
1.1       paf        92:        if(!(_ffblk->dir=opendir(_ffblk->filePath)))
1.28      moko       93:                return true;
1.1       paf        94: 
                     95:        return findnext(_ffblk);
                     96: }
                     97: 
                     98: bool findnext(struct ffblk *_ffblk) {
1.28      moko       99:        while(true) {
                    100:                struct dirent *entry=readdir(_ffblk->dir);
                    101:                if(!entry)
                    102:                        return true;
1.1       paf       103: 
1.31      moko      104:                pa_strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name));
                    105: 
1.22      moko      106: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
                    107:                // http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
                    108:                _ffblk->_d_type=entry->d_type;
                    109: #endif
1.1       paf       110:                return false;
                    111:     }
                    112: }
                    113: 
                    114: void findclose(struct ffblk *_ffblk) {
                    115:        closedir(_ffblk->dir);
                    116: }
                    117: 
1.22      moko      118: void ffblk::stat_file() {
                    119:        char fileSpec[MAXPATH];
1.42      moko      120:        pa_snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name);
1.22      moko      121:        
1.27      moko      122:        if(pa_stat(fileSpec, &_st) != 0) {
1.22      moko      123:                memset(&_st,0,sizeof(_st));
                    124:        }
                    125: }
                    126: 
1.25      moko      127: bool ffblk::is_dir(bool stat) {
1.22      moko      128: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
1.43    ! moko      129:        if(!stat && _d_type != DT_UNKNOWN && _d_type != DT_LNK)
1.25      moko      130:                return _d_type == DT_DIR;
                    131: #endif
                    132:        stat_file();
1.22      moko      133:        return S_ISDIR(_st.st_mode) != 0;
                    134: }
                    135: 
1.43    ! moko      136: bool ffblk::is_symlink() {
        !           137: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
        !           138:        if(_d_type != DT_UNKNOWN)
        !           139:                return _d_type == DT_LNK;
        !           140: #endif
        !           141:        char fileSpec[MAXPATH];
        !           142:        pa_snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name);
        !           143: 
        !           144:        struct stat st;
        !           145:        return lstat(fileSpec, &st) == 0 && S_ISLNK(st.st_mode) != 0;
        !           146: }
        !           147: 
1.22      moko      148: double ffblk::size() {
                    149:        return (double)_st.st_size;
                    150: }
                    151: 
                    152: time_t ffblk::c_timestamp() {
                    153:        return _st.st_ctime;
                    154: }
                    155: 
                    156: time_t ffblk::m_timestamp() {
                    157:        return _st.st_mtime;
                    158: }
                    159: 
                    160: time_t ffblk::a_timestamp() {
                    161:        return _st.st_atime;
                    162: }
                    163: 
1.1       paf       164: #endif

E-mail: