std::string EnvironmentImpl::osNameImpl() { OSVERSIONINFO vi; vi.dwOSVersionInfoSize = sizeof(vi); if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information"); switch (vi.dwPlatformId) { case VER_PLATFORM_WIN32s: return "Windows 3.x"; case VER_PLATFORM_WIN32_WINDOWS: return vi.dwMinorVersion == 0 ? "Windows 95" : "Windows 98"; case VER_PLATFORM_WIN32_NT: return "Windows NT"; default: return "Unknown"; } }
std::string EnvironmentImpl::osDisplayNameImpl() { OSVERSIONINFO vi; vi.dwOSVersionInfoSize = sizeof(vi); if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information"); switch(vi.dwMajorVersion) { case 6: switch (vi.dwMinorVersion) { case 0: return "Windows Vista/Server 2008"; case 1: return "Windows 7/Server 2008 SP2"; default: return "Unknown"; } case 5: switch (vi.dwMinorVersion) { case 0: return "Windows 2000"; case 1: return "Windows XP"; case 2: return "Windows Server 2003/Windows Server 2003 R2"; default: return "Unknown"; } case 4: switch (vi.dwMinorVersion) { case 0: return "Windows 95/Windows NT 4.0"; case 10: return "Windows 98"; case 90: return "Windows ME"; default: return "Unknown"; } default: return "Unknown"; } }
std::string EnvironmentImpl::osDisplayNameImpl() { OSVERSIONINFO vi; vi.dwOSVersionInfoSize = sizeof(vi); if (GetVersionEx(&vi) == 0) throw SystemException("Cannot get OS version information"); switch(vi.dwMajorVersion) { case 6: switch (vi.dwMinorVersion) { case 0: return "Windows_Vista"; case 1: return "Windows_7"; default: return "Windows_Unknown"; } case 5: switch (vi.dwMinorVersion) { case 0: return "Windows_2000"; case 1: return "Windows_XP"; case 2: return "Windows_2003"; default: return "Windows_Unknown"; } case 4: switch (vi.dwMinorVersion) { case 0: return "Windows_95"; case 10: return "Windows_98"; case 90: return "Windows_ME"; default: return "Windows_Unknown"; } default: return "Windows_Unknown"; } }
/** * \brief Function to make a link from a given * \param src : the source file * \return the path of the link * Throw exception on error */ std::string vishnu::mklink(const std::string& src) { if( ! bfs::exists(src) ) { throw UserException(ERRCODE_FILENOTFOUND, "SSHJobExec::mklink : " "the file "+ src + " doesnot exist."); } bfs::path dest ; try { dest = bfs::unique_path("/tmp/vishnulink%%%%%%") ; bfs::create_symlink(src, dest) ; }catch (...) { throw SystemException(ERRCODE_SYSTEM, "SSHJobExec::mklink : " "error while making a link on the file " + src + " from " + dest.string()); } return dest.string() ; }
void RWLockImpl::readLockImpl() { HANDLE h[2]; h[0] = _mutex; h[1] = _readEvent; switch (WaitForMultipleObjects(2, h, TRUE, INFINITE)) { case WAIT_OBJECT_0: case WAIT_OBJECT_0 + 1: ++_readers; ResetEvent(_writeEvent); ReleaseMutex(_mutex); poco_assert_dbg(_writers == 0); break; default: throw SystemException("cannot lock reader/writer lock"); } }
/** * Create a random access stream from a file, removing it if it already exists. * @param path Path of the file to open. * @param access Type of access (one of READ, WRITE, READ_WRITE). * @return Opened file. * @throws IOException Thrown if there is an error. */ io::RandomAccessStream *System::createRandomFile( const sys::Path& path, access_t access) throw(SystemException) { ASSERTP(access != READ, "file creation requires at least a write mode"); int fd = ::open(&path.toString(), makeFlags(access) | O_CREAT | O_TRUNC, 0666); if(fd < 0) throw SystemException(errno, _ << "cannot create \"" << path << "\""); else # if defined(__unix) || defined(__APPLE__) return new UnixRandomAccessStream(fd); # elif defined(__WIN32) || defined(__WIN64) return new WinRandomAccessStream(fd); # else # error "Unsupported on this OS !" # endif }
void SysSemaphore::Construct(const int32_t initialCount, const int32_t maxCount, const wchar_t* pName, void* security) { if (nullptr != pName) { std::wstring fullName = MakeFullname(pName); m_handle = CreateSemaphore(reinterpret_cast<LPSECURITY_ATTRIBUTES>(security), initialCount, maxCount, fullName.c_str()); } else { m_handle = CreateSemaphore(reinterpret_cast<LPSECURITY_ATTRIBUTES>(security), initialCount, maxCount, pName); } if (nullptr == m_handle) { throw SystemException("Couldn't create a new semaphore"); } }
void parseEmfObject(const std::string& objectSerialized, T*& object_ptr, const std::string msgComp=std::string()) { object_ptr = NULL; try { //CREATE DATA MODEL T tmpObject; ecore::EPackage_ptr ecorePackage = tmpObject._eClass()->getEPackage(); ecorecpp::MetaModelRepository::_instance()->load(ecorePackage); //Parse the model ecorecpp::parser::parser parser; object_ptr = parser.load_str(objectSerialized)->as< T >(); } catch (std::exception& e) { throw SystemException(ERRCODE_INVDATA, msgComp); } }
void ThreadImpl::setPriorityImpl(int prio) { if (prio != _pData->prio) { _pData->prio = prio; _pData->policy = SCHED_OTHER; if (isRunningImpl()) { struct sched_param par; struct MyStruct { }; par.sched_priority = mapPrio(_pData->prio, SCHED_OTHER); if (pthread_setschedparam(_pData->thread, SCHED_OTHER, &par)) throw SystemException("cannot set thread priority"); } } }
static string readAll(int fd) { string result; char buf[1024 * 32]; ssize_t ret; while (true) { do { ret = read(fd, buf, sizeof(buf)); } while (ret == -1 && errno == EINTR); if (ret == 0) { break; } else if (ret == -1) { throw SystemException("Cannot read from socket", errno); } else { result.append(buf, ret); } } return result; }
std::string PathImpl::currentImpl() { std::string result; DWORD len = GetCurrentDirectoryW(0, NULL); if (len > 0) { Buffer<wchar_t> buffer(len); DWORD n = GetCurrentDirectoryW(len, buffer.begin()); if (n > 0 && n <= len) { UnicodeConverter::toUTF8(buffer.begin(), result); if (result[result.size() - 1] != '\\') result.append("\\"); return result; } } throw SystemException("Cannot get current directory"); }
RWLockImpl::RWLockImpl() { #if defined(POCO_VXWORKS) // This workaround is for VxWorks 5.x where // pthread_mutex_init() won't properly initialize the mutex // resulting in a subsequent freeze in pthread_mutex_destroy() // if the mutex has never been used. std::memset(&_mutex, 0, sizeof(_mutex)); #endif pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); if (pthread_mutex_init(&_mutex, &attr)) { pthread_mutexattr_destroy(&attr); throw SystemException("cannot create mutex"); } pthread_mutexattr_destroy(&attr); }
/* * vislib::sys::IPCSemaphore::TryLock */ bool vislib::sys::IPCSemaphore::TryLock(void) { struct sembuf lock = { MEMBER_IDX, -1, IPC_NOWAIT }; int error = 0; if (this->getCount() < 1) { return false; } if ((::semop(this->id, &lock, 1)) == -1) { if ((error = ::GetLastError()) == EAGAIN) { return false; } else { throw SystemException(__FILE__, __LINE__); } } return true; }
bool MutexImpl::tryLockImpl() { switch (WaitForSingleObject(_mutex, 0)) { case WAIT_TIMEOUT: return false; case WAIT_OBJECT_0: if (!_recursive && _lockCount > 0) { ReleaseMutex(_mutex); return false; } ++_lockCount; return true; default: throw SystemException("cannot lock mutex"); } }
bool RWLockImpl::tryReadLockImpl() { HANDLE h[2]; h[0] = _mutex; h[1] = _readEvent; switch (WaitForMultipleObjects(2, h, TRUE, 1)) { case WAIT_OBJECT_0: case WAIT_OBJECT_0 + 1: ++_readers; ResetEvent(_writeEvent); ReleaseMutex(_mutex); return true; case WAIT_TIMEOUT: return false; default: throw SystemException("cannot lock reader/writer lock"); } }
/* * vislib::sys::Path::GetUserHomeDirectoryW */ vislib::StringW vislib::sys::Path::GetUserHomeDirectoryW(void) { #ifdef _WIN32 StringW retval; if (FAILED(::SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, 0, retval.AllocateBuffer(MAX_PATH)))) { throw SystemException(ERROR_NOT_FOUND, __FILE__, __LINE__); } if (!retval.EndsWith(SEPARATOR_W)) { retval += SEPARATOR_W; } return retval; #else /* _WIN32 */ return StringW(GetUserHomeDirectoryA()); #endif /* _WIN32 */ }
void Directory::Iterator::go(void) { errno = 0; while(true) { struct dirent *dirent = readdir((DIR *)dir); if(dirent) { if(cstring(dirent->d_name) != "." && cstring(dirent->d_name) != "..") { file = FileItem::get(path / dirent->d_name); return; } } else { if(errno) throw SystemException(errno, "file"); else file = 0; break; } } }
SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void*): _name(file.path()), _memHandle(INVALID_HANDLE_VALUE), _fileHandle(INVALID_HANDLE_VALUE), _size(0), _mode(PAGE_READONLY), _address(0) { if (!file.exists() || !file.isFile()) throw FileNotFoundException(_name); _size = static_cast<DWORD>(file.getSize()); DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; DWORD fileMode = GENERIC_READ; if (mode == SharedMemory::AM_WRITE) { _mode = PAGE_READWRITE; fileMode |= GENERIC_WRITE; } #if defined (POCO_WIN32_UTF8) std::wstring utf16name; UnicodeConverter::toUTF16(_name, utf16name); _fileHandle = CreateFileW(utf16name.c_str(), fileMode, shareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); #else _fileHandle = CreateFileA(_name.c_str(), fileMode, shareMode, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); #endif if (_fileHandle == INVALID_HANDLE_VALUE) throw OpenFileException("Cannot open memory mapped file", _name); _memHandle = CreateFileMapping(_fileHandle, NULL, _mode, 0, 0, NULL); if (!_memHandle) { CloseHandle(_fileHandle); _fileHandle = INVALID_HANDLE_VALUE; throw SystemException("Cannot map file into shared memory", _name); } map(); }
// // Destructor // DatagramSocket::~DatagramSocket() throw(SystemException) { // Close this socket if (! isClosed()) { try { close(); } catch(IOException& e) { delete _impl; throw SystemException(e.what()); } } // Release resources delete _impl; }
bool RWLockImpl::tryReadLockImpl() { for (;;) { if (_writers != 0 || _writersWaiting != 0) return false; DWORD result = tryReadLockOnce(); switch (result) { case WAIT_OBJECT_0: case WAIT_OBJECT_0 + 1: return true; case WAIT_TIMEOUT: continue; // try again default: throw SystemException("cannot lock reader/writer lock"); } } }
static String getThisProgramFileName() { MemArray<char> buffer(2000); long len = 0; while (true) { len = GetModuleFileName(NULL, buffer.getPtr(), buffer.getLength()); if (len == buffer.getLength()) { buffer.increaseTo(len + 1000); continue; } else { break; } } if (len > 0) { return String(buffer.getPtr(), len); } else { throw SystemException(String() << "Error in call to Win32 GetModuleFileName: " << getLastWin32ErrorMessage()); } }
bool MutexImpl::tryLockImpl(long milliseconds) { const int sleepMillis = 5; Timestamp now; Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000); do { try { if (tryLockImpl()) return true; } catch (...) { throw SystemException("cannot lock mutex"); } Sleep(sleepMillis); } while (!now.isElapsed(diff)); return false; }
static String toWindowsFileName(const String& cygwinFileName) { MemArray<char> buffer(2000); while (true) { ssize_t rc = cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_ABSOLUTE, cygwinFileName.toCString(), buffer.getPtr(), buffer.getLength()); if (rc != 0 && errno == ENOSPC) { buffer.increaseTo(buffer.getLength() + 1000); continue; } else if (rc != 0) { throw SystemException(String() << "error in call to cygwin_conv_path for '" << cygwinFileName << "': " << strerror(errno)); } else { return String(buffer.getPtr()); } } }
unsigned int readExact(int fd, void *buf, unsigned int size, unsigned long long *timeout) { ssize_t ret; unsigned int alreadyRead = 0; while (alreadyRead < size) { if (timeout != NULL && !waitUntilReadable(fd, timeout)) { throw TimeoutException("Cannot read enough data within the specified timeout"); } ret = syscalls::read(fd, (char *) buf + alreadyRead, size - alreadyRead); if (ret == -1) { int e = errno; throw SystemException("read() failed", e); } else if (ret == 0) { return alreadyRead; } else { alreadyRead += ret; } } return alreadyRead; }
uint32_t SocketTransport::Write(const uint32_t size, const void* buffer) { if (0 == size) { return 0; } const int status = m_socket.Send(reinterpret_cast<const char*>(buffer), static_cast<int>(size), 0); if (status > 0) { return static_cast<uint32_t>(status); } const DWORD code = WSAGetLastError(); if (WSAEWOULDBLOCK == code) { return 0; } throw SystemException("Couldn't write data to a socket"); }
void RWLockImpl::writeLockImpl() { addWriter(); HANDLE h[2]; h[0] = _mutex; h[1] = _writeEvent; switch (WaitForMultipleObjects(2, h, TRUE, INFINITE)) { case WAIT_OBJECT_0: case WAIT_OBJECT_0 + 1: --_writers; ++_readers; ResetEvent(_readEvent); ResetEvent(_writeEvent); ReleaseMutex(_mutex); break; default: removeWriter(); throw SystemException("cannot lock reader/writer lock"); } }
uint32_t SocketTransport::Read(const uint32_t size, void* buffer) { if (0 == size) { return 0; } const int status = m_socket.Recv(reinterpret_cast<char*>(buffer), size, 0); if (status > 0) { return static_cast<uint32_t>(status); } const DWORD code = WSAGetLastError(); if (WSAEWOULDBLOCK == code) { return 0; } throw SystemException("Couldn't read data from a socket"); }
DWORD RWLockImpl::tryReadLockOnce() { HANDLE h[2]; h[0] = _mutex; h[1] = _readEvent; DWORD result = WaitForMultipleObjects(2, h, TRUE, 1); switch (result) { case WAIT_OBJECT_0: case WAIT_OBJECT_0 + 1: ++_readers; ResetEvent(_writeEvent); ReleaseMutex(_mutex); poco_assert_dbg(_writers == 0); return result; case WAIT_TIMEOUT: return result; default: throw SystemException("cannot lock reader/writer lock"); } }
/* * vislib::sys::Path::GetCurrentDirectoryW */ vislib::StringW vislib::sys::Path::GetCurrentDirectoryW(void) { #ifdef _WIN32 DWORD bufferSize = ::GetCurrentDirectoryW(0, NULL); wchar_t *buffer = new wchar_t[bufferSize]; if (::GetCurrentDirectoryW(bufferSize, buffer) == 0) { ARY_SAFE_DELETE(buffer); throw SystemException(__FILE__, __LINE__); } StringW retval(buffer); ARY_SAFE_DELETE(buffer); if (!retval.EndsWith(SEPARATOR_W)) { retval += SEPARATOR_W; } return retval; #else /* _WIN32 */ return StringW(GetCurrentDirectoryA()); #endif /* _WIN32 */ }
void Shell::runAsAdmin(const TCHAR *pathToFile, const TCHAR *parameters) { SHELLEXECUTEINFO sei; ZeroMemory(&sei, sizeof(sei)); sei.cbSize = sizeof(SHELLEXECUTEINFOW); sei.hwnd = 0; sei.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS; sei.lpVerb = _T("runas"); sei.lpFile = pathToFile; sei.lpParameters = parameters; sei.nShow = SW_SHOWNORMAL; if (ShellExecuteEx(&sei) == FALSE) { throw SystemException(); } WaitForSingleObject(sei.hProcess, INFINITE); CloseHandle(sei.hProcess); }