Permalink
Browse files

Skip save when file is not writable

Previously we would rely on an exception being raised, but this exception is raised after we create the backup file and is never removed again.
  • Loading branch information...
1 parent 7ea7eed commit b59aef28c00e6d102a942971687ece8dbf37ed57 @sellmerfud sellmerfud committed with sorbits Feb 28, 2013
Showing with 14 additions and 10 deletions.
  1. +14 −10 rmate
View
24 rmate
@@ -100,16 +100,20 @@ end
def handle_save(socket, variables, data)
path = variables["token"]
- $stderr.puts "Saving #{path}" if $settings.verbose
- begin
- backup_path = "#{path}~"
- backup_path = "#{backup_path}~" while File.exists? backup_path
- FileUtils.cp(path, backup_path, :preserve => true) if File.exists?(path)
- open(path, 'wb') { |file| file << data }
- File.unlink(backup_path) if File.exist? backup_path
- rescue
- # TODO We probably want some way to notify the server app that the save failed
- $stderr.puts "Save failed! #{$!}" if $settings.verbose
+ if File.writable? path
+ $stderr.puts "Saving #{path}" if $settings.verbose
+ begin
+ backup_path = "#{path}~"
+ backup_path = "#{backup_path}~" while File.exists? backup_path
+ FileUtils.cp(path, backup_path, :preserve => true) if File.exists?(path)
+ open(path, 'wb') { |file| file << data }
+ File.unlink(backup_path) if File.exist? backup_path
+ rescue
+ # TODO We probably want some way to notify the server app that the save failed
+ $stderr.puts "Save failed! #{$!}" if $settings.verbose
+ end
+ else
+ $stderr.puts "Skipping save, file not writable." if $settings.verbose
end
end

0 comments on commit b59aef2

Please sign in to comment.