-
Notifications
You must be signed in to change notification settings - Fork 1
/
actionpage.cpp
329 lines (270 loc) · 8.15 KB
/
actionpage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright Arnt Gulbrandsen, [email protected].
#include "actionpage.h"
#include <QGridLayout>
#include <QGridLayout>
#include <QFileDialog>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QDateTime>
#include <QMessageBox>
#include <iostream>
/*! \class ActionPage actionpage.h
The ActionPage is the other mandatory page. This is what performs
a backup, or writes a script.
*/
/*! Constructs a
*/
ActionPage::ActionPage( BackupWizard * parent, IncludePage * details )
: QWizardPage( parent ),
archived( 0 ),
unusualOutput( new QTextEdit( this ) ),
lastArchived( new QLineEdit( this ) ),
writeScript( new QPushButton( tr( "&Write Backup Script" ), this ) ),
backup( new QPushButton( tr( "&Perform backup" ), this ) ),
tarsnap( nullptr ),
w( parent ),
inclusions( details )
{
setTitle( tr( "Back up" ) );
setSubTitle( tr( "Perform backup, or write a script for later" ) );
connect( backup, SIGNAL(clicked()),
this, SLOT(performBackup()) );
connect( writeScript, SIGNAL(clicked()),
this, SLOT(saveScript()) );
QGridLayout * l = new QGridLayout( this );
l->addWidget( writeScript,
0, 0 );
l->addWidget( backup,
0, 1 );
l->addWidget( lastArchived,
1, 0, 1, 3 );
l->addWidget( unusualOutput,
2, 0, 1, 3 );
lastArchived->hide();
unusualOutput->hide();
l->setColumnStretch( 2, 2 );
}
/*! Runs one entire backup using the current configuration, displaying
progress information along the way.
*/
void ActionPage::performBackup()
{
QString key = field( "keyFile" ).toString();
QString cache = field( "cacheDirectory" ).toString();
QString backupName( field( "host" ).toString() );
QDateTime now = QDateTime::currentDateTime();
backupName.append( now.toString( "-yyyy-MM-dd-hh-mm" ) );
if ( tarsnap )
delete tarsnap;
tarsnap = new QProcess( this );
connect( tarsnap, SIGNAL(readyReadStandardOutput()),
this, SLOT(read()) );
connect( tarsnap, SIGNAL(finished(int, QProcess::ExitStatus)),
this, SLOT(finish(int, QProcess::ExitStatus)) );
tarsnap->setWorkingDirectory( field( "baseDirectory").toString() );
tarsnap->setProcessChannelMode( QProcess::MergedChannels );
QStringList cli = commandLine( backupName );
tarsnap->start( "/usr/local/bin/tarsnap", cli );
lastArchived->show();
lastArchived->setText( "" );
unusualOutput->setText( "" );
}
/*! Writes a script to carry out the currently configured backup,
asking the user for a suitable name.
*/
void ActionPage::saveScript()
{
QString name = QFileDialog::getSaveFileName( this );
if ( name.isNull() )
return;
QFile script( name );
if ( !script.open( QIODevice::WriteOnly |
QIODevice::Text |
QIODevice::Truncate) )
return;
QString key = field( "keyFile" ).toString();
QString cache = field( "cacheDirectory" ).toString();
QTextStream out( &script );
out << "#!/bin/sh\n"
<< "\n"
<< "# Generated at "
<< QDateTime::currentDateTime().toString( "yyyy-MM-dd hh:mm:ss" )
<< " by tarsnap-gui.\n" // might want a version number
<< "\n"
<< "if [ ! -r \"" << key << "\" ]; then\n"
<< " echo Cannot access " << key << "\n"
<< " [ 0 -eq $(id -u) ] || echo Root access may be needed.\n"
<< " exit 1\n"
<< "fi\n"
<< "\n"
<< "# Feel free to delete everything above this point. And to change\n"
<< "# this filename pattern to something you like. It is probably a\n"
<< "# good idea to remove the -v option from the tarsnap invocation\n"
<< "# once you have seen it work correctly.\n"
<< "\n"
<< "cd " << shQuoted( field( "baseDirectory" ).toString() ) << "\n"
<< "\n"
<< "FN=$(/bin/hostname)-$(date +%Y-%m-%d-%H-%M)\n"
<< "\n";
QStringList l;
l << "tarsnap";
l << commandLine( "$FN" );
auto s = l.begin();
int col = 0;
while ( s != l.end() ) {
QString quoted;
if ( *s == "$FN" )
quoted = *s;
else
quoted = shQuoted( *s );
if ( col == 0 ) {
// it's the start. just do it.
} else if ( col + quoted.length() > 75 &&
quoted.length() < 75 ) {
// would wrap and making a new line helps
out << " \\\n ";
col = 2;
} else {
// not too wide, or too wide anyway
out << " ";
col++;
}
out << quoted;
col += quoted.length();
++s;
}
out << "\n";
script.setPermissions( QFile::ReadOwner |
QFile::WriteOwner |
QFile::ExeOwner |
QFile::ReadGroup |
QFile::ExeGroup |
QFile::ReadOther |
QFile::ExeOther );
}
/*! Returns the tarsnap command line to perform one backup.
*/
QStringList ActionPage::commandLine( const QString & filename ) const
{
QStringList result;
result << "-c"
<< "-f"
<< filename
<< "-v"
<< "--keyfile"
<< field( "keyFile" ).toString()
<< "--cachedir"
<< field( "cacheDirectory" ).toString();
if ( !field( "crossMountPoints" ).toBool() )
result << "--one-file-system";
if( field( "something" ).toBool() )
result << inclusions->selectedDirectories();
else
result << ".";
return result;
}
/*! Returns a sh-quoted version of \a s.
This is rather conservative; it quotes with ' at the slightest
hint of trouble.
*/
QString ActionPage::shQuoted( const QString & s ) const
{
int i = 0;
while ( i < s.length() &&
( s[i] == '-' || s[i] == '/' || s[i] == '.' ||
s[i].isLetterOrNumber() ) )
i++;
if ( !s.isEmpty() && i == s.length() )
return s;
QString result = "'";
i = 0;
while ( i < s.length() ) {
if ( s[i] == '\'' || s[i] == '\\' || s[i] < ' ' )
result += '\\';
result += s[i];
i++;
}
result += "\'";
return result;
}
/*! This slot reads tarsnap's output and puts it onscreen. Only the
last filename is displayed because that's routine and not terribly
interesting, but all the odd and unusual output is shown to the
user.
*/
void ActionPage::read()
{
partialLine.append( tarsnap->readAll() );
int bol = 0;
bool progress = true;
QString filename;
QString other;
while ( progress ) {
progress = false;
int i = bol;
while ( i < partialLine.size() && partialLine[i] != '\n' )
i++;
if ( i < partialLine.size() ) {
progress = true;
if ( i - bol >= 3 &&
partialLine[bol] == 'a' &&
partialLine[bol+1] == ' ' ) {
filename = QString::fromUtf8( partialLine.data() + bol + 2,
i - bol - 2 );
archived++;
} else {
other.append( QString::fromUtf8( partialLine.data() + bol,
i + 1 - bol ) );
}
bol = i + 1;
}
}
partialLine = partialLine.mid( bol );
unusualOutput->append( other );
if ( !unusualOutput->isVisible() && !other.isEmpty() )
unusualOutput->show();
if ( !filename.isEmpty() )
lastArchived->setText( filename );
}
/*! This slot changes the UI to signal that the backup is done. */
void ActionPage::finish(int code, QProcess::ExitStatus status)
{
if ( status != QProcess::NormalExit && code != 0 ) {
QMessageBox::critical( wizard(),
tr( "Error running tarsnap" ),
tr( "Oddly, tarsnap did not finish "
"normally. It seems likely that something "
"may have failed. The command line that "
"failed was something like "
"<code>tarsnap %1</code>" )
.arg( commandLine( "hostname" ).join( " " ) ) );
}
// this is ungood. a label would be better. where should it go?
lastArchived->setText( tr( "Done; %1 files archived" ).arg( archived ) );
delete tarsnap;
tarsnap = nullptr;
}
/*! Clicking Back aborts any running backup. Is that perfect? Not sure. */
void ActionPage::cleanupPage()
{
if ( !tarsnap )
return;
tarsnap->terminate();
// we leak tarsnap rather than wait for termination to complete
tarsnap = nullptr;
}
/*! This reimplementation of validatePage() doesn't actually validate,
it just prepares for going away.
*/
bool ActionPage::validatePage()
{
if ( tarsnap && tarsnap->state() == QProcess::Running )
QMessageBox::critical( this,
tr( "A backup is running" ),
tr( "The backup may be interrupted when you "
"leave this program. Perhaps you should "
"write a backup script and run that." ) );
w->nag();
return true;
}