--- parser3/src/main/pa_file.C 2026/09/27 17:37:51 1.2 +++ parser3/src/main/pa_file.C 2026/09/30 17:45:25 1.7 @@ -16,7 +16,7 @@ #include #endif -volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.2 2026/09/27 17:37:51 moko Exp $" IDENT_PA_FILE_H; +volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.7 2026/09/30 17:45:25 moko Exp $" IDENT_PA_FILE_H; // some maybe-undefined constants @@ -45,11 +45,46 @@ bool is_os_absolute_path(const String& p bool is_os_absolute_path(const char* path) { return path[0] && path[0]!=':' && path[1]==':' // DRIVE: (including drive-relative paths, for compatibility) - || path[0]=='\\' && path[1]=='\\' // UNC - || path[0]=='/' && path[1]=='/'; + || path[0]=='\\' && path[1]=='\\'; // UNC, only '\\' is supported as '//' is a document root relative path } #endif +uint path_scheme(const String& path) { + if(path.starts_with("file://")) + return PA_SCHEME_FILE; + if(path.starts_with("http://")) + return PA_SCHEME_HTTP; + if(path.starts_with("parser://")) + return PA_SCHEME_PARSER; + return PA_SCHEME_NONE; +} + +void check_path_scheme(uint scheme, const String& path, uint allowed) { + if(scheme!=PA_SCHEME_FILE && !(allowed&scheme)) + throw Exception(PARSER_RUNTIME, &path, "unsupported path scheme"); +} + +const String* file_uri_to_path(const String& uri) { + const String* rest=&uri.mid(7 /* "file://" */, uri.length()); + if(rest->starts_with("localhost/")) + rest=&rest->mid(9 /* "localhost", keeping the slash */, rest->length()); + if(rest->first_char()=='/') { + // file:///C:/path and file://localhost/C:/path: the slash before a drive name is not a part of the path + const String& disk=rest->mid(1, rest->length()); + return is_os_absolute_path(disk) ? &disk : rest; + } + if(is_os_absolute_path(*rest)) + return rest; // file://C:/path, not a standard form but in use +#ifdef WIN32 + if(rest->pos('/')!=STRING_NOT_FOUND) { // file://server/share/path is the UNC path \\server\share\path + String& result=*new String("//"); + result << *rest; + return &result; + } +#endif + return 0; +} + // externs const UTF16* pa_utf16_encode(const char* in, Charset& source_charset); @@ -117,11 +152,11 @@ int pa_get_valid_file_options_count(Hash struct File_read_action_info { char **data; size_t *data_size; char* buf; uint64_t offset; size_t limit; -}; +}; #endif static void file_read_action(struct stat& finfo, int f, const String& file_spec, void *context) { - File_read_action_info& info = *static_cast(context); + File_read_action_info& info = *static_cast(context); 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); if(to_read_size) { if(info.offset) @@ -148,7 +183,7 @@ File_read_result file_read_binary(const return result; } -File_read_result file_read(Request_charsets& charsets, const String& file_spec, +File_read_result file_read(Request_charsets& charsets, const String& file_spec, bool as_text, HashStringValue *options, bool fail_on_read_problem, size_t offset = 0, size_t limit = 0, bool transcode_text_result = true) { @@ -159,9 +194,9 @@ File_read_result file_read(Request_chars throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); } - File_read_action_info info = {&result.str, &result.length, 0, offset, limit}; + File_read_action_info info = {&result.str, &result.length, 0, offset, limit}; - result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); + result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem); if(as_text){ if(result.success){ @@ -187,7 +222,7 @@ File_read_result file_read(Request_chars return result; } -File_read_result file_load(Request& r, const String& file_spec, +File_read_result file_load(Request& r, const String& file_spec, bool as_text, HashStringValue *options, bool fail_on_read_problem, bool transcode_text_result) { @@ -252,10 +287,10 @@ bool file_read_action_under_lock(const S // first open, next stat: // directory update of NTFS hard links performed on open. - // ex: + // ex: // a.html:^test[] and b.html hardlink to a.html // user inserts ! before ^test in a.html - // directory entry of b.html in NTFS not updated at once, + // directory entry of b.html in NTFS not updated at once, // they delay update till open, so we would receive "!^test[" string // if would do stat, next open. // later: it seems, even this does not help sometimes @@ -272,15 +307,15 @@ bool file_read_action_under_lock(const S check_safe_mode(finfo, file_spec, fname); - action(finfo, f, file_spec, context); + action(finfo, f, file_spec, context); } catch(...) { - pa_unlock(f);close(f); + pa_unlock(f);close(f); if(fail_on_read_problem) rethrow; return false; - } + } - pa_unlock(f);close(f); + pa_unlock(f);close(f); return true; } else { if(fail_on_read_problem) @@ -294,7 +329,7 @@ void create_dir_for_file(const String& f const char *str=file_spec.taint_cstr(String::L_FILE_SPEC); if(str[0]){ const char *pos=str+1; - while((pos=strpbrk(pos, "/\\")) && pos[1]) { // to avoid trailing /, see #1166 + while((pos=strchr(pos, '/')) && pos[1]) { // to avoid trailing /, see #1166 pa_mkdir(pa_strdup(str,pos-str), 0775); pos++; } @@ -382,8 +417,8 @@ void file_write( Request_charsets& charsets, const String& file_spec, const char* data, - size_t size, - bool as_text, + size_t size, + bool as_text, bool do_append, Charset* asked_charset) { @@ -410,7 +445,7 @@ static size_t get_dir(char* fname, size_ size_t pos=0; for(pos=helper_length; pos; pos--){ char c=fname[pos-1]; - if(c=='/' || c=='\\'){ + if(c=='/'){ fname[pos-1]=0; dir=true; } else if(dir) break; @@ -427,7 +462,7 @@ bool entry_exists(const char* fname, str } bool entry_exists(const String& file_spec) { - return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0); + return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0); } static bool entry_ifdir(char *fname, bool need_dir) { @@ -435,7 +470,7 @@ static bool entry_ifdir(char *fname, boo size_t size=strlen(fname); while(size) { char c=fname[size-1]; - if(c=='/' || c=='\\') + if(c=='/') fname[--size]=0; else break; @@ -476,7 +511,7 @@ static void rmdir(const String& file_spe } bool file_delete(const String& file_spec, bool fail_on_problem, bool keep_empty_dirs) { - const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); + const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); if(pa_unlink(fname)!=0) { if(fail_on_problem) throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, @@ -486,37 +521,37 @@ bool file_delete(const String& file_spec } if(!keep_empty_dirs) - rmdir(file_spec, 1); + rmdir(file_spec, 1); return true; } void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs) { - const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC); - const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC); + const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC); + const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC); - create_dir_for_file(new_spec); + create_dir_for_file(new_spec); if(pa_rename(old_spec_cstr, new_spec_cstr)!=0) throw Exception(errno==EACCES ? "file.access" : errno==ENOENT ? "file.missing" : 0, &old_spec, "rename failed: %s (%d), actual filename '%s' to '%s'", strerror(errno), errno, old_spec_cstr, new_spec_cstr); if(!keep_empty_dirs) - rmdir(old_spec, 1); + rmdir(old_spec, 1); } bool file_exist(const String& file_spec) { - return entry_ifdir(file_spec, false); + return entry_ifdir(file_spec, false); } bool dir_exists(const String& file_spec) { - return entry_ifdir(file_spec, true); + return entry_ifdir(file_spec, true); } const String* file_exist(const String& path, const String& name) { String& result=*new String(path); - if(path.last_char() != '/' && path.last_char() != '\\') + if(path.last_char() != '/') result << "/"; result << name; return file_exist(result)?&result:0; @@ -527,7 +562,7 @@ bool file_executable(const String& file_ } bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem) { - const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); + const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC); struct stat finfo; if(pa_stat(fname, &finfo)!=0) { if(fail_on_read_problem) @@ -548,7 +583,7 @@ size_t check_file_size(uint64_t size, co return (size_t)size; } -/** +/** String related functions */