Fabcoin Core  0.16.2
P2P Digital Currency
TransientDirectory.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 <thread>
23 #include <boost/filesystem.hpp>
24 #include "Exceptions.h"
25 #include "TransientDirectory.h"
26 #include "CommonIO.h"
27 #include "Log.h"
28 using namespace std;
29 using namespace dev;
30 namespace fs = boost::filesystem;
31 
32 TransientDirectory::TransientDirectory():
33  TransientDirectory((boost::filesystem::temp_directory_path() / "eth_transient" / toString(FixedHash<4>::random())).string())
34 {}
35 
36 TransientDirectory::TransientDirectory(std::string const& _path):
37  m_path(_path)
38 {
39  // we never ever want to delete a directory (including all its contents) that we did not create ourselves.
40  if (boost::filesystem::exists(m_path))
41  BOOST_THROW_EXCEPTION(FileError());
42 
43  if (!fs::create_directories(m_path))
44  BOOST_THROW_EXCEPTION(FileError());
45  DEV_IGNORE_EXCEPTIONS(fs::permissions(m_path, fs::owner_all));
46 }
47 
49 {
50  boost::system::error_code ec;
51  fs::remove_all(m_path, ec);
52  if (!ec)
53  return;
54 
55  // In some cases, antivirus runnig on Windows will scan all the newly created directories.
56  // As a consequence, directory is locked and can not be deleted immediately.
57  // Retry after 10 milliseconds usually is successful.
58  // This will help our tests run smoothly in such environment.
59  this_thread::sleep_for(chrono::milliseconds(10));
60 
61  ec.clear();
62  fs::remove_all(m_path, ec);
63  if (!ec)
64  {
65  cwarn << "Failed to delete directory '" << m_path << "': " << ec.message();
66  }
67 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Definition: Log.h:35
std::hash for asio::adress
Definition: Common.h:323
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
#define cwarn
Definition: Log.h:304
temporary directory implementation It creates temporary directory in the given path.
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:63