[Groonga-commit] droonga/droonga-engine at 9b71656 [master] Extract logic to write a file safely

Back to archive index

YUKI Hiroshi null+****@clear*****
Fri Jun 27 21:41:29 JST 2014


YUKI Hiroshi	2014-06-27 21:41:29 +0900 (Fri, 27 Jun 2014)

  New Revision: 9b716561ab756a160f822131fdd0a0462150588c
  https://github.com/droonga/droonga-engine/commit/9b716561ab756a160f822131fdd0a0462150588c

  Message:
    Extract logic to write a file safely

  Added files:
    lib/droonga/safe_file_writer.rb
  Modified files:
    bin/droonga-engine-catalog-generate
    bin/droonga-engine-modify-catalog
    lib/droonga/command/serf_event_handler.rb

  Modified: bin/droonga-engine-catalog-generate (+2 -6)
===================================================================
--- bin/droonga-engine-catalog-generate    2014-06-27 21:32:11 +0900 (3f256cf)
+++ bin/droonga-engine-catalog-generate    2014-06-27 21:41:29 +0900 (3553d85)
@@ -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 = {}
@@ -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

  Modified: bin/droonga-engine-modify-catalog (+2 -6)
===================================================================
--- bin/droonga-engine-modify-catalog    2014-06-27 21:32:11 +0900 (7ada1a9)
+++ bin/droonga-engine-modify-catalog    2014-06-27 21:41:29 +0900 (0c7e213)
@@ -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 = {}
@@ -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

  Modified: lib/droonga/command/serf_event_handler.rb (+3 -12)
===================================================================
--- lib/droonga/command/serf_event_handler.rb    2014-06-27 21:32:11 +0900 (bf97ed7)
+++ lib/droonga/command/serf_event_handler.rb    2014-06-27 21:41:29 +0900 (2a71473)
@@ -21,6 +21,7 @@ require "tempfile"
 
 require "droonga/path"
 require "droonga/serf"
+require "droonga/safe_file_writer"
 
 module Droonga
   module Command
@@ -92,23 +93,13 @@ module Droonga
         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

  Added: lib/droonga/safe_file_writer.rb (+35 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/safe_file_writer.rb    2014-06-27 21:41:29 +0900 (a9d9632)
@@ -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
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index