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 #12764

Closed
Closed
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 {{ flag?(:win32) ? WinError::ERROR_IO_PENDING : Errno::EWOULDBLOCK }}
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
36 changes: 34 additions & 2 deletions src/crystal/system/win32/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,49 @@ module Crystal::System::File
end

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

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

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

private def flock(exclusive, retry)
flags = LibC::DWORD.new
flags |= LibC::LOCKFILE_EXCLUSIVE_LOCK if exclusive

flags |= LibC::LOCKFILE_FAIL_IMMEDIATELY

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

private def lock_file(flags)
overlapped = LibC::OVERLAPPED.new
overlapped.union.offset.offset = LibC::DWORD.new(0)
overlapped.union.offset.offsetHigh = LibC::DWORD.new(0)
size = self.size
if LibC::TRUE == LibC.LockFileEx(fd, flags, 0, size.to_u32!, (size << 32).to_u32!, pointerof(overlapped))
true
else
winerror = WinError.value
if winerror == WinError::ERROR_IO_PENDING
false
else
raise File::Error.from_winerror("LockFileEx", winerror)
end
end
end

private def system_fsync(flush_metadata = true) : Nil
if LibC._commit(fd) != 0
raise IO::Error.from_errno("Error syncing file")
Expand Down
4 changes: 4 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,10 @@ 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 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)

STATUS_PENDING = 0x103
STILL_ACTIVE = STATUS_PENDING
end