Skip to content

Commit 20c8fd0

Browse files
committed
make Tags an enum
1 parent 581a98e commit 20c8fd0

8 files changed

Lines changed: 248 additions & 210 deletions

File tree

src/Core/Main.vala

Lines changed: 54 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ public class Main : GLib.Object{
8484
public LinuxDistro current_distro;
8585
public bool mirror_system = false;
8686

87-
public bool schedule_monthly = false;
88-
public bool schedule_weekly = false;
89-
public bool schedule_daily = false;
90-
public bool schedule_hourly = false;
91-
public bool schedule_boot = false;
87+
// tags that are scheduled
88+
public Tags schedule = 0;
89+
90+
// planned snapshots per tag
9291
public int count_monthly = 2;
9392
public int count_weekly = 3;
9493
public int count_daily = 5;
@@ -184,7 +183,7 @@ public class Main : GLib.Object{
184183
}
185184

186185
public Main(string[] args, bool gui_mode){
187-
186+
188187
this.mount_point_app = "/run/timeshift/%d".printf(Posix.getpid());
189188
dir_create(this.mount_point_app);
190189

@@ -1056,9 +1055,7 @@ public class Main : GLib.Object{
10561055

10571056
public bool scheduled{
10581057
get{
1059-
return !live_system()
1060-
&& (schedule_boot || schedule_hourly || schedule_daily ||
1061-
schedule_weekly || schedule_monthly);
1058+
return !live_system() && (schedule > 0);
10621059
}
10631060
}
10641061

@@ -1127,7 +1124,7 @@ public class Main : GLib.Object{
11271124

11281125
// ondemand
11291126
if (is_ondemand){
1130-
bool ok = create_snapshot_for_tag ("ondemand",now);
1127+
bool ok = create_snapshot_for_tag (Tags.OnDemand,now);
11311128
if(!ok){
11321129
return false;
11331130
}
@@ -1136,16 +1133,16 @@ public class Main : GLib.Object{
11361133
}
11371134
}
11381135
else if (scheduled){
1139-
Snapshot last_snapshot_boot = repo.get_latest_snapshot("boot", sys_uuid);
1140-
Snapshot last_snapshot_hourly = repo.get_latest_snapshot("hourly", sys_uuid);
1141-
Snapshot last_snapshot_daily = repo.get_latest_snapshot("daily", sys_uuid);
1142-
Snapshot last_snapshot_weekly = repo.get_latest_snapshot("weekly", sys_uuid);
1143-
Snapshot last_snapshot_monthly = repo.get_latest_snapshot("monthly", sys_uuid);
1136+
Snapshot last_snapshot_boot = repo.get_latest_snapshot(Tags.Boot, sys_uuid);
1137+
Snapshot last_snapshot_hourly = repo.get_latest_snapshot(Tags.Hourly, sys_uuid);
1138+
Snapshot last_snapshot_daily = repo.get_latest_snapshot(Tags.Daily, sys_uuid);
1139+
Snapshot last_snapshot_weekly = repo.get_latest_snapshot(Tags.Weekly, sys_uuid);
1140+
Snapshot last_snapshot_monthly = repo.get_latest_snapshot(Tags.Monthly, sys_uuid);
11441141

11451142
DateTime dt_sys_boot = now.add_seconds((-1) * get_system_uptime_seconds());
11461143
bool take_new = false;
11471144

1148-
if (schedule_boot){
1145+
if (Tags.Boot in App.schedule){
11491146

11501147
log_msg(_("Boot snapshots are enabled"));
11511148

@@ -1164,7 +1161,7 @@ public class Main : GLib.Object{
11641161
}
11651162

11661163
if (take_new){
1167-
status = create_snapshot_for_tag ("boot",now);
1164+
status = create_snapshot_for_tag (Tags.Boot,now);
11681165
if(!status){
11691166
log_error(_("Boot snapshot failed!"));
11701167
return false;
@@ -1176,7 +1173,7 @@ public class Main : GLib.Object{
11761173
}
11771174
}
11781175

1179-
if (schedule_hourly){
1176+
if (Tags.Hourly in App.schedule){
11801177

11811178
log_msg(_("Hourly snapshots are enabled"));
11821179

@@ -1195,7 +1192,7 @@ public class Main : GLib.Object{
11951192
}
11961193

11971194
if (take_new){
1198-
status = create_snapshot_for_tag ("hourly",now);
1195+
status = create_snapshot_for_tag (Tags.Hourly,now);
11991196
if(!status){
12001197
log_error(_("Hourly snapshot failed!"));
12011198
return false;
@@ -1207,7 +1204,7 @@ public class Main : GLib.Object{
12071204
}
12081205
}
12091206

1210-
if (schedule_daily){
1207+
if (Tags.Daily in App.schedule){
12111208

12121209
log_msg(_("Daily snapshots are enabled"));
12131210

@@ -1226,7 +1223,7 @@ public class Main : GLib.Object{
12261223
}
12271224

12281225
if (take_new){
1229-
status = create_snapshot_for_tag ("daily",now);
1226+
status = create_snapshot_for_tag (Tags.Daily,now);
12301227
if(!status){
12311228
log_error(_("Daily snapshot failed!"));
12321229
return false;
@@ -1238,7 +1235,7 @@ public class Main : GLib.Object{
12381235
}
12391236
}
12401237

1241-
if (schedule_weekly){
1238+
if (Tags.Weekly in App.schedule){
12421239

12431240
log_msg(_("Weekly snapshots are enabled"));
12441241

@@ -1257,7 +1254,7 @@ public class Main : GLib.Object{
12571254
}
12581255

12591256
if (take_new){
1260-
status = create_snapshot_for_tag ("weekly",now);
1257+
status = create_snapshot_for_tag (Tags.Weekly,now);
12611258
if(!status){
12621259
log_error(_("Weekly snapshot failed!"));
12631260
return false;
@@ -1269,7 +1266,7 @@ public class Main : GLib.Object{
12691266
}
12701267
}
12711268

1272-
if (schedule_monthly){
1269+
if (Tags.Monthly in App.schedule){
12731270

12741271
log_msg(_("Monthly snapshot are enabled"));
12751272

@@ -1288,7 +1285,7 @@ public class Main : GLib.Object{
12881285
}
12891286

12901287
if (take_new){
1291-
status = create_snapshot_for_tag ("monthly",now);
1288+
status = create_snapshot_for_tag (Tags.Monthly,now);
12921289
if(!status){
12931290
log_error(_("Monthly snapshot failed!"));
12941291
return false;
@@ -1328,7 +1325,7 @@ public class Main : GLib.Object{
13281325
return status;
13291326
}
13301327

1331-
private bool create_snapshot_for_tag(string tag, DateTime dt_created){
1328+
private bool create_snapshot_for_tag(Tags tag, DateTime dt_created){
13321329

13331330
log_debug("Main: backup_and_rotate()");
13341331

@@ -1344,20 +1341,17 @@ public class Main : GLib.Object{
13441341

13451342
DateTime dt_filter = null;
13461343

1347-
if (tag != "ondemand"){
1344+
if (tag != Tags.OnDemand){
13481345
switch(tag){
1349-
case "boot":
1346+
case Tags.Boot:
13501347
dt_filter = dt_sys_boot;
13511348
break;
1352-
case "hourly":
1353-
case "daily":
1354-
case "weekly":
1355-
case "monthly":
1349+
case Tags.Hourly:
1350+
case Tags.Daily:
1351+
case Tags.Weekly:
1352+
case Tags.Monthly:
13561353
dt_filter = now.add_hours(-1).add_seconds(59);
13571354
break;
1358-
default:
1359-
log_error(_("Unknown snapshot type") + ": %s".printf(tag));
1360-
return false;
13611355
}
13621356

13631357
// find a recent backup that can be used
@@ -1374,7 +1368,7 @@ public class Main : GLib.Object{
13741368
// tag the backup
13751369
backup_to_rotate.add_tag(tag);
13761370

1377-
var message = _("Tagged snapshot") + " '%s': %s".printf(backup_to_rotate.name, tag);
1371+
var message = _("Tagged snapshot") + " '%s': %s".printf(backup_to_rotate.name, tag.localized_name());
13781372
log_msg(message);
13791373

13801374
return true;
@@ -1448,7 +1442,7 @@ public class Main : GLib.Object{
14481442
OSDNotify.notify_send("TimeShift", message, 10000, "low");
14491443

14501444
if (new_snapshot != null){
1451-
message = _("Tagged snapshot") + " '%s': %s".printf(new_snapshot.name, tag);
1445+
message = _("Tagged snapshot") + " '%s': %s".printf(new_snapshot.name, tag.localized_name());
14521446
log_msg(message);
14531447
}
14541448

@@ -1506,7 +1500,7 @@ public class Main : GLib.Object{
15061500
// get latest snapshot to link if not set -------
15071501

15081502
if (snapshot_to_link == null){
1509-
snapshot_to_link = repo.get_latest_snapshot("", sys_uuid);
1503+
snapshot_to_link = repo.get_latest_snapshot(0, sys_uuid);
15101504
}
15111505

15121506
string link_from_path = "";
@@ -1555,7 +1549,7 @@ public class Main : GLib.Object{
15551549
return total_size;
15561550
}
15571551

1558-
private Snapshot? create_snapshot_with_rsync(string tag, DateTime dt_created){
1552+
private Snapshot? create_snapshot_with_rsync(Tags tag, DateTime dt_created){
15591553
log_msg(string.nfill(78, '-'));
15601554

15611555
if (first_snapshot_size == 0){
@@ -1619,7 +1613,7 @@ public class Main : GLib.Object{
16191613
// get latest snapshot to link if not set -------
16201614

16211615
if (snapshot_to_link == null){
1622-
snapshot_to_link = repo.get_latest_snapshot("", sys_uuid);
1616+
snapshot_to_link = repo.get_latest_snapshot(0, sys_uuid);
16231617
}
16241618

16251619
string link_from_path = "";
@@ -1687,7 +1681,7 @@ public class Main : GLib.Object{
16871681
return null;
16881682
}
16891683

1690-
string initial_tags = (tag == "ondemand") ? "" : tag;
1684+
string initial_tags = (tag == Tags.OnDemand) ? "" : tag.name();
16911685

16921686
// write control file
16931687
// this step is redundant - just in case if app crashes while parsing log file in next step
@@ -1716,7 +1710,7 @@ public class Main : GLib.Object{
17161710
return snapshot;
17171711
}
17181712

1719-
private Snapshot? create_snapshot_with_btrfs(string tag, DateTime dt_created){
1713+
private Snapshot? create_snapshot_with_btrfs(Tags tag, DateTime dt_created){
17201714

17211715
log_msg(_("Creating new backup...") + "(BTRFS)");
17221716

@@ -1788,7 +1782,7 @@ public class Main : GLib.Object{
17881782

17891783
snapshot_path = path_combine(repo.mount_paths["@"], "timeshift-btrfs/snapshots/%s".printf(snapshot_name));
17901784

1791-
string initial_tags = (tag == "ondemand") ? "" : tag;
1785+
string initial_tags = (tag == Tags.OnDemand) ? "" : tag.name();
17921786

17931787
// write control file
17941788
var snapshot = Snapshot.write_control_file(
@@ -1830,50 +1824,25 @@ public class Main : GLib.Object{
18301824
// add tags passed on commandline for both --check and --create
18311825

18321826
foreach(string tag in cmd_tags.split(",")){
1833-
switch(tag.strip().up()){
1834-
case "O":
1835-
snapshot.add_tag("ondemand");
1836-
break;
1837-
case "B":
1838-
snapshot.add_tag("boot");
1839-
break;
1840-
case "H":
1841-
snapshot.add_tag("hourly");
1842-
break;
1843-
case "D":
1844-
snapshot.add_tag("daily");
1845-
break;
1846-
case "W":
1847-
snapshot.add_tag("weekly");
1848-
break;
1849-
case "M":
1850-
snapshot.add_tag("monthly");
1851-
break;
1827+
Tags? parsed = Tags.parse(tag);
1828+
if(parsed != null) {
1829+
snapshot.add_tag(parsed);
18521830
}
18531831
}
18541832

18551833
// add tag as ondemand if no other tag is specified
18561834

1857-
if (snapshot.tags.size == 0){
1858-
snapshot.add_tag("ondemand");
1835+
if (snapshot.tags == 0){
1836+
snapshot.add_tag(Tags.OnDemand);
18591837
}
18601838
}
18611839

18621840
public void validate_cmd_tags(){
18631841
foreach(string tag in cmd_tags.split(",")){
1864-
switch(tag.strip().up()){
1865-
case "O":
1866-
case "B":
1867-
case "H":
1868-
case "D":
1869-
case "W":
1870-
case "M":
1871-
break;
1872-
default:
1842+
if(Tags.parse(tag) == null) {
18731843
log_error(_("Unknown value specified for option --tags") + " (%s).".printf(tag));
18741844
log_error(_("Expected values: O, B, H, D, W, M"));
18751845
exit_app(1);
1876-
break;
18771846
}
18781847
}
18791848
}
@@ -3363,11 +3332,11 @@ public class Main : GLib.Object{
33633332
config.set_string_member("include_btrfs_home_for_restore", include_btrfs_home_for_restore.to_string());
33643333
config.set_string_member("stop_cron_emails", stop_cron_emails.to_string());
33653334

3366-
config.set_string_member("schedule_monthly", schedule_monthly.to_string());
3367-
config.set_string_member("schedule_weekly", schedule_weekly.to_string());
3368-
config.set_string_member("schedule_daily", schedule_daily.to_string());
3369-
config.set_string_member("schedule_hourly", schedule_hourly.to_string());
3370-
config.set_string_member("schedule_boot", schedule_boot.to_string());
3335+
config.set_string_member("schedule_monthly", (Tags.Monthly in App.schedule).to_string());
3336+
config.set_string_member("schedule_weekly", (Tags.Weekly in App.schedule).to_string());
3337+
config.set_string_member("schedule_daily", (Tags.Daily in App.schedule).to_string());
3338+
config.set_string_member("schedule_hourly", (Tags.Hourly in App.schedule).to_string());
3339+
config.set_string_member("schedule_boot", (Tags.Boot in App.schedule).to_string());
33713340

33723341
config.set_string_member("count_monthly", count_monthly.to_string());
33733342
config.set_string_member("count_weekly", count_weekly.to_string());
@@ -3480,11 +3449,11 @@ public class Main : GLib.Object{
34803449
backup_uuid = json_get_string(config,"backup_device_uuid", backup_uuid);
34813450
backup_parent_uuid = json_get_string(config,"parent_device_uuid", backup_parent_uuid);
34823451

3483-
this.schedule_monthly = json_get_bool(config,"schedule_monthly",schedule_monthly);
3484-
this.schedule_weekly = json_get_bool(config,"schedule_weekly",schedule_weekly);
3485-
this.schedule_daily = json_get_bool(config,"schedule_daily",schedule_daily);
3486-
this.schedule_hourly = json_get_bool(config,"schedule_hourly",schedule_hourly);
3487-
this.schedule_boot = json_get_bool(config,"schedule_boot",schedule_boot);
3452+
Tags.set_value(ref this.schedule, Tags.Monthly, json_get_bool(config,"schedule_monthly", Tags.Monthly in schedule));
3453+
Tags.set_value(ref this.schedule, Tags.Weekly, json_get_bool(config,"schedule_weekly", Tags.Weekly in schedule));
3454+
Tags.set_value(ref this.schedule, Tags.Daily, json_get_bool(config,"schedule_daily", Tags.Daily in schedule));
3455+
Tags.set_value(ref this.schedule, Tags.Hourly, json_get_bool(config,"schedule_hourly", Tags.Hourly in schedule));
3456+
Tags.set_value(ref this.schedule, Tags.Boot, json_get_bool(config,"schedule_boot", Tags.Boot in schedule));
34883457

34893458
this.count_monthly = json_get_int(config,"count_monthly",count_monthly);
34903459
this.count_weekly = json_get_int(config,"count_weekly",count_weekly);
@@ -4331,7 +4300,7 @@ public class Main : GLib.Object{
43314300
CronTab.add_script_file("timeshift-hourly", "d", "0 * * * * root timeshift --check --scripted", stop_cron_emails);
43324301

43334302
//boot
4334-
if (schedule_boot){
4303+
if (Tags.Boot in App.schedule){
43354304
CronTab.add_script_file("timeshift-boot", "d", "@reboot root sleep 10m && timeshift --create --scripted --tags B", stop_cron_emails);
43364305
}
43374306
else{

0 commit comments

Comments
 (0)