mirror of
https://github.com/moby/moby.git
synced 2026-01-11 10:41:43 +00:00
pkg/fileutils: move ReadSymlinkedDirectory to daemon
It has no external consumers, is written with specific behavior, making it not a good candidate to carry in the module. This moves it to the daemon as a non-exported `resolveSymlinkedDirectory` utility, so that it's only accessible where it's currently used. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -63,7 +63,6 @@ import (
|
||||
volumesservice "github.com/docker/docker/daemon/volume/service"
|
||||
"github.com/docker/docker/dockerversion"
|
||||
"github.com/docker/docker/pkg/authorization"
|
||||
"github.com/docker/docker/pkg/fileutils"
|
||||
"github.com/docker/docker/pkg/plugingetter"
|
||||
"github.com/docker/docker/pkg/sysinfo"
|
||||
"github.com/moby/buildkit/util/grpcerrors"
|
||||
@@ -804,7 +803,7 @@ func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.S
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to get the TempDir under %s: %s", config.Root, err)
|
||||
}
|
||||
realTmp, err := fileutils.ReadSymlinkedDirectory(tmp)
|
||||
realTmp, err := resolveSymlinkedDirectory(tmp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Unable to get the full path to the TempDir (%s): %s", tmp, err)
|
||||
}
|
||||
@@ -1540,9 +1539,9 @@ func CreateDaemonRoot(config *config.Config) error {
|
||||
if _, err := os.Stat(config.Root); err != nil && os.IsNotExist(err) {
|
||||
realRoot = config.Root
|
||||
} else {
|
||||
realRoot, err = fileutils.ReadSymlinkedDirectory(config.Root)
|
||||
realRoot, err = resolveSymlinkedDirectory(config.Root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Unable to get the full path to root (%s): %s", config.Root, err)
|
||||
return fmt.Errorf("unable to get the full path to root (%s): %s", config.Root, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1672,3 +1671,25 @@ func (i *imageBackend) GetRepositories(ctx context.Context, ref reference.Named,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// resolveSymlinkedDirectory returns the target directory of a symlink and
|
||||
// checks if it resolves to a directory (not a file).
|
||||
func resolveSymlinkedDirectory(path string) (realPath string, _ error) {
|
||||
var err error
|
||||
realPath, err = filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get absolute path for %s: %w", path, err)
|
||||
}
|
||||
realPath, err = filepath.EvalSymlinks(realPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to canonicalise path for %s: %w", path, err)
|
||||
}
|
||||
realPathInfo, err := os.Stat(realPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to stat target '%s' of '%s': %w", realPath, path, err)
|
||||
}
|
||||
if !realPathInfo.Mode().IsDir() {
|
||||
return "", fmt.Errorf("canonical path points to a file '%s'", realPath)
|
||||
}
|
||||
return realPath, nil
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package daemon
|
||||
import (
|
||||
"net/netip"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
cerrdefs "github.com/containerd/errdefs"
|
||||
@@ -341,3 +343,100 @@ func TestDeriveULABaseNetwork(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Reading a symlink to a directory must return the directory
|
||||
func TestResolveSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
||||
// TODO Windows: Port this test
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Needs porting to Windows")
|
||||
}
|
||||
|
||||
// On macOS, tmp itself is symlinked, so resolve this one upfront;
|
||||
// see https://github.com/golang/go/issues/56259
|
||||
tmpDir, err := filepath.EvalSymlinks(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
srcPath := filepath.Join(tmpDir, "/testReadSymlinkToExistingDirectory")
|
||||
dstPath := filepath.Join(tmpDir, "/dirLinkTest")
|
||||
if err = os.Mkdir(srcPath, 0o777); err != nil {
|
||||
t.Errorf("failed to create directory: %s", err)
|
||||
}
|
||||
|
||||
if err = os.Symlink(srcPath, dstPath); err != nil {
|
||||
t.Errorf("failed to create symlink: %s", err)
|
||||
}
|
||||
|
||||
var symlinkedPath string
|
||||
if symlinkedPath, err = resolveSymlinkedDirectory(dstPath); err != nil {
|
||||
t.Fatalf("failed to read symlink to directory: %s", err)
|
||||
}
|
||||
|
||||
if symlinkedPath != srcPath {
|
||||
t.Fatalf("symlink returned unexpected directory: %s", symlinkedPath)
|
||||
}
|
||||
|
||||
if err = os.Remove(srcPath); err != nil {
|
||||
t.Errorf("failed to remove temporary directory: %s", err)
|
||||
}
|
||||
|
||||
if err = os.Remove(dstPath); err != nil {
|
||||
t.Errorf("failed to remove symlink: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Reading a non-existing symlink must fail
|
||||
func TestResolveSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
symLinkedPath, err := resolveSymlinkedDirectory(path.Join(tmpDir, "/Non/ExistingPath"))
|
||||
if err == nil {
|
||||
t.Errorf("error expected for non-existing symlink")
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
t.Errorf("Expected an os.ErrNotExist, got: %v", err)
|
||||
}
|
||||
if symLinkedPath != "" {
|
||||
t.Fatalf("expected empty path, but '%s' was returned", symLinkedPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Reading a symlink to a file must fail
|
||||
func TestResolveSymlinkedDirectoryToFile(t *testing.T) {
|
||||
// TODO Windows: Port this test
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Needs porting to Windows")
|
||||
}
|
||||
var err error
|
||||
var file *os.File
|
||||
|
||||
// #nosec G303
|
||||
if file, err = os.Create("/tmp/testSymlinkToFile"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
}
|
||||
|
||||
_ = file.Close()
|
||||
|
||||
if err = os.Symlink("/tmp/testSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
|
||||
t.Errorf("failed to create symlink: %s", err)
|
||||
}
|
||||
|
||||
symlinkedPath, err := resolveSymlinkedDirectory("/tmp/fileLinkTest")
|
||||
if err == nil {
|
||||
t.Errorf("resolveSymlinkedDirectory on a symlink to a file should've failed")
|
||||
} else if !strings.HasPrefix(err.Error(), "canonical path points to a file") {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if symlinkedPath != "" {
|
||||
t.Errorf("path should've been empty: %s", symlinkedPath)
|
||||
}
|
||||
|
||||
if err = os.Remove("/tmp/testSymlinkToFile"); err != nil {
|
||||
t.Errorf("failed to remove file: %s", err)
|
||||
}
|
||||
|
||||
if err = os.Remove("/tmp/fileLinkTest"); err != nil {
|
||||
t.Errorf("failed to remove symlink: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package fileutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// ReadSymlinkedDirectory returns the target directory of a symlink.
|
||||
// The target of the symbolic link may not be a file.
|
||||
func ReadSymlinkedDirectory(path string) (realPath string, _ error) {
|
||||
var err error
|
||||
realPath, err = filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to get absolute path for %s: %w", path, err)
|
||||
}
|
||||
realPath, err = filepath.EvalSymlinks(realPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to canonicalise path for %s: %w", path, err)
|
||||
}
|
||||
realPathInfo, err := os.Stat(realPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to stat target '%s' of '%s': %w", realPath, path, err)
|
||||
}
|
||||
if !realPathInfo.Mode().IsDir() {
|
||||
return "", fmt.Errorf("canonical path points to a file '%s'", realPath)
|
||||
}
|
||||
return realPath, nil
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
package fileutils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Reading a symlink to a directory must return the directory
|
||||
func TestReadSymlinkedDirectoryExistingDirectory(t *testing.T) {
|
||||
// TODO Windows: Port this test
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Needs porting to Windows")
|
||||
}
|
||||
|
||||
// On macOS, tmp itself is symlinked, so resolve this one upfront;
|
||||
// see https://github.com/golang/go/issues/56259
|
||||
tmpDir, err := filepath.EvalSymlinks(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
srcPath := filepath.Join(tmpDir, "/testReadSymlinkToExistingDirectory")
|
||||
dstPath := filepath.Join(tmpDir, "/dirLinkTest")
|
||||
if err = os.Mkdir(srcPath, 0o777); err != nil {
|
||||
t.Errorf("failed to create directory: %s", err)
|
||||
}
|
||||
|
||||
if err = os.Symlink(srcPath, dstPath); err != nil {
|
||||
t.Errorf("failed to create symlink: %s", err)
|
||||
}
|
||||
|
||||
var symlinkedPath string
|
||||
if symlinkedPath, err = ReadSymlinkedDirectory(dstPath); err != nil {
|
||||
t.Fatalf("failed to read symlink to directory: %s", err)
|
||||
}
|
||||
|
||||
if symlinkedPath != srcPath {
|
||||
t.Fatalf("symlink returned unexpected directory: %s", symlinkedPath)
|
||||
}
|
||||
|
||||
if err = os.Remove(srcPath); err != nil {
|
||||
t.Errorf("failed to remove temporary directory: %s", err)
|
||||
}
|
||||
|
||||
if err = os.Remove(dstPath); err != nil {
|
||||
t.Errorf("failed to remove symlink: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Reading a non-existing symlink must fail
|
||||
func TestReadSymlinkedDirectoryNonExistingSymlink(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
symLinkedPath, err := ReadSymlinkedDirectory(path.Join(tmpDir, "/Non/ExistingPath"))
|
||||
if err == nil {
|
||||
t.Errorf("error expected for non-existing symlink")
|
||||
}
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
t.Errorf("Expected an os.ErrNotExist, got: %v", err)
|
||||
}
|
||||
if symLinkedPath != "" {
|
||||
t.Fatalf("expected empty path, but '%s' was returned", symLinkedPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Reading a symlink to a file must fail
|
||||
func TestReadSymlinkedDirectoryToFile(t *testing.T) {
|
||||
// TODO Windows: Port this test
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Needs porting to Windows")
|
||||
}
|
||||
var err error
|
||||
var file *os.File
|
||||
|
||||
// #nosec G303
|
||||
if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
|
||||
t.Fatalf("failed to create file: %s", err)
|
||||
}
|
||||
|
||||
_ = file.Close()
|
||||
|
||||
if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
|
||||
t.Errorf("failed to create symlink: %s", err)
|
||||
}
|
||||
|
||||
symlinkedPath, err := ReadSymlinkedDirectory("/tmp/fileLinkTest")
|
||||
if err == nil {
|
||||
t.Errorf("ReadSymlinkedDirectory on a symlink to a file should've failed")
|
||||
}
|
||||
if !strings.HasPrefix(err.Error(), "canonical path points to a file") {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if symlinkedPath != "" {
|
||||
t.Errorf("path should've been empty: %s", symlinkedPath)
|
||||
}
|
||||
|
||||
if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
|
||||
t.Errorf("failed to remove file: %s", err)
|
||||
}
|
||||
|
||||
if err = os.Remove("/tmp/fileLinkTest"); err != nil {
|
||||
t.Errorf("failed to remove symlink: %s", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user