зеркало из https://github.com/microsoft/docker.git
Merge pull request #11909 from runcom/11908-refactor-utils-utils-daemon
Refactor utils/utils_daemon
This commit is contained in:
Коммит
ebd7df3bbe
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
|
@ -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)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Загрузка…
Ссылка в новой задаче