summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <mbuland@penny-arcade.com>2018-05-25 14:17:20 -0700
committerMike Buland <mbuland@penny-arcade.com>2018-05-25 14:17:20 -0700
commit9bcbc8f919fc1c779d3b9c649dcbe3606fdbed32 (patch)
tree3f3983791f54b0b6112f7a9a887739c52d0504ea
parentf31d308827f848fdcd8ad61b89513f3c20b34e20 (diff)
downloadlibbu++-9bcbc8f919fc1c779d3b9c649dcbe3606fdbed32.tar.gz
libbu++-9bcbc8f919fc1c779d3b9c649dcbe3606fdbed32.tar.bz2
libbu++-9bcbc8f919fc1c779d3b9c649dcbe3606fdbed32.tar.xz
libbu++-9bcbc8f919fc1c779d3b9c649dcbe3606fdbed32.zip
Added locking support to Bu::File.
-rw-r--r--src/stable/file.cpp16
-rw-r--r--src/stable/file.h19
2 files changed, 35 insertions, 0 deletions
diff --git a/src/stable/file.cpp b/src/stable/file.cpp
index 35933f1..a15f394 100644
--- a/src/stable/file.cpp
+++ b/src/stable/file.cpp
@@ -9,6 +9,7 @@
9#include <errno.h> 9#include <errno.h>
10#include <sys/types.h> 10#include <sys/types.h>
11#include <sys/stat.h> 11#include <sys/stat.h>
12#include <sys/file.h>
12#include <fcntl.h> 13#include <fcntl.h>
13#include <unistd.h> 14#include <unistd.h>
14#include <time.h> 15#include <time.h>
@@ -200,6 +201,21 @@ void Bu::File::setBlocking( bool bBlocking )
200#endif 201#endif
201} 202}
202 203
204bool Bu::File::canLock() const
205{
206 return true;
207}
208
209void Bu::File::lock( bool bExclusive )
210{
211 ::flock( fd, bExclusive?LOCK_EX:LOCK_SH );
212}
213
214void Bu::File::unlock()
215{
216 ::flock( fd, LOCK_UN );
217}
218
203Bu::File Bu::File::tempFile( Bu::String &sName ) 219Bu::File Bu::File::tempFile( Bu::String &sName )
204{ 220{
205 int iXes; 221 int iXes;
diff --git a/src/stable/file.h b/src/stable/file.h
index e3497d3..6e05dc7 100644
--- a/src/stable/file.h
+++ b/src/stable/file.h
@@ -56,6 +56,25 @@ namespace Bu
56 virtual bool isBlocking(); 56 virtual bool isBlocking();
57 virtual void setBlocking( bool bBlocking=true ); 57 virtual void setBlocking( bool bBlocking=true );
58 58
59 /**
60 * Tells you if advisory locks are supported.
61 */
62 bool canLock() const;
63
64 /**
65 * Acquires an advisory lock on the file. On posix/linux this is done
66 * via flock.
67 *@param bExclusive Set to true to acquire an exclusive lock (i.e. a
68 * write lock). If this is false then a shared lock (i.e. read lock) is
69 * acquired. A file cannot have both an exclusive and shared lock.
70 */
71 void lock( bool bExclusive=true );
72
73 /**
74 * Release the held advisory lock.
75 */
76 void unlock();
77
59 enum { 78 enum {
60 // Flags 79 // Flags
61 Read = 0x01, ///< Open file for reading 80 Read = 0x01, ///< Open file for reading