void ParRenamer::CheckFiles(const char* destDir, bool checkPars) { DirBrowser dir(destDir); while (const char* filename = dir.Next()) { if (!IsStopped()) { BString<1024> fullFilename("%s%c%s", destDir, PATH_SEPARATOR, filename); if (!FileSystem::DirectoryExists(fullFilename)) { m_progressLabel.Format("Checking file %s", filename); m_stageProgress = m_fileCount > 0 ? m_curFile * 1000 / m_fileCount / 2 : 1000; UpdateProgress(); m_curFile++; if (checkPars) { CheckParFile(destDir, fullFilename); } else { CheckRegularFile(destDir, fullFilename); } } } } }
void PrePostProcessor::DeleteCleanup(NzbInfo* nzbInfo) { if (nzbInfo->GetCleanupDisk() || nzbInfo->GetDeleteStatus() == NzbInfo::dsDupe) { // download was cancelled, deleting already downloaded files from disk for (CompletedFile& completedFile: nzbInfo->GetCompletedFiles()) { BString<1024> fullFileName("%s%c%s", nzbInfo->GetDestDir(), PATH_SEPARATOR, completedFile.GetFilename()); if (FileSystem::FileExists(fullFileName)) { detail("Deleting file %s", completedFile.GetFilename()); FileSystem::DeleteFile(fullFileName); } } // delete .out.tmp-files DirBrowser dir(nzbInfo->GetDestDir()); while (const char* filename = dir.Next()) { int len = strlen(filename); if (len > 8 && !strcmp(filename + len - 8, ".out.tmp")) { BString<1024> fullFilename("%s%c%s", nzbInfo->GetDestDir(), PATH_SEPARATOR, filename); detail("Deleting file %s", filename); FileSystem::DeleteFile(fullFilename); } } // delete old directory (if empty) FileSystem::DeleteDirectory(nzbInfo->GetDestDir()); } }
bool sBuffer::read(std::string const& _filename) { bool status = true; // Open file for reading. std::ifstream file; std::string errorString; std::string fullFilename(_filename); if(!btg::core::os::fileOperation::check(fullFilename, errorString, false)) { //BTG_NOTICE("Failed to read file " << _filename << "."); return false; } #if HAVE_IOS_BASE file.open(fullFilename.c_str(), std::ios_base::in | std::ios_base::binary); #else file.open(fullFilename.c_str(), std::ios::in | std::ios::binary); #endif if (file.is_open()) { // Find out the size of the file. #if HAVE_IOS_BASE file.seekg (0, std::ios_base::end); size_ = file.tellg(); file.seekg (0, std::ios_base::beg); #else file.seekg (0, std::ios::end); size_ = file.tellg(); file.seekg (0, ios::beg); #endif if (size_ > 0) { t_uint temp = size_; reset(); size_ = temp; buffer_ = new t_byte[size_]; file.read(reinterpret_cast<char*>(buffer_), size_); } file.close(); } else { size_ = 0; delete [] buffer_; buffer_ = 0; status = false; } return status; }
void ParRenamer::BuildDirList(const char* destDir) { m_dirList.push_back(destDir); DirBrowser dirBrowser(destDir); while (const char* filename = dirBrowser.Next()) { if (!IsStopped()) { BString<1024> fullFilename("%s%c%s", destDir, PATH_SEPARATOR, filename); if (FileSystem::DirectoryExists(fullFilename)) { BuildDirList(fullFilename); } else { m_fileCount++; } } } }
string Gesture::getForegroundFilename() { string filename = ""; HWND hwnd = GetForegroundWindow(); DWORD result = 0; DWORD processID = 0; GetWindowThreadProcessId(hwnd, &processID); HMODULE hMods[1024]; HANDLE hProcess; DWORD cbNeeded; unsigned int i; // Get a handle to the process. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); if (NULL == hProcess) return false; TCHAR szModName[MAX_PATH]; // Get the full path to the module's file. if (GetModuleFileNameEx(hProcess, NULL, szModName, sizeof(szModName) / sizeof(TCHAR))) { // Convert full filename to string std::wstring arr_w(szModName); std::string fullFilename(arr_w.begin(), arr_w.end()); // Get executable name only vector<string> parts = explode(fullFilename, '\\'); if (parts.size() > 0) filename = parts.back(); } CloseHandle(hProcess); return filename; }
bool FileSystem::DeleteDirectoryWithContent(const char* dirFilename, CString& errmsg) { errmsg.Clear(); bool del = false; bool ok = true; { DirBrowser dir(dirFilename); while (const char* filename = dir.Next()) { BString<1024> fullFilename("%s%c%s", dirFilename, PATH_SEPARATOR, filename); if (FileSystem::DirectoryExists(fullFilename)) { del = DeleteDirectoryWithContent(fullFilename, errmsg); } else { del = DeleteFile(fullFilename); } ok &= del; if (!del && errmsg.Empty()) { errmsg.Format("could not delete %s: %s", *fullFilename, *GetLastErrorMessage()); } } } // make sure "DirBrowser dir" is destroyed (and has closed its handle) before we trying to delete the directory del = RemoveDirectory(dirFilename); ok &= del; if (!del && errmsg.Empty()) { errmsg = GetLastErrorMessage(); } return ok; }
void NntpProcessor::SendSegment() { detail("[%i] Sending segment %s (%i=%lli:%i)", m_id, *m_filename, m_part, (long long)m_offset, m_size); if (m_speed > 0) { m_start = Util::GetCurrentTicks(); } BString<1024> fullFilename("%s/%s", m_dataDir, *m_filename); BString<1024> cacheFileDir("%s/%s", m_cacheDir, *m_filename); BString<1024> cacheFileName("%i=%lli-%i", m_part, (long long)m_offset, m_size); BString<1024> cacheFullFilename("%s/%s", *cacheFileDir, *cacheFileName); BString<1024> cacheKey("%s/%s", *m_filename, *cacheFileName); const char* cachedData = nullptr; int cachedSize; if (m_cache) { m_cache->Find(cacheKey, cachedData, cachedSize); } DiskFile cacheFile; bool readCache = !cachedData && m_cacheDir && cacheFile.Open(cacheFullFilename, DiskFile::omRead); bool writeCache = !cachedData && m_cacheDir && !readCache; StringBuilder cacheMem; if (m_cache && !cachedData) { cacheMem.Reserve((int)(m_size * 1.1)); } CString errmsg; if (writeCache && !FileSystem::ForceDirectories(cacheFileDir, errmsg)) { error("Could not create directory %s: %s", *cacheFileDir, *errmsg); } if (writeCache && !cacheFile.Open(cacheFullFilename, DiskFile::omWrite)) { error("Could not create file %s: %s", *cacheFullFilename, *FileSystem::GetLastErrorMessage()); } if (!cachedData && !readCache && !FileSystem::FileExists(fullFilename)) { m_connection->WriteLine(CString::FormatStr("430 Article not found\r\n")); return; } YEncoder encoder(fullFilename, m_part, m_offset, m_size, [proc = this, writeCache, &cacheFile, &cacheMem](const char* buf, int size) { if (proc->m_cache) { cacheMem.Append(buf); } if (writeCache) { cacheFile.Write(buf, size); } proc->SendData(buf, size); }); if (!cachedData && !readCache && !encoder.OpenFile(errmsg)) { m_connection->WriteLine(CString::FormatStr("403 %s\r\n", *errmsg)); return; } m_connection->WriteLine(CString::FormatStr("%i, 0 %s\r\n", m_sendHeaders ? 222 : 220, m_messageid)); if (m_sendHeaders) { m_connection->WriteLine(CString::FormatStr("Message-ID: %s\r\n", m_messageid)); m_connection->WriteLine(CString::FormatStr("Subject: \"%s\"\r\n", FileSystem::BaseFileName(m_filename))); m_connection->WriteLine("\r\n"); } if (cachedData) { SendData(cachedData, cachedSize); } else if (readCache) { cacheFile.Seek(0, DiskFile::soEnd); int size = (int)cacheFile.Position(); CharBuffer buf(size); cacheFile.Seek(0); if (cacheFile.Read((char*)buf, size) != size) { error("Could not read file %s: %s", *cacheFullFilename, *FileSystem::GetLastErrorMessage()); } if (m_cache) { cacheMem.Append(buf, size); } SendData(buf, size); } else { encoder.WriteSegment(); } if (!cachedData && cacheMem.Length() > 0) { m_cache->Append(cacheKey, cacheMem, cacheMem.Length()); } m_connection->WriteLine(".\r\n"); }
void WinConsole::ResetFactoryDefaults() { BString<1024> path; CString errmsg; g_Options->SetPauseDownload(true); g_Options->SetPausePostProcess(true); char commonAppDataPath[MAX_PATH]; SHGetFolderPath(nullptr, CSIDL_COMMON_APPDATA, nullptr, 0, commonAppDataPath); // delete default directories const char* DefDirs[] = {"nzb", "tmp", "queue", "scripts"}; for (int i=0; i < 4; i++) { path.Format("%s\\NZBGet\\%s", commonAppDataPath, DefDirs[i]); // try to delete the directory int retry = 10; while (retry > 0 && FileSystem::DirectoryExists(path) && !FileSystem::DeleteDirectoryWithContent(path, errmsg)) { usleep(200 * 1000); retry--; } if (FileSystem::DirectoryExists(path)) { MessageBox(m_trayWindow, BString<1024>("Could not delete directory %s:\n%s.\nPlease delete the directory manually and try again.", *path, *errmsg), "NZBGet", MB_ICONERROR); return; } } // delete old config file in the program's directory path.Format("%s\\nzbget.conf", g_Options->GetAppDir()); FileSystem::DeleteFile(path); errmsg = FileSystem::GetLastErrorMessage(); if (FileSystem::FileExists(path)) { MessageBox(m_trayWindow, BString<1024>("Could not delete file %s:\n%s.\nPlease delete the file manually and try again.", *path, *errmsg), "NZBGet", MB_ICONERROR); return; } // delete config file in default directory path.Format("%s\\NZBGet\\nzbget.conf", commonAppDataPath); FileSystem::DeleteFile(path); errmsg = FileSystem::GetLastErrorMessage(); if (FileSystem::FileExists(path)) { MessageBox(m_trayWindow, BString<1024>("Could not delete file %s:\n%s.\nPlease delete the file manually and try again.", *path, *errmsg), "NZBGet", MB_ICONERROR); return; } // delete log files in default directory path.Format("%s\\NZBGet", commonAppDataPath); DirBrowser dir(path); while (const char* filename = dir.Next()) { if (Util::MatchFileExt(filename, ".log", ",")) { BString<1024> fullFilename("%s%c%s", *path, PATH_SEPARATOR, filename); FileSystem::DeleteFile(fullFilename); // ignore errors } } MessageBox(m_trayWindow, "The program has been reset to factory defaults.", "NZBGet", MB_ICONINFORMATION); mayStartBrowser = true; Reload(); }
bool FileSystem::FileExists(const char* path, const char* filenameWithoutPath) { BString<1024> fullFilename("%s%c%s", path, (int)PATH_SEPARATOR, filenameWithoutPath); bool exists = FileExists(fullFilename); return exists; }