Add utility method to resolve symlinks

The same logic block to resolve possible symlinks exists in 5 different
places now. This change moves this duplicated code to a static member
function of WX_FILENAME instead.
This commit is contained in:
Johannes Maibaum 2021-06-07 21:12:30 +02:00 committed by Jeff Young
parent 542c02046b
commit 022dd6072c
7 changed files with 29 additions and 55 deletions

View File

@ -22,6 +22,7 @@
*/
#include <wx_filename.h>
#include <macros.h>
WX_FILENAME::WX_FILENAME( const wxString& aPath, const wxString& aFilename )
@ -78,4 +79,19 @@ long long WX_FILENAME::GetTimestamp()
return m_fn.GetModificationTime().GetValue().GetValue();
return 0;
}
}
// Resolve possible symlink(s) in aFileName to an absolute path
void WX_FILENAME::ResolvePossibleSymlinks( wxFileName& aFilename )
{
#ifndef __WINDOWS__
if( aFilename.Exists( wxFILE_EXISTS_SYMLINK ) )
{
char buffer[PATH_MAX];
char* realPath = realpath( TO_UTF8( aFilename.GetFullPath() ), buffer );
if( realPath )
aFilename.Assign( wxString::FromUTF8( realPath ) );
}
#endif
}

View File

@ -61,6 +61,7 @@
#include <tools/ee_actions.h>
#include <tools/ee_inspection_tool.h>
#include <paths.h>
#include <wx_filename.h> // For ::ResolvePossibleSymlinks
bool SCH_EDIT_FRAME::SaveEEFile( SCH_SHEET* aSheet, bool aSaveUnderNewName )
{
@ -109,17 +110,8 @@ bool SCH_EDIT_FRAME::SaveEEFile( SCH_SHEET* aSheet, bool aSaveUnderNewName )
schematicFileName.SetExt( KiCadSchematicFileExtension );
}
#ifndef __WINDOWS__
// Write through symlinks, don't replace them
if( schematicFileName.Exists( wxFILE_EXISTS_SYMLINK ) )
{
char buffer[ PATH_MAX ];
char *realPath = realpath( TO_UTF8( schematicFileName.GetFullPath() ), buffer );
if( realPath )
schematicFileName.Assign( wxString::FromUTF8( realPath ) );
}
#endif
WX_FILENAME::ResolvePossibleSymlinks( schematicFileName );
if( !IsWritable( schematicFileName ) )
return false;

View File

@ -61,6 +61,7 @@
#include <symbol_lib_table.h> // for PropPowerSymsOnly definintion.
#include <ee_selection.h>
#include <kicad_string.h>
#include <wx_filename.h> // for ::ResolvePossibleSymlinks()
using namespace TSCHEMATIC_T;
@ -1380,18 +1381,7 @@ SCH_SEXPR_PLUGIN_CACHE::~SCH_SEXPR_PLUGIN_CACHE()
wxFileName SCH_SEXPR_PLUGIN_CACHE::GetRealFile() const
{
wxFileName fn( m_libFileName );
#ifndef __WINDOWS__
if( fn.Exists( wxFILE_EXISTS_SYMLINK ) )
{
char buffer[ PATH_MAX ];
char *realPath = realpath( TO_UTF8( fn.GetFullPath() ), buffer );
if( realPath )
fn.Assign( wxString::FromUTF8( realPath ) );
}
#endif
WX_FILENAME::ResolvePossibleSymlinks( fn );
return fn;
}

View File

@ -73,6 +73,7 @@
#include <confirm.h>
#include <tool/selection.h>
#include <default_values.h> // For some default values
#include <wx_filename.h> // For ::ResolvePossibleSymlinks()
#define Mils2Iu( x ) Mils2iu( x )
@ -2437,18 +2438,7 @@ SCH_LEGACY_PLUGIN_CACHE::~SCH_LEGACY_PLUGIN_CACHE()
wxFileName SCH_LEGACY_PLUGIN_CACHE::GetRealFile() const
{
wxFileName fn( m_libFileName );
#ifndef __WINDOWS__
if( fn.Exists( wxFILE_EXISTS_SYMLINK ) )
{
char buffer[ PATH_MAX ];
char *realPath = realpath( TO_UTF8( fn.GetFullPath() ), buffer );
if( realPath )
fn.Assign( wxString::FromUTF8( realPath ) );
}
#endif
WX_FILENAME::ResolvePossibleSymlinks( fn );
return fn;
}

View File

@ -48,6 +48,9 @@ public:
// Avoid multiple calls to stat() on POSIX kernels.
long long GetTimestamp();
// Resolve possible symlink(s) to absolute path
static void ResolvePossibleSymlinks( wxFileName& aFilename );
private:
// Write cached values to the wrapped wxFileName. MUST be called before using m_fn.
void resolve();

View File

@ -60,6 +60,7 @@
#include <tool/tool_manager.h>
#include <tools/pcb_actions.h>
#include "footprint_info_impl.h"
#include <wx_filename.h> // For ::ResolvePossibleSymlinks()
#include <wx/wupdlock.h>
#include <wx/filedlg.h>
@ -943,17 +944,8 @@ bool PCB_EDIT_FRAME::SavePcbFile( const wxString& aFileName, bool addToHistory,
if( pcbFileName.GetExt() == LegacyPcbFileExtension )
pcbFileName.SetExt( KiCadPcbFileExtension );
#ifndef __WINDOWS__
// Write through symlinks, don't replace them
if( pcbFileName.Exists( wxFILE_EXISTS_SYMLINK ) )
{
char buffer[ PATH_MAX ];
char *realPath = realpath( TO_UTF8( pcbFileName.GetFullPath() ), buffer );
if( realPath )
pcbFileName.Assign( wxString::FromUTF8( realPath ) );
}
#endif
WX_FILENAME::ResolvePossibleSymlinks( pcbFileName );
if( !IsWritable( pcbFileName ) )
{

View File

@ -2407,17 +2407,8 @@ void PCB_IO::FootprintSave( const wxString& aLibraryPath, const FOOTPRINT* aFoot
wxFileName fn( aLibraryPath, aFootprint->GetFPID().GetLibItemName(),
KiCadFootprintFileExtension );
#ifndef __WINDOWS__
// Write through symlinks, don't replace them
if( fn.Exists( wxFILE_EXISTS_SYMLINK ) )
{
char buffer[ PATH_MAX ];
char *realPath = realpath( TO_UTF8( fn.GetFullPath() ), buffer );
if( realPath )
fn.Assign( wxString::FromUTF8( realPath ) );
}
#endif
WX_FILENAME::ResolvePossibleSymlinks( fn );
if( !fn.IsOk() )
{