From 246bebc464f3fe89b4ddce058c633df6f4623721 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Wed, 10 Feb 2016 14:11:05 -0800 Subject: [PATCH] clean up image files on failed write --- DirectXTex/DirectXTexDDS.cpp | 34 ++++++++++++++++++++++++++++++++-- DirectXTex/DirectXTexWIC.cpp | 8 ++++++++ MIT.txt | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/DirectXTex/DirectXTexDDS.cpp b/DirectXTex/DirectXTexDDS.cpp index 8783a33..eb122d6 100644 --- a/DirectXTex/DirectXTexDDS.cpp +++ b/DirectXTex/DirectXTexDDS.cpp @@ -17,6 +17,32 @@ #include "dds.h" +namespace +{ + class auto_delete_file + { + public: + auto_delete_file(HANDLE hFile) : m_handle(hFile) {} + ~auto_delete_file() + { + if (m_handle) + { + FILE_DISPOSITION_INFO info = {0}; + info.DeleteFileW = TRUE; + (void)SetFileInformationByHandle(m_handle, FileDispositionInfo, &info, sizeof(info)); + } + } + + void clear() { m_handle = 0; } + + private: + HANDLE m_handle; + + auto_delete_file(const auto_delete_file&) DIRECTX_CTOR_DELETE; + auto_delete_file& operator=(const auto_delete_file&) DIRECTX_CTOR_DELETE; + }; +} + namespace DirectX { @@ -1841,15 +1867,17 @@ HRESULT SaveToDDSFile( const Image* images, size_t nimages, const TexMetadata& m // Create file and write header #if (_WIN32_WINNT >= _WIN32_WINNT_WIN8) - ScopedHandle hFile( safe_handle( CreateFile2( szFile, GENERIC_WRITE, 0, CREATE_ALWAYS, 0 ) ) ); + ScopedHandle hFile( safe_handle( CreateFile2( szFile, GENERIC_WRITE | DELETE, 0, CREATE_ALWAYS, 0 ) ) ); #else - ScopedHandle hFile( safe_handle( CreateFileW( szFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0 ) ) ); + ScopedHandle hFile( safe_handle( CreateFileW( szFile, GENERIC_WRITE | DELETE, 0, 0, CREATE_ALWAYS, 0, 0 ) ) ); #endif if ( !hFile ) { return HRESULT_FROM_WIN32( GetLastError() ); } + auto_delete_file delonfail(hFile.get()); + DWORD bytesWritten; if ( !WriteFile( hFile.get(), header, static_cast( required ), &bytesWritten, 0 ) ) { @@ -2003,6 +2031,8 @@ HRESULT SaveToDDSFile( const Image* images, size_t nimages, const TexMetadata& m return E_FAIL; } + delonfail.clear(); + return S_OK; } diff --git a/DirectXTex/DirectXTexWIC.cpp b/DirectXTex/DirectXTexWIC.cpp index ce0c4db..3419a49 100644 --- a/DirectXTex/DirectXTexWIC.cpp +++ b/DirectXTex/DirectXTexWIC.cpp @@ -1185,7 +1185,11 @@ HRESULT SaveToWICFile( const Image& image, DWORD flags, REFGUID containerFormat, hr = _EncodeSingleFrame( image, flags, containerFormat, stream.Get(), targetFormat, setCustomProps ); if ( FAILED(hr) ) + { + stream.Reset(); + DeleteFileW( szFile ); return hr; + } return S_OK; } @@ -1217,7 +1221,11 @@ HRESULT SaveToWICFile( const Image* images, size_t nimages, DWORD flags, REFGUID hr = _EncodeSingleFrame( images[0], flags, containerFormat, stream.Get(), targetFormat, setCustomProps ); if ( FAILED(hr) ) + { + stream.Reset(); + DeleteFileW( szFile ); return hr; + } return S_OK; } diff --git a/MIT.txt b/MIT.txt index e8cc726..96e5e14 100644 --- a/MIT.txt +++ b/MIT.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Microsoft Corp +Copyright (c) 2016 Microsoft Corp Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software