Fabcoin Core  0.16.2
P2P Digital Currency
FileSystem.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #include "FileSystem.h"
25 #include "Common.h"
26 #include "Log.h"
27 
28 #if defined(_WIN32)
29 #include <shlobj.h>
30 #else
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <pwd.h>
34 #include <unistd.h>
35 #endif
36 #include <boost/filesystem.hpp>
37 using namespace std;
38 using namespace dev;
39 #ifndef FASC_BUILD
40 static_assert(BOOST_VERSION == 106300, "Wrong boost headers version");
41 #endif
42 // Should be written to only once during startup
43 static string s_ethereumDatadir;
44 static string s_ethereumIpcPath;
45 
46 void dev::setDataDir(string const& _dataDir)
47 {
48  s_ethereumDatadir = _dataDir;
49 }
50 
51 void dev::setIpcPath(string const& _ipcDir)
52 {
53  s_ethereumIpcPath = _ipcDir;
54 }
55 
57 {
58  if (s_ethereumIpcPath.empty())
59  return string(getDataDir());
60  else
61  {
62  size_t socketPos = s_ethereumIpcPath.rfind("geth.ipc");
63  if (socketPos != string::npos)
64  return s_ethereumIpcPath.substr(0, socketPos);
65  return s_ethereumIpcPath;
66  }
67 }
68 
69 string dev::getDataDir(string _prefix)
70 {
71  if (_prefix.empty())
72  _prefix = "ethereum";
73  if (_prefix == "ethereum" && !s_ethereumDatadir.empty())
74  return s_ethereumDatadir;
75  return getDefaultDataDir(_prefix);
76 }
77 
78 string dev::getDefaultDataDir(string _prefix)
79 {
80  if (_prefix.empty())
81  _prefix = "ethereum";
82 
83 #if defined(_WIN32)
84  _prefix[0] = toupper(_prefix[0]);
85  char path[1024] = "";
86  if (SHGetSpecialFolderPathA(NULL, path, CSIDL_APPDATA, true))
87  return (boost::filesystem::path(path) / _prefix).string();
88  else
89  {
90  #ifndef _MSC_VER // todo?
91  cwarn << "getDataDir(): SHGetSpecialFolderPathA() failed.";
92  #endif
93  BOOST_THROW_EXCEPTION(std::runtime_error("getDataDir() - SHGetSpecialFolderPathA() failed."));
94  }
95 #else
96  boost::filesystem::path dataDirPath;
97  char const* homeDir = getenv("HOME");
98  if (!homeDir || strlen(homeDir) == 0)
99  {
100  struct passwd* pwd = getpwuid(getuid());
101  if (pwd)
102  homeDir = pwd->pw_dir;
103  }
104 
105  if (!homeDir || strlen(homeDir) == 0)
106  dataDirPath = boost::filesystem::path("/");
107  else
108  dataDirPath = boost::filesystem::path(homeDir);
109 
110  return (dataDirPath / ("." + _prefix)).string();
111 #endif
112 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string getDefaultDataDir(std::string _prefix="ethereum")
std::hash for asio::adress
Definition: Common.h:323
std::string getDataDir(std::string _prefix="ethereum")
void setIpcPath(std::string const &_ipcPath)
Sets the ipc socket dir.
std::string getIpcPath()
Definition: FileSystem.cpp:56
void setDataDir(std::string const &_dir)
Sets the data dir for the default ("ethereum") prefix.
#define cwarn
Definition: Log.h:304