StatusWith<std::vector<PerfCounterCollector::CounterInfo>> PerfCounterCollector::addCounters( StringData path) { std::wstring pathWide = toNativeString(path.toString().c_str()); DWORD pathListLength = 0; PDH_STATUS status = PdhExpandCounterPathW(pathWide.c_str(), nullptr, &pathListLength); if (status != PDH_MORE_DATA) { return {ErrorCodes::WindowsPdhError, str::stream() << formatFunctionCallError("PdhExpandCounterPathW", status) << " for counter '" << path << "'"}; } auto buf = stdx::make_unique<wchar_t[]>(pathListLength); status = PdhExpandCounterPathW(pathWide.c_str(), buf.get(), &pathListLength); if (status != ERROR_SUCCESS) { return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhExpandCounterPathW", status)}; } std::vector<CounterInfo> counters; // Windows' PdhExpandWildCardPathW returns a nullptr terminated array of nullptr separated // strings. std::vector<std::string> counterNames; const wchar_t* ptr = buf.get(); while (ptr && *ptr) { counterNames.emplace_back(toUtf8String(ptr)); ptr += wcslen(ptr) + 1; } // Sort to ensure we have a predictable ordering in the final BSON std::sort(counterNames.begin(), counterNames.end()); for (const auto& name : counterNames) { auto swCounterInfo = addCounter(name); if (!swCounterInfo.isOK()) { return swCounterInfo.getStatus(); } counters.emplace_back(std::move(swCounterInfo.getValue())); } return {std::move(counters)}; }
void* MemoryMappedFile::map(const char *filenameIn, long &length, int options) { _filename = filenameIn; /* big hack here: Babble uses db names with colons. doesn't seem to work on windows. temporary perhaps. */ char filename[256]; strncpy(filename, filenameIn, 255); filename[255] = 0; { size_t len = strlen( filename ); for ( size_t i=len-1; i>=0; i-- ){ if ( filename[i] == '/' || filename[i] == '\\' ) break; if ( filename[i] == ':' ) filename[i] = '_'; } } updateLength( filename, length ); DWORD createOptions = FILE_ATTRIBUTE_NORMAL; if ( options & SEQUENTIAL ) createOptions |= FILE_FLAG_SEQUENTIAL_SCAN; fd = CreateFile( toNativeString(filename).c_str(), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, createOptions , NULL); if ( fd == INVALID_HANDLE_VALUE ) { out() << "Create/OpenFile failed " << filename << ' ' << GetLastError() << endl; return 0; } mapped += length; maphandle = CreateFileMapping(fd, NULL, PAGE_READWRITE, 0, length, NULL); if ( maphandle == NULL ) { out() << "CreateFileMapping failed " << filename << ' ' << GetLastError() << endl; return 0; } view = MapViewOfFile(maphandle, FILE_MAP_ALL_ACCESS, 0, 0, 0); if ( view == 0 ) { out() << "MapViewOfFile failed " << filename << " " << errnoWithDescription() << " "; out() << GetLastError(); out() << endl; } len = length; return view; }
LogFile::LogFile(const std::string& name, bool readwrite) : _name(name) { _fd = CreateFile( toNativeString(name.c_str()).c_str(), (readwrite?GENERIC_READ:0)|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_FLAG_NO_BUFFERING, NULL); if( _fd == INVALID_HANDLE_VALUE ) { DWORD e = GetLastError(); uasserted(13518, str::stream() << "couldn't open file " << name << " for writing " << errnoWithDescription(e)); } SetFilePointer(_fd, 0, 0, FILE_BEGIN); }
void File::open(const char* filename, bool readOnly, bool direct) { _name = filename; _handle = CreateFileW(toNativeString(filename).c_str(), // filename (readOnly ? 0 : GENERIC_WRITE) | GENERIC_READ, // desired access FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode NULL, // security OPEN_ALWAYS, // create or open FILE_ATTRIBUTE_NORMAL, // file attributes NULL); // template _bad = !is_open(); if (_bad) { DWORD dosError = GetLastError(); log() << "In File::open(), CreateFileW for '" << _name << "' failed with " << errnoWithDescription(dosError) << std::endl; } }
Status StorageEngineLockFile::open() { try { if (!boost::filesystem::exists(_dbpath)) { return Status(ErrorCodes::NonExistentPath, str::stream() << "Data directory " << _dbpath << " not found."); } } catch (const std::exception& ex) { return Status(ErrorCodes::UnknownError, str::stream() << "Unable to check existence of data directory " << _dbpath << ": " << ex.what()); } HANDLE lockFileHandle = CreateFileW(toNativeString(_filespec.c_str()).c_str(), GENERIC_READ | GENERIC_WRITE, 0 /* do not allow anyone else access */, NULL, OPEN_ALWAYS /* success if fh can open */, 0, NULL); if (lockFileHandle == INVALID_HANDLE_VALUE) { int errorcode = GetLastError(); if (errorcode == ERROR_ACCESS_DENIED) { return Status(ErrorCodes::IllegalOperation, str::stream() << "Attempted to create a lock file on a read-only directory: " << _dbpath); } return Status(ErrorCodes::DBPathInUse, str::stream() << "Unable to create/open the lock file: " << _filespec << " (" << errnoWithDescription(errorcode) << ")." << " Ensure the user executing mongod is the owner of the lock " "file and has the appropriate permissions. Also make sure " "that another mongod instance is not already running on the " << _dbpath << " directory"); } _lockFileHandle->_handle = lockFileHandle; return Status::OK(); }
void* MemoryMappedFile::map(const char *filenameIn, unsigned long long &length, int options) { setFilename(filenameIn); /* big hack here: Babble uses db names with colons. doesn't seem to work on windows. temporary perhaps. */ char filename[256]; strncpy(filename, filenameIn, 255); filename[255] = 0; { size_t len = strlen( filename ); for ( size_t i=len-1; i>=0; i-- ) { if ( filename[i] == '/' || filename[i] == '\\' ) break; if ( filename[i] == ':' ) filename[i] = '_'; } } updateLength( filename, length ); { DWORD createOptions = FILE_ATTRIBUTE_NORMAL; if ( options & SEQUENTIAL ) createOptions |= FILE_FLAG_SEQUENTIAL_SCAN; DWORD rw = GENERIC_READ | GENERIC_WRITE; fd = CreateFile( toNativeString(filename).c_str(), rw, // desired access FILE_SHARE_WRITE | FILE_SHARE_READ, // share mode NULL, // security OPEN_ALWAYS, // create disposition createOptions , // flags NULL); // hTempl if ( fd == INVALID_HANDLE_VALUE ) { DWORD e = GetLastError(); log() << "Create/OpenFile failed " << filename << " errno:" << e << endl; return 0; } } mapped += length; { DWORD flProtect = PAGE_READWRITE; //(options & READONLY)?PAGE_READONLY:PAGE_READWRITE; maphandle = CreateFileMapping(fd, NULL, flProtect, length >> 32 /*maxsizehigh*/, (unsigned) length /*maxsizelow*/, NULL/*lpName*/); if ( maphandle == NULL ) { DWORD e = GetLastError(); // log() call was killing lasterror before we get to that point in the stream log() << "CreateFileMapping failed " << filename << ' ' << errnoWithDescription(e) << endl; return 0; } } void *view = 0; { scoped_lock lk(mapViewMutex); DWORD access = (options&READONLY)? FILE_MAP_READ : FILE_MAP_ALL_ACCESS; view = MapViewOfFile(maphandle, access, /*f ofs hi*/0, /*f ofs lo*/ 0, /*dwNumberOfBytesToMap 0 means to eof*/0); } if ( view == 0 ) { DWORD e = GetLastError(); log() << "MapViewOfFile failed " << filename << " " << errnoWithDescription(e) << endl; } else { views.push_back(view); } len = length; return view; }
StatusWith<PerfCounterCollector::CounterInfo> PerfCounterCollector::addCounter(StringData path) { PDH_HCOUNTER counter{0}; PDH_STATUS status = PdhAddCounterW(_query, toNativeString(path.toString().c_str()).c_str(), NULL, &counter); if (status != ERROR_SUCCESS) { return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhAddCounterW", status)}; } DWORD bufferSize = 0; status = PdhGetCounterInfoW(counter, false, &bufferSize, nullptr); if (status != PDH_MORE_DATA) { return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhGetCounterInfoW", status)}; } auto buf = stdx::make_unique<char[]>(bufferSize); auto counterInfo = reinterpret_cast<PPDH_COUNTER_INFO>(buf.get()); status = PdhGetCounterInfoW(counter, false, &bufferSize, counterInfo); if (status != ERROR_SUCCESS) { return {ErrorCodes::WindowsPdhError, formatFunctionCallError("PdhGetCounterInfoW", status)}; } // A full qualified path is as such: // "\\MYMACHINE\\Processor(0)\\% Idle Time" // MachineName \\ Object Name (Instance Name) \\ CounterName // Ex: // MachineName: MYMACHINE // Object Name: Processor // InstanceName: 0 // CounterName: % Idle Time // We do not want to use Machine Name, but sometimes we want InstanceName // std::string firstName = str::stream() << '\\' << toUtf8String(counterInfo->szObjectName) << '\\' << toUtf8String(counterInfo->szCounterName); // Compute a second name std::string secondName(firstName); bool hasSecondValue = false; // Only include base for counters that need it if ((counterInfo->dwType & PERF_COUNTER_PRECISION) == PERF_COUNTER_PRECISION) { secondName += " Base"; hasSecondValue = true; } // InstanceName is null for counters without instance names return {CounterInfo{std::move(firstName), std::move(secondName), hasSecondValue, counterInfo->szInstanceName ? toUtf8String(counterInfo->szInstanceName) : std::string(), counterInfo->dwType, counter}}; }
void DXF2BRD_CONVERTER::addMText( const DRW_MText& aData ) { wxString text = toNativeString( wxString::FromUTF8( aData.text.c_str() ) ); wxString attrib, tmp; /* Some texts start by '\' and have formating chars (font name, font option...) * ending with ';' * Here are some mtext formatting codes: * Format code Purpose * \0...\o Turns overline on and off * \L...\l Turns underline on and off * \~ Inserts a nonbreaking space \\ Inserts a backslash \\\{...\} Inserts an opening and closing brace \\ \File name; Changes to the specified font file \\ \Hvalue; Changes to the text height specified in drawing units \\ \Hvaluex; Changes the text height to a multiple of the current text height \\ \S...^...; Stacks the subsequent text at the \, #, or ^ symbol \\ \Tvalue; Adjusts the space between characters, from.75 to 4 times \\ \Qangle; Changes obliquing angle \\ \Wvalue; Changes width factor to produce wide text \\ \A Sets the alignment value; valid values: 0, 1, 2 (bottom, center, top) while( text.StartsWith( wxT("\\") ) ) */ while( text.StartsWith( wxT( "\\" ) ) ) { attrib << text.BeforeFirst( ';' ); tmp = text.AfterFirst( ';' ); text = tmp; } BOARD_ITEM* brdItem; EDA_TEXT* textItem; if( m_useModuleItems ) { TEXTE_MODULE* modText = new TEXTE_MODULE( NULL ); brdItem = static_cast< BOARD_ITEM* >( modText ); textItem = static_cast< EDA_TEXT* >( modText ); } else { TEXTE_PCB* pcbText = new TEXTE_PCB( NULL ); brdItem = static_cast< BOARD_ITEM* >( pcbText ); textItem = static_cast< EDA_TEXT* >( pcbText ); } brdItem->SetLayer( ToLAYER_ID( m_brdLayer ) ); wxPoint textpos( mapX( aData.basePoint.x ), mapY( aData.basePoint.y ) ); textItem->SetTextPos( textpos ); textItem->SetTextAngle( aData.angle * 10 ); // The 0.8 factor gives a better height/width ratio with our font textItem->SetTextWidth( mapDim( aData.height * 0.8 ) ); textItem->SetTextHeight( mapDim( aData.height ) ); textItem->SetThickness( mapDim( aData.thickness == 0 ? m_defaultThickness : aData.thickness ) ); textItem->SetText( text ); // Initialize text justifications: if( aData.textgen <= 3 ) { textItem->SetVertJustify( GR_TEXT_VJUSTIFY_TOP ); } else if( aData.textgen <= 6 ) { textItem->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER ); } else { textItem->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM ); } if( aData.textgen % 3 == 1 ) { textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT ); } else if( aData.textgen % 3 == 2 ) { textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER ); } else { textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT ); } #if 0 // These setting have no mening in Pcbnew if( data.alignH == 1 ) { // Text is left to right; } else if( data.alignH == 3 ) { // Text is top to bottom; } else { // use ByStyle; } if( aData.alignV == 1 ) { // use AtLeast; } else { // useExact; } #endif m_newItemsList.push_back( static_cast< BOARD_ITEM* >( brdItem ) ); }
void DXF2BRD_CONVERTER::addText( const DRW_Text& aData ) { BOARD_ITEM* brdItem; EDA_TEXT* textItem; if( m_useModuleItems ) { TEXTE_MODULE* modText = new TEXTE_MODULE( NULL ); brdItem = static_cast< BOARD_ITEM* >( modText ); textItem = static_cast< EDA_TEXT* >( modText ); } else { TEXTE_PCB* pcbText = new TEXTE_PCB( NULL ); brdItem = static_cast< BOARD_ITEM* >( pcbText ); textItem = static_cast< EDA_TEXT* >( pcbText ); } brdItem->SetLayer( ToLAYER_ID( m_brdLayer ) ); wxPoint refPoint( mapX( aData.basePoint.x ), mapY( aData.basePoint.y ) ); wxPoint secPoint( mapX( aData.secPoint.x ), mapY( aData.secPoint.y ) ); if( aData.alignV != 0 || aData.alignH != 0 || aData.alignH == DRW_Text::HMiddle ) { if( aData.alignH != DRW_Text::HAligned && aData.alignH != DRW_Text::HFit ) { wxPoint tmp = secPoint; secPoint = refPoint; refPoint = tmp; } } switch( aData.alignV ) { case DRW_Text::VBaseLine: textItem->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM ); break; case DRW_Text::VBottom: textItem->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM ); break; case DRW_Text::VMiddle: textItem->SetVertJustify( GR_TEXT_VJUSTIFY_CENTER ); break; case DRW_Text::VTop: textItem->SetVertJustify( GR_TEXT_VJUSTIFY_TOP ); break; } switch( aData.alignH ) { case DRW_Text::HLeft: textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT ); break; case DRW_Text::HCenter: textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER ); break; case DRW_Text::HRight: textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT ); break; case DRW_Text::HAligned: // no equivalent options in text pcb. textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT ); break; case DRW_Text::HMiddle: // no equivalent options in text pcb. textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_CENTER ); break; case DRW_Text::HFit: // no equivalent options in text pcb. textItem->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT ); break; } #if 0 wxString sty = wxString::FromUTF8( aData.style.c_str() ); sty = sty.ToLower(); if( aData.textgen == 2 ) { // Text dir = left to right; } else if( aData.textgen == 4 ) { // Text dir = top to bottom; } else { } #endif wxString text = toNativeString( wxString::FromUTF8( aData.text.c_str() ) ); textItem->SetTextPos( refPoint ); textItem->SetTextAngle( aData.angle * 10 ); // The 0.8 factor gives a better height/width ratio with our font textItem->SetTextWidth( mapDim( aData.height * 0.8 ) ); textItem->SetTextHeight( mapDim( aData.height ) ); textItem->SetThickness( mapDim( aData.thickness == 0 ? m_defaultThickness : aData.thickness ) ); textItem->SetText( text ); m_newItemsList.push_back( static_cast< BOARD_ITEM* >( brdItem ) ); }
#if 0 wxString sty = wxString::FromUTF8(data.style.c_str()); sty=sty.ToLower(); if (data.textgen==2) { // Text dir = left to right; } else if (data.textgen==4) { / Text dir = top to bottom; } else { } #endif wxString text = toNativeString( wxString::FromUTF8( data.text.c_str() ) ); pcb_text->SetTextPosition( refPoint ); pcb_text->SetOrientation( data.angle * 10 ); // The 0.8 factor gives a better height/width ratio with our font pcb_text->SetWidth( mapDim( data.height * 0.8 ) ); pcb_text->SetHeight( mapDim( data.height ) ); pcb_text->SetThickness( mapDim( data.thickness == 0 ? m_defaultThickness : data.thickness ) ); pcb_text->SetText( text ); appendToBoard( pcb_text ); } /**
void ProgramRunner::launchProcess(int child_stdout) { std::vector<std::string> envStrings; for (const auto& envKeyValue : _envp) { envStrings.emplace_back(envKeyValue.first + '=' + envKeyValue.second); } #ifdef _WIN32 stringstream ss; for (unsigned i = 0; i < _argv.size(); i++) { if (i) ss << ' '; if (_argv[i].find(' ') == string::npos) ss << _argv[i]; else { ss << '"'; // escape all embedded quotes for (size_t j = 0; j < _argv[i].size(); ++j) { if (_argv[i][j] == '"') ss << '\\'; ss << _argv[i][j]; } ss << '"'; } } std::wstring args = toNativeString(ss.str().c_str()); // Construct the environment block which the new process will use. // An environment block is a NULL terminated array of NULL terminated WCHAR strings. The // strings are of the form "name=value\0". Because the strings are variable length, we must // precompute the size of the array before we may allocate it. size_t environmentBlockSize = 0; std::vector<std::wstring> nativeEnvStrings; // Compute the size of the environment block, in characters. Note that we have to count // wchar_t characters, which we'll actually be storing in the block later, rather than UTF8 // characters we have in _envp and need to convert. for (const std::string& envKeyValue : envStrings) { std::wstring nativeKeyValue = toNativeString(envKeyValue.c_str()); environmentBlockSize += (nativeKeyValue.size() + 1); nativeEnvStrings.emplace_back(std::move(nativeKeyValue)); } // Reserve space for the final NULL character which terminates the environment block environmentBlockSize += 1; auto lpEnvironment = stdx::make_unique<wchar_t[]>(environmentBlockSize); size_t environmentOffset = 0; for (const std::wstring& envKeyValue : nativeEnvStrings) { // Ensure there is enough room to write the string, the string's NULL byte, and the block's // NULL byte invariant(environmentOffset + envKeyValue.size() + 1 + 1 <= environmentBlockSize); wcscpy_s( lpEnvironment.get() + environmentOffset, envKeyValue.size() + 1, envKeyValue.c_str()); environmentOffset += envKeyValue.size(); std::memset(lpEnvironment.get() + environmentOffset, 0, sizeof(wchar_t)); environmentOffset += 1; } std::memset(lpEnvironment.get() + environmentOffset, 0, sizeof(wchar_t)); HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(child_stdout)); invariant(h != INVALID_HANDLE_VALUE); invariant(SetHandleInformation(h, HANDLE_FLAG_INHERIT, 1)); STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.hStdError = h; si.hStdOutput = h; si.dwFlags |= STARTF_USESTDHANDLES; PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); DWORD dwCreationFlags = 0; dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT; bool success = CreateProcessW(nullptr, const_cast<LPWSTR>(args.c_str()), nullptr, nullptr, true, dwCreationFlags, lpEnvironment.get(), nullptr, &si, &pi) != 0; if (!success) { const auto ewd = errnoWithDescription(); ss << "couldn't start process " << _argv[0] << "; " << ewd; uasserted(14042, ss.str()); } CloseHandle(pi.hThread); invariant(SetHandleInformation(h, HANDLE_FLAG_INHERIT, 0)); _pid = ProcessId::fromNative(pi.dwProcessId); registry.insertHandleForPid(_pid, pi.hProcess); #else std::string execErrMsg = str::stream() << "Unable to start program " << _argv[0]; auto constCharStorageMaker = [](const std::vector<std::string>& in) { std::vector<const char*> out; std::transform(in.begin(), in.end(), std::back_inserter(out), [](const std::string& x) { return x.c_str(); }); out.push_back(nullptr); return out; }; std::vector<const char*> argvStorage = constCharStorageMaker(_argv); std::vector<const char*> envpStorage = constCharStorageMaker(envStrings); pid_t nativePid = fork(); _pid = ProcessId::fromNative(nativePid); // Async signal unsafe functions should not be called in the child process. if (nativePid == -1) { // Fork failed so it is time for the process to exit const auto ewd = errnoWithDescription(); cout << "ProgramRunner is unable to fork child process: " << ewd << endl; fassertFailed(34363); } if (nativePid == 0) { // DON'T ASSERT IN THIS BLOCK - very bad things will happen // // Also, deliberately call _exit instead of quickExit. We intended to // fork() and exec() here, so we never want to run any form of cleanup. // This includes things that quickExit calls, such as atexit leak // checks. if (dup2(child_stdout, STDOUT_FILENO) == -1 || dup2(child_stdout, STDERR_FILENO) == -1) { // Async signal unsafe code reporting a terminal error condition. perror("Unable to dup2 child output: "); _exit(-1); // do not pass go, do not call atexit handlers } execve(argvStorage[0], const_cast<char**>(argvStorage.data()), const_cast<char**>(envpStorage.data())); // Async signal unsafe code reporting a terminal error condition. perror(execErrMsg.c_str()); _exit(-1); } #endif }