OSDN Git Service

Fix: myopen doesn't LOCK_EX when mode is RDWR or Integer.
authorNARUSE, Yui <naruse@users.sourceforge.jp>
Sat, 5 Dec 2009 06:09:04 +0000 (15:09 +0900)
committerNARUSE, Yui <naruse@users.sourceforge.jp>
Sat, 5 Dec 2009 06:09:04 +0000 (15:09 +0900)
feedgenerator.rb
filemanager.rb

index 34ad9f8..7c1dbf9 100644 (file)
@@ -58,9 +58,16 @@ class Object
   # _arg[1]_ :: モードの指定。例 : w:utf-8(書き込みモード・UTF-8エンコードでファイルを開く)
   def myopen(*arg)
     mode = arg[1]
-    arg[1] = mode[/[^:]+/] if mode && RUBY_VERSION < "1.8.7" && mode.include?(':')
+    rdonly_p = true
+    case mode
+    when String
+      arg[1] = mode[/[^:]+/] if RUBY_VERSION < "1.8.7" && mode.include?(':')
+      rdonly_p = /\A[^:]*[wa+]/ !~ mode
+    when Numeric
+      rdonly_p = !(mode & (IO::WRONY | IO::RDWR))
+    end
     open(*arg) do |f|
-      f.flock(/\A\w+r/ =~ mode ? File::LOCK_SH : File::LOCK_EX)
+      f.flock(rdonly_p ? File::LOCK_SH : File::LOCK_EX)
       return yield(f)
     end
   end
index a476e47..f7275df 100644 (file)
@@ -41,9 +41,16 @@ class Object
   # _arg[1]_ :: モードの指定。例 : w:utf-8(書き込みモード・UTF-8エンコードでファイルを開く)
   def myopen(*arg)
     mode = arg[1]
-    arg[1] = mode[/[^:]+/] if mode && RUBY_VERSION < "1.8.7" && mode.include?(':')
+    rdonly_p = true
+    case mode
+    when String
+      arg[1] = mode[/[^:]+/] if RUBY_VERSION < "1.8.7" && mode.include?(':')
+      rdonly_p = /\A[^:]*[wa+]/ !~ mode
+    when Numeric
+      rdonly_p = !(mode & (IO::WRONY | IO::RDWR))
+    end
     open(*arg) do |f|
-      f.flock(/\A\w+r/ =~ mode ? File::LOCK_SH : File::LOCK_EX)
+      f.flock(rdonly_p ? File::LOCK_SH : File::LOCK_EX)
       return yield(f)
     end
   end