Project

General

Profile

Bug #11172

Windowsでmode: ab+の時に開いたファイルの内容が読めない

Added by naruse (Yui NARUSE) about 4 years ago. Updated almost 4 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-dev:48989]

Description

Windowsでopen("hoge.txt", "ab+")すると、開いたファイルの既存の内容が読めません。
File::APPEND|File::CREAT|File::RDWR|File::BINARYでも同様です。

Unixでは読める上に、a+の時、つまり_openを使う場合も読めるので、意図しない挙動でしょう。

diff --git a/win32/win32.c b/win32/win32.c
index 55e0d2e..57d9df4 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -6476,12 +6476,16 @@ rb_w32_close(int fd)
 }

 static int
-setup_overlapped(OVERLAPPED *ol, int fd)
+setup_overlapped(OVERLAPPED *ol, int fd, int iswrite)
 {
     memset(ol, 0, sizeof(*ol));
     if (!(_osfile(fd) & (FDEV | FPIPE))) {
    LONG high = 0;
-   DWORD method = _osfile(fd) & FAPPEND ? FILE_END : FILE_CURRENT;
+   /* On mode:a, it can write only FILE_END.
+    * On mode:a+, though it can write only FILE_END,
+    * it can read from everywhere.
+    */
+   DWORD method = ((_osfile(fd) & FAPPEND) && iswrite) ? FILE_END : FILE_CURRENT;
    DWORD low = SetFilePointer((HANDLE)_osfhnd(fd), 0, &high, method);
 #ifndef INVALID_SET_FILE_POINTER
 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
@@ -6578,7 +6582,7 @@ rb_w32_read(int fd, void *buf, size_t size)

     /* if have cancel_io, use Overlapped I/O */
     if (cancel_io) {
-   if (setup_overlapped(&ol, fd)) {
+   if (setup_overlapped(&ol, fd, FALSE)) {
        rb_acrt_lowio_unlock_fh(fd);
        return -1;
    }
@@ -6708,7 +6712,7 @@ rb_w32_write(int fd, const void *buf, size_t size)

     /* if have cancel_io, use Overlapped I/O */
     if (cancel_io) {
-   if (setup_overlapped(&ol, fd)) {
+   if (setup_overlapped(&ol, fd, TRUE)) {
        rb_acrt_lowio_unlock_fh(fd);
        return -1;
    }

Associated revisions

Revision 16b551fe
Added by usa (Usaku NAKAMURA) almost 4 years ago

merge revision(s) 50637: [Backport #11172]

    * win32/win32.c (setup_overlapped): seek to the file end only when
      writing (mode:a), not reading (mode:a+, read).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@50806 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 50806
Added by usa (Usaku NAKAMURA) almost 4 years ago

merge revision(s) 50637: [Backport #11172]

* win32/win32.c (setup_overlapped): seek to the file end only when
  writing (mode:a), not reading (mode:a+, read).

Revision 61ed7934
Added by nagachika (Tomoyuki Chikanaga) almost 4 years ago

merge revision(s) 50637: [Backport #11172]

    * win32/win32.c (setup_overlapped): seek to the file end only when
      writing (mode:a), not reading (mode:a+, read).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@50836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 50836
Added by nagachika (Tomoyuki Chikanaga) almost 4 years ago

merge revision(s) 50637: [Backport #11172]

* win32/win32.c (setup_overlapped): seek to the file end only when
  writing (mode:a), not reading (mode:a+, read).

History

Updated by usa (Usaku NAKAMURA) about 4 years ago

commitしてくださいませ。

Updated by naruse (Yui NARUSE) about 4 years ago

  • Description updated (diff)

Updated by usa (Usaku NAKAMURA) about 4 years ago

  • Status changed from Assigned to Closed

Updated by usa (Usaku NAKAMURA) almost 4 years ago

  • Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED to 2.0.0: REQUIRED, 2.1: DONE, 2.2: REQUIRED

ruby_2_1 r50806 merged revision(s) 50637.

Updated by nagachika (Tomoyuki Chikanaga) almost 4 years ago

  • Backport changed from 2.0.0: REQUIRED, 2.1: DONE, 2.2: REQUIRED to 2.0.0: REQUIRED, 2.1: DONE, 2.2: DONE

Backported into ruby_2_2 branch at r50836.

Also available in: Atom PDF