Only check for permissions if opening a file fails

This commit is contained in:
Thomas Goyne 2014-05-26 19:37:40 -07:00
parent ac5f40a543
commit d08f4e73b4
1 changed files with 8 additions and 13 deletions

View File

@ -32,12 +32,12 @@ namespace agi {
std::unique_ptr<std::istream> Open(fs::path const& file, bool binary) { std::unique_ptr<std::istream> Open(fs::path const& file, bool binary) {
LOG_D("agi/io/open/file") << file; LOG_D("agi/io/open/file") << file;
acs::CheckFileRead(file);
auto stream = agi::make_unique<boost::filesystem::ifstream>(file, (binary ? std::ios::binary : std::ios::in)); auto stream = agi::make_unique<boost::filesystem::ifstream>(file, (binary ? std::ios::binary : std::ios::in));
if (stream->fail()) {
if (stream->fail()) acs::CheckFileRead(file);
throw IOFatal("Unknown fatal error as occurred"); throw IOFatal("Unknown fatal error occurred opening " + file.string());
}
return std::unique_ptr<std::istream>(stream.release()); return std::unique_ptr<std::istream>(stream.release());
} }
@ -47,19 +47,14 @@ Save::Save(fs::path const& file, bool binary)
, tmp_name(unique_path(file.parent_path()/(file.stem().string() + "_tmp_%%%%" + file.extension().string()))) , tmp_name(unique_path(file.parent_path()/(file.stem().string() + "_tmp_%%%%" + file.extension().string())))
{ {
LOG_D("agi/io/save/file") << file; LOG_D("agi/io/save/file") << file;
acs::CheckDirWrite(file.parent_path());
try {
acs::CheckFileWrite(file);
}
catch (fs::FileNotFound const&) {
// Not an error
}
fp = agi::make_unique<boost::filesystem::ofstream>(tmp_name, binary ? std::ios::binary : std::ios::out); fp = agi::make_unique<boost::filesystem::ofstream>(tmp_name, binary ? std::ios::binary : std::ios::out);
if (!fp->good()) if (!fp->good()) {
acs::CheckDirWrite(file.parent_path());
acs::CheckFileWrite(file);
throw fs::WriteDenied(tmp_name); throw fs::WriteDenied(tmp_name);
} }
}
Save::~Save() { Save::~Save() {
fp.reset(); // Need to close before rename on Windows to unlock the file fp.reset(); // Need to close before rename on Windows to unlock the file