Skip to content
This repository has been archived by the owner on Oct 16, 2022. It is now read-only.

Commit

Permalink
Fix #895, #809, #689, #653, #10: Unmount devices that were mounted by…
Browse files Browse the repository at this point in the history
… Timeshift before exit

Any devices that Timeshift needs to mount will be mounted under /run/timeshift/<pid> and unmounted on application exit
  • Loading branch information
teejee2008 committed May 28, 2022
1 parent a7c8f3c commit d7ba851
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 10 deletions.
91 changes: 85 additions & 6 deletions src/Core/Main.vala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class Main : GLib.Object{
public Gee.HashMap<string, Subvolume> sys_subvolumes;

public string mount_point_restore = "";
public string mount_point_app = "/run/timeshift";
public string mount_point_app = "";

public LinuxDistro current_distro;
public bool mirror_system = false;
Expand Down Expand Up @@ -173,9 +173,12 @@ public class Main : GLib.Object{

public string encrypted_private_dirs = "";
public bool encrypted_private_warning_shown = false;

public Main(string[] args, bool gui_mode){


this.mount_point_app = "/run/timeshift/%lld".printf(Posix.getpid());
dir_create(this.mount_point_app);

parse_some_arguments(args);

if (gui_mode){
Expand Down Expand Up @@ -927,7 +930,7 @@ public class Main : GLib.Object{
});
}

//properties
// properties

public bool scheduled{
get{
Expand Down Expand Up @@ -3421,7 +3424,7 @@ public class Main : GLib.Object{

public bool mount_target_devices(Gtk.Window? parent_win = null){
/* Note:
* Target device will be mounted explicitly to /mnt/timeshift/restore
* Target device will be mounted explicitly to /run/timeshift/<pid>/restore
* Existing mount points are not used since we need to mount other devices in sub-directories
* */

Expand Down Expand Up @@ -4257,11 +4260,87 @@ public class Main : GLib.Object{
app_lock.remove();

dir_delete(TEMP_DIR);


cleanup_unmount_devices();

exit(exit_code);

//Gtk.main_quit ();
}

private void cleanup_unmount_devices(){

log_debug("cleanup_unmount_devices()");

if (!dir_exists("/run/timeshift")){ return; }

var dirlist = dir_list_names("/run/timeshift");

foreach(var dname in dirlist){

int pid = int.parse(dname);

if (pid != Posix.getpid()){ // if some other process

// check if the process is still running

string procdir = "/proc/%d".printf(pid);

if (dir_exists(procdir)){ continue; }
}

// -----------------------------------------------

string mdir = "/run/timeshift/%s".printf(dname);

var dirlist2 = dir_list_names(mdir);

foreach(var dname2 in dirlist2){

string mdir2 = "/run/timeshift/%s/%s".printf(dname, dname2);

// check if a device is mounted here

foreach (var dev in Device.get_filesystems()){

foreach (var mnt in dev.mount_points){

if (mnt.mount_point == mdir2){

log_msg("\nFound stale mount for device '%s' at path '%s'".printf(dev.device, mdir2));

string cmd = "umount '%s'".printf(escape_single_quote(mdir2));
int retval = exec_sync(cmd);

string cmd2 = "rmdir '%s'".printf(escape_single_quote(mdir2));
int retval2 = exec_sync(cmd2);

if (retval2 != 0){
log_error("Failed to unmount");
log_msg("Ret=%d".printf(retval));
//ignore
}
else{
log_msg("Unmounted successfully");
}
}
}
}
}

if (dir_exists(mdir)){

string cmd3 = "rmdir '%s'".printf(escape_single_quote(mdir));
int retval3 = exec_sync(cmd3);

if (retval3 != 0){
log_error("Failed to remove directory");
log_msg("Ret=%d".printf(retval3));
//ignore
}
}
}
}
}


Expand Down
4 changes: 2 additions & 2 deletions src/Core/SnapshotRepo.vala
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public class SnapshotRepo : GLib.Object{
log_debug("device=%s".printf(device.device));
}

mount_path = unlock_and_mount_device(device, "/run/timeshift/backup");
mount_path = unlock_and_mount_device(device, App.mount_point_app + "/backup");

if (mount_path.length == 0){
return false;
Expand Down Expand Up @@ -217,7 +217,7 @@ public class SnapshotRepo : GLib.Object{
// @home is on a separate device
device_home = subvol.get_device();

mount_paths["@home"] = unlock_and_mount_device(device_home, "/run/timeshift/backup-home");
mount_paths["@home"] = unlock_and_mount_device(device_home, App.mount_point_app + "/backup-home");

if (mount_paths["@home"].length == 0){
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Subvolume.vala
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ public class Subvolume : GLib.Object{

if (is_system_subvolume){
if (name == "@"){
path = path_combine("/run/timeshift/backup", "@");
path = path_combine(App.mount_point_app + "/backup", "@");
}
else if (name == "@home"){
path = path_combine("/run/timeshift/backup-home", "@home");
path = path_combine(App.mount_point_app + "/backup-home", "@home");
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Gtk/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ class MainWindow : Gtk.Window{
}
}
}

App.exit_app(0);

return false;
}
Expand Down

0 comments on commit d7ba851

Please sign in to comment.