/** @file
Parser: file and file system related functions.
Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
*/
#ifndef PA_FILE_H
#define PA_FILE_H
#define IDENT_PA_FILE_H "$Id: pa_file.h,v 1.1 2026/09/27 17:14:23 moko Exp $"
#include "pa_common.h"
#ifdef _MSC_VER
//access
#define F_OK 0
#define X_OK 1
#define W_OK 2
#define R_OK 4
#define stat __stat64
#define pa_fstat _fstat64
int pa_stat(const char *pathname, struct stat *buffer);
int pa_open(const char *pathname, int flags, int mode=0);
FILE *pa_fopen(const char *pathname, const char *mode);
#define pa_lseek _lseeki64
#else
#define pa_stat stat
#define pa_fstat fstat
#define pa_open open
#define pa_fopen fopen
#define pa_lseek lseek
#endif
#ifdef WIN32
bool is_os_absolute_path(const String& path);
bool is_os_absolute_path(const char* path);
#else
inline bool is_os_absolute_path(const String&) { return false; }
inline bool is_os_absolute_path(const char*) { return false; }
#endif
#define FILE_BUFFER_SIZE (128*0x400)
int pa_lock_shared_blocking(int fd);
int pa_lock_exclusive_blocking(int fd);
int pa_lock_exclusive_nonblocking(int fd);
int pa_unlock(int fd);
const char *pa_filename(const char *path);
void create_dir_for_file(const String& file_spec);
int pa_get_valid_file_options_count(HashStringValue& options);
typedef void (*File_read_action)(struct stat& finfo, int f, const String& file_spec, void *context);
/**
shared-lock specified file,
do actions under lock.
if fail_on_read_problem is true[default] throws an exception
@returns true if read OK
*/
bool file_read_action_under_lock(const String& file_spec,
const char* action_name, File_read_action action, void *context,
bool as_text=false,
bool fail_on_read_problem=true);
/**
read specified text file
if fail_on_read_problem is true[default] throws an exception
WARNING: charset is used for http header case conversion, it's not a charset of input file!
*/
char *file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem = true);
char *file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem = true, HashStringValue* options = 0, bool transcode_result = true);
struct File_read_result {
bool success;
char* str; size_t length;
HashStringValue* headers;
};
// read specified binary file; if fail_on_read_problem is true[default] throws an exception
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);
// load specified file; if fail_on_read_problem is true[default] throws an exception
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);
typedef void (*File_write_action)(int f, void *context);
/**
lock specified file exclusively, do actions under lock.
throws an exception in case of problems
if block=false does non-blocking lock
@returns true if locked OK, or false if non-blocking locking failed
*/
bool file_write_action_under_lock(
const String& file_spec,
const char* action_name,
File_write_action action,
void *context,
bool as_text=false,
bool do_append=false,
bool do_block=true,
bool fail_on_lock_problem=true);
// write data to specified file; throws an exception in case of problems
void file_write(
Request_charsets& charsets,
const String& file_spec,
const char* data,
size_t size,
bool as_text,
bool do_append=false,
Charset* asked_charset=0);
// delete specified file; throws an exception in case of problems
bool file_delete(const String& file_spec, bool fail_on_problem=true, bool keep_empty_dirs=false);
// move specified file; throw an exception in case of problems
void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs=false);
bool entry_exists(const char* fname, struct stat *afinfo=0);
bool entry_exists(const String& file_spec);
bool file_exist(const String& file_spec);
bool dir_exists(const String& file_spec);
const String* file_exist(const String& path, const String& name);
bool file_executable(const String& file_spec);
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);
size_t check_file_size(uint64_t size, const String* file_spec);
size_t stdout_write(const void *buf, size_t size);
void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname);
ssize_t file_block_read(const int f, void* buffer, const size_t size);
#endif
E-mail: