OSDN Git Service

Validate a yaml file for floodagte history. If it is invalid, the default value ...
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sun, 14 Dec 2008 15:13:19 +0000 (15:13 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sun, 14 Dec 2008 15:13:19 +0000 (15:13 +0000)
shogi-server
shogi_server/league/floodgate.rb

index 0533040..2c7f920 100755 (executable)
@@ -183,14 +183,20 @@ end
 # Return true if the file is writable, otherwise false.
 #
 def is_writable_file?(file)
+  if File.exist?(file)
+    if FileTest.file?(file)
+      return FileTest.writable_real?(file)
+    else
+      return false
+    end
+  end
+  
   begin
     open(file, "w") {|fh| } 
+    FileUtils.rm file
   rescue
     return false
   end
-  unless FileTest.file? file
-    return false
-  end
 
   return true
 end
index 484ffd3..f780cc8 100644 (file)
@@ -101,13 +101,16 @@ class League
       def load
         return unless @file.exist?
 
-        yaml = @file.open("r") {|f| f.read}
-        @records = YAML.load(yaml)
+        @records = YAML.load_file(@file)
+        unless @records && @records.instance_of?(Array)
+          $logger.error "%s is not a valid yaml file. Instead, an empty array will be used and updated." % [@file]
+          @records = [].to_yaml
+        end
       end
 
       def save
         begin
-          @file.open("w+") do |f| 
+          @file.open("w") do |f| 
             f << YAML.dump(@records)
           end
         rescue Errno::ENOSPC