Skip to content

Commit

Permalink
Add new "Atomic File Operations" rule
Browse files Browse the repository at this point in the history
When 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 prefer to use atomic file operations.

```ruby
# bad
unless File.exist?(path)
  FileUtils.makedirs(path)
end

if File.exist?(path)
  FileUtils.remove(path)
end

# good
FileUtils.mkdir_p(path)

FileUtils.rm_rf(path)
```
  • Loading branch information
ydah committed Jun 27, 2022
1 parent f04159a commit ae3426d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.adoc
Expand Up @@ -2185,6 +2185,28 @@ File.open('testfile') do |f|
end
----

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

When 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 prefer to use atomic file operations.

[source,ruby]
----
# bad
unless File.exist?(path)
FileUtils.makedirs(path)
end
if File.exist?(path)
FileUtils.remove(path)
end
# good
FileUtils.mkdir_p(path)
FileUtils.rm_rf(path)
----

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

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

0 comments on commit ae3426d

Please sign in to comment.