When copying global tables, ensure writable
Copying read-only files into the user directory, the files will need to
be read/write in order to work correctly
Fixes https://gitlab.com/kicad/code/kicad/issues/21899
(cherry picked from commit ab7c30a2a1)
This commit is contained in:
@@ -27,6 +27,8 @@
|
||||
|
||||
#include "design_block_lib_table.h"
|
||||
|
||||
#include <kiplatform/io.h>
|
||||
|
||||
|
||||
DIALOG_GLOBAL_DESIGN_BLOCK_LIB_TABLE_CONFIG::DIALOG_GLOBAL_DESIGN_BLOCK_LIB_TABLE_CONFIG(
|
||||
wxWindow* aParent ) :
|
||||
@@ -124,6 +126,9 @@ bool DIALOG_GLOBAL_DESIGN_BLOCK_LIB_TABLE_CONFIG::TransferDataFromWindow()
|
||||
fn.GetFullPath(), designBlockTableFileName.GetFullPath() ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure the copied file is writable
|
||||
KIPLATFORM::IO::MakeWriteable( designBlockTableFileName.GetFullPath() );
|
||||
}
|
||||
|
||||
// Load the successfully copied design block library table file. This should not fail since the
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <kiway.h>
|
||||
#include <macros.h>
|
||||
|
||||
#include <kiplatform/io.h>
|
||||
#include "symbol_lib_table.h"
|
||||
|
||||
|
||||
@@ -118,6 +119,9 @@ bool DIALOG_GLOBAL_SYM_LIB_TABLE_CONFIG::TransferDataFromWindow()
|
||||
fn.GetFullPath(), symTableFileName.GetFullPath() ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure the copied file is writable
|
||||
KIPLATFORM::IO::MakeWriteable( symTableFileName.GetFullPath() );
|
||||
}
|
||||
|
||||
// Load the successfully copied symbol library table file. This should not fail since the
|
||||
|
||||
@@ -46,6 +46,14 @@ namespace IO
|
||||
*/
|
||||
bool DuplicatePermissions( const wxString& aSrc, const wxString& aDest );
|
||||
|
||||
/**
|
||||
* Ensures that a file has write permissions.
|
||||
* This is useful after copying files that may have been read-only.
|
||||
* @param aFilePath path to the file to make writeable
|
||||
* @return true if the process was successful
|
||||
*/
|
||||
bool MakeWriteable( const wxString& aFilePath );
|
||||
|
||||
/**
|
||||
* Helper function to determine the status of the 'Hidden' file attribute.
|
||||
* @return true if the file attribut is set.
|
||||
|
||||
@@ -65,6 +65,44 @@ bool KIPLATFORM::IO::DuplicatePermissions(const wxString& sourceFilePath, const
|
||||
}
|
||||
}
|
||||
|
||||
bool KIPLATFORM::IO::MakeWriteable( const wxString& aFilePath )
|
||||
{
|
||||
NSString *path = [NSString stringWithUTF8String:aFilePath.utf8_str()];
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
|
||||
NSError *error;
|
||||
NSDictionary *attributes = [fileManager attributesOfItemAtPath:path error:&error];
|
||||
|
||||
if( !attributes )
|
||||
{
|
||||
NSLog(@"Error retrieving file attributes: %@", error);
|
||||
return false;
|
||||
}
|
||||
|
||||
NSNumber *permissions = attributes[NSFilePosixPermissions];
|
||||
|
||||
if( permissions == nil )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Add user write permission (S_IWUSR = 0200)
|
||||
unsigned short currentPerms = [permissions unsignedShortValue];
|
||||
unsigned short newPerms = currentPerms | 0200;
|
||||
|
||||
if( [fileManager setAttributes:@{NSFilePosixPermissions: @(newPerms)}
|
||||
ofItemAtPath:path
|
||||
error:&error] )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
NSLog(@"Error setting permissions: %@", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
|
||||
{
|
||||
wxFileName fn( aFileName );
|
||||
|
||||
@@ -67,6 +67,21 @@ bool KIPLATFORM::IO::DuplicatePermissions( const wxString &aSrc, const wxString
|
||||
}
|
||||
}
|
||||
|
||||
bool KIPLATFORM::IO::MakeWriteable( const wxString& aFilePath )
|
||||
{
|
||||
struct stat fileStat;
|
||||
if( stat( aFilePath.fn_str(), &fileStat ) == 0 )
|
||||
{
|
||||
// Add user write permission to existing permissions
|
||||
mode_t newPermissions = fileStat.st_mode | S_IWUSR;
|
||||
if( chmod( aFilePath.fn_str(), newPermissions ) == 0 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
|
||||
{
|
||||
wxFileName fn( aFileName );
|
||||
|
||||
@@ -115,6 +115,23 @@ bool KIPLATFORM::IO::DuplicatePermissions( const wxString &aSrc, const wxString
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool KIPLATFORM::IO::MakeWriteable( const wxString& aFilePath )
|
||||
{
|
||||
DWORD attrs = GetFileAttributesW( aFilePath.wc_str() );
|
||||
|
||||
if( attrs == INVALID_FILE_ATTRIBUTES )
|
||||
return false;
|
||||
|
||||
// Remove read-only attribute if present
|
||||
if( attrs & FILE_ATTRIBUTE_READONLY )
|
||||
{
|
||||
attrs &= ~FILE_ATTRIBUTE_READONLY;
|
||||
return SetFileAttributesW( aFilePath.wc_str(), attrs ) != 0;
|
||||
}
|
||||
|
||||
return true; // Already writeable
|
||||
}
|
||||
|
||||
bool KIPLATFORM::IO::IsFileHidden( const wxString& aFileName )
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
@@ -26,6 +26,8 @@
|
||||
|
||||
#include "fp_lib_table.h"
|
||||
|
||||
#include <kiplatform/io.h>
|
||||
|
||||
|
||||
DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG( wxWindow* aParent ) :
|
||||
DIALOG_GLOBAL_LIB_TABLE_CONFIG( aParent, _( "footprint" ), KIWAY::FACE_PCB )
|
||||
@@ -120,6 +122,9 @@ bool DIALOG_GLOBAL_FP_LIB_TABLE_CONFIG::TransferDataFromWindow()
|
||||
fn.GetFullPath(), fpTableFileName.GetFullPath() ) );
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure the copied file is writable
|
||||
KIPLATFORM::IO::MakeWriteable( fpTableFileName.GetFullPath() );
|
||||
}
|
||||
|
||||
// Load the successfully copied footprint library table file. This should not fail
|
||||
|
||||
Reference in New Issue
Block a user