bool LocalInputFile::Open(std::string uri) { CHECK(!is_open_) << "File already open."; if (!IsValidUri(uri, "local")) { LOG(ERROR) << "failed to validate uri: " << uri; return false; } std::string scheme, path; ParseUri(uri, &scheme, &path); fd_ = open(path.c_str(), O_RDONLY); if (fd_ < 0) { LOG(ERROR) << "Failed to open file: " << path; LOG(ERROR) << strerror(errno); return false; } // Check file size struct stat st; CHECK_EQ(stat(path.c_str(), &st), 0) << "Cannot determine size of " << path << " " << strerror(errno); size_ = st.st_size; modification_time_ = fs::last_write_time(path); is_open_ = true; return true; }
bool LocalOutputFile::Open(std::string uri, short replication, uint64 chunk_size, uint64 desired_mod_time){ CHECK_EQ(replication, 0); CHECK_EQ(chunk_size, 0); CHECK_EQ(desired_mod_time, 0) << "setting mod time for local file is not yet implemented"; CHECK(!is_open_); if (!IsValidUri(uri, "local")) { return false; } std::string scheme, path; CHECK(ParseUri(uri, &scheme, &path)) << "Invalid uri format: " << uri; path_ = path; uri_ = uri; desired_mod_time_ = desired_mod_time; // create parent directories if they don't yet exist string parent_path = fs::path(path).remove_filename().string(); if (!fs::exists(parent_path)){ CHECK(fs::create_directories(parent_path)); } fd_ = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd_ < 0) { LOG(ERROR) << "Failed to open file: " << path; LOG(ERROR) << strerror(errno); return false; } is_open_ = true; uri_ = uri; return true; }
bool LocalOutputCodedBlockFile::Open(std::string uri, short replication, uint64 chunk_size) { CHECK(!is_open_); CHECK_EQ(replication, 0); CHECK_EQ(chunk_size, 0); if (!IsValidUri(uri, "local")) { return false; } std::string scheme, path; ParseUri(uri, &scheme, &path); // Ensure parent path exists. fs::path parent_path = fs::path(path).parent_path(); if (!fs::is_directory(parent_path)) { fs::create_directories(parent_path); } fd_ = open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd_ < 0) { LOG(ERROR) << "LocalOutputStream::Open - failed to open path: " << path; LOG(ERROR) << strerror(errno); return false; } file_stream_.reset(new google::protobuf::io::FileOutputStream(fd_)); is_open_ = true; uri_ = uri; return true; }
bool MaprInputCodedBlockFile::Open(std::string uri) { CHECK(!is_open_) << "File already open."; if (!IsValidUri(uri, "maprfs")) { LOG(ERROR) << "failed to validate uri: " << uri; return false; } std::string scheme, path; CHECK(ParseUri(uri, &scheme, &path)) << "Invalid uri format: " << uri; file_ = hdfsOpenFile(fs_, path.c_str(), O_RDONLY, 0, 0, 0); if (file_ == NULL) { LOG(ERROR) << "Failed to open file: " << path; return false; } is_open_ = true; path_ = path; // Cache file size hdfsFileInfo* info = hdfsGetPathInfo(fs_, path_.c_str()); CHECK(info); size_ = info->mSize; hdfsFreeFileInfo(info, 1); return true; }
bool MaprFileSystem::ListDirectory(const std::string& uri, std::vector<std::string>* contents){ CHECK(contents); contents->clear(); std::string path = GetUriPathOrDie(uri); std::string host = "default"; hdfsFS fs = hdfsConnect(host.c_str(), 0); // use default config file settings int num_entries; hdfsFileInfo* entries = hdfsListDirectory(fs, path.c_str(), &num_entries); hdfsFileInfo* cur_entry = entries; for (int i=0; i < num_entries; ++i) { // Sometimes the list directory command returns paths with the scheme and sometimes it doesn't // Strange. // Anyway, we need to consistently output uris with a proper scheme prefix. std::string cur_scheme, cur_path, error; if (ParseUri(cur_entry->mName, &cur_scheme, &cur_path, &error)){ CHECK_EQ(cur_scheme, "maprfs"); // if it has a scheme prefix, make sure it is maprfs as expected } else{ // this doesn't have a uri scheme prefix, so assume it is just the path portion cur_path = cur_entry->mName; } contents->push_back(Uri("maprfs", cur_path)); cur_entry++; } hdfsFreeFileInfo(entries, num_entries); CHECK_EQ(hdfsDisconnect(fs), 0); return true; }
// Called by the UPnP Remote I/O Microstack // Implements the SetPeerOverride call, lets a CP connect this RIO client to // a new URI. If this RIO client is currently connected, it will disconnect and // switch to the new URI. void UpnpRemoteIOClient_RemoteIO_SetPeerOverride(void* upnptoken,char* PeerConnection) { struct parser_result* ParsedAddress = NULL; char* RemoteIOSessionPath = NULL; char* RemoteIOSessionAddress = NULL; int address = 0; if (upnptoken && (PeerConnection == NULL || (int)strlen(PeerConnection) < 7)) { UpnpResponse_Error(upnptoken,700,"Invalid PeerConnection"); return; } sem_wait(&RemoteIOLock); if (RIO->PeerConnection == NULL || strcmp(RIO->PeerConnection,PeerConnection) != 0) { if (RIO->PeerConnection != NULL) { // Disconnect the socket ILibAsyncSocket_Disconnect(RIO->Session); free(RIO->PeerConnection); } // Set the new session URI RIO->PeerConnection = (char*)malloc((int)strlen(PeerConnection) + 1); strcpy(RIO->PeerConnection,PeerConnection); // Event the new connection UpnpSetState_RemoteIOClient_RemoteIO_PeerConnection(RIO->RIOmicroStack,RIO->PeerConnection); // Connect session ParseUri(RIO->PeerConnection,&RemoteIOSessionAddress,&RIO->SessionPort,&RemoteIOSessionPath); free(RemoteIOSessionPath); ParsedAddress = ILibParseString(RemoteIOSessionAddress,0,(int)strlen(RemoteIOSessionAddress),".",1); address = atoi(ParsedAddress->FirstResult->data); address += atoi(ParsedAddress->FirstResult->NextResult->data) << 8; address += atoi(ParsedAddress->FirstResult->NextResult->NextResult->data) << 16; address += atoi(ParsedAddress->FirstResult->NextResult->NextResult->NextResult->data) << 24; ILibAsyncSocket_ConnectTo(RIO->Session,0,address,RIO->SessionPort,NULL,NULL); ILibDestructParserResults(ParsedAddress); free(RemoteIOSessionAddress); sem_post(&RemoteIOLock); // Event the user if (RemoteIOConnectionChanged != NULL) RemoteIOConnectionChanged(RIO->PeerConnection); } else { sem_post(&RemoteIOLock); } if(upnptoken) {UpnpResponse_RemoteIOClient_RemoteIO_SetPeerOverride(upnptoken);} }
void VideoInput::Open( const std::string& input_uri, const std::string& output_uri ) { str_uri_input = input_uri; uri_input = ParseUri(input_uri); uri_output = ParseUri(output_uri); if (uri_output.scheme == "file") { // Default to pango output uri_output.scheme = "pango"; } // Start off playing from video_src video_src = OpenVideo(input_uri); Source(); }
bool LocalFileSystem::GetUriPath(const std::string& uri, std::string* path){ CHECK(path); std::string scheme; if (!ParseUri(uri, &scheme, path)){ return false; } if (scheme != "local"){ return false; } return true; }
// Called by the UPnP Remote I/O Microstack // Implements the ForceDisconnect call, lets a CP connect this RIO client // to a URI if, and only if, this RIO client is not currently connected. void UpnpRemoteIO_SetPeerInterlock(void* upnptoken,char* PeerConnection) { struct parser_result* ParsedAddress = NULL; char* RemoteIOSessionPath = NULL; char* RemoteIOSessionAddress = NULL; int address = 0; if (PeerConnection == NULL || (int)strlen(PeerConnection) < 7) { UpnpResponse_Error(upnptoken,700,"Invalid PeerConnection"); return; } sem_wait(&RemoteIOLock); if (RIO->PeerConnection == NULL) { RIO->PeerConnection = (char*)RIO_MALLOC((int)strlen(PeerConnection) + 1); strcpy(RIO->PeerConnection,PeerConnection); // Event the new connection UpnpSetState_RemoteIO_PeerConnection(RIO->RIOmicroStack,RIO->PeerConnection); // Connect session ParseUri(RIO->PeerConnection,&RemoteIOSessionAddress,&RIO->SessionPort,&RemoteIOSessionPath); RIO_FREE(RemoteIOSessionPath); ParsedAddress = ILibParseString(RemoteIOSessionAddress,0,(int)strlen(RemoteIOSessionAddress),".",1); address = atoi(ParsedAddress->FirstResult->data); address += atoi(ParsedAddress->FirstResult->NextResult->data) << 8; address += atoi(ParsedAddress->FirstResult->NextResult->NextResult->data) << 16; address += atoi(ParsedAddress->FirstResult->NextResult->NextResult->NextResult->data) << 24; ILibConnectTo(RIO->Session,0,address,RIO->SessionPort); ILibDestructParserResults(ParsedAddress); RIO_FREE(RemoteIOSessionAddress); UpnpResponse_RemoteIO_SetPeerInterlock(upnptoken,RIO->PeerConnection); sem_post(&RemoteIOLock); // Event the user if (RemoteIOConnectionChanged != NULL) RemoteIOConnectionChanged(RIO->PeerConnection); } else { UpnpResponse_RemoteIO_SetPeerInterlock(upnptoken,RIO->PeerConnection); sem_post(&RemoteIOLock); } }
bool MaprOutputCodedBlockFile::Open(std::string uri, short replication, uint64 chunk_size) { CHECK_GE(replication, 0); CHECK_LE(replication, 6); CHECK_GE(chunk_size, 0); CHECK(!is_open_); if (!IsValidUri(uri, "maprfs")) { return false; } std::string scheme, path; CHECK(ParseUri(uri, &scheme, &path)) << "Invalid uri format: " << uri; path_ = path; // note a chunk_size of zero to hdfs means use hdfs's default... however, we want to use maprfs's default... which should be based on the settings of the parent directory... if it exists string parent_path = fs::path(path).remove_filename().string();; if (chunk_size == 0){ string parent_uri = Uri(scheme, parent_path); while (!Exists(parent_uri)){ parent_path = fs::path(parent_path).remove_filename().string(); parent_uri = Uri(scheme, parent_path); LOG(INFO) << "parent_uri: " << parent_uri; } CHECK(ChunkSize(parent_uri, &chunk_size)); } CHECK_EQ(chunk_size % (1 << 16), 0) << "MaprFS requires chunk size is a multiple of 2^16"; CHECK_LE(chunk_size, 1024 * (1<<20)) << "hdfs.h uses a signed 32 int which artificially limits the chunk size to 1GB... maprfs can do more, but not through the c api... ;-("; file_ = hdfsOpenFile(fs_, path.c_str(), O_WRONLY, 0, replication, chunk_size); if (file_ == NULL){ LOG(ERROR) << "Failed to open file: " << path; return false; } copying_output_stream_.reset(new MaprCopyingOutputStream(this)); output_stream_.reset(new google::protobuf::io::CopyingOutputStreamAdaptor(copying_output_stream_.get())); is_open_ = true; uri_ = uri; return true; }
void VideoOutput::Open(const std::string& str_uri) { Close(); uri = ParseUri(str_uri); recorder = OpenVideoOutput(uri); }
VideoOutputInterface* OpenVideoOutput(std::string str_uri) { Uri uri = ParseUri(str_uri); return OpenVideoOutput(uri); }
/* ** The input azSeries[] is a sequence of URIs. This command must ** resolve them all and put the result in the interp->result field ** of the interpreter associated with the HTML widget. Return ** TCL_OK on success and TCL_ERROR if there is a failure. ** ** This function can cause the HTML widget to be deleted or changed ** arbitrarily. */ int HtmlCallResolver( HtmlWidget *htmlPtr, /* The widget that is doing the resolving. */ char **azSeries /* A list of URIs. NULL terminated */ ){ int rc = TCL_OK; /* Return value of this function. */ char *z; HtmlVerifyLock(htmlPtr); if( htmlPtr->zResolverCommand && htmlPtr->zResolverCommand[0] ){ /* ** Append the current base URI then the azSeries arguments to the ** TCL command specified by the -resolvercommand optoin, then execute ** the result. ** ** The -resolvercommand could do nasty things, such as delete ** the HTML widget out from under us. Be prepared for the worst. */ Tcl_DString cmd; Tcl_DStringInit(&cmd); Tcl_DStringAppend(&cmd, htmlPtr->zResolverCommand, -1); if( htmlPtr->zBaseHref && htmlPtr->zBaseHref[0] ){ z = Trim(htmlPtr->zBaseHref); }else if( htmlPtr->zBase && htmlPtr->zBase[0] ){ z = Trim(htmlPtr->zBase); } if( z ){ Tcl_DStringAppendElement(&cmd, z); HtmlFree(z); } while( azSeries[0] ){ z = Trim(azSeries[0]); if( z ){ Tcl_DStringAppendElement(&cmd, z); HtmlFree(z); } azSeries++; } HtmlLock(htmlPtr); rc = Tcl_GlobalEval(htmlPtr->interp, Tcl_DStringValue(&cmd)); Tcl_DStringFree(&cmd); if( HtmlUnlock(htmlPtr) ) return TCL_ERROR; if( rc!=TCL_OK ){ Tcl_AddErrorInfo(htmlPtr->interp, "\n (-resolvercommand executed by HTML widget)"); } }else{ /* ** No -resolvercommand has been specified. Do the default ** resolver algorithm specified in section 5.2 of RFC 2396. */ HtmlUri *base, *term; if( htmlPtr->zBaseHref && htmlPtr->zBaseHref[0] ){ base = ParseUri(htmlPtr->zBaseHref); }else{ base = ParseUri(htmlPtr->zBase); } while( azSeries[0] ){ term = ParseUri(azSeries[0]); azSeries++; if( term->zScheme==0 && term->zAuthority==0 && term->zPath==0 && term->zQuery==0 && term->zFragment ){ ReplaceStr(&base->zFragment, term->zFragment); }else if( term->zScheme ){ HtmlUri temp; temp = *term; *term = *base; *base = temp; }else if( term->zAuthority ){ ReplaceStr(&base->zAuthority, term->zAuthority); ReplaceStr(&base->zPath, term->zPath); ReplaceStr(&base->zQuery, term->zQuery); ReplaceStr(&base->zFragment, term->zFragment); }else if( term->zPath && (term->zPath[0]=='/' || base->zPath==0) ){ ReplaceStr(&base->zPath, term->zPath); ReplaceStr(&base->zQuery, term->zQuery); ReplaceStr(&base->zFragment, term->zFragment); }else if( term->zPath && base->zPath ){ char *zBuf; int i, j; zBuf = HtmlAlloc( strlen(base->zPath) + strlen(term->zPath) + 2 ); if( zBuf ){ sprintf(zBuf,"%s", base->zPath); for(i=strlen(zBuf)-1; i>=0 && zBuf[i]!='/'; i--){ zBuf[i] = 0; } strcat(zBuf, term->zPath); for(i=0; zBuf[i]; i++){ if( zBuf[i]=='/' && zBuf[i+1]=='.' && zBuf[i+2]=='/' ){ strcpy(&zBuf[i+1], &zBuf[i+3]); i--; continue; } if( zBuf[i]=='/' && zBuf[i+1]=='.' && zBuf[i+2]==0 ){ zBuf[i+1] = 0; continue; } if( i>0 && zBuf[i]=='/' && zBuf[i+1]=='.' && zBuf[i+2]=='.' && (zBuf[i+3]=='/' || zBuf[i+3]==0) ){ for(j=i-1; j>=0 && zBuf[j]!='/'; j--){} if( zBuf[i+3] ){ strcpy(&zBuf[j+1], &zBuf[i+4]); }else{ zBuf[j+1] = 0; } i = j-1; if( i<-1 ) i = -1; continue; } } HtmlFree(base->zPath); base->zPath = zBuf; } ReplaceStr(&base->zQuery, term->zQuery); ReplaceStr(&base->zFragment, term->zFragment); } FreeUri(term); } Tcl_SetResult(htmlPtr->interp, BuildUri(base), TCL_DYNAMIC); FreeUri(base); } return rc; }