From 8ce62c8c06a43e4f1f5945a35d8d7f6ff170d2bf Mon Sep 17 00:00:00 2001 From: Chris Schlaeger Date: Sun, 29 Mar 2015 20:49:49 +0200 Subject: New: Create backup files for all important files that are modified. --- lib/postrunner/ActivitiesDB.rb | 7 +++-- lib/postrunner/Activity.rb | 2 +- lib/postrunner/BackedUpFile.rb | 56 +++++++++++++++++++++++++++++++++++++++ lib/postrunner/Main.rb | 4 +-- lib/postrunner/PersonalRecords.rb | 7 ++--- lib/postrunner/RuntimeConfig.rb | 5 ++-- 6 files changed, 71 insertions(+), 10 deletions(-) create mode 100644 lib/postrunner/BackedUpFile.rb 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 +# 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 @@ -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 +# 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 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 +# +# 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 .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 +# 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 @@ -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 +# 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 @@ -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 +# 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 @@ -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}': #{$!}" -- cgit v1.2.3