2014-11-13 15:36:05 -05:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
2015-05-07 18:14:11 -04:00
|
|
|
"os"
|
|
|
|
"time"
|
2014-11-13 15:36:05 -05:00
|
|
|
)
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// StatT type contains status of a file. It contains metadata
|
2017-04-05 18:35:43 -04:00
|
|
|
// like permission, size, etc about a file.
|
2015-07-28 12:13:12 -04:00
|
|
|
type StatT struct {
|
2017-04-05 18:35:43 -04:00
|
|
|
mode os.FileMode
|
|
|
|
size int64
|
|
|
|
mtim time.Time
|
2015-05-07 18:14:11 -04:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// Size returns file's size.
|
|
|
|
func (s StatT) Size() int64 {
|
2015-05-07 18:14:11 -04:00
|
|
|
return s.size
|
|
|
|
}
|
|
|
|
|
2015-07-28 12:13:12 -04:00
|
|
|
// Mode returns file's permission mode.
|
|
|
|
func (s StatT) Mode() os.FileMode {
|
2017-04-05 18:35:43 -04:00
|
|
|
return os.FileMode(s.mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mtim returns file's last modification time.
|
|
|
|
func (s StatT) Mtim() time.Time {
|
|
|
|
return time.Time(s.mtim)
|
2015-05-07 18:14:11 -04:00
|
|
|
}
|
|
|
|
|
2017-04-05 18:35:43 -04:00
|
|
|
// 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) {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fromStatT(&fi)
|
2015-05-07 18:14:11 -04:00
|
|
|
}
|
|
|
|
|
2017-04-05 18:35:43 -04:00
|
|
|
// 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
|
2015-03-11 11:42:49 -04:00
|
|
|
}
|