1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove MkdirAllNewAs and update tests.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-05-31 17:36:48 -04:00
parent df248d31d9
commit 6150ebf7b4
4 changed files with 23 additions and 48 deletions

View file

@ -34,7 +34,7 @@ func (clnt *client) prepareBundleDir(uid, gid int) (string, error) {
} }
if os.IsNotExist(err) || fi.Mode()&1 == 0 { if os.IsNotExist(err) || fi.Mode()&1 == 0 {
p = fmt.Sprintf("%s.%d.%d", p, uid, gid) p = fmt.Sprintf("%s.%d.%d", p, uid, gid)
if err := idtools.MkdirAs(p, 0700, uid, gid); err != nil && !os.IsExist(err) { if err := idtools.MkdirAndChown(p, 0700, idtools.IDPair{uid, gid}); err != nil && !os.IsExist(err) {
return "", err return "", err
} }
} }
@ -71,7 +71,7 @@ func (clnt *client) Create(containerID string, checkpoint string, checkpointDir
} }
}() }()
if err := idtools.MkdirAllAs(container.dir, 0700, uid, gid); err != nil && !os.IsExist(err) { if err := idtools.MkdirAllAndChown(container.dir, 0700, idtools.IDPair{uid, gid}); err != nil && !os.IsExist(err) {
return err return err
} }

View file

@ -46,14 +46,15 @@ func untarHandler(tarArchive io.Reader, dest string, options *archive.TarOptions
options.ExcludePatterns = []string{} options.ExcludePatterns = []string{}
} }
rootUID, rootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps) idMappings := idtools.NewIDMappingsFromMaps(options.UIDMaps, options.GIDMaps)
rootIDs, err := idMappings.RootPair()
if err != nil { if err != nil {
return err return err
} }
dest = filepath.Clean(dest) dest = filepath.Clean(dest)
if _, err := os.Stat(dest); os.IsNotExist(err) { if _, err := os.Stat(dest); os.IsNotExist(err) {
if err := idtools.MkdirAllNewAs(dest, 0755, rootUID, rootGID); err != nil { if err := idtools.MkdirAllAndChownNew(dest, 0755, rootIDs); err != nil {
return err return err
} }
} }

View file

@ -42,14 +42,6 @@ func MkdirAllAs(path string, mode os.FileMode, ownerUID, ownerGID int) error {
return mkdirAs(path, mode, ownerUID, ownerGID, true, true) return mkdirAs(path, mode, ownerUID, ownerGID, true, true)
} }
// MkdirAllNewAs creates a directory (include any along the path) and then modifies
// ownership ONLY of newly created directories to the requested uid/gid. If the
// directories along the path exist, no change of ownership will be performed
// Deprecated: Use MkdirAllAndChownNew
func MkdirAllNewAs(path string, mode os.FileMode, ownerUID, ownerGID int) error {
return mkdirAs(path, mode, ownerUID, ownerGID, true, false)
}
// MkdirAs creates a directory and then modifies ownership to the requested uid/gid. // MkdirAs creates a directory and then modifies ownership to the requested uid/gid.
// If the directory already exists, this function still changes ownership // If the directory already exists, this function still changes ownership
// Deprecated: Use MkdirAndChown with a IDPair // Deprecated: Use MkdirAndChown with a IDPair

View file

@ -9,6 +9,8 @@ import (
"path/filepath" "path/filepath"
"syscall" "syscall"
"testing" "testing"
"github.com/stretchr/testify/require"
) )
type node struct { type node struct {
@ -76,12 +78,9 @@ func TestMkdirAllAs(t *testing.T) {
} }
} }
func TestMkdirAllNewAs(t *testing.T) { func TestMkdirAllAndChownNew(t *testing.T) {
dirName, err := ioutil.TempDir("", "mkdirnew") dirName, err := ioutil.TempDir("", "mkdirnew")
if err != nil { require.NoError(t, err)
t.Fatalf("Couldn't create temp dir: %v", err)
}
defer os.RemoveAll(dirName) defer os.RemoveAll(dirName)
testTree := map[string]node{ testTree := map[string]node{
@ -91,49 +90,32 @@ func TestMkdirAllNewAs(t *testing.T) {
"lib/x86_64": {45, 45}, "lib/x86_64": {45, 45},
"lib/x86_64/share": {1, 1}, "lib/x86_64/share": {1, 1},
} }
require.NoError(t, buildTree(dirName, testTree))
if err := buildTree(dirName, testTree); err != nil {
t.Fatal(err)
}
// test adding a directory to a pre-existing dir; only the new dir is owned by the uid/gid // test adding a directory to a pre-existing dir; only the new dir is owned by the uid/gid
if err := MkdirAllNewAs(filepath.Join(dirName, "usr", "share"), 0755, 99, 99); err != nil { err = MkdirAllAndChownNew(filepath.Join(dirName, "usr", "share"), 0755, IDPair{99, 99})
t.Fatal(err) require.NoError(t, err)
}
testTree["usr/share"] = node{99, 99} testTree["usr/share"] = node{99, 99}
verifyTree, err := readTree(dirName, "") verifyTree, err := readTree(dirName, "")
if err != nil { require.NoError(t, err)
t.Fatal(err) require.NoError(t, compareTrees(testTree, verifyTree))
}
if err := compareTrees(testTree, verifyTree); err != nil {
t.Fatal(err)
}
// test 2-deep new directories--both should be owned by the uid/gid pair // test 2-deep new directories--both should be owned by the uid/gid pair
if err := MkdirAllNewAs(filepath.Join(dirName, "lib", "some", "other"), 0755, 101, 101); err != nil { err = MkdirAllAndChownNew(filepath.Join(dirName, "lib", "some", "other"), 0755, IDPair{101, 101})
t.Fatal(err) require.NoError(t, err)
}
testTree["lib/some"] = node{101, 101} testTree["lib/some"] = node{101, 101}
testTree["lib/some/other"] = node{101, 101} testTree["lib/some/other"] = node{101, 101}
verifyTree, err = readTree(dirName, "") verifyTree, err = readTree(dirName, "")
if err != nil { require.NoError(t, err)
t.Fatal(err) require.NoError(t, compareTrees(testTree, verifyTree))
}
if err := compareTrees(testTree, verifyTree); err != nil {
t.Fatal(err)
}
// test a directory that already exists; should NOT be chowned // test a directory that already exists; should NOT be chowned
if err := MkdirAllNewAs(filepath.Join(dirName, "usr"), 0755, 102, 102); err != nil { err = MkdirAllAndChownNew(filepath.Join(dirName, "usr"), 0755, IDPair{102, 102})
t.Fatal(err) require.NoError(t, err)
}
verifyTree, err = readTree(dirName, "") verifyTree, err = readTree(dirName, "")
if err != nil { require.NoError(t, err)
t.Fatal(err) require.NoError(t, compareTrees(testTree, verifyTree))
}
if err := compareTrees(testTree, verifyTree); err != nil {
t.Fatal(err)
}
} }
func TestMkdirAs(t *testing.T) { func TestMkdirAs(t *testing.T) {