KiCad PCB EDA Suite
Loading...
Searching...
No Matches
gestfich.cpp File Reference

Functions for file management. More...

#include <wx/mimetype.h>
#include <wx/dir.h>
#include <wx/stdpaths.h>
#include <wx/utils.h>
#include <filesystem>
#include <pgm_base.h>
#include <kiplatform/io.h>
#include <confirm.h>
#include <core/arraydim.h>
#include <gestfich.h>
#include <string_utils.h>
#include <launch_ext.h>
#include <paths.h>
#include "wx/tokenzr.h"
#include <richio.h>
#include <sexpr/sexpr.h>
#include <sexpr/sexpr_parser.h>
#include <wx/wfstream.h>
#include <wx/fs_zip.h>
#include <wx/zipstrm.h>
#include <wx/tarstrm.h>
#include <wx/zstream.h>
#include <algorithm>
#include <string>
#include <system_error>
#include <unordered_set>
#include <vector>
#include <core/kicad_algo.h>

Go to the source code of this file.

Functions

void QuoteString (wxString &string)
 Add un " to the start and the end of string (if not already done).
 
wxString FindKicadFile (const wxString &shortname)
 Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin.
 
int ExecuteFile (const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
 Call the executable file aEditorName with the parameter aFileName.
 
static wxString & envPathEntry (wxEnvVariableHashMap &aEnv)
 
static wxString normalizedDir (const wxString &aPath)
 
static bool pathListContains (const wxString &aPathList, const wxString &aDir)
 
int ExecuteCommandThroughShell (const wxString &aCommand, wxProcess *aProcess)
 Run a user-supplied command line through the platform shell so glob expansion, pipes, and other shell features work consistently.
 
bool OpenPDF (const wxString &file)
 Run the PDF viewer and display a PDF file.
 
void KiCopyFile (const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
 
static void traverseSEXPR (SEXPR::SEXPR *aNode, const std::function< void(SEXPR::SEXPR *)> &aVisitor)
 
void CopySexprFile (const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
 
wxString QuoteFullPath (wxFileName &fn, wxPathFormat format)
 Quote return value of wxFileName::GetFullPath().
 
bool RmDirRecursive (const wxString &aFileName, wxString *aErrors)
 Remove the directory aDirName and all its contents including subdirectories and their files.
 
bool CopyDirectory (const wxString &aSourceDir, const wxString &aDestDir, const std::vector< wxString > &aPathsWithOverwriteDisallowed, wxString &aErrors)
 Copy a directory and its contents to another directory.
 
bool CopyFilesOrDirectory (const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten)
 
bool AddDirectoryToZip (wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
 Add a directory and its contents to a zip file.
 
bool WriteDirectoryArchive (const wxString &aSourceDir, wxOutputStream &aOut, ARCHIVE_FORMAT aFormat, const wxString &aTopDir)
 Write directory entries in sorted archive path order, using the archive root when aTopDir is empty.
 
bool WriteDirectoryArchive (const wxString &aSourceDir, const wxString &aArchive, ARCHIVE_FORMAT aFormat, const wxString &aTopDir, wxString *aError, const ARCHIVE_STREAM_FACTORY &aOpen)
 Replace the archive atomically after writing and closing a sibling temporary file.
 
std::filesystem::path ToFsPath (const wxString &aPath)
 Give aPath to std::filesystem without a detour through the C locale.
 
wxString FromFsPath (const std::filesystem::path &aPath)
 
void CollectFilesLoopSafe (const wxString &aRoot, wxArrayString &aFiles, const wxString &aFileSpec, int aFlags)
 Recursively collect every file under aRoot, deduplicating subdirectories by their resolved path.
 
void CollectSubdirsLoopSafe (const wxString &aRoot, wxArrayString &aDirs, int aFlags)
 Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopSafe().
 

Variables

static const wxChar PATH_LIST_SEPARATOR = wxT( ':' )
 

Detailed Description

Functions for file management.

Definition in file gestfich.cpp.

Function Documentation

◆ AddDirectoryToZip()

bool AddDirectoryToZip ( wxZipOutputStream & aZip,
const wxString & aSourceDir,
wxString & aErrors,
const wxString & aParentDir = "" )

Add a directory and its contents to a zip file.

Parameters
aZipis the zip file to add to.
aSourceDiris the directory to add.
aErrorsis a string to append any errors to.
aParentDiris the parent directory to add to the zip file.

Definition at line 737 of file gestfich.cpp.

References _, and AddDirectoryToZip().

Referenced by AddDirectoryToZip(), and JOBS_OUTPUT_ARCHIVE::HandleOutputs().

◆ CollectFilesLoopSafe()

void CollectFilesLoopSafe ( const wxString & aRoot,
wxArrayString & aFiles,
const wxString & aFileSpec = wxEmptyString,
int aFlags = wxDIR_DEFAULT )

Recursively collect every file under aRoot, deduplicating subdirectories by their resolved path.

Legitimate one-level symlinks resolve normally, but recursive symlinks (a Wine dosdevices/z: -> / loop, a self-referencing . link, etc.) are visited at most once instead of recursing until OOM. This is a loop-safe replacement for wxDir::GetAllFiles() when scanning user-controlled directory trees.

Parameters
aRootis the directory to walk.
aFilesreceives the full path of every matching file found.
aFileSpecis an optional wildcard filter (e.g. *.step); empty matches all.
aFlagsis the wxDir traversal flag set; defaults to wxDIR_DEFAULT to match wxDir::GetAllFiles(). Pass wxDIR_FILES | wxDIR_DIRS to exclude hidden entries. wxDIR_FILES and wxDIR_DIRS are always implied so recursion and file collection happen regardless.

Definition at line 1184 of file gestfich.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), MODEL_SUBSTITUTION::STEP_CATALOG::Build(), PCB_IO_GEDA::DeleteLibrary(), PCB_IO_KICAD_SEXPR::DeleteLibrary(), PCB_IO_SPRINT_LAYOUT::FootprintEnumerate(), and DIALOG_MIGRATE_3D_MODELS::scanDirectory().

◆ CollectSubdirsLoopSafe()

void CollectSubdirsLoopSafe ( const wxString & aRoot,
wxArrayString & aDirs,
int aFlags = wxDIR_DIRS )

Recursively collect every subdirectory under aRoot using the same loop detection as CollectFilesLoopSafe().

Loop-safe replacement for wxDir::GetAllFiles() called with the wxDIR_DIRS flag.

Parameters
aRootis the directory to walk.
aDirsreceives the full path of every subdirectory found.
aFlagsis the wxDir traversal flag set; defaults to wxDIR_DIRS (hidden directories excluded). Pass wxDIR_DIRS | wxDIR_HIDDEN to include them. wxDIR_DIRS is always implied.

Definition at line 1194 of file gestfich.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), and DESIGN_BLOCK_IO::DeleteLibrary().

◆ CopyDirectory()

bool CopyDirectory ( const wxString & aSourceDir,
const wxString & aDestDir,
const std::vector< wxString > & aOverwriteExclusions,
wxString & aErrors )

Copy a directory and its contents to another directory.

Parameters
aSourceDiris the directory to copy.
aDestDiris the directory to copy to.
aOverwriteExclusions
aErrorsis a string to append any errors to.

Definition at line 560 of file gestfich.cpp.

References _, alg::contains(), and CopyDirectory().

Referenced by CopyDirectory(), and JOBS_OUTPUT_FOLDER::HandleOutputs().

◆ CopyFilesOrDirectory()

bool CopyFilesOrDirectory ( const wxString & aSourcePath,
const wxString & aDestDir,
bool aAllowOverwrites,
wxString & aErrors,
std::vector< wxString > & aPathsWritten )

Definition at line 614 of file gestfich.cpp.

References _, and CopyFilesOrDirectory().

Referenced by CopyFilesOrDirectory(), and JOBS_RUNNER::runSpecialCopyFiles().

◆ CopySexprFile()

void CopySexprFile ( const wxString & aSrcPath,
const wxString & aDestPath,
std::function< bool(const std::string &token, wxString &value)> aCallback,
wxString & aErrors )
Parameters
aSrcPathis the full filename of the source.
[in]aDestPathis the full filename of the target.
[in]aCallbacka facility to allow modification of the values of particular tokens. Normally used to update paths in the copied document.
[out]aErrorsa wxString to append any errors to.

Definition at line 443 of file gestfich.cpp.

References PRETTIFIED_FILE_OUTPUTFORMATTER::Finish(), SEXPR::PARSER::ParseFromFile(), OUTPUTFORMATTER::Print(), TO_UTF8, and traverseSEXPR().

Referenced by PCB::IFACE::SaveFileAs(), and SCH::IFACE::SaveFileAs().

◆ envPathEntry()

static wxString & envPathEntry ( wxEnvVariableHashMap & aEnv)
static

Definition at line 289 of file gestfich.cpp.

References name.

Referenced by ExecuteCommandThroughShell().

◆ ExecuteCommandThroughShell()

int ExecuteCommandThroughShell ( const wxString & aCommand,
wxProcess * aProcess = nullptr )

Run a user-supplied command line through the platform shell so glob expansion, pipes, and other shell features work consistently.

On Windows the command is wrapped as cmd.exe /d /s /c "<cmd>" so that absolute paths containing spaces or quotes survive cmd.exe's quote handling intact. On POSIX the command is handed to /bin/sh -c as a single argument.

Parameters
aCommandthe command line exactly as entered by the user.
aProcessan optional wxProcess for the call; pass a Redirect()ed process to capture output.
Returns
the process exit code, or -1 if the command could not be executed.

Definition at line 334 of file gestfich.cpp.

References envPathEntry(), flag, PATHS::GetExecutableDir(), PATHS::GetToolDirectories(), PATH_LIST_SEPARATOR, and pathListContains().

Referenced by executeViaShell(), and JOBS_RUNNER::runSpecialExecute().

◆ ExecuteFile()

int ExecuteFile ( const wxString & aEditorName,
const wxString & aFileName = wxEmptyString,
wxProcess * aCallback = nullptr,
bool aFileForKicad = true )

Call the executable file aEditorName with the parameter aFileName.

Parameters
[in]aEditorNameis the full filename for the binary.
[in]aFileNameis the full filename of the file to open.
[in]aCallbacka wxProcess* for the call.
aFileForKicada boolean to flag if aFileName runs with a KiCad binary. In this case aFileName is a shortname and FindKicadFile() is called to return the path. In the other case, aFileName is a full file name (passed prefixed with the path).

Definition at line 170 of file gestfich.cpp.

References _, DisplayErrorMessage(), and FindKicadFile().

Referenced by COMMON_CONTROL::Execute(), DIALOG_CONFIG_EQUFILES::OnEditEquFile(), DIALOG_BOM::OnEditGenerator(), SCH_EDIT_FRAME::OnOpenPcbnew(), PROJECT_TREE_PANE::openInTextEditor(), FOOTPRINT_EDITOR_CONTROL::OpenWithTextEditor(), SYMBOL_EDITOR_CONTROL::OpenWithTextEditor(), BOARD_EDITOR_CONTROL::ShowEeschema(), and GERBVIEW_INSPECTION_TOOL::ShowSource().

◆ FindKicadFile()

wxString FindKicadFile ( const wxString & shortname)

Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin.

The binary path is found from:

  • binary path.
  • KICAD environment variable.
  • c:\kicad or /usr/local/kicad (the default).
  • default binary path.

Definition at line 72 of file gestfich.cpp.

References arrayDim(), PGM_BASE::GetExecutablePath(), PGM_BASE::GetKicadEnvVariable(), Pgm(), and PATHS::RunningFromBuildDir().

Referenced by API_PLUGIN_MANAGER::doInvokeAction(), KICAD_MANAGER_FRAME::DoWithAcceptedFiles(), ExecuteFile(), PYTHON_MANAGER::FindPythonInterpreter(), API_HANDLER_COMMON::handleGetKiCadBinaryPath(), and API_PLUGIN_MANAGER::processNextJob().

◆ FromFsPath()

wxString FromFsPath ( const std::filesystem::path & aPath)

Definition at line 987 of file gestfich.cpp.

References utf8().

Referenced by generateOneODBPackage().

◆ KiCopyFile()

void KiCopyFile ( const wxString & aSrcPath,
const wxString & aDestPath,
wxString & aErrors )
Parameters
aSrcPathis the full filename of the source.
[in]aDestPathis the full filename of the target.
[out]aErrorsa wxString to append any errors to.

Definition at line 416 of file gestfich.cpp.

References _.

Referenced by MIGRATION_TRAVERSER::OnFile(), PROJECT_TREE_TRAVERSER::OnFile(), GERBV::IFACE::SaveFileAs(), PGE::IFACE::SaveFileAs(), HEADLESS_PCB_CONTEXT::SavePcbCopy(), PCB_EDIT_FRAME::SavePcbCopy(), and PCB_EDIT_FRAME::SavePcbFile().

◆ normalizedDir()

static wxString normalizedDir ( const wxString & aPath)
static

Definition at line 305 of file gestfich.cpp.

Referenced by pathListContains().

◆ OpenPDF()

bool OpenPDF ( const wxString & file)

Run the PDF viewer and display a PDF file.

Parameters
filethe PDF file to open.
Return values
trueif PDF viewer found.
falseif no PDF viewer found.

Definition at line 380 of file gestfich.cpp.

References _, DisplayErrorMessage(), PGM_BASE::GetPdfBrowserName(), LaunchExternal(), Pgm(), and PGM_BASE::ReadPdfBrowserInfos().

Referenced by PROJECT_TREE_ITEM::Activate(), GetAssociatedDocument(), and DIALOG_PLOT_SCHEMATIC::plotSchematic().

◆ pathListContains()

static bool pathListContains ( const wxString & aPathList,
const wxString & aDir )
static

Definition at line 318 of file gestfich.cpp.

References normalizedDir(), and PATH_LIST_SEPARATOR.

Referenced by ExecuteCommandThroughShell().

◆ QuoteFullPath()

wxString QuoteFullPath ( wxFileName & fn,
wxPathFormat format = wxPATH_NATIVE )

Quote return value of wxFileName::GetFullPath().

This allows file name paths with spaces to be used as parameters to ProcessExecute function calls.

Parameters
fnis the filename to wrap.
formatif provided, can be used to transform the nature of the wrapped filename to another platform.

Definition at line 506 of file gestfich.cpp.

◆ QuoteString()

void QuoteString ( wxString & string)

Add un " to the start and the end of string (if not already done).

Parameters
stringstring to modify.

Definition at line 62 of file gestfich.cpp.

◆ RmDirRecursive()

bool RmDirRecursive ( const wxString & aFileName,
wxString * aErrors )

Remove the directory aDirName and all its contents including subdirectories and their files.

Definition at line 512 of file gestfich.cpp.

References _, and path.

Referenced by BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), BOOST_AUTO_TEST_CASE(), LIBGIT_BACKEND::RemoveVCS(), and DIALOG_GIT_REPOSITORY::~DIALOG_GIT_REPOSITORY().

◆ ToFsPath()

std::filesystem::path ToFsPath ( const wxString & aPath)

Give aPath to std::filesystem without a detour through the C locale.

Definition at line 977 of file gestfich.cpp.

Referenced by BOOST_FIXTURE_TEST_CASE(), DIR_LOOP_GUARD::DIR_LOOP_GUARD(), generateOneODBPackage(), SCH_IO_DATABASE::loadSettingsBounded(), and DIR_LOOP_GUARD::ShouldDescend().

◆ traverseSEXPR()

static void traverseSEXPR ( SEXPR::SEXPR * aNode,
const std::function< void(SEXPR::SEXPR *)> & aVisitor )
static

◆ WriteDirectoryArchive() [1/2]

bool WriteDirectoryArchive ( const wxString & aSourceDir,
const wxString & aArchive,
ARCHIVE_FORMAT aFormat,
const wxString & aTopDir,
wxString * aError,
const ARCHIVE_STREAM_FACTORY & aOpen )

Replace the archive atomically after writing and closing a sibling temporary file.

Definition at line 899 of file gestfich.cpp.

References _, KIPLATFORM::IO::CLOUD_SYNC_BUFFER_SIZE, KIPLATFORM::IO::CommitTempFile(), KIPLATFORM::IO::FlushToDisk(), KIPLATFORM::IO::OpenUniqueSiblingTempFile(), and WriteDirectoryArchive().

◆ WriteDirectoryArchive() [2/2]

bool WriteDirectoryArchive ( const wxString & aSourceDir,
wxOutputStream & aOut,
ARCHIVE_FORMAT aFormat,
const wxString & aTopDir )

Write directory entries in sorted archive path order, using the archive root when aTopDir is empty.

Definition at line 861 of file gestfich.cpp.

References DIR_LOOP_GUARD::IsRooted(), and TGZ.

Referenced by GenerateIpc2581File(), generateOneODBPackage(), and WriteDirectoryArchive().

Variable Documentation

◆ PATH_LIST_SEPARATOR

const wxChar PATH_LIST_SEPARATOR = wxT( ':' )
static

Definition at line 284 of file gestfich.cpp.

Referenced by ExecuteCommandThroughShell(), and pathListContains().