Xmipp  v3.23.11-Nereus
Public Member Functions | Protected Attributes | List of all members
FileLock Class Reference

#include <xmipp_filename.h>

Public Member Functions

 FileLock ()
 constructor More...
 
 FileLock (int fileno)
 
void lock (int fileno=0)
 Lock file. More...
 
void lock (FILE *file)
 
void unlock ()
 Unlock. More...
 

Protected Attributes

struct flock fl
 
bool islocked
 
int filenum
 

Detailed Description

Lock/unlock files

Definition at line 749 of file xmipp_filename.h.

Constructor & Destructor Documentation

◆ FileLock() [1/2]

FileLock::FileLock ( )
inline

constructor

Definition at line 764 of file xmipp_filename.h.

765  {
766 #ifndef __MINGW32__
767  fl.l_type = F_WRLCK; /* F_RDLCK, F_WRLCK, F_UNLCK */
768  fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
769  fl.l_start = 0; /* Offset from l_whence */
770  fl.l_len = 0; /* length, 0 = to EOF */
771  fl.l_pid = getpid(); /* our PID */
772 
773  islocked = false;
774  filenum = 0;
775 #endif
776  }
struct flock fl

◆ FileLock() [2/2]

FileLock::FileLock ( int  fileno)
inline

Definition at line 778 of file xmipp_filename.h.

779  {
780  FileLock();
781  this->filenum = fileno;
782  }
FileLock()
constructor

Member Function Documentation

◆ lock() [1/2]

void FileLock::lock ( int  fileno = 0)

Lock file.

Definition at line 883 of file xmipp_filename.cpp.

884 {
885 #ifndef __MINGW32__
886  if (islocked)
887  unlock();
888 
889  if (_fileno != 0)
890  filenum = _fileno;
891 
892  fl.l_type = F_WRLCK;
893  fcntl(filenum, F_SETLKW, &fl);
894  islocked = true;
895 #endif
896 
897 }
void unlock()
Unlock.
struct flock fl

◆ lock() [2/2]

void FileLock::lock ( FILE *  file)

Definition at line 899 of file xmipp_filename.cpp.

900 {
901  if (islocked)
902  unlock();
903 
904  if (hdlFile != NULL)
905  this->filenum = fileno(hdlFile);
906 
907 #ifdef __MINGW32__
908 
909  HANDLE hFile = (HANDLE)_get_osfhandle(filenum);
910  DWORD dwLastPos = SetFilePointer(hFile, 0, NULL, FILE_END);
911  if (LockFile(hFile, 0, 0, dwLastPos, 0) != NULL)
912  REPORT_ERROR(ERR_IO_LOCKED,"File cannot be locked.");
913 #else
914 
915  fl.l_type = F_WRLCK;
916  fcntl(filenum, F_SETLKW, &fl);
917 #endif
918 
919  islocked = true;
920 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void unlock()
Unlock.
struct flock fl
Error when locking/unloking a file.
Definition: xmipp_error.h:144

◆ unlock()

void FileLock::unlock ( )

Unlock.

Definition at line 923 of file xmipp_filename.cpp.

924 {
925  if (islocked)
926  {
927 #ifdef __MINGW32__
928  HANDLE hFile = (HANDLE)_get_osfhandle(filenum);
929  DWORD dwLastPos = SetFilePointer(hFile, 0, NULL, FILE_END);
930  if (UnlockFile(hFile, 0, 0, dwLastPos, 0) != NULL)
931  REPORT_ERROR(ERR_IO_LOCKED,"File cannot be unlocked.");
932 #else
933 
934  fl.l_type = F_UNLCK;
935  fcntl(filenum, F_SETLK, &fl);
936 #endif
937 
938  islocked = false;
939  }
940 }
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
struct flock fl
Error when locking/unloking a file.
Definition: xmipp_error.h:144

Member Data Documentation

◆ filenum

int FileLock::filenum
protected

Definition at line 759 of file xmipp_filename.h.

◆ fl

struct flock FileLock::fl
protected

Definition at line 755 of file xmipp_filename.h.

◆ islocked

bool FileLock::islocked
protected

Definition at line 758 of file xmipp_filename.h.


The documentation for this class was generated from the following files: