mfm/src/filesystems/gocryptfs.cr

42 lines
1.1 KiB
Crystal
Raw Normal View History

2023-10-24 12:50:01 +02:00
require "shellwords"
2023-10-22 23:23:56 +02:00
module GX
class GoCryptFS
2023-10-24 12:50:01 +02:00
getter name : String
getter encrypted_path : String
getter mount_dir : String
2023-10-22 23:23:56 +02:00
2023-10-24 12:50:01 +02:00
def initialize(@name, @encrypted_path, mount_name : String)
home_dir = ENV["HOME"] || raise "Home directory not found"
@mount_dir = File.join(home_dir, "mnt/#{mount_name}")
end
2023-10-22 23:23:56 +02:00
2023-10-24 12:50:01 +02:00
def mounted? : Bool
`mount`.includes?("#{encrypted_path} on #{mount_dir}")
end
2023-10-22 23:23:56 +02:00
2023-10-24 12:50:01 +02:00
def mount
Dir.mkdir_p(mount_dir) unless Dir.exists?(mount_dir)
2023-10-22 23:23:56 +02:00
2023-10-24 12:50:01 +02:00
if mounted?
puts "Already mounted. Skipping.".colorize(:yellow)
else
input = STDIN
output = STDOUT
error = STDERR
process = Process.new("gocryptfs", ["-idle", "15m", encrypted_path, mount_dir], input: input, output: output, error: error)
unless process.wait.success?
puts "Error mounting the vault".colorize(:red)
return
end
puts "GoCryptFS #{name} is now available on #{mount_dir}".colorize(:green)
2023-10-22 23:23:56 +02:00
end
2023-10-24 12:50:01 +02:00
end
def unmount
system("fusermount -u #{mount_dir.shellescape}")
puts "GoCryptFS #{name} is now closed.".colorize(:green)
2023-10-22 23:23:56 +02:00
end
end
end