Annotation of parser3/src/main/pa_file.C, revision 1.15
1.1 moko 1: /** @file
2: Parser: file and file system related functions.
3:
4: Copyright (c) 2000-2026 Art. Lebedev Studio (https://www.artlebedev.com)
5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
6: */
7:
8: #include "pa_file.h"
9: #include "pa_dir.h"
1.13 moko 10: #include "pa_os.h"
1.1 moko 11: #include "pa_charsets.h"
12: #include "pa_http.h"
13: #include "pa_convert_utf.h"
14:
15: #ifdef _MSC_VER
16: #include <windows.h>
17: #include <direct.h>
18: #endif
19:
1.15 ! moko 20: volatile const char * IDENT_PA_FILE_C="$Id: pa_file.C,v 1.14 2026/11/03 16:04:19 moko Exp $" IDENT_PA_FILE_H;
1.8 moko 21:
22: // a tainted path scheme (file://, http:// or parser://) would be accepted
23: //#define IGNORE_FILE_PREFIX_LANGUAGES
1.1 moko 24:
25: // some maybe-undefined constants
26:
27: #ifndef _O_TEXT
28: # define _O_TEXT 0
29: #endif
30: #ifndef _O_BINARY
31: # define _O_BINARY 0
32: #endif
33:
34: #ifdef WIN32
35:
36: bool is_os_absolute_path(const String& path) {
37: return is_os_absolute_path(path.cstr());
38: }
39:
40: bool is_os_absolute_path(const char* path) {
41: return path[0] && path[0]!=':' && path[1]==':' // DRIVE: (including drive-relative paths, for compatibility)
1.4 moko 42: || path[0]=='\\' && path[1]=='\\'; // UNC, only '\\' is supported as '//' is a document root relative path
1.1 moko 43: }
44: #endif
45:
1.5 moko 46: uint path_scheme(const String& path) {
47: if(path.starts_with("file://"))
48: return PA_SCHEME_FILE;
49: if(path.starts_with("http://"))
50: return PA_SCHEME_HTTP;
51: if(path.starts_with("parser://"))
52: return PA_SCHEME_PARSER;
53: return PA_SCHEME_NONE;
54: }
55:
1.9 moko 56: bool clean_path_scheme(uint scheme, const String& path) {
57: #ifdef IGNORE_FILE_PREFIX_LANGUAGES
58: return true;
59: #else
60: return path.check_lang(String::L_AS_IS, 0, scheme==PA_SCHEME_PARSER ? 9 /* "parser://" */ : 7 /* "file://" or "http://" */);
61: #endif
62: }
63:
1.5 moko 64: void check_path_scheme(uint scheme, const String& path, uint allowed) {
65: if(scheme!=PA_SCHEME_FILE && !(allowed&scheme))
66: throw Exception(PARSER_RUNTIME, &path, "unsupported path scheme");
1.9 moko 67: if(!clean_path_scheme(scheme, path))
1.8 moko 68: throw Exception(PARSER_RUNTIME, &path, "tainted path scheme");
1.5 moko 69: }
70:
1.10 moko 71: int path_cmp(const char* path, const char* dir, size_t length) {
72: #ifdef WIN32
73: return strncasecmp(path, dir, length); // mostly for the drive letter
74: #else
75: return strncmp(path, dir, length);
76: #endif
77: }
78:
1.11 moko 79: const char* actual_filename(const String& file_spec, const char* fname) {
80: return strcmp(file_spec.cstr(), fname) ? pa_strcat(", actual filename '", fname, "'") : "";
81: }
82:
1.5 moko 83: const String* file_uri_to_path(const String& uri) {
84: const String* rest=&uri.mid(7 /* "file://" */, uri.length());
85: if(rest->starts_with("localhost/"))
86: rest=&rest->mid(9 /* "localhost", keeping the slash */, rest->length());
87: if(rest->first_char()=='/') {
88: // file:///C:/path and file://localhost/C:/path: the slash before a drive name is not a part of the path
89: const String& disk=rest->mid(1, rest->length());
90: return is_os_absolute_path(disk) ? &disk : rest;
91: }
92: if(is_os_absolute_path(*rest))
93: return rest; // file://C:/path, not a standard form but in use
1.7 moko 94: #ifdef WIN32
95: if(rest->pos('/')!=STRING_NOT_FOUND) { // file://server/share/path is the UNC path \\server\share\path
96: String& result=*new String("//");
97: result << *rest;
98: return &result;
99: }
100: #endif
1.5 moko 101: return 0;
102: }
103:
1.1 moko 104: // functions
105:
106: #ifdef _MSC_VER
107:
1.12 moko 108: // externs
109:
110: const UTF16* pa_utf16_encode(const char* in, Charset& source_charset);
1.1 moko 111: #define PA_UTF16_ENC(value) (const wchar_t *)pa_utf16_encode(value, pa_thread_request().charsets.source())
112:
1.12 moko 113: // non-UTF-8 name should be converted to an error without throw
114: #define PA_UTF16_VAR(var, value, error, failed) const wchar_t* var=PA_UTF16_ENC(value); if(!var){ errno=error; return failed; }
115:
1.1 moko 116: int pa_stat(const char *pathname, struct stat *buffer){
1.12 moko 117: PA_UTF16_VAR(wpathname, pathname, ENOENT, -1)
118: return _wstat64(wpathname, buffer);
1.1 moko 119: }
120:
121: int pa_open(const char *pathname, int flags, int mode){
1.12 moko 122: PA_UTF16_VAR(wpathname, pathname, (flags & _O_CREAT) ? EINVAL : ENOENT, -1)
123: return _wopen(wpathname, flags, mode);
1.1 moko 124: }
125:
126: FILE *pa_fopen(const char *pathname, const char *mode){
1.12 moko 127: PA_UTF16_VAR(wpathname, pathname, *mode=='r' ? ENOENT : EINVAL, 0)
128: PA_UTF16_VAR(wmode, mode, EINVAL, 0)
129: return _wfopen(wpathname, wmode);
1.1 moko 130: }
131:
132: int pa_mkdir(const char *pathname, int){
1.12 moko 133: PA_UTF16_VAR(wpathname, pathname, EINVAL, -1)
134: return _wmkdir(wpathname);
1.1 moko 135: }
136:
137: int pa_rmdir(const char *pathname){
1.12 moko 138: PA_UTF16_VAR(wpathname, pathname, ENOENT, -1)
139: return _wrmdir(wpathname);
1.1 moko 140: }
141:
1.13 moko 142: static bool pa_is_dir(const wchar_t* wpathname){
143: DWORD attributes=GetFileAttributesW(wpathname);
144: return attributes!=INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY);
145: }
146:
1.1 moko 147: int pa_rename(const char *oldpath, const char *newpath){
1.12 moko 148: PA_UTF16_VAR(woldpath, oldpath, ENOENT, -1)
149: PA_UTF16_VAR(wnewpath, newpath, ENOENT, -1)
1.13 moko 150: if(pa_set_errno(MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING)))
151: return 0;
152: // like rename on unix, a directory replaces the empty one: windows does not replace directories at all
153: if(errno==EACCES && pa_is_dir(woldpath) && pa_is_dir(wnewpath) && _wrmdir(wnewpath)==0)
154: return pa_set_errno(MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING)) ? 0 : -1;
155: return -1;
1.1 moko 156: }
157:
158: int pa_unlink(const char *pathname){
1.12 moko 159: PA_UTF16_VAR(wpathname, pathname, ENOENT, -1)
160: return _wunlink(wpathname);
1.1 moko 161: }
162:
1.15 ! moko 163: static int pa_ftruncate(int fd, uint64_t size){
! 164: return _chsize_s(fd, size);
! 165: }
! 166:
1.1 moko 167: #else
168:
169: #define pa_mkdir mkdir
170: #define pa_rmdir rmdir
171: #define pa_rename rename
172: #define pa_unlink unlink
1.15 ! moko 173: #define pa_ftruncate ftruncate
1.1 moko 174:
175: #endif
176:
177: /// these options were handled but not checked elsewhere, now check them
178: int pa_get_valid_file_options_count(HashStringValue& options) {
179: int result=0;
180: if(options.get(PA_SQL_LIMIT_NAME))
181: result++;
182: if(options.get(PA_SQL_OFFSET_NAME))
183: result++;
184: if(options.get(PA_COLUMN_SEPARATOR_NAME))
185: result++;
186: if(options.get(PA_COLUMN_ENCLOSER_NAME))
187: result++;
188: if(options.get(PA_CHARSET_NAME))
189: result++;
190: return result;
191: }
192:
193: #ifndef DOXYGEN
194: struct File_read_action_info {
195: char **data; size_t *data_size;
196: char* buf; uint64_t offset; size_t limit;
1.3 moko 197: };
1.1 moko 198: #endif
199:
200: static void file_read_action(struct stat& finfo, int f, const String& file_spec, void *context) {
1.3 moko 201: File_read_action_info& info = *static_cast<File_read_action_info *>(context);
1.1 moko 202: size_t to_read_size = check_file_size(info.limit && info.limit < (size_t)finfo.st_size ? info.limit : (size_t)finfo.st_size, &file_spec);
203: if(to_read_size) {
204: if(info.offset)
205: pa_lseek(f, info.offset, SEEK_SET); // seek never fails as POSIX allows the file offset to be set beyond the EOF
206: *info.data = info.buf ? info.buf : (char *)pa_malloc_atomic(to_read_size+1);
207: ssize_t result = read(f, *info.data, to_read_size);
208: if(result<0)
209: throw Exception("file.read", &file_spec, "read failed: %s (%d)", strerror(errno), errno);
210: *info.data_size = result;
211: } else { // empty file
212: // for both, text and binary: for text we need that terminator, for binary we need nonzero pointer to be able to save such files
213: *info.data = (char *)pa_malloc_atomic(1);
214: *(char*)(*info.data) = 0;
215: *info.data_size = 0;
216: return;
217: }
218: }
219:
220: File_read_result file_read_binary(const String& file_spec, bool fail_on_read_problem, char* buf, uint64_t offset, size_t limit) {
221: File_read_result result = {false, 0, 0, 0};
222: File_read_action_info info = {&result.str, &result.length, buf, offset, limit};
223:
224: result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, 0, fail_on_read_problem);
225: return result;
226: }
227:
1.3 moko 228: File_read_result file_read(Request_charsets& charsets, const String& file_spec,
1.1 moko 229: bool as_text, HashStringValue *options,
230: bool fail_on_read_problem,
231: size_t offset = 0, size_t limit = 0, bool transcode_text_result = true) {
232: File_read_result result = {false, 0, 0, 0};
233: if(options){
234: int valid_options = pa_get_valid_file_options_count(*options);
235: if(valid_options != options->count())
236: throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
237: }
238:
1.3 moko 239: File_read_action_info info = {&result.str, &result.length, 0, offset, limit};
1.1 moko 240:
1.3 moko 241: result.success = file_read_action_under_lock(file_spec, "read", file_read_action, &info, as_text, fail_on_read_problem);
1.1 moko 242:
243: if(as_text){
244: if(result.success){
245: Charset* asked_charset = 0;
246: if(options)
247: if(Value* vcharset_name = options->get(PA_CHARSET_NAME))
248: asked_charset = &pa_charsets.get(vcharset_name->as_string());
249:
250: asked_charset = pa_charsets.checkBOM(result.str, result.length, asked_charset);
251:
252: if(result.length && transcode_text_result && asked_charset){ // length must be checked because transcode returns CONST string in case length==0, which contradicts hacking few lines below
253: String::C body = String::C(result.str, result.length);
254: body=Charset::transcode(body, *asked_charset, charsets.source());
255:
256: result.str = const_cast<char*>(body.str); // hacking a little
257: result.length = body.length;
258: }
259: }
260: if(result.length)
261: fix_line_breaks(result.str, result.length);
262: }
263:
264: return result;
265: }
266:
1.3 moko 267: File_read_result file_load(Request& r, const String& file_spec,
1.1 moko 268: bool as_text, HashStringValue *options,
269: bool fail_on_read_problem,
270: bool transcode_text_result) {
271:
272: size_t offset = 0;
273: size_t limit = 0;
274:
275: if(options){
276: if(Value *voffset = (Value *)options->get(sql_offset_name))
277: offset = r.process(*voffset).as_int();
278: if(Value *vlimit = (Value *)options->get(sql_limit_name))
279: limit = r.process(*vlimit).as_int();
280: // no check on options count here
281: }
282:
283: if(file_spec.starts_with("http://")) {
284: if(offset || limit)
285: throw Exception(PARSER_RUNTIME, 0, "offset and load options are not supported for HTTP:// file load");
286:
287: // fail on read problem
288: File_read_http_result http = pa_internal_file_read_http(r, file_spec, as_text, options, transcode_text_result);
289:
290: File_read_result result = {true, http.str, http.length, http.headers};
291: return result;
292: } else
293: return file_read(r.charsets, file_spec, as_text, options, fail_on_read_problem, offset, limit, transcode_text_result);
294: }
295:
296: char* file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem) {
297: File_read_result file = file_read(charsets, file_spec, true, 0, fail_on_read_problem);
298: return file.success ? file.str : 0;
299: }
300:
301: char* file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem, HashStringValue* options, bool transcode_result) {
302: File_read_result file = file_load(r, file_spec, true, options, fail_on_read_problem, transcode_result);
303: return file.success ? file.str : 0;
304: }
305:
306: #ifdef PA_SAFE_MODE
307: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname) {
308: if(finfo.st_uid/*foreign?*/!=geteuid()
309: && finfo.st_gid/*foreign?*/!=getegid())
310: throw Exception(PARSER_RUNTIME,
311: &file_spec,
312: "parser is in safe mode: reading files of foreign group and user disabled "
313: "[recompile parser with --disable-safe-mode configure option], "
314: "actual filename '%s', fuid(%d)!=euid(%d) or fgid(%d)!=egid(%d)",
315: fname, finfo.st_uid, geteuid(), finfo.st_gid, getegid()
316: );
317: }
318: #else
319: void check_safe_mode(struct stat, const String&, const char*) {
320: }
321: #endif
322:
323:
324: bool file_read_action_under_lock(const String& file_spec, const char* action_name, File_read_action action, void *context,
325: bool as_text, bool fail_on_read_problem) {
326:
327: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
328: int f;
329:
330: // first open, next stat:
331: // directory update of NTFS hard links performed on open.
1.3 moko 332: // ex:
1.1 moko 333: // a.html:^test[] and b.html hardlink to a.html
334: // user inserts ! before ^test in a.html
1.3 moko 335: // directory entry of b.html in NTFS not updated at once,
1.1 moko 336: // they delay update till open, so we would receive "!^test[" string
337: // if would do stat, next open.
338: // later: it seems, even this does not help sometimes
339: if((f=pa_open(fname, O_RDONLY | (as_text ? _O_TEXT : _O_BINARY) ))>=0) {
340: try {
341: int pa_errno=pa_lock_shared_blocking(f);
342: if(pa_errno!=0)
1.11 moko 343: throw Exception("file.lock", &file_spec, "shared lock failed: %s (%d)%s", strerror(pa_errno), pa_errno, actual_filename(file_spec, fname));
1.1 moko 344:
345: struct stat finfo;
346: if(pa_fstat(f, &finfo)!=0)
347: throw Exception("file.missing", // hardly possible: we just opened it OK
1.11 moko 348: &file_spec, "stat failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname));
1.1 moko 349:
350: check_safe_mode(finfo, file_spec, fname);
351:
1.3 moko 352: action(finfo, f, file_spec, context);
1.1 moko 353: } catch(...) {
1.3 moko 354: pa_unlock(f);close(f);
1.1 moko 355: if(fail_on_read_problem)
356: rethrow;
357: return false;
1.3 moko 358: }
1.1 moko 359:
1.3 moko 360: pa_unlock(f);close(f);
1.1 moko 361: return true;
362: } else {
363: if(fail_on_read_problem)
364: throw Exception(errno==EACCES ? "file.access" : (errno==ENOENT || errno==ENOTDIR || errno==ENODEV) ? "file.missing" : 0,
1.11 moko 365: &file_spec, "%s failed: %s (%d)%s", action_name, strerror(errno), errno, actual_filename(file_spec, fname));
1.1 moko 366: return false;
367: }
368: }
369:
370: void create_dir_for_file(const String& file_spec) {
371: const char *str=file_spec.taint_cstr(String::L_FILE_SPEC);
372: if(str[0]){
373: const char *pos=str+1;
1.6 moko 374: while((pos=strchr(pos, '/')) && pos[1]) { // to avoid trailing /, see #1166
1.1 moko 375: pa_mkdir(pa_strdup(str,pos-str), 0775);
376: pos++;
377: }
378: }
379: }
380:
381: bool file_write_action_under_lock(const String& file_spec, const char* action_name, File_write_action action, void *context,
382: bool as_text, bool do_append, bool do_block, bool fail_on_lock_problem) {
383:
384: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
385: int f;
386: if(access(fname, W_OK)!=0) // no
387: create_dir_for_file(file_spec);
388:
1.15 ! moko 389: if((f=pa_open(fname, O_CREAT | O_RDWR | (as_text ? _O_TEXT : _O_BINARY) | (do_append ? O_APPEND : 0), 0664))>=0) { // not O_TRUNC: the file is cut after the action, see below
1.1 moko 390: int pa_errno=do_block ? pa_lock_exclusive_blocking(f) : pa_lock_exclusive_nonblocking(f);
391: if(pa_errno!=0) {
1.11 moko 392: Exception e("file.lock", &file_spec, "exclusive lock failed: %s (%d)%s", strerror(pa_errno), pa_errno, actual_filename(file_spec, fname));
1.1 moko 393: close(f);
394: if(fail_on_lock_problem)
395: throw e;
396: return false;
397: }
398:
399: try {
400: #if (defined(HAVE_FCHMOD) && defined(PA_SAFE_MODE))
401: struct stat finfo;
402: if(pa_fstat(f, &finfo)==0 && finfo.st_mode & 0111)
403: fchmod(f, finfo.st_mode & 0666/*clear executable bits*/); // backward: ignore errors if any
404: #endif
405: action(f, context);
406: } catch(...) {
407: if(!do_append)
1.15 ! moko 408: PA_UNUSED int ignore_result=pa_ftruncate(f, pa_lseek(f, 0, SEEK_CUR)); // one cannot use O_TRUNC, read lower
1.1 moko 409: pa_unlock(f);close(f);
410: rethrow;
411: }
412:
413: if(!do_append)
1.15 ! moko 414: PA_UNUSED int ignore_result=pa_ftruncate(f, pa_lseek(f, 0, SEEK_CUR)); // O_TRUNC truncates even exclusevely write-locked file [thanks to Igor Milyakov <virtan@rotabanner.com> for discovering]
1.1 moko 415: pa_unlock(f);close(f);
416: return true;
417: } else
1.11 moko 418: throw Exception(errno==EACCES ? "file.access" : 0, &file_spec, "%s failed: %s (%d)%s", action_name, strerror(errno), errno, actual_filename(file_spec, fname));
1.1 moko 419: // here should be nothing, see rethrow above
420: }
421:
422: #ifndef DOXYGEN
423: struct File_write_action_info {
424: const char* str;
425: size_t length;
426: };
427: #endif
428:
429: // a single write() must stay below INT_MAX as windows _write() reports written bytes as int
430: #define FILE_WRITE_CHUNK_SIZE (256*0x100000)
431:
432: static const char* bytes_left(size_t rest) {
433: return rest ? pa_strcat(", ", pa_uitoa(rest), " bytes left") : "";
434: }
435:
436: static void file_write_action(int f, void *context) {
437: File_write_action_info& info=*static_cast<File_write_action_info *>(context);
438:
439: const char* data=info.str;
440: size_t left=info.length;
441:
442: while(left) {
443: size_t portion=min(left, (size_t)FILE_WRITE_CHUNK_SIZE);
444: ssize_t written=write(f, data, portion);
445: if(written<0)
446: throw Exception("file.write", 0, "error writing %s bytes: %s (%d)%s", pa_uitoa(portion), strerror(errno), errno, bytes_left(left-portion));
447: if(written==0)
448: throw Exception("file.write", 0, "error writing %s bytes: zero bytes written%s", pa_uitoa(portion), bytes_left(left-portion));
449: data+=written;
450: left-=written;
451: }
452: }
453:
454: void file_write(
455: Request_charsets& charsets,
456: const String& file_spec,
457: const char* data,
1.3 moko 458: size_t size,
459: bool as_text,
1.1 moko 460: bool do_append,
461: Charset* asked_charset) {
462:
463: if(as_text && asked_charset){
464: String::C body=String::C(data, size);
465: body=Charset::transcode(body, charsets.source(), *asked_charset);
466: data=body.str;
467: size=body.length;
468: };
469:
470: File_write_action_info info={data, size};
471:
472: file_write_action_under_lock(
473: file_spec,
474: "write",
475: file_write_action,
476: &info,
477: as_text,
478: do_append);
479: }
480:
481: static size_t get_dir(char* fname, size_t helper_length){
482: bool dir=false;
483: size_t pos=0;
484: for(pos=helper_length; pos; pos--){
485: char c=fname[pos-1];
1.6 moko 486: if(c=='/'){
1.1 moko 487: fname[pos-1]=0;
488: dir=true;
489: } else if(dir) break;
490: }
491: return pos;
492: }
493:
494: bool entry_exists(const char* fname, struct stat *afinfo) {
495: struct stat lfinfo;
496: bool result=pa_stat(fname, &lfinfo)==0;
497: if(afinfo)
498: *afinfo=lfinfo;
499: return result;
500: }
501:
502: bool entry_exists(const String& file_spec) {
1.3 moko 503: return entry_exists(file_spec.taint_cstr(String::L_FILE_SPEC), 0);
1.1 moko 504: }
505:
506: static bool entry_ifdir(char *fname, bool need_dir) {
507: if(need_dir){
508: size_t size=strlen(fname);
509: while(size) {
510: char c=fname[size-1];
1.6 moko 511: if(c=='/')
1.1 moko 512: fname[--size]=0;
513: else
514: break;
515: }
516: }
517:
518: struct stat finfo;
519: if(entry_exists(fname, &finfo)) {
520: bool is_dir=(finfo.st_mode&S_IFDIR) != 0;
521: return is_dir==need_dir;
522: }
523: return false;
524: }
525:
526: static bool entry_ifdir(const String& file_spec, bool need_dir) {
527: return entry_ifdir(file_spec.taint_cstrm(String::L_FILE_SPEC), need_dir);
528: }
529:
530: // throws nothing! [this is required in file_move & file_delete]
531: static void rmdir(const String& file_spec, size_t pos_after) {
532: char* dir_spec=file_spec.taint_cstrm(String::L_FILE_SPEC);
533: size_t length=strlen(dir_spec);
534: while( (length=get_dir(dir_spec, length)) && (length > pos_after) ){
535: #ifdef _MSC_VER
536: if(!entry_ifdir(dir_spec, true))
537: break;
538: DWORD attrs=GetFileAttributesW(PA_UTF16_ENC(dir_spec));
539: if(
540: (attrs==INVALID_FILE_ATTRIBUTES)
541: || !(attrs & FILE_ATTRIBUTE_DIRECTORY)
542: || (attrs & FILE_ATTRIBUTE_REPARSE_POINT)
543: )
544: break;
545: #endif
546: if(pa_rmdir(dir_spec))
547: break;
548: };
549: }
550:
551: bool file_delete(const String& file_spec, bool fail_on_problem, bool keep_empty_dirs) {
1.3 moko 552: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.1 moko 553: if(pa_unlink(fname)!=0) {
554: if(fail_on_problem)
1.14 moko 555: throw Exception(errno==EACCES ? "file.access" : errno==ENOENT ? "file.missing" : 0,
1.11 moko 556: &file_spec, "unlink failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname));
1.1 moko 557: else
558: return false;
559: }
560:
561: if(!keep_empty_dirs)
1.3 moko 562: rmdir(file_spec, 1);
1.1 moko 563:
564: return true;
565: }
566:
1.14 moko 567: bool dir_delete(const String& dir_spec, bool fail_on_problem) {
568: const char* dname=dir_spec.taint_cstr(String::L_FILE_SPEC);
569: if(pa_rmdir(dname)!=0) {
570: if(fail_on_problem)
571: throw Exception(errno==EACCES ? "file.access" : errno==ENOENT ? "file.missing" : 0,
572: &dir_spec, "rmdir failed: %s (%d)%s", strerror(errno), errno, actual_filename(dir_spec, dname));
573: else
574: return false;
575: }
576: return true;
577: }
578:
1.1 moko 579: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs) {
1.3 moko 580: const char* old_spec_cstr=old_spec.taint_cstr(String::L_FILE_SPEC);
581: const char* new_spec_cstr=new_spec.taint_cstr(String::L_FILE_SPEC);
1.1 moko 582:
1.3 moko 583: create_dir_for_file(new_spec);
1.1 moko 584:
585: if(pa_rename(old_spec_cstr, new_spec_cstr)!=0)
586: throw Exception(errno==EACCES ? "file.access" : errno==ENOENT ? "file.missing" : 0,
1.11 moko 587: &old_spec, "rename to '%s' failed: %s (%d)%s", new_spec_cstr, strerror(errno), errno, actual_filename(old_spec, old_spec_cstr));
1.1 moko 588:
589: if(!keep_empty_dirs)
1.3 moko 590: rmdir(old_spec, 1);
1.1 moko 591: }
592:
593:
594: bool file_exist(const String& file_spec) {
1.3 moko 595: return entry_ifdir(file_spec, false);
1.1 moko 596: }
597:
598: bool dir_exists(const String& file_spec) {
1.3 moko 599: return entry_ifdir(file_spec, true);
1.1 moko 600: }
601:
602: const String* file_exist(const String& path, const String& name) {
603: String& result=*new String(path);
1.6 moko 604: if(path.last_char() != '/')
1.1 moko 605: result << "/";
606: result << name;
607: return file_exist(result)?&result:0;
608: }
609:
610: bool file_executable(const String& file_spec) {
611: return access(file_spec.taint_cstr(String::L_FILE_SPEC), X_OK)==0;
612: }
613:
614: bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem) {
1.3 moko 615: const char* fname=file_spec.taint_cstr(String::L_FILE_SPEC);
1.1 moko 616: struct stat finfo;
617: if(pa_stat(fname, &finfo)!=0) {
618: if(fail_on_read_problem)
1.11 moko 619: throw Exception("file.missing", &file_spec, "getting file size failed: %s (%d)%s", strerror(errno), errno, actual_filename(file_spec, fname));
1.1 moko 620: else
621: return false;
622: }
623: rsize=finfo.st_size;
624: ratime=(time_t)finfo.st_atime;
625: rmtime=(time_t)finfo.st_mtime;
626: rctime=(time_t)finfo.st_ctime;
627: return true;
628: }
629:
630: size_t check_file_size(uint64_t size, const String* file_spec){
631: if(size > (uint64_t)pa_file_size_limit)
1.2 moko 632: throw Exception(PARSER_RUNTIME, file_spec, "content size of %s bytes exceeds the limit (%s bytes)", pa_uitoa(size), pa_uitoa(pa_file_size_limit));
1.1 moko 633: return (size_t)size;
634: }
635:
1.3 moko 636: /**
1.1 moko 637: String related functions
638: */
639:
640: const char *pa_filename(const char *path){
641: const char *slash=strrpbrk(path, "/\\");
642: return slash ? slash+1 : path;
643: }
E-mail: