Move file locking utilities to a separate file

The global ::LockFile() function is used in a single place, but it's in
common.h, so visible to all files.

The GetKicadLockFilePath function is used in only two places, and one of
them is LockFile.

This commit puts them both in a separate header, so they're only visible
to code using them.

The implementation of GetKicadLockFilePath is moved to lockfile.cpp,
where LockFile already was.

Also removed a (large) handful of wxT macros, which aren't needed any
more and make code less readable.
This commit is contained in:
John Beard 2017-02-09 16:09:28 +08:00 committed by Wayne Stambaugh
parent f2763e90ea
commit 27374c0aa3
7 changed files with 105 additions and 71 deletions

View File

@ -313,46 +313,6 @@ wxConfigBase* GetNewConfig( const wxString& aProgName )
return cfg;
}
wxString GetKicadLockFilePath()
{
wxFileName lockpath;
lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
#if defined( __WXMAC__ )
// In OSX use the standard per user cache directory
lockpath.AppendDir( wxT( "Library" ) );
lockpath.AppendDir( wxT( "Caches" ) );
lockpath.AppendDir( wxT( "kicad" ) );
#elif defined( __UNIX__ )
wxString envstr;
// Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
if( wxGetEnv( wxT( "XDG_RUNTIME_DIR" ), &envstr ) && !envstr.IsEmpty() )
{
lockpath.AssignDir( envstr );
}
else if( wxGetEnv( wxT( "XDG_CACHE_HOME" ), &envstr ) && !envstr.IsEmpty() )
{
lockpath.AssignDir( envstr );
}
else
{
// If all fails, just use ~/.cache
lockpath.AppendDir( wxT( ".cache" ) );
}
lockpath.AppendDir( wxT( "kicad" ) );
#endif
#if defined( __WXMAC__ ) || defined( __UNIX__ )
if( !lockpath.DirExists() )
{
// Lockfiles should be only readable by the user
lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
}
#endif
return lockpath.GetPath();
}
wxString GetKicadConfigPath()
{

View File

@ -45,6 +45,7 @@
#include <dialog_helpers.h>
#include <base_units.h>
#include <math/box2.h>
#include <lockfile.h>
#include <wx/fontdlg.h>
#include <wx/snglinst.h>
@ -130,8 +131,6 @@ EDA_DRAW_FRAME::EDA_DRAW_FRAME( KIWAY* aKiway, wxWindow* aParent,
KIWAY_PLAYER( aKiway, aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName ),
m_galDisplayOptions( std::make_unique<KIGFX::GAL_DISPLAY_OPTIONS>() )
{
m_file_checker = NULL;
m_drawToolBar = NULL;
m_optionsToolBar = NULL;
m_gridSelectBox = NULL;
@ -229,15 +228,12 @@ EDA_DRAW_FRAME::~EDA_DRAW_FRAME()
void EDA_DRAW_FRAME::ReleaseFile()
{
delete m_file_checker;
m_file_checker = 0;
m_file_checker = nullptr;
}
bool EDA_DRAW_FRAME::LockFile( const wxString& aFileName )
{
delete m_file_checker;
m_file_checker = ::LockFile( aFileName );
return bool( m_file_checker );

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2014-2015 KiCad Developers, see CHANGELOG.TXT for contributors.
* Copyright (C) 2014-2017 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -22,12 +22,14 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <lockfile.h>
#include <wx/filename.h>
#include <wx/snglinst.h>
#include <common.h>
wxSingleInstanceChecker* LockFile( const wxString& aFileName )
std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName )
{
// first make absolute and normalize, to avoid that different lock files
// for the same file can be created
@ -35,22 +37,61 @@ wxSingleInstanceChecker* LockFile( const wxString& aFileName )
fn.MakeAbsolute();
wxString lockFileName = fn.GetFullPath() + wxT( ".lock" );
wxString lockFileName = fn.GetFullPath() + ".lock";
lockFileName.Replace( wxT( "/" ), wxT( "_" ) );
lockFileName.Replace( "/", "_" );
// We can have filenames coming from Windows, so also convert Windows separator
lockFileName.Replace( wxT( "\\" ), wxT( "_" ) );
lockFileName.Replace( "\\", "_" );
wxSingleInstanceChecker* p = new wxSingleInstanceChecker( lockFileName,
GetKicadLockFilePath() );
auto p = std::make_unique<wxSingleInstanceChecker>( lockFileName,
GetKicadLockFilePath() );
if( p->IsAnotherRunning() )
{
delete p;
p = NULL;
p = nullptr;
}
return p;
}
wxString GetKicadLockFilePath()
{
wxFileName lockpath;
lockpath.AssignDir( wxGetHomeDir() ); // Default wx behavior
#if defined( __WXMAC__ )
// In OSX use the standard per user cache directory
lockpath.AppendDir( "Library" );
lockpath.AppendDir( "Caches" );
lockpath.AppendDir( "kicad" );
#elif defined( __UNIX__ )
wxString envstr;
// Try first the standard XDG_RUNTIME_DIR, falling back to XDG_CACHE_HOME
if( wxGetEnv( "XDG_RUNTIME_DIR", &envstr ) && !envstr.IsEmpty() )
{
lockpath.AssignDir( envstr );
}
else if( wxGetEnv( "XDG_CACHE_HOME", &envstr ) && !envstr.IsEmpty() )
{
lockpath.AssignDir( envstr );
}
else
{
// If all fails, just use ~/.cache
lockpath.AppendDir( ".cache" );
}
lockpath.AppendDir( "kicad" );
#endif
#if defined( __WXMAC__ ) || defined( __UNIX__ )
if( !lockpath.DirExists() )
{
// Lockfiles should be only readable by the user
lockpath.Mkdir( 0700, wxPATH_MKDIR_FULL );
}
#endif
return lockpath.GetPath();
}

View File

@ -51,6 +51,7 @@
#include <menus_helpers.h>
#include <confirm.h>
#include <dialog_env_var_config.h>
#include <lockfile.h>
#define KICAD_COMMON wxT( "kicad_common" )

View File

@ -49,7 +49,6 @@
class wxAboutDialogInfo;
class SEARCH_STACK;
class wxSingleInstanceChecker;
class REPORTER;
@ -350,14 +349,6 @@ bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
const wxString& aBaseFilename,
REPORTER* aReporter = NULL );
/**
* Function LockFile
* tests to see if aFileName can be locked (is not already locked) and only then
* returns a wxSingleInstanceChecker protecting aFileName. Caller owns the return value.
*/
wxSingleInstanceChecker* LockFile( const wxString& aFileName );
/// Put aPriorityPath in front of all paths in the value of aEnvVar.
const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
@ -377,11 +368,6 @@ const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPa
*/
wxConfigBase* GetNewConfig( const wxString& aProgName );
/**
* Function GetKicadLockFilePath
* @return A wxString containing the path for lockfiles in Kicad
*/
wxString GetKicadLockFilePath();
/**
* Function GetKicadConfigPath

View File

@ -71,7 +71,7 @@ class EDA_DRAW_FRAME : public KIWAY_PLAYER
protected:
wxSingleInstanceChecker* m_file_checker; ///< prevents opening same file multiple times.
std::unique_ptr<wxSingleInstanceChecker> m_file_checker; ///< prevents opening same file multiple times.
EDA_HOTKEY_CONFIG* m_hotkeysDescrList;
int m_LastGridSizeId; // the command id offset (>= 0) of the last selected grid

50
include/lockfile.h Normal file
View File

@ -0,0 +1,50 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* File locking utilities
* @file lock_file.h
*/
#ifndef INCLUDE__LOCK_FILE_H_
#define INCLUDE__LOCK_FILE_H_
#include <wx/string.h>
#include <memory>
class wxSingleInstanceChecker;
/**
* Function LockFile
* tests to see if aFileName can be locked (is not already locked) and only then
* returns a wxSingleInstanceChecker protecting aFileName.
*/
std::unique_ptr<wxSingleInstanceChecker> LockFile( const wxString& aFileName );
/**
* Function GetKicadLockFilePath
* @return A wxString containing the path for lockfiles in Kicad
*/
wxString GetKicadLockFilePath();
#endif // INCLUDE__LOCK_FILE_H_