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/types.h>
#include <fcntl.h>
#include <errno.h>
#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 <errno.h>
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" );

View File

@ -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 );