-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathSnapshotRepo.vala
More file actions
1013 lines (764 loc) · 25.8 KB
/
SnapshotRepo.vala
File metadata and controls
1013 lines (764 loc) · 25.8 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
/*
* SnapshotRepo.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 TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.JsonHelper;
using TeeJee.ProcessHelper;
using TeeJee.GtkHelper;
using TeeJee.System;
using TeeJee.Misc;
public class SnapshotRepo : GLib.Object{
public Device device = null;
public Device device_home = null; // used for btrfs mode only
public string mount_path = "";
public Gee.HashMap<string,string> mount_paths;
public bool btrfs_mode = false;
public Gee.ArrayList<Snapshot?> snapshots;
public Gee.ArrayList<Snapshot?> invalid_snapshots;
public string status_message = "";
public string status_details = "";
public SnapshotLocationStatus status_code;
public bool last_snapshot_failed_space = false;
// private
private Gtk.Window? parent_window = null;
private bool thr_success = false;
private bool thr_running = false;
private string thr_args1 = "";
public SnapshotRepo.from_path(string path, Gtk.Window? parent_win, bool _btrfs_mode){
log_debug("SnapshotRepo: from_path()");
this.mount_path = path;
this.parent_window = parent_win;
this.btrfs_mode = _btrfs_mode;
snapshots = new Gee.ArrayList<Snapshot>();
invalid_snapshots = new Gee.ArrayList<Snapshot>();
mount_paths = new Gee.HashMap<string,string>();
//log_debug("Selected snapshot repo path: %s".printf(path));
var list = Device.get_disk_space_using_df(path);
if (list.size > 0){
device = list[0];
log_debug(_("Device") + ": %s".printf(device.device));
log_debug(_("Free space") + ": %s".printf(format_file_size(device.free_bytes)));
}
check_status();
}
public SnapshotRepo.from_device(Device dev, Gtk.Window? parent_win, bool btrfs_repo){
log_debug("SnapshotRepo: from_device(): %s".printf(btrfs_repo ? "BTRFS" : "RSYNC"));
this.device = dev;
//this.use_snapshot_path_custom = false;
this.parent_window = parent_win;
this.btrfs_mode = btrfs_repo;
snapshots = new Gee.ArrayList<Snapshot>();
invalid_snapshots = new Gee.ArrayList<Snapshot>();
mount_paths = new Gee.HashMap<string,string>();
init_from_device();
}
public SnapshotRepo.from_uuid(string uuid, Gtk.Window? parent_win, bool btrfs_repo){
log_debug("SnapshotRepo: from_uuid(): %s".printf(btrfs_repo ? "BTRFS" : "RSYNC"));
log_debug("uuid=%s".printf(uuid));
device = Device.get_device_by_uuid(uuid);
if (device == null){
device = new Device();
device.uuid = uuid;
}
//this.use_snapshot_path_custom = false;
this.parent_window = parent_win;
this.btrfs_mode = btrfs_repo;
snapshots = new Gee.ArrayList<Snapshot>();
invalid_snapshots = new Gee.ArrayList<Snapshot>();
mount_paths = new Gee.HashMap<string,string>();
init_from_device();
log_debug("SnapshotRepo: from_uuid(): exit");
}
public SnapshotRepo.from_null(){
log_debug("SnapshotRepo: from_null()");
log_debug("device not set");
snapshots = new Gee.ArrayList<Snapshot>();
invalid_snapshots = new Gee.ArrayList<Snapshot>();
mount_paths = new Gee.HashMap<string,string>();
log_debug("SnapshotRepo: from_null(): exit");
}
private void init_from_device(){
log_debug("SnapshotRepo: init_from_device()");
if ((device != null) && (device.uuid.length > 0) && (Device.get_device_by_uuid(device.uuid) != null)){
log_debug("");
unlock_and_mount_devices();
if ((device != null) && (device.device.length > 0)){
log_debug(_("Selected snapshot device") + ": %s".printf(device.device));
log_debug(_("Free space") + ": %s".printf(format_file_size(device.free_bytes)));
}
}
if ((device != null) && (device.device.length > 0)){
check_status();
}
log_debug("SnapshotRepo: init_from_device(): exit");
}
// properties
public string timeshift_path {
owned get{
if (btrfs_mode){
return path_combine(mount_path, "timeshift-btrfs");
}
else{
return path_combine(mount_path, "timeshift");
}
}
}
public string snapshots_path {
owned get{
return path_combine(timeshift_path, "snapshots");
}
}
// load ---------------------------------------
public bool unlock_and_mount_devices(){
log_debug("SnapshotRepo: unlock_and_mount_devices()");
if (device == null){
log_debug("device=null");
}
else{
log_debug("device=%s".printf(device.device));
}
mount_path = unlock_and_mount_device(device, App.mount_point_app + "/backup");
if (mount_path.length == 0){
return false;
}
if (btrfs_mode){
if (App.root_subvolume_name == "") return false;
// Don't add mount paths if root_subvolume_name or home_subvolume_name are empty.
// We don't add default values either anymore, because it could lead us to bugs.
mount_paths[App.root_subvolume_name] = mount_path;
if (App.home_subvolume_name != "") mount_paths[App.home_subvolume_name] = mount_path;
device_home = device; //default
// mount @home if on different disk -------
var repo_subvolumes = Subvolume.detect_subvolumes_for_system_by_path(path_combine(mount_path,App.root_subvolume_name), this, parent_window);
if (App.home_subvolume_name != "" && repo_subvolumes.has_key(App.home_subvolume_name)){
var subvol = repo_subvolumes[App.home_subvolume_name];
if (subvol.device_uuid != device.uuid){
// @home is on a separate device
device_home = subvol.get_device();
mount_paths[App.home_subvolume_name] = unlock_and_mount_device(device_home, App.mount_point_app + "/backup-home");
if (mount_paths[App.home_subvolume_name].length == 0){
return false;
}
}
}
}
load_snapshots();
log_debug("SnapshotRepo: unlock_and_mount_device(): exit");
return true;
}
public string unlock_and_mount_device(Device device_to_mount, string path_to_mount){
// mounts the device and returns mount path
log_debug("SnapshotRepo: unlock_and_mount_device()");
Device dev = device_to_mount;
if (dev == null){
log_debug("device=null");
}
else{
log_debug("device=%s".printf(dev.device));
}
// unlock encrypted device
if (dev.is_encrypted_partition()){
dev = unlock_encrypted_device(dev);
device = dev; // set repo device to unlocked child disk
if (dev == null){
log_debug("device is null");
log_debug("SnapshotRepo: unlock_and_mount_device(): exit");
return "";
}
}
// mount
var mount_options = "";
if (btrfs_mode){
mount_options = "subvolid=0";
}
bool ok = Device.mount(dev.uuid, path_to_mount, mount_options); // TODO: check if already mounted
if (ok){
return path_to_mount;
}
else{
return "";
}
}
public Device? unlock_encrypted_device(Device luks_device){
log_debug("SnapshotRepo: unlock_encrypted_device()");
if (luks_device == null){
log_debug("luks_device=null");
return null;
}
else{
log_debug("luks_device=%s".printf(luks_device.device));
}
string msg_out, msg_err;
var luks_unlocked = Device.luks_unlock(
luks_device, "", "", parent_window, out msg_out, out msg_err);
return luks_unlocked;
}
public bool load_snapshots(){
log_debug("SnapshotRepo: load_snapshots()");
snapshots.clear();
invalid_snapshots.clear();
if ((device == null) || !dir_exists(snapshots_path)){
return false;
}
try{
var dir = File.new_for_path(snapshots_path);
var enumerator = dir.enumerate_children("*", 0);
var info = enumerator.next_file ();
while (info != null) {
if (info.get_file_type() == FileType.DIRECTORY) {
if (info.get_name() != ".sync") {
//log_debug("load_snapshots():" + snapshots_path + "/" + info.get_name());
Snapshot bak = new Snapshot(snapshots_path + "/" + info.get_name(), btrfs_mode, this);
if (bak.valid){
snapshots.add(bak);
}
else{
invalid_snapshots.add(bak);
}
}
}
info = enumerator.next_file ();
}
}
catch(Error e){
log_error (e.message);
return false;
}
snapshots.sort((a,b) => {
Snapshot t1 = (Snapshot) a;
Snapshot t2 = (Snapshot) b;
return t1.date.compare(t2.date);
});
// reset the 'live' flag ------------
DateTime dt_boot = new DateTime.now_local();
dt_boot = dt_boot.add_seconds(-1.0 * get_system_uptime_seconds());
foreach(var bak in snapshots){
if (bak.live){
if ((App.sys_root == null) || (App.sys_root.uuid != bak.sys_uuid)){
// we are accessing the snapshot from a live system or another system
bak.live = false;
bak.update_control_file();
}
else{
if (bak.date.difference(dt_boot) < 0){
// snapshot was created before the last reboot
bak.live = false;
bak.update_control_file();
}
else{
// do nothing, snapshot is still in use by system
}
}
}
}
if (btrfs_mode){
App.query_subvolume_info(this);
}
log_debug("loading snapshots from '%s': %d found".printf(snapshots_path, snapshots.size));
return true;
}
// get tagged snapshots ----------------------------------
public Gee.ArrayList<Snapshot?> get_snapshots_by_tag(string tag = ""){
var list = new Gee.ArrayList<Snapshot?>();
foreach(Snapshot bak in snapshots){
if (bak.valid && (tag.length == 0) || bak.has_tag(tag)){
list.add(bak);
}
}
list.sort((a,b) => {
Snapshot t1 = (Snapshot) a;
Snapshot t2 = (Snapshot) b;
return (t1.date.compare(t2.date));
});
return list;
}
public Snapshot? get_latest_snapshot(string tag, string sys_uuid){
var list = get_snapshots_by_tag(tag);
for(int i = list.size - 1; i >= 0; i--){
var bak = list[i];
if (bak.sys_uuid == sys_uuid){
return bak;
}
}
return null;
}
public Snapshot? get_oldest_snapshot(string tag, string sys_uuid){
var list = get_snapshots_by_tag(tag);
for(int i = 0; i < list.size; i++){
var bak = list[i];
if (bak.sys_uuid == sys_uuid){
return bak;
}
}
return null;
}
// status check ---------------------------
public void check_status(){
log_debug("SnapshotRepo: check_status()");
if (!last_snapshot_failed_space)
{
status_code = SnapshotLocationStatus.HAS_SNAPSHOTS_HAS_SPACE;
status_message = "";
status_details = "";
}
if (available()){
has_snapshots();
if (!last_snapshot_failed_space)
{
has_space();
}
}
if ((App != null) && (App.app_mode.length == 0)){
log_debug("%s: '%s'".printf(
_("Snapshot device"),
(device == null) ? " UNKNOWN" : device.device));
log_debug("%s: %s".printf(
_("Snapshot location"), mount_path));
log_debug(status_message);
log_debug(status_details);
log_debug("%s: %s".printf(
_("Status"),
status_code.to_string().replace("SNAPSHOT_LOCATION_STATUS_","")));
log_debug("");
}
last_snapshot_failed_space = false;
log_debug("SnapshotRepo: check_status(): exit");
}
public bool available(){
log_debug("SnapshotRepo: available()");
//log_debug("checking selected device");
// Snapshot repo is available if the config is valid.
bool ok = check_config(out status_message, out status_details, out status_code);
if (ok){
log_debug(status_message);
log_debug("is_available: ok");
} else {
log_debug(status_message);
log_debug("is_available: false");
}
return ok;
}
/*
* Validates the btrfs config and displays an appropriate error message for the user.
* It's a little convoluted because it needs to catch any misconfiguration leading
* to error states.
*/
private bool check_config(out string title, out string msg, out SnapshotLocationStatus code){
log_debug("SnapshotRepo: check_btrfs_config()");
log_debug("btrfs_mode=%s".printf(btrfs_mode.to_string()));
if (device == null){
if (App.backup_uuid == null || App.backup_uuid.length == 0){
log_debug("device is null");
title = _("Snapshot device not selected");
msg = _("Select the snapshot device");
code = SnapshotLocationStatus.NOT_SELECTED;
return false;
}
else{
title = _("Snapshot device not available");
msg = _("Device not found") + ": UUID='%s'".printf(App.backup_uuid);
code = SnapshotLocationStatus.NOT_AVAILABLE;
return false;
}
}
if (btrfs_mode) {
// Run the btrfs checks from Main.check_btrfs_system_config.
if (!App.check_btrfs_system_config(out title, out msg)) {
code = SnapshotLocationStatus.NO_BTRFS_SYSTEM;
return false;
}
// Run some additional checks, these cases are unlikely to come up,
// but they could point to bugs in SnapshotRepo code or any other
// code related to configuring the system layout.
// Bear with me, even though it seems confusing at first...
// Check the home subvolume configuration.
// home_path can be null if mount_paths[App.home_subvolume_name] doesn't exist.
var home_path = path_combine(mount_paths[App.home_subvolume_name], App.home_subvolume_name);
var home_subvolume_configured = App.home_subvolume_name != "";
var has_home_mount_path = mount_paths.has_key(App.home_subvolume_name);
log_debug("home_path=%s".printf(home_path));
log_debug("home_subvolume_configured=%s".printf(home_subvolume_configured.to_string()));
log_debug("has_home_mount_path=%s".printf(has_home_mount_path.to_string()));
// If home_subvolume_configured and the directory does not exists, the
// configuration is invalid.
if (!has_home_mount_path || (home_subvolume_configured && !dir_exists(home_path))) {
title = _("Home subvolume configuration is invalid");
msg = _("Home subvolume is configured but the path does not exist") + " (" + App.home_subvolume_name + ")";
code = SnapshotLocationStatus.NO_BTRFS_SYSTEM;
return false;
}
// Further check root subvolume configuration.
// We already know App.root_subvolume_name is not empty, but we still need to check if
// the key exists (it might not if there is no mount path associated to it).
var has_root_mount_path = mount_paths.has_key(App.root_subvolume_name);
// root_path can still be null if has_root_mount_path is false.
var root_path = path_combine(mount_paths[App.root_subvolume_name], App.root_subvolume_name);
log_debug("root_path=%s".printf(root_path));
log_debug("has_root_mount_path=%s".printf(has_root_mount_path.to_string()));
// If we don't have a mount_path for root, or root_path is null, or the root_path
// directory doesn't exist, the configuration is invalid.
// Technically, we don't need to explicitly check root_path here, it's implied by
// !has_root_mount_path.
if (!has_root_mount_path || !dir_exists(root_path)){
title = _("Root subvolume configuration is invalid");
msg = _("Root subvolume is configured but the path does not exist. Select BTRFS system disk with root subvolume ") + " (" + App.home_subvolume_name + ")";
code = SnapshotLocationStatus.NO_BTRFS_SYSTEM;
return false;
}
}
return true;
}
public bool has_snapshots(){
log_debug("SnapshotRepo: has_snapshots()");
//load_snapshots();
return (snapshots.size > 0);
}
public bool has_space(uint64 needed = 0) {
log_debug("SnapshotRepo: has_space() - %llu required (%s)".printf(needed, format_file_size(needed)));
if ((device != null) && (device.device.length > 0)){
device.query_disk_space();
}
else{
log_debug("device is NULL");
return false;
}
if (snapshots.size > 0){
// has snapshots, check minimum space
if (device.free_bytes < (needed > 0 ? needed : Main.MIN_FREE_SPACE)) {
status_message = _("Not enough disk space");
status_message += " (< %s)".printf(format_file_size((needed > 0 ? needed : Main.MIN_FREE_SPACE), false, "", true, 0));
status_details = _("Select another device or free up some space");
status_code = SnapshotLocationStatus.HAS_SNAPSHOTS_NO_SPACE;
last_snapshot_failed_space = true;
return false;
}
else{
//ok
status_message = _("OK");
status_details = _("%d snapshots, %s free").printf(
snapshots.size, format_file_size(device.free_bytes));
last_snapshot_failed_space = false;
status_code = SnapshotLocationStatus.HAS_SNAPSHOTS_HAS_SPACE;
return true;
}
}
else {
// no snapshots, check estimated space
log_debug("no snapshots");
var required_space = Main.first_snapshot_size;
if (device.free_bytes < required_space){
status_message = _("Not enough disk space");
status_message += " (< %s)".printf(format_file_size(required_space));
status_details = _("Select another device or free up some space");
status_code = SnapshotLocationStatus.NO_SNAPSHOTS_NO_SPACE;
return false;
}
else{
status_message = _("No snapshots on this device");
status_details = _("First snapshot requires:");
status_details += " %s".printf(format_file_size(required_space));
status_code = SnapshotLocationStatus.NO_SNAPSHOTS_HAS_SPACE;
return true;
}
}
}
public void print_status(){
check_status();
//log_msg("");
if (device == null){
log_msg("%-6s : %s".printf(_("Device"), _("Not Selected")));
}
else{
log_msg("%-6s : %s".printf(_("Device"), device.device_name_with_parent));
log_msg("%-6s : %s".printf("UUID", device.uuid));
log_msg("%-6s : %s".printf(_("Path"), mount_path));
log_msg("%-6s : %s".printf(_("Mode"), btrfs_mode ? "BTRFS" : "RSYNC"));
log_msg("%-6s : %s".printf(_("Status"), status_message));
log_msg(status_details);
}
log_msg("");
}
// actions -------------------------------------
public void auto_remove(){
log_debug("SnapshotRepo: auto_remove()");
last_snapshot_failed_space = false;
DateTime now = new DateTime.now_local();
DateTime dt_limit;
int count_limit;
// remove tags from older backups - boot ---------------
var list = get_snapshots_by_tag("boot");
if (list.size > App.count_boot){
log_msg(_("Maximum backups exceeded for backup level") + " '%s'".printf("boot"));
while (list.size > App.count_boot){
list[0].remove_tag("boot");
log_msg(_("Snapshot") + " '%s' ".printf(list[0].name) + _("un-tagged") + " '%s'".printf("boot"));
list = get_snapshots_by_tag("boot");
}
}
// remove tags from older backups - hourly, daily, weekly, monthly ---------
string[] levels = { "hourly","daily","weekly","monthly" };
foreach(string level in levels){
list = get_snapshots_by_tag(level);
if (list.size == 0) { continue; }
switch (level){
case "hourly":
dt_limit = now.add_hours(-1 * App.count_hourly);
count_limit = App.count_hourly;
break;
case "daily":
dt_limit = now.add_days(-1 * App.count_daily);
count_limit = App.count_daily;
break;
case "weekly":
dt_limit = now.add_weeks(-1 * App.count_weekly);
count_limit = App.count_weekly;
break;
case "monthly":
dt_limit = now.add_months(-1 * App.count_monthly);
count_limit = App.count_monthly;
break;
default:
dt_limit = now.add_years(-1 * 10);
count_limit = 100000;
break;
}
if (list.size > count_limit){
log_msg(_("Maximum backups exceeded for backup level") + " '%s'".printf(level));
int snaps_count = list.size;
foreach(var snap in list){
if (snap.description.strip().length > 0){ continue; } // don't delete snapshots with comments
if ((snap.date.compare(dt_limit) < 0) && (snaps_count > count_limit)){
snap.remove_tag(level);
snaps_count--;
log_msg(_("Snapshot") + " '%s' ".printf(list[0].name) + _("un-tagged") + " '%s'".printf(level));
}
}
}
}
// remove tags from older backups - max days -------
/*show_msg = true;
count = 0;
foreach(var bak in snapshots){
if (bak.date.compare(now.add_days(-1 * App.retain_snapshots_max_days)) < 0){
if (!bak.has_tag("ondemand")){
if (show_msg){
log_msg(_("Removing backups older than") + " %d ".printf(
App.retain_snapshots_max_days) + _("days..."));
show_msg = false;
}
log_msg(_("Snapshot") + " '%s' ".printf(bak.name) + _("un-tagged"));
bak.tags.clear();
count++;
}
}
}*/
// delete untagged snapshots
remove_untagged();
// delete older backups - minimum space -------
/*
device.query_disk_space();
show_msg = true;
count = 0;
while ((device.size_bytes - device.used_bytes) < App.minimum_free_disk_space){
load_snapshots();
if (snapshots.size > 0){
if (!snapshots[0].has_tag("ondemand")){
if (show_msg){
log_msg(_("Free space is less than") + " %lld GB".printf(
Main.MIN_FREE_SPACE / GB));
log_msg(_("Removing older backups to free disk space"));
show_msg = false;
}
snapshots[0].remove();
}
}
device.query_disk_space();
}
* */
// delete snapshots marked for deletion
remove_marked_for_deletion();
// delete invalid snapshots
remove_invalid();
}
public void remove_untagged(){
log_debug("SnapshotRepo: remove_untagged()");
bool show_msg = true;
foreach(Snapshot bak in snapshots){
if (bak.tags.size == 0){
if (show_msg){
log_msg("%s (%s):".printf(_("Removing snapshots"), _("un-tagged")));
show_msg = false;
}
bak.remove(true);
}
}
load_snapshots(); // update the list
}
public void remove_marked_for_deletion(){
bool show_msg = true;
foreach(var bak in snapshots){
if (bak.marked_for_deletion){
if (show_msg){
log_msg("%s (%s):".printf(_("Removing snapshots"), _("marked for deletion")));
show_msg = false;
}
bak.remove(true);
}
}
load_snapshots(); // update the list
}
public void remove_invalid(){
bool show_msg = true;
foreach(var bak in invalid_snapshots){
if (show_msg){
log_msg("%s (%s):".printf(_("Removing snapshots"), _("incomplete")));
show_msg = false;
}
bak.remove(true);
}
load_snapshots(); // update the list
}
public bool remove_all(){
if (dir_exists(timeshift_path)){
log_msg(_("Removing snapshots") + " > " + _("all") + "...");
//delete snapshots
foreach(var bak in snapshots){
bak.remove(true);
}
remove_sync_dir();
bool ok = delete_directory(timeshift_path);
load_snapshots(); // update the list
return ok;
}
else{
log_msg(_("No snapshots found") + " '%s'".printf(mount_path));
return true;
}
}
public bool remove_sync_dir(){
string sync_dir = mount_path + "/timeshift/snapshots/.sync";
//delete .sync
if (dir_exists(sync_dir)){
if (!delete_directory(sync_dir)){
return false;
}
}
return true;
}
public bool remove_timeshift_dir(){
// delete /timeshift
if (dir_exists(timeshift_path)){
if (!delete_directory(timeshift_path)){
return false;
}
}
return true;
}
// private -------------------------------------------
private bool delete_directory(string dir_path){
thr_args1 = dir_path;
try {
thr_running = true;
thr_success = false;
new Thread<void>.try ("delete-directory", () => {delete_directory_thread();});
} catch (Error e) {
thr_running = false;
thr_success = false;
log_error (e.message);
}
while (thr_running){
gtk_do_events ();
Thread.usleep((ulong) GLib.TimeSpan.MILLISECOND * 100);
}
thr_args1 = null;
return thr_success;
}
private void delete_directory_thread(){
thr_success = TeeJee.FileSystem.dir_delete_recursive(thr_args1);
if (thr_success) {
log_msg(_("Removed") + ": '%s'".printf(thr_args1));
} else{
log_error(_("Failed to remove") + ": '%s'".printf(thr_args1));
}
thr_running = false;
}
// symlinks ----------------------------------------
public void create_symlinks(){
cleanup_symlink_dir("boot");
cleanup_symlink_dir("hourly");
cleanup_symlink_dir("daily");
cleanup_symlink_dir("weekly");
cleanup_symlink_dir("monthly");
cleanup_symlink_dir("ondemand");
foreach(Snapshot bak in snapshots){
foreach(string tag in bak.tags) {
string linkTarget = "%s-%s/%s".printf(snapshots_path, tag, bak.name);
string linkValue = "../snapshots/" + bak.name;
try {
File f = File.new_for_path(linkTarget);
if (!f.make_symbolic_link(linkValue)) {
log_error(_("Failed to create symlinks") + ": %s".printf(linkTarget));
}
} catch(Error e) {
log_debug(e.message);
log_error(_("Failed to create symlinks") + ": %s".printf(linkTarget));
}
}
}
log_debug (_("Symlinks updated"));
}
public void cleanup_symlink_dir(string tag){
try{
string path = "%s-%s".printf(snapshots_path, tag);
if(!TeeJee.FileSystem.dir_delete_recursive(path)) {
log_error(_("Failed to delete symlinks") + ": '%s'".printf(path));
}
File f = File.new_for_path(path);
f.make_directory_with_parents();
}
catch (Error e) {
log_error (e.message);
}
}
}
public enum SnapshotLocationStatus{
/*
-1 - device un-available, path does not exist
0 - first snapshot taken, disk space sufficient
1 - first snapshot taken, disk space not sufficient
2 - first snapshot not taken, disk space not sufficient
3 - first snapshot not taken, disk space sufficient
4 - path is readonly