Navigation Menu

Skip to content

Commit

Permalink
Extract logic to write a file safely
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jun 27, 2014
1 parent 71c40fa commit 9b71656
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 24 deletions.
8 changes: 2 additions & 6 deletions bin/droonga-engine-catalog-generate
Expand Up @@ -23,6 +23,7 @@ require "pathname"

require "droonga/engine/version"
require "droonga/catalog_generator"
require "droonga/safe_file_writer"

generator = Droonga::CatalogGenerator.new
current_dataset = {}
Expand Down Expand Up @@ -99,13 +100,8 @@ def open_output(path)
if path == "-"
yield($stdout)
else
# Don't output the file directly to prevent loading of incomplete file!
path = Pathname(path).expand_path
FileUtils.mkdir_p(path.dirname.to_s)
Tempfile.open(path.basename.to_s, path.dirname.to_s, "w") do |output|
Droonga::SafeFileWriter.write(path) do |output|
yield(output)
output.flush
File.rename(output.path, path.to_s)
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions bin/droonga-engine-modify-catalog
Expand Up @@ -23,6 +23,7 @@ require "pathname"

require "droonga/engine/version"
require "droonga/catalog_generator"
require "droonga/safe_file_writer"

generator = Droonga::CatalogGenerator.new
current_dataset = {}
Expand Down Expand Up @@ -125,13 +126,8 @@ def open_output(path)
if path == "-"
yield($stdout)
else
# Don't output the file directly to prevent loading of incomplete file!
path = Pathname(path).expand_path
FileUtils.mkdir_p(path.dirname.to_s)
Tempfile.open(path.basename.to_s, path.dirname.to_s, "w") do |output|
Droonga::SafeFileWriter.write(path) do |output|
yield(output)
output.flush
File.rename(output.path, path.to_s)
end
end
end
Expand Down
15 changes: 3 additions & 12 deletions lib/droonga/command/serf_event_handler.rb
Expand Up @@ -21,6 +21,7 @@

require "droonga/path"
require "droonga/serf"
require "droonga/safe_file_writer"

module Droonga
module Command
Expand Down Expand Up @@ -92,23 +93,13 @@ def output_live_nodes
path = Path.live_nodes
nodes = live_nodes
file_contents = JSON.pretty_generate(nodes)
output(path, file_contents)
Droonga::SafeFileWriter.write(path, file_contents)
end

def save_status(key, value)
status = Serf.load_status
status[key] = value
output(Serf.status_file, JSON.pretty_generate(status))
end

def output(path, file_contents)
FileUtils.mkdir_p(path.parent.to_s)
# Don't output the file directly to prevent loading of incomplete file!
Tempfile.open(path.basename.to_s, path.parent.to_s, "w") do |output|
output.write(file_contents)
output.flush
File.rename(output.path, path.to_s)
end
Droonga::SafeFileWriter.write(Serf.status_file, JSON.pretty_generate(status))
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions lib/droonga/safe_file_writer.rb
@@ -0,0 +1,35 @@
# Copyright (C) 2013-2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

module Droonga
class SafeFileWriter
class << self
def write(path, contents=nil)
# Don't output the file directly to prevent loading of incomplete file!
path = Pathname(path).expand_path
FileUtils.mkdir_p(path.dirname.to_s)
Tempfile.open(path.basename.to_s, path.dirname.to_s, "w") do |output|
if block_given?
yield(output)
else
output.write(contents)
end
output.flush
File.rename(output.path, path.to_s)
end
end
end
end
end

0 comments on commit 9b71656

Please sign in to comment.