Make sure temp file will be cleaned up when base::Move fails

This commit is contained in:
Cheng Zhao 2015-12-02 11:36:29 +08:00
parent c3645e3f95
commit c493bec089

View file

@ -33,17 +33,20 @@ bool ScopedTemporaryFile::Init(const base::FilePath::StringType& ext) {
return true; return true;
base::ThreadRestrictions::ScopedAllowIO allow_io; base::ThreadRestrictions::ScopedAllowIO allow_io;
if (!base::CreateTemporaryFile(&path_))
base::FilePath temp_path;
if (!base::CreateTemporaryFile(&temp_path))
return false; return false;
if (ext.empty()) #if defined(OS_WIN)
return true;
// Keep the original extension. // Keep the original extension.
path_ = temp_path.AddExtension(ext); if (!ext.empty()) {
return base::Move(temp_path, path_); base::FilePath new_path = path_.AddExtension(ext);
if (!base::Move(path_, new_path))
return false;
path_ = new_path;
}
#endif
return true;
} }
bool ScopedTemporaryFile::InitFromFile(base::File* src, bool ScopedTemporaryFile::InitFromFile(base::File* src,