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

Moving File#fsync, File#flock_exclusive, File#flock_shared, and File#flock_unlock methods to IO::FileDescriptor - Fixes #9793 #9794

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
54 changes: 0 additions & 54 deletions src/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,60 @@ class IO::FileDescriptor < IO
value
end

# 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.
# 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 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)
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 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)
end

# Removes an existing advisory lock held by this process.
def flock_unlock
system_flock_unlock
end

def finalize
return if closed?

Expand Down