static int yajl_parse_map_key_cb(void *data, const unsigned char *stringval, size_t stringlen) { nx_json_parser_ctx_t *ctx; //log_info("map_key_cb: %s", stringval); ctx = (nx_json_parser_ctx_t *) data; if ( ctx->in_map > 0 ) { if ( ctx->tmpstr == NULL ) { throw_msg("tmpstr missing"); } nx_string_append(ctx->tmpstr, "\"", 1); nx_string_append(ctx->tmpstr, (const char *) stringval, (int) stringlen); nx_string_append(ctx->tmpstr, "\":", 2); } else { if ( ctx->key != NULL ) { throw_msg("already got map key"); } ctx->key = malloc(stringlen + 1); memcpy(ctx->key, stringval, stringlen); ctx->key[stringlen] = '\0'; } return ( 1 ); }
void OperCFThread::CreateDirectory( FS* fs, FSPath& srcPath, FSPath& destPath, bool processMultipleFolders ) { if ( processMultipleFolders ) { const int DirIndex = srcPath.GetFirstUnmatchedItem( destPath ); FSPath Path; for ( int i = 0; i < destPath.Count(); i++ ) { // get next dir Path.PushStr( *destPath.GetItem( i ) ); int ret_err; // try to create dir if ( i >= DirIndex && fs->MkDir( Path, 0777, &ret_err, Info() ) ) { // skip "already exists" error if ( !fs->IsEEXIST( ret_err ) ) { throw_msg( "%s", fs->StrError( ret_err ).GetUtf8() ); } } } } else { int ret_err; if ( fs->MkDir( destPath, 0777, &ret_err, Info() ) ) { throw_msg( "%s", fs->StrError( ret_err ).GetUtf8() ); } } }
static void im_ssl_write(nx_module_t *module, nx_event_t *event) { int rv; SSL *ssl; apr_socket_t *sock; nx_module_input_t *input = NULL; int errcode; nx_exception_t e; sock = (apr_socket_t *) event->data; ASSERT(module != NULL); CHECKERR_MSG(apr_socket_data_get((void **) &input, "input", sock), "couldn't get input data from socket"); ASSERT(input != NULL); ssl = (SSL *) nx_module_input_data_get(input, "ssl"); ASSERT(ssl != NULL); if ( !SSL_is_init_finished(ssl) ) { log_debug("doing handshake"); try { if ( (rv = SSL_do_handshake(ssl)) <= 0 ) { switch ( (errcode = nx_ssl_check_io_error(ssl, rv)) ) { case SSL_ERROR_ZERO_RETURN: // disconnected throw_msg("im_ssl got disconnected during handshake"); break; case SSL_ERROR_WANT_WRITE: log_debug("im_ssl WANT_WRITE"); nx_module_pollset_add_socket(module, input->desc.s, APR_POLLOUT | APR_POLLHUP); break; case SSL_ERROR_WANT_READ: log_debug("im_ssl WANT_READ"); nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP); break; default: throw_msg("im_ssl couldn't write handshake data (error code: %d)", errcode); } } } catch(e) { log_exception(e); im_ssl_disconnect(input); return; } } else { log_warn("SSL socket should not be sending anything after the handshake"); nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP); } }
void nx_expr_proc__xm_fileop_file_touch(nx_expr_eval_ctx_t *eval_ctx, nx_module_t *module, nx_expr_arg_list_t *args) { nx_expr_arg_t *arg; nx_value_t file; apr_status_t rv; apr_pool_t *pool; apr_file_t *fd; ASSERT(module != NULL); ASSERT(args != NULL); arg = NX_DLIST_FIRST(args); ASSERT(arg != NULL); ASSERT(arg->expr != NULL); nx_expr_evaluate(eval_ctx, &file, arg->expr); if ( file.defined != TRUE ) { throw_msg("'file' is undef"); } if ( file.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&file); throw_msg("string type required for 'file'"); } pool = nx_pool_create_core(); if ( (rv = apr_file_open(&fd, file.string->buf, APR_WRITE | APR_CREATE, APR_OS_DEFAULT, pool)) != APR_SUCCESS ) { log_aprerror(rv, "failed to open file '%s' when trying to touch", file.string->buf); } if ( rv == APR_SUCCESS ) { apr_file_close(fd); rv = apr_file_mtime_set(file.string->buf, apr_time_now(), pool); if ( rv == APR_SUCCESS ) { } else if ( APR_STATUS_IS_ENOTIMPL(rv) ) { } else { log_aprerror(rv, "failed to set mtime on file '%s' when trying to touch", file.string->buf); } } apr_pool_destroy(pool); nx_value_kill(&file); }
static void parse_args(nx_expr_eval_ctx_t *eval_ctx, apr_pool_t *pool, const char **cmd, const char **argv, nx_expr_arg_list_t *args) { int argc = 0; nx_value_t value; nx_expr_arg_t *arg; ASSERT(args != NULL); arg = NX_DLIST_FIRST(args); ASSERT(arg != NULL); ASSERT(arg->expr != NULL); nx_expr_evaluate(eval_ctx, &value, arg->expr); if ( value.defined != TRUE ) { throw_msg("command is undef"); } if ( value.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&value); throw_msg("string type required for command"); } *cmd = apr_pstrdup(pool, value.string->buf); nx_value_kill(&value); argv[argc] = NULL; for ( ; arg != NULL; arg = NX_DLIST_NEXT(arg, link) ) { if ( argc >= NX_XM_EXEC_MAX_ARGC - 1) { apr_pool_destroy(pool); throw_msg("too many arguments passed to exec(), limit is %d", NX_XM_EXEC_MAX_ARGC - 1); } nx_expr_evaluate(eval_ctx, &value, arg->expr); if ( value.defined != TRUE ) { throw_msg("arg passed to exec() is undef"); } if ( value.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&value); throw_msg("string type required for exec() argument"); } argv[argc] = apr_pstrdup(pool, value.string->buf); nx_value_kill(&value); argc++; argv[argc] = NULL; } }
static int yajl_parse_integer_cb(void *data, long long integerval) { nx_json_parser_ctx_t *ctx; char intstr[32]; ctx = (nx_json_parser_ctx_t *) data; //log_info("integer_cb"); if ( (ctx->in_array > 0) || (ctx->in_map > 0) ) { apr_snprintf(intstr, sizeof(intstr), "%lld", integerval); nx_string_append(ctx->tmpstr, intstr, -1); nx_string_append(ctx->tmpstr, ",", 1); return ( 1 ); } if ( ctx->key == NULL ) { throw_msg("map key name not found"); } nx_logdata_set_integer(ctx->logdata, ctx->key, (int64_t) integerval); free(ctx->key); ctx->key = NULL; return ( 1 ); }
static int yajl_parse_string_cb(void *data, const unsigned char *stringval, size_t stringlen) { nx_json_parser_ctx_t *ctx; nx_value_t *val; //log_info("string_cb: %s [%d]", stringval, stringlen); ctx = (nx_json_parser_ctx_t *) data; if ( (ctx->in_array > 0) || (ctx->in_map > 0) ) { nx_string_append(ctx->tmpstr, (const char *) stringval, (int) stringlen); nx_string_append(ctx->tmpstr, ",", 1); return ( 1 ); } if ( ctx->key == NULL ) { throw_msg("map key name not found"); } if ( strcmp(ctx->key, "raw_event") != 0 ) { // setting raw event would cause a free() on our buffer being parsed, ignore it val = nx_value_new(NX_VALUE_TYPE_STRING); val->string = nx_string_create((const char *) stringval, (int) stringlen); nx_logdata_set_field_value(ctx->logdata, ctx->key, val); } free(ctx->key); ctx->key = NULL; return ( 1 ); }
static int yajl_parse_null_cb(void *data) { nx_json_parser_ctx_t *ctx; nx_value_t *val; //log_info("null_cb"); ctx = (nx_json_parser_ctx_t *) data; if ( (ctx->in_array > 0) || (ctx->in_map > 0) ) { nx_string_append(ctx->tmpstr, "null,", 5); return ( 1 ); } if ( ctx->key == NULL ) { throw_msg("map key name not found"); } val = nx_value_new(NX_VALUE_TYPE_STRING); val->defined = FALSE; nx_logdata_set_field_value(ctx->logdata, ctx->key, val); free(ctx->key); ctx->key = NULL; return ( 1 ); }
static int yajl_parse_start_map_cb(void *data) { nx_json_parser_ctx_t *ctx; //log_info("start_map_cb"); ctx = (nx_json_parser_ctx_t *) data; if ( ctx->in_array != 0 ) { throw_msg("unexpected in_array"); } if ( ctx->key != NULL ) { (ctx->in_map)++; if ( ctx->tmpstr == NULL ) { ctx->tmpstr = nx_string_new(); } nx_string_append(ctx->tmpstr, "{", 1); } return ( 1 ); }
void nx_expr_proc__xm_fileop_dir_remove(nx_expr_eval_ctx_t *eval_ctx, nx_module_t *module, nx_expr_arg_list_t *args) { nx_expr_arg_t *arg; nx_value_t path; apr_status_t rv; apr_pool_t *pool; ASSERT(module != NULL); ASSERT(args != NULL); arg = NX_DLIST_FIRST(args); ASSERT(arg != NULL); ASSERT(arg->expr != NULL); nx_expr_evaluate(eval_ctx, &path, arg->expr); if ( path.defined != TRUE ) { throw_msg("'path' is undef"); } if ( path.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&path); throw_msg("string type required for 'path'"); } // TODO: remove contents before pool = nx_pool_create_core(); rv = apr_dir_remove(path.string->buf, pool); if ( APR_STATUS_IS_ENOENT(rv) ) { } else if ( rv == APR_SUCCESS ) { } else { log_aprerror(rv, "failed to remove directory '%s'", path.string->buf); } apr_pool_destroy(pool); nx_value_kill(&path); }
static void im_exec_fill_buffer(nx_module_input_t *input) { apr_status_t rv; apr_size_t len; ASSERT(input != NULL); ASSERT(input->buf != NULL); ASSERT(input->module != NULL); ASSERT(input->desc_type == APR_POLL_FILE); ASSERT(input->desc.f != NULL); if ( input->bufstart == input->bufsize ) { input->bufstart = 0; input->buflen = 0; } if ( input->buflen == 0 ) { input->bufstart = 0; } ASSERT(input->bufstart + input->buflen <= input->bufsize); len = (apr_size_t) (input->bufsize - (input->buflen + input->bufstart)); rv = apr_file_read(input->desc.f, input->buf + input->bufstart + input->buflen, &len); if ( rv != APR_SUCCESS ) { if ( APR_STATUS_IS_EOF(rv) ) { throw_msg("Module %s got EOF, process exited? ", input->module->name); } else if ( APR_STATUS_IS_EAGAIN(rv) ) { #ifdef WIN32 // on windows EAGAIN is normal because we are using NON-BLOCKING reads // so we try again after 500 ms im_exec_add_read_event(input->module, 500); log_debug("got EAGAIN"); #else log_error("got EAGAIN for blocking read in module %s", input->module->name); #endif ASSERT(len == 0); return; } else { throw(rv, "Module %s couldn't read from pipe", input->module->name); } } else { log_debug("im_exec read %d bytes", (int) len); } input->buflen += (int) len; ASSERT(input->buflen <= input->bufsize); }
void VSearcher::Set( unicode_t* uStr, bool sens, charset_struct* charset ) //throw { sBigList.clear(); sList.clear(); bytes.clear(); _cs = charset; mode = 0; int maxSymLen = 0; bool one = true; int i; for ( ; *uStr; uStr++ ) { SBigNode node; if ( !node.Set( *uStr, sens, charset ) ) { unicode_t x[2] = { *uStr, 0}; throw_msg( "symbol '%s' not exist in charset '%s'", unicode_to_utf8( x ).data(), charset->name ); } int n = node.MaxLen(); if ( maxSymLen < n ) { maxSymLen = n; } if ( node.b[0] ) { one = false; } sBigList.append( node ); } sBigList.append( SBigNode() ); mode = 1; if ( one ) { for ( i = 0; i < sBigList.count(); i++ ) { for ( char* s = sBigList[i].a; *s; s++ ) { bytes.append( *s ); } } mode = 3; } else if ( maxSymLen == 1 ) { for ( i = 0; i < sBigList.count(); i++ ) { sList.append( SNode( sBigList[i].a[0], sBigList[i].b[0] ) ); } mode = 2; } }
static void nx_pattern_matchfield_compile(apr_pool_t *pool, nx_pattern_matchfield_t *matchfield) { const char *error = NULL; int erroroffs = 0; int size; pcre *tmppcre; int rc; int capturecount; matchfield->regexp = pcre_compile(matchfield->value, 0, &error, &erroroffs, NULL); if ( matchfield->regexp == NULL ) { throw_msg("failed to compile regular expression '%s', error at position %d: %s", matchfield->value, erroroffs, error); } rc = pcre_fullinfo(matchfield->regexp, NULL, PCRE_INFO_SIZE, &size); if ( rc < 0 ) { pcre_free(matchfield->regexp); throw_msg("failed to get compiled regexp size"); } rc = pcre_fullinfo(matchfield->regexp, NULL, PCRE_INFO_CAPTURECOUNT, &capturecount); if ( rc < 0 ) { pcre_free(matchfield->regexp); throw_msg("failed to get regexp captured count"); } if ( capturecount >= NX_PATTERNDB_MAX_CAPTURED_FIELDS ) { pcre_free(matchfield->regexp); throw_msg("maximum number of captured substrings is limited to %d", NX_PATTERNDB_MAX_CAPTURED_FIELDS); } ASSERT(size > 0); ASSERT(pool != NULL); tmppcre = apr_palloc(pool, (apr_size_t) size); memcpy(tmppcre, matchfield->regexp, (apr_size_t) size); pcre_free(matchfield->regexp); matchfield->regexp = tmppcre; //TODO: use pcre_study for further speed optimization }
static int yajl_parse_end_map_cb(void *data) { nx_json_parser_ctx_t *ctx; //log_info("end_map_cb"); ctx = (nx_json_parser_ctx_t *) data; if ( ctx->in_array != 0 ) { throw_msg("unexpected in_array"); } if ( ctx->key != NULL ) { (ctx->in_map)--; if ( ctx->in_map < 0 ) { throw_msg("in_map is negative"); } if ( ctx->tmpstr == NULL ) { throw_msg("tmpstr missing"); } if ( ctx->tmpstr->len > 2 ) { // chop trailing comma (ctx->tmpstr->len)--; } nx_string_append(ctx->tmpstr, "}", 1); if ( ctx->in_map == 0 ) { nx_logdata_set_string(ctx->logdata, ctx->key, ctx->tmpstr->buf); nx_string_free(ctx->tmpstr); ctx->tmpstr = NULL; free(ctx->key); ctx->key = NULL; } } return ( 1 ); }
static int yajl_parse_end_array_cb(void *data) { nx_json_parser_ctx_t *ctx; //log_info("end_array_cb"); ctx = (nx_json_parser_ctx_t *) data; if ( ctx->in_array == 0 ) { throw_msg("in_array expected"); } if ( ctx->tmpstr == NULL ) { throw_msg("tmpstr missing"); } if ( ctx->tmpstr->len > 2 ) { // chop trailing comma (ctx->tmpstr->len)--; } nx_string_append(ctx->tmpstr, "]", 1); (ctx->in_array)--; if ( ctx->in_array == 0 ) { if ( ctx->key == NULL ) { throw_msg("map key name not found"); } nx_logdata_set_string(ctx->logdata, ctx->key, ctx->tmpstr->buf); nx_string_free(ctx->tmpstr); ctx->tmpstr = NULL; free(ctx->key); ctx->key = NULL; } return ( 1 ); }
void nx_pattern_add_matchfield(apr_pool_t *pool, nx_pattern_t *pattern, nx_pattern_matchfield_t *matchfield) { int rc; int capturedcnt = 0, cnt; nx_pattern_capturedfield_t *capturedfield; ASSERT(pattern != NULL); ASSERT(matchfield != NULL); if ( matchfield->type == NX_PATTERN_MATCH_TYPE_REGEXP ) { nx_pattern_matchfield_compile(pool, matchfield); NX_DLIST_INSERT_TAIL(pattern->matchfields, matchfield, link); for ( capturedfield = NX_DLIST_FIRST(matchfield->capturedfields); capturedfield != NULL; capturedfield = NX_DLIST_NEXT(capturedfield, link) ) { capturedcnt++; } rc = pcre_fullinfo(matchfield->regexp, NULL, PCRE_INFO_CAPTURECOUNT, &cnt); if ( rc < 0 ) { pcre_free(matchfield->regexp); throw_msg("failed to get captured count"); } if ( capturedcnt != cnt ) { throw_msg("number of captured fields (%d) does no match value (%d) reported by regexp engine for pattern %ld", capturedcnt, cnt, pattern->id); } matchfield->capturedfield_cnt = cnt; } else { // for optimazition purposes we insert EXACT matches in front because it is faster to match NX_DLIST_INSERT_HEAD(pattern->matchfields, matchfield, link); } }
void nx_patterngroup_add_pattern(nx_patterngroup_t *group, nx_pattern_t *pattern) { ASSERT(group != NULL); ASSERT(pattern != NULL); if ( NX_DLIST_FIRST(pattern->matchfields) == NULL ) { throw_msg("pattern has no matchfields"); } pattern->group = group; NX_DLIST_INSERT_TAIL(group->patterns, pattern, link); }
void OperRDThread::Run() { int n = 8; int ret_err; while (true) { if (!(fs->Flags() & FS::HAVE_SYMLINK)) break; FSStat st; if (fs->Stat(path, &st, &ret_err, Info())) throw_msg("%s", fs->StrError(ret_err).GetUtf8()); if (!st.IsLnk()) break; n--; if (n<0) throw_msg("too more symbolic links '%s'", path.GetUtf8()); path.Pop(); if (!ParzeLink(path, st.link)) throw_msg("invalid symbolic link '%s'", path.GetUtf8()); } cptr<FSList> list = new FSList; int ret = fs->ReadDir(list.ptr(), path, &ret_err, Info()); if (ret) throw_msg("%s", fs->StrError(ret_err).GetUtf8()); MutexLock lock(Node().GetMutex()); //!!! if (Node().NBStopped()) return; OperRDData *data = ((OperRDData*)Node().Data()); data->list = list; data->path = path; data->executed = true; }
nx_pattern_match_type_t nx_pattern_match_type_from_string(const char *typestr) { ASSERT(typestr != NULL); if ( strcasecmp(typestr, "EXACT") == 0 ) { return ( NX_PATTERN_MATCH_TYPE_EXACT ); } if ( strcasecmp(typestr, "REGEXP") == 0 ) { return ( NX_PATTERN_MATCH_TYPE_REGEXP ); } throw_msg("invalid match type '%s'", typestr); }
bool FileAttributesDlg( NCDialogParent* Parent, PanelWin* Panel ) { clFileAttributesWin Dialog( Parent, Panel ); if ( Dialog.DoModal( ) == CMD_OK ) { FSNode* Node = Dialog.GetNode(); // apply changes if ( Node ) { FSPath fspath; FSString URI = Dialog.GetURI(); clPtr<FS> fs = ParzeURI( URI.GetUnicode(), fspath, {} ); if ( fs ) { int Err = 0; FSCInfo Info; fs->StatSetAttr( fspath, &Node->st, &Err, &Info ); if ( Err != 0 ) { throw_msg( "Error setting file attributes: %s", fs->StrError( Err ).GetUtf8() ); } fs->SetFileTime( fspath, Node->GetCreationTime(), Node->GetLastAccessTime(), Node->GetLastWriteTime(), &Err, &Info ); if ( Err != 0 ) { throw_msg("Error setting file date & time: %s", fs->StrError(Err).GetUtf8()); } } } return true; } return false; }
void nx_expr_proc__xm_fileop_dir_make(nx_expr_eval_ctx_t *eval_ctx, nx_module_t *module, nx_expr_arg_list_t *args) { nx_expr_arg_t *arg; nx_value_t path; apr_status_t rv; apr_pool_t *pool; ASSERT(module != NULL); ASSERT(args != NULL); arg = NX_DLIST_FIRST(args); ASSERT(arg != NULL); ASSERT(arg->expr != NULL); nx_expr_evaluate(eval_ctx, &path, arg->expr); if ( path.defined != TRUE ) { throw_msg("'path' is undef"); } if ( path.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&path); throw_msg("string type required for 'path'"); } pool = nx_pool_create_core(); if ( (rv = apr_dir_make_recursive(path.string->buf, APR_OS_DEFAULT, pool)) != APR_SUCCESS ) { log_aprerror(rv, "failed to create directory '%s'", path.string->buf); } apr_pool_destroy(pool); nx_value_kill(&path); }
/** * the THROW impl */ void p4_throwstr (int id, const char* description) { p4_Except *frame = catchframe; char msg[256]; char* addr = (char*) description; int len = 0; if (description) len = strlen(description); if (frame && frame->magic == P4_EXCEPTION_MAGIC) { IP = frame->ipp; SP = frame->spp; #if defined PF_WITH_FLOATING FP = frame->fpp; #endif RP = frame->rpp; longjmp (frame->jmp, id); } *--RP = IP; CSP = (p4cell*) RP; /* come_back marker */ switch (id) { case P4_ON_ABORT_QUOTE: { show_error (addr); } case P4_ON_ABORT: pf_longjmp_abort (); case P4_ON_QUIT: pf_longjmp_quit (); default: throw_msg (id, msg); if (addr) { strcat (msg, " : "); if (! len) strcat (msg, addr); else { msg[len+strlen(msg)] = '\0'; strncat (msg, addr, len); } } show_error (msg); } }
static int yajl_parse_boolean_cb(void *data, int boolval) { nx_json_parser_ctx_t *ctx; nx_value_t *val; //log_info("boolean_cb"); ctx = (nx_json_parser_ctx_t *) data; if ( (ctx->in_array > 0) || (ctx->in_map > 0) ) { if ( boolval == 0 ) { nx_string_append(ctx->tmpstr, "FALSE,", 6); } else { nx_string_append(ctx->tmpstr, "TRUE,", 6); } return ( 1 ); } if ( ctx->key == NULL ) { throw_msg("map key name not found"); } val = nx_value_new(NX_VALUE_TYPE_BOOLEAN); if ( boolval != 0 ) { val->boolean = TRUE; } else { val->boolean = FALSE; } nx_logdata_set_field_value(ctx->logdata, ctx->key, val); free(ctx->key); ctx->key = NULL; return ( 1 ); }
static int yajl_parse_start_array_cb(void *data) { nx_json_parser_ctx_t *ctx; //log_info("start_array_cb"); ctx = (nx_json_parser_ctx_t *) data; (ctx->in_array)++; if ( ctx->key == NULL ) { throw_msg("map key name not found"); } if ( ctx->tmpstr == NULL ) { ctx->tmpstr = nx_string_new(); } nx_string_append(ctx->tmpstr, "[", 1); return ( 1 ); }
static void xm_charconv_config(nx_module_t *module) { const nx_directive_t *curr; nx_xm_charconv_conf_t *modconf; char *list; const char *charset; int i; curr = module->directives; modconf = apr_pcalloc(module->pool, sizeof(nx_xm_charconv_conf_t)); module->config = modconf; while ( curr != NULL ) { if ( nx_module_common_keyword(curr->directive) == TRUE ) { } else if ( strcasecmp(curr->directive, "AutodetectCharsets") == 0 ) { list = apr_pstrdup(module->pool, curr->args); for ( i = 0; (charset = list_parse(&list)) != NULL; i++ ) { log_debug("adding xm_charconv charset: %s", charset); if ( i >= NX_XM_CHARCONV_MAX_CHARSETS - 1 ) { throw_msg("maximum number of charsets reached, limit is %d", NX_XM_CHARCONV_MAX_CHARSETS); } modconf->autocharsets[i] = charset; } modconf->num_charsets = i; } curr = curr->next; } log_debug("locale charset: %s", nx_get_locale_charset()); }
static int yajl_parse_number_cb(void *data, const char *s, size_t l) { nx_json_parser_ctx_t *ctx; long int intval; nx_value_t *val; //log_info("number_cb"); ctx = (nx_json_parser_ctx_t *) data; if ( (ctx->in_array > 0) || (ctx->in_map > 0) ) { nx_string_append(ctx->tmpstr, s, (int) l); nx_string_append(ctx->tmpstr, ",", 1); return ( 1 ); } if ( ctx->key == NULL ) { throw_msg("map key name not found"); } if ( sscanf(s, "%ld", &intval) != 1 ) { val = nx_value_new(NX_VALUE_TYPE_STRING); val->string = nx_string_create(s, (int) l); nx_logdata_set_field_value(ctx->logdata, ctx->key, val); } else { nx_logdata_set_integer(ctx->logdata, ctx->key, (int64_t) intval); } free(ctx->key); ctx->key = NULL; return ( 1 ); }
void OperRDThread::Run() { if ( !fs.Ptr() ) { return; } int n = 8; int ret_err; int havePostponedStatError = 0; FSString postponedStrError; while ( true ) { if ( !( fs->Flags() & FS::HAVE_SYMLINK ) ) { break; } FSStat st; // if path is inaccessible, try .. path. Throw the exception later // This makes panel at least having some valid folder while ( fs->Stat( path, &st, &ret_err, Info() ) ) { havePostponedStatError = 1; postponedStrError = fs->StrError( ret_err ); if ( !path.IsAbsolute() || path.Count() <= 1 || !path.Pop() ) { throw_msg( "%s", postponedStrError.GetUtf8() ); } } // yell immediately if the path is inaccessible (orig behavior) //if (fs->Stat(path, &st, &ret_err, Info())) // throw_msg("%s", fs->StrError(ret_err).GetUtf8()); if ( !st.IsLnk() ) { break; } n--; if ( n < 0 ) { throw_msg( "too many symbolic links '%s'", path.GetUtf8() ); } path.Pop(); if ( !ParzeLink( path, st.link ) ) { throw_msg( "invalid symbolic link '%s'", path.GetUtf8() ); } } clPtr<FSList> list = new FSList; int havePostponedReadError = 0; // if directory is not readable, try .. path. Throw the exception later // "Stat" call above does not catch this: it checks only folder existence, but not accessibilly while ( fs->ReadDir( list.ptr(), path, &ret_err, Info() ) ) { havePostponedReadError = 1; postponedStrError = fs->StrError( ret_err ); if ( !path.IsAbsolute() || path.Count() <= 1 || !path.Pop() ) { throw_msg( "%s", postponedStrError.GetUtf8() ); } } // yell immediately if the dir is unreadable (orig behavior) //int ret = fs->ReadDir(list.ptr(), path, &ret_err, Info()); //if (ret) // throw_msg("%s", fs->StrError(ret_err).GetUtf8()); FSStatVfs vst; fs->StatVfs( path, &vst, &ret_err, Info() ); MutexLock lock( Node().GetMutex() ); //!!! if ( Node().NBStopped() ) { return; } OperRDData* data = ( ( OperRDData* )Node().Data() ); data->list = list; data->path = path; data->executed = true; data->vst = vst; if ( havePostponedReadError || havePostponedStatError ) { data->nonFatalErrorString = postponedStrError; } }
static void parse_kvp(nx_logdata_t *logdata, nx_kvp_ctx_t *ctx, const char *src, size_t len) { nx_string_t *valuestr = NULL; nx_exception_t e; nx_kvp_state_t state = NX_KVP_STATE_KEY_START; char keyname[256]; int keylen = 0; volatile boolean got_keyquote = FALSE; try { int pos = 0; boolean got_valquote = FALSE; for ( pos = 0; pos < (int) len; pos++ ) { // log_info("parse [%c] state: %d", src[pos], state); switch ( state ) { case NX_KVP_STATE_KEY: if ( src[pos] == ctx->keyquotechar ) { if ( got_keyquote == TRUE ) { state = NX_KVP_STATE_KV_DELIMITER; } else { throw_msg("invalid key quotation in key-value pair"); } } else if ( (ctx->kvdelimiter == '\0') && IS_KVDELIMITERCHAR(src[pos]) ) { ctx->kvdelimiter = src[pos]; state = NX_KVP_STATE_VALUE_START; } else if ( src[pos] == ctx->kvdelimiter ) { state = NX_KVP_STATE_VALUE_START; } else if ( src[pos] == ctx->escapechar ) { state = NX_KVP_STATE_KEY_ESCAPE; } /* Unquoted keys containing space don't work with this, so instead we trim the space at the end else if ( (src[pos] == ' ') && (got_keyquote != TRUE) ) { state = NX_KVP_STATE_KV_DELIMITER; } */ else { if ( keylen < (int) sizeof(keyname) - 1 ) { keyname[keylen] = src[pos]; keylen++; } } break; case NX_KVP_STATE_VALUE_START: if ( src[pos] == ' ' ) { // skip leading space break; } got_valquote = FALSE; ASSERT(valuestr == NULL); valuestr = nx_string_new(); if ( (ctx->valquotechar == '\0') && IS_QUOTECHAR(src[pos]) ) { // auto-detected quotechar ctx->valquotechar = src[pos]; got_valquote = TRUE; } else if ( src[pos] == ctx->valquotechar ) { got_valquote = TRUE; } else { pos--; // handle character in STATE_VALUE } state = NX_KVP_STATE_VALUE; break; case NX_KVP_STATE_VALUE: if ( src[pos] == ctx->valquotechar ) { state = NX_KVP_STATE_KVP_DELIMITER; if ( got_valquote == TRUE ) { } else { if ( valuestr->len > 0) { throw_msg("invalid value quotation in key-value pair: %s", valuestr->buf); } } } else if ( (ctx->kvpdelimiter == '\0') && IS_KVPDELIMITERCHAR(src[pos]) && (got_valquote == FALSE) ) { ctx->kvpdelimiter = src[pos]; // add field-value add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr); valuestr = NULL; state = NX_KVP_STATE_KEY_START; } else if ( (src[pos] == ctx->kvpdelimiter) && (got_valquote == FALSE) ) { // add field-value add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr); valuestr = NULL; state = NX_KVP_STATE_KEY_START; } else if ( src[pos] == ctx->escapechar ) { state = NX_KVP_STATE_VALUE_ESCAPE; } else { // append character to value nx_string_append(valuestr, src + pos, 1); } break; case NX_KVP_STATE_VALUE_ESCAPE: unescape_value(ctx, src[pos], valuestr, got_valquote); state = NX_KVP_STATE_VALUE; break; case NX_KVP_STATE_KV_DELIMITER: if ( src[pos] == ' ' ) { // skip space break; } if ( (ctx->kvdelimiter == '\0') && IS_KVDELIMITERCHAR(src[pos]) ) { ctx->kvdelimiter = src[pos]; state = NX_KVP_STATE_VALUE_START; } else if ( src[pos] == ctx->kvdelimiter ) { state = NX_KVP_STATE_VALUE_START; } break; case NX_KVP_STATE_KVP_DELIMITER: if ( (ctx->kvpdelimiter == '\0') && IS_KVPDELIMITERCHAR(src[pos]) ) { ctx->kvpdelimiter = src[pos]; state = NX_KVP_STATE_KEY_START; add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr); valuestr = NULL; } else if ( src[pos] == ctx->kvpdelimiter ) { state = NX_KVP_STATE_KEY_START; add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr); valuestr = NULL; } break; case NX_KVP_STATE_KEY_START: keylen = 0; got_keyquote = FALSE; if ( src[pos] == ' ' ) { // skip space break; } else if ( (ctx->keyquotechar == '\0') && IS_QUOTECHAR(src[pos]) ) { // auto-detected quotechar ctx->keyquotechar = src[pos]; got_keyquote = TRUE; state = NX_KVP_STATE_KEY; } else if ( src[pos] == ctx->keyquotechar ) { got_keyquote = TRUE; state = NX_KVP_STATE_KEY; } else if ( src[pos] == ctx->escapechar ) { state = NX_KVP_STATE_KEY_ESCAPE; } else if ( src[pos] == ctx->kvpdelimiter ) { // double delimiter, no value for kvp, skip } else { // first character of unquoted key state = NX_KVP_STATE_KEY; if ( keylen < (int) sizeof(keyname) - 1 ) { keyname[keylen] = src[pos]; keylen++; } } break; case NX_KVP_STATE_KEY_ESCAPE: if ( (src[pos] == ctx->escapechar) || (src[pos] == ctx->keyquotechar) ) { if ( keylen < (int) sizeof(keyname) - 1 ) { keyname[keylen] = src[pos]; keylen++; } } else { if ( keylen < (int) sizeof(keyname) - 2 ) { keyname[keylen] = ctx->escapechar; keylen++; keyname[keylen] = src[pos]; keylen++; } } state = NX_KVP_STATE_KEY; break; default: nx_panic("invalid state %d", state); } } } catch(e) { if ( valuestr != NULL ) { nx_string_free(valuestr); } rethrow(e); } switch ( state ) { case NX_KVP_STATE_VALUE: case NX_KVP_STATE_KVP_DELIMITER: add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr); break; case NX_KVP_STATE_KEY_START: ASSERT(valuestr == NULL); break; case NX_KVP_STATE_KV_DELIMITER: case NX_KVP_STATE_KEY: case NX_KVP_STATE_KEY_ESCAPE: case NX_KVP_STATE_VALUE_START: case NX_KVP_STATE_VALUE_ESCAPE: if ( valuestr != NULL ) { nx_string_free(valuestr); } throw_msg("invalid KVP input: '%s' [state: %d]", src, state); break; default: nx_panic("invalid state %d", state); } }
void nx_expr_proc__xm_fileop_file_chmod(nx_expr_eval_ctx_t *eval_ctx, nx_module_t *module, nx_expr_arg_list_t *args) { nx_expr_arg_t *arg; nx_value_t file; nx_expr_arg_t *mode; nx_value_t modeval; nx_exception_t e; int rv; ASSERT(module != NULL); ASSERT(args != NULL); arg = NX_DLIST_FIRST(args); ASSERT(arg != NULL); ASSERT(arg->expr != NULL); mode = NX_DLIST_NEXT(arg, link); ASSERT(mode != NULL); ASSERT(mode->expr != NULL); nx_expr_evaluate(eval_ctx, &file, arg->expr); if ( file.defined != TRUE ) { throw_msg("'file' is undef"); } if ( file.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&file); throw_msg("string type required for 'file'"); } try { nx_expr_evaluate(eval_ctx, &modeval, mode->expr); } catch(e) { nx_value_kill(&file); rethrow(e); } if ( modeval.defined != TRUE ) { nx_value_kill(&file); throw_msg("'mode' is undef"); } if ( modeval.type != NX_VALUE_TYPE_INTEGER ) { nx_value_kill(&file); throw_msg("integer type required for 'mode'"); } #ifdef HAVE_CHMOD if ( (rv = chmod(file.string->buf, (mode_t) modeval.integer)) != 0 ) { log_aprerror(rv, "failed to change file ownership on '%s'", file.string->buf); } #else log_error("This platform does not support the file_chown() function"); #endif nx_value_kill(&file); }
void nx_expr_proc__xm_fileop_file_cycle(nx_expr_eval_ctx_t *eval_ctx, nx_module_t *module, nx_expr_arg_list_t *args) { nx_expr_arg_t *arg; nx_value_t file; nx_expr_arg_t *val; nx_value_t value; apr_status_t rv; apr_pool_t *pool = NULL; nx_exception_t e; volatile int64_t max = 0; int32_t i, last; nx_string_t *tmpstr = NULL; nx_string_t *tmpstr2 = NULL; ASSERT(module != NULL); ASSERT(args != NULL); arg = NX_DLIST_FIRST(args); ASSERT(arg != NULL); ASSERT(arg->expr != NULL); nx_expr_evaluate(eval_ctx, &file, arg->expr); if ( file.defined != TRUE ) { throw_msg("'file' is undef"); } if ( file.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&file); throw_msg("string type required for 'file'"); } val = NX_DLIST_NEXT(arg, link); if ( val != NULL ) { ASSERT(val->expr != NULL); nx_expr_evaluate(eval_ctx, &value, val->expr); if ( value.type != NX_VALUE_TYPE_INTEGER ) { nx_value_kill(&file); nx_value_kill(&value); throw_msg("integer type required for 'max'"); } if ( value.defined == TRUE ) { if ( value.integer <= 0 ) { nx_value_kill(&file); nx_value_kill(&value); throw_msg("'max' must be a positive integer"); } max = value.integer; } } try { pool = nx_pool_create_core(); if ( _file_exists(file.string->buf, pool) == TRUE ) { // check if the file we need to cycle exists tmpstr = nx_string_new(); tmpstr2 = nx_string_new(); last = 0; for ( i = 1; i < 2147483647 /*APR_INT32_MAX*/; i++ ) { nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, i); if ( _file_exists(tmpstr->buf, pool) == FALSE ) { break; } if ( (max > 0) && (i >= max) ) { log_info("removing file %s", tmpstr->buf); if ( (rv = apr_file_remove(tmpstr->buf, NULL)) != APR_SUCCESS ) { log_aprerror(rv, "failed to remove file '%s'", tmpstr->buf); } } else { last = i; } } if ( last > 0 ) { // now starting from the last existing file, cycle them for ( i = last; i > 0; i-- ) { nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, i); nx_string_sprintf(tmpstr2, "%s.%d", file.string->buf, i + 1); log_debug("cycling %s to %s", tmpstr->buf, tmpstr2->buf); if ( (rv = apr_file_rename(tmpstr->buf, tmpstr2->buf, NULL)) != APR_SUCCESS ) { log_aprerror(rv, "failed to rename file from '%s' to '%s'", tmpstr->buf, tmpstr2->buf); } } } // finally rename file to file.1 nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, 1); if ( (rv = apr_file_rename(file.string->buf, tmpstr->buf, NULL)) != APR_SUCCESS ) { log_aprerror(rv, "failed to rename file from '%s' to '%s'", file.string->buf, tmpstr->buf); } _reopen_logfile(file.string->buf); nx_string_free(tmpstr); nx_string_free(tmpstr2); } apr_pool_destroy(pool); nx_value_kill(&file); } catch(e) { nx_value_kill(&file); if ( pool != NULL ) { apr_pool_destroy(pool); } if ( tmpstr != NULL ) { nx_string_free(tmpstr); } if ( tmpstr2 != NULL ) { nx_string_free(tmpstr2); } log_exception(e); } }