Lock open project.

This commit is contained in:
Jonathan Moore Liles 2008-05-29 15:06:22 -05:00
parent 0643dac88a
commit 7b03a7fb4d
2 changed files with 59 additions and 10 deletions

View File

@ -27,6 +27,7 @@ project state belongs to Timeline and other classes. */
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#include "Loggable.H" #include "Loggable.H"
#include "Project.H" #include "Project.H"
@ -45,9 +46,55 @@ extern TLE *tle;
#define PROJECT_VERSION "0.28.0" #define PROJECT_VERSION "0.28.0"
#include "util/debug.h" #include "util/debug.h"
char Project::_name[256]; char Project::_name[256];
char Project::_path[512]; char Project::_path[512];
bool Project::_is_open = false; 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 void
Project::set_name ( const char *name ) Project::set_name ( const char *name )
@ -68,16 +115,6 @@ Project::set_name ( const char *name )
*s = ' '; *s = ' ';
} }
static int
exists ( const char *name )
{
struct stat st;
return 0 == stat( name, &st );
}
#include <errno.h>
bool bool
Project::close ( void ) Project::close ( void )
{ {
@ -94,6 +131,8 @@ Project::close ( void )
*Project::_name = '\0'; *Project::_name = '\0';
release_lock( ".lock" );
return true; return true;
} }
@ -180,6 +219,12 @@ Project::open ( const char *name )
chdir( name ); chdir( name );
if ( ! get_lock( ".lock" ) )
{
WARNING( "Could not open project: locked by another process!" );
return false;
}
if ( ! Loggable::open( "history" ) ) if ( ! Loggable::open( "history" ) )
FATAL( "error opening journal" ); FATAL( "error opening journal" );

View File

@ -23,10 +23,14 @@ const char user_template_dir[] = "~/.non-daw/templates";
class Project class Project
{ {
static int _lockfd;
static bool _is_open; static bool _is_open;
static char _name[256]; static char _name[256];
static char _path[512]; static char _path[512];
static bool get_lock ( const char *filename );
static void release_lock ( const char *filename );
public: public:
static bool write_info ( void ); static bool write_info ( void );