int get_existance_test(int fd, const char *path, int *exists) { int use_cache = (opts & OPT_SHMCACHE) && (shm_cachesize > 0); int found_file, result; char cache_name[MAX_PATH_LEN+2]; char *exist_str; if (use_cache) { debug_printf2("Looking up file existance for %s in shared cache\n", path); get_cache_name(path, "&", cache_name); cache_name[sizeof(cache_name)-1] = '\0'; found_file = fetch_from_cache(cache_name, &exist_str); if (found_file) { *exists = (exist_str[0] == 'y'); return 0; } } result = send_existance_test(fd, (char *) path, exists); if (result == -1) return -1; if (use_cache) { exist_str = *exists ? "y" : "n"; shmcache_update(cache_name, exist_str); } return 0; }
int get_stat_result(int fd, const char *path, int is_lstat, int *exists, struct stat *buf) { int result; char buffer[MAX_PATH_LEN+1]; char cache_name[MAX_PATH_LEN+3]; char *newpath; int use_cache = (opts & OPT_SHMCACHE) && (shm_cachesize > 0); int found_file = 0; if (use_cache) { debug_printf2("Looking up %sstat for %s in shared cache\n", is_lstat ? "l" : "", path); get_cache_name(path, is_lstat ? "**" : "*", cache_name); cache_name[sizeof(cache_name)-1] = '\0'; found_file = fetch_from_cache(cache_name, &newpath); } if (!found_file) { result = send_stat_request(fd, (char *) path, is_lstat, buffer); if (result == -1) { *exists = 0; return -1; } newpath = buffer[0] != '\0' ? buffer : NULL; if (use_cache) shmcache_update(cache_name, newpath); } if (newpath == NULL) { *exists = 0; return 0; } *exists = 1; test_log(newpath); result = read_stat(newpath, buf); if (result == -1) { err_printf("Failed to read stat info for %s from %s\n", path, newpath); *exists = 0; return -1; } return 0; }
int get_relocated_file(int fd, const char *name, char** newname, int *errorcode) { int found_file = 0; int use_cache = (opts & OPT_SHMCACHE) && (shm_cachesize > 0); char cache_name[MAX_PATH_LEN+1]; if (use_cache) { debug_printf2("Looking up %s in shared cache\n", name); get_cache_name(name, "", cache_name); cache_name[sizeof(cache_name)-1] = '\0'; found_file = fetch_from_cache(cache_name, newname); } if (!found_file) { debug_printf2("Send file request to server: %s\n", name); send_file_query(fd, (char *) name, newname, errorcode); debug_printf2("Recv file from server: %s\n", *newname ? *newname : "NONE"); if (use_cache) shmcache_update(cache_name, *newname); } return 0; }
static int do_multiple_queries(bool do_update, bool use_cache, h2o_req_t *req) { thread_context_t * const ctx = H2O_STRUCT_FROM_MEMBER(thread_context_t, event_loop.h2o_ctx, req->conn->ctx); const size_t num_query = get_query_number(req); // MAX_QUERIES is a relatively small number, so assume no overflow in the following // arithmetic operations. assert(num_query <= MAX_QUERIES); size_t base_size = offsetof(multiple_query_ctx_t, res) + num_query * sizeof(query_result_t); base_size = ((base_size + _Alignof(query_param_t) - 1) / _Alignof(query_param_t)); base_size = base_size * _Alignof(query_param_t); const size_t num_query_in_progress = MIN(num_query, ctx->config->max_db_conn_num); size_t sz = base_size + num_query_in_progress * sizeof(query_param_t); if (do_update) { const size_t reuse_size = (num_query_in_progress - 1) * sizeof(query_param_t); const size_t update_query_len = MAX_UPDATE_QUERY_LEN(num_query); if (update_query_len > reuse_size) sz += update_query_len - reuse_size; } multiple_query_ctx_t * const query_ctx = calloc(1, sz); if (query_ctx) { multiple_query_ctx_t ** const p = h2o_mem_alloc_shared(&req->pool, sizeof(*p), cleanup_multiple_query_request); *p = query_ctx; query_ctx->ctx = ctx; query_ctx->num_query = num_query; query_ctx->req = req; query_ctx->do_update = do_update; query_ctx->use_cache = use_cache; query_ctx->query_param = (query_param_t *) ((char *) query_ctx + base_size); initialize_ids(num_query, query_ctx->res, &ctx->random_seed); if (use_cache) { fetch_from_cache(h2o_now(ctx->event_loop.h2o_ctx.loop), &ctx->global_data->world_cache, query_ctx); if (query_ctx->num_result == query_ctx->num_query) { complete_multiple_query(query_ctx); return 0; } } query_ctx->num_query_in_progress = MIN(num_query_in_progress, query_ctx->num_query - query_ctx->num_result); for (size_t i = 0; i < query_ctx->num_query_in_progress; i++) { query_ctx->query_param[i].ctx = query_ctx; // We need a copy of id because the original may be overwritten // by a completed query. query_ctx->query_param[i].id = htonl(query_ctx->res[query_ctx->num_result + i].id); query_ctx->query_param[i].id_format = 1; query_ctx->query_param[i].id_len = sizeof(query_ctx->query_param[i].id); query_ctx->query_param[i].id_pointer = (const char *) &query_ctx->query_param[i].id; query_ctx->query_param[i].param.command = WORLD_TABLE_NAME; query_ctx->query_param[i].param.nParams = 1; query_ctx->query_param[i].param.on_error = on_multiple_query_error; query_ctx->query_param[i].param.on_result = on_multiple_query_result; query_ctx->query_param[i].param.on_timeout = on_multiple_query_timeout; query_ctx->query_param[i].param.paramFormats = &query_ctx->query_param[i].id_format; query_ctx->query_param[i].param.paramLengths = &query_ctx->query_param[i].id_len; query_ctx->query_param[i].param.paramValues = &query_ctx->query_param[i].id_pointer; query_ctx->query_param[i].param.flags = IS_PREPARED; query_ctx->query_param[i].param.resultFormat = 1; if (execute_query(ctx, &query_ctx->query_param[i].param)) { query_ctx->num_query_in_progress = i; query_ctx->cleanup = true; send_service_unavailable_error(DB_REQ_ERROR, req); return 0; } } } else send_error(INTERNAL_SERVER_ERROR, REQ_ERROR, req); return 0; }