From 01724c1cf11ce4a9e3b1978c7c07fd25656ed137 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Sun, 29 Mar 2015 21:48:52 +0200 Subject: [PATCH] Refactor ultis/utils_daemon, fixes #11908 Signed-off-by: Antonio Murdaca --- docker/daemon.go | 15 +++++++++++++-- pkg/fileutils/fileutils.go | 3 ++- utils/utils_daemon.go | 18 ------------------ utils/utils_daemon_test.go | 26 -------------------------- 4 files changed, 15 insertions(+), 47 deletions(-) delete mode 100644 utils/utils_daemon.go delete mode 100644 utils/utils_daemon_test.go diff --git a/docker/daemon.go b/docker/daemon.go index 861bcdbc13..f16c20b902 100644 --- a/docker/daemon.go +++ b/docker/daemon.go @@ -20,9 +20,9 @@ import ( "github.com/docker/docker/pkg/homedir" flag "github.com/docker/docker/pkg/mflag" "github.com/docker/docker/pkg/signal" + "github.com/docker/docker/pkg/system" "github.com/docker/docker/pkg/timeutils" "github.com/docker/docker/registry" - "github.com/docker/docker/utils" ) const CanDaemon = true @@ -41,7 +41,7 @@ func migrateKey() (err error) { // Migrate trust key if exists at ~/.docker/key.json and owned by current user oldPath := filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile) newPath := filepath.Join(getDaemonConfDir(), defaultTrustKeyFile) - if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && utils.IsFileOwner(oldPath) { + if _, statErr := os.Stat(newPath); os.IsNotExist(statErr) && currentUserIsOwner(oldPath) { defer func() { // Ensure old path is removed if no error occurred if err == nil { @@ -191,3 +191,14 @@ func mainDaemon() { } } + +// currentUserIsOwner checks whether the current user is the owner of the given +// file. +func currentUserIsOwner(f string) bool { + if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil { + if int(fileInfo.Uid()) == os.Getuid() { + return true + } + } + return false +} diff --git a/pkg/fileutils/fileutils.go b/pkg/fileutils/fileutils.go index 64442e40fe..4325297651 100644 --- a/pkg/fileutils/fileutils.go +++ b/pkg/fileutils/fileutils.go @@ -1,8 +1,9 @@ package fileutils import ( - "github.com/Sirupsen/logrus" "path/filepath" + + "github.com/Sirupsen/logrus" ) // Matches returns true if relFilePath matches any of the patterns diff --git a/utils/utils_daemon.go b/utils/utils_daemon.go deleted file mode 100644 index 3f8f4d569f..0000000000 --- a/utils/utils_daemon.go +++ /dev/null @@ -1,18 +0,0 @@ -// +build daemon - -package utils - -import ( - "github.com/docker/docker/pkg/system" - "os" -) - -// IsFileOwner checks whether the current user is the owner of the given file. -func IsFileOwner(f string) bool { - if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil { - if int(fileInfo.Uid()) == os.Getuid() { - return true - } - } - return false -} diff --git a/utils/utils_daemon_test.go b/utils/utils_daemon_test.go deleted file mode 100644 index e8361489b7..0000000000 --- a/utils/utils_daemon_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package utils - -import ( - "os" - "path" - "testing" -) - -func TestIsFileOwner(t *testing.T) { - var err error - var file *os.File - - if file, err = os.Create(path.Join(os.TempDir(), "testIsFileOwner")); err != nil { - t.Fatalf("failed to create file: %s", err) - } - file.Close() - - if ok := IsFileOwner(path.Join(os.TempDir(), "testIsFileOwner")); !ok { - t.Fatalf("User should be owner of file") - } - - if err = os.Remove(path.Join(os.TempDir(), "testIsFileOwner")); err != nil { - t.Fatalf("failed to remove file: %s", err) - } - -}