Merge pull request #15125 from WeiZhang555/golint-stdcopy-system
fix golint warnings/errors on pkg/system and pkg/stdcopy
This commit is contained in:
commit
94a39da482
27 changed files with 126 additions and 86 deletions
|
@ -173,7 +173,7 @@ func Changes(layers []string, rw string) ([]Change, error) {
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
parent *FileInfo
|
parent *FileInfo
|
||||||
name string
|
name string
|
||||||
stat *system.Stat_t
|
stat *system.StatT
|
||||||
children map[string]*FileInfo
|
children map[string]*FileInfo
|
||||||
capability []byte
|
capability []byte
|
||||||
added bool
|
added bool
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"github.com/docker/docker/pkg/system"
|
"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
|
// Don't look at size for dirs, its not a good measure of change
|
||||||
if oldStat.Mode() != newStat.Mode() ||
|
if oldStat.Mode() != newStat.Mode() ||
|
||||||
oldStat.Uid() != newStat.Uid() ||
|
oldStat.UID() != newStat.UID() ||
|
||||||
oldStat.Gid() != newStat.Gid() ||
|
oldStat.Gid() != newStat.Gid() ||
|
||||||
oldStat.Rdev() != newStat.Rdev() ||
|
oldStat.Rdev() != newStat.Rdev() ||
|
||||||
// Don't look at size for dirs, its not a good measure of change
|
// Don't look at size for dirs, its not a good measure of change
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"github.com/docker/docker/pkg/system"
|
"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
|
// Don't look at size for dirs, its not a good measure of change
|
||||||
if oldStat.ModTime() != newStat.ModTime() ||
|
if oldStat.ModTime() != newStat.ModTime() ||
|
||||||
|
|
|
@ -9,19 +9,24 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
StdWriterPrefixLen = 8
|
stdWriterPrefixLen = 8
|
||||||
StdWriterFdIndex = 0
|
stdWriterFdIndex = 0
|
||||||
StdWriterSizeIndex = 4
|
stdWriterSizeIndex = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
type StdType [StdWriterPrefixLen]byte
|
// StdType prefixes type and length to standard stream.
|
||||||
|
type StdType [stdWriterPrefixLen]byte
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Stdin StdType = StdType{0: 0}
|
// Stdin represents standard input stream type.
|
||||||
Stdout StdType = StdType{0: 1}
|
Stdin = StdType{0: 0}
|
||||||
Stderr StdType = StdType{0: 2}
|
// 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 {
|
type StdWriter struct {
|
||||||
io.Writer
|
io.Writer
|
||||||
prefix StdType
|
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)))
|
binary.BigEndian.PutUint32(w.prefix[4:], uint32(len(buf)))
|
||||||
n1, err = w.Writer.Write(w.prefix[:])
|
n1, err = w.Writer.Write(w.prefix[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
n = n1 - StdWriterPrefixLen
|
n = n1 - stdWriterPrefixLen
|
||||||
} else {
|
} else {
|
||||||
n2, err = w.Writer.Write(buf)
|
n2, err = w.Writer.Write(buf)
|
||||||
n = n1 + n2 - StdWriterPrefixLen
|
n = n1 + n2 - stdWriterPrefixLen
|
||||||
}
|
}
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
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.
|
// 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`.
|
// `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) {
|
func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error) {
|
||||||
var (
|
var (
|
||||||
buf = make([]byte, 32*1024+StdWriterPrefixLen+1)
|
buf = make([]byte, 32*1024+stdWriterPrefixLen+1)
|
||||||
bufLen = len(buf)
|
bufLen = len(buf)
|
||||||
nr, nw int
|
nr, nw int
|
||||||
er, ew error
|
er, ew error
|
||||||
|
@ -85,12 +90,12 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// Make sure we have at least a full header
|
// Make sure we have at least a full header
|
||||||
for nr < StdWriterPrefixLen {
|
for nr < stdWriterPrefixLen {
|
||||||
var nr2 int
|
var nr2 int
|
||||||
nr2, er = src.Read(buf[nr:])
|
nr2, er = src.Read(buf[nr:])
|
||||||
nr += nr2
|
nr += nr2
|
||||||
if er == io.EOF {
|
if er == io.EOF {
|
||||||
if nr < StdWriterPrefixLen {
|
if nr < stdWriterPrefixLen {
|
||||||
logrus.Debugf("Corrupted prefix: %v", buf[:nr])
|
logrus.Debugf("Corrupted prefix: %v", buf[:nr])
|
||||||
return written, nil
|
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
|
// Check the first byte to know where to write
|
||||||
switch buf[StdWriterFdIndex] {
|
switch buf[stdWriterFdIndex] {
|
||||||
case 0:
|
case 0:
|
||||||
fallthrough
|
fallthrough
|
||||||
case 1:
|
case 1:
|
||||||
|
@ -113,30 +118,30 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
|
||||||
// Write on stderr
|
// Write on stderr
|
||||||
out = dsterr
|
out = dsterr
|
||||||
default:
|
default:
|
||||||
logrus.Debugf("Error selecting output fd: (%d)", buf[StdWriterFdIndex])
|
logrus.Debugf("Error selecting output fd: (%d)", buf[stdWriterFdIndex])
|
||||||
return 0, ErrInvalidStdHeader
|
return 0, errInvalidStdHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retrieve the size of the frame
|
// 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)
|
logrus.Debugf("framesize: %d", frameSize)
|
||||||
|
|
||||||
// Check if the buffer is big enough to read the frame.
|
// Check if the buffer is big enough to read the frame.
|
||||||
// Extend it if necessary.
|
// Extend it if necessary.
|
||||||
if frameSize+StdWriterPrefixLen > bufLen {
|
if frameSize+stdWriterPrefixLen > bufLen {
|
||||||
logrus.Debugf("Extending buffer cap by %d (was %d)", frameSize+StdWriterPrefixLen-bufLen+1, len(buf))
|
logrus.Debugf("Extending buffer cap by %d (was %d)", frameSize+stdWriterPrefixLen-bufLen+1, len(buf))
|
||||||
buf = append(buf, make([]byte, frameSize+StdWriterPrefixLen-bufLen+1)...)
|
buf = append(buf, make([]byte, frameSize+stdWriterPrefixLen-bufLen+1)...)
|
||||||
bufLen = len(buf)
|
bufLen = len(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// While the amount of bytes read is less than the size of the frame + header, we keep reading
|
// 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
|
var nr2 int
|
||||||
nr2, er = src.Read(buf[nr:])
|
nr2, er = src.Read(buf[nr:])
|
||||||
nr += nr2
|
nr += nr2
|
||||||
if er == io.EOF {
|
if er == io.EOF {
|
||||||
if nr < frameSize+StdWriterPrefixLen {
|
if nr < frameSize+stdWriterPrefixLen {
|
||||||
logrus.Debugf("Corrupted frame: %v", buf[StdWriterPrefixLen:nr])
|
logrus.Debugf("Corrupted frame: %v", buf[stdWriterPrefixLen:nr])
|
||||||
return written, nil
|
return written, nil
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@ -148,7 +153,7 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write the retrieved frame (without header)
|
// 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 {
|
if ew != nil {
|
||||||
logrus.Debugf("Error writing frame: %s", ew)
|
logrus.Debugf("Error writing frame: %s", ew)
|
||||||
return 0, ew
|
return 0, ew
|
||||||
|
@ -161,8 +166,8 @@ func StdCopy(dstout, dsterr io.Writer, src io.Reader) (written int64, err error)
|
||||||
written += int64(nw)
|
written += int64(nw)
|
||||||
|
|
||||||
// Move the rest of the buffer to the beginning
|
// Move the rest of the buffer to the beginning
|
||||||
copy(buf, buf[frameSize+StdWriterPrefixLen:])
|
copy(buf, buf[frameSize+stdWriterPrefixLen:])
|
||||||
// Move the index
|
// Move the index
|
||||||
nr -= frameSize + StdWriterPrefixLen
|
nr -= frameSize + stdWriterPrefixLen
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,5 +5,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// ErrNotSupportedPlatform means the platform is not supported.
|
||||||
ErrNotSupportedPlatform = errors.New("platform and architecture is not supported")
|
ErrNotSupportedPlatform = errors.New("platform and architecture is not supported")
|
||||||
)
|
)
|
||||||
|
|
|
@ -8,11 +8,6 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
EVENT_ALL_ACCESS = 0x1F0003
|
|
||||||
EVENT_MODIFY_STATUS = 0x0002
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
procCreateEvent = modkernel32.NewProc("CreateEventW")
|
procCreateEvent = modkernel32.NewProc("CreateEventW")
|
||||||
procOpenEvent = modkernel32.NewProc("OpenEventW")
|
procOpenEvent = modkernel32.NewProc("OpenEventW")
|
||||||
|
@ -21,13 +16,14 @@ var (
|
||||||
procPulseEvent = modkernel32.NewProc("PulseEvent")
|
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) {
|
func CreateEvent(eventAttributes *syscall.SecurityAttributes, manualReset bool, initialState bool, name string) (handle syscall.Handle, err error) {
|
||||||
namep, _ := syscall.UTF16PtrFromString(name)
|
namep, _ := syscall.UTF16PtrFromString(name)
|
||||||
var _p1 uint32 = 0
|
var _p1 uint32
|
||||||
if manualReset {
|
if manualReset {
|
||||||
_p1 = 1
|
_p1 = 1
|
||||||
}
|
}
|
||||||
var _p2 uint32 = 0
|
var _p2 uint32
|
||||||
if initialState {
|
if initialState {
|
||||||
_p2 = 1
|
_p2 = 1
|
||||||
}
|
}
|
||||||
|
@ -40,9 +36,10 @@ func CreateEvent(eventAttributes *syscall.SecurityAttributes, manualReset bool,
|
||||||
return
|
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) {
|
func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle syscall.Handle, err error) {
|
||||||
namep, _ := syscall.UTF16PtrFromString(name)
|
namep, _ := syscall.UTF16PtrFromString(name)
|
||||||
var _p1 uint32 = 0
|
var _p1 uint32
|
||||||
if inheritHandle {
|
if inheritHandle {
|
||||||
_p1 = 1
|
_p1 = 1
|
||||||
}
|
}
|
||||||
|
@ -55,14 +52,17 @@ func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle sy
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetEvent implements win32 SetEvent func in golang.
|
||||||
func SetEvent(handle syscall.Handle) (err error) {
|
func SetEvent(handle syscall.Handle) (err error) {
|
||||||
return setResetPulse(handle, procSetEvent)
|
return setResetPulse(handle, procSetEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResetEvent implements win32 ResetEvent func in golang.
|
||||||
func ResetEvent(handle syscall.Handle) (err error) {
|
func ResetEvent(handle syscall.Handle) (err error) {
|
||||||
return setResetPulse(handle, procResetEvent)
|
return setResetPulse(handle, procResetEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PulseEvent implements win32 PulseEvent func in golang.
|
||||||
func PulseEvent(handle syscall.Handle) (err error) {
|
func PulseEvent(handle syscall.Handle) (err error) {
|
||||||
return setResetPulse(handle, procPulseEvent)
|
return setResetPulse(handle, procPulseEvent)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"os"
|
"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 {
|
func MkdirAll(path string, perm os.FileMode) error {
|
||||||
return os.MkdirAll(path, perm)
|
return os.MkdirAll(path, perm)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Lstat takes a path to a file and returns
|
// 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
|
// 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{}
|
s := &syscall.Stat_t{}
|
||||||
if err := syscall.Lstat(path, s); err != nil {
|
if err := syscall.Lstat(path, s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -6,21 +6,17 @@ import (
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Some explanation for my own sanity, and hopefully maintainers in the
|
|
||||||
// future.
|
|
||||||
//
|
|
||||||
// Lstat calls os.Lstat to get a fileinfo interface back.
|
// Lstat calls os.Lstat to get a fileinfo interface back.
|
||||||
// This is then copied into our own locally defined structure.
|
// This is then copied into our own locally defined structure.
|
||||||
// Note the Linux version uses fromStatT to do the copy back,
|
// Note the Linux version uses fromStatT to do the copy back,
|
||||||
// but that not strictly necessary when already in an OS specific module.
|
// but that not strictly necessary when already in an OS specific module.
|
||||||
|
func Lstat(path string) (*StatT, error) {
|
||||||
func Lstat(path string) (*Stat_t, error) {
|
|
||||||
fi, err := os.Lstat(path)
|
fi, err := os.Lstat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Stat_t{
|
return &StatT{
|
||||||
name: fi.Name(),
|
name: fi.Name(),
|
||||||
size: fi.Size(),
|
size: fi.Size(),
|
||||||
mode: fi.Mode(),
|
mode: fi.Mode(),
|
||||||
|
|
|
@ -2,7 +2,6 @@ package system
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -11,10 +10,6 @@ import (
|
||||||
"github.com/docker/docker/pkg/units"
|
"github.com/docker/docker/pkg/units"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
ErrMalformed = errors.New("malformed file")
|
|
||||||
)
|
|
||||||
|
|
||||||
// ReadMemInfo retrieves memory statistics of the host system and returns a
|
// ReadMemInfo retrieves memory statistics of the host system and returns a
|
||||||
// MemInfo type.
|
// MemInfo type.
|
||||||
func ReadMemInfo() (*MemInfo, error) {
|
func ReadMemInfo() (*MemInfo, error) {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
package system
|
package system
|
||||||
|
|
||||||
|
// ReadMemInfo is not supported on platforms other than linux and windows.
|
||||||
func ReadMemInfo() (*MemInfo, error) {
|
func ReadMemInfo() (*MemInfo, error) {
|
||||||
return nil, ErrNotSupportedPlatform
|
return nil, ErrNotSupportedPlatform
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Mknod creates a filesystem node (file, device special file or named pipe) named path
|
// 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 {
|
func Mknod(path string, mode uint32, dev int) error {
|
||||||
return syscall.Mknod(path, mode, dev)
|
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.
|
// 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,
|
// 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 {
|
func Mkdev(major int64, minor int64) uint32 {
|
||||||
return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
|
return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
package system
|
package system
|
||||||
|
|
||||||
|
// Mknod is not implemented on Windows.
|
||||||
func Mknod(path string, mode uint32, dev int) error {
|
func Mknod(path string, mode uint32, dev int) error {
|
||||||
return ErrNotSupportedPlatform
|
return ErrNotSupportedPlatform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mkdev is not implemented on Windows.
|
||||||
func Mkdev(major int64, minor int64) uint32 {
|
func Mkdev(major int64, minor int64) uint32 {
|
||||||
panic("Mkdev not implemented on Windows.")
|
panic("Mkdev not implemented on Windows.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Stat_t type contains status of a file. It contains metadata
|
// StatT type contains status of a file. It contains metadata
|
||||||
// like permission, owner, group, size, etc about a file
|
// like permission, owner, group, size, etc about a file.
|
||||||
type Stat_t struct {
|
type StatT struct {
|
||||||
mode uint32
|
mode uint32
|
||||||
uid uint32
|
uid uint32
|
||||||
gid uint32
|
gid uint32
|
||||||
|
@ -17,30 +17,37 @@ type Stat_t struct {
|
||||||
mtim syscall.Timespec
|
mtim syscall.Timespec
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Stat_t) Mode() uint32 {
|
// Mode returns file's permission mode.
|
||||||
|
func (s StatT) Mode() uint32 {
|
||||||
return s.mode
|
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
|
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
|
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
|
return s.rdev
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Stat_t) Size() int64 {
|
// Size returns file's size.
|
||||||
|
func (s StatT) Size() int64 {
|
||||||
return s.size
|
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
|
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()
|
return s.Mtim()
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
|
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
|
||||||
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
|
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
|
||||||
return &Stat_t{size: s.Size,
|
return &StatT{size: s.Size,
|
||||||
mode: uint32(s.Mode),
|
mode: uint32(s.Mode),
|
||||||
uid: s.Uid,
|
uid: s.Uid,
|
||||||
gid: s.Gid,
|
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.
|
// a system.Stat_t type pertaining to that file.
|
||||||
//
|
//
|
||||||
// Throws an error if the file does not exist
|
// 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{}
|
s := &syscall.Stat_t{}
|
||||||
if err := syscall.Stat(path, s); err != nil {
|
if err := syscall.Stat(path, s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
|
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
|
||||||
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
|
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
|
||||||
return &Stat_t{size: s.Size,
|
return &StatT{size: s.Size,
|
||||||
mode: s.Mode,
|
mode: s.Mode,
|
||||||
uid: s.Uid,
|
uid: s.Uid,
|
||||||
gid: s.Gid,
|
gid: s.Gid,
|
||||||
|
@ -14,17 +14,17 @@ func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
|
||||||
mtim: s.Mtim}, nil
|
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.
|
// syscal.Stat_t.
|
||||||
func FromStatT(s *syscall.Stat_t) (*Stat_t, error) {
|
func FromStatT(s *syscall.Stat_t) (*StatT, error) {
|
||||||
return fromStatT(s)
|
return fromStatT(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stat takes a path to a file and returns
|
// 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
|
// 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{}
|
s := &syscall.Stat_t{}
|
||||||
if err := syscall.Stat(path, s); err != nil {
|
if err := syscall.Stat(path, s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -22,7 +22,7 @@ func TestFromStatT(t *testing.T) {
|
||||||
if stat.Mode != s.Mode() {
|
if stat.Mode != s.Mode() {
|
||||||
t.Fatal("got invalid mode")
|
t.Fatal("got invalid mode")
|
||||||
}
|
}
|
||||||
if stat.Uid != s.Uid() {
|
if stat.Uid != s.UID() {
|
||||||
t.Fatal("got invalid uid")
|
t.Fatal("got invalid uid")
|
||||||
}
|
}
|
||||||
if stat.Gid != s.Gid() {
|
if stat.Gid != s.Gid() {
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fromStatT creates a system.Stat_t type from a syscall.Stat_t type
|
// fromStatT creates a system.StatT type from a syscall.Stat_t type
|
||||||
func fromStatT(s *syscall.Stat_t) (*Stat_t, error) {
|
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
|
||||||
return &Stat_t{size: s.Size,
|
return &StatT{size: s.Size,
|
||||||
mode: uint32(s.Mode),
|
mode: uint32(s.Mode),
|
||||||
uid: s.Uid,
|
uid: s.Uid,
|
||||||
gid: s.Gid,
|
gid: s.Gid,
|
||||||
|
|
|
@ -7,7 +7,9 @@ import (
|
||||||
"time"
|
"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
|
name string
|
||||||
size int64
|
size int64
|
||||||
mode os.FileMode
|
mode os.FileMode
|
||||||
|
@ -15,22 +17,27 @@ type Stat_t struct {
|
||||||
isDir bool
|
isDir bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Stat_t) Name() string {
|
// Name returns file's name.
|
||||||
|
func (s StatT) Name() string {
|
||||||
return s.name
|
return s.name
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s Stat_t) Size() int64 {
|
// Size returns file's size.
|
||||||
|
func (s StatT) Size() int64 {
|
||||||
return s.size
|
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
|
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
|
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
|
return s.isDir
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Umask sets current process's file mode creation mask to newmask
|
||||||
|
// and return oldmask.
|
||||||
func Umask(newmask int) (oldmask int, err error) {
|
func Umask(newmask int) (oldmask int, err error) {
|
||||||
return syscall.Umask(newmask), nil
|
return syscall.Umask(newmask), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
package system
|
package system
|
||||||
|
|
||||||
|
// Umask is not supported on the windows platform.
|
||||||
func Umask(newmask int) (oldmask int, err error) {
|
func Umask(newmask int) (oldmask int, err error) {
|
||||||
// should not be called on cli code path
|
// should not be called on cli code path
|
||||||
return 0, ErrNotSupportedPlatform
|
return 0, ErrNotSupportedPlatform
|
||||||
|
|
|
@ -2,10 +2,13 @@ package system
|
||||||
|
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
|
// LUtimesNano is not supported by darwin platform.
|
||||||
func LUtimesNano(path string, ts []syscall.Timespec) error {
|
func LUtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return ErrNotSupportedPlatform
|
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 {
|
func UtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return syscall.UtimesNano(path, ts)
|
return syscall.UtimesNano(path, ts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"unsafe"
|
"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 {
|
func LUtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
var _path *byte
|
var _path *byte
|
||||||
_path, err := syscall.BytePtrFromString(path)
|
_path, err := syscall.BytePtrFromString(path)
|
||||||
|
@ -19,6 +21,8 @@ func LUtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return nil
|
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 {
|
func UtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return syscall.UtimesNano(path, ts)
|
return syscall.UtimesNano(path, ts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,12 @@ import (
|
||||||
"unsafe"
|
"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 {
|
func LUtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
// These are not currently available in syscall
|
// These are not currently available in syscall
|
||||||
AT_FDCWD := -100
|
atFdCwd := -100
|
||||||
AT_SYMLINK_NOFOLLOW := 0x100
|
atSymLinkNoFollow := 0x100
|
||||||
|
|
||||||
var _path *byte
|
var _path *byte
|
||||||
_path, err := syscall.BytePtrFromString(path)
|
_path, err := syscall.BytePtrFromString(path)
|
||||||
|
@ -16,13 +18,15 @@ func LUtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return err
|
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 err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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 {
|
func UtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return syscall.UtimesNano(path, ts)
|
return syscall.UtimesNano(path, ts)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,12 @@ package system
|
||||||
|
|
||||||
import "syscall"
|
import "syscall"
|
||||||
|
|
||||||
|
// LUtimesNano is not supported on platforms other than linux, freebsd and darwin.
|
||||||
func LUtimesNano(path string, ts []syscall.Timespec) error {
|
func LUtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return ErrNotSupportedPlatform
|
return ErrNotSupportedPlatform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UtimesNano is not supported on platforms other than linux, freebsd and darwin.
|
||||||
func UtimesNano(path string, ts []syscall.Timespec) error {
|
func UtimesNano(path string, ts []syscall.Timespec) error {
|
||||||
return ErrNotSupportedPlatform
|
return ErrNotSupportedPlatform
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,9 @@ import (
|
||||||
"unsafe"
|
"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) {
|
func Lgetxattr(path string, attr string) ([]byte, error) {
|
||||||
pathBytes, err := syscall.BytePtrFromString(path)
|
pathBytes, err := syscall.BytePtrFromString(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -36,6 +38,8 @@ func Lgetxattr(path string, attr string) ([]byte, error) {
|
||||||
|
|
||||||
var _zero uintptr
|
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 {
|
func Lsetxattr(path string, attr string, data []byte, flags int) error {
|
||||||
pathBytes, err := syscall.BytePtrFromString(path)
|
pathBytes, err := syscall.BytePtrFromString(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
package system
|
package system
|
||||||
|
|
||||||
|
// Lgetxattr is not supported on platforms other than linux.
|
||||||
func Lgetxattr(path string, attr string) ([]byte, error) {
|
func Lgetxattr(path string, attr string) ([]byte, error) {
|
||||||
return nil, ErrNotSupportedPlatform
|
return nil, ErrNotSupportedPlatform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lsetxattr is not supported on platforms other than linux.
|
||||||
func Lsetxattr(path string, attr string, data []byte, flags int) error {
|
func Lsetxattr(path string, attr string, data []byte, flags int) error {
|
||||||
return ErrNotSupportedPlatform
|
return ErrNotSupportedPlatform
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue