Ethereum  PoC-8
The C++ Implementation of Ethereum
CommonIO.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 */
22 #include "CommonIO.h"
23 #include <libdevcore/FileSystem.h>
24 #include <iostream>
25 #include <cstdlib>
26 #include <fstream>
27 #include <stdio.h>
28 #if defined(_WIN32)
29 #include <windows.h>
30 #else
31 #include <termios.h>
32 #endif
33 #include "Exceptions.h"
34 #include <boost/filesystem.hpp>
35 using namespace std;
36 using namespace dev;
37 
38 namespace fs = boost::filesystem;
39 
40 namespace dev
41 {
42 namespace
43 {
44 void createDirectoryIfNotExistent(boost::filesystem::path const& _path)
45 {
46  if (!fs::exists(_path))
47  {
48  fs::create_directories(_path);
49  DEV_IGNORE_EXCEPTIONS(fs::permissions(_path, fs::owner_all));
50  }
51 }
52 
53 } // namespace
54 
55 string memDump(bytes const& _bytes, unsigned _width, bool _html)
56 {
57  stringstream ret;
58  if (_html)
59  ret << "<pre style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">";
60  for (unsigned i = 0; i < _bytes.size(); i += _width)
61  {
62  ret << hex << setw(4) << setfill('0') << i << " ";
63  for (unsigned j = i; j < i + _width; ++j)
64  if (j < _bytes.size())
65  if (_bytes[j] >= 32 && _bytes[j] < 127)
66  if ((char)_bytes[j] == '<' && _html)
67  ret << "&lt;";
68  else if ((char)_bytes[j] == '&' && _html)
69  ret << "&amp;";
70  else
71  ret << (char)_bytes[j];
72  else
73  ret << '?';
74  else
75  ret << ' ';
76  ret << " ";
77  for (unsigned j = i; j < i + _width && j < _bytes.size(); ++j)
78  ret << setfill('0') << setw(2) << hex << (unsigned)_bytes[j] << " ";
79  ret << "\n";
80  }
81  if (_html)
82  ret << "</pre>";
83  return ret.str();
84 }
85 
86 template <typename _T>
87 inline _T contentsGeneric(boost::filesystem::path const& _file)
88 {
89  _T ret;
90  size_t const c_elementSize = sizeof(typename _T::value_type);
91  boost::filesystem::ifstream is(_file, std::ifstream::binary);
92  if (!is)
93  return ret;
94 
95  // get length of file:
96  is.seekg(0, is.end);
97  streamoff length = is.tellg();
98  if (length == 0)
99  return ret; // do not read empty file (MSVC does not like it)
100  is.seekg(0, is.beg);
101 
102  ret.resize((length + c_elementSize - 1) / c_elementSize);
103  is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
104  return ret;
105 }
106 
107 bytes contents(boost::filesystem::path const& _file)
108 {
109  return contentsGeneric<bytes>(_file);
110 }
111 
112 bytesSec contentsSec(boost::filesystem::path const& _file)
113 {
114  bytes b = contentsGeneric<bytes>(_file);
115  bytesSec ret(b);
116  bytesRef(&b).cleanse();
117  return ret;
118 }
119 
120 string contentsString(boost::filesystem::path const& _file)
121 {
122  return contentsGeneric<string>(_file);
123 }
124 
125 void writeFile(boost::filesystem::path const& _file, bytesConstRef _data, bool _writeDeleteRename)
126 {
127  if (_writeDeleteRename)
128  {
129  fs::path tempPath = appendToFilename(_file, "-%%%%%%"); // XXX should not convert to string for this
130  writeFile(tempPath, _data, false);
131  // will delete _file if it exists
132  fs::rename(tempPath, _file);
133  }
134  else
135  {
136  createDirectoryIfNotExistent(_file.parent_path());
137 
138  boost::filesystem::ofstream s(_file, ios::trunc | ios::binary);
139  s.write(reinterpret_cast<char const*>(_data.data()), _data.size());
140  if (!s)
141  BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + _file.string()));
142  DEV_IGNORE_EXCEPTIONS(fs::permissions(_file, fs::owner_read | fs::owner_write));
143  }
144 }
145 
146 void copyDirectory(boost::filesystem::path const& _srcDir, boost::filesystem::path const& _dstDir)
147 {
148  createDirectoryIfNotExistent(_dstDir);
149 
150  for (fs::directory_iterator file(_srcDir); file != fs::directory_iterator(); ++file)
151  fs::copy_file(file->path(), _dstDir / file->path().filename());
152 }
153 
154 std::string getPassword(std::string const& _prompt)
155 {
156 #if defined(_WIN32)
157  cout << _prompt << flush;
158  // Get current Console input flags
159  HANDLE hStdin;
160  DWORD fdwSaveOldMode;
161  if ((hStdin = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
162  BOOST_THROW_EXCEPTION(
163  ExternalFunctionFailure() << errinfo_externalFunction("GetStdHandle"));
164  if (!GetConsoleMode(hStdin, &fdwSaveOldMode))
165  BOOST_THROW_EXCEPTION(
166  ExternalFunctionFailure() << errinfo_externalFunction("GetConsoleMode"));
167  // Set console flags to no echo
168  if (!SetConsoleMode(hStdin, fdwSaveOldMode & (~ENABLE_ECHO_INPUT)))
169  BOOST_THROW_EXCEPTION(
170  ExternalFunctionFailure() << errinfo_externalFunction("SetConsoleMode"));
171  // Read the string
172  std::string ret;
173  std::getline(cin, ret);
174  // Restore old input mode
175  if (!SetConsoleMode(hStdin, fdwSaveOldMode))
176  BOOST_THROW_EXCEPTION(
177  ExternalFunctionFailure() << errinfo_externalFunction("SetConsoleMode"));
178  return ret;
179 #else
180  struct termios oflags;
181  struct termios nflags;
182  char password[256];
183 
184  // disable echo in the terminal
185  tcgetattr(fileno(stdin), &oflags);
186  nflags = oflags;
187  nflags.c_lflag &= ~ECHO;
188  nflags.c_lflag |= ECHONL;
189 
190  if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
191  BOOST_THROW_EXCEPTION(ExternalFunctionFailure() << errinfo_externalFunction("tcsetattr"));
192 
193  printf("%s", _prompt.c_str());
194  if (!fgets(password, sizeof(password), stdin))
195  BOOST_THROW_EXCEPTION(ExternalFunctionFailure() << errinfo_externalFunction("fgets"));
196  password[strlen(password) - 1] = 0;
197 
198  // restore terminal
199  if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0)
200  BOOST_THROW_EXCEPTION(ExternalFunctionFailure() << errinfo_externalFunction("tcsetattr"));
201 
202 
203  return password;
204 #endif
205 }
206 
207 } // namespace dev
dev::contents
bytes contents(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:107
dev::vector_ref< byte const >
dev::errinfo_externalFunction
boost::errinfo_api_function errinfo_externalFunction
Definition: Exceptions.h:94
Exceptions.h
dev::copyDirectory
void copyDirectory(boost::filesystem::path const &_srcDir, boost::filesystem::path const &_dstDir)
Definition: CommonIO.cpp:146
dev::contentsString
string contentsString(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:120
dev::secure_vector
Definition: Common.h:78
CommonIO.h
dev::appendToFilename
boost::filesystem::path appendToFilename(boost::filesystem::path const &_orig, std::string const &_suffix)
dev::getPassword
std::string getPassword(std::string const &_prompt)
Requests the user to enter a password on the console.
Definition: CommonIO.cpp:154
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::vector_ref::data
_T * data() const
Definition: vector_ref.h:49
dev::vector_ref::size
size_t size() const
Definition: vector_ref.h:53
FileSystem.h
dev::bytesRef
vector_ref< byte > bytesRef
Definition: Common.h:73
dev::contentsSec
bytesSec contentsSec(boost::filesystem::path const &_file)
Secure variation.
Definition: CommonIO.cpp:112
dev::contentsGeneric
_T contentsGeneric(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:87
std
Definition: FixedHash.h:393
dev::memDump
string memDump(bytes const &_bytes, unsigned _width, bool _html)
Definition: CommonIO.cpp:55
dev
Definition: Address.cpp:21
dev::writeFile
void writeFile(boost::filesystem::path const &_file, bytesConstRef _data, bool _writeDeleteRename)
Definition: CommonIO.cpp:125
DEV_IGNORE_EXCEPTIONS
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:59
dev::errinfo_comment
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:69