Refactor node.go

This commit is contained in:
Florian Weingarten 2015-04-28 21:24:43 -04:00
parent c9422c3b32
commit 4bb724fac2
4 changed files with 113 additions and 175 deletions

View file

@ -30,16 +30,10 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
node.UID = stat.Uid
node.GID = stat.Gid
// TODO: cache uid lookup
if u, err := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
node.User = u.Username
}
// TODO: implement getgrnam() or use https://github.com/kless/osutil
// if g, nil := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
// node.User = u.Username
// }
node.Inode = stat.Ino
var err error
@ -49,7 +43,6 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
node.Size = uint64(stat.Size)
node.Links = uint64(stat.Nlink)
case "dir":
// nothing to do
case "symlink":
node.LinkTarget, err = os.Readlink(path)
case "dev":
@ -57,11 +50,9 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
case "chardev":
node.Device = stat.Rdev
case "fifo":
// nothing to do
case "socket":
// nothing to do
default:
panic(fmt.Sprintf("invalid node type %q", node.Type))
err = fmt.Errorf("invalid node type %q", node.Type)
}
return err
@ -80,27 +71,22 @@ func (node *Node) createFifoAt(path string) error {
}
func (node *Node) isNewer(path string, fi os.FileInfo) bool {
// if this node has a type other than "file", treat as if content has changed
if node.Type != "file" {
debug.Log("node.isNewer", "node %v is newer: not file", path)
return true
}
// if the name or type has changed, this is surely something different
tpe := nodeTypeFromFileInfo(path, fi)
tpe := nodeTypeFromFileInfo(fi)
if node.Name != fi.Name() || node.Type != tpe {
debug.Log("node.isNewer", "node %v is newer: name or type changed", path)
return true
}
// collect extended stat
stat := fi.Sys().(*syscall.Stat_t)
extendedStat := fi.Sys().(*syscall.Stat_t)
changeTime := time.Unix(extendedStat.Ctim.Unix())
inode := extendedStat.Ino
size := uint64(extendedStat.Size)
changeTime := time.Unix(stat.Ctim.Unix())
inode := stat.Ino
size := uint64(stat.Size)
// if timestamps or inodes differ, content has changed
if node.ModTime != fi.ModTime() ||
node.ChangeTime != changeTime ||
node.Inode != inode ||
@ -109,7 +95,6 @@ func (node *Node) isNewer(path string, fi os.FileInfo) bool {
return true
}
// otherwise the node is assumed to have the same content
debug.Log("node.isNewer", "node %v is not newer", path)
return false
}