Ethereum  PoC-8
The C++ Implementation of Ethereum
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 
40 namespace fs = boost::filesystem;
41 
42 static_assert(BOOST_VERSION >= 106400, "Wrong boost headers version");
43 
44 // Should be written to only once during startup
45 static fs::path s_ethereumDatadir;
46 static fs::path s_ethereumIpcPath;
47 
48 void dev::setDataDir(fs::path const& _dataDir)
49 {
50  s_ethereumDatadir = _dataDir;
51 }
52 
53 void dev::setIpcPath(fs::path const& _ipcDir)
54 {
55  s_ethereumIpcPath = _ipcDir;
56 }
57 
58 fs::path dev::getIpcPath()
59 {
60  // Strip "geth.ipc" suffix if provided.
61  if (s_ethereumIpcPath.filename() == "geth.ipc")
62  return s_ethereumIpcPath.parent_path();
63  else
64  return s_ethereumIpcPath;
65 }
66 
67 fs::path dev::getDataDir(string _prefix)
68 {
69  if (_prefix.empty())
70  _prefix = "ethereum";
71  if (_prefix == "ethereum" && !s_ethereumDatadir.empty())
72  return s_ethereumDatadir;
73  return getDefaultDataDir(_prefix);
74 }
75 
76 fs::path dev::getDefaultDataDir(string _prefix)
77 {
78  if (_prefix.empty())
79  _prefix = "ethereum";
80 
81 #if defined(_WIN32)
82  _prefix[0] = toupper(_prefix[0]);
83  char path[1024] = "";
84  if (SHGetSpecialFolderPathA(NULL, path, CSIDL_APPDATA, true))
85  return fs::path(path) / _prefix;
86  else
87  {
88  #ifndef _MSC_VER // todo?
89  cwarn << "getDataDir(): SHGetSpecialFolderPathA() failed.";
90  #endif
91  BOOST_THROW_EXCEPTION(std::runtime_error("getDataDir() - SHGetSpecialFolderPathA() failed."));
92  }
93 #else
94  fs::path dataDirPath;
95  char const* homeDir = getenv("HOME");
96  if (!homeDir || strlen(homeDir) == 0)
97  {
98  struct passwd* pwd = getpwuid(getuid());
99  if (pwd)
100  homeDir = pwd->pw_dir;
101  }
102 
103  if (!homeDir || strlen(homeDir) == 0)
104  dataDirPath = fs::path("/");
105  else
106  dataDirPath = fs::path(homeDir);
107 
108  return dataDirPath / ("." + _prefix);
109 #endif
110 }
111 
112 fs::path dev::appendToFilename(fs::path const& _orig, string const& _suffix)
113 {
114  if (_orig.filename() == fs::path(".") || _orig.filename() == fs::path(".."))
115  return _orig / fs::path(_suffix);
116  else
117  return _orig.parent_path() / fs::path( _orig.filename().string() + _suffix);
118 }
dev::setDataDir
void setDataDir(boost::filesystem::path const &_dir)
Sets the data dir for the default ("ethereum") prefix.
dev::getIpcPath
boost::filesystem::path getIpcPath()
Definition: FileSystem.cpp:58
dev::getDefaultDataDir
boost::filesystem::path getDefaultDataDir(std::string _prefix="ethereum")
dev::setIpcPath
void setIpcPath(boost::filesystem::path const &_ipcPath)
Sets the ipc socket dir.
Common.h
dev::appendToFilename
boost::filesystem::path appendToFilename(boost::filesystem::path const &_orig, std::string const &_suffix)
dev::getDataDir
boost::filesystem::path getDataDir(std::string _prefix="ethereum")
FileSystem.h
std
Definition: FixedHash.h:393
dev
Definition: Address.cpp:21
cwarn
#define cwarn
Log.h