Skip to content
Snippets Groups Projects
Commit 043feff8 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Log a build warning if building for Linux without OFD lock support

OFD locks are needed on Linux for appropriate multi-threaded shared file
access (using fcntl record locks to prevent corruption), so log a warning if
building for Linux without that support.

lock() now mimics DOS/Windows again: the result lock is an "all access" lock
regardless of what mode the file was open in. I'm not sure why this change was
made (commit 11b73134), but I don't think it was necessary or
appropriate (though I can't think of any immediate negative effects). At
minimum it makes the code a little more understandable and eliminates an
extra call to fcntl().
parent 4c6de442
No related branches found
No related tags found
No related merge requests found
Pipeline #7114 passed
......@@ -75,23 +75,21 @@ off_t filelength(int fd)
#undef F_OFD_SETLK
#endif
#if defined(__linux__) && !defined(F_OFD_SETLK)
#warning Linux OFD locks not enabled!
#endif
/* Sets a lock on a portion of a file */
int lock(int fd, off_t pos, off_t len)
{
#if !defined(BSD)
struct flock alock = {0};
int cmd = F_SETLK;
#if !defined(BSD)
struct flock alock = {0};
int cmd = F_SETLK;
#ifdef F_OFD_SETLK
cmd = F_OFD_SETLK;
#endif
int flags;
if((flags=fcntl(fd,F_GETFL))==-1)
return -1;
if((flags & (O_RDONLY|O_RDWR|O_WRONLY))==O_RDONLY)
alock.l_type = F_RDLCK; /* set read lock to prevent writes */
else
alock.l_type = F_WRLCK; /* set write lock to prevent all access */
alock.l_type = F_WRLCK; /* set write lock to prevent all access */
alock.l_whence = L_SET; /* SEEK_SET */
alock.l_start = pos;
alock.l_len = (int)len;
......@@ -99,19 +97,12 @@ int lock(int fd, off_t pos, off_t len)
int result = fcntl(fd, cmd, &alock);
if(result == -1 && errno != EINVAL)
return -1;
#ifdef F_OFD_SETLK
if(result == 0)
return 0;
#endif
#elif !defined(__QNX__) && !defined(__solaris__)
/* use flock (doesn't work over NFS) */
if(flock(fd,LOCK_EX|LOCK_NB)!=0 && errno != EOPNOTSUPP)
return(-1);
#endif
#if !defined(__QNX__) && !defined(__solaris__)
/* use flock (doesn't work over NFS) */
if(flock(fd,LOCK_EX|LOCK_NB)!=0 && errno != EOPNOTSUPP)
return(-1);
#endif
return(0);
return(0);
}
/* Removes a lock from a file record */
......@@ -131,13 +122,7 @@ int unlock(int fd, off_t pos, off_t len)
int result = fcntl(fd, cmd, &alock);
if(result == -1 && errno != EINVAL)
return -1;
#ifdef F_OFD_SETLK
if(result == 0)
return 0;
#endif
#endif
#if !defined(__QNX__) && !defined(__solaris__)
#elif !defined(__QNX__) && !defined(__solaris__)
/* use flock (doesn't work over NFS) */
if(flock(fd,LOCK_UN|LOCK_NB)!=0 && errno != EOPNOTSUPP)
return(-1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment