Browse code

Tidy pkg\system *stat* functions

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2017/04/06 07:35:43
Showing 13 changed files
... ...
@@ -9,16 +9,16 @@ import (
9 9
 func statDifferent(oldStat *system.StatT, newStat *system.StatT) bool {
10 10
 
11 11
 	// Don't look at size for dirs, its not a good measure of change
12
-	if oldStat.ModTime() != newStat.ModTime() ||
12
+	if oldStat.Mtim() != newStat.Mtim() ||
13 13
 		oldStat.Mode() != newStat.Mode() ||
14
-		oldStat.Size() != newStat.Size() && !oldStat.IsDir() {
14
+		oldStat.Size() != newStat.Size() && !oldStat.Mode().IsDir() {
15 15
 		return true
16 16
 	}
17 17
 	return false
18 18
 }
19 19
 
20 20
 func (info *FileInfo) isDir() bool {
21
-	return info.parent == nil || info.stat.IsDir()
21
+	return info.parent == nil || info.stat.Mode().IsDir()
22 22
 }
23 23
 
24 24
 func getIno(fi os.FileInfo) (inode uint64) {
25 25
deleted file mode 100644
... ...
@@ -1,19 +0,0 @@
1
-// +build !windows
2
-
3
-package system
4
-
5
-import (
6
-	"syscall"
7
-)
8
-
9
-// Lstat takes a path to a file and returns
10
-// a system.StatT type pertaining to that file.
11
-//
12
-// Throws an error if the file does not exist
13
-func Lstat(path string) (*StatT, error) {
14
-	s := &syscall.Stat_t{}
15
-	if err := syscall.Lstat(path, s); err != nil {
16
-		return nil, err
17
-	}
18
-	return fromStatT(s)
19
-}
20 1
new file mode 100644
... ...
@@ -0,0 +1,17 @@
0
+// +build !windows
1
+
2
+package system
3
+
4
+import "syscall"
5
+
6
+// Lstat takes a path to a file and returns
7
+// a system.StatT type pertaining to that file.
8
+//
9
+// Throws an error if the file does not exist
10
+func Lstat(path string) (*StatT, error) {
11
+	s := &syscall.Stat_t{}
12
+	if err := syscall.Lstat(path, s); err != nil {
13
+		return nil, err
14
+	}
15
+	return fromStatT(s)
16
+}
... ...
@@ -1,25 +1,14 @@
1
-// +build windows
2
-
3 1
 package system
4 2
 
5
-import (
6
-	"os"
7
-)
3
+import "os"
8 4
 
9 5
 // Lstat calls os.Lstat to get a fileinfo interface back.
10 6
 // This is then copied into our own locally defined structure.
11
-// Note the Linux version uses fromStatT to do the copy back,
12
-// but that not strictly necessary when already in an OS specific module.
13 7
 func Lstat(path string) (*StatT, error) {
14 8
 	fi, err := os.Lstat(path)
15 9
 	if err != nil {
16 10
 		return nil, err
17 11
 	}
18 12
 
19
-	return &StatT{
20
-		name:    fi.Name(),
21
-		size:    fi.Size(),
22
-		mode:    fi.Mode(),
23
-		modTime: fi.ModTime(),
24
-		isDir:   fi.IsDir()}, nil
13
+	return fromStatT(&fi)
25 14
 }
26 15
deleted file mode 100644
... ...
@@ -1,53 +0,0 @@
1
-// +build !windows
2
-
3
-package system
4
-
5
-import (
6
-	"syscall"
7
-)
8
-
9
-// StatT type contains status of a file. It contains metadata
10
-// like permission, owner, group, size, etc about a file.
11
-type StatT struct {
12
-	mode uint32
13
-	uid  uint32
14
-	gid  uint32
15
-	rdev uint64
16
-	size int64
17
-	mtim syscall.Timespec
18
-}
19
-
20
-// Mode returns file's permission mode.
21
-func (s StatT) Mode() uint32 {
22
-	return s.mode
23
-}
24
-
25
-// UID returns file's user id of owner.
26
-func (s StatT) UID() uint32 {
27
-	return s.uid
28
-}
29
-
30
-// GID returns file's group id of owner.
31
-func (s StatT) GID() uint32 {
32
-	return s.gid
33
-}
34
-
35
-// Rdev returns file's device ID (if it's special file).
36
-func (s StatT) Rdev() uint64 {
37
-	return s.rdev
38
-}
39
-
40
-// Size returns file's size.
41
-func (s StatT) Size() int64 {
42
-	return s.size
43
-}
44
-
45
-// Mtim returns file's last modification time.
46
-func (s StatT) Mtim() syscall.Timespec {
47
-	return s.mtim
48
-}
49
-
50
-// GetLastModification returns file's last modification time.
51
-func (s StatT) GetLastModification() syscall.Timespec {
52
-	return s.Mtim()
53
-}
... ...
@@ -1,10 +1,8 @@
1 1
 package system
2 2
 
3
-import (
4
-	"syscall"
5
-)
3
+import "syscall"
6 4
 
7
-// fromStatT creates a system.StatT type from a syscall.Stat_t type
5
+// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
8 6
 func fromStatT(s *syscall.Stat_t) (*StatT, error) {
9 7
 	return &StatT{size: s.Size,
10 8
 		mode: uint32(s.Mode),
... ...
@@ -13,20 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
13 13
 		rdev: uint64(s.Rdev),
14 14
 		mtim: s.Mtimespec}, nil
15 15
 }
16
-
17
-// FromStatT loads a system.StatT from a syscall.Stat_t.
18
-func FromStatT(s *syscall.Stat_t) (*StatT, error) {
19
-	return fromStatT(s)
20
-}
21
-
22
-// Stat takes a path to a file and returns
23
-// a system.StatT type pertaining to that file.
24
-//
25
-// Throws an error if the file does not exist
26
-func Stat(path string) (*StatT, error) {
27
-	s := &syscall.Stat_t{}
28
-	if err := syscall.Stat(path, s); err != nil {
29
-		return nil, err
30
-	}
31
-	return fromStatT(s)
32
-}
... ...
@@ -1,8 +1,6 @@
1 1
 package system
2 2
 
3
-import (
4
-	"syscall"
5
-)
3
+import "syscall"
6 4
 
7 5
 // fromStatT converts a syscall.Stat_t type to a system.Stat_t type
8 6
 func fromStatT(s *syscall.Stat_t) (*StatT, error) {
... ...
@@ -13,15 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
13 13
 		rdev: uint64(s.Rdev),
14 14
 		mtim: s.Mtimespec}, nil
15 15
 }
16
-
17
-// Stat takes a path to a file and returns
18
-// a system.Stat_t type pertaining to that file.
19
-//
20
-// Throws an error if the file does not exist
21
-func Stat(path string) (*StatT, error) {
22
-	s := &syscall.Stat_t{}
23
-	if err := syscall.Stat(path, s); err != nil {
24
-		return nil, err
25
-	}
26
-	return fromStatT(s)
27
-}
... ...
@@ -1,33 +1,19 @@
1 1
 package system
2 2
 
3
-import (
4
-	"syscall"
5
-)
3
+import "syscall"
6 4
 
7 5
 // fromStatT converts a syscall.Stat_t type to a system.Stat_t type
8 6
 func fromStatT(s *syscall.Stat_t) (*StatT, error) {
9 7
 	return &StatT{size: s.Size,
10
-		mode: s.Mode,
8
+		mode: uint32(s.Mode),
11 9
 		uid:  s.Uid,
12 10
 		gid:  s.Gid,
13
-		rdev: s.Rdev,
11
+		rdev: uint64(s.Rdev),
14 12
 		mtim: s.Mtim}, nil
15 13
 }
16 14
 
17
-// FromStatT exists only on linux, and loads a system.StatT from a
18
-// syscal.Stat_t.
15
+// FromStatT converts a syscall.Stat_t type to a system.Stat_t type
16
+// This is exposed on Linux as pkg/archive/changes uses it.
19 17
 func FromStatT(s *syscall.Stat_t) (*StatT, error) {
20 18
 	return fromStatT(s)
21 19
 }
22
-
23
-// Stat takes a path to a file and returns
24
-// a system.StatT type pertaining to that file.
25
-//
26
-// Throws an error if the file does not exist
27
-func Stat(path string) (*StatT, error) {
28
-	s := &syscall.Stat_t{}
29
-	if err := syscall.Stat(path, s); err != nil {
30
-		return nil, err
31
-	}
32
-	return fromStatT(s)
33
-}
... ...
@@ -1,10 +1,8 @@
1 1
 package system
2 2
 
3
-import (
4
-	"syscall"
5
-)
3
+import "syscall"
6 4
 
7
-// fromStatT creates a system.StatT type from a syscall.Stat_t type
5
+// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
8 6
 func fromStatT(s *syscall.Stat_t) (*StatT, error) {
9 7
 	return &StatT{size: s.Size,
10 8
 		mode: uint32(s.Mode),
... ...
@@ -13,15 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
13 13
 		rdev: uint64(s.Rdev),
14 14
 		mtim: s.Mtim}, nil
15 15
 }
16
-
17
-// Stat takes a path to a file and returns
18
-// a system.Stat_t type pertaining to that file.
19
-//
20
-// Throws an error if the file does not exist
21
-func Stat(path string) (*StatT, error) {
22
-	s := &syscall.Stat_t{}
23
-	if err := syscall.Stat(path, s); err != nil {
24
-		return nil, err
25
-	}
26
-	return fromStatT(s)
27
-}
... ...
@@ -1,12 +1,8 @@
1
-// +build solaris
2
-
3 1
 package system
4 2
 
5
-import (
6
-	"syscall"
7
-)
3
+import "syscall"
8 4
 
9
-// fromStatT creates a system.StatT type from a syscall.Stat_t type
5
+// fromStatT converts a syscall.Stat_t type to a system.Stat_t type
10 6
 func fromStatT(s *syscall.Stat_t) (*StatT, error) {
11 7
 	return &StatT{size: s.Size,
12 8
 		mode: uint32(s.Mode),
... ...
@@ -15,20 +11,3 @@ func fromStatT(s *syscall.Stat_t) (*StatT, error) {
15 15
 		rdev: uint64(s.Rdev),
16 16
 		mtim: s.Mtim}, nil
17 17
 }
18
-
19
-// FromStatT loads a system.StatT from a syscal.Stat_t.
20
-func FromStatT(s *syscall.Stat_t) (*StatT, error) {
21
-	return fromStatT(s)
22
-}
23
-
24
-// Stat takes a path to a file and returns
25
-// a system.StatT type pertaining to that file.
26
-//
27
-// Throws an error if the file does not exist
28
-func Stat(path string) (*StatT, error) {
29
-	s := &syscall.Stat_t{}
30
-	if err := syscall.Stat(path, s); err != nil {
31
-		return nil, err
32
-	}
33
-	return fromStatT(s)
34
-}
35 18
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+// +build !windows
1
+
2
+package system
3
+
4
+import "syscall"
5
+
6
+// StatT type contains status of a file. It contains metadata
7
+// like permission, owner, group, size, etc about a file.
8
+type StatT struct {
9
+	mode uint32
10
+	uid  uint32
11
+	gid  uint32
12
+	rdev uint64
13
+	size int64
14
+	mtim syscall.Timespec
15
+}
16
+
17
+// Mode returns file's permission mode.
18
+func (s StatT) Mode() uint32 {
19
+	return s.mode
20
+}
21
+
22
+// UID returns file's user id of owner.
23
+func (s StatT) UID() uint32 {
24
+	return s.uid
25
+}
26
+
27
+// GID returns file's group id of owner.
28
+func (s StatT) GID() uint32 {
29
+	return s.gid
30
+}
31
+
32
+// Rdev returns file's device ID (if it's special file).
33
+func (s StatT) Rdev() uint64 {
34
+	return s.rdev
35
+}
36
+
37
+// Size returns file's size.
38
+func (s StatT) Size() int64 {
39
+	return s.size
40
+}
41
+
42
+// Mtim returns file's last modification time.
43
+func (s StatT) Mtim() syscall.Timespec {
44
+	return s.mtim
45
+}
46
+
47
+// Stat takes a path to a file and returns
48
+// a system.StatT type pertaining to that file.
49
+//
50
+// Throws an error if the file does not exist
51
+func Stat(path string) (*StatT, error) {
52
+	s := &syscall.Stat_t{}
53
+	if err := syscall.Stat(path, s); err != nil {
54
+		return nil, err
55
+	}
56
+	return fromStatT(s)
57
+}
0 58
deleted file mode 100644
... ...
@@ -1,17 +0,0 @@
1
-// +build !linux,!windows,!freebsd,!solaris,!openbsd,!darwin
2
-
3
-package system
4
-
5
-import (
6
-	"syscall"
7
-)
8
-
9
-// fromStatT creates a system.StatT type from a syscall.Stat_t type
10
-func fromStatT(s *syscall.Stat_t) (*StatT, error) {
11
-	return &StatT{size: s.Size,
12
-		mode: uint32(s.Mode),
13
-		uid:  s.Uid,
14
-		gid:  s.Gid,
15
-		rdev: uint64(s.Rdev),
16
-		mtim: s.Mtimespec}, nil
17
-}
... ...
@@ -1,5 +1,3 @@
1
-// +build windows
2
-
3 1
 package system
4 2
 
5 3
 import (
... ...
@@ -8,18 +6,11 @@ import (
8 8
 )
9 9
 
10 10
 // StatT type contains status of a file. It contains metadata
11
-// like name, permission, size, etc about a file.
11
+// like permission, size, etc about a file.
12 12
 type StatT struct {
13
-	name    string
14
-	size    int64
15
-	mode    os.FileMode
16
-	modTime time.Time
17
-	isDir   bool
18
-}
19
-
20
-// Name returns file's name.
21
-func (s StatT) Name() string {
22
-	return s.name
13
+	mode os.FileMode
14
+	size int64
15
+	mtim time.Time
23 16
 }
24 17
 
25 18
 // Size returns file's size.
... ...
@@ -29,15 +20,30 @@ func (s StatT) Size() int64 {
29 29
 
30 30
 // Mode returns file's permission mode.
31 31
 func (s StatT) Mode() os.FileMode {
32
-	return s.mode
32
+	return os.FileMode(s.mode)
33
+}
34
+
35
+// Mtim returns file's last modification time.
36
+func (s StatT) Mtim() time.Time {
37
+	return time.Time(s.mtim)
33 38
 }
34 39
 
35
-// ModTime returns file's last modification time.
36
-func (s StatT) ModTime() time.Time {
37
-	return s.modTime
40
+// Stat takes a path to a file and returns
41
+// a system.StatT type pertaining to that file.
42
+//
43
+// Throws an error if the file does not exist
44
+func Stat(path string) (*StatT, error) {
45
+	fi, err := os.Stat(path)
46
+	if err != nil {
47
+		return nil, err
48
+	}
49
+	return fromStatT(&fi)
38 50
 }
39 51
 
40
-// IsDir returns whether file is actually a directory.
41
-func (s StatT) IsDir() bool {
42
-	return s.isDir
52
+// fromStatT converts a os.FileInfo type to a system.StatT type
53
+func fromStatT(fi *os.FileInfo) (*StatT, error) {
54
+	return &StatT{
55
+		size: (*fi).Size(),
56
+		mode: (*fi).Mode(),
57
+		mtim: (*fi).ModTime()}, nil
43 58
 }