Browse Source

Refactor ultis/utils_daemon, fixes #11908

Signed-off-by: Antonio Murdaca <me@runcom.ninja>
Antonio Murdaca 10 years ago
parent
commit
01724c1cf1
4 changed files with 15 additions and 47 deletions
  1. 13 2
      docker/daemon.go
  2. 2 1
      pkg/fileutils/fileutils.go
  3. 0 18
      utils/utils_daemon.go
  4. 0 26
      utils/utils_daemon_test.go

+ 13 - 2
docker/daemon.go

@@ -20,9 +20,9 @@ import (
 	"github.com/docker/docker/pkg/homedir"
 	"github.com/docker/docker/pkg/homedir"
 	flag "github.com/docker/docker/pkg/mflag"
 	flag "github.com/docker/docker/pkg/mflag"
 	"github.com/docker/docker/pkg/signal"
 	"github.com/docker/docker/pkg/signal"
+	"github.com/docker/docker/pkg/system"
 	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/pkg/timeutils"
 	"github.com/docker/docker/registry"
 	"github.com/docker/docker/registry"
-	"github.com/docker/docker/utils"
 )
 )
 
 
 const CanDaemon = true
 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
 	// Migrate trust key if exists at ~/.docker/key.json and owned by current user
 	oldPath := filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
 	oldPath := filepath.Join(homedir.Get(), ".docker", defaultTrustKeyFile)
 	newPath := filepath.Join(getDaemonConfDir(), 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() {
 		defer func() {
 			// Ensure old path is removed if no error occurred
 			// Ensure old path is removed if no error occurred
 			if err == nil {
 			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
+}

+ 2 - 1
pkg/fileutils/fileutils.go

@@ -1,8 +1,9 @@
 package fileutils
 package fileutils
 
 
 import (
 import (
-	"github.com/Sirupsen/logrus"
 	"path/filepath"
 	"path/filepath"
+
+	"github.com/Sirupsen/logrus"
 )
 )
 
 
 // Matches returns true if relFilePath matches any of the patterns
 // Matches returns true if relFilePath matches any of the patterns

+ 0 - 18
utils/utils_daemon.go

@@ -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
-}

+ 0 - 26
utils/utils_daemon_test.go

@@ -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)
-	}
-
-}