Commit ca8d1f10 authored by claes's avatar claes

Lockfile for wb session

parent 5347a161
#include <sys/stat.h>
#include <iostream.h>
#include <fstream.h>
#include "pwr.h"
#include "wb_dblock.h"
#include "co_dcli.h"
vector<wb_lockfile> wb_dblock::m_lockfiles;
char *wb_dblock::lockname( char *name)
{
static pwr_tFileName fname;
strcpy( fname, name);
strcat( fname, ".lock");
dcli_translate_filename( fname, fname);
return fname;
}
bool wb_dblock::is_locked( char *name, char *user)
{
struct stat info;
if ( stat( lockname( name), &info) != -1) {
if ( user) {
ifstream fp( lockname( name));
fp.getline( user, 80);
fp.close();
}
return true;
}
return false;
}
void wb_dblock::dblock( char *name)
{
char *value;
ofstream fp( lockname( name));
value = getenv( "USER");
if ( value)
fp << value << endl;
else
fp << "Unknown" << endl;
fp.close();
wb_lockfile lf( lockname( name));
m_lockfiles.push_back(lf);
}
void wb_dblock::dbunlock( char *name)
{
pwr_tCmd cmd;
sprintf( cmd, "rm %s", lockname(name));
system( cmd);
for ( int i = 0; i < (int) m_lockfiles.size(); i++) {
if ( strcmp( m_lockfiles[i].fname, lockname(name)) == 0) {
m_lockfiles[i].removed = true;
break;
}
}
}
void wb_dblock::dbunlock_all()
{
pwr_tCmd cmd;
for ( int i = 0; i < (int) m_lockfiles.size(); i++) {
if ( !m_lockfiles[i].removed) {
sprintf( cmd, "rm %s", m_lockfiles[i].fname);
system( cmd);
}
}
m_lockfiles.clear();
}
#ifndef wb_dblock_h
#define wb_dblock_h
#include <vector.h>
class wb_lockfile
{
public:
wb_lockfile( char *name) : removed(false)
{ strcpy( fname, name); }
pwr_tFileName fname;
bool removed;
};
class wb_dblock
{
private:
static vector<wb_lockfile> m_lockfiles;
static char *lockname( char *name);
public:
static bool is_locked( char *name, char *user = 0);
static void dblock( char *name);
static void dbunlock( char *name);
static void dbunlock_all();
};
#endif
......@@ -13,6 +13,7 @@
#include "wb_tdrep.h"
#include "wb_adrep.h"
#include "wb_name.h"
#include "wb_dblock.h"
#include "wb_ldh_msg.h"
#include "co_msgwindow.h"
......@@ -26,9 +27,12 @@ extern "C" {
pwr_dImport pwr_BindClasses(System);
pwr_dImport pwr_BindClasses(Base);
wb_erep::wb_erep() : m_dir_cnt(0), m_volatile_idx(0), m_buffer_max(10)
wb_erep::wb_erep() : m_dir_cnt(0), m_volatile_idx(0), m_buffer_max(10),
m_ref_merep_occupied(false)
{
m_merep = new wb_merep(0);
atexit( at_exit);
}
wb_erep::~wb_erep()
......@@ -36,6 +40,11 @@ wb_erep::~wb_erep()
delete m_merep;
}
void wb_erep::at_exit()
{
wb_dblock::dbunlock_all();
}
void wb_erep::unref()
{
if (--m_nRef == 0)
......@@ -558,6 +567,8 @@ void wb_erep::loadMeta( pwr_tStatus *status, char *db)
}
else {
// Load db for this volume
char uname[80];
if ( db) {
// If db is specified, load only specified db, load as dbs instead
if ( cdh_NoCaseStrcmp( vol_array[0], db) != 0) {
......@@ -591,15 +602,37 @@ void wb_erep::loadMeta( pwr_tStatus *status, char *db)
sts = dcli_search_file( vname, found_file, DCLI_DIR_SEARCH_INIT);
dcli_search_file( vname, found_file, DCLI_DIR_SEARCH_END);
if ( ODD(sts)) {
wb_vrepdb *vrepdb = new wb_vrepdb( this, vname);
vrepdb->name(vol_array[0]);
addDb( &sts, vrepdb);
MsgWindow::message( 'I', "Database opened", vname);
vol_cnt++;
if ( wb_dblock::is_locked(vname, uname)) {
MsgWindow::message( 'E', "Database is locked by user", uname, vname);
// Try to load dbs-file instead
cdh_ToLower( vol_array[0], vol_array[0]);
strcpy( vname, "$pwrp_load/");
strcat( vname, vol_array[0]);
strcat( vname, ".dbs");
dcli_translate_filename( vname, vname);
try {
vrep = new wb_vrepdbs( this, vname);
vrep->load();
addDbs( &sts, vrep);
MsgWindow::message( 'I', "Volume loaded", vname);
vol_cnt++;
}
catch ( wb_error& e) {
MsgWindow::message( 'E', "Unable to open volume", vname, e.what().c_str());
}
}
else {
if ( ODD(sts)) {
wb_vrepdb *vrepdb = new wb_vrepdb( this, vname);
vrepdb->name(vol_array[0]);
addDb( &sts, vrepdb);
MsgWindow::message( 'I', "Database opened", vname);
vol_cnt++;
}
else
MsgWindow::message( 'E', "Database not found", vname);
}
else
MsgWindow::message( 'E', "Database not found", vname);
}
}
fpm.close();
......@@ -617,20 +650,26 @@ void wb_erep::loadMeta( pwr_tStatus *status, char *db)
// Load directory volume
if ( !db || (db && cdh_NoCaseStrcmp( "directory", db) == 0)) {
char uname[80];
strcpy( vname, "$pwrp_db/directory.db");
dcli_translate_filename( vname, vname);
sts = dcli_search_file( vname, found_file, DCLI_DIR_SEARCH_INIT);
dcli_search_file( vname, found_file, DCLI_DIR_SEARCH_END);
if ( ODD(sts)) {
wb_vrepdb *vrepdb = new wb_vrepdb( this, vname);
vrepdb->name("directory");
addDb( &sts, vrepdb);
MsgWindow::message( 'I', "Database opened", vname);
}
if ( EVEN(sts)) {
*status = LDH__PROJCONFIG;
return;
if ( wb_dblock::is_locked(vname, uname))
MsgWindow::message( 'E', "Database is locked by user", uname, vname);
else {
if ( ODD(sts)) {
wb_vrepdb *vrepdb = new wb_vrepdb( this, vname);
vrepdb->name("directory");
addDb( &sts, vrepdb);
MsgWindow::message( 'I', "Database opened", vname);
}
if ( EVEN(sts)) {
*status = LDH__PROJCONFIG;
return;
}
}
}
if ( !vol_cnt)
......@@ -866,6 +905,7 @@ void wb_erep::volumeNameToFilename( pwr_tStatus *sts, char *name, char *filename
void wb_erep::setRefMerep( wb_merep *merep)
{
pwr_tStatus sts;
m_ref_merep_occupied = true;
wb_vrepref *vrepref = (wb_vrepref *) volume( &sts, ldh_cPlcConnectVolume);
if ( ODD(sts))
......@@ -881,6 +921,7 @@ void wb_erep::setRefMerep( wb_merep *merep)
void wb_erep::resetRefMerep()
{
pwr_tStatus sts;
m_ref_merep_occupied = false;
wb_vrepref *vrepref = (wb_vrepref *) volume( &sts, ldh_cPlcConnectVolume);
if ( ODD(sts))
......
......@@ -34,6 +34,7 @@ class wb_erep
int m_dir_cnt;
int m_volatile_idx;
int m_buffer_max;
bool m_ref_merep_occupied;
public:
wb_erep();
......@@ -72,6 +73,7 @@ public:
int nextVolatileVid( pwr_tStatus *sts, char *name);
void setRefMerep( wb_merep *merep);
void resetRefMerep();
bool refMerepOccupied() { return m_ref_merep_occupied;}
static void volumeNameToFilename( pwr_tStatus *sts, char *name, char *filename);
......@@ -81,6 +83,8 @@ private:
void loadMeta( pwr_tStatus *status, char *db);
void loadLocalWb( pwr_tStatus *sts);
void bindMethods();
static void at_exit();
};
#endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment