From 7b61dd700a1efc1452d3a5daeebf490ee70fc63b Mon Sep 17 00:00:00 2001 From: Tristan Ancelet Date: Sat, 19 Oct 2024 10:16:53 -0500 Subject: [PATCH] Added installation backup functionality --- install-discord-linux.rb | 63 ++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/install-discord-linux.rb b/install-discord-linux.rb index 19491d3..772945d 100755 --- a/install-discord-linux.rb +++ b/install-discord-linux.rb @@ -161,12 +161,13 @@ def do_install(version) File.open(installer_file, 'rb') do |file| Gem::Package.new("").extract_tar_gz(file, $CONFIG[:install_dir]) end + + if not $CONFIG[:keep_installer] + puts "do_install: Removing new installer (#{installer_file})" + File.unlink(installer_file) + end end - if not $CONFIG[:keep_installer] - puts "do_install: Removing new installer (#{installer_file})" - File.unlink(installer_file) - end end def update_desktop_file @@ -194,32 +195,52 @@ def update_desktop_file end def make_backup - files = Array.new - Dir.glob("#{$CONFIG[:discord_config_path]}/**").each{|item| - if File.file?(item) or File.directory?(item) - files << File.new(item) + files = { + files: Array.new, + dirs: Array.new, + symlinks: Array.new + } + + Dir.glob("#{$CONFIG[:discord_config_path].path}/**/**/**/**").each{ |item| + if File.file?(item) + files[:files] << item + elsif File.symlink?(item) + files[:symlinks] << item + elsif File.directory?(item) + files[:dirs] << item end } - Dir.glob("#{$CONFIG[:discord_path]}/**").each {|item| - if File.file?(item) or File.directory?(item) - files << File.new(item) + + Dir.glob("#{$CONFIG[:discord_path].path}/**/**/**/**").each {|item| + if File.file?(item) + files[:files] << item + elsif File.symlink?(item) + files[:symlinks] << item + elsif File.directory?(item) + files[:dirs] << item end } Dir.chdir(Dir.home) do - File.open($CONFIG[:backup_filename], "wb") do |file| - Zlib::GzipWriter.wrap(file) do |gzip| + File.open($CONFIG[:backup_filename], "wb") do |backup_file| + Zlib::GzipWriter.wrap(backup_file) do |gzip| Gem::Package::TarWriter.new(gzip) do |tar| - files.each do |file| - if File.file?(file) - content = file.read() - tar.add_file_simple(file.path.sub("#{Dir.home}/",''), file.stat.mode, content.length) do |io| - io.write(content) + files[:files].each do |file| + File.open(file, 'rb') { |file_to_archive| + contents = file_to_archive.read() + tar.add_file_simple(file_to_archive.path.sub("#{Dir.home}/",''), file_to_archive.stat.mode, contents.length) do |io| + io.write(contents) end - elsif File.directory?(file) - tar.mkdir(file.path.sub("#{Dir.home}/",""), file.stat.mode) - end + } end + files[:dirs].each do |dir| + tar.mkdir(dir.sub("#{Dir.home}/",""), File.stat(dir).mode) + end + + files[:symlinks].each {|symlink| + tar.add_symlink(symlink.sub("#{Dir.home}/",""), File.readlink(symlink), File.lstat(symlink).mode) + } + end end end