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