Skip to content

Commit

Permalink
Remove getDataDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
sgourdas committed Aug 4, 2024
1 parent ece4096 commit 1adefbc
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 83 deletions.
2 changes: 1 addition & 1 deletion include/downloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Downloader
typedef std::vector<std::pair<std::string, std::string>> Options;

public: // functions
Downloader();
Downloader(std::string downloadDir);
virtual ~Downloader();

void close();
Expand Down
28 changes: 2 additions & 26 deletions include/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,6 @@ typedef std::vector<std::string> FeedCategories;
*/
std::string getCurrentDirectory();

/**
* Return the data directory
*
* The data directory is the default directory where downloaded files
* should be saved (it can be overriden via the options parameter of
* `kiwix::Downloader::startDownload()`).
*
* Its path can vary and is determined as follows:
*
* * `$KIWIX_DATA_DIR` if `$KIWIX_DATA_DIR` environment variable set, *otherwise...*
* * On Windows:
*
* * `$APPDATA/kiwix` if environment variable `$APPDATA` set, *otherwise...*
* * `$USERPROFILE/kiwix` if environment variable `$USERPROFILE` set, *otherwise...*
*
* * On other Operating Systems:
*
* * `$XDG_DATA_HOME/kiwix` if environment variable `$XDG_DATA_HOME` set, *otherwise...*
* * `$HOME/.local/share/kiwx` if environment variable `$HOME` set, *otherwise...*
*
* * Current working directory.
*
* @return the path of the data directory (UTF-8 encoded)
*/
std::string getDataDirectory();

/** Return the path of the executable
*
* Some application may be packaged in auto extractible archive (Appimage) and the
Expand Down Expand Up @@ -214,6 +188,8 @@ bool fileExists(const std::string& path);
*/
bool fileReadable(const std::string& path);

bool makeDirectory(const std::string& path);

/** Provides mimetype from filename.
*
* This function provides mimetype from file-name.
Expand Down
10 changes: 5 additions & 5 deletions src/aria2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ void pauseAnyActiveDownloads(const std::string& ariaSessionFilePath)

} // unnamed namespace

Aria2::Aria2():
Aria2::Aria2(std::string downloadDir):

Check warning on line 58 in src/aria2.cpp

View check run for this annotation

Codecov / codecov/patch

src/aria2.cpp#L58

Added line #L58 was not covered by tests
mp_aria(nullptr),
m_port(42042),
m_secret(getNewRpcSecret())
m_secret(getNewRpcSecret()),
m_downloadDir(downloadDir)
{
m_downloadDir = getDataDirectory();
makeDirectory(m_downloadDir);
std::vector<const char*> callCmd;

std::string rpc_port = "--rpc-listen-port=" + to_string(m_port);
std::string download_dir = "--dir=" + getDataDirectory();
std::string session_file = appendToDirectory(getDataDirectory(), "kiwix.session");
std::string download_dir = "--dir=" + m_downloadDir;
std::string session_file = appendToDirectory(m_downloadDir, "kiwix.session");
pauseAnyActiveDownloads(session_file);
std::string session = "--save-session=" + session_file;
std::string inputFile = "--input-file=" + session_file;
Expand Down
2 changes: 1 addition & 1 deletion src/aria2.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Aria2
std::string doRequest(const MethodCall& methodCall);

public:
Aria2();
Aria2(std::string downloadDir);
virtual ~Aria2() = default;
void close();

Expand Down
4 changes: 2 additions & 2 deletions src/downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ void Download::cancelDownload()
}

/* Constructor */
Downloader::Downloader() :
mp_aria(new Aria2())
Downloader::Downloader(std::string downloadDir) :

Check warning on line 128 in src/downloader.cpp

View check run for this annotation

Codecov / codecov/patch

src/downloader.cpp#L128

Added line #L128 was not covered by tests
mp_aria(new Aria2(downloadDir))
{
try {
for (auto gid : mp_aria->tellWaiting()) {
Expand Down
48 changes: 1 addition & 47 deletions src/tools/pathTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ bool kiwix::fileReadable(const std::string& path)
#endif
}

bool makeDirectory(const std::string& path)
bool kiwix::makeDirectory(const std::string& path)

Check warning on line 323 in src/tools/pathTools.cpp

View check run for this annotation

Codecov / codecov/patch

src/tools/pathTools.cpp#L323

Added line #L323 was not covered by tests
{
#ifdef _WIN32
int status = _wmkdir(Utf8ToWide(path).c_str());
Expand Down Expand Up @@ -438,52 +438,6 @@ std::string kiwix::getCurrentDirectory()
return ret;
}

std::string kiwix::getDataDirectory()
{
// Try to get the dataDir from the `KIWIX_DATA_DIR` env var
#ifdef _WIN32
wchar_t* cDataDir = ::_wgetenv(L"KIWIX_DATA_DIR");
if (cDataDir != nullptr) {
return WideToUtf8(cDataDir);
}
#else
char* cDataDir = ::getenv("KIWIX_DATA_DIR");
if (cDataDir != nullptr) {
return cDataDir;
}
#endif

// Compute the dataDir from the user directory.
std::string dataDir;
#ifdef _WIN32
cDataDir = ::_wgetenv(L"APPDATA");
if (cDataDir == nullptr)
cDataDir = ::_wgetenv(L"USERPROFILE");
if (cDataDir != nullptr)
dataDir = WideToUtf8(cDataDir);
#else
cDataDir = ::getenv("XDG_DATA_HOME");
if (cDataDir != nullptr) {
dataDir = cDataDir;
} else {
cDataDir = ::getenv("HOME");
if (cDataDir != nullptr) {
dataDir = cDataDir;
dataDir = appendToDirectory(dataDir, ".local");
dataDir = appendToDirectory(dataDir, "share");
}
}
#endif
if (!dataDir.empty()) {
dataDir = appendToDirectory(dataDir, "kiwix");
makeDirectory(dataDir);
return dataDir;
}

// Let's use the currentDirectory
return getCurrentDirectory();
}

static std::map<std::string, std::string> extMimeTypes = {
{ "html", "text/html"},
{ "htm", "text/html"},
Expand Down
1 change: 0 additions & 1 deletion src/tools/pathTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ std::wstring Utf8ToWide(const std::string& str);

unsigned int getFileSize(const std::string& path);
std::string getFileSizeAsString(const std::string& path);
bool makeDirectory(const std::string& path);
std::string makeTmpDirectory();
bool copyFile(const std::string& sourcePath, const std::string& destPath);
bool writeTextFile(const std::string& path, const std::string& content);
Expand Down

0 comments on commit 1adefbc

Please sign in to comment.