From 321c7efa4133bf1bb8db306351ae902d9c930e7d Mon Sep 17 00:00:00 2001 From: Andrey Prygunkov Date: Tue, 22 Dec 2015 22:01:03 +0100 Subject: [PATCH] #130: moved parts from module "Util" into new module "FileSystem" --- Makefile.am | 2 + Makefile.in | 26 +- daemon/connect/WebDownloader.cpp | 5 +- daemon/extension/NzbScript.cpp | 4 +- daemon/extension/QueueScript.cpp | 9 +- daemon/extension/ScanScript.cpp | 10 +- daemon/extension/SchedulerScript.cpp | 7 +- daemon/extension/ScriptConfig.cpp | 17 +- daemon/feed/FeedCoordinator.cpp | 3 +- daemon/main/CommandLineParser.cpp | 7 +- daemon/main/DiskService.cpp | 7 +- daemon/main/Maintenance.cpp | 18 +- daemon/main/Options.cpp | 43 +- daemon/main/nzbget.cpp | 5 +- daemon/nntp/ArticleWriter.cpp | 79 +-- daemon/postprocess/Cleanup.cpp | 17 +- daemon/postprocess/DupeMatcher.cpp | 3 +- daemon/postprocess/ParChecker.cpp | 47 +- daemon/postprocess/ParCoordinator.cpp | 12 +- daemon/postprocess/ParParser.cpp | 2 +- daemon/postprocess/ParRenamer.cpp | 27 +- daemon/postprocess/PrePostProcessor.cpp | 7 +- daemon/postprocess/Unpack.cpp | 33 +- daemon/queue/DiskState.cpp | 31 +- daemon/queue/DownloadInfo.cpp | 9 +- daemon/queue/HistoryCoordinator.cpp | 17 +- daemon/queue/NzbFile.cpp | 9 +- daemon/queue/QueueCoordinator.cpp | 3 +- daemon/queue/QueueEditor.cpp | 5 +- daemon/queue/Scanner.cpp | 37 +- daemon/queue/UrlCoordinator.cpp | 5 +- daemon/remote/BinRpc.cpp | 5 +- daemon/remote/RemoteClient.cpp | 3 +- daemon/remote/RemoteServer.cpp | 6 +- daemon/remote/WebServer.cpp | 3 +- daemon/remote/XmlRpc.cpp | 7 +- daemon/util/FileSystem.cpp | 846 +++++++++++++++++++++++ daemon/util/FileSystem.h | 103 +++ daemon/util/Log.cpp | 9 +- daemon/util/Script.cpp | 7 +- daemon/util/Util.cpp | 865 +----------------------- daemon/util/Util.h | 75 +- daemon/windows/WinConsole.cpp | 40 +- nzbget.vcxproj | 2 + tests/postprocess/DupeMatcherTest.cpp | 9 +- tests/postprocess/ParRenamerTest.cpp | 5 +- tests/suite/TestMain.cpp | 3 +- tests/suite/TestUtil.cpp | 26 +- 48 files changed, 1314 insertions(+), 1206 deletions(-) create mode 100644 daemon/util/FileSystem.cpp create mode 100644 daemon/util/FileSystem.h diff --git a/Makefile.am b/Makefile.am index 68758e0d..870031ad 100644 --- a/Makefile.am +++ b/Makefile.am @@ -143,6 +143,8 @@ nzbget_SOURCES = \ daemon/util/Thread.h \ daemon/util/Service.cpp \ daemon/util/Service.h \ + daemon/util/FileSystem.cpp \ + daemon/util/FileSystem.h \ daemon/util/Util.cpp \ daemon/util/Util.h \ code_revision.cpp diff --git a/Makefile.in b/Makefile.in index 46ddbecd..52d10e9c 100644 --- a/Makefile.in +++ b/Makefile.in @@ -215,7 +215,8 @@ am__nzbget_SOURCES_DIST = daemon/connect/Connection.cpp \ daemon/util/Observer.h daemon/util/Script.cpp \ daemon/util/Script.h daemon/util/Thread.cpp \ daemon/util/Thread.h daemon/util/Service.cpp \ - daemon/util/Service.h daemon/util/Util.cpp daemon/util/Util.h \ + daemon/util/Service.h daemon/util/FileSystem.cpp \ + daemon/util/FileSystem.h daemon/util/Util.cpp daemon/util/Util.h \ code_revision.cpp lib/par2/commandline.cpp \ lib/par2/commandline.h lib/par2/crc.cpp lib/par2/crc.h \ lib/par2/creatorpacket.cpp lib/par2/creatorpacket.h \ @@ -291,8 +292,9 @@ am_nzbget_OBJECTS = Connection.$(OBJEXT) TlsSocket.$(OBJEXT) \ RemoteClient.$(OBJEXT) RemoteServer.$(OBJEXT) \ WebServer.$(OBJEXT) XmlRpc.$(OBJEXT) Log.$(OBJEXT) \ NString.$(OBJEXT) Observer.$(OBJEXT) Script.$(OBJEXT) \ - Thread.$(OBJEXT) Service.$(OBJEXT) Util.$(OBJEXT) \ - code_revision.$(OBJEXT) $(am__objects_1) $(am__objects_2) + Thread.$(OBJEXT) Service.$(OBJEXT) FileSystem.$(OBJEXT) \ + Util.$(OBJEXT) code_revision.$(OBJEXT) $(am__objects_1) \ + $(am__objects_2) nzbget_OBJECTS = $(am_nzbget_OBJECTS) nzbget_LDADD = $(LDADD) am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; @@ -497,7 +499,8 @@ nzbget_SOURCES = daemon/connect/Connection.cpp \ daemon/util/Observer.h daemon/util/Script.cpp \ daemon/util/Script.h daemon/util/Thread.cpp \ daemon/util/Thread.h daemon/util/Service.cpp \ - daemon/util/Service.h daemon/util/Util.cpp daemon/util/Util.h \ + daemon/util/Service.h daemon/util/FileSystem.cpp \ + daemon/util/FileSystem.h daemon/util/Util.cpp daemon/util/Util.h \ code_revision.cpp $(am__append_1) $(am__append_2) AM_CPPFLAGS = -I$(srcdir)/daemon/connect -I$(srcdir)/daemon/extension \ -I$(srcdir)/daemon/feed -I$(srcdir)/daemon/frontend \ @@ -783,6 +786,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FeedFilterTest.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FeedInfo.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FeedScript.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/FileSystem.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Frontend.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/HistoryCoordinator.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Log.Po@am__quote@ @@ -1709,6 +1713,20 @@ Service.obj: daemon/util/Service.cpp @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o Service.obj `if test -f 'daemon/util/Service.cpp'; then $(CYGPATH_W) 'daemon/util/Service.cpp'; else $(CYGPATH_W) '$(srcdir)/daemon/util/Service.cpp'; fi` +FileSystem.o: daemon/util/FileSystem.cpp +@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT FileSystem.o -MD -MP -MF "$(DEPDIR)/FileSystem.Tpo" -c -o FileSystem.o `test -f 'daemon/util/FileSystem.cpp' || echo '$(srcdir)/'`daemon/util/FileSystem.cpp; \ +@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/FileSystem.Tpo" "$(DEPDIR)/FileSystem.Po"; else rm -f "$(DEPDIR)/FileSystem.Tpo"; exit 1; fi +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='daemon/util/FileSystem.cpp' object='FileSystem.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o FileSystem.o `test -f 'daemon/util/FileSystem.cpp' || echo '$(srcdir)/'`daemon/util/FileSystem.cpp + +FileSystem.obj: daemon/util/FileSystem.cpp +@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT FileSystem.obj -MD -MP -MF "$(DEPDIR)/FileSystem.Tpo" -c -o FileSystem.obj `if test -f 'daemon/util/FileSystem.cpp'; then $(CYGPATH_W) 'daemon/util/FileSystem.cpp'; else $(CYGPATH_W) '$(srcdir)/daemon/util/FileSystem.cpp'; fi`; \ +@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/FileSystem.Tpo" "$(DEPDIR)/FileSystem.Po"; else rm -f "$(DEPDIR)/FileSystem.Tpo"; exit 1; fi +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='daemon/util/FileSystem.cpp' object='FileSystem.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o FileSystem.obj `if test -f 'daemon/util/FileSystem.cpp'; then $(CYGPATH_W) 'daemon/util/FileSystem.cpp'; else $(CYGPATH_W) '$(srcdir)/daemon/util/FileSystem.cpp'; fi` + Util.o: daemon/util/Util.cpp @am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT Util.o -MD -MP -MF "$(DEPDIR)/Util.Tpo" -c -o Util.o `test -f 'daemon/util/Util.cpp' || echo '$(srcdir)/'`daemon/util/Util.cpp; \ @am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/Util.Tpo" "$(DEPDIR)/Util.Po"; else rm -f "$(DEPDIR)/Util.Tpo"; exit 1; fi diff --git a/daemon/connect/WebDownloader.cpp b/daemon/connect/WebDownloader.cpp index 3998ddbf..6d9f495c 100644 --- a/daemon/connect/WebDownloader.cpp +++ b/daemon/connect/WebDownloader.cpp @@ -28,6 +28,7 @@ #include "Log.h" #include "Options.h" #include "Util.h" +#include "FileSystem.h" WebDownloader::WebDownloader() { @@ -526,7 +527,7 @@ void WebDownloader::ParseFilename(const char* contentDisposition) WebUtil::HttpUnquote(fname); - m_originalFilename = Util::BaseFileName(fname); + m_originalFilename = FileSystem::BaseFileName(fname); debug("OriginalFilename: %s", *m_originalFilename); } @@ -652,7 +653,7 @@ void WebDownloader::LogDebugInfo() ctime_r(&m_lastUpdateTime, time); #endif - info(" Web-Download: status=%i, LastUpdateTime=%s, filename=%s", m_status, time, Util::BaseFileName(m_outputFilename)); + info(" Web-Download: status=%i, LastUpdateTime=%s, filename=%s", m_status, time, FileSystem::BaseFileName(m_outputFilename)); } void WebDownloader::Stop() diff --git a/daemon/extension/NzbScript.cpp b/daemon/extension/NzbScript.cpp index f91248a3..3ea62304 100644 --- a/daemon/extension/NzbScript.cpp +++ b/daemon/extension/NzbScript.cpp @@ -27,7 +27,7 @@ #include "NzbScript.h" #include "Options.h" #include "Log.h" -#include "Util.h" +#include "FileSystem.h" /** * If szStripPrefix is not NULL, only pp-parameters, whose names start with the prefix @@ -93,7 +93,7 @@ void NzbScriptController::ExecuteScriptList(const char* scriptList) Tokenizer tok(scriptList, ",;"); while (const char* scriptName = tok.Next()) { - if (Util::SameFilename(scriptName, script->GetName())) + if (FileSystem::SameFilename(scriptName, script->GetName())) { ExecuteScript(script); break; diff --git a/daemon/extension/QueueScript.cpp b/daemon/extension/QueueScript.cpp index d12ce6b0..6b0e29c2 100644 --- a/daemon/extension/QueueScript.cpp +++ b/daemon/extension/QueueScript.cpp @@ -30,6 +30,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" static const char* QUEUE_EVENT_NAMES[] = { "FILE_DOWNLOADED", "URL_COMPLETED", "NZB_ADDED", "NZB_DOWNLOADED", "NZB_DELETED" }; @@ -117,12 +118,12 @@ void QueueScriptController::Run() void QueueScriptController::ExecuteScript(ScriptConfig::Script* script) { PrintMessage(m_event == QueueScriptCoordinator::qeFileDownloaded ? Message::mkDetail : Message::mkInfo, - "Executing queue-script %s for %s", script->GetName(), Util::BaseFileName(m_nzbName)); + "Executing queue-script %s for %s", script->GetName(), FileSystem::BaseFileName(m_nzbName)); SetScript(script->GetLocation()); SetArgs(NULL, false); - BString<1024> infoName("queue-script %s for %s", script->GetName(), Util::BaseFileName(m_nzbName)); + BString<1024> infoName("queue-script %s for %s", script->GetName(), FileSystem::BaseFileName(m_nzbName)); SetInfoName(infoName); SetLogPrefix(script->GetDisplayName()); @@ -308,7 +309,7 @@ void QueueScriptCoordinator::EnqueueScript(NzbInfo* nzbInfo, EEvent event) Tokenizer tok(queueScript, ",;"); while (const char* scriptName = tok.Next()) { - if (Util::SameFilename(scriptName, script->GetName())) + if (FileSystem::SameFilename(scriptName, script->GetName())) { useScript = true; break; @@ -330,7 +331,7 @@ void QueueScriptCoordinator::EnqueueScript(NzbInfo* nzbInfo, EEvent event) { BString<1024> scriptName = varname; scriptName[strlen(scriptName)-1] = '\0'; // remove trailing ':' - if (Util::SameFilename(scriptName, script->GetName())) + if (FileSystem::SameFilename(scriptName, script->GetName())) { useScript = true; break; diff --git a/daemon/extension/ScanScript.cpp b/daemon/extension/ScanScript.cpp index c878d08c..13052b39 100644 --- a/daemon/extension/ScanScript.cpp +++ b/daemon/extension/ScanScript.cpp @@ -28,7 +28,7 @@ #include "Scanner.h" #include "Options.h" #include "Log.h" -#include "Util.h" +#include "FileSystem.h" void ScanScriptController::ExecuteScripts(const char* nzbFilename, const char* url, const char* directory, char** nzbName, char** category, @@ -58,17 +58,17 @@ void ScanScriptController::ExecuteScripts(const char* nzbFilename, void ScanScriptController::ExecuteScript(ScriptConfig::Script* script) { - if (!script->GetScanScript() || !Util::FileExists(m_nzbFilename)) + if (!script->GetScanScript() || !FileSystem::FileExists(m_nzbFilename)) { return; } - PrintMessage(Message::mkInfo, "Executing scan-script %s for %s", script->GetName(), Util::BaseFileName(m_nzbFilename)); + PrintMessage(Message::mkInfo, "Executing scan-script %s for %s", script->GetName(), FileSystem::BaseFileName(m_nzbFilename)); SetScript(script->GetLocation()); SetArgs(NULL, false); - BString<1024> infoName("scan-script %s for %s", script->GetName(), Util::BaseFileName(m_nzbFilename)); + BString<1024> infoName("scan-script %s for %s", script->GetName(), FileSystem::BaseFileName(m_nzbFilename)); SetInfoName(infoName); SetLogPrefix(script->GetDisplayName()); @@ -86,7 +86,7 @@ void ScanScriptController::PrepareParams(const char* scriptName) SetEnvVar("NZBNP_FILENAME", m_nzbFilename); SetEnvVar("NZBNP_URL", m_url); - SetEnvVar("NZBNP_NZBNAME", strlen(*m_nzbName) > 0 ? *m_nzbName : Util::BaseFileName(m_nzbFilename)); + SetEnvVar("NZBNP_NZBNAME", strlen(*m_nzbName) > 0 ? *m_nzbName : FileSystem::BaseFileName(m_nzbFilename)); SetEnvVar("NZBNP_CATEGORY", *m_category); SetIntEnvVar("NZBNP_PRIORITY", *m_priority); SetIntEnvVar("NZBNP_TOP", *m_addTop ? 1 : 0); diff --git a/daemon/extension/SchedulerScript.cpp b/daemon/extension/SchedulerScript.cpp index 82b636fb..d5b185d9 100644 --- a/daemon/extension/SchedulerScript.cpp +++ b/daemon/extension/SchedulerScript.cpp @@ -28,6 +28,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" void SchedulerScriptController::StartScript(const char* param, bool externalProcess, int taskId) { @@ -101,12 +102,12 @@ void SchedulerScriptController::PrepareParams(const char* scriptName) void SchedulerScriptController::ExecuteExternalProcess() { - info("Executing scheduled process-script %s for Task%i", Util::BaseFileName(GetScript()), m_taskId); + info("Executing scheduled process-script %s for Task%i", FileSystem::BaseFileName(GetScript()), m_taskId); - BString<1024> infoName("scheduled process-script %s for Task%i", Util::BaseFileName(GetScript()), m_taskId); + BString<1024> infoName("scheduled process-script %s for Task%i", FileSystem::BaseFileName(GetScript()), m_taskId); SetInfoName(infoName); - BString<1024> logPrefix = Util::BaseFileName(GetScript()); + BString<1024> logPrefix = FileSystem::BaseFileName(GetScript()); if (char* ext = strrchr(logPrefix, '.')) *ext = '\0'; // strip file extension SetLogPrefix(logPrefix); diff --git a/daemon/extension/ScriptConfig.cpp b/daemon/extension/ScriptConfig.cpp index 02a973aa..0b7b6aaa 100644 --- a/daemon/extension/ScriptConfig.cpp +++ b/daemon/extension/ScriptConfig.cpp @@ -25,6 +25,7 @@ #include "nzbget.h" #include "Util.h" +#include "FileSystem.h" #include "Options.h" #include "Log.h" #include "ScriptConfig.h" @@ -123,7 +124,7 @@ bool ScriptConfig::LoadConfig(Options::OptEntries* optEntries) return false; } - int bufLen = (int)Util::FileSize(g_Options->GetConfigFilename()) + 1; + int bufLen = (int)FileSystem::FileSize(g_Options->GetConfigFilename()) + 1; char* buf = (char*)malloc(bufLen); while (fgets(buf, bufLen - 1, infile)) @@ -168,7 +169,7 @@ bool ScriptConfig::SaveConfig(Options::OptEntries* optEntries) std::set writtenOptions; // read config file into memory array - int fileLen = (int)Util::FileSize(g_Options->GetConfigFilename()); + int fileLen = (int)FileSystem::FileSize(g_Options->GetConfigFilename()); CString content; content.Reserve(fileLen); while (fgets(content, fileLen, infile)) @@ -228,7 +229,7 @@ bool ScriptConfig::SaveConfig(Options::OptEntries* optEntries) int pos = (int)ftell(infile); fclose(infile); - Util::TruncateFile(g_Options->GetConfigFilename(), pos); + FileSystem::TruncateFile(g_Options->GetConfigFilename(), pos); return true; } @@ -237,7 +238,7 @@ bool ScriptConfig::LoadConfigTemplates(ConfigTemplates* configTemplates) { char* buffer; int length; - if (!Util::LoadFileIntoBuffer(g_Options->GetConfigTemplate(), &buffer, &length)) + if (!FileSystem::LoadFileIntoBuffer(g_Options->GetConfigTemplate(), &buffer, &length)) { return false; } @@ -376,7 +377,7 @@ void ScriptConfig::LoadScriptDir(Scripts* scripts, const char* directory, bool i { BString<1024> fullFilename("%s%s", directory, filename); - if (!Util::DirectoryExists(fullFilename)) + if (!FileSystem::DirectoryExists(fullFilename)) { // check if the file contains pp-script-signature FILE* infile = fopen(fullFilename, FOPEN_RB); @@ -412,7 +413,7 @@ void ScriptConfig::LoadScriptDir(Scripts* scripts, const char* directory, bool i directory2[len-1] = '\0'; } - scriptName.Format("%s%c%s", Util::BaseFileName(directory2), PATH_SEPARATOR, filename); + scriptName.Format("%s%c%s", FileSystem::BaseFileName(directory2), PATH_SEPARATOR, filename); } else { @@ -474,7 +475,7 @@ void ScriptConfig::BuildScriptDisplayNames(Scripts* scripts) BString<1024> shortName = script->GetName(); if (char* ext = strrchr(shortName, '.')) *ext = '\0'; // strip file extension - const char* displayName = Util::BaseFileName(shortName); + const char* displayName = FileSystem::BaseFileName(shortName); for (Scripts::iterator it2 = scripts->begin(); it2 != scripts->end(); it2++) { @@ -483,7 +484,7 @@ void ScriptConfig::BuildScriptDisplayNames(Scripts* scripts) BString<1024> shortName2 = script2->GetName(); if (char* ext = strrchr(shortName2, '.')) *ext = '\0'; // strip file extension - const char* displayName2 = Util::BaseFileName(shortName2); + const char* displayName2 = FileSystem::BaseFileName(shortName2); if (!strcmp(displayName, displayName2) && script->GetName() != script2->GetName()) { diff --git a/daemon/feed/FeedCoordinator.cpp b/daemon/feed/FeedCoordinator.cpp index 1ed689dc..c22901f1 100644 --- a/daemon/feed/FeedCoordinator.cpp +++ b/daemon/feed/FeedCoordinator.cpp @@ -28,6 +28,7 @@ #include "Options.h" #include "WebDownloader.h" #include "Util.h" +#include "FileSystem.h" #include "FeedFile.h" #include "FeedFilter.h" #include "FeedScript.h" @@ -506,7 +507,7 @@ NzbInfo* FeedCoordinator::CreateNzbInfo(FeedInfo* feedInfo, FeedItemInfo* feedIt if (!nzbName.Empty()) { BString<1024> nzbName2("%s.nzb", *nzbName); - Util::MakeValidFilename(nzbName2, '_', false); + FileSystem::MakeValidFilename(nzbName2, '_', false); nzbInfo->SetFilename(nzbName2); } diff --git a/daemon/main/CommandLineParser.cpp b/daemon/main/CommandLineParser.cpp index edb5219d..e25685c9 100644 --- a/daemon/main/CommandLineParser.cpp +++ b/daemon/main/CommandLineParser.cpp @@ -24,11 +24,12 @@ #include "nzbget.h" -#include "Util.h" +#include "CommandLineParser.h" #include "Log.h" #include "MessageBase.h" #include "DownloadInfo.h" -#include "CommandLineParser.h" +#include "FileSystem.h" +#include "Util.h" #ifdef HAVE_GETOPT_LONG static struct option long_options[] = @@ -846,7 +847,7 @@ void CommandLineParser::PrintUsage(const char* com) " e. g.: \"my nzb download%cmyfile.nfo\" \"another nzb\"\n" " List of regular expressions (options \"FR\", \"GR\")\n" " using POSIX Extended Regular Expression Syntax\n", - Util::BaseFileName(com), + FileSystem::BaseFileName(com), PATH_SEPARATOR); } diff --git a/daemon/main/DiskService.cpp b/daemon/main/DiskService.cpp index 10047f8b..43a655ea 100644 --- a/daemon/main/DiskService.cpp +++ b/daemon/main/DiskService.cpp @@ -29,6 +29,7 @@ #include "StatMeter.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" DiskService::DiskService() { @@ -59,7 +60,7 @@ void DiskService::ServiceWork() void DiskService::CheckDiskSpace() { - int64 freeSpace = Util::FreeDiskSize(g_Options->GetDestDir()); + int64 freeSpace = FileSystem::FreeDiskSize(g_Options->GetDestDir()); if (freeSpace > -1 && freeSpace / 1024 / 1024 < g_Options->GetDiskSpace()) { warn("Low disk space on %s. Pausing download", g_Options->GetDestDir()); @@ -68,7 +69,7 @@ void DiskService::CheckDiskSpace() if (!Util::EmptyStr(g_Options->GetInterDir())) { - freeSpace = Util::FreeDiskSize(g_Options->GetInterDir()); + freeSpace = FileSystem::FreeDiskSize(g_Options->GetInterDir()); if (freeSpace > -1 && freeSpace / 1024 / 1024 < g_Options->GetDiskSpace()) { warn("Low disk space on %s. Pausing download", g_Options->GetInterDir()); @@ -87,7 +88,7 @@ void DiskService::CheckRequiredDir() Tokenizer tok(g_Options->GetRequiredDir(), ",;"); while (const char* dir = tok.Next()) { - if (!Util::FileExists(dir) && !Util::DirectoryExists(dir)) + if (!FileSystem::FileExists(dir) && !FileSystem::DirectoryExists(dir)) { if (!wasWaitingReported) { diff --git a/daemon/main/Maintenance.cpp b/daemon/main/Maintenance.cpp index f2e4688b..92e25bd4 100644 --- a/daemon/main/Maintenance.cpp +++ b/daemon/main/Maintenance.cpp @@ -25,7 +25,7 @@ #include "nzbget.h" #include "Log.h" -#include "Util.h" +#include "FileSystem.h" #include "Maintenance.h" #include "Options.h" #include "CommandLineParser.h" @@ -182,7 +182,7 @@ bool Maintenance::ReadPackageInfoStr(const char* key, char** value) char* packageInfo; int packageInfoLen; - if (!Util::LoadFileIntoBuffer(fileName, &packageInfo, &packageInfoLen)) + if (!FileSystem::LoadFileIntoBuffer(fileName, &packageInfo, &packageInfoLen)) { error("Could not load file %s", *fileName); return false; @@ -252,7 +252,7 @@ void UpdateScriptController::Run() m_prefixLen = 0; PrintMessage(Message::mkInfo, "Executing update-script %s", GetScript()); - BString<1024> infoName("update-script %s", Util::BaseFileName(GetScript())); + BString<1024> infoName("update-script %s", FileSystem::BaseFileName(GetScript())); SetInfoName(infoName); const char* branchName[] = { "STABLE", "TESTING", "DEVEL" }; @@ -274,7 +274,7 @@ void UpdateScriptController::Run() SetEnvVar("NZBUP_PROCESSID", BString<100>("%i", pid)); - BString<100> logPrefix = Util::BaseFileName(GetScript()); + BString<100> logPrefix = FileSystem::BaseFileName(GetScript()); if (char* ext = strrchr(logPrefix, '.')) *ext = '\0'; // strip file extension SetLogPrefix(logPrefix); m_prefixLen = strlen(logPrefix) + 2; // 2 = strlen(": "); @@ -310,15 +310,15 @@ void UpdateScriptController::AddMessage(Message::EKind kind, const char* text) void UpdateInfoScriptController::ExecuteScript(const char* script, char** updateInfo) { - detail("Executing update-info-script %s", Util::BaseFileName(script)); + detail("Executing update-info-script %s", FileSystem::BaseFileName(script)); UpdateInfoScriptController* scriptController = new UpdateInfoScriptController(); scriptController->SetScript(script); - BString<1024> infoName("update-info-script %s", Util::BaseFileName(script)); + BString<1024> infoName("update-info-script %s", FileSystem::BaseFileName(script)); scriptController->SetInfoName(infoName); - BString<1024> logPrefix = Util::BaseFileName(script); + BString<1024> logPrefix = FileSystem::BaseFileName(script); if (char* ext = strrchr(logPrefix, '.')) *ext = '\0'; // strip file extension scriptController->SetLogPrefix(logPrefix); scriptController->m_prefixLen = strlen(logPrefix) + 2; // 2 = strlen(": "); @@ -394,7 +394,7 @@ bool Signature::ComputeInHash() // Read signature from file (m_szSigFilename) into memory bool Signature::ReadSignature() { - BString<1024> sigTitle("\"RSA-SHA256(%s)\" : \"", Util::BaseFileName(m_inFilename)); + BString<1024> sigTitle("\"RSA-SHA256(%s)\" : \"", FileSystem::BaseFileName(m_inFilename)); FILE* infile = fopen(m_sigFilename, FOPEN_RB); if (!infile) @@ -442,7 +442,7 @@ bool Signature::ReadPubKey() { char* keybuf; int keybuflen; - if (!Util::LoadFileIntoBuffer(m_pubKeyFilename, &keybuf, &keybuflen)) + if (!FileSystem::LoadFileIntoBuffer(m_pubKeyFilename, &keybuf, &keybuflen)) { return false; } diff --git a/daemon/main/Options.cpp b/daemon/main/Options.cpp index 9962ece0..189e2aea 100644 --- a/daemon/main/Options.cpp +++ b/daemon/main/Options.cpp @@ -26,6 +26,7 @@ #include "nzbget.h" #include "Util.h" +#include "FileSystem.h" #include "Options.h" #include "Log.h" #include "MessageBase.h" @@ -411,9 +412,9 @@ void Options::Init(const char* exeName, const char* configFilename, bool noConfi } else { - filename = Util::GetExeFileName(exeName); + filename = FileSystem::GetExeFileName(exeName); } - Util::NormalizePathSeparators(filename); + FileSystem::NormalizePathSeparators(filename); SetOption(OPTION_APPBIN, filename); char* end = strrchr(filename, PATH_SEPARATOR); if (end) *end = '\0'; @@ -485,8 +486,8 @@ void Options::ConfigError(const char* msg, ...) tmp2[1024-1] = '\0'; va_end(ap); - printf("%s(%i): %s\n", m_configFilename ? Util::BaseFileName(m_configFilename) : "", m_configLine, tmp2); - error("%s(%i): %s", m_configFilename ? Util::BaseFileName(m_configFilename) : "", m_configLine, tmp2); + printf("%s(%i): %s\n", m_configFilename ? FileSystem::BaseFileName(m_configFilename) : "", m_configLine, tmp2); + error("%s(%i): %s", m_configFilename ? FileSystem::BaseFileName(m_configFilename) : "", m_configLine, tmp2); m_configErrors = true; } @@ -501,8 +502,8 @@ void Options::ConfigWarn(const char* msg, ...) tmp2[1024-1] = '\0'; va_end(ap); - printf("%s(%i): %s\n", Util::BaseFileName(m_configFilename), m_configLine, tmp2); - warn("%s(%i): %s", Util::BaseFileName(m_configFilename), m_configLine, tmp2); + printf("%s(%i): %s\n", FileSystem::BaseFileName(m_configFilename), m_configLine, tmp2); + warn("%s(%i): %s", FileSystem::BaseFileName(m_configFilename), m_configLine, tmp2); } void Options::LocateOptionSrcPos(const char *optionName) @@ -639,19 +640,19 @@ void Options::InitOptFile() #ifdef WIN32 BString<1024> filename("%s\\nzbget.conf", *m_appDir); - if (!Util::FileExists(filename)) + if (!FileSystem::FileExists(filename)) { char appDataPath[MAX_PATH]; SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, appDataPath); filename.Format("%s\\NZBGet\\nzbget.conf", appDataPath); - if (m_extender && !Util::FileExists(filename)) + if (m_extender && !FileSystem::FileExists(filename)) { m_extender->SetupFirstStart(); } } - if (Util::FileExists(filename)) + if (FileSystem::FileExists(filename)) { m_configFilename = filename; } @@ -659,7 +660,7 @@ void Options::InitOptFile() // look in the exe-directory first BString<1024> filename("%s/nzbget.conf", *m_appDir); - if (Util::FileExists(filename)) + if (FileSystem::FileExists(filename)) { m_configFilename = filename; } @@ -669,9 +670,9 @@ void Options::InitOptFile() while (const char* altfilename = PossibleConfigLocations[p++]) { // substitute HOME-variable - filename = Util::ExpandHomePath(altfilename); + filename = FileSystem::ExpandHomePath(altfilename); - if (Util::FileExists(filename)) + if (FileSystem::FileExists(filename)) { m_configFilename = *filename; break; @@ -684,11 +685,11 @@ void Options::InitOptFile() if (m_configFilename) { // normalize path in filename - CString filename = Util::ExpandFileName(m_configFilename); + CString filename = FileSystem::ExpandFileName(m_configFilename); #ifndef WIN32 // substitute HOME-variable - filename = Util::ExpandHomePath(filename); + filename = FileSystem::ExpandHomePath(filename); #endif m_configFilename = *filename; @@ -720,7 +721,7 @@ void Options::CheckDir(CString& dir, const char* optionName, } dir = tempdir; - Util::NormalizePathSeparators((char*)dir); + FileSystem::NormalizePathSeparators((char*)dir); if (dir[dir.Length() - 1] != PATH_SEPARATOR) { dir.AppendFmt("%c", (int)PATH_SEPARATOR); @@ -743,7 +744,7 @@ void Options::CheckDir(CString& dir, const char* optionName, usedir2.Format("%s%c%s", parentDir, PATH_SEPARATOR, *dir); } - Util::NormalizePathSeparators((char*)usedir2); + FileSystem::NormalizePathSeparators((char*)usedir2); dir = usedir2; usedir2[usedir2.Length() - 1] = '\0'; @@ -752,7 +753,7 @@ void Options::CheckDir(CString& dir, const char* optionName, // Ensure the dir is created CString errmsg; - if (create && !Util::ForceDirectories(dir, errmsg)) + if (create && !FileSystem::ForceDirectories(dir, errmsg)) { ConfigError("Invalid value for option \"%s\" (%s): %s", optionName, *dir, *errmsg); } @@ -981,7 +982,7 @@ void Options::SetOption(const char* optname, const char* value) } else { - curvalue = Util::ExpandHomePath(value); + curvalue = FileSystem::ExpandHomePath(value); } } else @@ -1477,7 +1478,7 @@ void Options::LoadConfigFile() } m_configLine = 0; - int bufLen = (int)Util::FileSize(m_configFilename) + 1; + int bufLen = (int)FileSystem::FileSize(m_configFilename) + 1; char* buf = (char*)malloc(bufLen); int line = 0; @@ -1794,7 +1795,7 @@ void Options::CheckOptions() if (m_configTemplate.Empty() && !m_noDiskAccess) { m_configTemplate.Format("%s%s", *m_webDir, "nzbget.conf"); - if (!Util::FileExists(m_configTemplate)) + if (!FileSystem::FileExists(m_configTemplate)) { m_configTemplate = ""; } @@ -1822,7 +1823,7 @@ void Options::CheckOptions() m_parBuffer = 400; } - if (!m_unpackPassFile.Empty() && !Util::FileExists(m_unpackPassFile)) + if (!m_unpackPassFile.Empty() && !FileSystem::FileExists(m_unpackPassFile)) { ConfigError("Invalid value for option \"UnpackPassFile\": %s. File not found", *m_unpackPassFile); } diff --git a/daemon/main/nzbget.cpp b/daemon/main/nzbget.cpp index 042d68bc..87a6e7b2 100644 --- a/daemon/main/nzbget.cpp +++ b/daemon/main/nzbget.cpp @@ -55,6 +55,7 @@ #include "StatMeter.h" #include "QueueScript.h" #include "Util.h" +#include "FileSystem.h" #include "StackTrace.h" #ifdef WIN32 #include "WinService.h" @@ -178,13 +179,13 @@ void RunMain() // the program is reloaded (RPC-Method "reload") in order for // config to properly load in a case relative paths are used // in command line - CString curDir = Util::GetCurrentDirectory(); + CString curDir = FileSystem::GetCurrentDirectory(); bool reload = false; while (g_Reloading) { g_Reloading = false; - Util::SetCurrentDirectory(curDir); + FileSystem::SetCurrentDirectory(curDir); Run(reload); reload = true; } diff --git a/daemon/nntp/ArticleWriter.cpp b/daemon/nntp/ArticleWriter.cpp index f3d1ccc1..64bbf566 100644 --- a/daemon/nntp/ArticleWriter.cpp +++ b/daemon/nntp/ArticleWriter.cpp @@ -29,6 +29,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" ArticleWriter::ArticleWriter() { @@ -97,7 +98,7 @@ bool ArticleWriter::Start(Decoder::EFormat format, const char* filename, int64 f } m_fileInfo->UnlockOutputFile(); if (!outputInitialized && filename && - Util::FileExists(m_fileInfo->GetNzbInfo()->GetDestDir(), filename)) + FileSystem::FileExists(m_fileInfo->GetNzbInfo()->GetDestDir(), filename)) { m_duplicate = true; return false; @@ -153,7 +154,7 @@ bool ArticleWriter::Start(Decoder::EFormat format, const char* filename, int64 f { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not %s file %s: %s", directWrite ? "open" : "create", filename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); return false; } SetWriteBuffer(m_outFile, m_articleInfo->GetSize()); @@ -209,11 +210,11 @@ void ArticleWriter::Finish(bool success) { if (!directWrite && !m_articleData) { - if (!Util::MoveFile(m_tempFilename, m_resultFilename)) + if (!FileSystem::MoveFile(m_tempFilename, m_resultFilename)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not rename file %s to %s: %s", *m_tempFilename, m_resultFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } } @@ -240,11 +241,11 @@ void ArticleWriter::Finish(bool success) else { // rawmode - if (!Util::MoveFile(m_tempFilename, m_resultFilename)) + if (!FileSystem::MoveFile(m_tempFilename, m_resultFilename)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not move file %s to %s: %s", *m_tempFilename, m_resultFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } } } @@ -252,8 +253,8 @@ void ArticleWriter::Finish(bool success) /* creates output file and subdirectores */ bool ArticleWriter::CreateOutputFile(int64 size) { - if (g_Options->GetDirectWrite() && Util::FileExists(m_outputFilename) && - Util::FileSize(m_outputFilename) == size) + if (g_Options->GetDirectWrite() && FileSystem::FileExists(m_outputFilename) && + FileSystem::FileSize(m_outputFilename) == size) { // keep existing old file from previous program session return true; @@ -264,17 +265,17 @@ bool ArticleWriter::CreateOutputFile(int64 size) // ensure the directory exist BString<1024> destDir; - destDir.Set(m_outputFilename, Util::BaseFileName(m_outputFilename) - m_outputFilename); + destDir.Set(m_outputFilename, FileSystem::BaseFileName(m_outputFilename) - m_outputFilename); CString errmsg; - if (!Util::ForceDirectories(destDir, errmsg)) + if (!FileSystem::ForceDirectories(destDir, errmsg)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not create directory %s: %s", *destDir, *errmsg); return false; } - if (!Util::CreateSparseFile(m_outputFilename, size, errmsg)) + if (!FileSystem::CreateSparseFile(m_outputFilename, size, errmsg)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not create file %s: %s", *m_outputFilename, *errmsg); @@ -352,14 +353,14 @@ void ArticleWriter::CompleteFileParts() } // Ensure the DstDir is created - if (!Util::ForceDirectories(nzbDestDir, errmsg)) + if (!FileSystem::ForceDirectories(nzbDestDir, errmsg)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not create directory %s: %s", *nzbDestDir, *errmsg); return; } - CString ofn = Util::MakeUniqueFilename(nzbDestDir, m_fileInfo->GetFilename()); + CString ofn = FileSystem::MakeUniqueFilename(nzbDestDir, m_fileInfo->GetFilename()); FILE* outfile = NULL; BString<1024> tmpdestfile("%s.tmp", *ofn); @@ -371,7 +372,7 @@ void ArticleWriter::CompleteFileParts() if (!outfile) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, - "Could not create file %s: %s", *tmpdestfile, *Util::GetLastErrorMessage()); + "Could not create file %s: %s", *tmpdestfile, *FileSystem::GetLastErrorMessage()); return; } } @@ -381,7 +382,7 @@ void ArticleWriter::CompleteFileParts() if (!outfile) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, - "Could not open file %s: %s", *m_outputFilename, *Util::GetLastErrorMessage()); + "Could not open file %s: %s", *m_outputFilename, *FileSystem::GetLastErrorMessage()); return; } tmpdestfile = *m_outputFilename; @@ -389,10 +390,10 @@ void ArticleWriter::CompleteFileParts() else if (!g_Options->GetDecode()) { remove(tmpdestfile); - if (!Util::CreateDirectory(ofn)) + if (!FileSystem::CreateDirectory(ofn)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, - "Could not create directory %s: %s", *ofn, *Util::GetLastErrorMessage()); + "Could not create directory %s: %s", *ofn, *FileSystem::GetLastErrorMessage()); return; } } @@ -468,11 +469,11 @@ void ArticleWriter::CompleteFileParts() else if (!g_Options->GetDecode()) { BString<1024> dstFileName("%s%c%03i", *ofn, (int)PATH_SEPARATOR, pa->GetPartNumber()); - if (!Util::MoveFile(pa->GetResultFilename(), dstFileName)) + if (!FileSystem::MoveFile(pa->GetResultFilename(), dstFileName)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not move file %s to %s: %s", pa->GetResultFilename(), - *dstFileName, *Util::GetLastErrorMessage()); + *dstFileName, *FileSystem::GetLastErrorMessage()); } } @@ -494,21 +495,21 @@ void ArticleWriter::CompleteFileParts() if (outfile) { fclose(outfile); - if (!directWrite && !Util::MoveFile(tmpdestfile, ofn)) + if (!directWrite && !FileSystem::MoveFile(tmpdestfile, ofn)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not move file %s to %s: %s", *tmpdestfile, *ofn, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } } if (directWrite) { - if (!Util::MoveFile(m_outputFilename, ofn)) + if (!FileSystem::MoveFile(m_outputFilename, ofn)) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not move file %s to %s: %s", *m_outputFilename, *ofn, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } // if destination directory was changed delete the old directory (if empty) @@ -518,8 +519,8 @@ void ArticleWriter::CompleteFileParts() { debug("Checking old dir for: %s", *m_outputFilename); BString<1024> oldDestDir; - oldDestDir.Set(m_outputFilename, Util::BaseFileName(m_outputFilename) - m_outputFilename); - if (Util::DirEmpty(oldDestDir)) + oldDestDir.Set(m_outputFilename, FileSystem::BaseFileName(m_outputFilename) - m_outputFilename); + if (FileSystem::DirEmpty(oldDestDir)) { debug("Deleting old dir: %s", *oldDestDir); rmdir(oldDestDir); @@ -573,7 +574,7 @@ void ArticleWriter::CompleteFileParts() // the locking is needed for accessing the members of NZBInfo DownloadQueue::Lock(); m_fileInfo->GetNzbInfo()->GetCompletedFiles()->push_back(new CompletedFile( - m_fileInfo->GetId(), Util::BaseFileName(ofn), fileStatus, crc)); + m_fileInfo->GetId(), FileSystem::BaseFileName(ofn), fileStatus, crc)); if (strcmp(m_fileInfo->GetNzbInfo()->GetDestDir(), nzbDestDir)) { // destination directory was changed during completion, need to move the file @@ -625,7 +626,7 @@ void ArticleWriter::FlushCache() { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not open file %s: %s", m_fileInfo->GetOutputFilename(), - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); break; } needBufFile = true; @@ -641,7 +642,7 @@ void ArticleWriter::FlushCache() { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not create file %s: %s", *destFile, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); break; } needBufFile = true; @@ -670,11 +671,11 @@ void ArticleWriter::FlushCache() fclose(outfile); outfile = NULL; - if (!Util::MoveFile(destFile, pa->GetResultFilename())) + if (!FileSystem::MoveFile(destFile, pa->GetResultFilename())) { m_fileInfo->GetNzbInfo()->PrintMessage(Message::mkError, "Could not rename file %s to %s: %s", *destFile, pa->GetResultFilename(), - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } } } @@ -703,7 +704,7 @@ bool ArticleWriter::MoveCompletedFiles(NzbInfo* nzbInfo, const char* oldDestDir) // Ensure the DstDir is created CString errmsg; - if (!Util::ForceDirectories(nzbInfo->GetDestDir(), errmsg)) + if (!FileSystem::ForceDirectories(nzbInfo->GetDestDir(), errmsg)) { nzbInfo->PrintMessage(Message::mkError, "Could not create directory %s: %s", nzbInfo->GetDestDir(), *errmsg); return false; @@ -721,13 +722,13 @@ bool ArticleWriter::MoveCompletedFiles(NzbInfo* nzbInfo, const char* oldDestDir) if (strcmp(oldFileName, newFileName)) { // prevent overwriting of existing files - newFileName = Util::MakeUniqueFilename(nzbInfo->GetDestDir(), completedFile->GetFileName()); + newFileName = FileSystem::MakeUniqueFilename(nzbInfo->GetDestDir(), completedFile->GetFileName()); detail("Moving file %s to %s", *oldFileName, *newFileName); - if (!Util::MoveFile(oldFileName, newFileName)) + if (!FileSystem::MoveFile(oldFileName, newFileName)) { nzbInfo->PrintMessage(Message::mkError, "Could not move file %s to %s: %s", - *oldFileName, *newFileName, *Util::GetLastErrorMessage()); + *oldFileName, *newFileName, *FileSystem::GetLastErrorMessage()); } } } @@ -736,12 +737,12 @@ bool ArticleWriter::MoveCompletedFiles(NzbInfo* nzbInfo, const char* oldDestDir) if (g_Options->GetBrokenLog()) { BString<1024> oldBrokenLogName("%s%c_brokenlog.txt", oldDestDir, (int)PATH_SEPARATOR); - if (Util::FileExists(oldBrokenLogName)) + if (FileSystem::FileExists(oldBrokenLogName)) { BString<1024> brokenLogName("%s%c_brokenlog.txt", nzbInfo->GetDestDir(), (int)PATH_SEPARATOR); detail("Moving file %s to %s", *oldBrokenLogName, *brokenLogName); - if (Util::FileExists(brokenLogName)) + if (FileSystem::FileExists(brokenLogName)) { // copy content to existing new file, then delete old file FILE* outfile; @@ -778,17 +779,17 @@ bool ArticleWriter::MoveCompletedFiles(NzbInfo* nzbInfo, const char* oldDestDir) else { // move to new destination - if (!Util::MoveFile(oldBrokenLogName, brokenLogName)) + if (!FileSystem::MoveFile(oldBrokenLogName, brokenLogName)) { nzbInfo->PrintMessage(Message::mkError, "Could not move file %s to %s: %s", - *oldBrokenLogName, *brokenLogName, *Util::GetLastErrorMessage()); + *oldBrokenLogName, *brokenLogName, *FileSystem::GetLastErrorMessage()); } } } } // delete old directory (if empty) - if (Util::DirEmpty(oldDestDir)) + if (FileSystem::DirEmpty(oldDestDir)) { // check if there are pending writes into directory bool pendingWrites = false; diff --git a/daemon/postprocess/Cleanup.cpp b/daemon/postprocess/Cleanup.cpp index e9472683..37eb165f 100644 --- a/daemon/postprocess/Cleanup.cpp +++ b/daemon/postprocess/Cleanup.cpp @@ -27,6 +27,7 @@ #include "Cleanup.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" #include "ParParser.h" #include "Options.h" @@ -84,7 +85,7 @@ void MoveController::Run() bool MoveController::MoveFiles() { CString errmsg; - if (!Util::ForceDirectories(m_destDir, errmsg)) + if (!FileSystem::ForceDirectories(m_destDir, errmsg)) { PrintMessage(Message::mkError, "Could not create directory %s: %s", *m_destDir, *errmsg); return false; @@ -95,23 +96,23 @@ bool MoveController::MoveFiles() while (const char* filename = dir.Next()) { BString<1024> srcFile("%s%c%s",* m_interDir, PATH_SEPARATOR, filename); - CString dstFile = Util::MakeUniqueFilename(m_destDir, filename); + CString dstFile = FileSystem::MakeUniqueFilename(m_destDir, filename); bool hiddenFile = filename[0] == '.'; if (!hiddenFile) { - PrintMessage(Message::mkInfo, "Moving file %s to %s", Util::BaseFileName(srcFile), *m_destDir); + PrintMessage(Message::mkInfo, "Moving file %s to %s", FileSystem::BaseFileName(srcFile), *m_destDir); } - if (!Util::MoveFile(srcFile, dstFile) && !hiddenFile) + if (!FileSystem::MoveFile(srcFile, dstFile) && !hiddenFile) { PrintMessage(Message::mkError, "Could not move file %s to %s: %s", - *srcFile, *dstFile, *Util::GetLastErrorMessage()); + *srcFile, *dstFile, *FileSystem::GetLastErrorMessage()); ok = false; } } - if (ok && !Util::DeleteDirectoryWithContent(m_interDir, errmsg)) + if (ok && !FileSystem::DeleteDirectoryWithContent(m_interDir, errmsg)) { PrintMessage(Message::mkWarning, "Could not delete intermediate directory %s: %s", *m_interDir, *errmsg); } @@ -205,7 +206,7 @@ bool CleanupController::Cleanup(const char* destDir, bool *deleted) { BString<1024> fullFilename("%s%c%s", destDir, PATH_SEPARATOR, filename); - bool isDir = Util::DirectoryExists(fullFilename); + bool isDir = FileSystem::DirectoryExists(fullFilename); if (isDir) { @@ -221,7 +222,7 @@ bool CleanupController::Cleanup(const char* destDir, bool *deleted) if (remove(fullFilename) != 0) { PrintMessage(Message::mkError, "Could not delete file %s: %s", *fullFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); ok = false; } diff --git a/daemon/postprocess/DupeMatcher.cpp b/daemon/postprocess/DupeMatcher.cpp index d913be5e..62c4afbd 100644 --- a/daemon/postprocess/DupeMatcher.cpp +++ b/daemon/postprocess/DupeMatcher.cpp @@ -27,6 +27,7 @@ #include "DupeMatcher.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" #include "Options.h" #include "Script.h" #include "Thread.h" @@ -214,7 +215,7 @@ void DupeMatcher::FindLargestFile(const char* directory, char* filenameBuf, int { BString<1024> fullFilename("%s%c%s", directory, PATH_SEPARATOR, filename); - int64 fileSize = Util::FileSize(fullFilename); + int64 fileSize = FileSystem::FileSize(fullFilename); if (fileSize > *maxSize) { *maxSize = fileSize; diff --git a/daemon/postprocess/ParChecker.cpp b/daemon/postprocess/ParChecker.cpp index 743299a1..a6bbb5bd 100644 --- a/daemon/postprocess/ParChecker.cpp +++ b/daemon/postprocess/ParChecker.cpp @@ -36,6 +36,7 @@ #include "Log.h" #include "Options.h" #include "Util.h" +#include "FileSystem.h" const char* Par2CmdLineErrStr[] = { "OK", "data files are damaged and there is enough recovery data available to repair them", @@ -115,7 +116,7 @@ Result Repairer::PreProcess(const char *parFilename) if (g_Options->GetParScan() == Options::psFull) { BString<1024> wildcardParam(parFilename, 1024); - char* basename = Util::BaseFileName(wildcardParam); + char* basename = FileSystem::BaseFileName(wildcardParam); if (basename != wildcardParam && strlen(basename) > 0) { basename[0] = '*'; @@ -340,10 +341,10 @@ public: */ bool MissingFilesComparator::operator()(CommandLine::ExtraFile* file1, CommandLine::ExtraFile* file2) const { - BString<1024> name1 = Util::BaseFileName(file1->FileName().c_str()); + BString<1024> name1 = FileSystem::BaseFileName(file1->FileName().c_str()); if (char* ext = strrchr(name1, '.')) *ext = '\0'; // trim extension - BString<1024> name2 = Util::BaseFileName(file2->FileName().c_str()); + BString<1024> name2 = FileSystem::BaseFileName(file2->FileName().c_str()); if (char* ext = strrchr(name2, '.')) *ext = '\0'; // trim extension return strcmp(name1, m_baseParFilename) == 0 && strcmp(name1, name2) != 0; @@ -819,11 +820,11 @@ bool ParChecker::LoadMorePars() bool loadedOK = ((Repairer*)m_repairer)->LoadPacketsFromFile(parFilename); if (loadedOK) { - PrintMessage(Message::mkInfo, "File %s successfully loaded for par-check", Util::BaseFileName(parFilename)); + PrintMessage(Message::mkInfo, "File %s successfully loaded for par-check", FileSystem::BaseFileName(parFilename)); } else { - PrintMessage(Message::mkInfo, "Could not load file %s for par-check", Util::BaseFileName(parFilename)); + PrintMessage(Message::mkInfo, "Could not load file %s for par-check", FileSystem::BaseFileName(parFilename)); } } @@ -861,7 +862,7 @@ bool ParChecker::AddSplittedFragments() std::string target = sourcefile->TargetFileName(); const char* filename2 = target.c_str(); - const char* basename2 = Util::BaseFileName(filename2); + const char* basename2 = FileSystem::BaseFileName(filename2); int baseLen = strlen(basename2); if (!strncasecmp(filename, basename2, baseLen)) @@ -874,7 +875,7 @@ bool ParChecker::AddSplittedFragments() { debug("Found splitted fragment %s", filename); BString<1024> fullfilename("%s%c%s", *m_destDir, PATH_SEPARATOR, filename); - CommandLine::ExtraFile extrafile(*fullfilename, Util::FileSize(fullfilename)); + CommandLine::ExtraFile extrafile(*fullfilename, FileSystem::FileSize(fullfilename)); extrafiles.push_back(extrafile); } } @@ -921,7 +922,7 @@ bool ParChecker::AddDupeFiles() for (DupeSourceList::iterator it = m_dupeSources.begin(); it != m_dupeSources.end(); it++) { DupeSource* dupeSource = *it; - if (((Repairer*)m_repairer)->missingblockcount > 0 && Util::DirectoryExists(dupeSource->GetDirectory())) + if (((Repairer*)m_repairer)->missingblockcount > 0 && FileSystem::DirectoryExists(dupeSource->GetDirectory())) { int wasBlocksMissing2 = ((Repairer*)m_repairer)->missingblockcount; bool oneAdded = AddExtraFiles(false, true, dupeSource->GetDirectory()); @@ -950,7 +951,7 @@ bool ParChecker::AddExtraFiles(bool onlyMissing, bool externalDir, const char* d { if (externalDir) { - PrintMessage(Message::mkInfo, "Performing dupe par-scan for %s in %s", *m_infoName, Util::BaseFileName(directory)); + PrintMessage(Message::mkInfo, "Performing dupe par-scan for %s in %s", *m_infoName, FileSystem::BaseFileName(directory)); } else { @@ -966,12 +967,12 @@ bool ParChecker::AddExtraFiles(bool onlyMissing, bool externalDir, const char* d (externalDir || (!IsParredFile(filename) && !IsProcessedFile(filename)))) { BString<1024> fullfilename("%s%c%s", directory, PATH_SEPARATOR, filename); - extrafiles.push_back(new CommandLine::ExtraFile(*fullfilename, Util::FileSize(fullfilename))); + extrafiles.push_back(new CommandLine::ExtraFile(*fullfilename, FileSystem::FileSize(fullfilename))); } } // Sort the list - char* baseParFilename = strdup(Util::BaseFileName(m_parFilename)); + char* baseParFilename = strdup(FileSystem::BaseFileName(m_parFilename)); if (char* ext = strrchr(baseParFilename, '.')) *ext = '\0'; // trim extension extrafiles.sort(MissingFilesComparator(baseParFilename)); free(baseParFilename); @@ -1006,8 +1007,8 @@ bool ParChecker::AddExtraFiles(bool onlyMissing, bool externalDir, const char* d if (fileAdded && !externalDir) { - PrintMessage(Message::mkInfo, "Found missing file %s", Util::BaseFileName(extraFile->FileName().c_str())); - RegisterParredFile(Util::BaseFileName(extraFile->FileName().c_str())); + PrintMessage(Message::mkInfo, "Found missing file %s", FileSystem::BaseFileName(extraFile->FileName().c_str())); + RegisterParredFile(FileSystem::BaseFileName(extraFile->FileName().c_str())); } else if (blockAdded) { @@ -1048,7 +1049,7 @@ bool ParChecker::IsProcessedFile(const char* filename) for (FileList::iterator it = m_processedFiles.begin(); it != m_processedFiles.end(); it++) { const char* processedFilename = *it; - if (!strcasecmp(Util::BaseFileName(processedFilename), filename)) + if (!strcasecmp(FileSystem::BaseFileName(processedFilename), filename)) { return true; } @@ -1161,7 +1162,7 @@ void ParChecker::signal_done(std::string str, int available, int total) it != ((Repairer*)m_repairer)->sourcefiles.end(); it++) { Par2RepairerSourceFile *sourcefile = *it; - if (sourcefile && !strcmp(filename, Util::BaseFileName(sourcefile->TargetFileName().c_str())) && + if (sourcefile && !strcmp(filename, FileSystem::BaseFileName(sourcefile->TargetFileName().c_str())) && !sourcefile->GetTargetExists()) { fileExists = false; @@ -1238,7 +1239,7 @@ void ParChecker::WriteBrokenLog(EStatus status) { BString<1024> brokenLogName("%s%c_brokenlog.txt", *m_destDir, (int)PATH_SEPARATOR); - if (status != psRepairNotNeeded || Util::FileExists(brokenLogName)) + if (status != psRepairNotNeeded || FileSystem::FileExists(brokenLogName)) { FILE* file = fopen(brokenLogName, FOPEN_AB); if (file) @@ -1321,7 +1322,7 @@ void ParChecker::DeleteLeftovers() if (!found) { - PrintMessage(Message::mkInfo, "Deleting file %s", Util::BaseFileName(sourceFile->FileName().c_str())); + PrintMessage(Message::mkInfo, "Deleting file %s", FileSystem::BaseFileName(sourceFile->FileName().c_str())); remove(sourceFile->FileName().c_str()); } } @@ -1368,7 +1369,7 @@ ParChecker::EFileStatus ParChecker::VerifyDataFile(void* diskfile, void* sourcef std::string filenameObj = sourceFile->GetTargetFile()->FileName(); const char* filename = filenameObj.c_str(); - if (Util::FileSize(filename) == 0 && sourceFile->BlockCount() > 0) + if (FileSystem::FileSize(filename) == 0 && sourceFile->BlockCount() > 0) { *availableBlocks = 0; return fsFailure; @@ -1377,7 +1378,7 @@ ParChecker::EFileStatus ParChecker::VerifyDataFile(void* diskfile, void* sourcef // find file status and CRC computed during download uint32 downloadCrc; SegmentList segments; - EFileStatus fileStatus = FindFileCrc(Util::BaseFileName(filename), &downloadCrc, &segments); + EFileStatus fileStatus = FindFileCrc(FileSystem::BaseFileName(filename), &downloadCrc, &segments); ValidBlocks validBlocks; if (fileStatus == fsFailure || fileStatus == fsUnknown) @@ -1388,7 +1389,7 @@ ParChecker::EFileStatus ParChecker::VerifyDataFile(void* diskfile, void* sourcef (fileStatus == fsPartial && !VerifyPartialDataFile(diskfile, sourcefile, &segments, &validBlocks))) { PrintMessage(Message::mkWarning, "Quick verification failed for %s file %s, performing full verification instead", - fileStatus == fsSuccess ? "good" : "damaged", Util::BaseFileName(filename)); + fileStatus == fsSuccess ? "good" : "damaged", FileSystem::BaseFileName(filename)); return fsUnknown; // let libpar2 do the full verification of the file } @@ -1424,7 +1425,7 @@ ParChecker::EFileStatus ParChecker::VerifyDataFile(void* diskfile, void* sourcef m_quickFiles++; PrintMessage(Message::mkDetail, "Quickly verified %s file %s", - fileStatus == fsSuccess ? "good" : "damaged", Util::BaseFileName(filename)); + fileStatus == fsSuccess ? "good" : "damaged", FileSystem::BaseFileName(filename)); return fileStatus; } @@ -1450,7 +1451,7 @@ bool ParChecker::VerifySuccessDataFile(void* diskfile, void* sourcefile, uint32 u32 blockCrc = entry->crc; parCrc = i == 0 ? blockCrc : Util::Crc32Combine(parCrc, blockCrc, (uint32)blocksize); } - debug("Block-CRC: %x, filename: %s", parCrc, Util::BaseFileName(sourceFile->GetTargetFile()->FileName().c_str())); + debug("Block-CRC: %x, filename: %s", parCrc, FileSystem::BaseFileName(sourceFile->GetTargetFile()->FileName().c_str())); return parCrc == downloadCrc; } @@ -1504,7 +1505,7 @@ bool ParChecker::VerifyPartialDataFile(void* diskfile, void* sourcefile, Segment if (!infile) { PrintMessage(Message::mkError, "Could not open file %s: %s", - filename, *Util::GetLastErrorMessage()); + filename, *FileSystem::GetLastErrorMessage()); } // For each sequential range of presumably valid blocks: diff --git a/daemon/postprocess/ParCoordinator.cpp b/daemon/postprocess/ParCoordinator.cpp index 4afe6c68..faf09dc0 100644 --- a/daemon/postprocess/ParCoordinator.cpp +++ b/daemon/postprocess/ParCoordinator.cpp @@ -30,7 +30,7 @@ #include "Options.h" #include "DiskState.h" #include "Log.h" -#include "Util.h" +#include "FileSystem.h" #ifndef DISABLE_PARCHECK bool ParCoordinator::PostParChecker::RequestMorePars(int blockNeeded, int* blockFound) @@ -92,7 +92,7 @@ ParChecker::EFileStatus ParCoordinator::PostParChecker::FindFileCrc(const char* return ParChecker::fsUnknown; } - debug("Found completed file: %s, CRC: %.8x, Status: %i", Util::BaseFileName(completedFile->GetFileName()), completedFile->GetCrc(), (int)completedFile->GetStatus()); + debug("Found completed file: %s, CRC: %.8x, Status: %i", FileSystem::BaseFileName(completedFile->GetFileName()), completedFile->GetCrc(), (int)completedFile->GetStatus()); *crc = completedFile->GetCrc(); @@ -143,12 +143,12 @@ void ParCoordinator::PostParChecker::RequestDupeSources(DupeSourceList* dupeSour NzbInfo* dupeNzbInfo = *it; if (sizeComparisonPossible) { - PrintMessage(Message::mkInfo, "Checking %s for dupe scan usability", Util::BaseFileName(dupeNzbInfo->GetDestDir())); + PrintMessage(Message::mkInfo, "Checking %s for dupe scan usability", FileSystem::BaseFileName(dupeNzbInfo->GetDestDir())); } bool useDupe = !sizeComparisonPossible || dupeMatcher.MatchDupeContent(dupeNzbInfo->GetDestDir()); if (useDupe) { - PrintMessage(Message::mkInfo, "Adding %s to dupe scan sources", Util::BaseFileName(dupeNzbInfo->GetDestDir())); + PrintMessage(Message::mkInfo, "Adding %s to dupe scan sources", FileSystem::BaseFileName(dupeNzbInfo->GetDestDir())); dupeSourceList->push_back(new ParChecker::DupeSource(dupeNzbInfo->GetId(), dupeNzbInfo->GetDestDir())); } } @@ -533,7 +533,7 @@ void ParCoordinator::FindPars(DownloadQueue* downloadQueue, NzbInfo* nzbInfo, co *blockFound = 0; // extract base name from m_szParFilename (trim .par2-extension and possible .vol-part) - char* baseParFilename = Util::BaseFileName(parFilename); + char* baseParFilename = FileSystem::BaseFileName(parFilename); int mainBaseLen = 0; if (!ParParser::ParseParFilename(baseParFilename, &mainBaseLen, NULL)) { @@ -556,7 +556,7 @@ void ParCoordinator::FindPars(DownloadQueue* downloadQueue, NzbInfo* nzbInfo, co if (exactParName) { - useFile = ParParser::SameParCollection(fileInfo->GetFilename(), Util::BaseFileName(parFilename)); + useFile = ParParser::SameParCollection(fileInfo->GetFilename(), FileSystem::BaseFileName(parFilename)); } else if (strictParName) { diff --git a/daemon/postprocess/ParParser.cpp b/daemon/postprocess/ParParser.cpp index 93d64b5c..ff9c9829 100644 --- a/daemon/postprocess/ParParser.cpp +++ b/daemon/postprocess/ParParser.cpp @@ -24,7 +24,7 @@ #include "nzbget.h" -#include "Util.h" +#include "FileSystem.h" #include "ParParser.h" bool ParParser::FindMainPars(const char* path, ParFileList* fileList) diff --git a/daemon/postprocess/ParRenamer.cpp b/daemon/postprocess/ParRenamer.cpp index bdfba727..a3ebe2d2 100644 --- a/daemon/postprocess/ParRenamer.cpp +++ b/daemon/postprocess/ParRenamer.cpp @@ -36,6 +36,7 @@ #include "Log.h" #include "Options.h" #include "Util.h" +#include "FileSystem.h" class ParRenamerRepairer : public Par2Repairer { @@ -155,7 +156,7 @@ void ParRenamer::BuildDirList(const char* destDir) if (!m_cancelled) { BString<1024> fullFilename("%s%c%s", destDir, PATH_SEPARATOR, filename); - if (Util::DirectoryExists(fullFilename)) + if (FileSystem::DirectoryExists(fullFilename)) { BuildDirList(fullFilename); } @@ -225,7 +226,7 @@ void ParRenamer::CheckFiles(const char* destDir, bool renamePars) { BString<1024> fullFilename("%s%c%s", destDir, PATH_SEPARATOR, filename); - if (!Util::DirectoryExists(fullFilename)) + if (!FileSystem::DirectoryExists(fullFilename)) { m_progressLabel.Format("Checking file %s", filename); m_stageProgress = m_curFile * 1000 / m_fileCount; @@ -269,7 +270,7 @@ void ParRenamer::CheckMissing() bool ParRenamer::IsSplittedFragment(const char* filename, const char* correctName) { bool splittedFragement = false; - const char* diskBasename = Util::BaseFileName(filename); + const char* diskBasename = FileSystem::BaseFileName(filename); const char* extension = strrchr(diskBasename, '.'); int baseLen = strlen(correctName); if (extension && !strncasecmp(diskBasename, correctName, baseLen)) @@ -320,7 +321,7 @@ void ParRenamer::CheckRegularFile(const char* destDir, const char* filename) free(buffer); - debug("file: %s; hash16k: %s", Util::BaseFileName(filename), hash16k.print().c_str()); + debug("file: %s; hash16k: %s", FileSystem::BaseFileName(filename), hash16k.print().c_str()); for (FileHashList::iterator it = m_fileHashList.begin(); it != m_fileHashList.end(); it++) { @@ -332,7 +333,7 @@ void ParRenamer::CheckRegularFile(const char* destDir, const char* filename) BString<1024> dstFilename("%s%c%s", destDir, PATH_SEPARATOR, fileHash->GetFilename()); - if (!Util::FileExists(dstFilename) && !IsSplittedFragment(filename, fileHash->GetFilename())) + if (!FileSystem::FileExists(dstFilename) && !IsSplittedFragment(filename, fileHash->GetFilename())) { RenameFile(filename, dstFilename); } @@ -350,7 +351,7 @@ void ParRenamer::CheckParFile(const char* destDir, const char* filename) { debug("Checking par2-header for %s", filename); - const char* basename = Util::BaseFileName(filename); + const char* basename = FileSystem::BaseFileName(filename); const char* extension = strrchr(basename, '.'); if (extension && !strcasecmp(extension, ".par2")) { @@ -382,7 +383,7 @@ void ParRenamer::CheckParFile(const char* destDir, const char* filename) if (packet_magic != header.magic || // not par2-file sizeof(PACKET_HEADER) > header.length || // packet length is too small 0 != (header.length & 3) || // packet length is not a multiple of 4 - Util::FileSize(filename) < (int)header.length) // packet would extend beyond the end of the file + FileSystem::FileSize(filename) < (int)header.length) // packet would extend beyond the end of the file { // not par2-file or damaged header, ignoring the file return; @@ -391,11 +392,11 @@ void ParRenamer::CheckParFile(const char* destDir, const char* filename) BString<100> setId = header.setid.print().c_str(); for (char* p = setId; *p; p++) *p = tolower(*p); // convert string to lowercase - debug("Renaming: %s; setid: %s", Util::BaseFileName(filename), *setId); + debug("Renaming: %s; setid: %s", FileSystem::BaseFileName(filename), *setId); BString<1024> destFileName; int num = 1; - while (num == 1 || Util::FileExists(destFileName)) + while (num == 1 || FileSystem::FileExists(destFileName)) { destFileName.Format("%s%c%s.vol%03i+01.PAR2", destDir, PATH_SEPARATOR, *setId, num); num++; @@ -406,18 +407,18 @@ void ParRenamer::CheckParFile(const char* destDir, const char* filename) void ParRenamer::RenameFile(const char* srcFilename, const char* destFileName) { - PrintMessage(Message::mkInfo, "Renaming %s to %s", Util::BaseFileName(srcFilename), Util::BaseFileName(destFileName)); - if (!Util::MoveFile(srcFilename, destFileName)) + PrintMessage(Message::mkInfo, "Renaming %s to %s", FileSystem::BaseFileName(srcFilename), FileSystem::BaseFileName(destFileName)); + if (!FileSystem::MoveFile(srcFilename, destFileName)) { PrintMessage(Message::mkError, "Could not rename %s to %s: %s", srcFilename, destFileName, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); return; } m_renamedCount++; // notify about new file name - RegisterRenamedFile(Util::BaseFileName(srcFilename), Util::BaseFileName(destFileName)); + RegisterRenamedFile(FileSystem::BaseFileName(srcFilename), FileSystem::BaseFileName(destFileName)); } #endif diff --git a/daemon/postprocess/PrePostProcessor.cpp b/daemon/postprocess/PrePostProcessor.cpp index 9ed7d725..c2c5821e 100644 --- a/daemon/postprocess/PrePostProcessor.cpp +++ b/daemon/postprocess/PrePostProcessor.cpp @@ -31,6 +31,7 @@ #include "DupeCoordinator.h" #include "PostScript.h" #include "Util.h" +#include "FileSystem.h" #include "Unpack.h" #include "Cleanup.h" #include "NzbFile.h" @@ -315,7 +316,7 @@ void PrePostProcessor::DeleteCleanup(NzbInfo* nzbInfo) { CompletedFile* completedFile = *it; BString<1024> fullFileName("%s%c%s", nzbInfo->GetDestDir(), (int)PATH_SEPARATOR, completedFile->GetFileName()); - if (Util::FileExists(fullFileName)) + if (FileSystem::FileExists(fullFileName)) { detail("Deleting file %s", completedFile->GetFileName()); remove(fullFileName); @@ -336,7 +337,7 @@ void PrePostProcessor::DeleteCleanup(NzbInfo* nzbInfo) } // delete old directory (if empty) - if (Util::DirEmpty(nzbInfo->GetDestDir())) + if (FileSystem::DirEmpty(nzbInfo->GetDestDir())) { rmdir(nzbInfo->GetDestDir()); } @@ -453,7 +454,7 @@ void PrePostProcessor::SanitisePostQueue(DownloadQueue* downloadQueue) { m_jobCount++; if (postInfo->GetStage() == PostInfo::ptExecutingScript || - !Util::DirectoryExists(nzbInfo->GetDestDir())) + !FileSystem::DirectoryExists(nzbInfo->GetDestDir())) { postInfo->SetStage(PostInfo::ptFinished); } diff --git a/daemon/postprocess/Unpack.cpp b/daemon/postprocess/Unpack.cpp index 2d4d5a41..e280c886 100644 --- a/daemon/postprocess/Unpack.cpp +++ b/daemon/postprocess/Unpack.cpp @@ -27,6 +27,7 @@ #include "Unpack.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" #include "ParParser.h" #include "Options.h" @@ -379,7 +380,7 @@ void UnpackController::ExecuteSevenZip(const char* password, bool multiVolumes) bool UnpackController::PrepareCmdParams(const char* command, ParamList* params, const char* infoName) { - if (Util::FileExists(command)) + if (FileSystem::FileExists(command)) { params->push_back(strdup(command)); return true; @@ -419,7 +420,7 @@ void UnpackController::JoinSplittedFiles() { BString<1024> fullFilename("%s%c%s", *m_destDir, PATH_SEPARATOR, filename); - if (!Util::DirectoryExists(fullFilename) && + if (!FileSystem::DirectoryExists(fullFilename) && regExSplitExt.Match(filename) && !FileHasRarSignature(fullFilename)) { if (!JoinFile(filename)) @@ -443,7 +444,7 @@ bool UnpackController::JoinFile(const char* fragBaseName) *extension = '\0'; BString<1024> fullFilename("%s%c%s", *m_destDir, PATH_SEPARATOR, fragBaseName); - int64 firstSegmentSize = Util::FileSize(fullFilename); + int64 firstSegmentSize = FileSystem::FileSize(fullFilename); int64 difSegmentSize = 0; // Validate joinable file: @@ -463,7 +464,7 @@ bool UnpackController::JoinFile(const char* fragBaseName) { fullFilename.Format("%s%c%s", *m_destDir, PATH_SEPARATOR, filename); - if (!Util::DirectoryExists(fullFilename) && regExSplitExt.Match(filename)) + if (!FileSystem::DirectoryExists(fullFilename) && regExSplitExt.Match(filename)) { const char* segExt = strrchr(filename, '.'); int segNum = atoi(segExt + 1); @@ -471,7 +472,7 @@ bool UnpackController::JoinFile(const char* fragBaseName) min = segNum < min || min == -1 ? segNum : min; max = segNum > max ? segNum : max; - int64 segmentSize = Util::FileSize(fullFilename); + int64 segmentSize = FileSystem::FileSize(fullFilename); if (segmentSize != firstSegmentSize) { difSizeCount++; @@ -500,7 +501,7 @@ bool UnpackController::JoinFile(const char* fragBaseName) if (!outFile) { PrintMessage(Message::mkError, "Could not create file %s: %s", *destFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); return false; } if (g_Options->GetWriteBuffer() > 0) @@ -521,7 +522,7 @@ bool UnpackController::JoinFile(const char* fragBaseName) SetProgressLabel(BString<1024>("Joining from %s.%.3i", *destBaseName, i)); BString<1024> fragFilename("%s%c%s.%.3i", *m_destDir, PATH_SEPARATOR, *destBaseName, i); - if (!Util::FileExists(fragFilename)) + if (!FileSystem::FileExists(fragFilename)) { break; } @@ -623,7 +624,7 @@ void UnpackController::CreateUnpackDir() { m_finalDir = m_postInfo->GetNzbInfo()->BuildFinalDirName(); m_unpackDir.Format("%s%c%s", *m_finalDir, PATH_SEPARATOR, "_unpack"); - m_finalDirCreated = !Util::DirectoryExists(m_finalDir); + m_finalDirCreated = !FileSystem::DirectoryExists(m_finalDir); } else { @@ -631,7 +632,7 @@ void UnpackController::CreateUnpackDir() } CString errmsg; - if (!Util::ForceDirectories(m_unpackDir, errmsg)) + if (!FileSystem::ForceDirectories(m_unpackDir, errmsg)) { PrintMessage(Message::mkError, "Could not create directory %s: %s", *m_unpackDir, *errmsg); } @@ -658,7 +659,7 @@ void UnpackController::CheckArchiveFiles(bool scanNonStdFiles) { BString<1024> fullFilename("%s%c%s", *m_destDir, PATH_SEPARATOR, filename); - if (!Util::DirectoryExists(fullFilename)) + if (!FileSystem::DirectoryExists(fullFilename)) { const char* ext = strchr(filename, '.'); int extNum = ext ? atoi(ext + 1) : -1; @@ -737,10 +738,10 @@ bool UnpackController::Cleanup() bool hiddenFile = filename[0] == '.'; - if (!Util::MoveFile(srcFile, dstFile) && !hiddenFile) + if (!FileSystem::MoveFile(srcFile, dstFile) && !hiddenFile) { PrintMessage(Message::mkError, "Could not move file %s to %s: %s", *srcFile, *dstFile, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); ok = false; } @@ -749,14 +750,14 @@ bool UnpackController::Cleanup() } CString errmsg; - if (ok && !Util::DeleteDirectoryWithContent(m_unpackDir, errmsg)) + if (ok && !FileSystem::DeleteDirectoryWithContent(m_unpackDir, errmsg)) { PrintMessage(Message::mkError, "Could not delete temporary directory %s: %s", *m_unpackDir, *errmsg); } if (!m_unpackOk && m_finalDirCreated) { - Util::RemoveDirectory(m_finalDir); + FileSystem::RemoveDirectory(m_finalDir); } if (m_unpackOk && ok && g_Options->GetUnpackCleanupDisk()) @@ -774,7 +775,7 @@ bool UnpackController::Cleanup() { BString<1024> fullFilename("%s%c%s", *m_destDir, PATH_SEPARATOR, filename); - if (!Util::DirectoryExists(fullFilename) && + if (!FileSystem::DirectoryExists(fullFilename) && (m_interDir || !extractedFiles.Exists(filename)) && (regExRar.Match(filename) || regExSevenZip.Match(filename) || (regExRarMultiSeq.Match(filename) && FileHasRarSignature(fullFilename)) || @@ -786,7 +787,7 @@ bool UnpackController::Cleanup() if (remove(fullFilename) != 0) { PrintMessage(Message::mkError, "Could not delete file %s: %s", *fullFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } } } diff --git a/daemon/queue/DiskState.cpp b/daemon/queue/DiskState.cpp index 146e621f..9484d440 100644 --- a/daemon/queue/DiskState.cpp +++ b/daemon/queue/DiskState.cpp @@ -29,6 +29,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" static const char* FORMATVERSION_SIGNATURE = "nzbget diskstate file version "; @@ -105,7 +106,7 @@ void StateFile::Discard() bool StateFile::FileExists() { - return Util::FileExists(m_destFilename) || Util::FileExists(m_tempFilename); + return FileSystem::FileExists(m_destFilename) || FileSystem::FileExists(m_tempFilename); } FILE* StateFile::BeginWriteTransaction() @@ -115,7 +116,7 @@ FILE* StateFile::BeginWriteTransaction() if (!m_file) { error("Error saving diskstate: Could not create file %s: %s", *m_tempFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); return NULL; } @@ -129,10 +130,10 @@ bool StateFile::FinishWriteTransaction() // flush file content before renaming if (g_Options->GetFlushQueue()) { - debug("Flushing data for file %s", Util::BaseFileName(m_tempFilename)); + debug("Flushing data for file %s", FileSystem::BaseFileName(m_tempFilename)); fflush(m_file); CString errmsg; - if (!Util::FlushFileBuffers(fileno(m_file), errmsg)) + if (!FileSystem::FlushFileBuffers(fileno(m_file), errmsg)) { warn("Could not flush file %s into disk: %s", *m_tempFilename, *errmsg); } @@ -146,16 +147,16 @@ bool StateFile::FinishWriteTransaction() if (rename(m_tempFilename, m_destFilename)) { error("Error saving diskstate: Could not rename file %s to %s: %s", - *m_tempFilename, *m_destFilename, *Util::GetLastErrorMessage()); + *m_tempFilename, *m_destFilename, *FileSystem::GetLastErrorMessage()); return false; } // flush directory buffer after renaming if (g_Options->GetFlushQueue()) { - debug("Flushing directory for file %s", Util::BaseFileName(m_destFilename)); + debug("Flushing directory for file %s", FileSystem::BaseFileName(m_destFilename)); CString errmsg; - if (!Util::FlushDirBuffers(m_destFilename, errmsg)) + if (!FileSystem::FlushDirBuffers(m_destFilename, errmsg)) { warn("Could not flush directory buffers for file %s into disk: %s", *m_destFilename, *errmsg); } @@ -166,14 +167,14 @@ bool StateFile::FinishWriteTransaction() FILE* StateFile::BeginReadTransaction() { - if (!Util::FileExists(m_destFilename) && Util::FileExists(m_tempFilename)) + if (!FileSystem::FileExists(m_destFilename) && FileSystem::FileExists(m_tempFilename)) { // disaster recovery: temp-file exists but the dest-file doesn't - warn("Restoring diskstate file %s from %s", Util::BaseFileName(m_destFilename), Util::BaseFileName(m_tempFilename)); + warn("Restoring diskstate file %s from %s", FileSystem::BaseFileName(m_destFilename), FileSystem::BaseFileName(m_tempFilename)); if (rename(m_tempFilename, m_destFilename)) { error("Error restoring diskstate: Could not rename file %s to %s: %s", - *m_tempFilename, *m_destFilename, *Util::GetLastErrorMessage()); + *m_tempFilename, *m_destFilename, *FileSystem::GetLastErrorMessage()); return NULL; } } @@ -183,7 +184,7 @@ FILE* StateFile::BeginReadTransaction() if (!m_file) { error("Error reading diskstate: could not open file %s: %s", *m_destFilename, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); return NULL; } @@ -1551,7 +1552,7 @@ bool DiskState::LoadPostQueue5(DownloadQueue* downloadQueue, NzbList* nzbList) BString<1024> fileName("%s%s", g_Options->GetQueueDir(), "postq"); - if (!Util::FileExists(fileName)) + if (!FileSystem::FileExists(fileName)) { return true; } @@ -2040,7 +2041,7 @@ bool DiskState::DownloadQueueExists() debug("Checking if a saved queue exists on disk"); BString<1024> fileName("%s%s", g_Options->GetQueueDir(), "queue"); - return Util::FileExists(fileName); + return FileSystem::FileExists(fileName); } void DiskState::DiscardFile(FileInfo* fileInfo, bool deleteData, bool deletePartialState, bool deleteCompletedState) @@ -2371,7 +2372,7 @@ void DiskState::CalcNzbFileStats(NzbInfo* nzbInfo, int formatVersion) bool DiskState::LoadAllFileStates(DownloadQueue* downloadQueue, Servers* servers) { BString<1024> cacheFlagFilename("%s%s", g_Options->GetQueueDir(), "acache"); - bool cacheWasActive = Util::FileExists(cacheFlagFilename); + bool cacheWasActive = FileSystem::FileExists(cacheFlagFilename); DirBrowser dir(g_Options->GetQueueDir()); while (const char* filename = dir.Next()) @@ -2913,7 +2914,7 @@ void DiskState::LoadNzbMessages(int nzbId, MessageList* messages) BString<1024> logFilename("%sn%i.log", g_Options->GetQueueDir(), nzbId); - if (!Util::FileExists(logFilename)) + if (!FileSystem::FileExists(logFilename)) { return; } diff --git a/daemon/queue/DownloadInfo.cpp b/daemon/queue/DownloadInfo.cpp index 75edaeb1..9290539d 100644 --- a/daemon/queue/DownloadInfo.cpp +++ b/daemon/queue/DownloadInfo.cpp @@ -30,6 +30,7 @@ #include "DiskState.h" #include "Options.h" #include "Util.h" +#include "FileSystem.h" int FileInfo::m_idGen = 0; int FileInfo::m_idMax = 0; @@ -379,14 +380,14 @@ void NzbInfo::SetFilename(const char* filename) CString NzbInfo::MakeNiceNzbName(const char * nzbFilename, bool removeExt) { - CString nicename = Util::BaseFileName(nzbFilename); + CString nicename = FileSystem::BaseFileName(nzbFilename); if (removeExt) { // wipe out ".nzb" char* p = strrchr(nicename, '.'); if (p && !strcasecmp(p, ".nzb")) *p = '\0'; } - Util::MakeValidFilename(nicename, '_', false); + FileSystem::MakeValidFilename(nicename, '_', false); return nicename; } @@ -451,7 +452,7 @@ CString NzbInfo::BuildFinalDirName() { BString<1024> categoryDir; categoryDir = m_category; - Util::MakeValidFilename(categoryDir, '_', true); + FileSystem::MakeValidFilename(categoryDir, '_', true); // we can't format using "finalDir.Format" because one of the parameter is "finalDir" itself. finalDir = BString<1024>("%s%s%c", *finalDir, *categoryDir, PATH_SEPARATOR); } @@ -1024,7 +1025,7 @@ void FileInfo::SetPaused(bool paused) void FileInfo::MakeValidFilename() { - Util::MakeValidFilename(m_filename, '_', false); + FileSystem::MakeValidFilename(m_filename, '_', false); } void FileInfo::LockOutputFile() diff --git a/daemon/queue/HistoryCoordinator.cpp b/daemon/queue/HistoryCoordinator.cpp index 7c553ff4..415180a1 100644 --- a/daemon/queue/HistoryCoordinator.cpp +++ b/daemon/queue/HistoryCoordinator.cpp @@ -30,6 +30,7 @@ #include "QueueCoordinator.h" #include "DiskState.h" #include "Util.h" +#include "FileSystem.h" #include "NzbFile.h" #include "DupeCoordinator.h" #include "ParParser.h" @@ -127,7 +128,7 @@ void HistoryCoordinator::DeleteDiskFiles(NzbInfo* nzbInfo) end = strchr(name1, '|'); if (end) *end = '\0'; - if (Util::FileExists(name1)) + if (FileSystem::FileExists(name1)) { info("Deleting file %s", name1); remove(name1); @@ -336,11 +337,11 @@ void HistoryCoordinator::HistoryDelete(DownloadQueue* downloadQueue, HistoryList historyInfo->GetNzbInfo()->GetParStatus() == NzbInfo::psFailure || historyInfo->GetNzbInfo()->GetUnpackStatus() == NzbInfo::usFailure || historyInfo->GetNzbInfo()->GetUnpackStatus() == NzbInfo::usPassword) && - Util::DirectoryExists(historyInfo->GetNzbInfo()->GetDestDir())) + FileSystem::DirectoryExists(historyInfo->GetNzbInfo()->GetDestDir())) { info("Deleting %s", historyInfo->GetNzbInfo()->GetDestDir()); CString errmsg; - if (!Util::DeleteDirectoryWithContent(historyInfo->GetNzbInfo()->GetDestDir(), errmsg)) + if (!FileSystem::DeleteDirectoryWithContent(historyInfo->GetNzbInfo()->GetDestDir(), errmsg)) { error("Could not delete directory %s: %s", historyInfo->GetNzbInfo()->GetDestDir(), *errmsg); } @@ -468,7 +469,7 @@ void HistoryCoordinator::HistoryRedownload(DownloadQueue* downloadQueue, History NzbInfo* nzbInfo = historyInfo->GetNzbInfo(); bool paused = restorePauseState && nzbInfo->GetDeletePaused(); - if (!Util::FileExists(nzbInfo->GetQueuedFilename())) + if (!FileSystem::FileExists(nzbInfo->GetQueuedFilename())) { error("Could not return %s from history back to queue: could not find source nzb-file %s", nzbInfo->GetName(), nzbInfo->GetQueuedFilename()); @@ -492,22 +493,22 @@ void HistoryCoordinator::HistoryRedownload(DownloadQueue* downloadQueue, History fileInfo->SetPaused(paused); } - if (Util::DirectoryExists(nzbInfo->GetDestDir())) + if (FileSystem::DirectoryExists(nzbInfo->GetDestDir())) { detail("Deleting %s", nzbInfo->GetDestDir()); CString errmsg; - if (!Util::DeleteDirectoryWithContent(nzbInfo->GetDestDir(), errmsg)) + if (!FileSystem::DeleteDirectoryWithContent(nzbInfo->GetDestDir(), errmsg)) { error("Could not delete directory %s: %s", nzbInfo->GetDestDir(), *errmsg); } } nzbInfo->BuildDestDirName(); - if (Util::DirectoryExists(nzbInfo->GetDestDir())) + if (FileSystem::DirectoryExists(nzbInfo->GetDestDir())) { detail("Deleting %s", nzbInfo->GetDestDir()); CString errmsg; - if (!Util::DeleteDirectoryWithContent(nzbInfo->GetDestDir(), errmsg)) + if (!FileSystem::DeleteDirectoryWithContent(nzbInfo->GetDestDir(), errmsg)) { error("Could not delete directory %s: %s", nzbInfo->GetDestDir(), *errmsg); } diff --git a/daemon/queue/NzbFile.cpp b/daemon/queue/NzbFile.cpp index 3f6d7376..68bb7352 100644 --- a/daemon/queue/NzbFile.cpp +++ b/daemon/queue/NzbFile.cpp @@ -31,6 +31,7 @@ #include "Options.h" #include "DiskState.h" #include "Util.h" +#include "FileSystem.h" NzbFile::NzbFile(const char* fileName, const char* category) { @@ -514,7 +515,7 @@ bool NzbFile::Parse() _bstr_t r(doc->GetparseError()->reason); const char* errMsg = r; m_nzbInfo->AddMessage(Message::mkError, BString<1024>("Error parsing nzb-file %s: %s", - Util::BaseFileName(m_fileName), errMsg)); + FileSystem::BaseFileName(m_fileName), errMsg)); return false; } @@ -526,7 +527,7 @@ bool NzbFile::Parse() if (GetNzbInfo()->GetFileList()->empty()) { m_nzbInfo->AddMessage(Message::mkError, BString<1024>( - "Error parsing nzb-file %s: file has no content", Util::BaseFileName(m_fileName))); + "Error parsing nzb-file %s: file has no content", FileSystem::BaseFileName(m_fileName))); return false; } @@ -651,14 +652,14 @@ bool NzbFile::Parse() if (ret != 0) { m_nzbInfo->AddMessage(Message::mkError, BString<1024>( - "Error parsing nzb-file %s", Util::BaseFileName(m_fileName))); + "Error parsing nzb-file %s", FileSystem::BaseFileName(m_fileName))); return false; } if (m_nzbInfo->GetFileList()->empty()) { m_nzbInfo->AddMessage(Message::mkError, BString<1024>( - "Error parsing nzb-file %s: file has no content", Util::BaseFileName(m_fileName))); + "Error parsing nzb-file %s: file has no content", FileSystem::BaseFileName(m_fileName))); return false; } diff --git a/daemon/queue/QueueCoordinator.cpp b/daemon/queue/QueueCoordinator.cpp index 7a49626c..e6122af6 100644 --- a/daemon/queue/QueueCoordinator.cpp +++ b/daemon/queue/QueueCoordinator.cpp @@ -32,6 +32,7 @@ #include "ArticleWriter.h" #include "DiskState.h" #include "Util.h" +#include "FileSystem.h" #include "Decoder.h" #include "StatMeter.h" @@ -666,7 +667,7 @@ void QueueCoordinator::ArticleCompleted(ArticleDownloader* articleDownloader) if (g_Options->GetDupeCheck() && nzbInfo->GetDupeMode() != dmForce && !nzbInfo->GetManyDupeFiles() && - Util::FileExists(nzbInfo->GetDestDir(), fileInfo->GetFilename())) + FileSystem::FileExists(nzbInfo->GetDestDir(), fileInfo->GetFilename())) { warn("File \"%s\" seems to be duplicate, cancelling download and deleting file from queue", fileInfo->GetFilename()); fileCompleted = false; diff --git a/daemon/queue/QueueEditor.cpp b/daemon/queue/QueueEditor.cpp index e304868b..f540997d 100644 --- a/daemon/queue/QueueEditor.cpp +++ b/daemon/queue/QueueEditor.cpp @@ -29,6 +29,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" #include "QueueCoordinator.h" #include "PrePostProcessor.h" #include "HistoryCoordinator.h" @@ -752,7 +753,7 @@ bool QueueEditor::BuildIdListFromNameList(IdList* idList, NameList* nameList, Do if (action < DownloadQueue::eaGroupMoveOffset) { // file action - BString<1024> filename("%s/%s", fileInfo->GetNzbInfo()->GetName(), Util::BaseFileName(fileInfo->GetFilename())); + BString<1024> filename("%s/%s", fileInfo->GetNzbInfo()->GetName(), FileSystem::BaseFileName(fileInfo->GetFilename())); if (((!regEx && !strcmp(filename, name)) || (regEx && regEx->Match(filename))) && (uniqueIds.find(fileInfo->GetId()) == uniqueIds.end())) { @@ -1182,7 +1183,7 @@ void QueueEditor::ReorderFiles(ItemList* itemList) void QueueEditor::SetNzbParameter(NzbInfo* nzbInfo, const char* paramString) { - debug("QueueEditor: setting nzb parameter '%s' for '%s'", paramString, Util::BaseFileName(nzbInfo->GetFilename())); + debug("QueueEditor: setting nzb parameter '%s' for '%s'", paramString, FileSystem::BaseFileName(nzbInfo->GetFilename())); char* str = strdup(paramString); diff --git a/daemon/queue/Scanner.cpp b/daemon/queue/Scanner.cpp index 25cf4a5b..0c85f710 100644 --- a/daemon/queue/Scanner.cpp +++ b/daemon/queue/Scanner.cpp @@ -31,6 +31,7 @@ #include "HistoryCoordinator.h" #include "ScanScript.h" #include "Util.h" +#include "FileSystem.h" Scanner::FileData::FileData(const char* filename) { @@ -185,7 +186,7 @@ void Scanner::CheckIncomingNzbs(const char* directory, const char* category, boo while (const char* filename = dir.Next()) { BString<1024> fullfilename("%s%s", directory, filename); - bool isDirectory = Util::DirectoryExists(fullfilename); + bool isDirectory = FileSystem::DirectoryExists(fullfilename); // check subfolders if (isDirectory) { @@ -228,7 +229,7 @@ bool Scanner::CanProcessFile(const char* fullFilename, bool checkStat) return true; } - int64 size = Util::FileSize(fullFilename); + int64 size = FileSystem::FileSize(fullFilename); time_t current = time(NULL); bool canProcess = false; bool inList = false; @@ -329,7 +330,7 @@ void Scanner::ProcessIncomingFile(const char* directory, const char* baseFilenam for (QueueList::iterator it = m_queueList.begin(); it != m_queueList.end(); it++) { QueueData* queueData1 = *it; - if (Util::SameFilename(queueData1->GetFilename(), fullFilename)) + if (FileSystem::SameFilename(queueData1->GetFilename(), fullFilename)) { queueData = queueData1; free(nzbName); @@ -358,15 +359,15 @@ void Scanner::ProcessIncomingFile(const char* directory, const char* baseFilenam urlInfo ? urlInfo->GetUrl() : "", directory, &nzbName, &nzbCategory, &priority, parameters, &addTop, &addPaused, &dupeKey, &dupeScore, &dupeMode); - exists = Util::FileExists(fullFilename); + exists = FileSystem::FileExists(fullFilename); if (exists && strcasecmp(extension, ".nzb")) { CString bakname2; - bool renameOK = Util::RenameBak(fullFilename, "processed", false, bakname2); + bool renameOK = FileSystem::RenameBak(fullFilename, "processed", false, bakname2); if (!renameOK) { error("Could not rename file %s to %s: %s", fullFilename, *bakname2, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } } } @@ -374,7 +375,7 @@ void Scanner::ProcessIncomingFile(const char* directory, const char* baseFilenam if (!strcasecmp(extension, ".nzb_processed")) { CString renamedName; - bool renameOK = Util::RenameBak(fullFilename, "nzb", true, renamedName); + bool renameOK = FileSystem::RenameBak(fullFilename, "nzb", true, renamedName); if (renameOK) { bool added = AddFileToQueue(renamedName, nzbName, nzbCategory, priority, @@ -384,7 +385,7 @@ void Scanner::ProcessIncomingFile(const char* directory, const char* baseFilenam else { error("Could not rename file %s to %s: %s", fullFilename, *renamedName, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); addStatus = asFailed; } } @@ -452,7 +453,7 @@ bool Scanner::AddFileToQueue(const char* filename, const char* nzbName, const ch int priority, const char* dupeKey, int dupeScore, EDupeMode dupeMode, NzbParameterList* parameters, bool addTop, bool addPaused, NzbInfo* urlInfo, int* nzbId) { - const char* basename = Util::BaseFileName(filename); + const char* basename = FileSystem::BaseFileName(filename); info("Adding collection %s to queue", basename); @@ -464,11 +465,11 @@ bool Scanner::AddFileToQueue(const char* filename, const char* nzbName, const ch } CString bakname2; - if (!Util::RenameBak(filename, nzbFile ? "queued" : "error", false, bakname2)) + if (!FileSystem::RenameBak(filename, nzbFile ? "queued" : "error", false, bakname2)) { ok = false; error("Could not rename file %s to %s: %s", filename, *bakname2, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); } NzbInfo* nzbInfo = nzbFile->GetNzbInfo(); @@ -560,13 +561,13 @@ Scanner::EAddStatus Scanner::AddExternalFile(const char* nzbName, const char* ca else { int num = 1; - while (num == 1 || Util::FileExists(tempFileName)) + while (num == 1 || FileSystem::FileExists(tempFileName)) { tempFileName.Format("%snzb-%i.tmp", g_Options->GetTempDir(), num); num++; } - if (!Util::SaveBufferIntoFile(tempFileName, buffer, bufSize)) + if (!FileSystem::SaveBufferIntoFile(tempFileName, buffer, bufSize)) { error("Could not create file %s", *tempFileName); return asFailed; @@ -579,8 +580,8 @@ Scanner::EAddStatus Scanner::AddExternalFile(const char* nzbName, const char* ca } // move file into NzbDir, make sure the file name is unique - BString<1024> validNzbName = Util::BaseFileName(nzbName); - Util::MakeValidFilename(validNzbName, '_', false); + BString<1024> validNzbName = FileSystem::BaseFileName(nzbName); + FileSystem::MakeValidFilename(validNzbName, '_', false); #ifdef WIN32 WebUtil::Utf8ToAnsi(validNzbName, validNzbName.Capacity()); @@ -602,7 +603,7 @@ Scanner::EAddStatus Scanner::AddExternalFile(const char* nzbName, const char* ca } int num = 2; - while (Util::FileExists(scanFileName)) + while (FileSystem::FileExists(scanFileName)) { if (ext) { @@ -617,10 +618,10 @@ Scanner::EAddStatus Scanner::AddExternalFile(const char* nzbName, const char* ca m_scanMutex.Lock(); - if (!Util::MoveFile(tempFileName, scanFileName)) + if (!FileSystem::MoveFile(tempFileName, scanFileName)) { error("Could not move file %s to %s: %s", *tempFileName, *scanFileName, - *Util::GetLastErrorMessage()); + *FileSystem::GetLastErrorMessage()); remove(tempFileName); m_scanMutex.Unlock(); // UNLOCK return asFailed; diff --git a/daemon/queue/UrlCoordinator.cpp b/daemon/queue/UrlCoordinator.cpp index 1d683793..c7c213dc 100644 --- a/daemon/queue/UrlCoordinator.cpp +++ b/daemon/queue/UrlCoordinator.cpp @@ -28,6 +28,7 @@ #include "Options.h" #include "WebDownloader.h" #include "Util.h" +#include "FileSystem.h" #include "NzbFile.h" #include "Scanner.h" #include "DiskState.h" @@ -310,12 +311,12 @@ void UrlCoordinator::UrlCompleted(UrlDownloader* urlDownloader) } else { - filename = Util::BaseFileName(nzbInfo->GetUrl()); + filename = FileSystem::BaseFileName(nzbInfo->GetUrl()); // TODO: decode URL escaping } - Util::MakeValidFilename(filename, '_', false); + FileSystem::MakeValidFilename(filename, '_', false); debug("Filename: [%s]", *filename); diff --git a/daemon/remote/BinRpc.cpp b/daemon/remote/BinRpc.cpp index 625f5af1..f0083c98 100644 --- a/daemon/remote/BinRpc.cpp +++ b/daemon/remote/BinRpc.cpp @@ -30,6 +30,7 @@ #include "Options.h" #include "QueueEditor.h" #include "Util.h" +#include "FileSystem.h" #include "DownloadInfo.h" #include "Scanner.h" #include "StatMeter.h" @@ -460,7 +461,7 @@ void DownloadBinCommand::Execute() SendBoolResponse(ok, BString<1024>(ok ? "Collection %s added to queue" : "Download Request failed for %s", - Util::BaseFileName(DownloadRequest.m_nzbFilename))); + FileSystem::BaseFileName(DownloadRequest.m_nzbFilename))); free(nzbContent); } @@ -649,7 +650,7 @@ void ListBinCommand::Execute() if (regEx && !matchGroup) { BString<1024> filename("%s/%s", fileInfo->GetNzbInfo()->GetName(), - Util::BaseFileName(fileInfo->GetFilename())); + FileSystem::BaseFileName(fileInfo->GetFilename())); listAnswer->m_match = htonl(regEx->Match(filename)); } diff --git a/daemon/remote/RemoteClient.cpp b/daemon/remote/RemoteClient.cpp index 0efc5234..11012ff8 100644 --- a/daemon/remote/RemoteClient.cpp +++ b/daemon/remote/RemoteClient.cpp @@ -30,6 +30,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" RemoteClient::RemoteClient() { @@ -153,7 +154,7 @@ bool RemoteClient::RequestServerDownload(const char* nzbFilename, const char* nz } else { - if (!Util::LoadFileIntoBuffer(nzbContent, &buffer, &length)) + if (!FileSystem::LoadFileIntoBuffer(nzbContent, &buffer, &length)) { printf("Could not load file %s\n", nzbContent); return false; diff --git a/daemon/remote/RemoteServer.cpp b/daemon/remote/RemoteServer.cpp index 1f123784..66aeda93 100644 --- a/daemon/remote/RemoteServer.cpp +++ b/daemon/remote/RemoteServer.cpp @@ -30,7 +30,7 @@ #include "WebServer.h" #include "Log.h" #include "Options.h" -#include "Util.h" +#include "FileSystem.h" //***************************************************************** // RemoteServer @@ -57,13 +57,13 @@ void RemoteServer::Run() #ifndef DISABLE_TLS if (m_tls) { - if (strlen(g_Options->GetSecureCert()) == 0 || !Util::FileExists(g_Options->GetSecureCert())) + if (strlen(g_Options->GetSecureCert()) == 0 || !FileSystem::FileExists(g_Options->GetSecureCert())) { error("Could not initialize TLS, secure certificate is not configured or the cert-file was not found. Check option "); return; } - if (strlen(g_Options->GetSecureKey()) == 0 || !Util::FileExists(g_Options->GetSecureKey())) + if (strlen(g_Options->GetSecureKey()) == 0 || !FileSystem::FileExists(g_Options->GetSecureKey())) { error("Could not initialize TLS, secure key is not configured or the key-file was not found. Check option "); return; diff --git a/daemon/remote/WebServer.cpp b/daemon/remote/WebServer.cpp index 308af434..06b7d02c 100644 --- a/daemon/remote/WebServer.cpp +++ b/daemon/remote/WebServer.cpp @@ -29,6 +29,7 @@ #include "Log.h" #include "Options.h" #include "Util.h" +#include "FileSystem.h" static const char* ERR_HTTP_BAD_REQUEST = "400 Bad Request"; static const char* ERR_HTTP_NOT_FOUND = "404 Not Found"; @@ -501,7 +502,7 @@ void WebProcessor::SendFileResponse(const char* filename) char *body; int bodyLen; - if (!Util::LoadFileIntoBuffer(filename, &body, &bodyLen)) + if (!FileSystem::LoadFileIntoBuffer(filename, &body, &bodyLen)) { // do not print warnings "404 not found" for certain files bool ignorable = !strcmp(filename, "package-info.json") || diff --git a/daemon/remote/XmlRpc.cpp b/daemon/remote/XmlRpc.cpp index a04c2cbb..3084e22f 100644 --- a/daemon/remote/XmlRpc.cpp +++ b/daemon/remote/XmlRpc.cpp @@ -31,6 +31,7 @@ #include "FeedCoordinator.h" #include "ServerPool.h" #include "Util.h" +#include "FileSystem.h" #include "Maintenance.h" #include "StatMeter.h" #include "ArticleWriter.h" @@ -1347,7 +1348,7 @@ void StatusXmlCommand::Execute() Util::SplitInt64(allBytes, &downloadedSizeHi, &downloadedSizeLo); int averageDownloadRate = (int)(downloadTimeSec > 0 ? allBytes / downloadTimeSec : 0); uint32 freeDiskSpaceHi, freeDiskSpaceLo; - int64 freeDiskSpace = Util::FreeDiskSize(g_Options->GetDestDir()); + int64 freeDiskSpace = FileSystem::FreeDiskSize(g_Options->GetDestDir()); Util::SplitInt64(freeDiskSpace, &freeDiskSpaceHi, &freeDiskSpaceLo); int freeDiskSpaceMB = (int)(freeDiskSpace / 1024 / 1024); int serverTime = time(NULL); @@ -3061,7 +3062,7 @@ void ReadUrlXmlCommand::Execute() // generate temp file name BString<1024> tempFileName; int num = 1; - while (num == 1 || Util::FileExists(tempFileName)) + while (num == 1 || FileSystem::FileExists(tempFileName)) { tempFileName.Format("%sreadurl-%i.tmp", g_Options->GetTempDir(), num); num++; @@ -3084,7 +3085,7 @@ void ReadUrlXmlCommand::Execute() { char* fileContent = NULL; int fileContentLen = 0; - Util::LoadFileIntoBuffer(tempFileName, &fileContent, &fileContentLen); + FileSystem::LoadFileIntoBuffer(tempFileName, &fileContent, &fileContentLen); CString xmlContent = EncodeStr(fileContent); free(fileContent); AppendResponse(IsJson() ? "\"" : ""); diff --git a/daemon/util/FileSystem.cpp b/daemon/util/FileSystem.cpp new file mode 100644 index 00000000..7f76e8ef --- /dev/null +++ b/daemon/util/FileSystem.cpp @@ -0,0 +1,846 @@ +/* + * This file is part of nzbget + * + * Copyright (C) 2007-2015 Andrey Prygunkov + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $Revision$ + * $Date$ + * + */ + + +#include "nzbget.h" +#include "FileSystem.h" + +CString FileSystem::GetLastErrorMessage() +{ + BString<1024> msg; + strerror_r(errno, msg, msg.Capacity()); + return *msg; +} + +void FileSystem::NormalizePathSeparators(char* path) +{ + for (char* p = path; *p; p++) + { + if (*p == ALT_PATH_SEPARATOR) + { + *p = PATH_SEPARATOR; + } + } +} + +bool FileSystem::ForceDirectories(const char* path, CString& errmsg) +{ + errmsg.Clear(); + BString<1024> normPath = path; + NormalizePathSeparators(normPath); + int len = strlen(normPath); + if ((len > 0) && normPath[len-1] == PATH_SEPARATOR +#ifdef WIN32 + && len > 3 +#endif + ) + { + normPath[len-1] = '\0'; + } + + struct stat buffer; + bool ok = !stat(normPath, &buffer); + if (!ok && errno != ENOENT) + { + errmsg.Format("could not read information for directory %s: errno %i, %s", + *normPath, errno, *GetLastErrorMessage()); + return false; + } + + if (ok && !S_ISDIR(buffer.st_mode)) + { + errmsg.Format("path %s is not a directory", *normPath); + return false; + } + + if (!ok +#ifdef WIN32 + && strlen(normPath) > 2 +#endif + ) + { + BString<1024> parentPath = *normPath; + char* p = (char*)strrchr(parentPath, PATH_SEPARATOR); + if (p) + { +#ifdef WIN32 + if (p - parentPath == 2 && parentPath[1] == ':' && strlen(parentPath) > 2) + { + parentPath[3] = '\0'; + } + else +#endif + { + *p = '\0'; + } + if (strlen(parentPath) != strlen(path) && !ForceDirectories(parentPath, errmsg)) + { + return false; + } + } + + if (mkdir(normPath, S_DIRMODE) != 0 && errno != EEXIST) + { + errmsg.Format("could not create directory %s: %s", *normPath, *GetLastErrorMessage()); + return false; + } + + if (stat(normPath, &buffer) != 0) + { + errmsg.Format("could not read information for directory %s: %s", + *normPath, *GetLastErrorMessage()); + return false; + } + + if (!S_ISDIR(buffer.st_mode)) + { + errmsg.Format("path %s is not a directory", *normPath); + return false; + } + } + + return true; +} + +CString FileSystem::GetCurrentDirectory() +{ + CString result; + result.Reserve(1024); +#ifdef WIN32 + ::GetCurrentDirectory(1024, result); +#else + getcwd(result, 1024); +#endif + return result; +} + +bool FileSystem::SetCurrentDirectory(const char* dirFilename) +{ +#ifdef WIN32 + return ::SetCurrentDirectory(dirFilename); +#else + return chdir(dirFilename) == 0; +#endif +} + +bool FileSystem::DirEmpty(const char* dirFilename) +{ + DirBrowser dir(dirFilename); + return dir.Next() == NULL; +} + +bool FileSystem::LoadFileIntoBuffer(const char* fileName, char** buffer, int* bufferLength) +{ + FILE* file = fopen(fileName, FOPEN_RB); + if (!file) + { + return false; + } + + // obtain file size. + fseek(file , 0 , SEEK_END); + int size = (int)ftell(file); + rewind(file); + + // allocate memory to contain the whole file. + *buffer = (char*) malloc(size + 1); + if (!*buffer) + { + return false; + } + + // copy the file into the buffer. + fread(*buffer, 1, size, file); + + fclose(file); + + (*buffer)[size] = 0; + + *bufferLength = size + 1; + + return true; +} + +bool FileSystem::SaveBufferIntoFile(const char* fileName, const char* buffer, int bufLen) +{ + FILE* file = fopen(fileName, FOPEN_WB); + if (!file) + { + return false; + } + + int writtenBytes = fwrite(buffer, 1, bufLen, file); + fclose(file); + + return writtenBytes == bufLen; +} + +bool FileSystem::CreateSparseFile(const char* filename, int64 size, CString& errmsg) +{ + errmsg.Clear(); + bool ok = false; +#ifdef WIN32 + HANDLE hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, 0, NULL); + if (hFile == INVALID_HANDLE_VALUE) + { + errmsg = GetLastErrorMessage(); + return false; + } + // first try to create sparse file (supported only on NTFS partitions), + // it may fail but that's OK. + DWORD dwBytesReturned; + DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dwBytesReturned, NULL); + + LARGE_INTEGER size64; + size64.QuadPart = size; + SetFilePointerEx(hFile, size64, NULL, FILE_END); + SetEndOfFile(hFile); + CloseHandle(hFile); + ok = true; +#else + // create file + FILE* file = fopen(filename, FOPEN_AB); + if (!file) + { + errmsg = GetLastErrorMessage(); + return false; + } + fclose(file); + + // there are no reliable function to expand file on POSIX, so we must try different approaches, + // starting with the fastest one and hoping it will work + // 1) set file size using function "truncate" (this is fast, if it works) + truncate(filename, size); + // check if it worked + ok = FileSize(filename) == size; + if (!ok) + { + // 2) truncate did not work, expanding the file by writing to it (that's slow) + truncate(filename, 0); + file = fopen(filename, FOPEN_AB); + if (!file) + { + errmsg = GetLastErrorMessage(); + return false; + } + char c = '0'; + fwrite(&c, 1, size, file); + fclose(file); + ok = FileSize(filename) == size; + } +#endif + return ok; +} + +bool FileSystem::TruncateFile(const char* filename, int size) +{ + bool ok = false; +#ifdef WIN32 + FILE *file = fopen(filename, FOPEN_RBP); + fseek(file, size, SEEK_SET); + ok = SetEndOfFile((HANDLE)_get_osfhandle(_fileno(file))) != 0; + fclose(file); +#else + ok = truncate(filename, size) == 0; +#endif + return ok; +} + +char* FileSystem::BaseFileName(const char* filename) +{ + char* p = (char*)strrchr(filename, PATH_SEPARATOR); + char* p1 = (char*)strrchr(filename, ALT_PATH_SEPARATOR); + if (p1) + { + if ((p && p < p1) || !p) + { + p = p1; + } + } + if (p) + { + return p + 1; + } + else + { + return (char*)filename; + } +} + +//replace bad chars in filename +void FileSystem::MakeValidFilename(char* filename, char cReplaceChar, bool allowSlashes) +{ + const char* replaceChars = allowSlashes ? ":*?\"><\n\r\t" : "\\/:*?\"><\n\r\t"; + char* p = filename; + while (*p) + { + if (strchr(replaceChars, *p)) + { + *p = cReplaceChar; + } + if (allowSlashes && *p == ALT_PATH_SEPARATOR) + { + *p = PATH_SEPARATOR; + } + p++; + } + + // remove trailing dots and spaces. they are not allowed in directory names on windows, + // but we remove them on posix also, in a case the directory is accessed from windows via samba. + for (int len = strlen(filename); len > 0 && (filename[len - 1] == '.' || filename[len - 1] == ' '); len--) + { + filename[len - 1] = '\0'; + } +} + +// returns TRUE if the name was changed by adding duplicate-suffix +CString FileSystem::MakeUniqueFilename(const char* destDir, const char* basename) +{ + CString result; + result.Format("%s%c%s", destDir, (int)PATH_SEPARATOR, basename); + + int dupeNumber = 0; + while (FileExists(result)) + { + dupeNumber++; + + const char* extension = strrchr(basename, '.'); + if (extension && extension != basename) + { + BString<1024> filenameWithoutExt = basename; + int end = extension - basename; + filenameWithoutExt[end < 1024 ? end : 1024-1] = '\0'; + + if (!strcasecmp(extension, ".par2")) + { + char* volExtension = strrchr(filenameWithoutExt, '.'); + if (volExtension && volExtension != filenameWithoutExt && + !strncasecmp(volExtension, ".vol", 4)) + { + *volExtension = '\0'; + extension = basename + (volExtension - filenameWithoutExt); + } + } + + result.Format("%s%c%s.duplicate%d%s", destDir, (int)PATH_SEPARATOR, + *filenameWithoutExt, dupeNumber, extension); + } + else + { + result.Format("%s%c%s.duplicate%d", destDir, (int)PATH_SEPARATOR, + basename, dupeNumber); + } + } + + return result; +} + +bool FileSystem::MoveFile(const char* srcFilename, const char* dstFilename) +{ + bool ok = rename(srcFilename, dstFilename) == 0; + +#ifndef WIN32 + if (!ok && errno == EXDEV) + { + ok = CopyFile(srcFilename, dstFilename) && remove(srcFilename) == 0; + } +#endif + + return ok; +} + +bool FileSystem::CopyFile(const char* srcFilename, const char* dstFilename) +{ + FILE* infile = fopen(srcFilename, FOPEN_RB); + if (!infile) + { + return false; + } + + FILE* outfile = fopen(dstFilename, FOPEN_WBP); + if (!outfile) + { + fclose(infile); + return false; + } + + static const int BUFFER_SIZE = 1024 * 50; + char* buffer = (char*)malloc(BUFFER_SIZE); + + int cnt = BUFFER_SIZE; + while (cnt == BUFFER_SIZE) + { + cnt = (int)fread(buffer, 1, BUFFER_SIZE, infile); + fwrite(buffer, 1, cnt, outfile); + } + + fclose(infile); + fclose(outfile); + free(buffer); + + return true; +} + +bool FileSystem::FileExists(const char* filename) +{ +#ifdef WIN32 + // we use a native windows call because c-lib function "stat" fails on windows if file date is invalid + WIN32_FIND_DATA findData; + HANDLE handle = FindFirstFile(filename, &findData); + if (handle != INVALID_HANDLE_VALUE) + { + bool exists = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; + FindClose(handle); + return exists; + } + return false; +#else + struct stat buffer; + bool exists = !stat(filename, &buffer) && S_ISREG(buffer.st_mode); + return exists; +#endif +} + +bool FileSystem::FileExists(const char* path, const char* filenameWithoutPath) +{ + BString<1024> fullFilename("%s%c%s", path, (int)PATH_SEPARATOR, filenameWithoutPath); + bool exists = FileSystem::FileExists(fullFilename); + return exists; +} + +bool FileSystem::DirectoryExists(const char* dirFilename) +{ +#ifdef WIN32 + // we use a native windows call because c-lib function "stat" fails on windows if file date is invalid + WIN32_FIND_DATA findData; + HANDLE handle = FindFirstFile(dirFilename, &findData); + if (handle != INVALID_HANDLE_VALUE) + { + bool exists = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; + FindClose(handle); + return exists; + } + return false; +#else + struct stat buffer; + bool exists = !stat(dirFilename, &buffer) && S_ISDIR(buffer.st_mode); + return exists; +#endif +} + +bool FileSystem::CreateDirectory(const char* dirFilename) +{ + mkdir(dirFilename, S_DIRMODE); + return DirectoryExists(dirFilename); +} + +bool FileSystem::RemoveDirectory(const char* dirFilename) +{ +#ifdef WIN32 + return _rmdir(dirFilename) == 0; +#else + return remove(dirFilename) == 0; +#endif +} + +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 = remove(fullFilename) == 0; + } + ok &= del; + if (!del && errmsg.Empty()) + { + errmsg.Format("could not delete %s: %s", *fullFilename, *GetLastErrorMessage()); + } + } + + del = RemoveDirectory(dirFilename); + ok &= del; + if (!del && errmsg.Empty()) + { + errmsg = GetLastErrorMessage(); + } + return ok; +} + +int64 FileSystem::FileSize(const char* filename) +{ +#ifdef WIN32 + struct _stat32i64 buffer; + _stat32i64(filename, &buffer); +#else + struct stat buffer; + stat(filename, &buffer); +#endif + return buffer.st_size; +} + +int64 FileSystem::FreeDiskSize(const char* path) +{ +#ifdef WIN32 + ULARGE_INTEGER free, dummy; + if (GetDiskFreeSpaceEx(path, &free, &dummy, &dummy)) + { + return free.QuadPart; + } +#else + struct statvfs diskdata; + if (!statvfs(path, &diskdata)) + { + return (int64)diskdata.f_frsize * (int64)diskdata.f_bavail; + } +#endif + return -1; +} + +bool FileSystem::RenameBak(const char* filename, const char* bakPart, bool removeOldExtension, CString& newName) +{ + BString<1024> changedFilename; + + if (removeOldExtension) + { + changedFilename = filename; + char* extension = strrchr(changedFilename, '.'); + if (extension) + { + *extension = '\0'; + } + } + + newName.Format("%s.%s", removeOldExtension ? *changedFilename : filename, bakPart); + + int i = 2; + struct stat buffer; + while (!stat(newName, &buffer)) + { + newName.Format("%s.%i.%s", removeOldExtension ? *changedFilename : filename, i++, bakPart); + } + + bool ok = !rename(filename, newName); + return ok; +} + +#ifndef WIN32 +CString FileSystem::ExpandHomePath(const char* filename) +{ + CString result; + + if (filename && (filename[0] == '~') && (filename[1] == '/')) + { + // expand home-dir + + char* home = getenv("HOME"); + if (!home) + { + struct passwd *pw = getpwuid(getuid()); + if (pw) + { + home = pw->pw_dir; + } + } + + if (!home) + { + return filename; + } + + if (home[strlen(home)-1] == '/') + { + result.Format("%s%s", home, filename + 2); + } + else + { + result.Format("%s/%s", home, filename + 2); + } + } + else + { + result.Append(filename ? filename : ""); + } + + return result; +} +#endif + +CString FileSystem::ExpandFileName(const char* filename) +{ + CString result; + result.Reserve(1024); + +#ifdef WIN32 + _fullpath(result, filename, 1024); +#else + if (filename[0] != '\0' && filename[0] != '/') + { + char curDir[MAX_PATH + 1]; + getcwd(curDir, sizeof(curDir) - 1); // 1 char reserved for adding backslash + int offset = 0; + if (filename[0] == '.' && filename[1] == '/') + { + offset += 2; + } + result.Format("%s/%s", curDir, filename + offset); + } + else + { + result = filename; + } +#endif + + return result; +} + +CString FileSystem::GetExeFileName(const char* argv0) +{ + CString exename; + exename.Reserve(1024); + exename[1024 - 1] = '\0'; + +#ifdef WIN32 + GetModuleFileName(NULL, exename, 1024); +#else + // Linux + int r = readlink("/proc/self/exe", exename, 1024 - 1); + if (r > 0) + { + return exename; + } + // FreeBSD + r = readlink("/proc/curproc/file", exename, 1024 - 1); + if (r > 0) + { + return exename; + } + + exename = ExpandFileName(argv0); +#endif + + return exename; +} + +bool FileSystem::SameFilename(const char* filename1, const char* filename2) +{ +#ifdef WIN32 + return strcasecmp(filename1, filename2) == 0; +#else + return strcmp(filename1, filename2) == 0; +#endif +} + +bool FileSystem::FlushFileBuffers(int fileDescriptor, CString& errmsg) +{ +#ifdef WIN32 + BOOL ok = ::FlushFileBuffers((HANDLE)_get_osfhandle(fileDescriptor)); + if (!ok) + { + errmsg.Reserve(1024); + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + errmsg, 1024, NULL); + } + return ok; +#else +#ifdef HAVE_FULLFSYNC + int ret = fcntl(fileDescriptor, F_FULLFSYNC) == -1 ? 1 : 0; +#elif HAVE_FDATASYNC + int ret = fdatasync(fileDescriptor); +#else + int ret = fsync(fileDescriptor); +#endif + if (ret != 0) + { + errmsg = GetLastErrorMessage(); + } + return ret == 0; +#endif +} + +bool FileSystem::FlushDirBuffers(const char* filename, CString& errmsg) +{ + BString<1024> parentPath = filename; + const char* fileMode = FOPEN_RBP; + +#ifndef WIN32 + char* p = (char*)strrchr(parentPath, PATH_SEPARATOR); + if (p) + { + *p = '\0'; + } + fileMode = FOPEN_RB; +#endif + + FILE* file = fopen(parentPath, fileMode); + if (!file) + { + errmsg = GetLastErrorMessage(); + return false; + } + bool ok = FlushFileBuffers(fileno(file), errmsg); + fclose(file); + return ok; +} + +#ifndef WIN32 +void FileSystem::FixExecPermission(const char* filename) +{ + struct stat buffer; + bool ok = !stat(filename, &buffer); + if (ok) + { + buffer.st_mode = buffer.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; + chmod(filename, buffer.st_mode); + } +} +#endif + +#ifdef WIN32 + +DirBrowser::DirBrowser(const char* path) +{ + BString<1024> mask("%s%c*.*", path, (int)PATH_SEPARATOR); + m_file = FindFirstFile(mask, &m_findData); + m_first = true; +} + +DirBrowser::~DirBrowser() +{ + if (m_file != INVALID_HANDLE_VALUE) + { + FindClose(m_file); + } +} + +const char* DirBrowser::InternNext() +{ + bool ok = false; + if (m_first) + { + ok = m_file != INVALID_HANDLE_VALUE; + m_first = false; + } + else + { + ok = FindNextFile(m_file, &m_findData) != 0; + } + if (ok) + { + return m_findData.cFileName; + } + return NULL; +} + +#else + +#ifdef DIRBROWSER_SNAPSHOT +DirBrowser::DirBrowser(const char* path, bool snapshot) +#else +DirBrowser::DirBrowser(const char* path) +#endif +{ +#ifdef DIRBROWSER_SNAPSHOT + m_snapshot = snapshot; + if (m_snapshot) + { + DirBrowser dir(path, false); + while (const char* filename = dir.Next()) + { + m_snapshot.push_back(strdup(filename)); + } + m_itSnapshot = m_snapshot.begin(); + } + else +#endif + { + m_dir = opendir(path); + } +} + +DirBrowser::~DirBrowser() +{ +#ifdef DIRBROWSER_SNAPSHOT + if (m_snapshot) + { + for (FileList::iterator it = m_snapshot.begin(); it != m_snapshot.end(); it++) + { + delete *it; + } + } + else +#endif + { + if (m_dir) + { + closedir(m_dir); + } + } +} + +const char* DirBrowser::InternNext() +{ +#ifdef DIRBROWSER_SNAPSHOT + if (m_snapshot) + { + return m_itSnapshot == m_snapshot.end() ? NULL : *m_itSnapshot++; + } + else +#endif + { + if (m_dir) + { + m_findData = readdir(m_dir); + if (m_findData) + { + return m_findData->d_name; + } + } + return NULL; + } +} +#endif + +const char* DirBrowser::Next() +{ + const char* filename = NULL; + for (filename = InternNext(); filename && (!strcmp(filename, ".") || !strcmp(filename, "..")); ) + { + filename = InternNext(); + } + return filename; +} diff --git a/daemon/util/FileSystem.h b/daemon/util/FileSystem.h new file mode 100644 index 00000000..4b45f11f --- /dev/null +++ b/daemon/util/FileSystem.h @@ -0,0 +1,103 @@ +/* + * This file is part of nzbget + * + * Copyright (C) 2007-2015 Andrey Prygunkov + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $Revision$ + * $Date$ + * + */ + + +#ifndef FILESYSTEM_H +#define FILESYSTEM_H + +#include "NString.h" + +class FileSystem +{ +public: + static CString GetLastErrorMessage(); + static char* BaseFileName(const char* filename); + static bool SameFilename(const char* filename1, const char* filename2); + static void NormalizePathSeparators(char* path); + static bool LoadFileIntoBuffer(const char* fileName, char** buffer, int* bufferLength); + static bool SaveBufferIntoFile(const char* fileName, const char* buffer, int bufLen); + static bool CreateSparseFile(const char* filename, int64 size, CString& errmsg); + static bool TruncateFile(const char* filename, int size); + static void MakeValidFilename(char* filename, char cReplaceChar, bool allowSlashes); + static CString MakeUniqueFilename(const char* destDir, const char* basename); + static bool MoveFile(const char* srcFilename, const char* dstFilename); + static bool CopyFile(const char* srcFilename, const char* dstFilename); + static bool FileExists(const char* filename); + static bool FileExists(const char* path, const char* filenameWithoutPath); + static bool DirectoryExists(const char* dirFilename); + static bool CreateDirectory(const char* dirFilename); + static bool RemoveDirectory(const char* dirFilename); + static bool DeleteDirectoryWithContent(const char* dirFilename, CString& errmsg); + static bool ForceDirectories(const char* path, CString& errmsg); + static CString GetCurrentDirectory(); + static bool SetCurrentDirectory(const char* dirFilename); + static int64 FileSize(const char* filename); + static int64 FreeDiskSize(const char* path); + static bool DirEmpty(const char* dirFilename); + static bool RenameBak(const char* filename, const char* bakPart, bool removeOldExtension, CString& newName); +#ifndef WIN32 + static CString ExpandHomePath(const char* filename); + static void FixExecPermission(const char* filename); +#endif + static CString ExpandFileName(const char* filename); + static CString GetExeFileName(const char* argv0); + + /* Flush disk buffers for file with given descriptor */ + static bool FlushFileBuffers(int fileDescriptor, CString& errmsg); + + /* Flush disk buffers for file metadata (after file renaming) */ + static bool FlushDirBuffers(const char* filename, CString& errmsg); +}; + +class DirBrowser +{ +private: +#ifdef WIN32 + WIN32_FIND_DATA m_findData; + HANDLE m_file; + bool m_first; +#else + DIR* m_dir; + struct dirent* m_findData; +#endif + +#ifdef DIRBROWSER_SNAPSHOT + bool m_snapshot; + typedef std::deque FileList; + FileList m_snapshot; + FileList::iterator m_itSnapshot; +#endif + + const char* InternNext(); +public: +#ifdef DIRBROWSER_SNAPSHOT + DirBrowser(const char* path, bool snapshot = true); +#else + DirBrowser(const char* path); +#endif + ~DirBrowser(); + const char* Next(); +}; + +#endif diff --git a/daemon/util/Log.cpp b/daemon/util/Log.cpp index 5e9faa52..65d0c33f 100644 --- a/daemon/util/Log.cpp +++ b/daemon/util/Log.cpp @@ -27,6 +27,7 @@ #include "Options.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" Log* g_Log = NULL; @@ -48,7 +49,7 @@ Log::Log() m_optInit = false; m_lastWritten = 0; #ifdef DEBUG - m_extraDebug = Util::FileExists("extradebug"); + m_extraDebug = FileSystem::FileExists("extradebug"); #endif } @@ -150,11 +151,11 @@ void debug(const char* msg, ...) #ifdef HAVE_VARIADIC_MACROS if (funcname) { - tmp2.Format("%s (%s:%i:%s)", tmp1, Util::BaseFileName(filename), lineNr, funcname); + tmp2.Format("%s (%s:%i:%s)", tmp1, FileSystem::BaseFileName(filename), lineNr, funcname); } else { - tmp2.Format("%s (%s:%i)", tmp1, Util::BaseFileName(filename), lineNr); + tmp2.Format("%s (%s:%i)", tmp1, FileSystem::BaseFileName(filename), lineNr); } #else tmp2.Format("%s", tmp1); @@ -350,7 +351,7 @@ void Log::RotateLog() BString<1024> directory = g_Options->GetLogFile(); // split the full filename into path, basename and extension - char* baseName = Util::BaseFileName(directory); + char* baseName = FileSystem::BaseFileName(directory); if (baseName > directory) { baseName[-1] = '\0'; diff --git a/daemon/util/Script.cpp b/daemon/util/Script.cpp index c018b18e..5a67d965 100644 --- a/daemon/util/Script.cpp +++ b/daemon/util/Script.cpp @@ -27,6 +27,7 @@ #include "Script.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" #include "Options.h" // System global variable holding environments variables @@ -315,7 +316,7 @@ void ScriptController::PrepareArgs() } } warn("Could not found associated program for %s. Trying to execute %s directly", - extension, Util::BaseFileName(GetScript())); + extension, FileSystem::BaseFileName(GetScript())); } } #endif @@ -506,7 +507,7 @@ int ScriptController::StartProcess() { PrintMessage(Message::mkError, "Could not start %s: error %i", m_infoName, errCode); } - if (!Util::FileExists(m_script)) + if (!FileSystem::FileExists(m_script)) { PrintMessage(Message::mkError, "Could not find file %s", m_script); } @@ -586,7 +587,7 @@ int ScriptController::StartProcess() { fprintf(stdout, "[WARNING] Fixing permissions for %s\n", m_script); fflush(stdout); - Util::FixExecPermission(m_script); + FileSystem::FixExecPermission(m_script); execvp(m_script, (char* const*)m_args); } diff --git a/daemon/util/Util.cpp b/daemon/util/Util.cpp index 37329f21..6c0603bc 100644 --- a/daemon/util/Util.cpp +++ b/daemon/util/Util.cpp @@ -32,7 +32,6 @@ const char* code_revision(void); #endif #ifdef WIN32 - // getopt for WIN32: // from http://www.codeproject.com/cpp/xgetopt.asp // Original Author: Hans Dietrich (hdietrich2@hotmail.com) @@ -108,451 +107,26 @@ int getopt(int argc, char *argv[], char *optstring) return c; } - -DirBrowser::DirBrowser(const char* path) -{ - BString<1024> mask("%s%c*.*", path, (int)PATH_SEPARATOR); - m_file = FindFirstFile(mask, &m_findData); - m_first = true; -} - -DirBrowser::~DirBrowser() -{ - if (m_file != INVALID_HANDLE_VALUE) - { - FindClose(m_file); - } -} - -const char* DirBrowser::InternNext() -{ - bool ok = false; - if (m_first) - { - ok = m_file != INVALID_HANDLE_VALUE; - m_first = false; - } - else - { - ok = FindNextFile(m_file, &m_findData) != 0; - } - if (ok) - { - return m_findData.cFileName; - } - return NULL; -} - -#else - -#ifdef DIRBROWSER_SNAPSHOT -DirBrowser::DirBrowser(const char* path, bool snapshot) -#else -DirBrowser::DirBrowser(const char* path) #endif -{ -#ifdef DIRBROWSER_SNAPSHOT - m_snapshot = snapshot; - if (m_snapshot) - { - DirBrowser dir(path, false); - while (const char* filename = dir.Next()) - { - m_snapshot.push_back(strdup(filename)); - } - m_itSnapshot = m_snapshot.begin(); - } - else -#endif - { - m_dir = opendir(path); - } -} - -DirBrowser::~DirBrowser() -{ -#ifdef DIRBROWSER_SNAPSHOT - if (m_snapshot) - { - for (FileList::iterator it = m_snapshot.begin(); it != m_snapshot.end(); it++) - { - delete *it; - } - } - else -#endif - { - if (m_dir) - { - closedir(m_dir); - } - } -} - -const char* DirBrowser::InternNext() -{ -#ifdef DIRBROWSER_SNAPSHOT - if (m_snapshot) - { - return m_itSnapshot == m_snapshot.end() ? NULL : *m_itSnapshot++; - } - else -#endif - { - if (m_dir) - { - m_findData = readdir(m_dir); - if (m_findData) - { - return m_findData->d_name; - } - } - return NULL; - } -} -#endif - -const char* DirBrowser::Next() -{ - const char* filename = NULL; - for (filename = InternNext(); filename && (!strcmp(filename, ".") || !strcmp(filename, "..")); ) - { - filename = InternNext(); - } - return filename; -} char Util::VersionRevisionBuf[100]; -char* Util::BaseFileName(const char* filename) +void Util::Init() { - char* p = (char*)strrchr(filename, PATH_SEPARATOR); - char* p1 = (char*)strrchr(filename, ALT_PATH_SEPARATOR); - if (p1) +#ifndef WIN32 + if ((strlen(code_revision()) > 0) && strstr(VERSION, "testing")) { - if ((p && p < p1) || !p) - { - p = p1; - } - } - if (p) - { - return p + 1; + snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s-r%s", VERSION, code_revision()); } else - { - return (char*)filename; - } -} - -void Util::NormalizePathSeparators(char* path) -{ - for (char* p = path; *p; p++) - { - if (*p == ALT_PATH_SEPARATOR) - { - *p = PATH_SEPARATOR; - } - } -} - -bool Util::ForceDirectories(const char* path, CString& errmsg) -{ - errmsg.Clear(); - BString<1024> normPath = path; - NormalizePathSeparators(normPath); - int len = strlen(normPath); - if ((len > 0) && normPath[len-1] == PATH_SEPARATOR -#ifdef WIN32 - && len > 3 #endif - ) { - normPath[len-1] = '\0'; + snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s", VERSION); } - struct stat buffer; - bool ok = !stat(normPath, &buffer); - if (!ok && errno != ENOENT) - { - errmsg.Format("could not read information for directory %s: errno %i, %s", - *normPath, errno, *GetLastErrorMessage()); - return false; - } - - if (ok && !S_ISDIR(buffer.st_mode)) - { - errmsg.Format("path %s is not a directory", *normPath); - return false; - } - - if (!ok -#ifdef WIN32 - && strlen(normPath) > 2 -#endif - ) - { - BString<1024> parentPath = *normPath; - char* p = (char*)strrchr(parentPath, PATH_SEPARATOR); - if (p) - { -#ifdef WIN32 - if (p - parentPath == 2 && parentPath[1] == ':' && strlen(parentPath) > 2) - { - parentPath[3] = '\0'; - } - else -#endif - { - *p = '\0'; - } - if (strlen(parentPath) != strlen(path) && !ForceDirectories(parentPath, errmsg)) - { - return false; - } - } - - if (mkdir(normPath, S_DIRMODE) != 0 && errno != EEXIST) - { - errmsg.Format("could not create directory %s: %s", *normPath, *GetLastErrorMessage()); - return false; - } - - if (stat(normPath, &buffer) != 0) - { - errmsg.Format("could not read information for directory %s: %s", - *normPath, *GetLastErrorMessage()); - return false; - } - - if (!S_ISDIR(buffer.st_mode)) - { - errmsg.Format("path %s is not a directory", *normPath); - return false; - } - } - - return true; -} - -CString Util::GetCurrentDirectory() -{ - CString result; - result.Reserve(1024); -#ifdef WIN32 - ::GetCurrentDirectory(1024, result); -#else - getcwd(result, 1024); -#endif - return result; -} - -bool Util::SetCurrentDirectory(const char* dirFilename) -{ -#ifdef WIN32 - return ::SetCurrentDirectory(dirFilename); -#else - return chdir(dirFilename) == 0; -#endif -} - -bool Util::DirEmpty(const char* dirFilename) -{ - DirBrowser dir(dirFilename); - while (const char* filename = dir.Next()) - { - if (strcmp(filename, ".") && strcmp(filename, "..")) - { - return false; - } - } - return true; -} - -bool Util::LoadFileIntoBuffer(const char* fileName, char** buffer, int* bufferLength) -{ - FILE* file = fopen(fileName, FOPEN_RB); - if (!file) - { - return false; - } - - // obtain file size. - fseek(file , 0 , SEEK_END); - int size = (int)ftell(file); - rewind(file); - - // allocate memory to contain the whole file. - *buffer = (char*) malloc(size + 1); - if (!*buffer) - { - return false; - } - - // copy the file into the buffer. - fread(*buffer, 1, size, file); - - fclose(file); - - (*buffer)[size] = 0; - - *bufferLength = size + 1; - - return true; -} - -bool Util::SaveBufferIntoFile(const char* fileName, const char* buffer, int bufLen) -{ - FILE* file = fopen(fileName, FOPEN_WB); - if (!file) - { - return false; - } - - int writtenBytes = fwrite(buffer, 1, bufLen, file); - fclose(file); - - return writtenBytes == bufLen; -} - -bool Util::CreateSparseFile(const char* filename, int64 size, CString& errmsg) -{ - errmsg.Clear(); - bool ok = false; -#ifdef WIN32 - HANDLE hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_NEW, 0, NULL); - if (hFile == INVALID_HANDLE_VALUE) - { - errmsg = GetLastErrorMessage(); - return false; - } - // first try to create sparse file (supported only on NTFS partitions), - // it may fail but that's OK. - DWORD dwBytesReturned; - DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dwBytesReturned, NULL); - - LARGE_INTEGER size64; - size64.QuadPart = size; - SetFilePointerEx(hFile, size64, NULL, FILE_END); - SetEndOfFile(hFile); - CloseHandle(hFile); - ok = true; -#else - // create file - FILE* file = fopen(filename, FOPEN_AB); - if (!file) - { - errmsg = GetLastErrorMessage(); - return false; - } - fclose(file); - - // there are no reliable function to expand file on POSIX, so we must try different approaches, - // starting with the fastest one and hoping it will work - // 1) set file size using function "truncate" (this is fast, if it works) - truncate(filename, size); - // check if it worked - ok = FileSize(filename) == size; - if (!ok) - { - // 2) truncate did not work, expanding the file by writing to it (that's slow) - truncate(filename, 0); - file = fopen(filename, FOPEN_AB); - if (!file) - { - errmsg = GetLastErrorMessage(); - return false; - } - char c = '0'; - fwrite(&c, 1, size, file); - fclose(file); - ok = FileSize(filename) == size; - } -#endif - return ok; -} - -bool Util::TruncateFile(const char* filename, int size) -{ - bool ok = false; -#ifdef WIN32 - FILE *file = fopen(filename, FOPEN_RBP); - fseek(file, size, SEEK_SET); - ok = SetEndOfFile((HANDLE)_get_osfhandle(_fileno(file))) != 0; - fclose(file); -#else - ok = truncate(filename, size) == 0; -#endif - return ok; -} - -//replace bad chars in filename -void Util::MakeValidFilename(char* filename, char cReplaceChar, bool allowSlashes) -{ - const char* replaceChars = allowSlashes ? ":*?\"><\n\r\t" : "\\/:*?\"><\n\r\t"; - char* p = filename; - while (*p) - { - if (strchr(replaceChars, *p)) - { - *p = cReplaceChar; - } - if (allowSlashes && *p == ALT_PATH_SEPARATOR) - { - *p = PATH_SEPARATOR; - } - p++; - } - - // remove trailing dots and spaces. they are not allowed in directory names on windows, - // but we remove them on posix also, in a case the directory is accessed from windows via samba. - for (int len = strlen(filename); len > 0 && (filename[len - 1] == '.' || filename[len - 1] == ' '); len--) - { - filename[len - 1] = '\0'; - } -} - -// returns TRUE if the name was changed by adding duplicate-suffix -CString Util::MakeUniqueFilename(const char* destDir, const char* basename) -{ - CString result; - result.Format("%s%c%s", destDir, (int)PATH_SEPARATOR, basename); - - int dupeNumber = 0; - while (FileExists(result)) - { - dupeNumber++; - - const char* extension = strrchr(basename, '.'); - if (extension && extension != basename) - { - BString<1024> filenameWithoutExt = basename; - int end = extension - basename; - filenameWithoutExt[end < 1024 ? end : 1024-1] = '\0'; - - if (!strcasecmp(extension, ".par2")) - { - char* volExtension = strrchr(filenameWithoutExt, '.'); - if (volExtension && volExtension != filenameWithoutExt && - !strncasecmp(volExtension, ".vol", 4)) - { - *volExtension = '\0'; - extension = basename + (volExtension - filenameWithoutExt); - } - } - - result.Format("%s%c%s.duplicate%d%s", destDir, (int)PATH_SEPARATOR, - *filenameWithoutExt, dupeNumber, extension); - } - else - { - result.Format("%s%c%s.duplicate%d", destDir, (int)PATH_SEPARATOR, - basename, dupeNumber); - } - } - - return result; + // init static vars there + GetCurrentTicks(); } int64 Util::JoinInt64(uint32 Hi, uint32 Lo) @@ -567,26 +141,26 @@ void Util::SplitInt64(int64 Int64, uint32* Hi, uint32* Lo) } /* Base64 decryption is taken from - * Article "BASE 64 Decoding and Encoding Class 2003" by Jan Raddatz - * http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5099/ - */ +* Article "BASE 64 Decoding and Encoding Class 2003" by Jan Raddatz +* http://www.codeguru.com/cpp/cpp/algorithms/article.php/c5099/ +*/ const static char BASE64_DEALPHABET [128] = - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 9 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 - 19 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 - 29 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 - 39 - 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40 - 49 +{ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 9 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10 - 19 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 - 29 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 30 - 39 + 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, // 40 - 49 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, // 50 - 59 - 0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60 - 69 - 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79 + 0, 61, 0, 0, 0, 0, 1, 2, 3, 4, // 60 - 69 + 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 70 - 79 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, // 80 - 89 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, // 90 - 99 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // 100 - 109 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, // 110 - 119 49, 50, 51, 0, 0, 0, 0, 0 // 120 - 127 - }; +}; uint32 DecodeByteQuartet(char* inputBuffer, char* outputBuffer) { @@ -635,308 +209,6 @@ uint32 DecodeByteQuartet(char* inputBuffer, char* outputBuffer) return 0; } -bool Util::MoveFile(const char* srcFilename, const char* dstFilename) -{ - bool ok = rename(srcFilename, dstFilename) == 0; - -#ifndef WIN32 - if (!ok && errno == EXDEV) - { - ok = CopyFile(srcFilename, dstFilename) && remove(srcFilename) == 0; - } -#endif - - return ok; -} - -bool Util::CopyFile(const char* srcFilename, const char* dstFilename) -{ - FILE* infile = fopen(srcFilename, FOPEN_RB); - if (!infile) - { - return false; - } - - FILE* outfile = fopen(dstFilename, FOPEN_WBP); - if (!outfile) - { - fclose(infile); - return false; - } - - static const int BUFFER_SIZE = 1024 * 50; - char* buffer = (char*)malloc(BUFFER_SIZE); - - int cnt = BUFFER_SIZE; - while (cnt == BUFFER_SIZE) - { - cnt = (int)fread(buffer, 1, BUFFER_SIZE, infile); - fwrite(buffer, 1, cnt, outfile); - } - - fclose(infile); - fclose(outfile); - free(buffer); - - return true; -} - -bool Util::FileExists(const char* filename) -{ -#ifdef WIN32 - // we use a native windows call because c-lib function "stat" fails on windows if file date is invalid - WIN32_FIND_DATA findData; - HANDLE handle = FindFirstFile(filename, &findData); - if (handle != INVALID_HANDLE_VALUE) - { - bool exists = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0; - FindClose(handle); - return exists; - } - return false; -#else - struct stat buffer; - bool exists = !stat(filename, &buffer) && S_ISREG(buffer.st_mode); - return exists; -#endif -} - -bool Util::FileExists(const char* path, const char* filenameWithoutPath) -{ - BString<1024> fullFilename("%s%c%s", path, (int)PATH_SEPARATOR, filenameWithoutPath); - bool exists = Util::FileExists(fullFilename); - return exists; -} - -bool Util::DirectoryExists(const char* dirFilename) -{ -#ifdef WIN32 - // we use a native windows call because c-lib function "stat" fails on windows if file date is invalid - WIN32_FIND_DATA findData; - HANDLE handle = FindFirstFile(dirFilename, &findData); - if (handle != INVALID_HANDLE_VALUE) - { - bool exists = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; - FindClose(handle); - return exists; - } - return false; -#else - struct stat buffer; - bool exists = !stat(dirFilename, &buffer) && S_ISDIR(buffer.st_mode); - return exists; -#endif -} - -bool Util::CreateDirectory(const char* dirFilename) -{ - mkdir(dirFilename, S_DIRMODE); - return DirectoryExists(dirFilename); -} - -bool Util::RemoveDirectory(const char* dirFilename) -{ -#ifdef WIN32 - return _rmdir(dirFilename) == 0; -#else - return remove(dirFilename) == 0; -#endif -} - -bool Util::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 (strcmp(filename, ".") && strcmp(filename, "..")) - { - if (Util::DirectoryExists(fullFilename)) - { - del = DeleteDirectoryWithContent(fullFilename, errmsg); - } - else - { - del = remove(fullFilename) == 0; - } - ok &= del; - if (!del && errmsg.Empty()) - { - errmsg.Format("could not delete %s: %s", *fullFilename, *GetLastErrorMessage()); - } - } - } - - del = RemoveDirectory(dirFilename); - ok &= del; - if (!del && errmsg.Empty()) - { - errmsg = GetLastErrorMessage(); - } - return ok; -} - -int64 Util::FileSize(const char* filename) -{ -#ifdef WIN32 - struct _stat32i64 buffer; - _stat32i64(filename, &buffer); -#else - struct stat buffer; - stat(filename, &buffer); -#endif - return buffer.st_size; -} - -int64 Util::FreeDiskSize(const char* path) -{ -#ifdef WIN32 - ULARGE_INTEGER free, dummy; - if (GetDiskFreeSpaceEx(path, &free, &dummy, &dummy)) - { - return free.QuadPart; - } -#else - struct statvfs diskdata; - if (!statvfs(path, &diskdata)) - { - return (int64)diskdata.f_frsize * (int64)diskdata.f_bavail; - } -#endif - return -1; -} - -bool Util::RenameBak(const char* filename, const char* bakPart, bool removeOldExtension, CString& newName) -{ - BString<1024> changedFilename; - - if (removeOldExtension) - { - changedFilename = filename; - char* extension = strrchr(changedFilename, '.'); - if (extension) - { - *extension = '\0'; - } - } - - newName.Format("%s.%s", removeOldExtension ? *changedFilename : filename, bakPart); - - int i = 2; - struct stat buffer; - while (!stat(newName, &buffer)) - { - newName.Format("%s.%i.%s", removeOldExtension ? *changedFilename : filename, i++, bakPart); - } - - bool ok = !rename(filename, newName); - return ok; -} - -#ifndef WIN32 -CString Util::ExpandHomePath(const char* filename) -{ - CString result; - - if (filename && (filename[0] == '~') && (filename[1] == '/')) - { - // expand home-dir - - char* home = getenv("HOME"); - if (!home) - { - struct passwd *pw = getpwuid(getuid()); - if (pw) - { - home = pw->pw_dir; - } - } - - if (!home) - { - return filename; - } - - if (home[strlen(home)-1] == '/') - { - result.Format("%s%s", home, filename + 2); - } - else - { - result.Format("%s/%s", home, filename + 2); - } - } - else - { - result.Append(filename ? filename : ""); - } - - return result; -} -#endif - -CString Util::ExpandFileName(const char* filename) -{ - CString result; - result.Reserve(1024); - -#ifdef WIN32 - _fullpath(result, filename, 1024); -#else - if (filename[0] != '\0' && filename[0] != '/') - { - char curDir[MAX_PATH + 1]; - getcwd(curDir, sizeof(curDir) - 1); // 1 char reserved for adding backslash - int offset = 0; - if (filename[0] == '.' && filename[1] == '/') - { - offset += 2; - } - result.Format("%s/%s", curDir, filename + offset); - } - else - { - result = filename; - } -#endif - - return result; -} - -CString Util::GetExeFileName(const char* argv0) -{ - CString exename; - exename.Reserve(1024); - exename[1024 - 1] = '\0'; - -#ifdef WIN32 - GetModuleFileName(NULL, exename, 1024); -#else - // Linux - int r = readlink("/proc/self/exe", exename, 1024 - 1); - if (r > 0) - { - return exename; - } - // FreeBSD - r = readlink("/proc/curproc/file", exename, 1024 - 1); - if (r > 0) - { - return exename; - } - - exename = ExpandFileName(argv0); -#endif - - return exename; -} - CString Util::FormatSize(int64 fileSize) { CString result; @@ -988,15 +260,6 @@ CString Util::FormatSpeed(int bytesPerSecond) return result; } -bool Util::SameFilename(const char* filename1, const char* filename2) -{ -#ifdef WIN32 - return strcasecmp(filename1, filename2) == 0; -#else - return strcmp(filename1, filename2) == 0; -#endif -} - bool Util::MatchFileExt(const char* filename, const char* extensionList, const char* listSeparator) { int filenameLen = strlen(filename); @@ -1022,43 +285,6 @@ bool Util::MatchFileExt(const char* filename, const char* extensionList, const c return false; } -#ifndef WIN32 -void Util::FixExecPermission(const char* filename) -{ - struct stat buffer; - bool ok = !stat(filename, &buffer); - if (ok) - { - buffer.st_mode = buffer.st_mode | S_IXUSR | S_IXGRP | S_IXOTH; - chmod(filename, buffer.st_mode); - } -} -#endif - -CString Util::GetLastErrorMessage() -{ - BString<1024> msg; - strerror_r(errno, msg, msg.Capacity()); - return *msg; -} - -void Util::Init() -{ -#ifndef WIN32 - if ((strlen(code_revision()) > 0) && strstr(VERSION, "testing")) - { - snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s-r%s", VERSION, code_revision()); - } - else -#endif - { - snprintf(VersionRevisionBuf, sizeof(VersionRevisionBuf), "%s", VERSION); - } - - // init static vars there - GetCurrentTicks(); -} - bool Util::SplitCommandLine(const char* commandLine, char*** argv) { int argCount = 0; @@ -1498,59 +724,6 @@ int Util::NumberOfCpuCores() return -1; } -bool Util::FlushFileBuffers(int fileDescriptor, CString& errmsg) -{ -#ifdef WIN32 - BOOL ok = ::FlushFileBuffers((HANDLE)_get_osfhandle(fileDescriptor)); - if (!ok) - { - errmsg.Reserve(1024); - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - errmsg, 1024, NULL); - } - return ok; -#else -#ifdef HAVE_FULLFSYNC - int ret = fcntl(fileDescriptor, F_FULLFSYNC) == -1 ? 1 : 0; -#elif HAVE_FDATASYNC - int ret = fdatasync(fileDescriptor); -#else - int ret = fsync(fileDescriptor); -#endif - if (ret != 0) - { - errmsg = GetLastErrorMessage(); - } - return ret == 0; -#endif -} - -bool Util::FlushDirBuffers(const char* filename, CString& errmsg) -{ - BString<1024> parentPath = filename; - const char* fileMode = FOPEN_RBP; - -#ifndef WIN32 - char* p = (char*)strrchr(parentPath, PATH_SEPARATOR); - if (p) - { - *p = '\0'; - } - fileMode = FOPEN_RB; -#endif - - FILE* file = fopen(parentPath, fileMode); - if (!file) - { - errmsg = GetLastErrorMessage(); - return false; - } - bool ok = FlushFileBuffers(fileno(file), errmsg); - fclose(file); - return ok; -} - int64 Util::GetCurrentTicks() { #ifdef WIN32 diff --git a/daemon/util/Util.h b/daemon/util/Util.h index f82deea4..25972a41 100644 --- a/daemon/util/Util.h +++ b/daemon/util/Util.h @@ -34,92 +34,25 @@ extern char *optarg; int getopt(int argc, char *argv[], char *optstring); #endif -class DirBrowser -{ -private: -#ifdef WIN32 - WIN32_FIND_DATA m_findData; - HANDLE m_file; - bool m_first; -#else - DIR* m_dir; - struct dirent* m_findData; -#endif - -#ifdef DIRBROWSER_SNAPSHOT - bool m_snapshot; - typedef std::deque FileList; - FileList m_snapshot; - FileList::iterator m_itSnapshot; -#endif - - const char* InternNext(); -public: -#ifdef DIRBROWSER_SNAPSHOT - DirBrowser(const char* path, bool snapshot = true); -#else - DirBrowser(const char* path); -#endif - ~DirBrowser(); - const char* Next(); -}; - class Util { public: - static char* BaseFileName(const char* filename); - static void NormalizePathSeparators(char* path); - static bool LoadFileIntoBuffer(const char* fileName, char** buffer, int* bufferLength); - static bool SaveBufferIntoFile(const char* fileName, const char* buffer, int bufLen); - static bool CreateSparseFile(const char* filename, int64 size, CString& errmsg); - static bool TruncateFile(const char* filename, int size); - static void MakeValidFilename(char* filename, char cReplaceChar, bool allowSlashes); - static CString MakeUniqueFilename(const char* destDir, const char* basename); - static bool MoveFile(const char* srcFilename, const char* dstFilename); - static bool CopyFile(const char* srcFilename, const char* dstFilename); - static bool FileExists(const char* filename); - static bool FileExists(const char* path, const char* filenameWithoutPath); - static bool DirectoryExists(const char* dirFilename); - static bool CreateDirectory(const char* dirFilename); - static bool RemoveDirectory(const char* dirFilename); - static bool DeleteDirectoryWithContent(const char* dirFilename, CString& errmsg); - static bool ForceDirectories(const char* path, CString& errmsg); - static CString GetCurrentDirectory(); - static bool SetCurrentDirectory(const char* dirFilename); - static int64 FileSize(const char* filename); - static int64 FreeDiskSize(const char* path); - static bool DirEmpty(const char* dirFilename); - static bool RenameBak(const char* filename, const char* bakPart, bool removeOldExtension, CString& newName); -#ifndef WIN32 - static CString ExpandHomePath(const char* filename); - static void FixExecPermission(const char* filename); -#endif - static CString ExpandFileName(const char* filename); - static CString GetExeFileName(const char* argv0); + static bool MatchFileExt(const char* filename, const char* extensionList, const char* listSeparator); static CString FormatSpeed(int bytesPerSecond); static CString FormatSize(int64 fileSize); - static bool SameFilename(const char* filename1, const char* filename2); - static bool MatchFileExt(const char* filename, const char* extensionList, const char* listSeparator); - static CString GetLastErrorMessage(); static int64 GetCurrentTicks(); - /* Flush disk buffers for file with given descriptor */ - static bool FlushFileBuffers(int fileDescriptor, CString& errmsg); - - /* Flush disk buffers for file metadata (after file renaming) */ - static bool FlushDirBuffers(const char* filename, CString& errmsg); - /* - * Split command line int arguments. + * Split command line into arguments. * Uses spaces and single quotation marks as separators. * Returns bool if sucessful or false if bad escaping was detected. * Parameter "argv" may be NULL if only a syntax check is needed. * Parsed parameters returned in Array "argv", which contains at least one element. * The last element in array is NULL. - * Restrictions: the number of arguments is limited to 100 and each arguments must + * Restrictions: the number of arguments is limited to 100 and each argument must * be maximum 1024 chars long. * If these restrictions are exceeded, only first 100 arguments and only first 1024 - * for each argument are returned (the functions still returns "true"). + * for each argument are returned (the function still returns "true"). */ static bool SplitCommandLine(const char* commandLine, char*** argv); diff --git a/daemon/windows/WinConsole.cpp b/daemon/windows/WinConsole.cpp index a81b93b6..365d97a0 100644 --- a/daemon/windows/WinConsole.cpp +++ b/daemon/windows/WinConsole.cpp @@ -26,11 +26,12 @@ #include "nzbget.h" #include "Log.h" #include "Options.h" -#include "Util.h" #include "FeedCoordinator.h" #include "StatMeter.h" #include "WinConsole.h" #include "WinService.h" +#include "FileSystem.h" +#include "Util.h" #include "resource.h" extern Options* g_Options; @@ -438,10 +439,10 @@ void WinConsole::ShowInExplorer(const char* filename) char fileName2[MAX_PATH + 1]; strncpy(fileName2, filename, MAX_PATH); fileName2[MAX_PATH] = '\0'; - Util::NormalizePathSeparators(fileName2); + FileSystem::NormalizePathSeparators(fileName2); if (*fileName2 && fileName2[strlen(fileName2) - 1] == PATH_SEPARATOR) fileName2[strlen(fileName2) - 1] = '\0'; // trim slash - if (!Util::FileExists(fileName2) && !Util::DirectoryExists(fileName2)) + if (!FileSystem::FileExists(fileName2) && !FileSystem::DirectoryExists(fileName2)) { BString<1024> message("Directory or file %s doesn't exist (yet).", fileName2); MessageBox(m_trayWindow, message, "Information", MB_ICONINFORMATION); @@ -868,7 +869,7 @@ void WinConsole::ShowCategoryDir(int catIndex) { BString<1024> categoryDir; categoryDir.Set(category->GetName()); - Util::MakeValidFilename(categoryDir, '_', true); + FileSystem::MakeValidFilename(categoryDir, '_', true); destDir.Format("%s%s", g_Options->GetDestDir(), *categoryDir); } @@ -897,7 +898,7 @@ void WinConsole::SetupConfigFile() BString<1024> filename("%s\\NZBGet\\nzbget.conf", commonAppDataPath); BString<1024> appDataPath("%s\\NZBGet", commonAppDataPath); - Util::CreateDirectory(appDataPath); + FileSystem::CreateDirectory(appDataPath); BString<1024> confTemplateFilename("%s\\nzbget.conf.template", g_Options->GetAppDir()); CopyFile(confTemplateFilename, filename, FALSE); @@ -905,7 +906,7 @@ void WinConsole::SetupConfigFile() // set MainDir in the config-file int size = 0; char* config = NULL; - if (Util::LoadFileIntoBuffer(filename, &config, &size)) + if (FileSystem::LoadFileIntoBuffer(filename, &config, &size)) { const char* SIGNATURE = "MainDir=${AppDir}\\downloads"; char* p = strstr(config, SIGNATURE); @@ -927,7 +928,7 @@ void WinConsole::SetupConfigFile() // create default destination directory (which is not created on start automatically) BString<1024> completeDir("%s\\NZBGet\\complete", commonAppDataPath); - Util::CreateDirectory(completeDir); + FileSystem::CreateDirectory(completeDir); } void WinConsole::SetupScripts() @@ -938,19 +939,16 @@ void WinConsole::SetupScripts() SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, appDataPath); BString<1024> destDir("%s\\NZBGet\\scripts", appDataPath); - Util::CreateDirectory(destDir); + FileSystem::CreateDirectory(destDir); BString<1024> srcDir("%s\\scripts", g_Options->GetAppDir()); DirBrowser dir(srcDir); while (const char* filename = dir.Next()) { - if (strcmp(filename, ".") && strcmp(filename, "..")) - { - BString<1024> srcFullFilename("%s\\%s", srcDir, filename); - BString<1024> dstFullFilename("%s\\%s", destDir, filename); - CopyFile(srcFullFilename, dstFullFilename, FALSE); - } + BString<1024> srcFullFilename("%s\\%s", srcDir, filename); + BString<1024> dstFullFilename("%s\\%s", destDir, filename); + CopyFile(srcFullFilename, dstFullFilename, FALSE); } } @@ -1015,14 +1013,14 @@ void WinConsole::ResetFactoryDefaults() // try to delete the directory int retry = 10; - while (retry > 0 && Util::DirectoryExists(path) && - !Util::DeleteDirectoryWithContent(path, errmsg)) + while (retry > 0 && FileSystem::DirectoryExists(path) && + !FileSystem::DeleteDirectoryWithContent(path, errmsg)) { usleep(200 * 1000); retry--; } - if (Util::DirectoryExists(path)) + 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), @@ -1035,9 +1033,9 @@ void WinConsole::ResetFactoryDefaults() path.Format("%s\\nzbget.conf", g_Options->GetAppDir()); remove(path); - errmsg = Util::GetLastErrorMessage(); + errmsg = FileSystem::GetLastErrorMessage(); - if (Util::FileExists(path)) + 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), @@ -1049,9 +1047,9 @@ void WinConsole::ResetFactoryDefaults() path.Format("%s\\NZBGet\\nzbget.conf", commonAppDataPath); remove(path); - errmsg = Util::GetLastErrorMessage(); + errmsg = FileSystem::GetLastErrorMessage(); - if (Util::FileExists(path)) + 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), diff --git a/nzbget.vcxproj b/nzbget.vcxproj index f830f791..4aa94626 100644 --- a/nzbget.vcxproj +++ b/nzbget.vcxproj @@ -153,6 +153,7 @@ + Create Create @@ -243,6 +244,7 @@ + diff --git a/tests/postprocess/DupeMatcherTest.cpp b/tests/postprocess/DupeMatcherTest.cpp index 24bde9f3..e1b4911c 100644 --- a/tests/postprocess/DupeMatcherTest.cpp +++ b/tests/postprocess/DupeMatcherTest.cpp @@ -29,6 +29,7 @@ #include "Options.h" #include "DupeMatcher.h" +#include "FileSystem.h" #include "TestUtil.h" TEST_CASE("Disk matcher", "[Par][DupeMatcher][Slow][TestData]") @@ -42,11 +43,11 @@ TEST_CASE("Disk matcher", "[Par][DupeMatcher][Slow][TestData]") // prepare directories std::string dupe1(TestUtil::WorkingDir() + "/dupe1"); - REQUIRE(Util::ForceDirectories(dupe1.c_str(), errmsg)); + REQUIRE(FileSystem::ForceDirectories(dupe1.c_str(), errmsg)); TestUtil::CopyAllFiles(dupe1, TestUtil::TestDataDir() + "/parchecker"); std::string dupe2(TestUtil::WorkingDir() + "/dupe2"); - REQUIRE(Util::ForceDirectories(dupe2.c_str(), errmsg)); + REQUIRE(FileSystem::ForceDirectories(dupe2.c_str(), errmsg)); TestUtil::CopyAllFiles(dupe2, TestUtil::TestDataDir() + "/parchecker"); remove((dupe2 + "/testfile.nfo").c_str()); @@ -54,12 +55,12 @@ TEST_CASE("Disk matcher", "[Par][DupeMatcher][Slow][TestData]") std::string rardupe2(TestUtil::TestDataDir() + "/dupematcher2"); std::string nondupe(TestUtil::WorkingDir() + "/nondupe"); - REQUIRE(Util::ForceDirectories(nondupe.c_str(), errmsg)); + REQUIRE(FileSystem::ForceDirectories(nondupe.c_str(), errmsg)); TestUtil::CopyAllFiles(nondupe, TestUtil::TestDataDir() + "/parchecker"); remove((nondupe + "/testfile.dat").c_str()); // now test - int64 expectedSize = Util::FileSize((dupe1 + "/testfile.dat").c_str()); + int64 expectedSize = FileSystem::FileSize((dupe1 + "/testfile.dat").c_str()); DupeMatcher dupe1Matcher(dupe1.c_str(), expectedSize); CHECK(dupe1Matcher.Prepare()); diff --git a/tests/postprocess/ParRenamerTest.cpp b/tests/postprocess/ParRenamerTest.cpp index 3c19304e..59bd4fbf 100644 --- a/tests/postprocess/ParRenamerTest.cpp +++ b/tests/postprocess/ParRenamerTest.cpp @@ -29,6 +29,7 @@ #include "Options.h" #include "ParRenamer.h" +#include "FileSystem.h" #include "TestUtil.h" class ParRenamerMock: public ParRenamer @@ -81,7 +82,7 @@ TEST_CASE("Par-renamer: rename successful", "[Par][ParRenamer][Slow][TestData]") Options options(&cmdOpts, NULL); ParRenamerMock parRenamer; - Util::MoveFile((TestUtil::WorkingDir() + "/testfile.dat").c_str(), (TestUtil::WorkingDir() + "/123456").c_str()); + FileSystem::MoveFile((TestUtil::WorkingDir() + "/testfile.dat").c_str(), (TestUtil::WorkingDir() + "/123456").c_str()); parRenamer.Execute(); REQUIRE(parRenamer.GetStatus() == ParRenamer::psSuccess); @@ -96,7 +97,7 @@ TEST_CASE("Par-renamer: detecting missing", "[Par][ParRenamer][Slow][TestData]") Options options(&cmdOpts, NULL); ParRenamerMock parRenamer; - Util::MoveFile((TestUtil::WorkingDir() + "/testfile.dat").c_str(), (TestUtil::WorkingDir() + "/123456").c_str()); + FileSystem::MoveFile((TestUtil::WorkingDir() + "/testfile.dat").c_str(), (TestUtil::WorkingDir() + "/123456").c_str()); parRenamer.SetDetectMissing(true); REQUIRE(remove((TestUtil::WorkingDir() + "/testfile.nfo").c_str()) == 0); parRenamer.Execute(); diff --git a/tests/suite/TestMain.cpp b/tests/suite/TestMain.cpp index d4088b7a..0f549831 100644 --- a/tests/suite/TestMain.cpp +++ b/tests/suite/TestMain.cpp @@ -31,6 +31,7 @@ #include "Thread.h" #include "Log.h" #include "Util.h" +#include "FileSystem.h" #include "TestUtil.h" int TestMain(int argc, char * argv[]) @@ -42,7 +43,7 @@ int TestMain(int argc, char * argv[]) if (argc == 1) { printf("Unit and integration tests for nzbget-%s.\nUse '%s -tests [quick]' to run only quick tests or '%s -h' for more options.\n", - Util::VersionRevision(), Util::BaseFileName(argv[0]), Util::BaseFileName(argv[0])); + Util::VersionRevision(), FileSystem::BaseFileName(argv[0]), FileSystem::BaseFileName(argv[0])); } // shift arguments for catch to not see the parameter "-tests" diff --git a/tests/suite/TestUtil.cpp b/tests/suite/TestUtil.cpp index ea68f652..60564c8a 100644 --- a/tests/suite/TestUtil.cpp +++ b/tests/suite/TestUtil.cpp @@ -27,7 +27,7 @@ #include "catch.h" -#include "Util.h" +#include "FileSystem.h" #include "TestUtil.h" bool TestUtil::m_usedWorkingDir = false; @@ -45,18 +45,18 @@ void TestUtil::Init(const char* argv0) { m_usedWorkingDir = false; - CString filename = Util::GetExeFileName(argv0); - Util::NormalizePathSeparators(filename); + CString filename = FileSystem::GetExeFileName(argv0); + FileSystem::NormalizePathSeparators(filename); char* end = strrchr(filename, PATH_SEPARATOR); if (end) *end = '\0'; DataDir = filename; DataDir += "/testdata"; - if (!Util::DirectoryExists(DataDir.c_str())) + if (!FileSystem::DirectoryExists(DataDir.c_str())) { DataDir = filename; DataDir += "/tests/testdata"; } - if (!Util::DirectoryExists(DataDir.c_str())) + if (!FileSystem::DirectoryExists(DataDir.c_str())) { DataDir = ""; } @@ -95,16 +95,16 @@ void TestUtil::PrepareWorkingDir(const std::string templateDir) CString errmsg; int retries = 20; - Util::DeleteDirectoryWithContent(workDir.c_str(), errmsg); - while (Util::DirectoryExists(workDir.c_str()) && retries > 0) + FileSystem::DeleteDirectoryWithContent(workDir.c_str(), errmsg); + while (FileSystem::DirectoryExists(workDir.c_str()) && retries > 0) { usleep(1000 * 100); retries--; - Util::DeleteDirectoryWithContent(workDir.c_str(), errmsg); + FileSystem::DeleteDirectoryWithContent(workDir.c_str(), errmsg); } - REQUIRE_FALSE(Util::DirectoryExists(workDir.c_str())); - Util::CreateDirectory(workDir.c_str()); - REQUIRE(Util::DirEmpty(workDir.c_str())); + REQUIRE_FALSE(FileSystem::DirectoryExists(workDir.c_str())); + FileSystem::CreateDirectory(workDir.c_str()); + REQUIRE(FileSystem::DirEmpty(workDir.c_str())); CopyAllFiles(workDir, srcDir); } @@ -116,14 +116,14 @@ void TestUtil::CopyAllFiles(const std::string destDir, const std::string srcDir) { std::string srcFile(srcDir + "/" + filename); std::string dstFile(destDir + "/" + filename); - REQUIRE(Util::CopyFile(srcFile.c_str(), dstFile.c_str())); + REQUIRE(FileSystem::CopyFile(srcFile.c_str(), dstFile.c_str())); } } void TestUtil::CleanupWorkingDir() { CString errmsg; - Util::DeleteDirectoryWithContent(WorkingDir().c_str(), errmsg); + FileSystem::DeleteDirectoryWithContent(WorkingDir().c_str(), errmsg); } void TestUtil::DisableCout()