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

Merge pull request #15125 from WeiZhang555/golint-stdcopy-system

fix golint warnings/errors on pkg/system and pkg/stdcopy
This commit is contained in:
Jessie Frazelle 2015-08-21 14:27:59 -07:00
commit ecff4badcd
30 changed files with 131 additions and 89 deletions

View file

@ -28,7 +28,7 @@ func copyOwnership(source, destination string) error {
return err
}
if err := os.Chown(destination, int(stat.Uid()), int(stat.Gid())); err != nil {
if err := os.Chown(destination, int(stat.UID()), int(stat.Gid())); err != nil {
return err
}

View file

@ -26,7 +26,7 @@ func setPlatformServerConfig(serverConfig *apiserver.Config, daemonCfg *daemon.C
// file.
func currentUserIsOwner(f string) bool {
if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil {
if int(fileInfo.Uid()) == os.Getuid() {
if int(fileInfo.UID()) == os.Getuid() {
return true
}
}

View file

@ -76,11 +76,13 @@ packages=(
pkg/reexec
pkg/signal
pkg/sockets
pkg/stdcopy
pkg/streamformatter
pkg/stringid
pkg/stringutils
pkg/sysinfo
pkg/symlink
pkg/sysinfo
pkg/system
pkg/tailfile
pkg/tarsum
pkg/term

View file

@ -173,7 +173,7 @@ func Changes(layers []string, rw string) ([]Change, error) {
type FileInfo struct {
parent *FileInfo
name string
stat *system.Stat_t
stat *system.StatT
children map[string]*FileInfo
capability []byte
added bool

View file

@ -8,10 +8,10 @@ import (
"github.com/docker/docker/pkg/system"
)
func statDifferent(oldStat *system.Stat_t, newStat *system.Stat_t) bool {
func statDifferent(oldStat *system.StatT, newStat *system.StatT) bool {
// Don't look at size for dirs, its not a good measure of change
if oldStat.Mode() != newStat.Mode() ||
oldStat.Uid() != newStat.Uid() ||
oldStat.UID() != newStat.UID() ||
oldStat.Gid() != newStat.Gid() ||
oldStat.Rdev() != newStat.Rdev() ||
// Don't look at size for dirs, its not a good measure of change

View file

@ -4,7 +4,7 @@ import (
"github.com/docker/docker/pkg/system"
)
func statDifferent(oldStat *system.Stat_t, newStat *system.Stat_t) bool {
func statDifferent(oldStat *system.StatT, newStat *system.StatT) bool {
// Don't look at size for dirs, its not a good measure of change
if oldStat.ModTime() != newStat.ModTime() ||

View file

@ -9,19 +9,24 @@ import (
)
const (
StdWriterPrefixLen = 8
StdWriterFdIndex = 0
StdWriterSizeIndex = 4
stdWriterPrefixLen = 8
stdWriterFdIndex = 0
stdWriterSizeIndex = 4
)
type StdType [StdWriterPrefixLen]byte
// StdType prefixes type and length to standard stream.
type StdType [stdWriterPrefixLen]byte
var (
Stdin StdType = StdType{0: 0}
Stdout StdType = StdType{0: 1}
Stderr StdType = StdType{0: 2}
// Stdin represents standard input stream type.
Stdin = StdType{0: 0}
// Stdout represents standard output stream type.
Stdout = StdType{0: 1}
// Stderr represents standard error steam type.
Stderr = StdType{0: 2}
)
// StdWriter is wrapper of io.Writer with extra customized info.
type StdWriter struct {
io.Writer
prefix StdType
@ -36,10 +41,10 @@ func (w *StdWriter) Write(buf []byte) (n int, err error) {
binary.BigEndian.PutUint32(w.prefix[4:], uint32(len(buf)))
n1, err = w.Writer.Write(w.prefix[:])
if err != nil {
n = n1 - StdWriterPrefixLen
n = n1 - stdWriterPrefixLen
} else {
n2, err = w.Writer.Write(buf)
n = n1 + n2 - StdWriterPrefixLen
n = n1 + n2 - stdWriterPrefixLen
}
if n < 0 {
n = 0
@ -61,7 +66,7 @@ func NewStdWriter(w io.Writer, t StdType) *StdWriter {
}
}
var ErrInvalidStdHeader = errors.New("Unrecognized input header")
var errInvalidStdHeader = errors.New("Unrecognized input header")
// StdCopy is a modified version of io.Copy.
//
@ -75,7 +80,7 @@ var ErrInvalidStdHeader = errors.New("Unrecognized input header")
// `written` will hold the total number of bytes written to `dstout` and `dsterr`.
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
var (
buf = make([]byte, 32*1024+StdWriterPrefixLen+1)
buf = make([]byte, 32*1024+stdWriterPrefixLen+1)
bufLen = len(buf)
nr, nw int
er, ew error
@ -85,12 +90,12 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
for {
// Make sure we have at least a full header
for nr < StdWriterPrefixLen {
for nr < stdWriterPrefixLen {
var nr2 int
nr2, er = src.Read(buf[nr:])
nr += nr2
if er == io.EOF {
if nr < StdWriterPrefixLen {
if nr < stdWriterPrefixLen {
logrus.Debugf("Corrupted prefix: %v", buf[:nr])
return written, nil
}
@ -103,7 +108,7 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
}
// Check the first byte to know where to write
switch buf[StdWriterFdIndex] {
switch buf[stdWriterFdIndex] {
case 0:
fallthrough
case 1:
@ -113,30 +118,30 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
// Write on stderr
out = dsterr
default:
logrus.Debugf("Error selecting output fd: (%d)", buf[StdWriterFdIndex])
return 0, ErrInvalidStdHeader
logrus.Debugf("Error selecting output fd: (%d)", buf[stdWriterFdIndex])
return 0, errInvalidStdHeader
}
// Retrieve the size of the frame
frameSize = int(binary.BigEndian.Uint32(buf[StdWriterSizeIndex : StdWriterSizeIndex+4]))
frameSize = int(binary.BigEndian.Uint32(buf[stdWriterSizeIndex : stdWriterSizeIndex+4]))
logrus.Debugf("framesize: %d", frameSize)
// Check if the buffer is big enough to read the frame.
// Extend it if necessary.
if frameSize+StdWriterPrefixLen > bufLen {
logrus.Debugf("Extending buffer cap by %d (was %d)", frameSize+StdWriterPrefixLen-bufLen+1, len(buf))
buf = append(buf, make([]byte, frameSize+StdWriterPrefixLen-bufLen+1)...)
if frameSize+stdWriterPrefixLen > bufLen {
logrus.Debugf("Extending buffer cap by %d (was %d)", frameSize+stdWriterPrefixLen-bufLen+1, len(buf))
buf = append(buf, make([]byte, frameSize+stdWriterPrefixLen-bufLen+1)...)
bufLen = len(buf)
}
// While the amount of bytes read is less than the size of the frame + header, we keep reading
for nr < frameSize+StdWriterPrefixLen {
for nr < frameSize+stdWriterPrefixLen {
var nr2 int
nr2, er = src.Read(buf[nr:])
nr += nr2
if er == io.EOF {
if nr < frameSize+StdWriterPrefixLen {
logrus.Debugf("Corrupted frame: %v", buf[StdWriterPrefixLen:nr])
if nr < frameSize+stdWriterPrefixLen {
logrus.Debugf("Corrupted frame: %v", buf[stdWriterPrefixLen:nr])
return written, nil
}
break
@ -148,7 +153,7 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
}
// Write the retrieved frame (without header)
nw, ew = out.Write(buf[StdWriterPrefixLen : frameSize+StdWriterPrefixLen])
nw, ew = out.Write(buf[stdWriterPrefixLen : frameSize+stdWriterPrefixLen])
if ew != nil {
logrus.Debugf("Error writing frame: %s", ew)
return 0, ew
@ -161,8 +166,8 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
written += int64(nw)
// Move the rest of the buffer to the beginning
copy(buf, buf[frameSize+StdWriterPrefixLen:])
copy(buf, buf[frameSize+stdWriterPrefixLen:])
// Move the index
nr -= frameSize + StdWriterPrefixLen
nr -= frameSize + stdWriterPrefixLen
}
}

View file

@ -5,5 +5,6 @@ import (
)
var (
// ErrNotSupportedPlatform means the platform is not supported.
ErrNotSupportedPlatform = errors.New("platform and architecture is not supported")
)

View file

@ -8,11 +8,6 @@ import (
"unsafe"
)
const (
EVENT_ALL_ACCESS = 0x1F0003
EVENT_MODIFY_STATUS = 0x0002
)
var (
procCreateEvent = modkernel32.NewProc("CreateEventW")
procOpenEvent = modkernel32.NewProc("OpenEventW")
@ -21,13 +16,14 @@ var (
procPulseEvent = modkernel32.NewProc("PulseEvent")
)
// CreateEvent implements win32 CreateEventW func in golang. It will create an event object.
func CreateEvent(eventAttributes *syscall.SecurityAttributes, manualReset bool, initialState bool, name string) (handle syscall.Handle, err error) {
namep, _ := syscall.UTF16PtrFromString(name)
var _p1 uint32 = 0
var _p1 uint32
if manualReset {
_p1 = 1
}
var _p2 uint32 = 0
var _p2 uint32
if initialState {
_p2 = 1
}
@ -40,9 +36,10 @@ func CreateEvent(eventAttributes *syscall.SecurityAttributes, manualReset bool,
return
}
// OpenEvent implements win32 OpenEventW func in golang. It opens an event object.
func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle syscall.Handle, err error) {
namep, _ := syscall.UTF16PtrFromString(name)
var _p1 uint32 = 0
var _p1 uint32
if inheritHandle {
_p1 = 1
}
@ -55,14 +52,17 @@ func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle sy
return
}
// SetEvent implements win32 SetEvent func in golang.
func SetEvent(handle syscall.Handle) (err error) {
return setResetPulse(handle, procSetEvent)
}
// ResetEvent implements win32 ResetEvent func in golang.
func ResetEvent(handle syscall.Handle) (err error) {
return setResetPulse(handle, procResetEvent)
}
// PulseEvent implements win32 PulseEvent func in golang.
func PulseEvent(handle syscall.Handle) (err error) {
return setResetPulse(handle, procPulseEvent)
}

View file

@ -6,6 +6,8 @@ import (
"os"
)
// MkdirAll creates a directory named path along with any necessary parents,
// with permission specified by attribute perm for all dir created.
func MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}

View file

@ -7,10 +7,10 @@ import (
)
// Lstat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
// a system.StatT type pertaining to that file.
//
// Throws an error if the file does not exist
func Lstat(path string) (*Stat_t, error) {
func Lstat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Lstat(path, s); err != nil {
return nil, err

View file

@ -6,21 +6,17 @@ import (
"os"
)
// Some explanation for my own sanity, and hopefully maintainers in the
// future.
//
// Lstat calls os.Lstat to get a fileinfo interface back.
// This is then copied into our own locally defined structure.
// Note the Linux version uses fromStatT to do the copy back,
// but that not strictly necessary when already in an OS specific module.
func Lstat(path string) (*Stat_t, error) {
func Lstat(path string) (*StatT, error) {
fi, err := os.Lstat(path)
if err != nil {
return nil, err
}
return &Stat_t{
return &StatT{
name: fi.Name(),
size: fi.Size(),
mode: fi.Mode(),

View file

@ -2,7 +2,6 @@ package system
import (
"bufio"
"errors"
"io"
"os"
"strconv"
@ -11,10 +10,6 @@ import (
"github.com/docker/docker/pkg/units"
)
var (
ErrMalformed = errors.New("malformed file")
)
// ReadMemInfo retrieves memory statistics of the host system and returns a
// MemInfo type.
func ReadMemInfo() (*MemInfo, error) {

View file

@ -2,6 +2,7 @@
package system
// ReadMemInfo is not supported on platforms other than linux and windows.
func ReadMemInfo() (*MemInfo, error) {
return nil, ErrNotSupportedPlatform
}

View file

@ -7,14 +7,16 @@ import (
)
// Mknod creates a filesystem node (file, device special file or named pipe) named path
// with attributes specified by mode and dev
// with attributes specified by mode and dev.
func Mknod(path string, mode uint32, dev int) error {
return syscall.Mknod(path, mode, dev)
}
// Mkdev is used to build the value of linux devices (in /dev/) which specifies major
// and minor number of the newly created device special file.
// 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
// 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))
}

View file

@ -2,10 +2,12 @@
package system
// Mknod is not implemented on Windows.
func Mknod(path string, mode uint32, dev int) error {
return ErrNotSupportedPlatform
}
// Mkdev is not implemented on Windows.
func Mkdev(major int64, minor int64) uint32 {
panic("Mkdev not implemented on Windows.")
}

View file

@ -6,9 +6,9 @@ import (
"syscall"
)
// Stat_t type contains status of a file. It contains metadata
// like permission, owner, group, size, etc about a file
type Stat_t struct {
// StatT type contains status of a file. It contains metadata
// like permission, owner, group, size, etc about a file.
type StatT struct {
mode uint32
uid uint32
gid uint32
@ -17,30 +17,37 @@ type Stat_t struct {
mtim syscall.Timespec
}
func (s Stat_t) Mode() uint32 {
// Mode returns file's permission mode.
func (s StatT) Mode() uint32 {
return s.mode
}
func (s Stat_t) Uid() uint32 {
// UID returns file's user id of owner.
func (s StatT) UID() uint32 {
return s.uid
}
func (s Stat_t) Gid() uint32 {
// Gid returns file's group id of owner.
func (s StatT) Gid() uint32 {
return s.gid
}
func (s Stat_t) Rdev() uint64 {
// Rdev returns file's device ID (if it's special file).
func (s StatT) Rdev() uint64 {
return s.rdev
}
func (s Stat_t) Size() int64 {
// Size returns file's size.
func (s StatT) Size() int64 {
return s.size
}
func (s Stat_t) Mtim() syscall.Timespec {
// Mtim returns file's last modification time.
func (s StatT) Mtim() syscall.Timespec {
return s.mtim
}
func (s Stat_t) GetLastModification() syscall.Timespec {
// GetLastModification returns file's last modification time.
func (s StatT) GetLastModification() syscall.Timespec {
return s.Mtim()
}

View file

@ -5,8 +5,8 @@ import (
)
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
return &Stat_t{size: s.Size,
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size,
mode: uint32(s.Mode),
uid: s.Uid,
gid: s.Gid,
@ -18,7 +18,7 @@ func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
// a system.Stat_t type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*Stat_t, error) {
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err

View file

@ -5,8 +5,8 @@ import (
)
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
return &Stat_t{size: s.Size,
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size,
mode: s.Mode,
uid: s.Uid,
gid: s.Gid,
@ -14,17 +14,17 @@ func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
mtim: s.Mtim}, nil
}
// FromStatT exists only on linux, and loads a system.Stat_t from a
// FromStatT exists only on linux, and loads a system.StatT from a
// syscal.Stat_t.
func FromStatT(s *syscall.Stat_t) (*Stat_t, error) {
func FromStatT(s *syscall.Stat_t) (*StatT, error) {
return fromStatT(s)
}
// Stat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
// a system.StatT type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*Stat_t, error) {
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err

View file

@ -22,7 +22,7 @@ func TestFromStatT(t *testing.T) {
if stat.Mode != s.Mode() {
t.Fatal("got invalid mode")
}
if stat.Uid != s.Uid() {
if stat.Uid != s.UID() {
t.Fatal("got invalid uid")
}
if stat.Gid != s.Gid() {

View file

@ -6,9 +6,9 @@ import (
"syscall"
)
// fromStatT creates a system.Stat_t type from a syscall.Stat_t type
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
return &Stat_t{size: s.Size,
// fromStatT creates a system.StatT type from a syscall.Stat_t type
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size,
mode: uint32(s.Mode),
uid: s.Uid,
gid: s.Gid,

View file

@ -7,7 +7,9 @@ import (
"time"
)
type Stat_t struct {
// StatT type contains status of a file. It contains metadata
// like name, permission, size, etc about a file.
type StatT struct {
name string
size int64
mode os.FileMode
@ -15,22 +17,27 @@ type Stat_t struct {
isDir bool
}
func (s Stat_t) Name() string {
// Name returns file's name.
func (s StatT) Name() string {
return s.name
}
func (s Stat_t) Size() int64 {
// Size returns file's size.
func (s StatT) Size() int64 {
return s.size
}
func (s Stat_t) Mode() os.FileMode {
// Mode returns file's permission mode.
func (s StatT) Mode() os.FileMode {
return s.mode
}
func (s Stat_t) ModTime() time.Time {
// ModTime returns file's last modification time.
func (s StatT) ModTime() time.Time {
return s.modTime
}
func (s Stat_t) IsDir() bool {
// IsDir returns whether file is actually a directory.
func (s StatT) IsDir() bool {
return s.isDir
}

View file

@ -6,6 +6,8 @@ import (
"syscall"
)
// Umask sets current process's file mode creation mask to newmask
// and return oldmask.
func Umask(newmask int) (oldmask int, err error) {
return syscall.Umask(newmask), nil
}

View file

@ -2,6 +2,7 @@
package system
// Umask is not supported on the windows platform.
func Umask(newmask int) (oldmask int, err error) {
// should not be called on cli code path
return 0, ErrNotSupportedPlatform

View file

@ -2,10 +2,13 @@ package system
import "syscall"
// LUtimesNano is not supported by darwin platform.
func LUtimesNano(path string, ts []syscall.Timespec) error {
return ErrNotSupportedPlatform
}
// UtimesNano is used to change access and modification time of path.
// it can't be used for symbol link file.
func UtimesNano(path string, ts []syscall.Timespec) error {
return syscall.UtimesNano(path, ts)
}

View file

@ -5,6 +5,8 @@ import (
"unsafe"
)
// LUtimesNano is used to change access and modification time of the specified path.
// It's used for symbol link file because syscall.UtimesNano doesn't support a NOFOLLOW flag atm.
func LUtimesNano(path string, ts []syscall.Timespec) error {
var _path *byte
_path, err := syscall.BytePtrFromString(path)
@ -19,6 +21,8 @@ func LUtimesNano(path string, ts []syscall.Timespec) error {
return nil
}
// UtimesNano is used to change access and modification time of the specified path.
// It can't be used for symbol link file.
func UtimesNano(path string, ts []syscall.Timespec) error {
return syscall.UtimesNano(path, ts)
}

View file

@ -5,10 +5,12 @@ import (
"unsafe"
)
// LUtimesNano is used to change access and modification time of the speficied path.
// It's used for symbol link file because syscall.UtimesNano doesn't support a NOFOLLOW flag atm.
func LUtimesNano(path string, ts []syscall.Timespec) error {
// These are not currently available in syscall
AT_FDCWD := -100
AT_SYMLINK_NOFOLLOW := 0x100
atFdCwd := -100
atSymLinkNoFollow := 0x100
var _path *byte
_path, err := syscall.BytePtrFromString(path)
@ -16,13 +18,15 @@ func LUtimesNano(path string, ts []syscall.Timespec) error {
return err
}
if _, _, err := syscall.Syscall6(syscall.SYS_UTIMENSAT, uintptr(AT_FDCWD), uintptr(unsafe.Pointer(_path)), uintptr(unsafe.Pointer(&ts[0])), uintptr(AT_SYMLINK_NOFOLLOW), 0, 0); err != 0 && err != syscall.ENOSYS {
if _, _, err := syscall.Syscall6(syscall.SYS_UTIMENSAT, uintptr(atFdCwd), uintptr(unsafe.Pointer(_path)), uintptr(unsafe.Pointer(&ts[0])), uintptr(atSymLinkNoFollow), 0, 0); err != 0 && err != syscall.ENOSYS {
return err
}
return nil
}
// UtimesNano is used to change access and modification time of the specified path.
// It can't be used for symbol link file.
func UtimesNano(path string, ts []syscall.Timespec) error {
return syscall.UtimesNano(path, ts)
}

View file

@ -4,10 +4,12 @@ package system
import "syscall"
// LUtimesNano is not supported on platforms other than linux, freebsd and darwin.
func LUtimesNano(path string, ts []syscall.Timespec) error {
return ErrNotSupportedPlatform
}
// UtimesNano is not supported on platforms other than linux, freebsd and darwin.
func UtimesNano(path string, ts []syscall.Timespec) error {
return ErrNotSupportedPlatform
}

View file

@ -5,7 +5,9 @@ import (
"unsafe"
)
// Returns a nil slice and nil error if the xattr is not set
// Lgetxattr retrieves the value of the extended attribute identified by attr
// and associated with the given path in the file system.
// It will returns a nil slice and nil error if the xattr is not set.
func Lgetxattr(path string, attr string) ([]byte, error) {
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
@ -36,6 +38,8 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
var _zero uintptr
// Lsetxattr sets the value of the extended attribute identified by attr
// and associated with the given path in the file system.
func Lsetxattr(path string, attr string, data []byte, flags int) error {
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {

View file

@ -2,10 +2,12 @@
package system
// Lgetxattr is not supported on platforms other than linux.
func Lgetxattr(path string, attr string) ([]byte, error) {
return nil, ErrNotSupportedPlatform
}
// Lsetxattr is not supported on platforms other than linux.
func Lsetxattr(path string, attr string, data []byte, flags int) error {
return ErrNotSupportedPlatform
}