/** * Load a library * @param filepath :: A Poco::File The full path to a library as a string * @param cacheKey :: An identifier for the cache if loading is successful * @return 1 if the file loaded successfully, 0 otherwise */ int LibraryManagerImpl::openLibrary(const Poco::File &filepath, const std::string &cacheKey) { // Try to open the library. The wrapper will unload the library when it // is deleted LibraryWrapper dlwrap; if (dlwrap.openLibrary(filepath.path())) { // Successfully opened, so add to map if (g_log.is(Poco::Message::PRIO_DEBUG)) { g_log.debug("Opened library: " + filepath.path() + ".\n"); } m_openedLibs.emplace(cacheKey, std::move(dlwrap)); return 1; } else return 0; }
int CStatsUtil::DoExportGameScripts() { //Validate output + input paths Poco::Path inpath(m_inputPath); Poco::Path outpath; if( m_outputPath.empty() ) outpath = inpath.absolute().makeParent().append(DefExportScriptsDir); else outpath = Poco::Path(m_outputPath).makeAbsolute(); Poco::File fTestOut = outpath; if( ! fTestOut.exists() ) { cout << "Created output directory \"" << fTestOut.path() <<"\"!\n"; fTestOut.createDirectory(); } else if( ! fTestOut.isDirectory() ) throw runtime_error("CStatsUtil::DoExportGameScripts(): Output path is not a directory!"); //Setup the script handler GameScripts scripts( inpath.absolute().toString() ); //Convert to XML scripts.ExportScriptsToXML( outpath.toString() ); return 0; }
int CStatsUtil::DoExportAll() { Poco::Path inpath(m_inputPath); Poco::Path outpath; if( m_outputPath.empty() ) { outpath = inpath.absolute().makeParent().append(DefExportAllDir); } else { outpath = Poco::Path(m_outputPath).makeAbsolute(); } GameStats gstats( m_inputPath, m_langconf ); gstats.Load(); //Test output path Poco::File fTestOut = outpath; if( ! fTestOut.exists() ) { cout << "Created output directory \"" << fTestOut.path() <<"\"!\n"; fTestOut.createDirectory(); } gstats.ExportAll(outpath.toString()); return 0; }
/** * Opens suitable DLLs on a given path. * @param libpath A Poco::File object pointing to a directory where the * libraries are. * @param loadingBehaviour Control how libraries are searched for * @param excludes If not empty then each string is considered as a substring * to search within each library to be opened. If the substring is found then * the library is not opened. * @return The number of libraries opened. */ int LibraryManagerImpl::openLibraries( const Poco::File &libpath, LibraryManagerImpl::LoadLibraries loadingBehaviour, const std::vector<std::string> &excludes) { int libCount(0); if (libpath.exists() && libpath.isDirectory()) { // Iterate over the available files Poco::DirectoryIterator end_itr; for (Poco::DirectoryIterator itr(libpath); itr != end_itr; ++itr) { const Poco::File &item = *itr; if (item.isFile()) { if (shouldBeLoaded(itr.path().getFileName(), excludes)) libCount += openLibrary(itr.path(), itr.path().getFileName()); else continue; } else if (loadingBehaviour == LoadLibraries::Recursive) { // it must be a directory libCount += openLibraries(item, LoadLibraries::Recursive, excludes); } } } else { g_log.error("In OpenAllLibraries: " + libpath.path() + " must be a directory."); } return libCount; }
void Logger::enableFileLogging(const std::string& fileName, int level) { Mutex::ScopedLock lock(loggerMutex); Logger::setLevel(level); // close the current file log and re-create it. disableFileLogging(); if (!simpleFileChannel) { std::string realName; // figure out what file name to use if (fileName.length() == 0) { // none given, use one from config. realName = Config::getString(Config::LOGGER_LOG_FILE_PATH); } else { realName = fileName; } if (realName.length() == 0) { // default log name. realName = joinPath(getTempDir(), "roadrunner.log"); } else { // expand any env vars and make absolute path. realName = Poco::Path::expand(realName); Poco::Path path(realName); realName = path.makeAbsolute().toString(); } // check if the path is writable Poco::Path p(realName); Poco::File fdir = p.parent(); if(!fdir.exists()) { realName = joinPath(getTempDir(), "roadrunner.log"); Log(Logger::LOG_ERROR) << "The specified log file directory path, " << fdir.path() << " does not exist, using default log file path: " << realName; } SplitterChannel *splitter = getSplitterChannel(); simpleFileChannel = new SimpleFileChannel(); simpleFileChannel->setProperty("path", realName); simpleFileChannel->setProperty("rotation", "never"); logFileName = simpleFileChannel->getProperty("path"); splitter->addChannel(simpleFileChannel); simpleFileChannel->release(); } }
SharedMemoryImpl::SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint): _size(0), _fd(-1), _address(0), _access(mode), _name(file.path()), _fileMapped(true), _server(false) { if (!file.exists() || !file.isFile()) throw FileNotFoundException(file.path()); _size = file.getSize(); int flag = O_RDONLY; if (mode == SharedMemory::AM_WRITE) flag = O_RDWR; _fd = ::open(_name.c_str(), flag); if (-1 == _fd) throw OpenFileException("Cannot open memory mapped file", _name); map(addrHint); }
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(); }
void FileChecker::updateImpl(const Poco::File & file) { map[Poco::Path(file.path()).getFileName()] = file.getSize(); }
/** * Module initialization function. The initialization arguments string should * provide the path of a module configuration file that defines what files * are interesting. If the empty string is passed to this function, the module * assumes a default config file is present in the output directory. * * @param args Path of the configuration file that defines what files are * interesting, may be set to the empty string. * @return TskModule::OK on success, TskModule::FAIL otherwise. */ TSK_MODULE_EXPORT TskModule::Status initialize(const char* arguments) { TskModule::Status status = TskModule::OK; const std::string MSG_PREFIX = "InterestingFilesModule::initialize : "; try { // Make sure the file sets are cleared in case initialize() is called more than once. fileSets.clear(); configFilePath.assign(arguments); if (configFilePath.empty()) { // Use the default config file path. Poco::Path configurationFilePath(Poco::Path::forDirectory(GetSystemProperty(TskSystemProperties::MODULE_CONFIG_DIR))); configurationFilePath.pushDirectory(MODULE_NAME); configurationFilePath.setFileName(DEFAULT_CONFIG_FILE_NAME); configFilePath = configurationFilePath.toString(); } // Compile the contents of the config file into interesting file set definitions. Poco::File configFile = Poco::File(configFilePath); if (configFile.exists()) { std::ifstream configStream(configFile.path().c_str()); if (configStream) { Poco::XML::InputSource inputSource(configStream); Poco::AutoPtr<Poco::XML::Document> configDoc = Poco::XML::DOMParser().parse(&inputSource); Poco::XML::Element * rootElement = configDoc->documentElement(); if (rootElement == NULL) { std::ostringstream msg; msg << MSG_PREFIX << "Root element of config file is NULL."; throw TskException(msg.str()); } const std::string& ignoreKnownValue = Poco::XML::fromXMLString(rootElement->getAttribute(IGNORE_KNOWN_TAG)); if (!ignoreKnownValue.empty()) { knownType = parseKnownType(ignoreKnownValue); ignoreKnown = true; } Poco::AutoPtr<Poco::XML::NodeList> fileSetDefinitions = configDoc->getElementsByTagName(INTERESTING_FILE_SET_ELEMENT_TAG); for (unsigned long i = 0; i < fileSetDefinitions->length(); ++i) { compileInterestingFilesSet(fileSetDefinitions->item(i)); } } else { std::ostringstream msg; msg << MSG_PREFIX << "failed to open config file '" << configFilePath << "'"; throw TskException(msg.str()); } } else { std::ostringstream msg; msg << MSG_PREFIX << "config file'" << configFilePath << "' does not exist"; LOGERROR(msg.str()); } // Log the configuration. std::ostringstream msg; msg << MSG_PREFIX << "configured with " << fileSets.size() << " interesting file set definitions from '" << configFilePath << "'"; LOGINFO(msg.str()); } catch (TskException &ex) { status = TskModule::FAIL; configFilePath.clear(); std::ostringstream msg; msg << MSG_PREFIX << "TskException: " << ex.message(); LOGERROR(msg.str()); } catch (Poco::Exception &ex) { status = TskModule::FAIL; configFilePath.clear(); std::ostringstream msg; msg << MSG_PREFIX << "Poco::Exception: " << ex.displayText(); LOGERROR(msg.str()); } catch (std::exception &ex) { status = TskModule::FAIL; configFilePath.clear(); std::ostringstream msg; msg << MSG_PREFIX << "std::exception: " << ex.what(); LOGERROR(msg.str()); } catch (...) { status = TskModule::FAIL; configFilePath.clear(); LOGERROR(MSG_PREFIX + "unrecognized exception"); } return status; }