Browse Source

Merge pull request #2261 from CJentzsch/createDirIfNotPresent

create directory if not existent when writing to file
cl-refactor
Gav Wood 10 years ago
parent
commit
afd4af51ee
  1. 8
      libdevcore/CommonIO.cpp

8
libdevcore/CommonIO.cpp

@ -98,9 +98,9 @@ string dev::contentsString(string const& _file)
void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename) void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename)
{ {
namespace fs = boost::filesystem;
if (_writeDeleteRename) if (_writeDeleteRename)
{ {
namespace fs = boost::filesystem;
fs::path tempPath = fs::unique_path(_file + "-%%%%%%"); fs::path tempPath = fs::unique_path(_file + "-%%%%%%");
writeFile(tempPath.string(), _data, false); writeFile(tempPath.string(), _data, false);
// will delete _file if it exists // will delete _file if it exists
@ -108,10 +108,14 @@ void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDe
} }
else else
{ {
// create directory if not existent
fs::path p(_file);
fs::create_directories(p.parent_path());
ofstream s(_file, ios::trunc | ios::binary); ofstream s(_file, ios::trunc | ios::binary);
s.write(reinterpret_cast<char const*>(_data.data()), _data.size()); s.write(reinterpret_cast<char const*>(_data.data()), _data.size());
if (!s) if (!s)
BOOST_THROW_EXCEPTION(FileError()); BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + _file));
} }
} }

Loading…
Cancel
Save