Skip to content
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

Implement flock_* for Win32 #12766

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -933,22 +933,21 @@ describe "File" do
end
end

# TODO: implement flock on windows
describe "flock" do
pending_win32 "exclusively locks a file" do
it "#flock_exclusive" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
file1.flock_exclusive do
exc = expect_raises(IO::Error, "Error applying file lock: file is already locked") do
file2.flock_exclusive(blocking: false) { }
end
exc.os_error.should eq Errno::EWOULDBLOCK
exc.os_error.should eq({% if flag?(:win32) %}WinError::ERROR_LOCK_VIOLATION{% else %}Errno::EWOULDBLOCK{% end %})
end
end
end
end

pending_win32 "shared locks a file" do
it "#flock_shared" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
file1.flock_shared do
Expand All @@ -958,7 +957,7 @@ describe "File" do
end
end

pending_win32 "#flock_shared soft blocking fiber" do
it "#flock_shared soft blocking fiber" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
done = Channel(Nil).new
Expand All @@ -975,7 +974,7 @@ describe "File" do
end
end

pending_win32 "#flock_exclusive soft blocking fiber" do
it "#flock_exclusive soft blocking fiber" do
File.open(datapath("test_file.txt")) do |file1|
File.open(datapath("test_file.txt")) do |file2|
done = Channel(Nil).new
Expand Down
45 changes: 42 additions & 3 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,54 @@ module Crystal::System::File
end

private def system_flock_shared(blocking : Bool) : Nil
raise NotImplementedError.new("File#flock_shared")
flock(false, blocking)
end

private def system_flock_exclusive(blocking : Bool) : Nil
raise NotImplementedError.new("File#flock_exclusive")
flock(true, blocking)
end

private def system_flock_unlock : Nil
raise NotImplementedError.new("File#flock_unlock")
unlock_file(windows_handle)
end

private def flock(exclusive, retry)
flags = LibC::LOCKFILE_FAIL_IMMEDIATELY
flags |= LibC::LOCKFILE_EXCLUSIVE_LOCK if exclusive

handle = windows_handle
if retry
until lock_file(handle, flags)
::Fiber.yield
end
else
lock_file(handle, flags) || raise IO::Error.from_winerror("Error applying file lock: file is already locked")
end
end

private def lock_file(handle, flags)
# lpOverlapped must be provided despite the synchronous use of this method.
overlapped = LibC::OVERLAPPED.new
# lock the entire file with offset 0 in overlapped and number of bytes set to max value
if 0 != LibC.LockFileEx(handle, flags, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, pointerof(overlapped))
true
else
winerror = WinError.value
if winerror == WinError::ERROR_LOCK_VIOLATION
false
else
raise IO::Error.from_winerror("LockFileEx", winerror)
end
end
end

private def unlock_file(handle)
# lpOverlapped must be provided despite the synchronous use of this method.
overlapped = LibC::OVERLAPPED.new
# unlock the entire file with offset 0 in overlapped and number of bytes set to max value
if 0 == LibC.UnlockFileEx(handle, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, pointerof(overlapped))
raise IO::Error.from_winerror("UnLockFileEx")
end
end

private def system_fsync(flush_metadata = true) : Nil
Expand Down
15 changes: 15 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/fileapi.cr
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ lib LibC
fun FindNextFileW(hFindFile : HANDLE, lpFindFileData : WIN32_FIND_DATAW*) : BOOL
fun FindClose(hFindFile : HANDLE) : BOOL

fun LockFileEx(
hFile : HANDLE,
dwFlags : DWORD,
dwReserved : DWORD,
nNumberOfBytesToLockLow : DWORD,
nNumberOfBytesToLockHigh : DWORD,
lpOverlapped : OVERLAPPED*
) : BOOL
fun UnlockFileEx(
hFile : HANDLE,
dwReserved : DWORD,
nNumberOfBytesToUnlockLow : DWORD,
nNumberOfBytesToUnlockHigh : DWORD,
lpOverlapped : OVERLAPPED*
) : BOOL
fun SetFileTime(hFile : HANDLE, lpCreationTime : FILETIME*,
lpLastAccessTime : FILETIME*, lpLastWriteTime : FILETIME*) : BOOL
end
3 changes: 3 additions & 0 deletions src/lib_c/x86_64-windows-msvc/c/minwinbase.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ lib LibC
GetFileExMaxInfoLevel
end

LOCKFILE_FAIL_IMMEDIATELY = DWORD.new(0x00000001)
LOCKFILE_EXCLUSIVE_LOCK = DWORD.new(0x00000002)
Comment on lines +48 to +49
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not really needed, right?

Suggested change
LOCKFILE_FAIL_IMMEDIATELY = DWORD.new(0x00000001)
LOCKFILE_EXCLUSIVE_LOCK = DWORD.new(0x00000002)
LOCKFILE_FAIL_IMMEDIATELY = 0x00000001
LOCKFILE_EXCLUSIVE_LOCK = 0x00000002

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily, but that's only because DWORD is equivalent to the default integer type Int32.
Prior evidence:

FILE_TYPE_CHAR = DWORD.new(0x2)
FILE_TYPE_DISK = DWORD.new(0x1)
FILE_TYPE_PIPE = DWORD.new(0x3)
FILE_TYPE_UNKNOWN = DWORD.new(0x0)


STATUS_PENDING = 0x103
STILL_ACTIVE = STATUS_PENDING
end