Annotation of parser3/src/include/pa_file.h, revision 1.7

1.1       moko        1: /** @file
                      2:        Parser: file and file system related functions.
                      3: 
                      4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
                      6: */
                      7: 
                      8: #ifndef PA_FILE_H
                      9: #define PA_FILE_H
                     10: 
1.7     ! moko       11: #define IDENT_PA_FILE_H "$Id: pa_file.h,v 1.6 2026/10/02 15:49:42 moko Exp $"
1.1       moko       12: 
                     13: #include "pa_common.h"
                     14: 
                     15: #ifdef _MSC_VER
                     16: 
                     17: //access
                     18: #define F_OK 0
                     19: #define X_OK 1
                     20: #define W_OK 2
                     21: #define R_OK 4
                     22: 
                     23: #define stat __stat64
                     24: #define pa_fstat _fstat64
                     25: 
                     26: int pa_stat(const char *pathname, struct stat *buffer);
                     27: int pa_open(const char *pathname, int flags, int mode=0);
                     28: FILE *pa_fopen(const char *pathname, const char *mode);
                     29: 
                     30: #define pa_lseek _lseeki64
                     31: 
                     32: #else
                     33: 
                     34: #define pa_stat stat
                     35: #define pa_fstat fstat
                     36: 
                     37: #define pa_open open
                     38: #define pa_fopen fopen
                     39: 
                     40: #define pa_lseek lseek
                     41: 
                     42: #endif
                     43: 
1.3       moko       44: /// the path should not be '/'-normalized yet, as \\server would become the web path //server
1.1       moko       45: #ifdef WIN32
                     46: bool is_os_absolute_path(const String& path);
                     47: bool is_os_absolute_path(const char* path);
                     48: #else
                     49: inline bool is_os_absolute_path(const String&) { return false; }
                     50: inline bool is_os_absolute_path(const char*) { return false; }
                     51: #endif
                     52: 
1.2       moko       53: /// recognized path scheme, also used as a bit mask of the schemes a caller allows
                     54: enum { PA_SCHEME_NONE=0, PA_SCHEME_FILE=1, PA_SCHEME_HTTP=2, PA_SCHEME_PARSER=4 };
                     55: enum { PA_ALLOW_HTTP=PA_SCHEME_HTTP, PA_ALLOW_ALL=PA_SCHEME_HTTP|PA_SCHEME_PARSER };
                     56: 
1.3       moko       57: /// scheme of a not '/'-normalized path: file://, http:// or parser://, PA_SCHEME_NONE otherwise
1.2       moko       58: uint path_scheme(const String& path);
                     59: 
1.5       moko       60: /// false if the scheme is tainted
                     61: bool clean_path_scheme(uint scheme, const String& path);
                     62: 
1.4       moko       63: /// throws unless the scheme is allowed, file:// always is, or if the scheme is tainted
1.2       moko       64: void check_path_scheme(uint scheme, const String& path, uint allowed);
                     65: 
1.3       moko       66: /// resolved file:// or null; the result is not '/'-normalized
1.2       moko       67: const String* file_uri_to_path(const String& uri);
                     68: 
1.6       moko       69: /// strncmp for disk paths, case insensitive under windows
                     70: int path_cmp(const char* path, const char* dir, size_t length);
                     71: 
1.7     ! moko       72: /// returns ", actual filename '...'" only if escaping changed the file name (exception source shows the original file)
        !            73: const char* actual_filename(const String& file_spec, const char* fname);
        !            74: 
1.1       moko       75: #define FILE_BUFFER_SIZE (128*0x400)
                     76: 
                     77: int pa_lock_shared_blocking(int fd);
                     78: int pa_lock_exclusive_blocking(int fd);
                     79: int pa_lock_exclusive_nonblocking(int fd);
                     80: int pa_unlock(int fd);
                     81: 
                     82: const char *pa_filename(const char *path);
                     83: 
                     84: void create_dir_for_file(const String& file_spec);
                     85: 
                     86: int pa_get_valid_file_options_count(HashStringValue& options);
                     87: 
                     88: typedef void (*File_read_action)(struct stat& finfo, int f, const String& file_spec, void *context);
                     89: 
                     90: /**
                     91:        shared-lock specified file, 
                     92:        do actions under lock.
                     93:        if fail_on_read_problem is true[default] throws an exception
                     94:        
                     95:        @returns true if read OK
                     96: */
                     97: bool file_read_action_under_lock(const String& file_spec,
                     98:                                const char* action_name, File_read_action action, void *context,
                     99:                                bool as_text=false,
                    100:                                bool fail_on_read_problem=true);
                    101: 
                    102: /**
                    103:        read specified text file
                    104:        if fail_on_read_problem is true[default] throws an exception
                    105: 
                    106:        WARNING: charset is used for http header case conversion, it's not a charset of input file!
                    107: */
                    108: char *file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem = true);
                    109: char *file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem = true, HashStringValue* options = 0, bool transcode_result = true);
                    110: 
                    111: struct File_read_result {
                    112:        bool success;
                    113:        char* str; size_t length;
                    114:        HashStringValue* headers;
                    115: };
                    116: 
                    117: // read specified binary file; if fail_on_read_problem is true[default] throws an exception
                    118: File_read_result file_read_binary(const String& file_spec, bool fail_on_read_problem = true, char* buf = 0, uint64_t offset = 0, size_t limit = 0);
                    119: 
                    120: // load specified file; if fail_on_read_problem is true[default] throws an exception
                    121: File_read_result file_load(Request& r, const String& file_spec, bool as_text, HashStringValue* options=0, bool fail_on_read_problem=true, bool transcode_text_result=true);
                    122: 
                    123: typedef void (*File_write_action)(int f, void *context);
                    124: 
                    125: /**
                    126:        lock specified file exclusively, do actions under lock.
                    127:        throws an exception in case of problems
                    128:        
                    129:        if block=false does non-blocking lock
                    130:        @returns true if locked OK, or false if non-blocking locking failed
                    131: */
                    132: bool file_write_action_under_lock(
                    133:                                const String& file_spec,
                    134:                                const char* action_name,
                    135:                                File_write_action action,
                    136:                                void *context,
                    137:                                bool as_text=false,
                    138:                                bool do_append=false,
                    139:                                bool do_block=true,
                    140:                                bool fail_on_lock_problem=true);
                    141: 
                    142: // write data to specified file; throws an exception in case of problems
                    143: void file_write(
                    144:                                Request_charsets& charsets,
                    145:                                const String& file_spec,
                    146:                                const char* data,
                    147:                                size_t size,
                    148:                                bool as_text,
                    149:                                bool do_append=false,
                    150:                                Charset* asked_charset=0);
                    151: 
                    152: // delete specified file; throws an exception in case of problems
                    153: bool file_delete(const String& file_spec, bool fail_on_problem=true, bool keep_empty_dirs=false);
                    154: 
                    155: // move specified file; throw an exception in case of problems
                    156: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs=false);
                    157: 
                    158: bool entry_exists(const char* fname, struct stat *afinfo=0);
                    159: bool entry_exists(const String& file_spec);
                    160: bool file_exist(const String& file_spec);
                    161: bool dir_exists(const String& file_spec);
                    162: const String* file_exist(const String& path, const String& name);
                    163: bool file_executable(const String& file_spec);
                    164: 
                    165: bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem=true);
                    166: size_t check_file_size(uint64_t size, const String* file_spec);
                    167: 
                    168: size_t stdout_write(const void *buf, size_t size);
                    169: 
                    170: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname); 
                    171: 
                    172: ssize_t file_block_read(const int f, void* buffer, const size_t size);
                    173: 
                    174: #endif

E-mail: