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