From 622902368e56100975ff946aa76f95289f0ad321 Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Sat, 3 Oct 2020 22:11:06 +0800 Subject: [PATCH 1/2] Fixes #9793 --- src/file.cr | 54 --------------------------------------- src/io/file_descriptor.cr | 54 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/file.cr b/src/file.cr index 2ce83afc457e..27efaee62791 100644 --- a/src/file.cr +++ b/src/file.cr @@ -801,22 +801,6 @@ class File < IO::FileDescriptor system_truncate(size) end - # Flushes all data written to this File to the disk device so that - # all changed information can be retrieved even if the system - # crashes or is rebooted. The call blocks until the device reports that - # the transfer has completed. - # To reduce disk activity the *flush_metadata* parameter can be set to false, - # then the syscall *fdatasync* will be used and only data required for - # subsequent data retrieval is flushed. Metadata such as modified time and - # access time is not written. - # - # NOTE: Metadata is flushed even when *flush_metadata* is false on Windows - # and DragonFly BSD. - def fsync(flush_metadata = true) : Nil - flush - system_fsync(flush_metadata) - end - # Yields an `IO` to read a section inside this file. # Multiple sections can be read concurrently. def read_at(offset, bytesize, &block) @@ -844,44 +828,6 @@ class File < IO::FileDescriptor io << '>' end - # TODO: use fcntl/lockf instead of flock (which doesn't lock over NFS) - # TODO: always use non-blocking locks, yield fiber until resource becomes available - - def flock_shared(blocking = true) - flock_shared blocking - begin - yield - ensure - flock_unlock - end - end - - # Places a shared advisory lock. More than one process may hold a shared lock for a given file at a given time. - # `IO::Error` is raised if *blocking* is set to `false` and an existing exclusive lock is set. - def flock_shared(blocking = true) - system_flock_shared(blocking) - end - - def flock_exclusive(blocking = true) - flock_exclusive blocking - begin - yield - ensure - flock_unlock - end - end - - # Places an exclusive advisory lock. Only one process may hold an exclusive lock for a given file at a given time. - # `IO::Error` is raised if *blocking* is set to `false` and any existing lock is set. - def flock_exclusive(blocking = true) - system_flock_exclusive(blocking) - end - - # Removes an existing advisory lock held by this process. - def flock_unlock - system_flock_unlock - end - # Deletes this file. def delete File.delete(@path) diff --git a/src/io/file_descriptor.cr b/src/io/file_descriptor.cr index 35754c9ec97b..7aaf4fb4474e 100644 --- a/src/io/file_descriptor.cr +++ b/src/io/file_descriptor.cr @@ -133,6 +133,60 @@ class IO::FileDescriptor < IO value end + # Flushes all data written to this File to the disk device so that + # all changed information can be retrieved even if the system + # crashes or is rebooted. The call blocks until the device reports that + # the transfer has completed. + # To reduce disk activity the *flush_metadata* parameter can be set to false, + # then the syscall *fdatasync* will be used and only data required for + # subsequent data retrieval is flushed. Metadata such as modified time and + # access time is not written. + # + # NOTE: Metadata is flushed even when *flush_metadata* is false on Windows + # and DragonFly BSD. + def fsync(flush_metadata = true) : Nil + flush + system_fsync(flush_metadata) + end + + # TODO: use fcntl/lockf instead of flock (which doesn't lock over NFS) + # TODO: always use non-blocking locks, yield fiber until resource becomes available + + def flock_shared(blocking = true) + flock_shared blocking + begin + yield + ensure + flock_unlock + end + end + + # Places a shared advisory lock. More than one process may hold a shared lock for a given file at a given time. + # `IO::Error` is raised if *blocking* is set to `false` and an existing exclusive lock is set. + def flock_shared(blocking = true) + system_flock_shared(blocking) + end + + def flock_exclusive(blocking = true) + flock_exclusive blocking + begin + yield + ensure + flock_unlock + end + end + + # Places an exclusive advisory lock. Only one process may hold an exclusive lock for a given file at a given time. + # `IO::Error` is raised if *blocking* is set to `false` and any existing lock is set. + def flock_exclusive(blocking = true) + system_flock_exclusive(blocking) + end + + # Removes an existing advisory lock held by this process. + def flock_unlock + system_flock_unlock + end + def finalize return if closed? From 46f4ef3f658cf3534d8ab8d03271a140ae549969 Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Sat, 3 Oct 2020 23:22:28 +0800 Subject: [PATCH 2/2] Update file_descriptor.cr --- src/io/file_descriptor.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/io/file_descriptor.cr b/src/io/file_descriptor.cr index 7aaf4fb4474e..204f468276b6 100644 --- a/src/io/file_descriptor.cr +++ b/src/io/file_descriptor.cr @@ -133,7 +133,7 @@ class IO::FileDescriptor < IO value end - # Flushes all data written to this File to the disk device so that + # Flushes all data written to this File Descriptor to the disk device so that # all changed information can be retrieved even if the system # crashes or is rebooted. The call blocks until the device reports that # the transfer has completed. @@ -161,7 +161,7 @@ class IO::FileDescriptor < IO end end - # Places a shared advisory lock. More than one process may hold a shared lock for a given file at a given time. + # Places a shared advisory lock. More than one process may hold a shared lock for a given file descriptor at a given time. # `IO::Error` is raised if *blocking* is set to `false` and an existing exclusive lock is set. def flock_shared(blocking = true) system_flock_shared(blocking) @@ -176,7 +176,7 @@ class IO::FileDescriptor < IO end end - # Places an exclusive advisory lock. Only one process may hold an exclusive lock for a given file at a given time. + # Places an exclusive advisory lock. Only one process may hold an exclusive lock for a given file descriptor at a given time. # `IO::Error` is raised if *blocking* is set to `false` and any existing lock is set. def flock_exclusive(blocking = true) system_flock_exclusive(blocking)