mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #11909 from runcom/11908-refactor-utils-utils-daemon
Refactor utils/utils_daemon
This commit is contained in:
commit
ebd7df3bbe
4 changed files with 15 additions and 47 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue