Refactor ultis/utils_daemon, fixes #11908

Signed-off-by: Antonio Murdaca <me@runcom.ninja>
This commit is contained in:
Antonio Murdaca 2015-03-29 21:48:52 +02:00
Родитель 326a5545d4
Коммит 01724c1cf1
4 изменённых файлов: 15 добавлений и 47 удалений

Просмотреть файл

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