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

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

E-mail: