Skip to content

Commit 81e533c

Browse files
committed
make Tags an enum
1 parent 8958d97 commit 81e533c

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;
@@ -189,7 +188,7 @@ public class Main : GLib.Object{
189188
}
190189

191190
public Main(string[] args, bool gui_mode){
192-
191+
193192
this.mount_point_app = "/run/timeshift/%d".printf(Posix.getpid());
194193
dir_create(this.mount_point_app);
195194

@@ -1061,9 +1060,7 @@ public class Main : GLib.Object{
10611060

10621061
public bool scheduled{
10631062
get{
1064-
return !live_system()
1065-
&& (schedule_boot || schedule_hourly || schedule_daily ||
1066-
schedule_weekly || schedule_monthly);
1063+
return !live_system() && (schedule > 0);
10671064
}
10681065
}
10691066

@@ -1138,7 +1135,7 @@ public class Main : GLib.Object{
11381135

11391136
// ondemand
11401137
if (is_ondemand){
1141-
bool ok = create_snapshot_for_tag ("ondemand",now);
1138+
bool ok = create_snapshot_for_tag (Tags.OnDemand,now);
11421139
if(!ok){
11431140
return false;
11441141
}
@@ -1147,16 +1144,16 @@ public class Main : GLib.Object{
11471144
}
11481145
}
11491146
else if (scheduled){
1150-
Snapshot last_snapshot_boot = repo.get_latest_snapshot("boot", sys_uuid);
1151-
Snapshot last_snapshot_hourly = repo.get_latest_snapshot("hourly", sys_uuid);
1152-
Snapshot last_snapshot_daily = repo.get_latest_snapshot("daily", sys_uuid);
1153-
Snapshot last_snapshot_weekly = repo.get_latest_snapshot("weekly", sys_uuid);
1154-
Snapshot last_snapshot_monthly = repo.get_latest_snapshot("monthly", sys_uuid);
1147+
Snapshot last_snapshot_boot = repo.get_latest_snapshot(Tags.Boot, sys_uuid);
1148+
Snapshot last_snapshot_hourly = repo.get_latest_snapshot(Tags.Hourly, sys_uuid);
1149+
Snapshot last_snapshot_daily = repo.get_latest_snapshot(Tags.Daily, sys_uuid);
1150+
Snapshot last_snapshot_weekly = repo.get_latest_snapshot(Tags.Weekly, sys_uuid);
1151+
Snapshot last_snapshot_monthly = repo.get_latest_snapshot(Tags.Monthly, sys_uuid);
11551152

11561153
DateTime dt_sys_boot = now.add_seconds((-1) * get_system_uptime_seconds());
11571154
bool take_new = false;
11581155

1159-
if (schedule_boot){
1156+
if (Tags.Boot in App.schedule){
11601157

11611158
log_msg(_("Boot snapshots are enabled"));
11621159

@@ -1175,7 +1172,7 @@ public class Main : GLib.Object{
11751172
}
11761173

11771174
if (take_new){
1178-
status = create_snapshot_for_tag ("boot",now);
1175+
status = create_snapshot_for_tag (Tags.Boot,now);
11791176
if(!status){
11801177
log_error(_("Boot snapshot failed!"));
11811178
return false;
@@ -1187,7 +1184,7 @@ public class Main : GLib.Object{
11871184
}
11881185
}
11891186

1190-
if (schedule_hourly){
1187+
if (Tags.Hourly in App.schedule){
11911188

11921189
log_msg(_("Hourly snapshots are enabled"));
11931190

@@ -1206,7 +1203,7 @@ public class Main : GLib.Object{
12061203
}
12071204

12081205
if (take_new){
1209-
status = create_snapshot_for_tag ("hourly",now);
1206+
status = create_snapshot_for_tag (Tags.Hourly,now);
12101207
if(!status){
12111208
log_error(_("Hourly snapshot failed!"));
12121209
return false;
@@ -1218,7 +1215,7 @@ public class Main : GLib.Object{
12181215
}
12191216
}
12201217

1221-
if (schedule_daily){
1218+
if (Tags.Daily in App.schedule){
12221219

12231220
log_msg(_("Daily snapshots are enabled"));
12241221

@@ -1237,7 +1234,7 @@ public class Main : GLib.Object{
12371234
}
12381235

12391236
if (take_new){
1240-
status = create_snapshot_for_tag ("daily",now);
1237+
status = create_snapshot_for_tag (Tags.Daily,now);
12411238
if(!status){
12421239
log_error(_("Daily snapshot failed!"));
12431240
return false;
@@ -1249,7 +1246,7 @@ public class Main : GLib.Object{
12491246
}
12501247
}
12511248

1252-
if (schedule_weekly){
1249+
if (Tags.Weekly in App.schedule){
12531250

12541251
log_msg(_("Weekly snapshots are enabled"));
12551252

@@ -1268,7 +1265,7 @@ public class Main : GLib.Object{
12681265
}
12691266

12701267
if (take_new){
1271-
status = create_snapshot_for_tag ("weekly",now);
1268+
status = create_snapshot_for_tag (Tags.Weekly,now);
12721269
if(!status){
12731270
log_error(_("Weekly snapshot failed!"));
12741271
return false;
@@ -1280,7 +1277,7 @@ public class Main : GLib.Object{
12801277
}
12811278
}
12821279

1283-
if (schedule_monthly){
1280+
if (Tags.Monthly in App.schedule){
12841281

12851282
log_msg(_("Monthly snapshot are enabled"));
12861283

@@ -1299,7 +1296,7 @@ public class Main : GLib.Object{
12991296
}
13001297

13011298
if (take_new){
1302-
status = create_snapshot_for_tag ("monthly",now);
1299+
status = create_snapshot_for_tag (Tags.Monthly,now);
13031300
if(!status){
13041301
log_error(_("Monthly snapshot failed!"));
13051302
return false;
@@ -1339,7 +1336,7 @@ public class Main : GLib.Object{
13391336
return status;
13401337
}
13411338

1342-
private bool create_snapshot_for_tag(string tag, DateTime dt_created){
1339+
private bool create_snapshot_for_tag(Tags tag, DateTime dt_created){
13431340

13441341
log_debug("Main: backup_and_rotate()");
13451342

@@ -1355,20 +1352,17 @@ public class Main : GLib.Object{
13551352

13561353
DateTime dt_filter = null;
13571354

1358-
if (tag != "ondemand"){
1355+
if (tag != Tags.OnDemand){
13591356
switch(tag){
1360-
case "boot":
1357+
case Tags.Boot:
13611358
dt_filter = dt_sys_boot;
13621359
break;
1363-
case "hourly":
1364-
case "daily":
1365-
case "weekly":
1366-
case "monthly":
1360+
case Tags.Hourly:
1361+
case Tags.Daily:
1362+
case Tags.Weekly:
1363+
case Tags.Monthly:
13671364
dt_filter = now.add_hours(-1).add_seconds(59);
13681365
break;
1369-
default:
1370-
log_error(_("Unknown snapshot type") + ": %s".printf(tag));
1371-
return false;
13721366
}
13731367

13741368
// find a recent backup that can be used
@@ -1385,7 +1379,7 @@ public class Main : GLib.Object{
13851379
// tag the backup
13861380
backup_to_rotate.add_tag(tag);
13871381

1388-
var message = _("Tagged snapshot") + " '%s': %s".printf(backup_to_rotate.name, tag);
1382+
var message = _("Tagged snapshot") + " '%s': %s".printf(backup_to_rotate.name, tag.localized_name());
13891383
log_msg(message);
13901384

13911385
return true;
@@ -1459,7 +1453,7 @@ public class Main : GLib.Object{
14591453
OSDNotify.notify_send("TimeShift", message, 10000, "low");
14601454

14611455
if (new_snapshot != null){
1462-
message = _("Tagged snapshot") + " '%s': %s".printf(new_snapshot.name, tag);
1456+
message = _("Tagged snapshot") + " '%s': %s".printf(new_snapshot.name, tag.localized_name());
14631457
log_msg(message);
14641458
}
14651459

@@ -1517,7 +1511,7 @@ public class Main : GLib.Object{
15171511
// get latest snapshot to link if not set -------
15181512

15191513
if (snapshot_to_link == null){
1520-
snapshot_to_link = repo.get_latest_snapshot("", sys_uuid);
1514+
snapshot_to_link = repo.get_latest_snapshot(0, sys_uuid);
15211515
}
15221516

15231517
string link_from_path = "";
@@ -1566,7 +1560,7 @@ public class Main : GLib.Object{
15661560
return total_size;
15671561
}
15681562

1569-
private Snapshot? create_snapshot_with_rsync(string tag, DateTime dt_created){
1563+
private Snapshot? create_snapshot_with_rsync(Tags tag, DateTime dt_created){
15701564
log_msg(string.nfill(78, '-'));
15711565

15721566
if (first_snapshot_size == 0){
@@ -1630,7 +1624,7 @@ public class Main : GLib.Object{
16301624
// get latest snapshot to link if not set -------
16311625

16321626
if (snapshot_to_link == null){
1633-
snapshot_to_link = repo.get_latest_snapshot("", sys_uuid);
1627+
snapshot_to_link = repo.get_latest_snapshot(0, sys_uuid);
16341628
}
16351629

16361630
string link_from_path = "";
@@ -1694,7 +1688,7 @@ public class Main : GLib.Object{
16941688
return null;
16951689
}
16961690

1697-
string initial_tags = (tag == "ondemand") ? "" : tag;
1691+
string initial_tags = (tag == Tags.OnDemand) ? "" : tag.name();
16981692

16991693
// write control file
17001694
// this step is redundant - just in case if app crashes while parsing log file in next step
@@ -1723,7 +1717,7 @@ public class Main : GLib.Object{
17231717
return snapshot;
17241718
}
17251719

1726-
private Snapshot? create_snapshot_with_btrfs(string tag, DateTime dt_created){
1720+
private Snapshot? create_snapshot_with_btrfs(Tags tag, DateTime dt_created){
17271721

17281722
log_msg(_("Creating new backup...") + "(BTRFS)");
17291723

@@ -1795,7 +1789,7 @@ public class Main : GLib.Object{
17951789

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

1798-
string initial_tags = (tag == "ondemand") ? "" : tag;
1792+
string initial_tags = (tag == Tags.OnDemand) ? "" : tag.name();
17991793

18001794
// write control file
18011795
var snapshot = Snapshot.write_control_file(
@@ -1837,50 +1831,25 @@ public class Main : GLib.Object{
18371831
// add tags passed on commandline for both --check and --create
18381832

18391833
foreach(string tag in cmd_tags.split(",")){
1840-
switch(tag.strip().up()){
1841-
case "O":
1842-
snapshot.add_tag("ondemand");
1843-
break;
1844-
case "B":
1845-
snapshot.add_tag("boot");
1846-
break;
1847-
case "H":
1848-
snapshot.add_tag("hourly");
1849-
break;
1850-
case "D":
1851-
snapshot.add_tag("daily");
1852-
break;
1853-
case "W":
1854-
snapshot.add_tag("weekly");
1855-
break;
1856-
case "M":
1857-
snapshot.add_tag("monthly");
1858-
break;
1834+
Tags? parsed = Tags.parse(tag);
1835+
if(parsed != null) {
1836+
snapshot.add_tag(parsed);
18591837
}
18601838
}
18611839

18621840
// add tag as ondemand if no other tag is specified
18631841

1864-
if (snapshot.tags.size == 0){
1865-
snapshot.add_tag("ondemand");
1842+
if (snapshot.tags == 0){
1843+
snapshot.add_tag(Tags.OnDemand);
18661844
}
18671845
}
18681846

18691847
public void validate_cmd_tags(){
18701848
foreach(string tag in cmd_tags.split(",")){
1871-
switch(tag.strip().up()){
1872-
case "O":
1873-
case "B":
1874-
case "H":
1875-
case "D":
1876-
case "W":
1877-
case "M":
1878-
break;
1879-
default:
1849+
if(Tags.parse(tag) == null) {
18801850
log_error(_("Unknown value specified for option --tags") + " (%s).".printf(tag));
18811851
log_error(_("Expected values: O, B, H, D, W, M"));
18821852
exit_app(1);
1883-
break;
18841853
}
18851854
}
18861855
}
@@ -3370,11 +3339,11 @@ public class Main : GLib.Object{
33703339
config.set_string_member("include_btrfs_home_for_restore", include_btrfs_home_for_restore.to_string());
33713340
config.set_string_member("stop_cron_emails", stop_cron_emails.to_string());
33723341

3373-
config.set_string_member("schedule_monthly", schedule_monthly.to_string());
3374-
config.set_string_member("schedule_weekly", schedule_weekly.to_string());
3375-
config.set_string_member("schedule_daily", schedule_daily.to_string());
3376-
config.set_string_member("schedule_hourly", schedule_hourly.to_string());
3377-
config.set_string_member("schedule_boot", schedule_boot.to_string());
3342+
config.set_string_member("schedule_monthly", (Tags.Monthly in App.schedule).to_string());
3343+
config.set_string_member("schedule_weekly", (Tags.Weekly in App.schedule).to_string());
3344+
config.set_string_member("schedule_daily", (Tags.Daily in App.schedule).to_string());
3345+
config.set_string_member("schedule_hourly", (Tags.Hourly in App.schedule).to_string());
3346+
config.set_string_member("schedule_boot", (Tags.Boot in App.schedule).to_string());
33783347

33793348
config.set_string_member("count_monthly", count_monthly.to_string());
33803349
config.set_string_member("count_weekly", count_weekly.to_string());
@@ -3493,11 +3462,11 @@ public class Main : GLib.Object{
34933462
backup_uuid = json_get_string(config,"backup_device_uuid", backup_uuid);
34943463
backup_parent_uuid = json_get_string(config,"parent_device_uuid", backup_parent_uuid);
34953464

3496-
this.schedule_monthly = json_get_bool(config,"schedule_monthly",schedule_monthly);
3497-
this.schedule_weekly = json_get_bool(config,"schedule_weekly",schedule_weekly);
3498-
this.schedule_daily = json_get_bool(config,"schedule_daily",schedule_daily);
3499-
this.schedule_hourly = json_get_bool(config,"schedule_hourly",schedule_hourly);
3500-
this.schedule_boot = json_get_bool(config,"schedule_boot",schedule_boot);
3465+
Tags.set_value(ref this.schedule, Tags.Monthly, json_get_bool(config,"schedule_monthly", Tags.Monthly in schedule));
3466+
Tags.set_value(ref this.schedule, Tags.Weekly, json_get_bool(config,"schedule_weekly", Tags.Weekly in schedule));
3467+
Tags.set_value(ref this.schedule, Tags.Daily, json_get_bool(config,"schedule_daily", Tags.Daily in schedule));
3468+
Tags.set_value(ref this.schedule, Tags.Hourly, json_get_bool(config,"schedule_hourly", Tags.Hourly in schedule));
3469+
Tags.set_value(ref this.schedule, Tags.Boot, json_get_bool(config,"schedule_boot", Tags.Boot in schedule));
35013470

35023471
this.count_monthly = json_get_int(config,"count_monthly",count_monthly);
35033472
this.count_weekly = json_get_int(config,"count_weekly",count_weekly);
@@ -4393,7 +4362,7 @@ public class Main : GLib.Object{
43934362
CronTab.add_script_file("timeshift-hourly", "d", "0 * * * * root timeshift --check --scripted", stop_cron_emails);
43944363

43954364
//boot
4396-
if (schedule_boot){
4365+
if (Tags.Boot in App.schedule){
43974366
CronTab.add_script_file("timeshift-boot", "d", "@reboot root sleep 10m && timeshift --create --scripted --tags B", stop_cron_emails);
43984367
}
43994368
else{

0 commit comments

Comments
 (0)