Skip to content

Commit 1ec2eac

Browse files
author
Rik Nijessen
committed
Make utils_daemon and volumes cross-platform compileable.
Signed-off-by: Rik Nijessen <[email protected]>
1 parent 1a5e46c commit 1ec2eac

10 files changed

Lines changed: 64 additions & 25 deletions

File tree

daemon/volumes.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
"path/filepath"
99
"sort"
1010
"strings"
11-
"syscall"
1211

1312
log "github.com/Sirupsen/logrus"
1413
"github.com/docker/docker/daemon/execdriver"
1514
"github.com/docker/docker/pkg/chrootarchive"
1615
"github.com/docker/docker/pkg/symlink"
16+
"github.com/docker/docker/pkg/system"
1717
"github.com/docker/docker/volumes"
1818
)
1919

@@ -385,15 +385,14 @@ func copyExistingContents(source, destination string) error {
385385
// copyOwnership copies the permissions and uid:gid of the source file
386386
// into the destination file
387387
func copyOwnership(source, destination string) error {
388-
var stat syscall.Stat_t
389-
390-
if err := syscall.Stat(source, &stat); err != nil {
388+
stat, err := system.Stat(source)
389+
if err != nil {
391390
return err
392391
}
393392

394-
if err := os.Chown(destination, int(stat.Uid), int(stat.Gid)); err != nil {
393+
if err := os.Chown(destination, int(stat.Uid()), int(stat.Gid())); err != nil {
395394
return err
396395
}
397396

398-
return os.Chmod(destination, os.FileMode(stat.Mode))
397+
return os.Chmod(destination, os.FileMode(stat.Mode()))
399398
}

pkg/archive/changes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func Changes(layers []string, rw string) ([]Change, error) {
143143
type FileInfo struct {
144144
parent *FileInfo
145145
name string
146-
stat *system.Stat
146+
stat *system.Stat_t
147147
children map[string]*FileInfo
148148
capability []byte
149149
added bool

pkg/system/lstat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"syscall"
77
)
88

9-
func Lstat(path string) (*Stat, error) {
9+
func Lstat(path string) (*Stat_t, error) {
1010
s := &syscall.Stat_t{}
1111
err := syscall.Lstat(path, s)
1212
if err != nil {

pkg/system/lstat_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package system
44

5-
func Lstat(path string) (*Stat, error) {
5+
func Lstat(path string) (*Stat_t, error) {
66
// should not be called on cli code path
77
return nil, ErrNotSupportedPlatform
88
}

pkg/system/stat.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"syscall"
55
)
66

7-
type Stat struct {
7+
type Stat_t struct {
88
mode uint32
99
uid uint32
1010
gid uint32
@@ -13,30 +13,30 @@ type Stat struct {
1313
mtim syscall.Timespec
1414
}
1515

16-
func (s Stat) Mode() uint32 {
16+
func (s Stat_t) Mode() uint32 {
1717
return s.mode
1818
}
1919

20-
func (s Stat) Uid() uint32 {
20+
func (s Stat_t) Uid() uint32 {
2121
return s.uid
2222
}
2323

24-
func (s Stat) Gid() uint32 {
24+
func (s Stat_t) Gid() uint32 {
2525
return s.gid
2626
}
2727

28-
func (s Stat) Rdev() uint64 {
28+
func (s Stat_t) Rdev() uint64 {
2929
return s.rdev
3030
}
3131

32-
func (s Stat) Size() int64 {
32+
func (s Stat_t) Size() int64 {
3333
return s.size
3434
}
3535

36-
func (s Stat) Mtim() syscall.Timespec {
36+
func (s Stat_t) Mtim() syscall.Timespec {
3737
return s.mtim
3838
}
3939

40-
func (s Stat) GetLastModification() syscall.Timespec {
40+
func (s Stat_t) GetLastModification() syscall.Timespec {
4141
return s.Mtim()
4242
}

pkg/system/stat_linux.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ import (
44
"syscall"
55
)
66

7-
func fromStatT(s *syscall.Stat_t) (*Stat, error) {
8-
return &Stat{size: s.Size,
7+
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
8+
return &Stat_t{size: s.Size,
99
mode: s.Mode,
1010
uid: s.Uid,
1111
gid: s.Gid,
1212
rdev: s.Rdev,
1313
mtim: s.Mtim}, nil
1414
}
15+
16+
func Stat(path string) (*Stat_t, error) {
17+
s := &syscall.Stat_t{}
18+
err := syscall.Stat(path, s)
19+
if err != nil {
20+
return nil, err
21+
}
22+
return fromStatT(s)
23+
}

pkg/system/stat_unsupported.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"syscall"
77
)
88

9-
func fromStatT(s *syscall.Stat_t) (*Stat, error) {
10-
return &Stat{size: s.Size,
9+
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
10+
return &Stat_t{size: s.Size,
1111
mode: uint32(s.Mode),
1212
uid: s.Uid,
1313
gid: s.Gid,

pkg/system/stat_windows.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import (
77
"syscall"
88
)
99

10-
func fromStatT(s *syscall.Win32FileAttributeData) (*Stat, error) {
10+
func fromStatT(s *syscall.Win32FileAttributeData) (*Stat_t, error) {
1111
return nil, errors.New("fromStatT should not be called on windows path")
1212
}
13+
14+
func Stat(path string) (*Stat_t, error) {
15+
// should not be called on cli code path
16+
return nil, ErrNotSupportedPlatform
17+
}

utils/utils_daemon.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
package utils
44

55
import (
6+
"github.com/docker/docker/pkg/system"
67
"os"
7-
"syscall"
88
)
99

1010
// IsFileOwner checks whether the current user is the owner of the given file.
1111
func IsFileOwner(f string) bool {
12-
if fileInfo, err := os.Stat(f); err == nil && fileInfo != nil {
13-
if stat, ok := fileInfo.Sys().(*syscall.Stat_t); ok && int(stat.Uid) == os.Getuid() {
12+
if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil {
13+
if int(fileInfo.Uid()) == os.Getuid() {
1414
return true
1515
}
1616
}

utils/utils_daemon_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
)
8+
9+
func TestIsFileOwner(t *testing.T) {
10+
var err error
11+
var file *os.File
12+
13+
if file, err = os.Create(path.Join(os.TempDir(), "testIsFileOwner")); err != nil {
14+
t.Fatalf("failed to create file: %s", err)
15+
}
16+
file.Close()
17+
18+
if ok := IsFileOwner(path.Join(os.TempDir(), "testIsFileOwner")); !ok {
19+
t.Fatalf("User should be owner of file")
20+
}
21+
22+
if err = os.Remove(path.Join(os.TempDir(), "testIsFileOwner")); err != nil {
23+
t.Fatalf("failed to remove file: %s", err)
24+
}
25+
26+
}

0 commit comments

Comments
 (0)