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

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.3     ! moko       11: #define IDENT_PA_FILE_H "$Id: pa_file.h,v 1.2 2026/09/29 02:56:52 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: 
                     60: /// throws unless the scheme is allowed, file:// always is
                     61: void check_path_scheme(uint scheme, const String& path, uint allowed);
                     62: 
1.3     ! moko       63: /// resolved file:// or null; the result is not '/'-normalized
1.2       moko       64: const String* file_uri_to_path(const String& uri);
                     65: 
1.1       moko       66: #define FILE_BUFFER_SIZE (128*0x400)
                     67: 
                     68: int pa_lock_shared_blocking(int fd);
                     69: int pa_lock_exclusive_blocking(int fd);
                     70: int pa_lock_exclusive_nonblocking(int fd);
                     71: int pa_unlock(int fd);
                     72: 
                     73: const char *pa_filename(const char *path);
                     74: 
                     75: void create_dir_for_file(const String& file_spec);
                     76: 
                     77: int pa_get_valid_file_options_count(HashStringValue& options);
                     78: 
                     79: typedef void (*File_read_action)(struct stat& finfo, int f, const String& file_spec, void *context);
                     80: 
                     81: /**
                     82:        shared-lock specified file, 
                     83:        do actions under lock.
                     84:        if fail_on_read_problem is true[default] throws an exception
                     85:        
                     86:        @returns true if read OK
                     87: */
                     88: bool file_read_action_under_lock(const String& file_spec,
                     89:                                const char* action_name, File_read_action action, void *context,
                     90:                                bool as_text=false,
                     91:                                bool fail_on_read_problem=true);
                     92: 
                     93: /**
                     94:        read specified text file
                     95:        if fail_on_read_problem is true[default] throws an exception
                     96: 
                     97:        WARNING: charset is used for http header case conversion, it's not a charset of input file!
                     98: */
                     99: char *file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem = true);
                    100: char *file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem = true, HashStringValue* options = 0, bool transcode_result = true);
                    101: 
                    102: struct File_read_result {
                    103:        bool success;
                    104:        char* str; size_t length;
                    105:        HashStringValue* headers;
                    106: };
                    107: 
                    108: // read specified binary file; if fail_on_read_problem is true[default] throws an exception
                    109: 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);
                    110: 
                    111: // load specified file; if fail_on_read_problem is true[default] throws an exception
                    112: 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);
                    113: 
                    114: typedef void (*File_write_action)(int f, void *context);
                    115: 
                    116: /**
                    117:        lock specified file exclusively, do actions under lock.
                    118:        throws an exception in case of problems
                    119:        
                    120:        if block=false does non-blocking lock
                    121:        @returns true if locked OK, or false if non-blocking locking failed
                    122: */
                    123: bool file_write_action_under_lock(
                    124:                                const String& file_spec,
                    125:                                const char* action_name,
                    126:                                File_write_action action,
                    127:                                void *context,
                    128:                                bool as_text=false,
                    129:                                bool do_append=false,
                    130:                                bool do_block=true,
                    131:                                bool fail_on_lock_problem=true);
                    132: 
                    133: // write data to specified file; throws an exception in case of problems
                    134: void file_write(
                    135:                                Request_charsets& charsets,
                    136:                                const String& file_spec,
                    137:                                const char* data,
                    138:                                size_t size,
                    139:                                bool as_text,
                    140:                                bool do_append=false,
                    141:                                Charset* asked_charset=0);
                    142: 
                    143: // delete specified file; throws an exception in case of problems
                    144: bool file_delete(const String& file_spec, bool fail_on_problem=true, bool keep_empty_dirs=false);
                    145: 
                    146: // move specified file; throw an exception in case of problems
                    147: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs=false);
                    148: 
                    149: bool entry_exists(const char* fname, struct stat *afinfo=0);
                    150: bool entry_exists(const String& file_spec);
                    151: bool file_exist(const String& file_spec);
                    152: bool dir_exists(const String& file_spec);
                    153: const String* file_exist(const String& path, const String& name);
                    154: bool file_executable(const String& file_spec);
                    155: 
                    156: 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);
                    157: size_t check_file_size(uint64_t size, const String* file_spec);
                    158: 
                    159: size_t stdout_write(const void *buf, size_t size);
                    160: 
                    161: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname); 
                    162: 
                    163: ssize_t file_block_read(const int f, void* buffer, const size_t size);
                    164: 
                    165: #endif

E-mail: