Some more improvements to LockedFile class.
This commit is contained in:
parent
8e64da1ea7
commit
96401c1f27
@ -34,8 +34,8 @@
|
|||||||
#define VER_LAMEXP_MINOR_HI 1
|
#define VER_LAMEXP_MINOR_HI 1
|
||||||
#define VER_LAMEXP_MINOR_LO 3
|
#define VER_LAMEXP_MINOR_LO 3
|
||||||
#define VER_LAMEXP_TYPE RC
|
#define VER_LAMEXP_TYPE RC
|
||||||
#define VER_LAMEXP_PATCH 5
|
#define VER_LAMEXP_PATCH 6
|
||||||
#define VER_LAMEXP_BUILD 1848
|
#define VER_LAMEXP_BUILD 1850
|
||||||
#define VER_LAMEXP_CONFG 1818
|
#define VER_LAMEXP_CONFG 1818
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -59,13 +59,33 @@
|
|||||||
|
|
||||||
#define VALID_HANDLE(H) (((H) != NULL) && ((H) != INVALID_HANDLE_VALUE))
|
#define VALID_HANDLE(H) (((H) != NULL) && ((H) != INVALID_HANDLE_VALUE))
|
||||||
|
|
||||||
|
static bool PROTECT_HANDLE(const HANDLE &h, const bool &lock)
|
||||||
|
{
|
||||||
|
if (SetHandleInformation(h, HANDLE_FLAG_PROTECT_FROM_CLOSE, (lock ? HANDLE_FLAG_PROTECT_FROM_CLOSE : 0U)))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static void CLOSE_HANDLE(HANDLE &h)
|
static void CLOSE_HANDLE(HANDLE &h)
|
||||||
{
|
{
|
||||||
if(VALID_HANDLE(h))
|
if(VALID_HANDLE(h))
|
||||||
{
|
{
|
||||||
|
PROTECT_HANDLE(h, false);
|
||||||
CloseHandle(h);
|
CloseHandle(h);
|
||||||
h = NULL;
|
|
||||||
}
|
}
|
||||||
|
h = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CLOSE_FILE(int &fd)
|
||||||
|
{
|
||||||
|
if (fd >= 0)
|
||||||
|
{
|
||||||
|
PROTECT_HANDLE((HANDLE)_get_osfhandle(fd), false);
|
||||||
|
_close(fd);
|
||||||
|
}
|
||||||
|
fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@ -126,7 +146,7 @@ static __forceinline void doLockFile(HANDLE &fileHandle, const QString &filePath
|
|||||||
const HANDLE hTemp = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
|
const HANDLE hTemp = CreateFileW(MUTILS_WCHR(QDir::toNativeSeparators(filePath)), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
|
||||||
if(VALID_HANDLE(hTemp))
|
if(VALID_HANDLE(hTemp))
|
||||||
{
|
{
|
||||||
fileHandle = hTemp;
|
PROTECT_HANDLE(fileHandle = hTemp, true);
|
||||||
break; /*file opened successfully*/
|
break; /*file opened successfully*/
|
||||||
}
|
}
|
||||||
if(i == 0)
|
if(i == 0)
|
||||||
@ -305,11 +325,7 @@ LockedFile::LockedFile(const QString &filePath, const bool bOwnsFile)
|
|||||||
|
|
||||||
LockedFile::~LockedFile(void)
|
LockedFile::~LockedFile(void)
|
||||||
{
|
{
|
||||||
if(m_fileDescriptor >= 0)
|
CLOSE_FILE(m_fileDescriptor);
|
||||||
{
|
|
||||||
_close(m_fileDescriptor);
|
|
||||||
m_fileDescriptor = -1;
|
|
||||||
}
|
|
||||||
if(m_bOwnsFile)
|
if(m_bOwnsFile)
|
||||||
{
|
{
|
||||||
doRemoveFile(m_filePath);
|
doRemoveFile(m_filePath);
|
||||||
@ -320,12 +336,10 @@ const QString LockedFile::filePath(void)
|
|||||||
{
|
{
|
||||||
if (m_fileDescriptor >= 0)
|
if (m_fileDescriptor >= 0)
|
||||||
{
|
{
|
||||||
const QString path = MUtils::OS::get_file_path(m_fileDescriptor);
|
if (GetFileType((HANDLE)_get_osfhandle(m_fileDescriptor)) == FILE_TYPE_UNKNOWN)
|
||||||
if (!path.isEmpty())
|
|
||||||
{
|
{
|
||||||
return path;
|
MUTILS_THROW_FMT("Failed to validate file handle!");
|
||||||
}
|
}
|
||||||
MUTILS_THROW_FMT("Failed to determine file path!");
|
|
||||||
}
|
}
|
||||||
return m_filePath;
|
return m_filePath;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user