From 78c2a815f1c36e42bee801989ff45bbb1e435425 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Fri, 27 Sep 2024 14:06:36 -0600 Subject: [PATCH] Update copy.DirCopy to not unlink sockets - Closes #2113 `copy.DirCopy` contains code to create named sockets matching those on the source; however, this operation closes the socket, removing it implicitly from the destination and causing later chown and chmod operations to fail. This commit removes configures the socket to not unlink the underlying socket file from the file system during `.Close()`, leaving the socket in place at the destination, as it is in the source. Signed-off-by: Jonathon Anderson --- drivers/copy/copy_linux.go | 3 +++ drivers/copy/copy_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/drivers/copy/copy_linux.go b/drivers/copy/copy_linux.go index caf07602ba..fd3073dbfc 100644 --- a/drivers/copy/copy_linux.go +++ b/drivers/copy/copy_linux.go @@ -203,6 +203,9 @@ func DirCopy(srcDir, dstDir string, copyMode Mode, copyXattrs bool) error { if err != nil { return err } + if unixListener, ok := s.(*net.UnixListener); ok { + unixListener.SetUnlinkOnClose(false) + } s.Close() case mode&os.ModeDevice != 0: diff --git a/drivers/copy/copy_test.go b/drivers/copy/copy_test.go index 9c69908840..487c3fc517 100644 --- a/drivers/copy/copy_test.go +++ b/drivers/copy/copy_test.go @@ -5,6 +5,7 @@ package copy import ( "fmt" "math/rand" + "net" "os" "path/filepath" "syscall" @@ -83,6 +84,13 @@ func randomMode(baseMode int) os.FileMode { func populateSrcDir(t *testing.T, srcDir string, remainingDepth int) { if remainingDepth == 0 { + socketName := filepath.Join(srcDir, "srcsocket") + s, err := net.Listen("unix", socketName) + assert.NilError(t, err) + if unixListener, ok := s.(*net.UnixListener); ok { + unixListener.SetUnlinkOnClose(false) + } + s.Close() return } aTime := time.Unix(rand.Int63(), 0)