From 4565afcb51b0950ed54f429490b6fbe7c15386e8 Mon Sep 17 00:00:00 2001 From: CJentzsch Date: Tue, 23 Jun 2015 13:14:45 +0200 Subject: [PATCH] create directory if not existent when writing to file --- libdevcore/CommonIO.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index cfe7b8a6b..055b645ed 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -98,9 +98,9 @@ string dev::contentsString(string const& _file) void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename) { + namespace fs = boost::filesystem; if (_writeDeleteRename) { - namespace fs = boost::filesystem; fs::path tempPath = fs::unique_path(_file + "-%%%%%%"); writeFile(tempPath.string(), _data, false); // will delete _file if it exists @@ -108,10 +108,14 @@ void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDe } else { + // create directory if not existent + fs::path p(_file); + fs::create_directories(p.parent_path()); + ofstream s(_file, ios::trunc | ios::binary); s.write(reinterpret_cast(_data.data()), _data.size()); if (!s) - BOOST_THROW_EXCEPTION(FileError()); + BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + _file)); } }