[前][次][番号順一覧][スレッド一覧]

ruby-changes:38767

From: usa <ko1@a...>
Date: Fri, 12 Jun 2015 21:14:33 +0900 (JST)
Subject: [ruby-changes:38767] usa:r50848 (trunk): * file.c (File::SHARE_DELETE): new flag to be able to delete opened file

usa	2015-06-12 21:14:06 +0900 (Fri, 12 Jun 2015)

  New Revision: 50848

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50848

  Log:
    * file.c (File::SHARE_DELETE): new flag to be able to delete opened file
      on Windows.
    
    * include/win32/win32.c (O_SHARE_DELETE): new pseudo file mode flag.
    
    * win32/win32.c (rb_w32_{w,}open): support above flag.  [EXPERIMENTAL]
    
    * NEWS: mention about this feature.
      [Feature #11218] [ruby-dev:49022]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/file.c
    trunk/include/ruby/win32.h
    trunk/test/ruby/test_file.rb
    trunk/win32/win32.c
Index: include/ruby/win32.h
===================================================================
--- include/ruby/win32.h	(revision 50847)
+++ include/ruby/win32.h	(revision 50848)
@@ -124,6 +124,8 @@ typedef unsigned int uintptr_t; https://github.com/ruby/ruby/blob/trunk/include/ruby/win32.h#L124
 
 #define WNOHANG -1
 
+#define O_SHARE_DELETE 0x80000000 /* for rb_w32_open(), rb_w32_wopen() */
+
 typedef int clockid_t;
 #define CLOCK_REALTIME  0
 #define CLOCK_MONOTONIC 1
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50847)
+++ ChangeLog	(revision 50848)
@@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jun 12 21:01:44 2015  NAKAMURA Usaku  <usa@r...>
+
+	* file.c (File::SHARE_DELETE): new flag to be able to delete opened file
+	  on Windows.
+
+	* include/win32/win32.c (O_SHARE_DELETE): new pseudo file mode flag.
+
+	* win32/win32.c (rb_w32_{w,}open): support above flag.  [EXPERIMENTAL]
+
+	* NEWS: mention about this feature.
+	  [Feature #11218] [ruby-dev:49022]
+
 Fri Jun 12 18:21:45 2015  SHIBATA Hiroshi  <hsbt@r...>
 
 	* ChangeLog: added missing commit message.
Index: win32/win32.c
===================================================================
--- win32/win32.c	(revision 50847)
+++ win32/win32.c	(revision 50848)
@@ -5775,6 +5775,7 @@ rb_w32_open(const char *file, int oflag, https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L5775
     va_end(arg);
 
     if ((oflag & O_TEXT) || !(oflag & O_BINARY)) {
+	oflag &= ~O_SHARE_DELETE;
 	ret = _open(file, oflag, pmode);
 	if (ret == -1 && errno == EACCES) check_if_dir(file);
 	return ret;
@@ -5797,7 +5798,10 @@ rb_w32_wopen(const WCHAR *file, int ofla https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L5798
     DWORD attr = FILE_ATTRIBUTE_NORMAL;
     SECURITY_ATTRIBUTES sec;
     HANDLE h;
+    int share_delete;
 
+    share_delete = oflag & O_SHARE_DELETE ? FILE_SHARE_DELETE : 0;
+    oflag &= ~O_SHARE_DELETE;
     if ((oflag & O_TEXT) || !(oflag & O_BINARY)) {
 	va_list arg;
 	int pmode;
@@ -5919,8 +5923,7 @@ rb_w32_wopen(const WCHAR *file, int ofla https://github.com/ruby/ruby/blob/trunk/win32/win32.c#L5923
 	_set_osfhnd(fd, (intptr_t)INVALID_HANDLE_VALUE);
 	_set_osflags(fd, 0);
 
-	h = CreateFileW(file, access, FILE_SHARE_READ | FILE_SHARE_WRITE, &sec,
-			create, attr, NULL);
+	h = CreateFileW(file, access, FILE_SHARE_READ | FILE_SHARE_WRITE | share_delete, &sec, create, attr, NULL);
 	if (h == INVALID_HANDLE_VALUE) {
 	    DWORD e = GetLastError();
 	    if (e != ERROR_ACCESS_DENIED || !check_if_wdir(file))
Index: NEWS
===================================================================
--- NEWS	(revision 50847)
+++ NEWS	(revision 50848)
@@ -24,6 +24,12 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L24
   * Numeric#positive? and Numeric#negative? are added, which return
     true when the receiver is positive and negative respectively.
 
+* IO
+
+  * new mode flag File::SHARE_DELETE is available.
+    this flag means to permit deleting opened file on Windows, but currently
+    this affect only files opened as binary.  [Feature #11218]
+
 === Core classes compatibility issues (excluding feature bug fixes)
 
 * Array
Index: test/ruby/test_file.rb
===================================================================
--- test/ruby/test_file.rb	(revision 50847)
+++ test/ruby/test_file.rb	(revision 50848)
@@ -375,6 +375,19 @@ class TestFile < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file.rb#L375
     }
   end
 
+  def test_file_share_delete
+    Dir.mktmpdir(__method__.to_s) do |tmpdir|
+      tmp = File.join(tmpdir, 'x')
+      File.open(tmp, mode: IO::WRONLY | IO::CREAT | IO::BINARY | IO::SHARE_DELETE) do |f|
+        assert_file.exist?(tmp)
+        assert_nothing_raised do
+          File.unlink(tmp)
+        end
+      end
+      assert_file.not_exist?(tmp)
+    end
+  end
+
   def test_conflicting_encodings
     Dir.mktmpdir(__method__.to_s) do |tmpdir|
       tmp = File.join(tmpdir, 'x')
Index: file.c
===================================================================
--- file.c	(revision 50847)
+++ file.c	(revision 50848)
@@ -5982,6 +5982,11 @@ Init_File(void) https://github.com/ruby/ruby/blob/trunk/file.c#L5982
 #endif
     /* disable line code conversion */
     rb_define_const(rb_mFConst, "BINARY", INT2FIX(O_BINARY));
+#ifndef O_SHARE_DELETE
+# define O_SHARE_DELETE 0
+#endif
+    /* can delete opened file */
+    rb_define_const(rb_mFConst, "SHARE_DELETE", INT2FIX(O_SHARE_DELETE));
 #ifdef O_SYNC
     /* any write operation perform synchronously */
     rb_define_const(rb_mFConst, "SYNC", INT2FIX(O_SYNC));

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]