Signed-off-by: Daniel Nephin <dnephin@docker.com>
| ... | ... |
@@ -34,7 +34,7 @@ func (clnt *client) prepareBundleDir(uid, gid int) (string, error) {
|
| 34 | 34 |
} |
| 35 | 35 |
if os.IsNotExist(err) || fi.Mode()&1 == 0 {
|
| 36 | 36 |
p = fmt.Sprintf("%s.%d.%d", p, uid, gid)
|
| 37 |
- if err := idtools.MkdirAs(p, 0700, uid, gid); err != nil && !os.IsExist(err) {
|
|
| 37 |
+ if err := idtools.MkdirAndChown(p, 0700, idtools.IDPair{uid, gid}); err != nil && !os.IsExist(err) {
|
|
| 38 | 38 |
return "", err |
| 39 | 39 |
} |
| 40 | 40 |
} |
| ... | ... |
@@ -71,7 +71,7 @@ func (clnt *client) Create(containerID string, checkpoint string, checkpointDir |
| 71 | 71 |
} |
| 72 | 72 |
}() |
| 73 | 73 |
|
| 74 |
- if err := idtools.MkdirAllAs(container.dir, 0700, uid, gid); err != nil && !os.IsExist(err) {
|
|
| 74 |
+ if err := idtools.MkdirAllAndChown(container.dir, 0700, idtools.IDPair{uid, gid}); err != nil && !os.IsExist(err) {
|
|
| 75 | 75 |
return err |
| 76 | 76 |
} |
| 77 | 77 |
|
| ... | ... |
@@ -46,14 +46,15 @@ func untarHandler(tarArchive io.Reader, dest string, options *archive.TarOptions |
| 46 | 46 |
options.ExcludePatterns = []string{}
|
| 47 | 47 |
} |
| 48 | 48 |
|
| 49 |
- rootUID, rootGID, err := idtools.GetRootUIDGID(options.UIDMaps, options.GIDMaps) |
|
| 49 |
+ idMappings := idtools.NewIDMappingsFromMaps(options.UIDMaps, options.GIDMaps) |
|
| 50 |
+ rootIDs, err := idMappings.RootPair() |
|
| 50 | 51 |
if err != nil {
|
| 51 | 52 |
return err |
| 52 | 53 |
} |
| 53 | 54 |
|
| 54 | 55 |
dest = filepath.Clean(dest) |
| 55 | 56 |
if _, err := os.Stat(dest); os.IsNotExist(err) {
|
| 56 |
- if err := idtools.MkdirAllNewAs(dest, 0755, rootUID, rootGID); err != nil {
|
|
| 57 |
+ if err := idtools.MkdirAllAndChownNew(dest, 0755, rootIDs); err != nil {
|
|
| 57 | 58 |
return err |
| 58 | 59 |
} |
| 59 | 60 |
} |
| ... | ... |
@@ -42,14 +42,6 @@ func MkdirAllAs(path string, mode os.FileMode, ownerUID, ownerGID int) error {
|
| 42 | 42 |
return mkdirAs(path, mode, ownerUID, ownerGID, true, true) |
| 43 | 43 |
} |
| 44 | 44 |
|
| 45 |
-// MkdirAllNewAs creates a directory (include any along the path) and then modifies |
|
| 46 |
-// ownership ONLY of newly created directories to the requested uid/gid. If the |
|
| 47 |
-// directories along the path exist, no change of ownership will be performed |
|
| 48 |
-// Deprecated: Use MkdirAllAndChownNew |
|
| 49 |
-func MkdirAllNewAs(path string, mode os.FileMode, ownerUID, ownerGID int) error {
|
|
| 50 |
- return mkdirAs(path, mode, ownerUID, ownerGID, true, false) |
|
| 51 |
-} |
|
| 52 |
- |
|
| 53 | 45 |
// MkdirAs creates a directory and then modifies ownership to the requested uid/gid. |
| 54 | 46 |
// If the directory already exists, this function still changes ownership |
| 55 | 47 |
// Deprecated: Use MkdirAndChown with a IDPair |
| ... | ... |
@@ -9,6 +9,8 @@ import ( |
| 9 | 9 |
"path/filepath" |
| 10 | 10 |
"syscall" |
| 11 | 11 |
"testing" |
| 12 |
+ |
|
| 13 |
+ "github.com/stretchr/testify/require" |
|
| 12 | 14 |
) |
| 13 | 15 |
|
| 14 | 16 |
type node struct {
|
| ... | ... |
@@ -76,12 +78,9 @@ func TestMkdirAllAs(t *testing.T) {
|
| 76 | 76 |
} |
| 77 | 77 |
} |
| 78 | 78 |
|
| 79 |
-func TestMkdirAllNewAs(t *testing.T) {
|
|
| 80 |
- |
|
| 79 |
+func TestMkdirAllAndChownNew(t *testing.T) {
|
|
| 81 | 80 |
dirName, err := ioutil.TempDir("", "mkdirnew")
|
| 82 |
- if err != nil {
|
|
| 83 |
- t.Fatalf("Couldn't create temp dir: %v", err)
|
|
| 84 |
- } |
|
| 81 |
+ require.NoError(t, err) |
|
| 85 | 82 |
defer os.RemoveAll(dirName) |
| 86 | 83 |
|
| 87 | 84 |
testTree := map[string]node{
|
| ... | ... |
@@ -91,49 +90,32 @@ func TestMkdirAllNewAs(t *testing.T) {
|
| 91 | 91 |
"lib/x86_64": {45, 45},
|
| 92 | 92 |
"lib/x86_64/share": {1, 1},
|
| 93 | 93 |
} |
| 94 |
- |
|
| 95 |
- if err := buildTree(dirName, testTree); err != nil {
|
|
| 96 |
- t.Fatal(err) |
|
| 97 |
- } |
|
| 94 |
+ require.NoError(t, buildTree(dirName, testTree)) |
|
| 98 | 95 |
|
| 99 | 96 |
// test adding a directory to a pre-existing dir; only the new dir is owned by the uid/gid |
| 100 |
- if err := MkdirAllNewAs(filepath.Join(dirName, "usr", "share"), 0755, 99, 99); err != nil {
|
|
| 101 |
- t.Fatal(err) |
|
| 102 |
- } |
|
| 97 |
+ err = MkdirAllAndChownNew(filepath.Join(dirName, "usr", "share"), 0755, IDPair{99, 99})
|
|
| 98 |
+ require.NoError(t, err) |
|
| 99 |
+ |
|
| 103 | 100 |
testTree["usr/share"] = node{99, 99}
|
| 104 | 101 |
verifyTree, err := readTree(dirName, "") |
| 105 |
- if err != nil {
|
|
| 106 |
- t.Fatal(err) |
|
| 107 |
- } |
|
| 108 |
- if err := compareTrees(testTree, verifyTree); err != nil {
|
|
| 109 |
- t.Fatal(err) |
|
| 110 |
- } |
|
| 102 |
+ require.NoError(t, err) |
|
| 103 |
+ require.NoError(t, compareTrees(testTree, verifyTree)) |
|
| 111 | 104 |
|
| 112 | 105 |
// test 2-deep new directories--both should be owned by the uid/gid pair |
| 113 |
- if err := MkdirAllNewAs(filepath.Join(dirName, "lib", "some", "other"), 0755, 101, 101); err != nil {
|
|
| 114 |
- t.Fatal(err) |
|
| 115 |
- } |
|
| 106 |
+ err = MkdirAllAndChownNew(filepath.Join(dirName, "lib", "some", "other"), 0755, IDPair{101, 101})
|
|
| 107 |
+ require.NoError(t, err) |
|
| 116 | 108 |
testTree["lib/some"] = node{101, 101}
|
| 117 | 109 |
testTree["lib/some/other"] = node{101, 101}
|
| 118 | 110 |
verifyTree, err = readTree(dirName, "") |
| 119 |
- if err != nil {
|
|
| 120 |
- t.Fatal(err) |
|
| 121 |
- } |
|
| 122 |
- if err := compareTrees(testTree, verifyTree); err != nil {
|
|
| 123 |
- t.Fatal(err) |
|
| 124 |
- } |
|
| 111 |
+ require.NoError(t, err) |
|
| 112 |
+ require.NoError(t, compareTrees(testTree, verifyTree)) |
|
| 125 | 113 |
|
| 126 | 114 |
// test a directory that already exists; should NOT be chowned |
| 127 |
- if err := MkdirAllNewAs(filepath.Join(dirName, "usr"), 0755, 102, 102); err != nil {
|
|
| 128 |
- t.Fatal(err) |
|
| 129 |
- } |
|
| 115 |
+ err = MkdirAllAndChownNew(filepath.Join(dirName, "usr"), 0755, IDPair{102, 102})
|
|
| 116 |
+ require.NoError(t, err) |
|
| 130 | 117 |
verifyTree, err = readTree(dirName, "") |
| 131 |
- if err != nil {
|
|
| 132 |
- t.Fatal(err) |
|
| 133 |
- } |
|
| 134 |
- if err := compareTrees(testTree, verifyTree); err != nil {
|
|
| 135 |
- t.Fatal(err) |
|
| 136 |
- } |
|
| 118 |
+ require.NoError(t, err) |
|
| 119 |
+ require.NoError(t, compareTrees(testTree, verifyTree)) |
|
| 137 | 120 |
} |
| 138 | 121 |
|
| 139 | 122 |
func TestMkdirAs(t *testing.T) {
|