Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 74b70e6

Browse files
committed
fix the attach files issue
1 parent 9f9f73e commit 74b70e6

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

daemon/pod.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -462,13 +462,13 @@ func (daemon *Daemon) StartPod(podId, vmId, podArgs string) (int, string, error)
462462
return -1, "", err
463463
}
464464
} else if storageDriver == "aufs" {
465-
err := aufs.AttachFiles(c.Id, fromFile, targetPath, rootPath, f.Perm, uid, gid)
465+
err := aufs.AttachFiles(c.Id, fromFile, targetPath, sharedDir, f.Perm, uid, gid)
466466
if err != nil {
467467
glog.Error("got error when attach files ", err.Error())
468468
return -1, "", err
469469
}
470470
} else if storageDriver == "overlay" {
471-
err := overlay.AttachFiles(c.Id, fromFile, targetPath, rootPath, f.Perm, uid, gid)
471+
err := overlay.AttachFiles(c.Id, fromFile, targetPath, sharedDir, f.Perm, uid, gid)
472472
if err != nil {
473473
glog.Error("got error when attach files ", err.Error())
474474
return -1, "", err

pod/pod.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"reflect"
11+
"regexp"
1112
)
1213

1314
// Pod Data Structure
@@ -172,7 +173,7 @@ func (pod *UserPod) Validate() error {
172173
return errors.New("Files name does not unique")
173174
}
174175
}
175-
176+
var permReg = regexp.MustCompile("0[0-7]{3}")
176177
for idx, container := range pod.Containers {
177178

178179
if uniq, _ := keySet(container.Volumes); !uniq {
@@ -187,6 +188,14 @@ func (pod *UserPod) Validate() error {
187188
if _, ok := fset[f.Filename]; !ok {
188189
return fmt.Errorf("in container %d, file %s does not exist in file list.", idx, f.Filename)
189190
}
191+
if f.Perm == "" {
192+
f.Perm = "0755"
193+
}
194+
if f.Perm != "0" {
195+
if !permReg.Match([]byte(f.Perm)) {
196+
return fmt.Errorf("in container %d, the permission %s only accept Octal digital in string")
197+
}
198+
}
190199
}
191200

192201
for _, v := range container.Volumes {

storage/aufs/aufs.go

+3-10
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ func AttachFiles(containerId, fromFile, toDir, rootDir, perm, uid, gid string) e
6565
if containerId == "" {
6666
return fmt.Errorf("Please make sure the arguments are not NULL!\n")
6767
}
68-
permInt, err := strconv.Atoi(perm)
69-
if err != nil {
70-
return err
71-
}
68+
permInt := utils.ConvertPermStrToInt(perm)
7269
// It just need the block device without copying any files
7370
// FIXME whether we need to return an error if the target directory is null
7471
if toDir == "" {
@@ -98,14 +95,10 @@ func AttachFiles(containerId, fromFile, toDir, rootDir, perm, uid, gid string) e
9895
if err != nil {
9996
return err
10097
}
98+
10199
user_id, _ := strconv.Atoi(uid)
102-
err = syscall.Setuid(user_id)
103-
if err != nil {
104-
return err
105-
}
106100
group_id, _ := strconv.Atoi(gid)
107-
err = syscall.Setgid(group_id)
108-
if err != nil {
101+
if err = syscall.Chown(targetFile, user_id, group_id); err != nil {
109102
return err
110103
}
111104

storage/devicemapper/dm.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,8 @@ func AttachFiles(containerId, devPrefix, fromFile, toDir, rootPath, perm, uid, g
145145
return err
146146
}
147147
user_id, _ := strconv.Atoi(uid)
148-
err = syscall.Setuid(user_id)
149-
if err != nil {
150-
syscall.Unmount(idMountPath, syscall.MNT_DETACH)
151-
return err
152-
}
153148
group_id, _ := strconv.Atoi(gid)
154-
err = syscall.Setgid(group_id)
155-
if err != nil {
149+
if err = syscall.Chown(targetFile, user_id, group_id); err != nil {
156150
syscall.Unmount(idMountPath, syscall.MNT_DETACH)
157151
return err
158152
}

storage/overlay/overlay.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ func AttachFiles(containerId, fromFile, toDir, rootDir, perm, uid, gid string) e
4141
if containerId == "" {
4242
return fmt.Errorf("Please make sure the arguments are not NULL!\n")
4343
}
44-
permInt, err := strconv.Atoi(perm)
45-
if err != nil {
46-
return err
47-
}
44+
permInt := utils.ConvertPermStrToInt(perm)
4845
// It just need the block device without copying any files
4946
// FIXME whether we need to return an error if the target directory is null
5047
if toDir == "" {
@@ -75,13 +72,8 @@ func AttachFiles(containerId, fromFile, toDir, rootDir, perm, uid, gid string) e
7572
return err
7673
}
7774
user_id, _ := strconv.Atoi(uid)
78-
err = syscall.Setuid(user_id)
79-
if err != nil {
80-
return err
81-
}
8275
group_id, _ := strconv.Atoi(gid)
83-
err = syscall.Setgid(group_id)
84-
if err != nil {
76+
if err = syscall.Chown(targetFile, user_id, group_id); err != nil {
8577
return err
8678
}
8779

utils/utils.go

+25
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,28 @@ func FormatMountLabel(src, mountLabel string) string {
7777
}
7878
return src
7979
}
80+
81+
func ConvertPermStrToInt(str string) int {
82+
var res = 0
83+
if str[0] == '0' {
84+
if len(str) == 1 {
85+
res = 0
86+
} else if str[1] == 'x' {
87+
// this is hex number
88+
for i := 2; i < len(str); i++ {
89+
res = res*16 + int(str[i]-'0')
90+
}
91+
} else {
92+
// this is a octal number
93+
for i := 1; i < len(str); i++ {
94+
res = res*8 + int(str[i]-'0')
95+
}
96+
}
97+
} else {
98+
res, _ = strconv.Atoi(str)
99+
}
100+
if res > 511 {
101+
res = 511
102+
}
103+
return res
104+
}

0 commit comments

Comments
 (0)