From 7b03a7fb4dc11268691d9365ba49a3ad61189c3d Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Thu, 29 May 2008 15:06:22 -0500 Subject: [PATCH] Lock open project. --- Timeline/Project.C | 65 +++++++++++++++++++++++++++++++++++++++------- Timeline/Project.H | 4 +++ 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Timeline/Project.C b/Timeline/Project.C index 6230cff..cbe837e 100644 --- a/Timeline/Project.C +++ b/Timeline/Project.C @@ -27,6 +27,7 @@ project state belongs to Timeline and other classes. */ #include #include #include +#include #include "Loggable.H" #include "Project.H" @@ -45,9 +46,55 @@ extern TLE *tle; #define PROJECT_VERSION "0.28.0" #include "util/debug.h" + + + char Project::_name[256]; char Project::_path[512]; bool Project::_is_open = false; +int Project::_lockfd = 0; + + + +bool +Project::get_lock ( const char *filename ) +{ + struct flock fl; + + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = 0; + fl.l_len = 0; + + assert( ! _lockfd ); + + _lockfd = ::creat( filename, 0777 ); + + if ( fcntl( _lockfd, F_SETLK, &fl ) != 0 ) + return false; + + return true; +} + +void +Project::release_lock ( const char *filename ) +{ + unlink( filename ); + + ::close( _lockfd ); + + _lockfd = 0; +} + +static int +exists ( const char *name ) +{ + struct stat st; + + return 0 == stat( name, &st ); +} + + void Project::set_name ( const char *name ) @@ -68,16 +115,6 @@ Project::set_name ( const char *name ) *s = ' '; } -static int -exists ( const char *name ) -{ - struct stat st; - - return 0 == stat( name, &st ); -} - -#include - bool Project::close ( void ) { @@ -94,6 +131,8 @@ Project::close ( void ) *Project::_name = '\0'; + release_lock( ".lock" ); + return true; } @@ -180,6 +219,12 @@ Project::open ( const char *name ) chdir( name ); + if ( ! get_lock( ".lock" ) ) + { + WARNING( "Could not open project: locked by another process!" ); + return false; + } + if ( ! Loggable::open( "history" ) ) FATAL( "error opening journal" ); diff --git a/Timeline/Project.H b/Timeline/Project.H index 35a6e67..1aab871 100644 --- a/Timeline/Project.H +++ b/Timeline/Project.H @@ -23,10 +23,14 @@ const char user_template_dir[] = "~/.non-daw/templates"; class Project { + static int _lockfd; static bool _is_open; static char _name[256]; static char _path[512]; + static bool get_lock ( const char *filename ); + static void release_lock ( const char *filename ); + public: static bool write_info ( void );