--- parser3/src/main/pa_common.C 2008/07/15 12:50:11 1.236 +++ parser3/src/main/pa_common.C 2009/01/12 07:15:45 1.239 @@ -26,7 +26,7 @@ * */ -static const char * const IDENT_COMMON_C="$Date: 2008/07/15 12:50:11 $"; +static const char * const IDENT_COMMON_C="$Date: 2009/01/12 07:15:45 $"; #include "pa_common.h" #include "pa_exception.h" @@ -35,6 +35,7 @@ static const char * const IDENT_COMMON_C #include "pa_charsets.h" #include "pa_http.h" #include "pa_request_charsets.h" +#include "pcre.h" // some maybe-undefined constants @@ -102,8 +103,7 @@ char* file_read_text(Request_charsets& c } /// these options were handled but not checked elsewhere, now check them -int pa_get_valid_file_options_count(HashStringValue& options) -{ +int pa_get_valid_file_options_count(HashStringValue& options) { int result=0; if(options.get(PA_SQL_LIMIT_NAME)) result++; @@ -143,7 +143,7 @@ static void file_read_action( *info.data_size=(size_t)read(f, *info.data, to_read_size); if(ssize_t(*info.data_size)<0 || *info.data_size>to_read_size) - throw Exception(0, + throw Exception("file.read", &file_spec, "read failed: actually read %u bytes count not in [0..%u] valid range", *info.data_size, to_read_size); @@ -360,7 +360,7 @@ static void file_write_action(int f, voi if(info.length) { int written=write(f, info.str, info.length); if(written<0) - throw Exception(0, + throw Exception("file.access", 0, "write failed: %s (%d)", strerror(errno), errno); } @@ -389,6 +389,7 @@ static void rmdir(const String& file_spe rmdir(file_spec.mid(0, pos_after-1/* / */).cstr(String::L_FILE_SPEC)); } + bool file_delete(const String& file_spec, bool fail_on_problem) { const char* fname=file_spec.cstr(String::L_FILE_SPEC); if(unlink(fname)!=0) @@ -403,6 +404,7 @@ bool file_delete(const String& file_spec rmdir(file_spec, 1); return true; } + void file_move(const String& old_spec, const String& new_spec) { const char* old_spec_cstr=old_spec.cstr(String::L_FILE_SPEC); const char* new_spec_cstr=new_spec.cstr(String::L_FILE_SPEC); @@ -451,20 +453,24 @@ static bool entry_readable(const String& } return false; } + bool file_exist(const String& file_spec) { return entry_readable(file_spec, false); } + bool dir_exists(const String& file_spec) { return entry_readable(file_spec, true); } + const String* file_exist(const String& path, const String& name) { String& result=*new String(path); result << "/"; result << name; return file_exist(result)?&result:0; } + bool file_executable(const String& file_spec) { - return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0; + return access(file_spec.cstr(String::L_FILE_SPEC), X_OK)==0; } bool file_stat(const String& file_spec, @@ -1175,8 +1181,7 @@ g_mime_utils_base64_decode_step (const u } -char* pa_base64_encode(const char *in, size_t in_size) -{ +char* pa_base64_encode(const char *in, size_t in_size){ /* wont go to more than 2x size (overly conservative) */ char* result=new(PointerFreeGC) char[in_size * 2 + 6]; int state=0; @@ -1192,8 +1197,7 @@ char* pa_base64_encode(const char *in, s } -char* pa_base64_encode(const String& file_spec) -{ +char* pa_base64_encode(const String& file_spec){ unsigned char* base64=0; File_base64_action_info info={&base64}; @@ -1229,8 +1233,7 @@ static void file_base64_file_action( } } -void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size) -{ +void pa_base64_decode(const char *in, size_t in_size, char*& result, size_t& result_size) { /* wont go to more than had (overly conservative) */ result=new(PointerFreeGC) char[in_size+1/*terminator*/]; int state=0; @@ -1246,24 +1249,23 @@ void pa_base64_decode(const char *in, si int file_block_read(const int f, unsigned char* buffer, const size_t size){ int nCount = read(f, buffer, size); if (nCount < 0) - throw Exception(0, + throw Exception("file.read", 0, "read failed: %s (%d)", strerror(errno), errno); return nCount; } -const unsigned long pa_crc32(const char *in, size_t in_size) -{ +const unsigned long pa_crc32(const char *in, size_t in_size){ unsigned long crc32=0xFFFFFFFF; InitCrc32Table(); - for(size_t i = 0; i < in_size; i++) CalcCrc32(in[i], crc32); + for(size_t i = 0; i(context); if(finfo.st_size) { InitCrc32Table(); @@ -1287,3 +1288,13 @@ static void file_crc32_file_action( } } +char* print_pcre_exec_error_text(int exec_result){ + switch(exec_result){ + case PCRE_ERROR_BADUTF8: + return "validation of UTF-8 string failed while pcre_exec (%d)."; + break; + default: + return "regular expression execute error (%d)"; + } +} +