aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--NEWS6
-rw-r--r--file.c5
-rw-r--r--include/ruby/win32.h2
-rw-r--r--test/ruby/test_file.rb13
-rw-r--r--win32/win32.c7
6 files changed, 43 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 0f7fe81246..16ae007547 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Fri Jun 12 21:01:44 2015 NAKAMURA Usaku <usa@ruby-lang.org>
+
+ * 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@ruby-lang.org>
* ChangeLog: added missing commit message.
diff --git a/NEWS b/NEWS
index 2ba73b4a26..7ac30e3f0a 100644
--- a/NEWS
+++ b/NEWS
@@ -24,6 +24,12 @@ with all sufficient information, see the ChangeLog file.
* 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
diff --git a/file.c b/file.c
index ac99de6043..28cb3dde6a 100644
--- a/file.c
+++ b/file.c
@@ -5982,6 +5982,11 @@ Init_File(void)
#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));
diff --git a/include/ruby/win32.h b/include/ruby/win32.h
index 285f201788..9daea06323 100644
--- a/include/ruby/win32.h
+++ b/include/ruby/win32.h
@@ -124,6 +124,8 @@ typedef unsigned int uintptr_t;
#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
diff --git a/test/ruby/test_file.rb b/test/ruby/test_file.rb
index 601d19efcd..c4fed73f48 100644
--- a/test/ruby/test_file.rb
+++ b/test/ruby/test_file.rb
@@ -375,6 +375,19 @@ class TestFile < Test::Unit::TestCase
}
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')
diff --git a/win32/win32.c b/win32/win32.c
index b23e9af24d..ab4c9924e3 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -5775,6 +5775,7 @@ rb_w32_open(const char *file, int oflag, ...)
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 oflag, ...)
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 oflag, ...)
_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))