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

198
node.go
View File

@ -66,7 +66,7 @@ func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
ModTime: fi.ModTime(),
}
node.Type = nodeTypeFromFileInfo(path, fi)
node.Type = nodeTypeFromFileInfo(fi)
if node.Type == "file" {
node.Size = uint64(fi.Size())
}
@ -75,7 +75,7 @@ func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
return node, err
}
func nodeTypeFromFileInfo(path string, fi os.FileInfo) string {
func nodeTypeFromFileInfo(fi os.FileInfo) string {
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
case 0:
return "file"
@ -96,19 +96,58 @@ func nodeTypeFromFileInfo(path string, fi os.FileInfo) string {
return ""
}
func CreateNodeAt(node *Node, m *Map, s *server.Server, path string) error {
func (node *Node) CreateAt(path string, m *Map, s *server.Server) error {
switch node.Type {
case "dir":
err := os.Mkdir(path, node.Mode)
if err != nil {
return arrar.Annotate(err, "Mkdir")
if err := node.createDirAt(path); err != nil {
return err
}
case "file":
if err := node.createFileAt(path, m, s); err != nil {
return err
}
case "symlink":
if err := node.createSymlinkAt(path); err != nil {
return err
}
case "dev":
if err := node.createDevAt(path); err != nil {
return arrar.Annotate(err, "Mknod")
}
case "chardev":
if err := node.createCharDevAt(path); err != nil {
return arrar.Annotate(err, "Mknod")
}
case "fifo":
if err := node.createFifoAt(path); err != nil {
return arrar.Annotate(err, "Mkfifo")
}
case "socket":
return nil
default:
return fmt.Errorf("filetype %q not implemented!\n", node.Type)
}
return node.restoreMetadata(path)
}
func (node Node) restoreMetadata(path string) error {
var err error
err = os.Lchown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Lchown")
}
if node.Type == "symlink" {
return nil
}
err = os.Chmod(path, node.Mode)
if err != nil {
return arrar.Annotate(err, "Chmod")
}
var utimes = []syscall.Timespec{
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
syscall.NsecToTimespec(node.ModTime.UnixNano()),
@ -117,10 +156,23 @@ func CreateNodeAt(node *Node, m *Map, s *server.Server, path string) error {
if err != nil {
return arrar.Annotate(err, "Utimesnano")
}
case "file":
// TODO: handle hard links
return nil
}
func (node Node) createDirAt(path string) error {
err := os.Mkdir(path, node.Mode)
if err != nil {
return arrar.Annotate(err, "Mkdir")
}
return nil
}
func (node Node) createFileAt(path string, m *Map, s *server.Server) error {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
if err != nil {
return arrar.Annotate(err, "OpenFile")
}
@ -142,109 +194,16 @@ func CreateNodeAt(node *Node, m *Map, s *server.Server, path string) error {
}
}
f.Close()
err = os.Lchown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Lchown")
return nil
}
var utimes = []syscall.Timespec{
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
syscall.NsecToTimespec(node.ModTime.UnixNano()),
}
err = syscall.UtimesNano(path, utimes)
if err != nil {
return arrar.Annotate(err, "Utimesnano")
}
case "symlink":
func (node Node) createSymlinkAt(path string) error {
err := os.Symlink(node.LinkTarget, path)
if err != nil {
return arrar.Annotate(err, "Symlink")
}
err = os.Lchown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Lchown")
}
// f, err := os.OpenFile(path, O_PATH|syscall.O_NOFOLLOW, 0600)
// defer f.Close()
// if err != nil {
// return arrar.Annotate(err, "OpenFile")
// }
// TODO: Get Futimes() working on older Linux kernels (fails with 3.2.0)
// var utimes = []syscall.Timeval{
// syscall.NsecToTimeval(node.AccessTime.UnixNano()),
// syscall.NsecToTimeval(node.ModTime.UnixNano()),
// }
// err = syscall.Futimes(int(f.Fd()), utimes)
// if err != nil {
// return arrar.Annotate(err, "Futimes")
// }
return nil
case "dev":
err := node.createDevAt(path)
if err != nil {
return arrar.Annotate(err, "Mknod")
}
case "chardev":
err := node.createCharDevAt(path)
if err != nil {
return arrar.Annotate(err, "Mknod")
}
case "fifo":
err := node.createFifoAt(path)
if err != nil {
return arrar.Annotate(err, "Mkfifo")
}
case "socket":
// nothing to do, we do not restore sockets
return nil
default:
return fmt.Errorf("filetype %q not implemented!\n", node.Type)
}
err := os.Chmod(path, node.Mode)
if err != nil {
return arrar.Annotate(err, "Chmod")
}
err = os.Chown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Chown")
}
err = os.Chtimes(path, node.AccessTime, node.ModTime)
if err != nil {
return arrar.Annotate(err, "Chtimes")
}
return nil
}
func (node Node) SameContent(olderNode *Node) bool {
// if this node has a type other than "file", treat as if content has changed
if node.Type != "file" {
return false
}
// if the name or type has changed, this is surely something different
if node.Name != olderNode.Name || node.Type != olderNode.Type {
return false
}
// if timestamps or inodes differ, content has changed
if node.ModTime != olderNode.ModTime ||
node.ChangeTime != olderNode.ChangeTime ||
node.Inode != olderNode.Inode {
return false
}
// otherwise the node is assumed to have the same content
return true
}
func (node Node) MarshalJSON() ([]byte, error) {
@ -270,7 +229,6 @@ func (node *Node) UnmarshalJSON(data []byte) error {
}
func (node Node) Equals(other Node) bool {
// TODO: add generatored code for this
if node.Name != other.Name {
return false
}
@ -316,11 +274,28 @@ func (node Node) Equals(other Node) bool {
if node.Device != other.Device {
return false
}
if node.Content != nil && other.Content == nil {
if !node.sameContent(other) {
return false
} else if node.Content == nil && other.Content != nil {
}
if !node.Subtree.Equal(other.Subtree) {
return false
} else if node.Content != nil && other.Content != nil {
}
if node.Error != other.Error {
return false
}
return true
}
func (node Node) sameContent(other Node) bool {
if node.Content == nil {
return other.Content == nil
}
if other.Content == nil {
return false
}
if len(node.Content) != len(other.Content) {
return false
}
@ -330,15 +305,6 @@ func (node Node) Equals(other Node) bool {
return false
}
}
}
if !node.Subtree.Equal(other.Subtree) {
return false
}
if node.Error != other.Error {
return false
}
return true
}

View File

@ -15,10 +15,10 @@ func (node *Node) OpenForReading() (*os.File, error) {
return os.Open(node.path)
}
func (node *Node) fillExtra(path string, fi os.FileInfo) (err error) {
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return
return nil
}
node.ChangeTime = time.Unix(stat.Ctimespec.Unix())
@ -26,24 +26,19 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) (err error) {
node.UID = stat.Uid
node.GID = stat.Gid
// TODO: cache uid lookup
if u, nil := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
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
switch node.Type {
case "file":
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":
@ -51,11 +46,9 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) (err error) {
case "chardev":
node.Device = uint64(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
@ -74,27 +67,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 false
}
// collect extended stat
stat := fi.Sys().(*syscall.Stat_t)
extendedStat := fi.Sys().(*syscall.Stat_t)
changeTime := time.Unix(extendedStat.Ctimespec.Unix())
inode := extendedStat.Ino
size := uint64(extendedStat.Size)
changeTime := time.Unix(stat.Ctimespec.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 ||
@ -103,7 +91,6 @@ func (node *Node) isNewer(path string, fi os.FileInfo) bool {
return false
}
// otherwise the node is assumed to have the same content
debug.Log("node.isNewer", "node %v is not newer", path)
return false
}

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
}

View File

@ -47,7 +47,7 @@ func (res *Restorer) to(dst string, dir string, treeBlob server.Blob) error {
if res.Filter == nil ||
res.Filter(filepath.Join(dir, node.Name), dstpath, node) {
err := CreateNodeAt(node, tree.Map, res.s, dstpath)
err := node.CreateAt(dstpath, tree.Map, res.s)
// Did it fail because of ENOENT?
if arrar.Check(err, func(err error) bool {
@ -60,7 +60,7 @@ func (res *Restorer) to(dst string, dir string, treeBlob server.Blob) error {
// Create parent directories and retry
err = os.MkdirAll(filepath.Dir(dstpath), 0700)
if err == nil || err == os.ErrExist {
err = CreateNodeAt(node, tree.Map, res.s, dstpath)
err = node.CreateAt(dstpath, tree.Map, res.s)
}
}