-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathAppConsole.vala
More file actions
1391 lines (1137 loc) · 33.9 KB
/
AppConsole.vala
File metadata and controls
1391 lines (1137 loc) · 33.9 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AppConsole.vala
*
* Copyright 2012-2018 Tony George <teejeetech@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
using GLib;
using Gtk;
using Gee;
//using Soup;
using Json;
using TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.JsonHelper;
using TeeJee.ProcessHelper;
using TeeJee.GtkHelper;
using TeeJee.System;
using TeeJee.Misc;
public Main App;
public const string AppName = "Timeshift";
public const string AppShortName = "timeshift";
public const string AppVersion = Constants.VERSION;
public const string AppAuthor = "Tony George";
public const string AppAuthorEmail = "teejeetech@gmail.com";
const string GETTEXT_PACKAGE = "";
const string LOCALE_DIR = "/usr/share/locale";
extern void exit(int exit_code);
public class AppConsole : GLib.Object {
public int snapshot_list_start_index = 0;
public static int main (string[] args) {
set_locale();
LOG_TIMESTAMP = false;
if (args.length > 1) {
switch (args[1].down()) {
case "--help":
case "-h":
stdout.printf (help_message ());
return 0;
case "--version":
stdout.printf (version_message ());
return 0;
}
}
else if (args.length == 1){
stdout.printf (help_message ());
return 0;
}
Main.setup_env();
LOG_ENABLE = false;
init_tmp();
LOG_ENABLE = true;
check_if_admin();
App = new Main(args, false);
parse_arguments(args);
App.initialize();
var console = new AppConsole();
bool ok = console.start_application();
App.exit_app((ok) ? 0 : 1);
return (ok) ? 0 : 1;
}
private static void set_locale() {
log_debug("setting locale...");
Intl.setlocale(GLib.LocaleCategory.MESSAGES, "timeshift");
Intl.textdomain(GETTEXT_PACKAGE);
Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "utf-8");
Intl.bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
}
public static void check_if_admin(){
if (!user_is_admin()) {
log_msg(_("Application needs admin access."));
log_msg(_("Please run the application as admin (using 'sudo' or 'su')"));
//App.exit_app(1);
exit(1);
}
}
// console members --------------
private static void parse_arguments(string[] args){
log_debug("AppConsole: parse_arguments()");
for (int k = 1; k < args.length; k++) // Oth arg is app path
{
switch (args[k].down()){
//case "--backup": // deprecated
case "--check":
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
App.app_mode = "backup";
break;
case "--delete":
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
App.app_mode = "delete";
break;
case "--delete-all":
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
App.app_mode = "delete-all";
break;
case "--restore":
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
App.mirror_system = false;
App.app_mode = "restore";
break;
case "--clone":
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
App.mirror_system = true;
App.app_mode = "restore";
break;
//case "--backup-now": // deprecated
case "--create":
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
App.app_mode = "ondemand";
break;
case "--comment":
case "--comments":
App.cmd_comments = args[++k];
break;
case "--skip-grub":
App.cmd_skip_grub = true;
break;
case "--verbose":
App.cmd_verbose = true;
break;
case "--quiet":
App.cmd_verbose = false;
break;
case "--scripted":
App.cmd_scripted = true;
App.cmd_confirm = true;
break;
case "--yes":
App.cmd_confirm = true;
break;
case "--grub":
case "--grub-device":
App.reinstall_grub2 = true;
App.cmd_grub_device = args[++k];
break;
case "--target":
case "--target-device":
App.cmd_target_device = args[++k];
break;
case "--backup-device":
case "--snapshot-device":
App.cmd_backup_device = args[++k];
break;
case "--snapshot":
case "--snapshot-name":
App.cmd_snapshot = args[++k];
break;
case "--tags":
App.cmd_tags = args[++k];
App.validate_cmd_tags();
break;
case "--debug":
LOG_COMMANDS = true;
LOG_DEBUG = true;
break;
case "--list":
case "--list-snapshots":
App.app_mode = "list-snapshots";
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
break;
case "--list-devices":
App.app_mode = "list-devices";
LOG_TIMESTAMP = false;
LOG_DEBUG = false;
break;
case "--btrfs":
App.btrfs_mode = true;
App.cmd_btrfs_mode = true;
break;
case "--rsync":
App.btrfs_mode = false;
App.cmd_btrfs_mode = false;
break;
case "--backup":
log_error("Option --backup has been replaced by option --check");
log_error("Run 'timeshift --help' to list all available options");
App.exit_app(1);
break;
case "--backup-now":
log_error("Option --backup-now has been replaced by option --create");
log_error("Run 'timeshift --help' to list all available options");
App.exit_app(1);
break;
default:
LOG_TIMESTAMP = false;
log_error("%s: %s".printf(
_("Invalid command line arguments"), args[k]), true);
log_error("Run 'timeshift --help' to list all available options");
App.app_mode = "abort";
App.exit_app(1);
break;
}
}
/* LOG_ENABLE = false; disables all console output
* LOG_TIMESTAMP = false; disables the timestamp prepended to every line in terminal output
* LOG_DEBUG = false; disables additional console messages
* LOG_COMMANDS = true; enables printing of all commands on terminal
* */
//if (app_mode == ""){
//Initialize GTK
// LOG_TIMESTAMP = true;
//}
//Gtk.init(ref args);
//X.init_threads();
}
public bool start_application(){
log_debug("AppConsole: start_application()");
bool is_success = true;
if (App.live_system()){
switch(App.app_mode){
case "backup":
case "ondemand":
log_error(_("Snapshots cannot be created in Live CD mode"));
return false;
}
}
switch(App.app_mode){
case "backup":
is_success = create_snapshot(false);
return is_success;
case "restore":
is_success = restore_snapshot();
return is_success;
case "delete":
is_success = delete_snapshot();
return is_success;
case "delete-all":
is_success = delete_all_snapshots();
return is_success;
case "ondemand":
is_success = create_snapshot(true);
return is_success;
case "list-snapshots":
LOG_ENABLE = true;
App.repo.print_status();
if (App.repo.has_snapshots()){
list_snapshots(false);
log_msg("");
return true;
}
else{
log_msg(_("No snapshots found"));
return false;
}
case "list-devices":
LOG_ENABLE = true;
log_msg("\n" + _("Devices with Linux file systems") + ":\n");
list_all_devices();
log_msg("");
return true;
default:
return true;
}
}
private static string version_message (){
string msg = "%s %s\n".printf( AppName, AppVersion);
return msg;
}
private static string help_message (){
string msg = "\n%s v%s by Tony George (%s)\n".printf(
AppName, AppVersion, AppAuthorEmail);
msg += "\n";
msg += "Syntax:\n";
msg += "\n";
msg += " timeshift --check\n";
msg += " timeshift --create [OPTIONS]\n";
msg += " timeshift --restore [OPTIONS]\n";
msg += " timeshift --delete-[all] [OPTIONS]\n";
msg += " timeshift --list-{snapshots|devices} [OPTIONS]\n";
msg += "\n";
msg += _("Options") + ":\n";
msg += "\n";
msg += _("List") + ":\n";
msg += " --list[-snapshots] " + _("List snapshots") + "\n";
msg += " --list-devices " + _("List devices") + "\n";
msg += "\n";
msg += _("Backup") + ":\n";
msg += " --check " + _("Create snapshot if scheduled") + "\n";
msg += " --create " + _("Create snapshot (even if not scheduled)") + "\n";
msg += " --comments <string> " + _("Set snapshot description") + "\n";
msg += " --tags {O,B,H,D,W,M} " + _("Add tags to snapshot (default: O)") + "\n";;
msg += "\n";
msg += _("Restore") + ":\n";
msg += " --restore " + _("Restore snapshot") + "\n";
//msg += " --clone " + _("Clone current system") + "\n"; // broken feature, not supported
msg += " --snapshot <name> " + _("Specify snapshot to restore") + "\n";
msg += " --target[-device] <device> " + _("Specify target device") + "\n";
msg += " --grub[-device] <device> " + _("Specify device for installing GRUB2 bootloader") + "\n";
msg += " --skip-grub " + _("Skip GRUB2 reinstall") + "\n";
msg += "\n";
msg += _("Delete") + ":\n";
msg += " --delete " + _("Delete snapshot") + "\n";
msg += " --delete-all " + _("Delete all snapshots") + "\n";
msg += "\n";
msg += _("Global") + ":\n";
msg += " --snapshot-device <device> " + _("Specify backup device (default: config)") + "\n";
msg += " --yes " + _("Answer YES to all confirmation prompts") + "\n";
msg += " --btrfs " + _("Switch to BTRFS mode (default: config)") + "\n";
msg += " --rsync " + _("Switch to RSYNC mode (default: config)") + "\n";
msg += " --debug " + _("Show additional debug messages") + "\n";
msg += " --verbose " + _("Show rsync output (default)") + "\n";
msg += " --quiet " + _("Hide rsync output") + "\n";
msg += " --scripted " + _("Run in non-interactive mode") + "\n";
msg += " --help " + _("Show all options") + "\n";
msg += " --version " + _("Print version number") + "\n";
msg += "\n";
msg += _("Examples") + ":\n";
msg += "\n";
msg += "timeshift --list\n";
msg += "timeshift --list --snapshot-device /dev/sda1\n";
msg += "timeshift --create --comments \"after update\" --tags D\n";
msg += "timeshift --restore \n";
msg += "timeshift --restore --snapshot '2014-10-12_16-29-08' --target /dev/sda1\n";
msg += "timeshift --delete --snapshot '2014-10-12_16-29-08'\n";
msg += "timeshift --delete-all \n";
msg += "\n";
msg += _("Notes") + ":\n";
msg += "\n";
msg += " 1) --create will always create a new snapshot\n";
msg += " 2) --check will create a snapshot only if a scheduled snapshot is due\n";
msg += " 3) Use --restore without other options to select options interactively\n";
msg += " 4) UUID can be specified instead of device name\n";
msg += " 5) Default values will be loaded from app config if options are not specified\n";
msg += "\n";
return msg;
}
//console functions
private void list_snapshots(bool paginate, int page_size = 20){
int count = 0;
for(int index = 0; index < App.repo.snapshots.size; index++){
if (!paginate || ((index >= snapshot_list_start_index) && (index < snapshot_list_start_index + page_size))){
count++;
}
}
string[,] grid = new string[count+1,5];
bool[] right_align = { false, false, false, false, false};
int row = 0;
int col = -1;
grid[row, ++col] = _("Num");
grid[row, ++col] = "";
grid[row, ++col] = _("Name");
grid[row, ++col] = _("Tags");
grid[row, ++col] = _("Description");
row++;
for(int index = 0; index < App.repo.snapshots.size; index++){
Snapshot bak = App.repo.snapshots[index];
if (!paginate || ((index >= snapshot_list_start_index) && (index < snapshot_list_start_index + page_size))){
col = -1;
grid[row, ++col] = "%d".printf(index);
grid[row, ++col] = ">";
grid[row, ++col] = "%s".printf(bak.name);
grid[row, ++col] = "%s".printf(bak.taglist_short);
grid[row, ++col] = "%s".printf(bak.description);
row++;
}
}
print_grid(grid, right_align);
}
private void list_devices(Gee.ArrayList<Device> device_list){
string[,] grid = new string[device_list.size+1,6];
bool[] right_align = { false, false, false, true, true, false};
int row = 0;
int col = -1;
grid[row, ++col] = _("Num");
grid[row, ++col] = "";
grid[row, ++col] = _("Device");
//grid[row, ++col] = _("UUID");
grid[row, ++col] = _("Size");
grid[row, ++col] = _("Type");
grid[row, ++col] = _("Label");
row++;
foreach(var pi in device_list) {
col = -1;
grid[row, ++col] = "%d".printf(row - 1);
grid[row, ++col] = ">";
grid[row, ++col] = "%s".printf(pi.device_name_with_parent);
//grid[row, ++col] = "%s".printf(pi.uuid);
grid[row, ++col] = "%s".printf((pi.size_bytes > 0) ? "%s".printf(pi.size) : "?? GB");
grid[row, ++col] = "%s".printf(pi.fstype);
grid[row, ++col] = "%s".printf(pi.label);
row++;
}
print_grid(grid, right_align);
}
private Gee.ArrayList<Device> list_all_devices(){
//add devices
var device_list = new Gee.ArrayList<Device>();
foreach(var dev in Device.get_block_devices_using_lsblk()) {
if (dev.has_linux_filesystem()){
device_list.add(dev);
}
}
string[,] grid = new string[device_list.size+1,6];
bool[] right_align = { false, false, false, true, true, false};
int row = 0;
int col = -1;
grid[row, ++col] = _("Num");
grid[row, ++col] = "";
grid[row, ++col] = _("Device");
//grid[row, ++col] = _("UUID");
grid[row, ++col] = _("Size");
grid[row, ++col] = _("Type");
grid[row, ++col] = _("Label");
row++;
foreach(var pi in device_list) {
col = -1;
grid[row, ++col] = "%d".printf(row - 1);
grid[row, ++col] = ">";
grid[row, ++col] = "%s".printf(pi.device_name_with_parent);
//grid[row, ++col] = "%s".printf(pi.uuid);
grid[row, ++col] = "%s".printf((pi.size_bytes > 0) ? format_file_size(pi.size_bytes): "?? GB");
grid[row, ++col] = "%s".printf(pi.fstype);
grid[row, ++col] = "%s".printf(pi.label);
row++;
}
print_grid(grid, right_align);
return device_list;
}
private Gee.ArrayList<Device> list_grub_devices(bool print_to_console = true){
//add devices
var grub_device_list = new Gee.ArrayList<Device>();
foreach(var dev in Device.get_block_devices_using_lsblk()) {
if (dev.type == "disk"){
grub_device_list.add(dev);
}
else if (dev.type == "part"){
if (dev.has_linux_filesystem()){
grub_device_list.add(dev);
}
}
// skip crypt/loop
}
/*Note: Lists are already sorted. No need to sort again */
string[,] grid = new string[grub_device_list.size+1,4];
bool[] right_align = { false, false, false, false };
int row = 0;
int col = -1;
grid[row, ++col] = _("Num");
grid[row, ++col] = "";
grid[row, ++col] = _("Device");
grid[row, ++col] = _("Description");
row++;
string desc = "";
foreach(Device pi in grub_device_list) {
col = -1;
grid[row, ++col] = "%d".printf(row - 1);
grid[row, ++col] = ">";
grid[row, ++col] = "%s".printf(pi.short_name_with_alias);
if (pi.type == "disk"){
desc = "%s".printf(((pi.vendor.length > 0)||(pi.model.length > 0)) ? (pi.vendor + " " + pi.model + " [MBR]") : "");
}
else{
desc = "%5s, ".printf(pi.fstype);
desc += "%10s".printf((pi.size_bytes > 0) ? "%s GB".printf(pi.size) : "?? GB");
desc += "%s".printf((pi.label.length > 0) ? ", " + pi.label : "");
}
grid[row, ++col] = "%s".printf(desc);
row++;
}
print_grid(grid, right_align);
return grub_device_list;
}
private void print_grid(string[,] grid, bool[] right_align, bool has_header = true){
int[] col_width = new int[grid.length[1]];
for(int col=0; col<grid.length[1]; col++){
for(int row=0; row<grid.length[0]; row++){
if (grid[row,col].length > col_width[col]){
col_width[col] = grid[row,col].length;
}
}
}
for(int row=0; row<grid.length[0]; row++){
for(int col=0; col<grid.length[1]; col++){
string fmt = "%" + (right_align[col] ? "+" : "-") + col_width[col].to_string() + "s ";
stdout.printf(fmt.printf(grid[row,col]));
}
stdout.printf("\n");
if (has_header && (row == 0)){
stdout.printf(string.nfill(78, '-'));
stdout.printf("\n");
}
}
}
// create
private bool create_snapshot(bool ondemand){
select_snapshot_device(false);
return App.create_snapshot(ondemand, null);
}
// restore
private bool restore_snapshot(){
select_snapshot_device(true);
select_snapshot_for_restore();
if (!App.cmd_scripted) {
stdout.printf("\n\n");
log_msg(string.nfill(78, '*'));
stdout.printf(_("To restore with default options, press the ENTER key for all prompts!") + "\n");
log_msg(string.nfill(78, '*'));
stdout.printf(_("\nPress ENTER to continue..."));
stdout.flush();
stdin.read_line();
}
init_mounts();
if (!App.btrfs_mode){
map_devices();
select_grub_device();
}
confirm_restore();
bool ok = App.mount_target_devices();
if (!ok){
return false;
}
return App.restore_snapshot(null);
}
private void select_snapshot_device(bool prompt_if_empty){
if (App.mirror_system){
return;
}
var list = new Gee.ArrayList<Device>();
foreach(var pi in App.partitions){
if (pi.has_linux_filesystem()){
list.add(pi);
}
}
if ((App.repo.device == null) || (prompt_if_empty && (App.repo.snapshots.size == 0))){
//prompt user for backup device
log_msg("");
if (App.cmd_scripted){
if (App.repo.device == null){
if (App.backup_uuid.length == 0){
log_debug("device is null");
string status_message = _("Snapshot device not selected");
log_msg(status_message);
}
else{
string status_message = _("Snapshot device not available");
string status_details = _("Device not found") + ": UUID='%s'".printf(App.backup_uuid);
log_msg(status_message);
log_msg(status_details);
}
}
App.exit_app(1);
}
log_msg(_("Select backup device") + ":\n");
list_devices(list);
log_msg("");
Device dev = null;
int attempts = 0;
while (dev == null){
attempts++;
if (attempts > 3) { break; }
stdout.printf("" +
_("Enter device name or number (a=Abort)") + ": ");
stdout.flush();
dev = read_stdin_device(list, "");
if (App.btrfs_mode && !App.check_device_for_backup(dev, true)){
log_error(_("Selected snapshot device is not a system disk"));
log_error(_("Select BTRFS system disk with root subvolume (@)"));
dev = null;
}
}
log_msg("");
if (dev == null){
log_error(_("Failed to get input from user in 3 attempts"));
log_msg(_("Aborted."));
App.exit_app(1);
}
App.repo = new SnapshotRepo.from_device(dev, null, App.btrfs_mode);
if (!App.repo.available()){
App.exit_app(1);
}
}
}
private Snapshot? select_snapshot(){
Snapshot selected_snapshot = null;
log_debug("AppConsole: select_snapshot()");
if (App.mirror_system){
return null;
}
if (App.cmd_snapshot.length > 0){
//check command line arguments
bool found = false;
foreach(var bak in App.repo.snapshots) {
if (bak.name == App.cmd_snapshot){
return bak;
}
}
//check if found
if (!found){
log_error(_("Could not find snapshot") + ": '%s'".printf(App.cmd_snapshot));
return null;
}
}
//prompt user for snapshot
if (selected_snapshot == null){
if (!App.repo.has_snapshots()){
log_error(_("No snapshots found on device") + ": '%s'".printf(App.repo.device.device));
App.exit_app(0);
return null;
}
if (App.cmd_scripted){
log_error("No snapshots selected. Use --snapshot to specify snapshots");
App.exit_app(1);
return null;
}
log_msg("");
log_msg(_("Select snapshot") + ":\n");
list_snapshots(true);
log_msg("");
int attempts = 0;
while (selected_snapshot == null){
attempts++;
if (attempts > 3) { break; }
stdout.printf(_("Enter snapshot number (a=Abort, p=Previous, n=Next)") + ": ");
stdout.flush();
selected_snapshot = read_stdin_snapshot();
}
log_msg("");
if (selected_snapshot == null){
log_error(_("Failed to get input from user in 3 attempts"));
log_msg(_("Aborted."));
App.exit_app(0);
}
}
return selected_snapshot;
}
private void select_snapshot_for_restore(){
App.snapshot_to_restore = select_snapshot();
if (App.snapshot_to_restore == null){
log_error("Snapshot not selected");
App.exit_app(1);
}
}
private void select_snapshot_for_deletion(){
App.snapshot_to_delete = select_snapshot();
if (App.snapshot_to_delete == null){
log_error("Snapshot not selected");
App.exit_app(1);
}
}
private void init_mounts(){
log_debug("AppConsole: init_mounts()");
App.init_mount_list();
// remove mount points which will remain on root fs
for(int i = App.mount_list.size-1; i >= 0; i--){
var entry = App.mount_list[i];
if (entry.device == null){
App.mount_list.remove(entry);
}
}
}
private void map_devices(){
log_debug("AppConsole: map_devices()");
if (App.cmd_target_device.length > 0){
//check command line arguments
bool found = false;
foreach(Device pi in App.partitions) {
if (!pi.has_linux_filesystem()) { continue; }
if ((pi.device == App.cmd_target_device)||((pi.uuid == App.cmd_target_device))){
App.dst_root = pi;
found = true;
break;
}
else {
foreach(string symlink in pi.symlinks){
if (symlink == App.cmd_target_device){
App.dst_root = pi;
found = true;
break;
}
}
if (found){ break; }
}
}
//check if found
if (!found){
log_error(_("Could not find device") + ": '%s'".printf(App.cmd_target_device));
App.exit_app(1);
return;
}
}
for(int i = 0; i < App.mount_list.size; i++){
MountEntry mnt = App.mount_list[i];
Device dev = null;
string default_device = "";
log_debug("selecting: %s".printf(mnt.mount_point));
// no need to ask user to map remaining devices if restoring same system
if ((App.dst_root != null) && (App.sys_root != null)
&& (App.dst_root.uuid == App.sys_root.uuid)){
break;
}
if (App.mirror_system){
default_device = (App.dst_root != null) ? App.dst_root.device : "";
}
else{
if (mnt.device != null){
default_device = mnt.device.device;
}
else{
default_device = (App.dst_root != null) ? App.dst_root.device : "";
}
}
if (App.cmd_scripted){
dev = Device.get_device_by_name(default_device);
if (dev == null){
log_error("Failed to get device by name %s".printf(default_device));
App.exit_app(1);
}
}
//prompt user for device
if (dev == null){
log_msg("");
log_msg(_("Select '%s' device (default = %s)").printf(
mnt.mount_point, default_device) + ":\n");
var device_list = list_all_devices();
log_msg("");
int attempts = 0;
while (dev == null){
attempts++;
if (attempts > 3) { break; }
stdout.printf("" +
_("[ENTER = Default (%s), r = Root device, a = Abort]").printf(default_device) + "\n\n");
stdout.printf(
_("Enter device name or number")
+ ": ");
stdout.flush();
dev = read_stdin_device_mounts(device_list, mnt);
}
log_msg("");
if (dev == null){
log_error(_("Failed to get input from user in 3 attempts"));
log_msg(_("Aborted."));
App.exit_app(0);
}
}
if (dev != null){
log_debug("selected: %s".printf(dev.uuid));
mnt.device = dev;
log_msg(string.nfill(78, '*'));
if ((mnt.mount_point != "/")
&& (App.dst_root != null)
&& (dev.device == App.dst_root.device)){
log_msg(_("'%s' will be on root device").printf(mnt.mount_point), true);
}
else{
log_msg(_("'%s' will be on '%s'").printf(
mnt.mount_point, mnt.device.short_name_with_alias), true);
//log_debug("UUID=%s".printf(dst_root.uuid));
}
log_msg(string.nfill(78, '*'));
}
}
}
private void select_grub_device(){
string grub_device_default = App.grub_device;
bool grub_reinstall_default = App.reinstall_grub2;
App.reinstall_grub2 = false;
App.grub_device = "";
if (App.cmd_grub_device.length > 0){
log_debug("Grub device is specified as command argument");
//check command line arguments
bool found = false;
var device_list = list_grub_devices(false);
foreach(Device dev in device_list) {
if ((dev.device == App.cmd_grub_device)
||((dev.uuid.length > 0) && (dev.uuid == App.cmd_grub_device))){
App.grub_device = dev.device;
found = true;
break;
}
else {
if (dev.type == "part"){
foreach(string symlink in dev.symlinks){
if (symlink == App.cmd_grub_device){
App.grub_device = dev.device;
found = true;
break;
}
}
if (found){ break; }
}
}
}
//check if found
if (!found){
log_error(_("Could not find device") + ": '%s'".printf(App.cmd_grub_device));
App.exit_app(1);
return;