From 515e7481ded9baffe76f26630e9721f80b08c3de Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Thu, 13 Nov 2014 12:00:04 -0800 Subject: [PATCH] Extract mknod, umask, lstat to pkg/system Some parts of pkg/archive is called on both client/daemon code. To get it compiling on Windows, these funcs are extracted into files with build tags. Signed-off-by: Ahmet Alp Balkan --- archive/archive.go | 2 +- archive/changes.go | 12 +++++++++++- archive/diff.go | 7 ------- system/lstat.go | 16 ++++++++++++++++ system/lstat_windows.go | 12 ++++++++++++ system/mknod.go | 18 ++++++++++++++++++ system/mknod_windows.go | 12 ++++++++++++ system/umask.go | 11 +++++++++++ system/umask_windows.go | 8 ++++++++ 9 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 system/lstat.go create mode 100644 system/lstat_windows.go create mode 100644 system/mknod.go create mode 100644 system/mknod_windows.go create mode 100644 system/umask.go create mode 100644 system/umask_windows.go diff --git a/archive/archive.go b/archive/archive.go index 530ea30..85d2319 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -291,7 +291,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader, L mode |= syscall.S_IFIFO } - if err := syscall.Mknod(path, mode, int(mkdev(hdr.Devmajor, hdr.Devminor))); err != nil { + if err := syscall.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil { return err } diff --git a/archive/changes.go b/archive/changes.go index 0a1f741..720d549 100644 --- a/archive/changes.go +++ b/archive/changes.go @@ -269,6 +269,14 @@ func newRootFileInfo() *FileInfo { return root } +func lstat(path string) (*stat, error) { + s, err := system.Lstat(path) + if err != nil { + return nil, err + } + return fromStatT(s), nil +} + func collectFileInfo(sourceDir string) (*FileInfo, error) { root := newRootFileInfo() @@ -299,9 +307,11 @@ func collectFileInfo(sourceDir string) (*FileInfo, error) { parent: parent, } - if err := syscall.Lstat(path, &info.stat); err != nil { + s, err := lstat(path) + if err != nil { return err } + info.stat = s info.capability, _ = system.Lgetxattr(path, "security.capability") diff --git a/archive/diff.go b/archive/diff.go index 215f62e..c208336 100644 --- a/archive/diff.go +++ b/archive/diff.go @@ -14,13 +14,6 @@ import ( "github.com/docker/docker/pkg/pools" ) -// Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes. -// They are, from low to high: the lower 8 bits of the minor, then 12 bits of the major, -// then the top 12 bits of the minor -func mkdev(major int64, minor int64) uint32 { - return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff)) -} - // ApplyLayer parses a diff in the standard layer format from `layer`, and // applies it to the directory `dest`. func ApplyLayer(dest string, layer ArchiveReader) error { diff --git a/system/lstat.go b/system/lstat.go new file mode 100644 index 0000000..d7e06b3 --- /dev/null +++ b/system/lstat.go @@ -0,0 +1,16 @@ +// +build !windows + +package system + +import ( + "syscall" +) + +func Lstat(path string) (*syscall.Stat_t, error) { + s := &syscall.Stat_t{} + err := syscall.Lstat(path, s) + if err != nil { + return nil, err + } + return s, nil +} diff --git a/system/lstat_windows.go b/system/lstat_windows.go new file mode 100644 index 0000000..f4c7e6d --- /dev/null +++ b/system/lstat_windows.go @@ -0,0 +1,12 @@ +// +build windows + +package system + +import ( + "syscall" +) + +func Lstat(path string) (*syscall.Win32FileAttributeData, error) { + // should not be called on cli code path + return nil, ErrNotSupportedPlatform +} diff --git a/system/mknod.go b/system/mknod.go new file mode 100644 index 0000000..06f9c6a --- /dev/null +++ b/system/mknod.go @@ -0,0 +1,18 @@ +// +build !windows + +package system + +import ( + "syscall" +) + +func Mknod(path string, mode uint32, dev int) error { + return syscall.Mknod(path, mode, dev) +} + +// Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes. +// They are, from low to high: the lower 8 bits of the minor, then 12 bits of the major, +// then the top 12 bits of the minor +func Mkdev(major int64, minor int64) uint32 { + return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff)) +} diff --git a/system/mknod_windows.go b/system/mknod_windows.go new file mode 100644 index 0000000..b4020c1 --- /dev/null +++ b/system/mknod_windows.go @@ -0,0 +1,12 @@ +// +build windows + +package system + +func Mknod(path string, mode uint32, dev int) error { + // should not be called on cli code path + return ErrNotSupportedPlatform +} + +func Mkdev(major int64, minor int64) uint32 { + panic("Mkdev not implemented on windows, should not be called on cli code") +} diff --git a/system/umask.go b/system/umask.go new file mode 100644 index 0000000..fddbecd --- /dev/null +++ b/system/umask.go @@ -0,0 +1,11 @@ +// +build !windows + +package system + +import ( + "syscall" +) + +func Umask(newmask int) (oldmask int, err error) { + return syscall.Umask(newmask), nil +} diff --git a/system/umask_windows.go b/system/umask_windows.go new file mode 100644 index 0000000..3be563f --- /dev/null +++ b/system/umask_windows.go @@ -0,0 +1,8 @@ +// +build windows + +package system + +func Umask(newmask int) (oldmask int, err error) { + // should not be called on cli code path + return 0, ErrNotSupportedPlatform +}