FileSystem: Make POSIXLock moveable

This commit is contained in:
Stenzek 2024-12-03 17:03:52 +10:00
parent 5b6e3a952c
commit d93c713fb7
No known key found for this signature in database
2 changed files with 22 additions and 0 deletions

View File

@ -16,6 +16,7 @@
#include <cstring> #include <cstring>
#include <limits> #include <limits>
#include <numeric> #include <numeric>
#include <utility>
#ifdef __APPLE__ #ifdef __APPLE__
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
@ -2823,6 +2824,10 @@ static bool SetLock(int fd, bool lock)
return res; return res;
} }
FileSystem::POSIXLock::POSIXLock() : m_fd(-1)
{
}
FileSystem::POSIXLock::POSIXLock(int fd) : m_fd(fd) FileSystem::POSIXLock::POSIXLock(int fd) : m_fd(fd)
{ {
if (!SetLock(m_fd, true)) if (!SetLock(m_fd, true))
@ -2835,10 +2840,21 @@ FileSystem::POSIXLock::POSIXLock(std::FILE* fp) : m_fd(fileno(fp))
m_fd = -1; m_fd = -1;
} }
FileSystem::POSIXLock::POSIXLock(POSIXLock&& move)
{
m_fd = std::exchange(move.m_fd, -1);
}
FileSystem::POSIXLock::~POSIXLock() FileSystem::POSIXLock::~POSIXLock()
{ {
if (m_fd >= 0) if (m_fd >= 0)
SetLock(m_fd, false); SetLock(m_fd, false);
} }
FileSystem::POSIXLock& FileSystem::POSIXLock::operator=(POSIXLock&& move)
{
m_fd = std::exchange(move.m_fd, -1);
return *this;
}
#endif #endif

View File

@ -159,10 +159,16 @@ void DiscardAtomicRenamedFile(AtomicRenamedFile& file);
class POSIXLock class POSIXLock
{ {
public: public:
POSIXLock();
POSIXLock(int fd); POSIXLock(int fd);
POSIXLock(std::FILE* fp); POSIXLock(std::FILE* fp);
POSIXLock(POSIXLock&& move);
POSIXLock(const POSIXLock&) = delete;
~POSIXLock(); ~POSIXLock();
POSIXLock& operator=(POSIXLock&& move);
POSIXLock& operator=(const POSIXLock&) = delete;
private: private:
int m_fd; int m_fd;
}; };