mirror of
https://github.com/microsoft/DirectXTex.git
synced 2025-07-15 06:20:14 +02:00
clean up image files on failed write
This commit is contained in:
parent
408208cc78
commit
246bebc464
@ -17,6 +17,32 @@
|
|||||||
|
|
||||||
#include "dds.h"
|
#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
|
namespace DirectX
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -1841,15 +1867,17 @@ HRESULT SaveToDDSFile( const Image* images, size_t nimages, const TexMetadata& m
|
|||||||
|
|
||||||
// Create file and write header
|
// Create file and write header
|
||||||
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
|
#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
|
#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
|
#endif
|
||||||
if ( !hFile )
|
if ( !hFile )
|
||||||
{
|
{
|
||||||
return HRESULT_FROM_WIN32( GetLastError() );
|
return HRESULT_FROM_WIN32( GetLastError() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto_delete_file delonfail(hFile.get());
|
||||||
|
|
||||||
DWORD bytesWritten;
|
DWORD bytesWritten;
|
||||||
if ( !WriteFile( hFile.get(), header, static_cast<DWORD>( required ), &bytesWritten, 0 ) )
|
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;
|
return E_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delonfail.clear();
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1185,7 +1185,11 @@ HRESULT SaveToWICFile( const Image& image, DWORD flags, REFGUID containerFormat,
|
|||||||
|
|
||||||
hr = _EncodeSingleFrame( image, flags, containerFormat, stream.Get(), targetFormat, setCustomProps );
|
hr = _EncodeSingleFrame( image, flags, containerFormat, stream.Get(), targetFormat, setCustomProps );
|
||||||
if ( FAILED(hr) )
|
if ( FAILED(hr) )
|
||||||
|
{
|
||||||
|
stream.Reset();
|
||||||
|
DeleteFileW( szFile );
|
||||||
return hr;
|
return hr;
|
||||||
|
}
|
||||||
|
|
||||||
return S_OK;
|
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 );
|
hr = _EncodeSingleFrame( images[0], flags, containerFormat, stream.Get(), targetFormat, setCustomProps );
|
||||||
|
|
||||||
if ( FAILED(hr) )
|
if ( FAILED(hr) )
|
||||||
|
{
|
||||||
|
stream.Reset();
|
||||||
|
DeleteFileW( szFile );
|
||||||
return hr;
|
return hr;
|
||||||
|
}
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
2
MIT.txt
2
MIT.txt
@ -1,6 +1,6 @@
|
|||||||
The MIT License (MIT)
|
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
|
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
|
software and associated documentation files (the "Software"), to deal in the Software
|
||||||
|
Loading…
x
Reference in New Issue
Block a user