--- parser3/src/main/pa_file.C 2026/09/28 00:23:20 1.3 +++ parser3/src/main/pa_file.C 2026/11/03 15:51:20 1.13 @@ -7,6 +7,7 @@ #include "pa_file.h" #include "pa_dir.h" +#include "pa_os.h" #include "pa_charsets.h" #include "pa_http.h" #include "pa_convert_utf.h" @@ -16,7 +17,10 @@ #include #endif -volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.3 2026/09/28 00:23:20 moko Exp $" IDENT_PA_FILE_H; +volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.13 2026/11/03 15:51:20 moko Exp $" IDENT_PA_FILE_H; + +// a tainted path scheme (file://, http:// or parser://) would be accepted +//#define IGNORE_FILE_PREFIX_LANGUAGES // some maybe-undefined constants @@ -45,47 +49,125 @@ 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 -// externs +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; +} -const UTF16* pa_utf16_encode(const char* in, Charset& source_charset); +bool clean_path_scheme(uint scheme, const String& path) { +#ifdef IGNORE_FILE_PREFIX_LANGUAGES + return true; +#else + return path.check_lang(String::L_AS_IS, 0, scheme==PA_SCHEME_PARSER ? 9 /* "parser://" */ : 7 /* "file://" or "http://" */); +#endif +} + +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"); + if(!clean_path_scheme(scheme, path)) + throw Exception(PARSER_RUNTIME, &path, "tainted path scheme"); +} + +int path_cmp(const char* path, const char* dir, size_t length) { +#ifdef WIN32 + return strncasecmp(path, dir, length); // mostly for the drive letter +#else + return strncmp(path, dir, length); +#endif +} + +const char* actual_filename(const String& file_spec, const char* fname) { + return strcmp(file_spec.cstr(), fname) ? pa_strcat(", actual filename '", fname, "'") : ""; +} + +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; +} // functions #ifdef _MSC_VER +// externs + +const UTF16* pa_utf16_encode(const char* in, Charset& source_charset); #define PA_UTF16_ENC(value) (const wchar_t *)pa_utf16_encode(value, pa_thread_request().charsets.source()) +// non-UTF-8 name should be converted to an error without throw +#define PA_UTF16_VAR(var, value, error, failed) const wchar_t* var=PA_UTF16_ENC(value); if(!var){ errno=error; return failed; } + int pa_stat(const char *pathname, struct stat *buffer){ - return _wstat64(PA_UTF16_ENC(pathname), buffer); + PA_UTF16_VAR(wpathname, pathname, ENOENT, -1) + return _wstat64(wpathname, buffer); } int pa_open(const char *pathname, int flags, int mode){ - return _wopen(PA_UTF16_ENC(pathname), flags, mode); + PA_UTF16_VAR(wpathname, pathname, (flags & _O_CREAT) ? EINVAL : ENOENT, -1) + return _wopen(wpathname, flags, mode); } FILE *pa_fopen(const char *pathname, const char *mode){ - return _wfopen(PA_UTF16_ENC(pathname), PA_UTF16_ENC(mode)); + PA_UTF16_VAR(wpathname, pathname, *mode=='r' ? ENOENT : EINVAL, 0) + PA_UTF16_VAR(wmode, mode, EINVAL, 0) + return _wfopen(wpathname, wmode); } int pa_mkdir(const char *pathname, int){ - return _wmkdir(PA_UTF16_ENC(pathname)); + PA_UTF16_VAR(wpathname, pathname, EINVAL, -1) + return _wmkdir(wpathname); } int pa_rmdir(const char *pathname){ - return _wrmdir(PA_UTF16_ENC(pathname)); + PA_UTF16_VAR(wpathname, pathname, ENOENT, -1) + return _wrmdir(wpathname); +} + +static bool pa_is_dir(const wchar_t* wpathname){ + DWORD attributes=GetFileAttributesW(wpathname); + return attributes!=INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY); } int pa_rename(const char *oldpath, const char *newpath){ - return _wrename(PA_UTF16_ENC(oldpath), PA_UTF16_ENC(newpath)); + PA_UTF16_VAR(woldpath, oldpath, ENOENT, -1) + PA_UTF16_VAR(wnewpath, newpath, ENOENT, -1) + if(pa_set_errno(MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING))) + return 0; + // like rename on unix, a directory replaces the empty one: windows does not replace directories at all + if(errno==EACCES && pa_is_dir(woldpath) && pa_is_dir(wnewpath) && _wrmdir(wnewpath)==0) + return pa_set_errno(MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING)) ? 0 : -1; + return -1; } int pa_unlink(const char *pathname){ - return _wunlink(PA_UTF16_ENC(pathname)); + PA_UTF16_VAR(wpathname, pathname, ENOENT, -1) + return _wunlink(wpathname); } #else @@ -263,12 +345,12 @@ bool file_read_action_under_lock(const S try { int pa_errno=pa_lock_shared_blocking(f); if(pa_errno!=0) - throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname); + throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d)%s", strerror(pa_errno), pa_errno, actual_filename(file_spec, fname)); struct stat finfo; if(pa_fstat(f, &finfo)!=0) throw Exception("file.missing", // hardly possible: we just opened it OK - &file_spec, "stat failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); + &file_spec, "stat failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname)); check_safe_mode(finfo, file_spec, fname); @@ -285,7 +367,7 @@ bool file_read_action_under_lock(const S } else { if(fail_on_read_problem) throw Exception(errno==EACCES ? "file.access" : (errno==ENOENT || errno==ENOTDIR || errno==ENODEV) ? "file.missing" : 0, - &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname); + &file_spec, "%s failed: %s (%d)%s", action_name, strerror(errno), errno, actual_filename(file_spec, fname)); return false; } } @@ -294,7 +376,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++; } @@ -312,7 +394,7 @@ bool file_write_action_under_lock(const if((f=pa_open(fname, O_CREAT | O_RDWR | (as_text ? _O_TEXT : _O_BINARY) | (do_append ? O_APPEND : PA_O_TRUNC), 0664))>=0) { int pa_errno=do_block ? pa_lock_exclusive_blocking(f) : pa_lock_exclusive_nonblocking(f); if(pa_errno!=0) { - Exception e("file.lock", &file_spec, "shared lock failed: %s (%d), actual filename '%s'", strerror(pa_errno), pa_errno, fname); + Exception e("file.lock", &file_spec, "exclusive lock failed: %s (%d)%s", strerror(pa_errno), pa_errno, actual_filename(file_spec, fname)); close(f); if(fail_on_lock_problem) throw e; @@ -342,7 +424,7 @@ bool file_write_action_under_lock(const pa_unlock(f);close(f); return true; } else - throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d), actual filename '%s'", action_name, strerror(errno), errno, fname); + throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d)%s", action_name, strerror(errno), errno, actual_filename(file_spec, fname)); // here should be nothing, see rethrow above } @@ -410,7 +492,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; @@ -435,7 +517,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; @@ -480,7 +562,7 @@ bool file_delete(const String& file_spec if(pa_unlink(fname)!=0) { if(fail_on_problem) throw Exception(errno==EACCES?"file.access":errno==ENOENT?"file.missing":0, - &file_spec, "unlink failed: %s (%d), actual filename '%s'", strerror(errno), errno, fname); + &file_spec, "unlink failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname)); else return false; } @@ -499,7 +581,7 @@ void file_move(const String& old_spec, c 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); + &old_spec, "rename to '%s' failed: %s (%d)%s", new_spec_cstr, strerror(errno), errno, actual_filename(old_spec, old_spec_cstr)); if(!keep_empty_dirs) rmdir(old_spec, 1); @@ -516,7 +598,7 @@ bool dir_exists(const String& file_spec) 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; @@ -531,7 +613,7 @@ bool file_stat(const String& file_spec, struct stat finfo; if(pa_stat(fname, &finfo)!=0) { if(fail_on_read_problem) - throw Exception("file.missing", &file_spec, "getting file size failed: %s (%d), real filename '%s'", strerror(errno), errno, fname); + throw Exception("file.missing", &file_spec, "getting file size failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname)); else return false; }