//读取远程文件的数据 int fileclient::readfile(const char *remote_file, FileData *file_data) { if (!_connected) { if (connection() < 0) return SOCK_CONNECT_ERROR; } file_data->len = 0; file_data->data = NULL; bigreadreqall read_req(remote_file, _seq++); int msg_len = sizeof(bigreadreqall); if (msg_len != send_tmout(_sock_fd, (const char*) &read_req, msg_len, 5000)) { return SOCK_ERROR; } bigreadrspall resp; int recv_len = sizeof(resp); if (recv_len != recv_tmout(_sock_fd, (char *) &resp, recv_len, 5000)) { return SOCK_ERROR; } if (resp.rsp.result != 0) { return FILE_FAIL; } unsigned int data_len = ntohl(resp.rsp.data_len); if (data_len > 10 * 1024 * 1024) { printf("getfile data len is too big : %d \n", data_len); return FILE_FAIL; } file_data->data = (char*) malloc(data_len); if (file_data->data == NULL) { printf("fileclient::readfile malloc fail \n"); return FILE_FAIL; } if (data_len != (unsigned int)recv_tmout(_sock_fd, (char *) file_data->data, data_len, 5000)) { free(file_data->data); return SOCK_ERROR; } file_data->len = data_len; return FILE_SUCCESS; }
void FileNameHandler::resolve(std::tr1::shared_ptr<MetadataRequest> request, NameCallback callback) { URL url(request->getURI()); assert(!url.empty()); Filesystem::Path fileRequested(url.fullpath()); std::tr1::shared_ptr<DiskManager::DiskRequest> read_req( new DiskManager::ReadRequest(fileRequested, std::tr1::bind(&FileNameHandler::onReadFinished, this, _1, request, callback))); DiskManager::getSingleton().addRequest(read_req); }
void FileChunkHandler::cache_check_callback(const SparseData* data, std::tr1::shared_ptr<RemoteFileMetadata> file, std::tr1::shared_ptr<Chunk> chunk, ChunkCallback callback) { if (data) { mStats.downloaded++; std::tr1::shared_ptr<const DenseData> flattened = data->flatten(); callback(flattened); } else { URL url(file->getURI()); assert(!url.empty()); Filesystem::Path fileRequested(url.fullpath()); std::tr1::shared_ptr<DiskManager::DiskRequest> read_req( new DiskManager::ReadRequest(fileRequested, std::tr1::bind(&FileChunkHandler::onReadFinished, this, _1, file, chunk, callback))); DiskManager::getSingleton().addRequest(read_req); } }
int WorkTodo::read_file( const char *fileName ) { int retVal = -1; TwsXml file; if( ! file.openFile(fileName) ) { return retVal; } retVal = 0; xmlNodePtr xn; while( (xn = file.nextXmlNode()) != NULL ) { assert( xn->type == XML_ELEMENT_NODE ); if( strcmp((char*)xn->name, "request") == 0 ) { retVal += read_req( xn ); } else { fprintf(stderr, "Warning, unknown request tag '%s' ignored.\n", xn->name ); } } return retVal; }
int main(int ac, char ** av){ int x = read_req(); printf("x=%d\n",x); }
/** * Write the data in the request to the page. * @orig: the very original request issued by the user. It may span * multiple pages. */ ssize_t global_cached_io::__write(original_io_request *orig, thread_safe_page *p, std::vector<thread_safe_page *> &dirty_pages) { ssize_t ret = 0; p->lock(); assert(!p->is_old_dirty()); if (!p->data_ready()) { if(!p->is_io_pending()) { assert(!p->is_dirty()); assert(orig->has_overlap(p->get_offset(), PAGE_SIZE)); // We are going to write to part of a page, therefore, // we need to first read the page. if (orig->get_offset() > p->get_offset() || orig->get_offset() + orig->get_size() < p->get_offset() + PAGE_SIZE) { off_t off = orig->get_offset(); data_loc_t pg_loc(orig->get_file_id(), ROUND_PAGE(off)); io_req_extension *ext = ext_allocator->alloc_obj(); io_request read_req(ext, pg_loc, READ, this, p->get_node_id()); read_req.add_page(p); read_req.set_priv(p); assert(p->get_io_req() == NULL); p->set_io_pending(true); p->add_req(orig); p->unlock(); send2underlying(read_req); } else { // This is an optimization. If we can overwrite the entire page, // we don't need to read the page first. However, we have to // make sure data is written to a page without anyone else // having IO operations on it. p->set_data_ready(true); thread_safe_page *dirty = __complete_req_unlocked(orig, p); if (dirty) dirty_pages.push_back(dirty); p->unlock(); ret = PAGE_SIZE; finalize_partial_request(p, orig); // TODO I may need to move page dereference further down. // dirty page now doesn't have a reference. p->dec_ref(); } } else { // If there is an IO pending, it means a read request // has been issuded. It can't be a write request, otherwise, // the data in the page will be ready. assert(orig->get_access_method() == WRITE); p->add_req(orig); p->unlock(); } } else { // The data in the page is ready. We can write data to the page directly. // // If data is ready, there shouldn't be an IO pending. // In other words, if the thread for writing dirty pages is writing // a page, the page will be referenced and therefore, can't be returned // from the cache. // TODO we should delay the write if the page is being written back. // assert(!p->is_io_pending()); p->unlock(); thread_safe_page *dirty = __complete_req(orig, p); if (dirty) dirty_pages.push_back(dirty); ret = orig->get_size(); finalize_partial_request(p, orig); // TODO I may need to move page dereference further down. // dirty page now doesn't have a reference. p->dec_ref(); } return ret; }
int main(int argc, char* argv[]) { char* hostname = NULL; char* certfile = NULL; char* reqfile = NULL; int check_exp = 0; int match_name = 0; int usage = 1; int selector = 0; int match_type = 1; int c = 0; int par_error = 0; char* proto[2] = { "tcp", "udp" }; int proto_sel = -1; int port = 0; int rv = 0; int use_type52 = 0; int be_quiet = 0; cert_ctx crt = { 0 }; req_ctx req = { 0 }; while ((c = getopt(argc, argv, "n:c:eMr:u:s:m:p:P:tqhv")) != -1) { switch(c) { case 'h': show_usage(); return 0; case 'v': show_version(); return 0; case 'n': hostname = strdup(optarg); break; case 'c': certfile = strdup(optarg); break; case 'r': reqfile = strdup(optarg); break; case 'e': check_exp = 1; break; case 'M': match_name = 1; break; case 'u': usage = atoi(optarg); if ((usage < 0) || (usage > 3)) { fprintf(stderr, "Invalid certificate usage (%d) specified\n", usage); par_error = 1; } break; case 's': selector = atoi(optarg); if ((selector < 0) || (selector > 1)) { fprintf(stderr, "Invalid selector (%d) specified\n", selector); par_error = 1; } break; case 'm': match_type = atoi(optarg); if ((match_type < 1) || (match_type > 2)) { fprintf(stderr, "Invalid match type (%d) specified\n", match_type); par_error = 1; } break; case 'p': port = atoi(optarg); break; case 'P': if (!strcasecmp(optarg, "tcp")) { proto_sel = 0; } else if (!strcasecmp(optarg, "udp")) { proto_sel = 1; } else { fprintf(stderr, "Invalid protocol %s specified!\n", optarg); par_error = 1; } break; case 't': use_type52 = 1; break; case 'q': be_quiet = 1; break; default: break; } } /* Check parameters */ if (!hostname) { fprintf(stderr, "Mandatory parameter -n <hostname> missing!\n\n"); par_error = 1; } if (!certfile) { fprintf(stderr, "Mandatory parameter -c <certfile> missing!\n\n"); par_error = 1; } if ((usage < 0) || (usage > 3)) { fprintf(stderr, "Unknown DANE certificate usage %d!\n\n", usage); par_error = 1; } if ((selector < 0) || (selector > 1)) { fprintf(stderr, "Unknown DANE certificate selector %d!\n\n", selector); par_error = 1; } if ((match_type < 1) || (match_type > 2)) { fprintf(stderr, "Unknown or unsupported DANE matching type %d!\n\n", match_type); par_error = 1; } if ((port < 1) || (port > 65535)) { fprintf(stderr, "Invalid (%d) or no port specified!\n\n", port); par_error = 1; } if (proto_sel == -1) { fprintf(stderr, "No protocol specified!\n\n"); par_error = 1; } if (par_error) { show_usage(); free(hostname); free(certfile); free(reqfile); return -1; } /* Process the request */ /* Load the certificate */ init_cert_ctx(&crt); if (read_cert(&crt, certfile) != 0) { fprintf(stderr, "Failed to read X.509 certificate from %s\n", certfile); return -1; } /* Load the certificate request if necessary */ init_req_ctx(&req); if (reqfile) { if (read_req(&req, reqfile) != 0) { fprintf(stderr, "Failed to read CSR from %s\n", reqfile); return -1; } } /* Output the TLSA record */ if (use_type52) { printf("_%d._%s.%s.\tIN\tTYPE52\t\\# %d %02X%02X%02X%s\n", port, proto[proto_sel], hostname, (match_type == 1) ? 35 : 67, usage, selector, match_type, (match_type == 1) ? cert_get_sha256_hash(&crt, selector) : cert_get_sha512_hash(&crt, selector)); } else { printf("_%d._%s.%s.\tIN\tTLSA\t%d %d %d %s\n", port, proto[proto_sel], hostname, usage, selector, match_type, (match_type == 1) ? cert_get_sha256_hash(&crt, selector) : cert_get_sha512_hash(&crt, selector)); } /* Perform expiration check if requested */ if (check_exp) { rv |= cert_is_valid(&crt, be_quiet); } /* Perform name match check if requested */ if (match_name) { rv |= cert_matches_name(&crt, hostname, be_quiet); } if (reqfile != NULL) { /* Perform CSR match if requested */ rv |= cert_matches_req(&crt, &req, be_quiet); } free_cert_ctx(&crt); free_req_ctx(&req); free(hostname); free(certfile); free(reqfile); return rv; }