From 39c79e08e41aadd71f8f973c0c3a6422757dcba9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 26 Jul 2024 13:12:01 +0200 Subject: [PATCH] daemon: move sortMounts to a platform-agnostic file The same code was used both on Linux and Windows; move it to a platform- agnostic file so that both can use this function, which contains GoDoc describing the functionality. Signed-off-by: Sebastiaan van Stijn --- daemon/oci_linux.go | 3 +-- daemon/volumes.go | 9 +++++++++ daemon/volumes_unix.go | 9 --------- daemon/volumes_windows.go | 4 +--- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/daemon/oci_linux.go b/daemon/oci_linux.go index c0a992db44..c9a82792e0 100644 --- a/daemon/oci_linux.go +++ b/daemon/oci_linux.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "path/filepath" - "sort" "strconv" "strings" @@ -509,7 +508,7 @@ func inSlice(slice []string, s string) bool { // withMounts sets the container's mounts func withMounts(daemon *Daemon, daemonCfg *configStore, c *container.Container, ms []container.Mount) coci.SpecOpts { return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) (err error) { - sort.Sort(mounts(ms)) + sortMounts(ms) mounts := ms diff --git a/daemon/volumes.go b/daemon/volumes.go index 017604889f..2650927433 100644 --- a/daemon/volumes.go +++ b/daemon/volumes.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "sort" "strings" "time" @@ -47,6 +48,14 @@ func (m mounts) parts(i int) int { return strings.Count(filepath.Clean(m[i].Destination), string(os.PathSeparator)) } +// sortMounts sorts an array of mounts in lexicographic order. This ensure that +// when mounting, the mounts don't shadow other mounts. For example, if mounting +// /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first. +func sortMounts(m []container.Mount) []container.Mount { + sort.Sort(mounts(m)) + return m +} + // registerMountPoints initializes the container mount points with the configured volumes and bind mounts. // It follows the next sequence to decide what to mount in each final destination: // diff --git a/daemon/volumes_unix.go b/daemon/volumes_unix.go index 623ef57dd9..f917d80a75 100644 --- a/daemon/volumes_unix.go +++ b/daemon/volumes_unix.go @@ -6,7 +6,6 @@ import ( "context" "fmt" "os" - "sort" "strconv" "strings" @@ -121,14 +120,6 @@ func (daemon *Daemon) setupMounts(ctx context.Context, c *container.Container) ( return append(mounts, netMounts...), cleanups.Release(), nil } -// sortMounts sorts an array of mounts in lexicographic order. This ensure that -// when mounting, the mounts don't shadow other mounts. For example, if mounting -// /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first. -func sortMounts(m []container.Mount) []container.Mount { - sort.Sort(mounts(m)) - return m -} - // setBindModeIfNull is platform specific processing to ensure the // shared mode is set to 'z' if it is null. This is called in the case // of processing a named volume and not a typical bind. diff --git a/daemon/volumes_windows.go b/daemon/volumes_windows.go index 2d70abc450..6a2b81050f 100644 --- a/daemon/volumes_windows.go +++ b/daemon/volumes_windows.go @@ -2,7 +2,6 @@ package daemon // import "github.com/docker/docker/daemon" import ( "context" - "sort" "github.com/containerd/log" "github.com/docker/docker/api/types/mount" @@ -49,8 +48,7 @@ func (daemon *Daemon) setupMounts(ctx context.Context, c *container.Container) ( }) } - sort.Sort(mounts(mnts)) - return mnts, mntCleanups.Release(), nil + return sortMounts(mnts), mntCleanups.Release(), nil } // setBindModeIfNull is platform specific processing which is a no-op on