Browse Source

[windows] handle unexpected conditions (#1185)

pull/1301/head
R4SAS 6 years ago
parent
commit
7d0d421724
  1. 41
      libi2pd/FS.cpp

41
libi2pd/FS.cpp

@ -11,6 +11,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <shlobj.h> #include <shlobj.h>
#include <windows.h>
#endif #endif
#include "Base.h" #include "Base.h"
@ -47,18 +48,39 @@ namespace fs {
} }
#if defined(WIN32) || defined(_WIN32) #if defined(WIN32) || defined(_WIN32)
char localAppData[MAX_PATH]; char localAppData[MAX_PATH];
// check executable directory first // check executable directory first
GetModuleFileName (NULL, localAppData, MAX_PATH); if(!GetModuleFileName(NULL, localAppData, MAX_PATH))
{
#if defined(WIN32_APP)
MessageBox(NULL, TEXT("Unable to get application path!"), TEXT("I2Pd: error"), MB_ICONERROR | MB_OK);
#else
fprintf(stderr, "Error: Unable to get application path!");
#endif
exit(1);
}
else
{
auto execPath = boost::filesystem::path(localAppData).parent_path(); auto execPath = boost::filesystem::path(localAppData).parent_path();
// if config file exists in .exe's folder use it // if config file exists in .exe's folder use it
if(boost::filesystem::exists(execPath/"i2pd.conf")) // TODO: magic string if(boost::filesystem::exists(execPath/"i2pd.conf")) // TODO: magic string
dataDir = execPath.string (); dataDir = execPath.string ();
else else // otherwise %appdata%
{ {
// otherwise %appdata% if(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, localAppData) != S_OK)
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, localAppData); {
#if defined(WIN32_APP)
MessageBox(NULL, TEXT("Unable to get AppData path!"), TEXT("I2Pd: error"), MB_ICONERROR | MB_OK);
#else
fprintf(stderr, "Error: Unable to get AppData path!");
#endif
exit(1);
}
else
dataDir = std::string(localAppData) + "\\" + appName; dataDir = std::string(localAppData) + "\\" + appName;
} }
}
return; return;
#elif defined(MAC_OSX) #elif defined(MAC_OSX)
char *home = getenv("HOME"); char *home = getenv("HOME");
@ -74,8 +96,8 @@ namespace fs {
dataDir = std::string (ext) + "/" + appName; dataDir = std::string (ext) + "/" + appName;
return; return;
} }
// otherwise use /data/files
#endif #endif
// otherwise use /data/files
char *home = getenv("HOME"); char *home = getenv("HOME");
if (isService) { if (isService) {
dataDir = "/var/lib/" + appName; dataDir = "/var/lib/" + appName;
@ -91,9 +113,11 @@ namespace fs {
bool Init() { bool Init() {
if (!boost::filesystem::exists(dataDir)) if (!boost::filesystem::exists(dataDir))
boost::filesystem::create_directory(dataDir); boost::filesystem::create_directory(dataDir);
std::string destinations = DataDirPath("destinations"); std::string destinations = DataDirPath("destinations");
if (!boost::filesystem::exists(destinations)) if (!boost::filesystem::exists(destinations))
boost::filesystem::create_directory(destinations); boost::filesystem::create_directory(destinations);
std::string tags = DataDirPath("tags"); std::string tags = DataDirPath("tags");
if (!boost::filesystem::exists(tags)) if (!boost::filesystem::exists(tags))
boost::filesystem::create_directory(tags); boost::filesystem::create_directory(tags);
@ -124,7 +148,8 @@ namespace fs {
uint32_t GetLastUpdateTime (const std::string & path) uint32_t GetLastUpdateTime (const std::string & path)
{ {
if (!boost::filesystem::exists(path)) return 0; if (!boost::filesystem::exists(path))
return 0;
boost::system::error_code ec; boost::system::error_code ec;
auto t = boost::filesystem::last_write_time (path, ec); auto t = boost::filesystem::last_write_time (path, ec);
return ec ? 0 : t; return ec ? 0 : t;
@ -138,8 +163,8 @@ namespace fs {
bool CreateDirectory (const std::string& path) bool CreateDirectory (const std::string& path)
{ {
if (boost::filesystem::exists(path) && if (boost::filesystem::exists(path) && boost::filesystem::is_directory (boost::filesystem::status (path)))
boost::filesystem::is_directory (boost::filesystem::status (path))) return true; return true;
return boost::filesystem::create_directory(path); return boost::filesystem::create_directory(path);
} }

Loading…
Cancel
Save