KiCad PCB EDA Suite
Loading...
Searching...
No Matches
db_backend_support.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "db_backend_support.h"
21
22#include <cctype>
23#include <cstddef>
24#include <utility>
25
26#include <wx/intl.h>
27#include <wx/string.h>
28
29
31{
32 const wxString path = wxString::FromUTF8( aPath );
33
34 switch( aOutcome )
35 {
37 return { DB_FAILURE::TIMEOUT,
38 utf8( wxString::Format( _( "still waiting on an earlier request for %s" ), path ) ),
40
42 return { DB_FAILURE::TIMEOUT, utf8( wxString::Format( _( "%s is busy with an earlier request" ), path ) ),
44
46 return { DB_FAILURE::UNKNOWN, utf8( wxString::Format( _( "could not start a thread to read %s" ), path ) ),
48
49 default:
50 return { DB_FAILURE::TIMEOUT, utf8( wxString::Format( _( "timed out waiting for %s" ), path ) ),
52 }
53}
54
55
56DB_READ_DELIVERY::DB_READ_DELIVERY( std::vector<std::string>& aLabels, const DB_ROW_SINK& aSink ) :
57 m_labels( aLabels ),
58 m_sink( aSink ),
59 m_jobLabels( std::make_shared<std::vector<std::string>>() )
60{
61 m_deliver = [this]( std::vector<DB_VALUE>&& aRow )
62 {
63 if( !m_labelsCopied )
64 {
66 m_labelsCopied = true;
67 }
68
69 try
70 {
71 m_sink( std::move( aRow ) );
72 }
73 catch( ... )
74 {
75 m_sinkThrew = true;
76 throw;
77 }
78 };
79}
80
81
87
88
90{
91 return utf8( _( "the rows read from the database were not accepted" ) );
92}
93
94
95std::string ScrubSecret( std::string aText, const std::string& aSecret )
96{
97 static constexpr size_t EVERYWHERE = 4;
98
99 if( aSecret.empty() )
100 return aText;
101
102 auto inWord = []( unsigned char aChar )
103 {
104 return std::isalnum( aChar ) || aChar >= 0x80;
105 };
106
107 auto scrub = [&]( const std::string& aForm )
108 {
109 for( size_t at = aText.find( aForm ); at != std::string::npos; )
110 {
111 const size_t end = at + aForm.size();
112 const bool alone =
113 ( at == 0 || !inWord( aText[at - 1] ) ) && ( end >= aText.size() || !inWord( aText[end] ) );
114
115 if( aForm.size() >= EVERYWHERE || alone )
116 {
117 aText.replace( at, aForm.size(), "***" );
118 at = aText.find( aForm, at + 3 );
119 }
120 else
121 {
122 at = aText.find( aForm, at + 1 );
123 }
124 }
125 };
126
127 // The doubled form first, since it contains the raw one only where a quote is absent
128 if( aSecret.find( '"' ) != std::string::npos )
129 {
130 std::string doubled;
131
132 for( char c : aSecret )
133 {
134 doubled += c;
135
136 if( c == '"' )
137 doubled += c;
138 }
139
140 scrub( doubled );
141 }
142
143 scrub( aSecret );
144 return aText;
145}
146
147
148std::string RedactDetail( const std::string& aDetail, const std::string& aSecret )
149{
150 static constexpr size_t SHORT_SECRET = 4;
151
152 if( !aSecret.empty() && aSecret.size() < SHORT_SECRET )
153 return std::string();
154
155 return ScrubSecret( aDetail, aSecret );
156}
const DB_ROW_SINK & m_sink
DB_READ_DELIVERY(std::vector< std::string > &aLabels, const DB_ROW_SINK &aSink)
std::shared_ptr< std::vector< std::string > > m_jobLabels
std::vector< std::string > & m_labels
void Finish()
Copies the labels out if no row did, after the call returned a result.
static std::string SinkFailureMessage()
The message for a read the caller's sink gave up on.
OUTCOME
Why the last Run or RunCleanup returned without a result.
@ UNAVAILABLE
the worker thread could not start
@ REFUSED
two abandoned jobs hold the source, or no slot freed within the bound
@ TOO_LATE
a slot freed with under half the bound left, so nothing was started
std::function< void(std::vector< DB_VALUE > &&)> DB_ROW_SINK
Receives one row at a time, in the order of the labels the read produced.
@ TIMEOUT
The source did not answer within the timeout.
DB_WORKER_FAILURE DescribeWorkerOutcome(DB_WORKER_THREAD::OUTCOME aOutcome, const std::string &aPath)
Maps the outcome of a call that returned no result to a failure and a connection decision.
std::string ScrubSecret(std::string aText, const std::string &aSecret)
Replaces a secret in a message, as ***.
std::string RedactDetail(const std::string &aDetail, const std::string &aSecret)
The provider text to show for a call made with aSecret, which is nothing at all when the secret is sh...
std::string utf8(const wxString &aText)
#define _(s)
STL namespace.
What a backend reports, and what it does with its connection, when a worker call did not complete.
@ KEEP
Nothing ran, so the connection is intact.
@ DROP
The connection is closed through the worker.
@ FORGET
The stuck job releases the connection itself when its blocked call returns.
std::string path
VECTOR2I end