mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-05-02 14:16:16 -04:00
- moved lock handle functions to rsdir.h/cc
- created a scope guard to manage file lock handles - added lock gards to PGP keyring read/writes. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-OpenPGP@5216 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
1885fb66c4
commit
f30a3f1b16
7 changed files with 185 additions and 98 deletions
|
@ -27,6 +27,7 @@
|
|||
// Includes for directory creation.
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "util/rsdir.h"
|
||||
|
@ -851,6 +852,118 @@ std::string RsDirUtil::makePath(const std::string &path1, const std::string &pat
|
|||
return path;
|
||||
}
|
||||
|
||||
int RsDirUtil::createLockFile(const std::string& lock_file_path,int& lock_handle)
|
||||
{
|
||||
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#ifndef WINDOWS_SYS
|
||||
// Suspended. The user should make sure he's not already using the file descriptor.
|
||||
// if(lock_handle != -1)
|
||||
// close(lock_handle);
|
||||
|
||||
// open the file in write mode, create it if necessary, truncate it (it should be empty)
|
||||
lock_handle = open(lock_file_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
|
||||
if(lock_handle == -1)
|
||||
{
|
||||
std::cerr << "Could not open lock file " << lock_file_path.c_str() << std::flush;
|
||||
perror(NULL);
|
||||
return 2;
|
||||
}
|
||||
|
||||
// see "man fcntl" for the details, in short: non blocking lock creation on the whole file contents
|
||||
struct flock lockDetails;
|
||||
lockDetails.l_type = F_WRLCK;
|
||||
lockDetails.l_whence = SEEK_SET;
|
||||
lockDetails.l_start = 0;
|
||||
lockDetails.l_len = 0;
|
||||
|
||||
if(fcntl(lock_handle, F_SETLK, &lockDetails) == -1)
|
||||
{
|
||||
int fcntlErr = errno;
|
||||
std::cerr << "Could not request lock on file " << lock_file_path.c_str() << std::flush;
|
||||
perror(NULL);
|
||||
|
||||
// there's no lock so let's release the file handle immediately
|
||||
close(lock_handle);
|
||||
lock_handle = -1;
|
||||
|
||||
if(fcntlErr == EACCES || fcntlErr == EAGAIN)
|
||||
return 1;
|
||||
else
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#else
|
||||
// Suspended. The user should make sure he's not already using the file descriptor.
|
||||
//
|
||||
// if (lock_handle) {
|
||||
// CloseHandle(lock_handle);
|
||||
// }
|
||||
|
||||
std::wstring wlockFile;
|
||||
librs::util::ConvertUtf8ToUtf16(lock_file_path, wlockFile);
|
||||
|
||||
// open the file in write mode, create it if necessary
|
||||
lock_handle = CreateFile(wlockFile.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
|
||||
|
||||
if (lock_handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
DWORD lasterror = GetLastError();
|
||||
|
||||
std::cerr << "Could not open lock file " << lock_file_path.c_str() << std::endl;
|
||||
std::cerr << "Last error: " << lasterror << std::endl << std::flush;
|
||||
perror(NULL);
|
||||
|
||||
if (lasterror == ERROR_SHARING_VIOLATION || lasterror == ERROR_ACCESS_DENIED)
|
||||
return 1;
|
||||
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
}
|
||||
|
||||
void RsDirUtil::releaseLockFile(int lockHandle)
|
||||
{
|
||||
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#ifndef WINDOWS_SYS
|
||||
if(lockHandle != -1)
|
||||
{
|
||||
close(lockHandle);
|
||||
lockHandle = -1;
|
||||
}
|
||||
#else
|
||||
if(lockHandle)
|
||||
{
|
||||
CloseHandle(lockHandle);
|
||||
lockHandle = 0;
|
||||
}
|
||||
#endif
|
||||
/******************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
}
|
||||
|
||||
RsStackFileLock::RsStackFileLock(const std::string& file_path)
|
||||
{
|
||||
while(RsDirUtil::createLockFile(file_path,_file_handle))
|
||||
{
|
||||
std::cerr << "Cannot acquire file lock " << file_path << ", waiting 1 sec." << std::endl;
|
||||
#ifdef WINDOWS_SYS
|
||||
Sleep(1000) ;
|
||||
#else
|
||||
sleep(1) ;
|
||||
#endif
|
||||
}
|
||||
std::cerr << "Acquired file handle " << _file_handle << ", lock file:" << file_path << std::endl;
|
||||
}
|
||||
RsStackFileLock::~RsStackFileLock()
|
||||
{
|
||||
RsDirUtil::releaseLockFile(_file_handle) ;
|
||||
std::cerr << "Released file lock with handle " << _file_handle << std::endl;
|
||||
}
|
||||
|
||||
#if 0 // NOT ENABLED YET!
|
||||
/************************* WIDE STRING ***************************/
|
||||
/************************* WIDE STRING ***************************/
|
||||
|
|
|
@ -36,6 +36,21 @@ class RsThread;
|
|||
|
||||
#include <retroshare/rstypes.h>
|
||||
|
||||
// This is a scope guard on a given file. Works like a mutex. Is blocking.
|
||||
// We could do that in another way: derive RsMutex into RsLockFileMutex, and
|
||||
// use RsStackMutex on it transparently. Only issue: this will cost little more
|
||||
// because of the multiple inheritance.
|
||||
//
|
||||
class RsStackFileLock
|
||||
{
|
||||
public:
|
||||
RsStackFileLock(const std::string& file_path) ;
|
||||
~RsStackFileLock() ;
|
||||
|
||||
private:
|
||||
int _file_handle ;
|
||||
};
|
||||
|
||||
namespace RsDirUtil {
|
||||
|
||||
std::string getTopDir(const std::string&);
|
||||
|
@ -70,6 +85,15 @@ bool getFileHash(const std::string& filepath,std::string &hash, uint64_t &size
|
|||
|
||||
Sha1CheckSum sha1sum(uint8_t *data,uint32_t size) ;
|
||||
|
||||
// Creates a lock file with given path, and returns the lock handle
|
||||
// returns:
|
||||
// 0: Success
|
||||
// 1: Another instance already has the lock
|
||||
// 2 : Unexpected error
|
||||
int createLockFile(const std::string& lock_file_path,int& lock_handle) ;
|
||||
|
||||
// Removes the lock file with specified handle.
|
||||
void releaseLockFile(int lockHandle) ;
|
||||
|
||||
std::wstring getWideTopDir(std::wstring);
|
||||
std::wstring getWideRootDir(std::wstring);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue