bool SCXFile::ReadAvailableBytesAsUnsigned(const SCXFilePath& , unsigned char* , size_t , size_t /*= 0*/) { throw SCXNotSupportedException(L"Reads not supported on windows", SCXSRCLOCATION); #else bool SCXFile::ReadAvailableBytesAsUnsigned(const SCXFilePath& path, unsigned char* buf, size_t size, size_t offset /*= 0*/) { int fd = open(SCXFileSystem::EncodePath(path).c_str(),O_RDONLY); if (-1 == fd) { throw SCXErrnoException(L"open(" + path.Get() + L")", errno, SCXSRCLOCATION); } size_t remainder; void *pDevice = NULL; #ifdef _SC_PAGESIZE //!< Size of a page in bytes remainder = offset% sysconf(_SC_PAGESIZE); #else remainder = offset% getpagesize(); #endif // //For API mmap, the sixth parameter mmapoffset must be a multiple of pagesize. // size_t mmapoffset = offset - remainder; pDevice = mmap(NULL, remainder + size, PROT_READ, MAP_SHARED, fd, mmapoffset); if (MAP_FAILED == pDevice) { return false; } memcpy(buf, reinterpret_cast<unsigned char*>(pDevice) + remainder, size); munmap(reinterpret_cast<char*>(pDevice), remainder + size); close(fd); return true; #endif }
void SCXGlob::DoGlob() { SCX_LOGTRACE(m_logHandle, L"Initialize()"); // Set a default m_index = cNoData; if (this->m_pathnames) { globfree(&this->m_globHolder); memset(&m_globHolder, 0, sizeof(glob_t)); this->m_pathnames = 0; } // Prepares the flag to pass to the OS-provided glob(). int flag = 0; if (!(this->m_isBackSlashEscapeOn)) { flag |= GLOB_NOESCAPE; } if (this->m_isErrorAbortOn) { flag |= GLOB_ERR; } // Calls the OS-provided glob(). int ret = glob(m_pattern.c_str(), flag, NULL, &this->m_globHolder); switch (ret) { case 0: this->m_pathnames = this->m_globHolder.gl_pathv; m_index = cBegin; break; case GLOB_NOMATCH: // No matching pathname exists in the file system. // Does nothing here. break; case GLOB_NOSPACE: { globfree(&this->m_globHolder); wstring message = L"SCXGlob_NoSpace_Error: " + StrFromUTF8(this->m_pattern); throw SCXResourceExhaustedException(L"Memory", message, SCXSRCLOCATION); } case GLOB_ABORTED: { // We are here only if isErrorAbortOn is true. globfree(&this->m_globHolder); wstring message = L"SCXGlob::Initialize(): " + StrFromUTF8(this->m_pattern); throw SCXErrnoException(message, errno, SCXSRCLOCATION); } default: globfree(&this->m_globHolder); wstring message = L"SCXGlob_Unknown_Error: " + StrFromUTF8(this->m_pattern); throw SCXInternalErrorException(message, SCXSRCLOCATION); } }
size_t SCXFile::ReadAvailableBytes(const SCXFilePath& , char* , size_t , size_t /*= 0*/) { throw SCXNotSupportedException(L"non-blocking reads not supported on windows", SCXSRCLOCATION); #else size_t SCXFile::ReadAvailableBytes(const SCXFilePath& path, char* buf, size_t size, size_t offset /*= 0*/) { int fd = open(SCXFileSystem::EncodePath(path).c_str(), 0, O_RDONLY); if (-1 == fd) { throw SCXErrnoException(L"open(" + path.Get() + L")", errno, SCXSRCLOCATION); } int fd_flags = fcntl(fd, F_GETFL); if (fd_flags < 0) { close(fd); throw SCXErrnoException(L"fcntl(F_GETFL, " + path.Get() + L")", errno, SCXSRCLOCATION); } if (-1 == fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK)) { close(fd); throw SCXErrnoException(L"fcntl(F_SETFL, O_NONBLOCK, " + path.Get() + L")", errno, SCXSRCLOCATION); } if (-1 == lseek(fd, offset, SEEK_SET)) { close(fd); if (ESPIPE == errno) { return 0; } throw SCXErrnoException(L"lseek(" + path.Get() + L")", errno, SCXSRCLOCATION); } size_t r = 0; while (r < size) { // Using a loop to read required on solaris. size_t t = read(fd, buf+r, size-r); if (static_cast<size_t>(-1) == t) { if (EAGAIN == errno) { break; } close(fd); throw SCXErrnoException(L"read(" + path.Get() + L")", errno, SCXSRCLOCATION); } r += t; if (0 == t) { break; } } close(fd); return r; #endif }
/** Make the application support log rotate by installing necessary signal handler. */ void SCXLogHandleFactory::InstallLogRotateSupport() { struct sigaction action; sigemptyset(&action.sa_mask); action.sa_sigaction = 0; action.sa_handler = reinterpret_cast<SCXLogRotateHandlerPtr> (& HandleLogRotate); action.sa_flags = 0; struct sigaction priorAction; if (sigaction(LOGROTATE_REACTION_SIGNAL, &action, &priorAction) < 0) { SCXErrnoException(L"sigaction", errno, SCXSRCLOCATION); } s_nextSignalHandler = priorAction.sa_handler; }