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

Merge pull request #32387 from Microsoft/jjh/tidystat

Tidy pkg\system *stat* functions
This commit is contained in:
Brian Goff 2017-04-06 08:40:27 -04:00 committed by GitHub
commit fa3e2d5ab9
11 changed files with 55 additions and 156 deletions

View file

@ -9,16 +9,16 @@ import (
func statDifferent(oldStat *system.StatT, newStat *system.StatT) 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.Mtim() != newStat.Mtim() ||
oldStat.Mode() != newStat.Mode() || oldStat.Mode() != newStat.Mode() ||
oldStat.Size() != newStat.Size() && !oldStat.IsDir() { oldStat.Size() != newStat.Size() && !oldStat.Mode().IsDir() {
return true return true
} }
return false return false
} }
func (info *FileInfo) isDir() bool { func (info *FileInfo) isDir() bool {
return info.parent == nil || info.stat.IsDir() return info.parent == nil || info.stat.Mode().IsDir()
} }
func getIno(fi os.FileInfo) (inode uint64) { func getIno(fi os.FileInfo) (inode uint64) {

View file

@ -2,9 +2,7 @@
package system package system
import ( import "syscall"
"syscall"
)
// Lstat takes a path to a file and returns // Lstat takes a path to a file and returns
// a system.StatT type pertaining to that file. // a system.StatT type pertaining to that file.

View file

@ -1,25 +1,14 @@
// +build windows
package system package system
import ( import "os"
"os"
)
// 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,
// but that not strictly necessary when already in an OS specific module.
func Lstat(path string) (*StatT, error) { func Lstat(path string) (*StatT, error) {
fi, err := os.Lstat(path) fi, err := os.Lstat(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &StatT{ return fromStatT(&fi)
name: fi.Name(),
size: fi.Size(),
mode: fi.Mode(),
modTime: fi.ModTime(),
isDir: fi.IsDir()}, nil
} }

View file

@ -1,10 +1,8 @@
package system package system
import ( import "syscall"
"syscall"
)
// fromStatT creates a system.StatT type from a syscall.Stat_t type // fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*StatT, error) { func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size, return &StatT{size: s.Size,
mode: uint32(s.Mode), mode: uint32(s.Mode),
@ -13,20 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
rdev: uint64(s.Rdev), rdev: uint64(s.Rdev),
mtim: s.Mtimespec}, nil mtim: s.Mtimespec}, nil
} }
// FromStatT loads a system.StatT from a syscall.Stat_t.
func FromStatT(s *syscall.Stat_t) (*StatT, error) {
return fromStatT(s)
}
// Stat takes a path to a file and returns
// a system.StatT type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
}

View file

@ -1,8 +1,6 @@
package system package system
import ( import "syscall"
"syscall"
)
// 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) (*StatT, error) { func fromStatT(s *syscall.Stat_t) (*StatT, error) {
@ -13,15 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
rdev: uint64(s.Rdev), rdev: uint64(s.Rdev),
mtim: s.Mtimespec}, nil mtim: s.Mtimespec}, nil
} }
// Stat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
}

View file

@ -1,33 +1,19 @@
package system package system
import ( import "syscall"
"syscall"
)
// 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) (*StatT, error) { func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size, return &StatT{size: s.Size,
mode: s.Mode, mode: uint32(s.Mode),
uid: s.Uid, uid: s.Uid,
gid: s.Gid, gid: s.Gid,
rdev: s.Rdev, rdev: uint64(s.Rdev),
mtim: s.Mtim}, nil mtim: s.Mtim}, nil
} }
// FromStatT exists only on linux, and loads a system.StatT from a // FromStatT converts a syscall.Stat_t type to a system.Stat_t type
// syscal.Stat_t. // This is exposed on Linux as pkg/archive/changes uses it.
func FromStatT(s *syscall.Stat_t) (*StatT, error) { func FromStatT(s *syscall.Stat_t) (*StatT, error) {
return fromStatT(s) return fromStatT(s)
} }
// Stat takes a path to a file and returns
// a system.StatT type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
}

View file

@ -1,10 +1,8 @@
package system package system
import ( import "syscall"
"syscall"
)
// fromStatT creates a system.StatT type from a syscall.Stat_t type // fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*StatT, error) { func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size, return &StatT{size: s.Size,
mode: uint32(s.Mode), mode: uint32(s.Mode),
@ -13,15 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
rdev: uint64(s.Rdev), rdev: uint64(s.Rdev),
mtim: s.Mtim}, nil mtim: s.Mtim}, nil
} }
// Stat takes a path to a file and returns
// a system.Stat_t type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
}

View file

@ -1,12 +1,8 @@
// +build solaris
package system package system
import ( import "syscall"
"syscall"
)
// fromStatT creates a system.StatT type from a syscall.Stat_t type // fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*StatT, error) { func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size, return &StatT{size: s.Size,
mode: uint32(s.Mode), mode: uint32(s.Mode),
@ -15,20 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
rdev: uint64(s.Rdev), rdev: uint64(s.Rdev),
mtim: s.Mtim}, nil mtim: s.Mtim}, nil
} }
// FromStatT loads a system.StatT from a syscal.Stat_t.
func FromStatT(s *syscall.Stat_t) (*StatT, error) {
return fromStatT(s)
}
// Stat takes a path to a file and returns
// a system.StatT type pertaining to that file.
//
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
}

View file

@ -2,9 +2,7 @@
package system package system
import ( import "syscall"
"syscall"
)
// StatT 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.
@ -47,7 +45,14 @@ func (s StatT) Mtim() syscall.Timespec {
return s.mtim return s.mtim
} }
// GetLastModification returns file's last modification time. // Stat takes a path to a file and returns
func (s StatT) GetLastModification() syscall.Timespec { // a system.StatT type pertaining to that file.
return s.Mtim() //
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
s := &syscall.Stat_t{}
if err := syscall.Stat(path, s); err != nil {
return nil, err
}
return fromStatT(s)
} }

View file

@ -1,17 +0,0 @@
// +build !linux,!windows,!freebsd,!solaris,!openbsd,!darwin
package system
import (
"syscall"
)
// 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,
rdev: uint64(s.Rdev),
mtim: s.Mtimespec}, nil
}

View file

@ -1,5 +1,3 @@
// +build windows
package system package system
import ( import (
@ -8,18 +6,11 @@ import (
) )
// StatT type contains status of a file. It contains metadata // StatT type contains status of a file. It contains metadata
// like name, permission, size, etc about a file. // like permission, size, etc about a file.
type StatT struct { type StatT struct {
name string
size int64
mode os.FileMode mode os.FileMode
modTime time.Time size int64
isDir bool mtim time.Time
}
// Name returns file's name.
func (s StatT) Name() string {
return s.name
} }
// Size returns file's size. // Size returns file's size.
@ -29,15 +20,30 @@ func (s StatT) Size() int64 {
// Mode returns file's permission mode. // Mode returns file's permission mode.
func (s StatT) Mode() os.FileMode { func (s StatT) Mode() os.FileMode {
return s.mode return os.FileMode(s.mode)
} }
// ModTime returns file's last modification time. // Mtim returns file's last modification time.
func (s StatT) ModTime() time.Time { func (s StatT) Mtim() time.Time {
return s.modTime return time.Time(s.mtim)
} }
// IsDir returns whether file is actually a directory. // Stat takes a path to a file and returns
func (s StatT) IsDir() bool { // a system.StatT type pertaining to that file.
return s.isDir //
// Throws an error if the file does not exist
func Stat(path string) (*StatT, error) {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
return fromStatT(&fi)
}
// fromStatT converts a os.FileInfo type to a system.StatT type
func fromStatT(fi *os.FileInfo) (*StatT, error) {
return &StatT{
size: (*fi).Size(),
mode: (*fi).Mode(),
mtim: (*fi).ModTime()}, nil
} }