Skip to content

Commit

Permalink
asepsisctl: make install_wrapper and uninstall_wrapper more robust
Browse files Browse the repository at this point in the history
1. we create additional time-stamped panic backup with every new asepsis install
2. asepsisctl uninstall_wrapper --panic restores latest panic backup
3. asepsisctl install_wrapper newly rejects to install if asepsis wrapper is present
4. asepsisctl uninstall_wrapper skips backup restoration in case asepsis wrapper is not present
  • Loading branch information
darwin committed May 22, 2014
1 parent 8df6ac8 commit 1283674
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
5 changes: 2 additions & 3 deletions ctl/cmd/diagnose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ def cmd_diagnose(options)
cry "DesktopServicesPriv has unexpected attributes: #{stat.strip}"
end

# this is simple and stupid test: our wrapper library is small, under 100kb
if File.size(ds_lib) > 100*1024 then
cry "DesktopServicesPriv (#{ds_lib}) is not properly installed.\n => Have you installed system update recently? It might revert it back to the original version."
if not desktopservicespriv_wrapper?(ds_lib) then
cry "DesktopServicesPriv (#{ds_lib}) is not properly installed.\n => Have you installed a system update recently? It might revert it back to the original version."
end

say "Your Asepsis setup seems to be OK" if $is_ok
Expand Down
37 changes: 27 additions & 10 deletions ctl/cmd/install_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
def cmd_install_wrapper(options)
force = options[:force]

unless force
# prevent installing on future OS versions
os_version_check()
end
# prevent installing on future OS versions
os_version_check() unless force

# sanity check
sys("sudo rm -rf \"#{DS_LIB_RELOCATED_FOLDER}\"") if File.exists? DS_LIB_RELOCATED_FOLDER and force
die("wrapper framework seems to be installed (#{DS_LIB_RELOCATED_FOLDER} exists), to reinstall please run: \"asepsisctl uninstall_wrapper\" first") if File.exists? DS_LIB_RELOCATED_FOLDER
ds_lib = File.join(DS_LIB_FOLDER, "DesktopServicesPriv")
if desktopservicespriv_wrapper?(ds_lib) then
die("wrapper framework seems to be installed (#{ds_lib} was created by Asepsis), to reinstall please run: \"asepsisctl uninstall_wrapper\" first")
end

# forced installation should remove potentially existing backup
if force and File.exists? DS_LIB_RELOCATED_FOLDER
sys("sudo rm -rf \"#{DS_LIB_RELOCATED_FOLDER}\"")
end

# make panic backup first
# sanity check
if File.exists? DS_LIB_RELOCATED_FOLDER
die("wrapper framework seems to be installed (#{DS_LIB_RELOCATED_FOLDER} exists), to reinstall please run: \"asepsisctl uninstall_wrapper\" first")
end

# make panic backup (only once, the first time asepsis was installed)
unless File.exists? DS_LIB_PANIC_BACKUP_FOLDER then
sys("sudo cp -r \"#{DS_LIB_FOLDER}\" \"#{DS_LIB_PANIC_BACKUP_FOLDER}\"")
sys("sudo cp -r \"#{DS_LIB_FOLDER}\" \"#{DS_LIB_PANIC_BACKUP_FOLDER}\"")
end

# make timestamped panic backup
timestamp = Time.now.strftime("%Y%m%d%H%M%S")
panic_backup_folder_with_timestamp = "#{DS_LIB_PANIC_BACKUP_FOLDER}_#{timestamp}"
unless File.exists? panic_backup_folder_with_timestamp then
sys("sudo cp -r \"#{DS_LIB_FOLDER}\" \"#{panic_backup_folder_with_timestamp}\"")
end

# make backup
# make normal backup
sys("sudo cp -r \"#{DS_LIB_FOLDER}\" \"#{DS_LIB_BACKUP_FOLDER}\"")
sys("sudo touch \"#{DS_LIB_ASEPSIS_REV}\"")

Expand All @@ -27,4 +44,4 @@ def cmd_install_wrapper(options)
sys("sudo \"#{RESOURCES_PATH}/codesign\" --force --sign - \"#{DS_WRAPPER_SOURCE_PATH}\"") unless lions?
sys("sudo cp \"#{DS_WRAPPER_SOURCE_PATH}\" \"#{DS_LIB_FOLDER}/DesktopServicesPriv\"")
sys("sudo rm -rf \"#{DS_LIB_FOLDER}/_CodeSignature\"")
end
end
42 changes: 34 additions & 8 deletions ctl/cmd/uninstall_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,54 @@
def cmd_uninstall_wrapper(options)
sudo = "sudo"
sudo = "" if options[:nosudo]

if options[:panic] then
sys("#{sudo} cp -r \"#{DS_LIB_PANIC_BACKUP_FOLDER}\"/* \"#{DS_LIB_FOLDER}\"")
# find the latest panic backup
filenames = Dir.glob(DS_LIB_PANIC_BACKUP_FOLDER+"*")
die("no panic backup folders found at #{DS_LIB_PANIC_BACKUP_FOLDER}") if filenames.size==0

sorted_filenames = filenames.sort_by { |filename| File.mtime(filename) }
latest_backup = sorted_filenames[-1]
sys("#{sudo} cp -r \"#{latest_backup}\"/* \"#{DS_LIB_FOLDER}\"")
exit
end

die("wrapper framework seems not to be installed (#{DS_LIB_RELOCATED_FOLDER} does not exist)") unless File.exists? DS_LIB_RELOCATED_FOLDER
# sanity check
if not File.exists? DS_LIB_RELOCATED_FOLDER
die("wrapper framework seems not to be installed (#{DS_LIB_RELOCATED_FOLDER} does not exist)")
end

sudo = "sudo"
sudo = "" if options[:nosudo]
# this is an extra inteligence for this scenario:
# 1. user has OS X 10.9.2
# 2. user installs Asepsis, backup of v10.9.2 DesktopServicesPriv is saved into DS_LIB_BACKUP_FOLDER
# 3. user updates system to OS X 10.9.3, this will overwrite DesktopServicesPriv with v10.9.3
# 4. Asepsis diagnosis correctly reports broken Asepsis
# 5. user correctly runs> asepsisctl uninstall_wrapper, this restores DesktopServicesPriv files to v10.9.2
# 6. OS X 10.9.3 has additional checks and fails to run with v10.9.2 version
#
# solution: we check if our DesktopServicesPriv wrapper is present, if not, we skip backup restoration
#
ds_lib = File.join(DS_LIB_FOLDER, "DesktopServicesPriv")
skip_restore = false
if not desktopservicespriv_wrapper?(ds_lib) then
puts "DesktopServicesPriv wrapper was replaced by system version since last Asepsis installation.\nUsually this is the case when you install a system update which updates DesktopServicesPriv related files.\n => continuing in wrapper uninstallation, but skipping backup restoration."
skip_restore = true
end

# replace DesktopServicesPriv with original version
if File.exists? DS_LIB_ASEPSIS_REV then
# asepsis 1.4 path
die("the backup folder is missing! (#{DS_LIB_BACKUP_FOLDER} does not exist)") unless File.exists? DS_LIB_BACKUP_FOLDER

sys("#{sudo} cp -r \"#{DS_LIB_BACKUP_FOLDER}\"/* \"#{DS_LIB_FOLDER}\"")
sys("#{sudo} cp -r \"#{DS_LIB_BACKUP_FOLDER}\"/* \"#{DS_LIB_FOLDER}\"") unless skip_restore
sys("#{sudo} rm -rf \"#{DS_LIB_BACKUP_FOLDER}\"")
sys("#{sudo} rm -rf \"#{DS_LIB_RELOCATED_FOLDER}\"")
sys("#{sudo} rm \"#{DS_LIB_ASEPSIS_REV}\"")
else
# asepsis 1.3.x and lower
sys("#{sudo} cp -r \"#{DS_LIB_RELOCATED_FOLDER}/_CodeSignature\" \"#{DS_LIB_FOLDER}\"")
sys("#{sudo} cp \"#{DS_LIB_RELOCATED_FOLDER}/DesktopServicesPriv\" \"#{DS_LIB_FOLDER}/DesktopServicesPriv\"")
sys("#{sudo} \"#{RESOURCES_PATH}/install_name_tool\" -id \"#{DS_LIB_FOLDER}/DesktopServicesPriv\" \"#{DS_LIB_FOLDER}/DesktopServicesPriv\"")
sys("#{sudo} cp -r \"#{DS_LIB_RELOCATED_FOLDER}/_CodeSignature\" \"#{DS_LIB_FOLDER}\"") unless skip_restore
sys("#{sudo} cp \"#{DS_LIB_RELOCATED_FOLDER}/DesktopServicesPriv\" \"#{DS_LIB_FOLDER}/DesktopServicesPriv\"") unless skip_restore
sys("#{sudo} \"#{RESOURCES_PATH}/install_name_tool\" -id \"#{DS_LIB_FOLDER}/DesktopServicesPriv\" \"#{DS_LIB_FOLDER}/DesktopServicesPriv\"") unless skip_restore
sys("#{sudo} rm -rf \"#{DS_LIB_RELOCATED_FOLDER}\"")
end
end
5 changes: 5 additions & 0 deletions ctl/lib/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,9 @@ def lions?
def os_version_check()
`sw_vers -productVersion|grep '10\\.\\(7\\|8\\|9\\)'`
die("Asepsis #{ASEPSISCTL_VERSION} can be only installed under OS X versions 10.7, 10.8 and 10.9\nCheck out http://asepsis.binaryage.com for updated version.") if $?!=0
end

def desktopservicespriv_wrapper?(file)
# this is simple and stupid test: our wrapper library is small, under 100kb
File.size(file) <= 100*1024
end

0 comments on commit 1283674

Please sign in to comment.