8000 backport 20172 by nurse · Pull Request #9796 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

backport 20172 #9796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
merge revision(s) e5a4f75: [Backport #20086]
	Fix Window private file mapping unlink EACCES issue. (#9358)

	* Don't return early.

	* Add missing `mapping` assignment.

	* Make debug logs conditional.
	---
	 io_buffer.c                 | 18 ++++++++++++------
	 test/ruby/test_io_buffer.rb | 32 ++++++++++++++------------------
	 2 files changed, 26 insertions(+), 24 deletions(-)
  • Loading branch information
nurse committed Jan 30, 2024
commit 818b4ea9b16e3570b431b86da9a24a5743b29617
18 changes: 12 additions & 6 deletions io_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ enum {

// This is used to validate the flags given by the user.
RB_IO_BUFFER_FLAGS_MASK = RB_IO_BUFFER_EXTERNAL | RB_IO_BUFFER_INTERNAL | RB_IO_BUFFER_MAPPED | RB_IO_BUFFER_SHARED | RB_IO_BUFFER_LOCKED | RB_IO_BUFFER_PRIVATE | RB_IO_BUFFER_READONLY,

RB_IO_BUFFER_DEBUG = 0,
};

struct rb_io_buffer {
Expand Down Expand Up @@ -113,6 +115,7 @@ io_buffer_map_file(struct rb_io_buffer *buffer, int descriptor, size_t size, rb_
}

HANDLE mapping = CreateFileMapping(file, NULL, protect, 0, 0, NULL);
if (RB_IO_BUFFER_DEBUG) fprintf(stderr, "io_buffer_map_file:CreateFileMapping -> %p\n", mapping);
if (!mapping) rb_sys_fail("io_buffer_map_descriptor:CreateFileMapping");

void *base = MapViewOfFile(mapping, access, (DWORD)(offset >> 32), (DWORD)(offset & 0xFFFFFFFF), size);
Expand Down Expand Up @@ -213,9 +216,13 @@ io_buffer_initialize(VALUE self, struct rb_io_buffer *buffer, void *base, size_t
buffer->size = size;
buffer->flags = flags;
RB_OBJ_WRITE(self, &buffer->source, source);

#if defined(_WIN32)
buffer->mapping = NULL;
#endif
}

static int
static void
io_buffer_free(struct rb_io_buffer *buffer)
{
if (buffer->base) {
Expand Down Expand Up @@ -247,18 +254,17 @@ io_buffer_free(struct rb_io_buffer *buffer)
buffer->size = 0;
buffer->flags = 0;
buffer->source = Qnil;

return 1;
}

#if defined(_WIN32)
if (buffer->mapping) {
CloseHandle(buffer->mapping);
if (RB_IO_BUFFER_DEBUG) fprintf(stderr, "io_buffer_free:CloseHandle -> %p\n", buffer->mapping);
if (!CloseHandle(buffer->mapping)) {
fprintf(stderr, "io_buffer_free:GetLastError -> %d\n", GetLastError());
}
buffer->mapping = NULL;
}
#endif

return 0;
}

void
Expand Down
32 changes: 14 additions & 18 deletions test/ruby/test_io_buffer.rb
8F0A
Original file line number Diff line number Diff line change
Expand Up @@ -521,24 +521,20 @@ def test_shared
def test_private
Tempfile.create(%w"buffer .txt") do |file|
file.write("Hello World")
file.close
assert_separately(["-W0", "-", file.path], "#{<<-"begin;"}\n#{<<-'end;'}")
begin;
file = File.open(ARGV[0], "r+")
buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE)
begin
assert buffer.private?
refute buffer.readonly?

buffer.set_string("J")

# It was not changed because the mapping was private:
file.seek(0)
assert_equal "Hello World", file.read
ensure
buffer&.free
end
end;

buffer = IO::Buffer.map(file, nil, 0, IO::Buffer::PRIVATE)
begin
assert buffer.private?
refute buffer.readonly?

buffer.set_string("J")

# It was not changed because the mapping was private:
file.seek(0)
assert_equal "Hello World", file.read
ensure
buffer&.free
end
end
end
end
2 changes: 1 addition & 1 deletion version.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR
#define RUBY_VERSION_TEENY 0
#define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR
#define RUBY_PATCHLEVEL 0
#define RUBY_PATCHLEVEL 1

#include "ruby/version.h"
#include "ruby/internal/abi.h"
Expand Down
0