mirror of
https://github.com/AxioDL/LibCommon.git
synced 2026-03-30 11:47:23 -07:00
7f20a5ef97
Makes any future potential of a class unlikely.
53 lines
856 B
C++
53 lines
856 B
C++
#ifndef AXIO_CFILELOCK_H
|
|
#define AXIO_CFILELOCK_H
|
|
|
|
#include <cstdio>
|
|
#include "Common/TString.h"
|
|
|
|
#ifndef _WIN32
|
|
#include <sys/file.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
// Maintain a file handle to prevent other processes from accessing the file.
|
|
class CFileLock
|
|
{
|
|
#ifdef _WIN32
|
|
FILE* mpFile = nullptr;
|
|
#else
|
|
int mpFile = -1;
|
|
#endif
|
|
|
|
public:
|
|
CFileLock() = default;
|
|
|
|
~CFileLock()
|
|
{
|
|
Release();
|
|
}
|
|
|
|
void Lock(const TString& rkPath)
|
|
{
|
|
Release();
|
|
#ifdef _WIN32
|
|
mpFile = _wfopen(ToWChar(rkPath), L"a+");
|
|
#else
|
|
mpFile = open(*rkPath, O_CREAT | O_APPEND | O_RDWR, 0644);
|
|
flock(mpFile, LOCK_EX);
|
|
#endif
|
|
}
|
|
|
|
void Release()
|
|
{
|
|
#ifdef _WIN32
|
|
if (mpFile)
|
|
fclose(mpFile);
|
|
#else
|
|
if (mpFile != -1)
|
|
close(mpFile);
|
|
#endif
|
|
}
|
|
};
|
|
|
|
#endif // AXIO_CFILELOCK_H
|