FileSystem.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file FileSystem.cpp
  15. * @authors
  16. * Eric Lombrozo <elombrozo@gmail.com>
  17. * Gav Wood <i@gavwood.com>
  18. * @date 2014
  19. */
  20. #include "FileSystem.h"
  21. #include "Common.h"
  22. #include "Log.h"
  23. #if defined(_WIN32)
  24. #include <shlobj.h>
  25. #else
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <pwd.h>
  29. #include <unistd.h>
  30. #endif
  31. #include <boost/filesystem.hpp>
  32. using namespace std;
  33. using namespace dev;
  34. // Should be written to only once during startup
  35. static string s_ethereumDatadir;
  36. static string s_ethereumIpcPath;
  37. void dev::setDataDir(string const& _dataDir)
  38. {
  39. s_ethereumDatadir = _dataDir;
  40. }
  41. void dev::setIpcPath(string const& _ipcDir)
  42. {
  43. s_ethereumIpcPath = _ipcDir;
  44. }
  45. string dev::getIpcPath()
  46. {
  47. if (s_ethereumIpcPath.empty())
  48. return string(getDataDir());
  49. else
  50. {
  51. size_t socketPos = s_ethereumIpcPath.rfind("geth.ipc");
  52. if (socketPos != string::npos)
  53. return s_ethereumIpcPath.substr(0, socketPos);
  54. return s_ethereumIpcPath;
  55. }
  56. }
  57. string dev::getDataDir(string _prefix)
  58. {
  59. if (_prefix.empty())
  60. _prefix = "ethereum";
  61. if (_prefix == "ethereum" && !s_ethereumDatadir.empty())
  62. return s_ethereumDatadir;
  63. return getDefaultDataDir(_prefix);
  64. }
  65. string dev::getDefaultDataDir(string _prefix)
  66. {
  67. if (_prefix.empty())
  68. _prefix = "ethereum";
  69. #if defined(_WIN32)
  70. _prefix[0] = toupper(_prefix[0]);
  71. char path[1024] = "";
  72. if (SHGetSpecialFolderPathA(NULL, path, CSIDL_APPDATA, true))
  73. return (boost::filesystem::path(path) / _prefix).string();
  74. else
  75. {
  76. #ifndef _MSC_VER // todo?
  77. cwarn << "getDataDir(): SHGetSpecialFolderPathA() failed.";
  78. #endif
  79. BOOST_THROW_EXCEPTION(std::runtime_error("getDataDir() - SHGetSpecialFolderPathA() failed."));
  80. }
  81. #else
  82. boost::filesystem::path dataDirPath;
  83. char const* homeDir = getenv("HOME");
  84. if (!homeDir || strlen(homeDir) == 0)
  85. {
  86. struct passwd* pwd = getpwuid(getuid());
  87. if (pwd)
  88. homeDir = pwd->pw_dir;
  89. }
  90. if (!homeDir || strlen(homeDir) == 0)
  91. dataDirPath = boost::filesystem::path("/");
  92. else
  93. dataDirPath = boost::filesystem::path(homeDir);
  94. return (dataDirPath / ("." + _prefix)).string();
  95. #endif
  96. }