From b0d443f526cf0df4156cf3d2384772aaf24d9d2b Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Sat, 10 Sep 2016 00:09:42 -0700 Subject: [PATCH] clean up image files on failed write --- DirectXTex/DirectXTexDDS.cpp | 24 ------------------------ DirectXTex/DirectXTexTGA.cpp | 4 ++++ DirectXTex/scoped.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/DirectXTex/DirectXTexDDS.cpp b/DirectXTex/DirectXTexDDS.cpp index 24b736f..fff9d53 100644 --- a/DirectXTex/DirectXTexDDS.cpp +++ b/DirectXTex/DirectXTexDDS.cpp @@ -21,30 +21,6 @@ using namespace DirectX; namespace { - class auto_delete_file - { - public: - auto_delete_file(HANDLE hFile) : m_handle(hFile) {} - - auto_delete_file(const auto_delete_file&) = delete; - auto_delete_file& operator=(const auto_delete_file&) = delete; - - ~auto_delete_file() - { - if (m_handle) - { - FILE_DISPOSITION_INFO info = { 0 }; - info.DeleteFile = TRUE; - (void)SetFileInformationByHandle(m_handle, FileDispositionInfo, &info, sizeof(info)); - } - } - - void clear() { m_handle = 0; } - - private: - HANDLE m_handle; - }; - //------------------------------------------------------------------------------------- // Legacy format mapping table (used for DDS files without 'DX10' extended header) //------------------------------------------------------------------------------------- diff --git a/DirectXTex/DirectXTexTGA.cpp b/DirectXTex/DirectXTexTGA.cpp index 8f9b6d4..ab300b8 100644 --- a/DirectXTex/DirectXTexTGA.cpp +++ b/DirectXTex/DirectXTexTGA.cpp @@ -1310,6 +1310,8 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile) return HRESULT_FROM_WIN32(GetLastError()); } + auto_delete_file delonfail(hFile.get()); + // Determine size for TGA pixel data size_t rowPitch, slicePitch; if (convFlags & CONV_FLAGS_888) @@ -1392,5 +1394,7 @@ HRESULT DirectX::SaveToTGAFile(const Image& image, const wchar_t* szFile) } } + delonfail.clear(); + return S_OK; } diff --git a/DirectXTex/scoped.h b/DirectXTex/scoped.h index d6329b1..47f1d6c 100644 --- a/DirectXTex/scoped.h +++ b/DirectXTex/scoped.h @@ -30,3 +30,33 @@ struct handle_closer { void operator()(HANDLE h) { assert(h != INVALID_HANDLE_VA typedef public std::unique_ptr ScopedHandle; inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; } + +//--------------------------------------------------------------------------------- +struct find_closer { void operator()(HANDLE h) { assert(h != INVALID_HANDLE_VALUE); if (h) FindClose(h); } }; + +typedef public std::unique_ptr ScopedFindHandle; + +//--------------------------------------------------------------------------------- +class auto_delete_file +{ +public: + auto_delete_file(HANDLE hFile) : m_handle(hFile) {} + + auto_delete_file(const auto_delete_file&) = delete; + auto_delete_file& operator=(const auto_delete_file&) = delete; + + ~auto_delete_file() + { + if (m_handle) + { + FILE_DISPOSITION_INFO info = { 0 }; + info.DeleteFile = TRUE; + (void)SetFileInformationByHandle(m_handle, FileDispositionInfo, &info, sizeof(info)); + } + } + + void clear() { m_handle = 0; } + +private: + HANDLE m_handle; +};