1
0
mirror of https://github.com/microsoft/DirectXTex synced 2025-01-14 03:20:17 +00:00

clean up image files on failed write

This commit is contained in:
Chuck Walbourn 2016-02-10 14:11:05 -08:00
parent 408208cc78
commit 246bebc464
3 changed files with 41 additions and 3 deletions

View File

@ -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<DWORD>( 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;
}

View File

@ -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;
}

View File

@ -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