KiCad PCB EDA Suite
Loading...
Searching...
No Matches
downgrade_copy.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7#pragma once
8
9#include <filesystem>
10#include <string>
11#include <system_error>
12#include <vector>
13#include <wx/filename.h>
14#include <wx/intl.h>
16#include <downgrade_scan.h>
17#include <downgrade_target.h>
18#include <kiplatform/io.h>
19
20inline std::filesystem::path DowngradeNativePath( const wxString& aPath )
21{
22#ifdef _WIN32
23 return std::filesystem::path( aPath.ToStdWstring() );
24#else
25 return std::filesystem::path( aPath.utf8_string() );
26#endif
27}
28
29inline wxString DowngradeWxPath( const std::filesystem::path& aPath )
30{
31#ifdef _WIN32
32 return wxString( aPath.wstring() );
33#else
34 return wxString::FromUTF8( aPath.string() );
35#endif
36}
37
39inline bool ValidateDowngradeReportPath( const wxString& aPath, wxString& aError )
40{
41 if( !IsDowngradeDesignFile( aPath ) )
42 return true;
43
44 aError = _( "The report file must not be a design or library file." );
45 return false;
46}
47
48
51inline bool WriteDowngradeReport( const COMPATIBILITY_REPORT& aReport, const wxString& aTarget, const wxString& aPath,
52 wxString& aError )
53{
54 if( aPath.IsEmpty() )
55 return true;
56
57 const DOWNGRADE_TARGET* target =
58 aTarget.IsEmpty() ? &GetDowngradeTargets().front() : FindDowngradeTarget( aTarget );
59
60 if( !target )
61 return true;
62
63 const std::string text( aReport.ToText( target->m_name ).ToUTF8() );
64
65 if( !KIPLATFORM::IO::AtomicWriteFile( aPath, text.data(), text.size(), &aError ) )
66 {
67 aError = wxString::Format( _( "Could not write the report to '%s'." ), aPath );
68 return false;
69 }
70
71 return true;
72}
73
74
75inline bool ValidateFreshDowngradeOutput( const wxString& aOutput, wxString& aError )
76{
77 if( aOutput.IsEmpty() )
78 {
79 aError = _( "An output path is required. Use --output." );
80 return false;
81 }
82
83 wxFileName output( aOutput );
84
85 // Match the normalization used by transaction destinations before testing freshness.
86 if( !output.MakeAbsolute() )
87 {
88 aError = _( "The output path is not accessible." );
89 return false;
90 }
91
92 std::error_code error;
93 auto status = std::filesystem::symlink_status( DowngradeNativePath( output.GetFullPath() ), error );
94
95 if( ( error && error != std::errc::no_such_file_or_directory )
96 || status.type() != std::filesystem::file_type::not_found )
97 {
98 aError = _( "The output path already exists or is inaccessible. Choose a new destination." );
99 return false;
100 }
101
102 return true;
103}
104
105inline bool IsDowngradeLibraryWorkingArtifact( const wxString& aPath )
106{
107 wxFileName file( aPath );
108 return IsDowngradeWorkingArtifact( aPath ) || file.GetExt() == wxT( "lck" ) || file.GetExt() == wxT( "kicad_prl" )
109 || file.GetFullName().Contains( wxT( ".downgrade_tmp." ) )
110 || file.GetFullName().Contains( wxT( ".downgrade_backup." ) );
111}
112
113inline bool ValidateDowngradeLibrarySource( const wxString& aSource, wxString& aError )
114{
115 std::error_code error;
116 auto source = std::filesystem::canonical( DowngradeNativePath( aSource ), error );
117
118 if( error || !std::filesystem::is_directory( source, error ) || error )
119 {
120 aError = _( "The library folder is not accessible." );
121 return false;
122 }
123
124 std::filesystem::recursive_directory_iterator iterator( source, error );
125 const std::filesystem::recursive_directory_iterator end;
126
127 while( !error && iterator != end )
128 {
129 const auto& entry = *iterator;
130 wxString path = DowngradeWxPath( entry.path() );
131
133 {
134 iterator.disable_recursion_pending();
135 }
136 else if( entry.is_symlink( error ) && entry.is_directory( error ) )
137 {
138 aError = _( "Library folders containing directory links cannot be copied safely." );
139 return false;
140 }
141 else if( !error && !entry.is_directory( error ) && !entry.is_regular_file( error ) )
142 {
143 aError = _( "The library folder contains an inaccessible or unsupported file." );
144 return false;
145 }
146
147 if( !error )
148 iterator.increment( error );
149 }
150
151 if( error )
152 {
153 aError = _( "The library folder could not be read completely." );
154 return false;
155 }
156
157 return true;
158}
159
160inline bool ValidateDowngradeLibraryCopy( const wxString& aSource, const wxString& aDestination, wxString& aError )
161{
162 if( !ValidateFreshDowngradeOutput( aDestination, aError ) || !ValidateDowngradeLibrarySource( aSource, aError ) )
163 {
164 return false;
165 }
166
167 std::error_code error;
168 auto source = std::filesystem::canonical( DowngradeNativePath( aSource ), error );
169 auto destination = std::filesystem::weakly_canonical( DowngradeNativePath( aDestination ), error );
170
171 if( error )
172 {
173 aError = _( "The output path is not accessible." );
174 return false;
175 }
176
177 auto relative = destination.lexically_relative( source );
178
179 if( destination == source || ( !relative.empty() && *relative.begin() != ".." ) )
180 {
181 aError = _( "The library output must be outside the source library folder." );
182 return false;
183 }
184
185 return true;
186}
187
189{
190public:
194
196 {
197 if( m_committed )
198 return;
199
200 for( auto it = m_created.rbegin(); it != m_created.rend(); ++it )
201 {
202 std::error_code error;
203
204 if( it->second )
205 std::filesystem::remove_all( it->first, error );
206 else
207 std::filesystem::remove( it->first, error );
208 }
209 }
210
211 bool Create( const wxString& aDirectory, bool aOwnContents = false )
212 {
213 std::error_code error;
214 auto directory = std::filesystem::absolute( DowngradeNativePath( aDirectory ), error ).lexically_normal();
215
216 if( error )
217 return false;
218
219 std::vector<std::filesystem::path> missing;
220
221 for( auto path = directory; !std::filesystem::exists( path, error ); path = path.parent_path() )
222 {
223 if( error || path.empty() || path == path.parent_path() )
224 return false;
225
226 missing.push_back( path );
227 }
228
229 if( error || ( aOwnContents && missing.empty() ) )
230 return false;
231
232 for( auto it = missing.rbegin(); it != missing.rend(); ++it )
233 {
234 if( !std::filesystem::create_directory( *it, error ) || error )
235 return false;
236
237 m_created.emplace_back( *it, aOwnContents && *it == directory );
238 }
239
240 return std::filesystem::is_directory( directory, error ) && !error;
241 }
242
243 void Commit() { m_committed = true; }
244
245private:
246 std::vector<std::pair<std::filesystem::path, bool>> m_created;
247 bool m_committed = false;
248};
249
250inline wxString DowngradeLibraryCopyPath( const wxString& aLibrary, const wxString& aOutputDirectory )
251{
252 std::error_code error;
253 auto source = std::filesystem::canonical( DowngradeNativePath( aLibrary ), error );
254
255 if( error )
256 return wxEmptyString;
257
258 return DowngradeWxPath( DowngradeNativePath( aOutputDirectory ) / source.filename() );
259}
260
261inline bool CopyDowngradeLibraryDirectory( const wxString& aSource, const wxString& aDestination,
262 DOWNGRADE_OUTPUT_DIRECTORIES& aDirectories, wxString& aError )
263{
264 if( !ValidateDowngradeLibraryCopy( aSource, aDestination, aError ) )
265 return false;
266
267 if( !aDirectories.Create( aDestination, true ) )
268 {
269 aError = _( "Could not create the library output folder." );
270 return false;
271 }
272
273 std::error_code error;
274 auto source = std::filesystem::canonical( DowngradeNativePath( aSource ), error );
275 std::filesystem::recursive_directory_iterator iterator( source, error );
276 const std::filesystem::recursive_directory_iterator end;
277
278 while( !error && iterator != end )
279 {
280 const auto& entry = *iterator;
281
283 {
284 iterator.disable_recursion_pending();
285 }
286 else
287 {
288 auto destination = DowngradeNativePath( aDestination ) / entry.path().lexically_relative( source );
289
290 if( entry.is_directory( error ) )
291 std::filesystem::create_directory( destination, error );
292 else if( !error )
293 std::filesystem::copy_file( entry.path(), destination, error );
294 }
295
296 if( !error )
297 iterator.increment( error );
298 }
299
300 if( error )
301 {
302 aError = _( "Could not copy the library folder." );
303 return false;
304 }
305
306 return true;
307}
What an export to a given target will keep, approximate, drop, or block.
wxString ToText(const wxString &aTargetName) const
A readable preview of the classified export, including counts and feature details.
std::vector< std::pair< std::filesystem::path, bool > > m_created
DOWNGRADE_OUTPUT_DIRECTORIES(const DOWNGRADE_OUTPUT_DIRECTORIES &)=delete
DOWNGRADE_OUTPUT_DIRECTORIES & operator=(const DOWNGRADE_OUTPUT_DIRECTORIES &)=delete
bool Create(const wxString &aDirectory, bool aOwnContents=false)
wxString DowngradeWxPath(const std::filesystem::path &aPath)
wxString DowngradeLibraryCopyPath(const wxString &aLibrary, const wxString &aOutputDirectory)
bool IsDowngradeLibraryWorkingArtifact(const wxString &aPath)
bool ValidateDowngradeReportPath(const wxString &aPath, wxString &aError)
A report destination that is a design or library file is a mistyped path, not a report.
bool ValidateDowngradeLibrarySource(const wxString &aSource, wxString &aError)
bool ValidateDowngradeLibraryCopy(const wxString &aSource, const wxString &aDestination, wxString &aError)
bool ValidateFreshDowngradeOutput(const wxString &aOutput, wxString &aError)
bool CopyDowngradeLibraryDirectory(const wxString &aSource, const wxString &aDestination, DOWNGRADE_OUTPUT_DIRECTORIES &aDirectories, wxString &aError)
bool WriteDowngradeReport(const COMPATIBILITY_REPORT &aReport, const wxString &aTarget, const wxString &aPath, wxString &aError)
Write the readable compatibility report, the same text the export dialog shows.
std::filesystem::path DowngradeNativePath(const wxString &aPath)
bool IsDowngradeWorkingArtifact(const wxString &aPath)
Editor working state (autosaves, local history, backups) holds current-format copies nothing downgrad...
bool IsDowngradeDesignFile(const wxString &aPath)
Files the project export carries over.
const std::vector< DOWNGRADE_TARGET > & GetDowngradeTargets()
Supported downgrade targets, newest first.
const DOWNGRADE_TARGET * FindDowngradeTarget(const wxString &aNameOrId)
Match a user-supplied target string against name or stable id, case-insensitively.
#define _(s)
std::string source
bool AtomicWriteFile(const wxString &aTargetPath, const void *aData, size_t aSize, wxString *aError=nullptr)
Writes aData to aTargetPath via a sibling temp file, fsyncs the data and directory,...
An older KiCad release we can export a project back to.
wxString m_name
User-facing name, e.g. "KiCad 10.0".
std::string path
VECTOR2I end