OSDN Git Service

../shogi_server.rb, ../shogi_server/util.rb: Refactored mkdir_for to Mkdir.mkdir_for.
authordaigo <beatles@users.sourceforge.jp>
Tue, 3 Aug 2010 13:12:36 +0000 (22:12 +0900)
committerDaigo Moriwaki <daigo@debian.org>
Tue, 3 Aug 2010 13:12:36 +0000 (22:12 +0900)
changelog
shogi_server.rb
shogi_server/util.rb
test/TC_logger.rb
test/TC_util.rb

index 2728a70..cc0a334 100644 (file)
--- a/changelog
+++ b/changelog
@@ -6,6 +6,8 @@
            earlier than system time.
          - shogi_server/league/floodgate_thread.rb: SetupFloodgate#start
            now returns an instance correctly.
            earlier than system time.
          - shogi_server/league/floodgate_thread.rb: SetupFloodgate#start
            now returns an instance correctly.
+         - ../shogi_server.rb, ../shogi_server/util.rb: Refactored
+           mkdir_for to Mkdir.mkdir_for.
 
 2010-07-25  Daigo Moriwaki <daigo at debian dot org>
 
 
 2010-07-25  Daigo Moriwaki <daigo at debian dot org>
 
index d7a3659..56c778b 100644 (file)
@@ -66,7 +66,6 @@ end
 module_function :reload
 
 class Logger < ::Logger
 module_function :reload
 
 class Logger < ::Logger
-  @@mkdir_mutex = Mutex.new
 
   def initialize(logdev, shift_age = 0, shift_size = 1048576)
     super
 
   def initialize(logdev, shift_age = 0, shift_size = 1048576)
     super
@@ -74,7 +73,10 @@ class Logger < ::Logger
       def shift_log_period(now)
         age_file = age_file_name(now)
         move_age_file_in_the_way(age_file)
       def shift_log_period(now)
         age_file = age_file_name(now)
         move_age_file_in_the_way(age_file)
-        mkdir_for(age_file)
+        unless Mkdir.mkdir_for(age_file)
+          @dev.write("[ERROR] Could not create a directory: %s\n" % [File.dirname(age_file)])
+          return true
+        end
 
         @dev.close
         rename_file(@filename, age_file)
 
         @dev.close
         rename_file(@filename, age_file)
@@ -110,18 +112,6 @@ class Logger < ::Logger
         rename_file(age_file, new_file)
       end
 
         rename_file(age_file, new_file)
       end
 
-      def mkdir_for(path)
-        @@mkdir_mutex.synchronize do
-          unless FileTest.directory?(File.dirname(path))
-            begin
-              FileUtils.mkdir_p File.dirname(path)
-            rescue
-              @dev.write("[ERROR] Could not create a directory: %s\n" % [File.dirname(path)])
-            end
-          end
-        end # mutex
-      end
-
     end # @logdev
   end
 
     end # @logdev
   end
 
index cd477af..044bd2a 100644 (file)
@@ -20,6 +20,7 @@
 require 'date'
 require 'fileutils'
 require 'pathname'
 require 'date'
 require 'fileutils'
 require 'pathname'
+require 'thread'
 
 module ShogiServer
 
 
 module ShogiServer
 
@@ -92,4 +93,30 @@ module ShogiServer
     return index == 0 ? 7 : index
   end
   module_function :parse_dow
     return index == 0 ? 7 : index
   end
   module_function :parse_dow
+
+  # Mkdir in a thread-safe way.
+  #
+  class Mkdir
+    @@mutex = Mutex.new
+
+    # Return true if a directory is successfully created or a directory
+    # exists already; false otherwise.
+    #
+    # @param path a directory name of a path to be created. For example,
+    # given /hoge/hoo/foo.txt, aim to create /hoge/hoo.
+    def Mkdir.mkdir_for(path)
+      unless FileTest.directory?(File.dirname(path))
+        @@mutex.synchronize do
+          unless FileTest.directory?(File.dirname(path))
+            begin
+              FileUtils.mkdir_p File.dirname(path)
+            rescue
+              return false
+            end
+          end
+        end # mutex
+      end
+      return true
+    end
+  end # class Mkdir
 end
 end
index 5c8689c..52c2258 100644 (file)
@@ -31,14 +31,6 @@ class TestLogger < Test::Unit::TestCase
     @logger.formatter = ShogiServer::Formatter.new
     @logger.level = TestableLogger::DEBUG
     @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
     @logger.formatter = ShogiServer::Formatter.new
     @logger.level = TestableLogger::DEBUG
     @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
-    @test_dir = File.join($topdir, "hoge", "hoo", "foo.txt")
-  end
-
-  def teardown
-    if FileTest.directory?(File.dirname(@test_dir))
-      Dir.rmdir(File.dirname(@test_dir))
-      Dir.rmdir(File.join($topdir, "hoge"))
-    end
   end
 
   def test_dummy
   end
 
   def test_dummy
@@ -87,9 +79,4 @@ class TestLogger < Test::Unit::TestCase
         @logger.logdev.result_rename_file
   end
 
         @logger.logdev.result_rename_file
   end
 
-  def test_mkdir_for
-    assert !FileTest.directory?(File.dirname(@test_dir))
-    @logger.logdev.mkdir_for(@test_dir)
-    assert FileTest.directory?(File.dirname(@test_dir))
-  end
 end
 end
index d3d4931..fcdae10 100644 (file)
@@ -1,4 +1,5 @@
 $:.unshift File.join(File.dirname(__FILE__), "..")
 $:.unshift File.join(File.dirname(__FILE__), "..")
+$topdir = File.expand_path File.dirname(__FILE__)
 require 'test/unit'
 require 'shogi_server/util'
 
 require 'test/unit'
 require 'shogi_server/util'
 
@@ -35,3 +36,24 @@ class TestShogiServer < Test::Unit::TestCase
   end
 
 end
   end
 
 end
+
+
+class TestMkdir < Test::Unit::TestCase
+  def setup
+    @test_dir = File.join($topdir, "hoge", "hoo", "foo.txt")
+  end
+
+  def teardown
+    if FileTest.directory?(File.dirname(@test_dir))
+      Dir.rmdir(File.dirname(@test_dir))
+      Dir.rmdir(File.join($topdir, "hoge"))
+    end
+  end
+
+  def test_mkdir_for
+    assert !FileTest.directory?(File.dirname(@test_dir))
+    ShogiServer::Mkdir.mkdir_for(@test_dir)
+    assert FileTest.directory?(File.dirname(@test_dir))
+  end
+
+end