Annotation of parser3/src/main/pa_file.C, revision 1.10

1.1       moko        1: /** @file
                      2:        Parser: file and file system related functions.
                      3: 
                      4:        Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
                      6: */
                      7: 
                      8: #include "pa_file.h"
                      9: #include "pa_dir.h"
                     10: #include "pa_charsets.h"
                     11: #include "pa_http.h"
                     12: #include "pa_convert_utf.h"
                     13: 
                     14: #ifdef _MSC_VER
                     15: #include <windows.h>
                     16: #include <direct.h>
                     17: #endif
                     18: 
1.10    ! moko       19: volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.9 2026/10/01 12:46:17 moko Exp $" IDENT_PA_FILE_H;
1.8       moko       20: 
                     21: // a tainted path scheme (file://, http:// or parser://) would be accepted
                     22: //#define IGNORE_FILE_PREFIX_LANGUAGES
1.1       moko       23: 
                     24: // some maybe-undefined constants
                     25: 
                     26: #ifndef _O_TEXT
                     27: #      define _O_TEXT 0
                     28: #endif
                     29: #ifndef _O_BINARY
                     30: #      define _O_BINARY 0
                     31: #endif
                     32: 
                     33: #ifdef HAVE_FTRUNCATE
                     34: #      define PA_O_TRUNC 0
                     35: #else
                     36: #      ifdef _O_TRUNC
                     37: #              define PA_O_TRUNC _O_TRUNC
                     38: #      else
                     39: #              error you must have either ftruncate function or _O_TRUNC bit declared
                     40: #      endif
                     41: #endif
                     42: 
                     43: #ifdef WIN32
                     44: 
                     45: bool is_os_absolute_path(const String& path) {
                     46:        return is_os_absolute_path(path.cstr());
                     47: }
                     48: 
                     49: bool is_os_absolute_path(const char* path) {
                     50:        return path[0] && path[0]!=':' && path[1]==':' // DRIVE: (including drive-relative paths, for compatibility)
1.4       moko       51:                || path[0]=='\\' && path[1]=='\\';     // UNC, only '\\' is supported as '//' is a document root relative path
1.1       moko       52: }
                     53: #endif
                     54: 
1.5       moko       55: uint path_scheme(const String& path) {
                     56:        if(path.starts_with("file://"))
                     57:                return PA_SCHEME_FILE;
                     58:        if(path.starts_with("http://"))
                     59:                return PA_SCHEME_HTTP;
                     60:        if(path.starts_with("parser://"))
                     61:                return PA_SCHEME_PARSER;
                     62:        return PA_SCHEME_NONE;
                     63: }
                     64: 
1.9       moko       65: bool clean_path_scheme(uint scheme, const String& path) {
                     66: #ifdef IGNORE_FILE_PREFIX_LANGUAGES
                     67:        return true;
                     68: #else
                     69:        return path.check_lang(String::L_AS_IS, 0, scheme==PA_SCHEME_PARSER ? 9 /* "parser://" */ : 7 /* "file://" or "http://" */);
                     70: #endif
                     71: }
                     72: 
1.5       moko       73: void check_path_scheme(uint scheme, const String& path, uint allowed) {
                     74:        if(scheme!=PA_SCHEME_FILE && !(allowed&scheme))
                     75:                throw Exception(PARSER_RUNTIME, &path, "unsupported path scheme");
1.9       moko       76:        if(!clean_path_scheme(scheme, path))
1.8       moko       77:                throw Exception(PARSER_RUNTIME, &path, "tainted path scheme");
1.5       moko       78: }
                     79: 
1.10    ! moko       80: int path_cmp(const char* path, const char* dir, size_t length) {
        !            81: #ifdef WIN32
        !            82:        return strncasecmp(path, dir, length); // mostly for the drive letter
        !            83: #else
        !            84:        return strncmp(path, dir, length);
        !            85: #endif
        !            86: }
        !            87: 
1.5       moko       88: const String* file_uri_to_path(const String& uri) {
                     89:        const String* rest=&uri.mid(7 /* "file://" */, uri.length());
                     90:        if(rest->starts_with("localhost/"))
                     91:                rest=&rest->mid(9 /* "localhost", keeping the slash */, rest->length());
                     92:        if(rest->first_char()=='/') {
                     93:                // file:///C:/path and file://localhost/C:/path: the slash before a drive name is not a part of the path
                     94:                const String& disk=rest->mid(1, rest->length());
                     95:                return is_os_absolute_path(disk) ? &disk : rest;
                     96:        }
                     97:        if(is_os_absolute_path(*rest))
                     98:                return rest; // file://C:/path, not a standard form but in use
1.7       moko       99: #ifdef WIN32
                    100:        if(rest->pos('/')!=STRING_NOT_FOUND) { // file://server/share/path is the UNC path \\server\share\path
                    101:                String& result=*new String("//");
                    102:                result << *rest;
                    103:                return &result;
                    104:        }
                    105: #endif
1.5       moko      106:        return 0;
                    107: }
                    108: 
1.1       moko      109: // externs
                    110: 
                    111: const UTF16* pa_utf16_encode(const char* in, Charset& source_charset);
                    112: 
                    113: // functions
                    114: 
                    115: #ifdef _MSC_VER
                    116: 
                    117: #define PA_UTF16_ENC(value) (const wchar_t *)pa_utf16_encode(value, pa_thread_request().charsets.source())
                    118: 
                    119: int pa_stat(const char *pathname, struct stat *buffer){
                    120:        return _wstat64(PA_UTF16_ENC(pathname), buffer);
                    121: }
                    122: 
                    123: int pa_open(const char *pathname, int flags, int mode){
                    124:        return _wopen(PA_UTF16_ENC(pathname), flags, mode);
                    125: }
                    126: 
                    127: FILE *pa_fopen(const char *pathname, const char *mode){
                    128:        return _wfopen(PA_UTF16_ENC(pathname), PA_UTF16_ENC(mode));
                    129: }
                    130: 
                    131: int pa_mkdir(const char *pathname, int){
                    132:        return _wmkdir(PA_UTF16_ENC(pathname));
                    133: }
                    134: 
                    135: int pa_rmdir(const char *pathname){
                    136:        return _wrmdir(PA_UTF16_ENC(pathname));
                    137: }
                    138: 
                    139: int pa_rename(const char *oldpath, const char *newpath){
                    140:        return _wrename(PA_UTF16_ENC(oldpath), PA_UTF16_ENC(newpath));
                    141: }
                    142: 
                    143: int pa_unlink(const char *pathname){
                    144:        return _wunlink(PA_UTF16_ENC(pathname));
                    145: }
                    146: 
                    147: #else
                    148: 
                    149: #define pa_mkdir mkdir
                    150: #define pa_rmdir rmdir
                    151: #define pa_rename rename
                    152: #define pa_unlink unlink
                    153: 
                    154: #endif
                    155: 
                    156: /// these options were handled but not checked elsewhere, now check them
                    157: int pa_get_valid_file_options_count(HashStringValue& options) {
                    158:        int result=0;
                    159:        if(options.get(PA_SQL_LIMIT_NAME))
                    160:                result++;
                    161:        if(options.get(PA_SQL_OFFSET_NAME))
                    162:                result++;
                    163:        if(options.get(PA_COLUMN_SEPARATOR_NAME))
                    164:                result++;
                    165:        if(options.get(PA_COLUMN_ENCLOSER_NAME))
                    166:                result++;
                    167:        if(options.get(PA_CHARSET_NAME))
                    168:                result++;
                    169:        return result;
                    170: }
                    171: 
                    172: #ifndef DOXYGEN
                    173: struct File_read_action_info {
                    174:        char **data; size_t *data_size;
                    175:        char* buf; uint64_t offset; size_t limit;
1.3       moko      176: };
1.1       moko      177: #endif
                    178: 
                    179: static void file_read_action(struct stat& finfo, int f, const String& file_spec, void *context) {
1.3       moko      180:        File_read_action_info& info = *static_cast<File_read_action_info *>(context);
1.1       moko      181:        size_t to_read_size = check_file_size(info.limit && info.limit < (size_t)finfo.st_size ? info.limit : (size_t)finfo.st_size, &file_spec);
                    182:        if(to_read_size) {
                    183:                if(info.offset)
                    184:                         pa_lseek(f, info.offset, SEEK_SET); // seek never fails as POSIX allows the file offset to be set beyond the EOF
                    185:                *info.data = info.buf ? info.buf : (char *)pa_malloc_atomic(to_read_size+1);
                    186:                ssize_t result = read(f, *info.data, to_read_size);
                    187:                if(result<0)
                    188:                        throw Exception("file.read", &file_spec, "read failed: %s (%d)", strerror(errno), errno);
                    189:                *info.data_size = result;
                    190:        } else { // empty file
                    191:                // for both, text and binary: for text we need that terminator, for binary we need nonzero pointer to be able to save such files
                    192:                *info.data = (char *)pa_malloc_atomic(1);
                    193:                *(char*)(*info.data) = 0;
                    194:                *info.data_size = 0;
                    195:                return;
                    196:        }
                    197: }
                    198: 
                    199: File_read_result file_read_binary(const String& file_spec, bool fail_on_read_problem, char* buf, uint64_t offset, size_t limit) {
                    200:        File_read_result result = {false, 0, 0, 0};
                    201:        File_read_action_info info = {&result.str, &result.length, buf, offset, limit};
                    202: 
                    203:        result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, 0, fail_on_read_problem);
                    204:        return result;
                    205: }
                    206: 
1.3       moko      207: File_read_result file_read(Request_charsets& charsets, const String& file_spec,
1.1       moko      208:                        bool as_text, HashStringValue *options,
                    209:                        bool fail_on_read_problem,
                    210:                        size_t offset = 0, size_t limit = 0, bool transcode_text_result = true) {
                    211:        File_read_result result = {false, 0, 0, 0};
                    212:        if(options){
                    213:                int valid_options = pa_get_valid_file_options_count(*options);
                    214:                if(valid_options != options->count())
                    215:                        throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                    216:        }
                    217: 
1.3       moko      218:        File_read_action_info info = {&result.str, &result.length, 0, offset, limit};
1.1       moko      219: 
1.3       moko      220:        result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem);
1.1       moko      221: 
                    222:        if(as_text){
                    223:                if(result.success){
                    224:                        Charset* asked_charset = 0;
                    225:                        if(options)
                    226:                                if(Value* vcharset_name = options->get(PA_CHARSET_NAME))
                    227:                                        asked_charset = &pa_charsets.get(vcharset_name->as_string());
                    228: 
                    229:                        asked_charset = pa_charsets.checkBOM(result.str, result.length, asked_charset);
                    230: 
                    231:                        if(result.length && transcode_text_result && asked_charset){ // length must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below
                    232:                                String::C body = String::C(result.str, result.length);
                    233:                                body=Charset::transcode(body, *asked_charset, charsets.source());
                    234: 
                    235:                                result.str = const_cast<char*>(body.str); // hacking a little
                    236:                                result.length = body.length;
                    237:                        }
                    238:                }
                    239:                if(result.length)
                    240:                        fix_line_breaks(result.str, result.length);
                    241:        }
                    242: 
                    243:        return result;
                    244: }
                    245: 
1.3       moko      246: File_read_result file_load(Request& r, const String& file_spec,
1.1       moko      247:                        bool as_text, HashStringValue *options,
                    248:                        bool fail_on_read_problem,
                    249:                        bool transcode_text_result) {
                    250: 
                    251:        size_t offset = 0;
                    252:        size_t limit = 0;
                    253: 
                    254:        if(options){
                    255:                if(Value *voffset = (Value *)options->get(sql_offset_name))
                    256:                        offset = r.process(*voffset).as_int();
                    257:                if(Value *vlimit = (Value *)options->get(sql_limit_name))
                    258:                        limit = r.process(*vlimit).as_int();
                    259:                // no check on options count here
                    260:        }
                    261: 
                    262:        if(file_spec.starts_with("http://")) {
                    263:                if(offset || limit)
                    264:                        throw Exception(PARSER_RUNTIME, 0, "offset and load options are not supported for HTTP:// file load");
                    265: 
                    266:                // fail on read problem
                    267:                File_read_http_result http = pa_internal_file_read_http(r, file_spec, as_text, options, transcode_text_result);
                    268: 
                    269:                File_read_result result = {true, http.str, http.length, http.headers};
                    270:                return result;
                    271:        } else
                    272:                return file_read(r.charsets, file_spec, as_text, options, fail_on_read_problem, offset, limit, transcode_text_result);
                    273: }
                    274: 
                    275: char* file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem) {
                    276:        File_read_result file = file_read(charsets, file_spec, true, 0, fail_on_read_problem);
                    277:        return file.success ? file.str : 0;
                    278: }
                    279: 
                    280: char* file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem, HashStringValue* options, bool transcode_result) {
                    281:        File_read_result file = file_load(r, file_spec, true, options, fail_on_read_problem, transcode_result);
                    282:        return file.success ? file.str : 0;
                    283: }
                    284: 
                    285: #ifdef PA_SAFE_MODE
                    286: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname) {
                    287:        if(finfo.st_uid/*foreign?*/!=geteuid()
                    288:                && finfo.st_gid/*foreign?*/!=getegid())
                    289:                throw Exception(PARSER_RUNTIME,
                    290:                        &file_spec,
                    291:                        "parser is in safe mode: reading files of foreign group and user disabled "
                    292:                        "[recompile parser with --disable-safe-mode configure option], "
                    293:                        "actual filename '%s', fuid(%d)!=euid(%d) or fgid(%d)!=egid(%d)",
                    294:                        fname, finfo.st_uid, geteuid(), finfo.st_gid, getegid()
                    295:                );
                    296: }
                    297: #else
                    298: void check_safe_mode(struct stat, const String&, const char*) {
                    299: }
                    300: #endif
                    301: 
                    302: 
                    303: bool file_read_action_under_lock(const String& file_spec, const char* action_name, File_read_action action, void *context,
                    304:                                bool as_text, bool fail_on_read_problem) {
                    305: 
                    306:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
                    307:        int f;
                    308: 
                    309:        // first open, next stat:
                    310:        // directory update of NTFS hard links performed on open.
1.3       moko      311:        // ex:
1.1       moko      312:        //   a.html:^test[] and b.html hardlink to a.html
                    313:        //   user inserts ! before ^test in a.html
1.3       moko      314:        //   directory entry of b.html in NTFS not updated at once,
1.1       moko      315:        //   they delay update till open, so we would receive "!^test[" string
                    316:        //   if would do stat, next open.
                    317:        // later: it seems, even this does not help sometimes
                    318:        if((f=pa_open(fname, O_RDONLY | (as_text ? _O_TEXT : _O_BINARY) ))>=0) {
                    319:                try {
                    320:                        int pa_errno=pa_lock_shared_blocking(f);
                    321:                        if(pa_errno!=0)
                    322:                                throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname);
                    323: 
                    324:                        struct stat finfo;
                    325:                        if(pa_fstat(f, &finfo)!=0)
                    326:                                throw Exception("file.missing", // hardly possible: we just opened it OK
                    327:                                        &file_spec, "stat failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname);
                    328: 
                    329:                        check_safe_mode(finfo, file_spec, fname);
                    330: 
1.3       moko      331:                        action(finfo, f, file_spec, context);
1.1       moko      332:                } catch(...) {
1.3       moko      333:                        pa_unlock(f);close(f);
1.1       moko      334:                        if(fail_on_read_problem)
                    335:                                rethrow;
                    336:                        return false;
1.3       moko      337:                }
1.1       moko      338: 
1.3       moko      339:                pa_unlock(f);close(f);
1.1       moko      340:                return true;
                    341:        } else {
                    342:                if(fail_on_read_problem)
                    343:                        throw Exception(errno==EACCES ? "file.access" : (errno==ENOENT || errno==ENOTDIR || errno==ENODEV) ? "file.missing" : 0,
                    344:                                &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname);
                    345:                return false;
                    346:        }
                    347: }
                    348: 
                    349: void create_dir_for_file(const String& file_spec) {
                    350:        const char *str=file_spec.taint_cstr(String::L_FILE_SPEC);
                    351:        if(str[0]){
                    352:                const char *pos=str+1;
1.6       moko      353:                while((pos=strchr(pos, '/')) && pos[1]) { // to avoid trailing /, see #1166
1.1       moko      354:                        pa_mkdir(pa_strdup(str,pos-str), 0775);
                    355:                        pos++;
                    356:                }
                    357:        }
                    358: }
                    359: 
                    360: bool file_write_action_under_lock(const String& file_spec, const char* action_name, File_write_action action, void *context,
                    361:                                bool as_text, bool do_append, bool do_block, bool fail_on_lock_problem) {
                    362: 
                    363:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
                    364:        int f;
                    365:        if(access(fname, W_OK)!=0) // no
                    366:                create_dir_for_file(file_spec);
                    367: 
                    368:        if((f=pa_open(fname, O_CREAT | O_RDWR | (as_text ? _O_TEXT : _O_BINARY) | (do_append ? O_APPEND : PA_O_TRUNC), 0664))>=0) {
                    369:                int pa_errno=do_block ? pa_lock_exclusive_blocking(f) : pa_lock_exclusive_nonblocking(f);
                    370:                if(pa_errno!=0) {
                    371:                        Exception e("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname);
                    372:                        close(f);
                    373:                        if(fail_on_lock_problem)
                    374:                                throw e;
                    375:                        return false;
                    376:                }
                    377: 
                    378:                try {
                    379: #if (defined(HAVE_FCHMOD) && defined(PA_SAFE_MODE))
                    380:                        struct stat finfo;
                    381:                        if(pa_fstat(f, &finfo)==0 && finfo.st_mode & 0111)
                    382:                                fchmod(f, finfo.st_mode & 0666/*clear executable bits*/); // backward: ignore errors if any
                    383: #endif
                    384:                        action(f, context);
                    385:                } catch(...) {
                    386: #ifdef HAVE_FTRUNCATE
                    387:                        if(!do_append)
                    388:                                PA_UNUSED int ignore_result=ftruncate(f, lseek(f, 0, SEEK_CUR)); // one cannot use O_TRUNC, read lower
                    389: #endif
                    390:                        pa_unlock(f);close(f);
                    391:                        rethrow;
                    392:                }
                    393:                
                    394: #ifdef HAVE_FTRUNCATE
                    395:                if(!do_append)
                    396:                        PA_UNUSED int ignore_result=ftruncate(f, lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
                    397: #endif
                    398:                pa_unlock(f);close(f);
                    399:                return true;
                    400:        } else
                    401:                throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname);
                    402:        // here should be nothing, see rethrow above
                    403: }
                    404: 
                    405: #ifndef DOXYGEN
                    406: struct File_write_action_info {
                    407:        const char* str;
                    408:        size_t length;
                    409: };
                    410: #endif
                    411: 
                    412: // a single write() must stay below INT_MAX as windows _write() reports written bytes as int
                    413: #define FILE_WRITE_CHUNK_SIZE (256*0x100000)
                    414: 
                    415: static const char* bytes_left(size_t rest) {
                    416:        return rest ? pa_strcat(", ", pa_uitoa(rest), " bytes left") : "";
                    417: }
                    418: 
                    419: static void file_write_action(int f, void *context) {
                    420:        File_write_action_info& info=*static_cast<File_write_action_info *>(context);
                    421: 
                    422:        const char* data=info.str;
                    423:        size_t left=info.length;
                    424: 
                    425:        while(left) {
                    426:                size_t portion=min(left, (size_t)FILE_WRITE_CHUNK_SIZE);
                    427:                ssize_t written=write(f, data, portion);
                    428:                if(written<0)
                    429:                        throw Exception("file.write", 0, "error writing %s bytes: %s (%d)%s", pa_uitoa(portion), strerror(errno), errno, bytes_left(left-portion));
                    430:                if(written==0)
                    431:                        throw Exception("file.write", 0, "error writing %s bytes: zero bytes written%s", pa_uitoa(portion), bytes_left(left-portion));
                    432:                data+=written;
                    433:                left-=written;
                    434:        }
                    435: }
                    436: 
                    437: void file_write(
                    438:                                Request_charsets& charsets,
                    439:                                const String& file_spec,
                    440:                                const char* data,
1.3       moko      441:                                size_t size,
                    442:                                bool as_text,
1.1       moko      443:                                bool do_append,
                    444:                                Charset* asked_charset) {
                    445: 
                    446:        if(as_text && asked_charset){
                    447:                String::C body=String::C(data, size);
                    448:                body=Charset::transcode(body, charsets.source(), *asked_charset);
                    449:                data=body.str;
                    450:                size=body.length;
                    451:        };
                    452: 
                    453:        File_write_action_info info={data, size};
                    454: 
                    455:        file_write_action_under_lock(
                    456:                file_spec,
                    457:                "write",
                    458:                file_write_action,
                    459:                &info,
                    460:                as_text,
                    461:                do_append);
                    462: }
                    463: 
                    464: static size_t get_dir(char* fname, size_t helper_length){
                    465:        bool dir=false;
                    466:        size_t pos=0;
                    467:        for(pos=helper_length; pos; pos--){
                    468:                char c=fname[pos-1];
1.6       moko      469:                if(c=='/'){
1.1       moko      470:                        fname[pos-1]=0;
                    471:                        dir=true;
                    472:                } else if(dir) break;
                    473:        }
                    474:        return pos;
                    475: }
                    476: 
                    477: bool entry_exists(const char* fname, struct stat *afinfo) {
                    478:        struct stat lfinfo;
                    479:        bool result=pa_stat(fname, &lfinfo)==0;
                    480:        if(afinfo)
                    481:                *afinfo=lfinfo;
                    482:        return result;
                    483: }
                    484: 
                    485: bool entry_exists(const String& file_spec) {
1.3       moko      486:        return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0);
1.1       moko      487: }
                    488: 
                    489: static bool entry_ifdir(char *fname, bool need_dir) {
                    490:        if(need_dir){
                    491:                size_t size=strlen(fname);
                    492:                while(size) {
                    493:                        char c=fname[size-1];
1.6       moko      494:                        if(c=='/')
1.1       moko      495:                                fname[--size]=0;
                    496:                        else
                    497:                                break;
                    498:                }
                    499:        }
                    500: 
                    501:        struct stat finfo;
                    502:        if(entry_exists(fname, &finfo)) {
                    503:                bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
                    504:                return is_dir==need_dir;
                    505:        }
                    506:        return false;
                    507: }
                    508: 
                    509: static bool entry_ifdir(const String& file_spec, bool need_dir) {
                    510:        return entry_ifdir(file_spec.taint_cstrm(String::L_FILE_SPEC), need_dir);
                    511: }
                    512: 
                    513: // throws nothing! [this is required in file_move & file_delete]
                    514: static void rmdir(const String& file_spec, size_t pos_after) {
                    515:        char* dir_spec=file_spec.taint_cstrm(String::L_FILE_SPEC);
                    516:        size_t length=strlen(dir_spec);
                    517:        while( (length=get_dir(dir_spec, length)) && (length > pos_after) ){
                    518: #ifdef _MSC_VER
                    519:                if(!entry_ifdir(dir_spec, true))
                    520:                        break;
                    521:                DWORD attrs=GetFileAttributesW(PA_UTF16_ENC(dir_spec));
                    522:                if(
                    523:                        (attrs==INVALID_FILE_ATTRIBUTES)
                    524:                        || !(attrs & FILE_ATTRIBUTE_DIRECTORY)
                    525:                        || (attrs & FILE_ATTRIBUTE_REPARSE_POINT)
                    526:                )
                    527:                        break;
                    528: #endif
                    529:                if(pa_rmdir(dir_spec))
                    530:                        break;
                    531:        };
                    532: }
                    533: 
                    534: bool file_delete(const String& file_spec, bool fail_on_problem, bool keep_empty_dirs) {
1.3       moko      535:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.1       moko      536:        if(pa_unlink(fname)!=0) {
                    537:                if(fail_on_problem)
                    538:                        throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0,
                    539:                                &file_spec, "unlink failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname);
                    540:                else
                    541:                        return false;
                    542:        }
                    543: 
                    544:        if(!keep_empty_dirs)
1.3       moko      545:                rmdir(file_spec, 1);
1.1       moko      546: 
                    547:        return true;
                    548: }
                    549: 
                    550: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs) {
1.3       moko      551:        const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC);
                    552:        const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC);
1.1       moko      553:        
1.3       moko      554:        create_dir_for_file(new_spec);
1.1       moko      555: 
                    556:        if(pa_rename(old_spec_cstr, new_spec_cstr)!=0)
                    557:                throw Exception(errno==EACCES ? "file.access" : errno==ENOENT ? "file.missing" : 0,
                    558:                        &old_spec, "rename failed: %s (%d), actual filename '%s' to '%s'", strerror(errno), errno, old_spec_cstr, new_spec_cstr);
                    559: 
                    560:        if(!keep_empty_dirs)
1.3       moko      561:                rmdir(old_spec, 1);
1.1       moko      562: }
                    563: 
                    564: 
                    565: bool file_exist(const String& file_spec) {
1.3       moko      566:        return entry_ifdir(file_spec, false);
1.1       moko      567: }
                    568: 
                    569: bool dir_exists(const String& file_spec) {
1.3       moko      570:        return entry_ifdir(file_spec, true);
1.1       moko      571: }
                    572: 
                    573: const String* file_exist(const String& path, const String& name) {
                    574:        String& result=*new String(path);
1.6       moko      575:        if(path.last_char() != '/')
1.1       moko      576:                result << "/";
                    577:        result << name;
                    578:        return file_exist(result)?&result:0;
                    579: }
                    580: 
                    581: bool file_executable(const String& file_spec) {
                    582:        return access(file_spec.taint_cstr(String::L_FILE_SPEC), X_OK)==0;
                    583: }
                    584: 
                    585: bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem) {
1.3       moko      586:        const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.1       moko      587:        struct stat finfo;
                    588:        if(pa_stat(fname, &finfo)!=0) {
                    589:                if(fail_on_read_problem)
                    590:                        throw Exception("file.missing", &file_spec, "getting file size failed: %s (%d), real filename '%s'", strerror(errno), errno, fname);
                    591:                else
                    592:                        return false;
                    593:        }
                    594:        rsize=finfo.st_size;
                    595:        ratime=(time_t)finfo.st_atime;
                    596:        rmtime=(time_t)finfo.st_mtime;
                    597:        rctime=(time_t)finfo.st_ctime;
                    598:        return true;
                    599: }
                    600: 
                    601: size_t check_file_size(uint64_t size, const String* file_spec){
                    602:        if(size > (uint64_t)pa_file_size_limit)
1.2       moko      603:                throw Exception(PARSER_RUNTIME, file_spec, "content size of %s bytes exceeds the limit (%s bytes)", pa_uitoa(size), pa_uitoa(pa_file_size_limit));
1.1       moko      604:        return (size_t)size;
                    605: }
                    606: 
1.3       moko      607: /**
1.1       moko      608:        String related functions
                    609: */
                    610: 
                    611: const char *pa_filename(const char *path){
                    612:        const char *slash=strrpbrk(path, "/\\");
                    613:        return slash ? slash+1 : path;
                    614: }

E-mail: