Replace syscall.Rmdir with golang.org/x/sys for cross-platform directory removal#1885
Merged
dgageot merged 1 commit intodocker:mainfrom Mar 3, 2026
Merged
Conversation
…ory removal Use platform-specific files with golang.org/x/sys/unix and golang.org/x/sys/windows instead of the frozen syscall package. Both unix.Rmdir and windows.RemoveDirectory are atomic, directory-only syscalls, avoiding the TOCTOU race that an os.Lstat + os.Remove sequence would introduce. Fixes docker#1884 Assisted-By: cagent
There was a problem hiding this comment.
Review Summary
This PR successfully modernizes the directory removal implementation by replacing the frozen syscall package with platform-specific implementations from golang.org/x/sys. The changes are well-structured with proper build tags and maintain atomicity guarantees.
However, there is one notable behavioral change in error handling that should be considered.
dgageot
approved these changes
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace
syscall.Rmdirwith platform-specific implementations usinggolang.org/x/sys, the supported successor to the frozensyscallpackage.Changes
rmdir_unix.go— usesunix.Rmdiron aix/darwin/dragonfly/freebsd/linux/netbsd/openbsd/solarisrmdir_windows.go— useswindows.RemoveDirectoryfilesystem.go— calls the newrmdir()helper, removessyscallanderrorsimportsfilesystem_test.go— relaxes assertion to match OS-level error string ("not a directory"vs"is not a directory")Both
unix.Rmdirandwindows.RemoveDirectoryare atomic, directory-only syscalls — they will not accidentally delete regular files and avoid the TOCTOU race that anos.Lstat+os.Removesequence would introduce.golang.org/x/sysis already a dependency of this project.Fixes #1884