Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dev, Nlink, and Ino to pkg/system/Stat struct #916

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/system/lstat_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ func Lstat(path string) (*StatT, error) {
return nil, err
}

return fromStatT(&fi)
return FileInfoToStatT(fi)
}
13 changes: 8 additions & 5 deletions pkg/system/stat_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import "syscall"
// fromStatT converts a syscall.Stat_t type to a system.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
mode: uint32(s.Mode),
uid: s.Uid,
gid: s.Gid,
rdev: uint64(s.Rdev),
dev: uint64(s.Dev),
ino: s.Ino,
nlink: uint64(s.Nlink),
mtim: s.Mtimespec}, nil
}
13 changes: 8 additions & 5 deletions pkg/system/stat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import "syscall"
// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
func fromStatT(s *syscall.Stat_t) (*StatT, error) {
return &StatT{size: s.Size,
mode: s.Mode,
uid: s.Uid,
gid: s.Gid,
rdev: uint64(s.Rdev),
mtim: s.Mtim}, nil
mode: s.Mode,
uid: s.Uid,
gid: s.Gid,
rdev: uint64(s.Rdev),
dev: uint64(s.Dev),
ino: uint64(s.Ino),
nlink: uint64(s.Nlink),
mtim: s.Mtim}, nil
}

// FromStatT converts a syscall.Stat_t type to a system.Stat_t type
Expand Down
35 changes: 29 additions & 6 deletions pkg/system/stat_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import (
// 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
rdev uint64
size int64
mtim syscall.Timespec
mode uint32
uid uint32
gid uint32
rdev uint64
size int64
mtim syscall.Timespec
dev uint64
ino uint64
nlink uint64
}

// Mode returns file's permission mode.
Expand Down Expand Up @@ -72,3 +75,23 @@ func Fstat(fd int) (*StatT, error) {
}
return fromStatT(s)
}

// Dev returns device identifier the file is stored on
func (s StatT) Dev() uint64 {
return s.dev
}

// Ino returns inode the file is stored on
func (s StatT) Ino() uint64 {
return s.ino
}

// Nlink returns number of hard links to the file
func (s StatT) Nlink() uint64 {
return s.nlink
}

// StatT converts a syscall.Stat_t type to a system.Stat_t type
func FileInfoToStatT(fi os.FileInfo) (*StatT, error) {
return fromStatT(fi.Sys().(*syscall.Stat_t))
}
9 changes: 9 additions & 0 deletions pkg/system/stat_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ func TestFromStatT(t *testing.T) {
if stat.Mtim != s.Mtim() {
t.Fatal("got invalid mtim")
}
if stat.Dev != s.Dev() {
t.Fatal("got invalid dev")
}
if stat.Nlink != s.Nlink() {
t.Fatal("got invalid nlink")
}
if stat.Ino != s.Ino() {
t.Fatal("got invalid inode")
}
}
36 changes: 27 additions & 9 deletions pkg/system/stat_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@ import (
// StatT type contains status of a file. It contains metadata
// like permission, size, etc about a file.
type StatT struct {
mode os.FileMode
size int64
mtim time.Time
mode os.FileMode
size int64
mtim time.Time
dev uint64
ino uint64
nlink uint64
}

// Dev returns device identifier the file is stored on
func (s StatT) Dev() uint64 {
return s.dev
}

// Ino returns inode the file is stored on
func (s StatT) Ino() uint64 {
return s.ino
}

// Nlink returns number of hard links to the file
func (s StatT) Nlink() uint64 {
return s.nlink
}

// Size returns file's size.
Expand Down Expand Up @@ -51,13 +69,13 @@ func Stat(path string) (*StatT, error) {
if err != nil {
return nil, err
}
return fromStatT(&fi)
return FileInfoToStatT(fi)
}

// fromStatT converts a os.FileInfo type to a system.StatT type
func fromStatT(fi *os.FileInfo) (*StatT, error) {
// FileInfoToStatT converts a os.FileInfo type to a system.StatT type
func FileInfoToStatT(fi os.FileInfo) (*StatT, error) {
return &StatT{
size: (*fi).Size(),
mode: (*fi).Mode(),
mtim: (*fi).ModTime()}, nil
size: fi.Size(),
mode: fi.Mode(),
mtim: fi.ModTime()}, nil
}