From 022dd6072c3dbc0ce0e66c67adb3495f4e36e206 Mon Sep 17 00:00:00 2001 From: Johannes Maibaum Date: Mon, 7 Jun 2021 21:12:30 +0200 Subject: [PATCH] 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. --- common/wx_filename.cpp | 18 +++++++++++++++++- eeschema/files-io.cpp | 12 ++---------- .../sch_plugins/kicad/sch_sexpr_plugin.cpp | 14 ++------------ .../sch_plugins/legacy/sch_legacy_plugin.cpp | 14 ++------------ include/wx_filename.h | 3 +++ pcbnew/files.cpp | 12 ++---------- pcbnew/plugins/kicad/kicad_plugin.cpp | 11 +---------- 7 files changed, 29 insertions(+), 55 deletions(-) diff --git a/common/wx_filename.cpp b/common/wx_filename.cpp index 805dbbc781..3ed6d18fbf 100644 --- a/common/wx_filename.cpp +++ b/common/wx_filename.cpp @@ -22,6 +22,7 @@ */ #include +#include 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; -} \ No newline at end of file +} + +// 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 +} diff --git a/eeschema/files-io.cpp b/eeschema/files-io.cpp index 3407000955..cb8a58ab4d 100644 --- a/eeschema/files-io.cpp +++ b/eeschema/files-io.cpp @@ -61,6 +61,7 @@ #include #include #include +#include // 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; diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp index 57861728e5..0b9c2482f6 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_plugin.cpp @@ -61,6 +61,7 @@ #include // for PropPowerSymsOnly definintion. #include #include +#include // 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; } diff --git a/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp b/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp index 7901f65c05..5b2fd4a0e1 100644 --- a/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp +++ b/eeschema/sch_plugins/legacy/sch_legacy_plugin.cpp @@ -73,6 +73,7 @@ #include #include #include // For some default values +#include // 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; } diff --git a/include/wx_filename.h b/include/wx_filename.h index c7b02246fe..d8961f70e5 100644 --- a/include/wx_filename.h +++ b/include/wx_filename.h @@ -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(); diff --git a/pcbnew/files.cpp b/pcbnew/files.cpp index 2876e86541..8800d5aab7 100644 --- a/pcbnew/files.cpp +++ b/pcbnew/files.cpp @@ -60,6 +60,7 @@ #include #include #include "footprint_info_impl.h" +#include // For ::ResolvePossibleSymlinks() #include #include @@ -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 ) ) { diff --git a/pcbnew/plugins/kicad/kicad_plugin.cpp b/pcbnew/plugins/kicad/kicad_plugin.cpp index 39209a21c5..4ca5b56208 100644 --- a/pcbnew/plugins/kicad/kicad_plugin.cpp +++ b/pcbnew/plugins/kicad/kicad_plugin.cpp @@ -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() ) {