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