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

Add new Atomic File Operations rule #898

Merged
merged 1 commit into from Jul 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.adoc
Expand Up @@ -2185,6 +2185,30 @@ File.open('testfile') do |f|
end
----

=== Atomic File Operations [[atomic-file-operations]]

When doing file operations after confirming the existence check of a file, frequent parallel file operations may cause problems that are difficult to reproduce.
Therefore, it is preferable to use atomic file operations.

[source,ruby]
----
# bad - race condition with another process may result in an error in `mkdir`
unless Dir.exist?(path)
FileUtils.mkdir(path)
end

# good - atomic and idempotent creation
FileUtils.mkdir_p(path)

# bad - race condition with another process may result in an error in `remove`
if File.exist?(path)
FileUtils.remove(path)
end

# good - atomic and idempotent removal
FileUtils.rm_f(path)
----

=== Standard Exceptions [[standard-exceptions]]

Prefer the use of exceptions from the standard library over introducing new exception classes.
Expand Down