summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Schlaeger <chris@linux.com>2015-03-29 20:49:49 +0200
committerChris Schlaeger <chris@linux.com>2015-03-29 20:49:49 +0200
commit8ce62c8c06a43e4f1f5945a35d8d7f6ff170d2bf (patch)
treeb641b393b8a831b9ebd2f0c166683202c4476777
parent90be2e3000c932b878772702027f94b09640492c (diff)
downloadpostrunner-8ce62c8c06a43e4f1f5945a35d8d7f6ff170d2bf.zip
New: Create backup files for all important files that are modified.
-rw-r--r--lib/postrunner/ActivitiesDB.rb7
-rw-r--r--lib/postrunner/Activity.rb2
-rw-r--r--lib/postrunner/BackedUpFile.rb56
-rw-r--r--lib/postrunner/Main.rb4
-rw-r--r--lib/postrunner/PersonalRecords.rb7
-rw-r--r--lib/postrunner/RuntimeConfig.rb5
6 files changed, 71 insertions, 10 deletions
diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb
index 559a8e5..ecf778d 100644
--- a/lib/postrunner/ActivitiesDB.rb
+++ b/lib/postrunner/ActivitiesDB.rb
@@ -3,7 +3,7 @@
#
# = ActivitiesDB.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
-# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
+# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
@@ -14,6 +14,7 @@ require 'fileutils'
require 'yaml'
require 'fit4ruby'
+require 'postrunner/BackedUpFile'
require 'postrunner/Activity'
require 'postrunner/PersonalRecords'
require 'postrunner/ActivityListView'
@@ -314,7 +315,9 @@ module PostRunner
def sync
begin
- File.open(@archive_file, 'w') { |f| f.write(@activities.to_yaml) }
+ BackedUpFile.open(@archive_file, 'w') do |f|
+ f.write(@activities.to_yaml)
+ end
rescue StandardError
Log.fatal "Cannot write archive file '#{@archive_file}': #{$!}"
end
diff --git a/lib/postrunner/Activity.rb b/lib/postrunner/Activity.rb
index 3e7e51e..ea809f5 100644
--- a/lib/postrunner/Activity.rb
+++ b/lib/postrunner/Activity.rb
@@ -3,7 +3,7 @@
#
# = Activity.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
-# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
+# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
diff --git a/lib/postrunner/BackedUpFile.rb b/lib/postrunner/BackedUpFile.rb
new file mode 100644
index 0000000..6fa820b
--- /dev/null
+++ b/lib/postrunner/BackedUpFile.rb
@@ -0,0 +1,56 @@
+#!/usr/bin/env ruby -w
+# encoding: UTF-8
+#
+# = ActivitiesDB.rb -- PostRunner - Manage the data from your Garmin sport devices.
+#
+# Copyright (c) 2015 by Chris Schlaeger <cs@taskjuggler.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of version 2 of the GNU General Public License as
+# published by the Free Software Foundation.
+#
+
+module PostRunner
+
+ # BackUpFile is a specialized version of File that creates a copy on
+ class BackedUpFile < File
+
+ def BackedUpFile.open(filename, mode = 'r', *opt, &block)
+ # If the file is opened for writing we create a backup file.
+ create_backup_file(filename) if mode.include?('w') || mode.include?('a')
+ super
+ end
+
+ def BackedUpFile.write(filename, string)
+ create_backup_file(filename)
+ super
+ end
+
+ private
+
+ def BackedUpFile.create_backup_file(filename)
+ bak_file = filename + '.bak'
+
+ # Delete the backup file if it exists.
+ if File.exists?(bak_file)
+ begin
+ File.delete(bak_file)
+ rescue SystemCallError
+ Log.fatal "Cannote remove backup file '#{bak_file}': #{$!}"
+ end
+ end
+
+ # Rename the old file to <filename>.bak
+ if File.exists?(filename)
+ begin
+ File.rename(filename, bak_file)
+ rescue SystemCallError
+ Log.fatal "Cannot rename file '#{filename}' to '#{bak_file}': #{$!}"
+ end
+ end
+ end
+
+ end
+
+end
+
diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb
index 57ca0e3..884608b 100644
--- a/lib/postrunner/Main.rb
+++ b/lib/postrunner/Main.rb
@@ -3,7 +3,7 @@
#
# = Main.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
-# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
+# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
@@ -51,7 +51,7 @@ module PostRunner
opts.separator <<"EOT"
-Copyright (c) 2014 by Chris Schlaeger
+Copyright (c) 2014, 2015 by Chris Schlaeger
This program is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published by the
diff --git a/lib/postrunner/PersonalRecords.rb b/lib/postrunner/PersonalRecords.rb
index 842625a..fc8c2cd 100644
--- a/lib/postrunner/PersonalRecords.rb
+++ b/lib/postrunner/PersonalRecords.rb
@@ -3,7 +3,7 @@
#
# = PersonalRecords.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
-# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
+# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
@@ -14,6 +14,7 @@ require 'fileutils'
require 'yaml'
require 'fit4ruby'
+require 'postrunner/BackedUpFile'
module PostRunner
@@ -109,7 +110,7 @@ module PostRunner
{ :halign => :right },
{ :halign => :right },
{ :halign => :right },
- { :halign => :right },
+ { :halign => :left },
{ :halign => :left }
])
t.body
@@ -145,7 +146,7 @@ module PostRunner
def save_records
begin
- File.open(@records_file, 'w') { |f| f.write(@records.to_yaml) }
+ BackedUpFile.open(@records_file, 'w') { |f| f.write(@records.to_yaml) }
rescue StandardError
Log.fatal "Cannot write records file '#{@records_file}': #{$!}"
end
diff --git a/lib/postrunner/RuntimeConfig.rb b/lib/postrunner/RuntimeConfig.rb
index 5b424df..cd297e0 100644
--- a/lib/postrunner/RuntimeConfig.rb
+++ b/lib/postrunner/RuntimeConfig.rb
@@ -3,7 +3,7 @@
#
# = ActivitiesDB.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
-# Copyright (c) 2014 by Chris Schlaeger <cs@taskjuggler.org>
+# Copyright (c) 2014, 2015 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
@@ -12,6 +12,7 @@
require 'yaml'
require 'fit4ruby'
+require 'postrunner/BackedUpFile'
module PostRunner
@@ -69,7 +70,7 @@ module PostRunner
def save_options
begin
- File.write(@config_file, @options.to_yaml)
+ BackedUpFile.write(@config_file, @options.to_yaml)
Log.info "Runtime config file '#{@config_file}' written"
rescue
Log.error "Cannot write config file '#{@config_file}': #{$!}"