2021-04-10 22:48:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
|
2022-05-15 15:45:43 +00:00
|
|
|
#include <libsolutil/TemporaryDirectory.h>
|
2021-04-10 22:48:16 +00:00
|
|
|
|
2022-05-15 15:45:43 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
2021-07-22 14:03:30 +00:00
|
|
|
|
2021-07-22 18:33:01 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2021-04-10 22:48:16 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
#include <regex>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace solidity;
|
2022-05-15 15:45:43 +00:00
|
|
|
using namespace solidity::util;
|
2021-04-10 22:48:16 +00:00
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
TemporaryDirectory::TemporaryDirectory(std::string const& _prefix):
|
2021-07-20 10:17:42 +00:00
|
|
|
m_path(fs::temp_directory_path() / fs::unique_path(_prefix + "-%%%%-%%%%-%%%%-%%%%"))
|
2021-04-10 22:48:16 +00:00
|
|
|
{
|
|
|
|
// Prefix should just be a file name and not contain anything that would make us step out of /tmp.
|
2022-05-15 15:45:43 +00:00
|
|
|
solAssert(fs::path(_prefix) == fs::path(_prefix).stem(), "");
|
2021-04-10 22:48:16 +00:00
|
|
|
|
|
|
|
fs::create_directory(m_path);
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:33:01 +00:00
|
|
|
TemporaryDirectory::TemporaryDirectory(
|
2023-08-17 13:53:20 +00:00
|
|
|
std::vector<boost::filesystem::path> const& _subdirectories,
|
|
|
|
std::string const& _prefix
|
2021-07-22 18:33:01 +00:00
|
|
|
):
|
|
|
|
TemporaryDirectory(_prefix)
|
|
|
|
{
|
|
|
|
for (boost::filesystem::path const& subdirectory: _subdirectories)
|
|
|
|
{
|
2022-05-15 15:45:43 +00:00
|
|
|
solAssert(!subdirectory.is_absolute() && subdirectory.root_path() != "/", "");
|
|
|
|
solAssert(
|
2021-07-22 18:33:01 +00:00
|
|
|
m_path.lexically_relative(subdirectory).empty() ||
|
|
|
|
*m_path.lexically_relative(subdirectory).begin() != "..",
|
|
|
|
""
|
|
|
|
);
|
|
|
|
boost::filesystem::create_directories(m_path / subdirectory);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-10 22:48:16 +00:00
|
|
|
TemporaryDirectory::~TemporaryDirectory()
|
|
|
|
{
|
|
|
|
// A few paranoid sanity checks just to be extra sure we're not deleting someone's homework.
|
2022-05-15 15:45:43 +00:00
|
|
|
solAssert(m_path.string().find(fs::temp_directory_path().string()) == 0, "");
|
|
|
|
solAssert(!fs::equivalent(m_path, fs::temp_directory_path()), "");
|
|
|
|
solAssert(!fs::equivalent(m_path, m_path.root_path()), "");
|
|
|
|
solAssert(!m_path.empty(), "");
|
2021-04-10 22:48:16 +00:00
|
|
|
|
|
|
|
boost::system::error_code errorCode;
|
|
|
|
uintmax_t numRemoved = fs::remove_all(m_path, errorCode);
|
|
|
|
if (errorCode.value() != boost::system::errc::success)
|
|
|
|
{
|
2023-08-17 13:53:20 +00:00
|
|
|
std::cerr << "Failed to completely remove temporary directory '" << m_path << "'. ";
|
|
|
|
std::cerr << "Only " << numRemoved << " files were actually removed." << std::endl;
|
|
|
|
std::cerr << "Reason: " << errorCode.message() << std::endl;
|
2021-04-10 22:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-16 18:31:13 +00:00
|
|
|
|
|
|
|
TemporaryWorkingDirectory::TemporaryWorkingDirectory(fs::path const& _newDirectory):
|
|
|
|
m_originalWorkingDirectory(fs::current_path())
|
|
|
|
{
|
|
|
|
fs::current_path(_newDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryWorkingDirectory::~TemporaryWorkingDirectory()
|
|
|
|
{
|
|
|
|
fs::current_path(m_originalWorkingDirectory);
|
|
|
|
}
|