This product is not supported for your selected Datadog site. ().
이 페이지는 아직 영어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:ruby-best-practices/atomic-file-operations
Language: Ruby
Severity: Notice
Category: Best Practices
Description
The rule “Prefer atomic file operations” is designed to promote the use of atomic operations when interacting with files or directories. This is important because non-atomic file operations can lead to race conditions, where the state of a file or directory changes between the time you check its state and the time you perform an operation on it. For example, if you check whether a directory exists and then try to create it if it doesn’t, another process could create the directory after you check, but before you create it, leading to an error.
In Ruby, you can avoid this problem by using methods from the FileUtils module, which provide atomic operations. For example, instead of checking whether a directory exists and then creating it if it doesn’t, you can use the FileUtils.mkdir_p method, which will create the directory if it doesn’t exist, and do nothing if it does.
Non-compliant code often checks for the existence of a file or directory using File.exist? or Dir.exist? and then performs a file operation. To fix this, replace these checks and operations with equivalent atomic operations. For example, replace unless Dir.exist?(path); FileUtils.mkdir(path); end with FileUtils.mkdir_p(path). Similarly, replace if File.exist?(path); FileUtils.remove(path); end with FileUtils.rm_f(path). This will ensure that your file operations are atomic, and safe from race conditions.
Non-Compliant Code Examples
# The failing snippets check if a file or directory exists,# then call a function from FileUtils on that file or directory.unlessDir.exist?(path)FileUtils.mkdir(path)endunlessnotFile.exist?(path)FileUtils.remove(path)endifFile.exist?(path)FileUtils.remove(path)endif!Dir.exist?(path)FileUtils.mkdir(path)end
Compliant Code Examples
# Preferred alternative to check-if-exist-then-mkdirFileUtils.mkdir_p(path)# Preferred alternative to check-if-exist-then-rmFileUtils.rm_f(path)# More than one argument to the operationifFile.exist?(path)FileUtils.doSomething(path,arg)end# These use different modules or methodsunlessFoo.exist?(path)FileUtils.mkdir(path)endunlessDir.exists?(path)FileUtils.mkdir(path)endunlessDir.exist?(path)FileUtilities.mkdir(path)end# These check one thing then operate on another thingunlessDir.exist?(path)FileUtils.mkdir(other_path)end# These check one thing then operate on another thingifFile.exist?(path)FileUtils.remove(other_path)end
원활한 통합. Datadog Code Security를 경험해 보세요
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드를 분석하세요
규칙 사용 방법
1
2
rulesets:- ruby-best-practices # Rules to enforce Ruby best practices.
리포지토리 루트에 위의 내용을 포함하는 static-analysis.datadog.yml을 만듭니다
무료 IDE 플러그인을 사용하거나 CI 파이프라인에 Code Security 검사를 추가합니다