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

222
node.go
View File

@ -66,7 +66,7 @@ func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
ModTime: fi.ModTime(), ModTime: fi.ModTime(),
} }
node.Type = nodeTypeFromFileInfo(path, fi) node.Type = nodeTypeFromFileInfo(fi)
if node.Type == "file" { if node.Type == "file" {
node.Size = uint64(fi.Size()) node.Size = uint64(fi.Size())
} }
@ -75,7 +75,7 @@ func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
return node, err return node, err
} }
func nodeTypeFromFileInfo(path string, fi os.FileInfo) string { func nodeTypeFromFileInfo(fi os.FileInfo) string {
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) { switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
case 0: case 0:
return "file" return "file"
@ -96,155 +96,114 @@ func nodeTypeFromFileInfo(path string, fi os.FileInfo) string {
return "" 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 { switch node.Type {
case "dir": case "dir":
err := os.Mkdir(path, node.Mode) if err := node.createDirAt(path); err != nil {
if err != nil { return err
return arrar.Annotate(err, "Mkdir")
}
err = os.Lchown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Lchown")
}
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 "file": case "file":
// TODO: handle hard links if err := node.createFileAt(path, m, s); err != nil {
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600) return err
defer f.Close()
if err != nil {
return arrar.Annotate(err, "OpenFile")
}
for _, blobid := range node.Content {
blob, err := m.FindID(blobid)
if err != nil {
return arrar.Annotate(err, "Find Blob")
}
buf, err := s.Load(backend.Data, blob)
if err != nil {
return arrar.Annotate(err, "Load")
}
_, err = f.Write(buf)
if err != nil {
return arrar.Annotate(err, "Write")
}
}
f.Close()
err = os.Lchown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Lchown")
}
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": case "symlink":
err := os.Symlink(node.LinkTarget, path) if err := node.createSymlinkAt(path); err != nil {
if err != nil { return err
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": case "dev":
err := node.createDevAt(path) if err := node.createDevAt(path); err != nil {
if err != nil {
return arrar.Annotate(err, "Mknod") return arrar.Annotate(err, "Mknod")
} }
case "chardev": case "chardev":
err := node.createCharDevAt(path) if err := node.createCharDevAt(path); err != nil {
if err != nil {
return arrar.Annotate(err, "Mknod") return arrar.Annotate(err, "Mknod")
} }
case "fifo": case "fifo":
err := node.createFifoAt(path) if err := node.createFifoAt(path); err != nil {
if err != nil {
return arrar.Annotate(err, "Mkfifo") return arrar.Annotate(err, "Mkfifo")
} }
case "socket": case "socket":
// nothing to do, we do not restore sockets
return nil return nil
default: default:
return fmt.Errorf("filetype %q not implemented!\n", node.Type) return fmt.Errorf("filetype %q not implemented!\n", node.Type)
} }
err := os.Chmod(path, node.Mode) 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 { if err != nil {
return arrar.Annotate(err, "Chmod") return arrar.Annotate(err, "Chmod")
} }
err = os.Chown(path, int(node.UID), int(node.GID)) var utimes = []syscall.Timespec{
if err != nil { syscall.NsecToTimespec(node.AccessTime.UnixNano()),
return arrar.Annotate(err, "Chown") syscall.NsecToTimespec(node.ModTime.UnixNano()),
} }
err = syscall.UtimesNano(path, utimes)
err = os.Chtimes(path, node.AccessTime, node.ModTime)
if err != nil { if err != nil {
return arrar.Annotate(err, "Chtimes") return arrar.Annotate(err, "Utimesnano")
} }
return nil return nil
} }
func (node Node) SameContent(olderNode *Node) bool { func (node Node) createDirAt(path string) error {
// if this node has a type other than "file", treat as if content has changed err := os.Mkdir(path, node.Mode)
if node.Type != "file" { if err != nil {
return false return arrar.Annotate(err, "Mkdir")
} }
// if the name or type has changed, this is surely something different return nil
if node.Name != olderNode.Name || node.Type != olderNode.Type { }
return false
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")
} }
// if timestamps or inodes differ, content has changed for _, blobid := range node.Content {
if node.ModTime != olderNode.ModTime || blob, err := m.FindID(blobid)
node.ChangeTime != olderNode.ChangeTime || if err != nil {
node.Inode != olderNode.Inode { return arrar.Annotate(err, "Find Blob")
return false }
buf, err := s.Load(backend.Data, blob)
if err != nil {
return arrar.Annotate(err, "Load")
}
_, err = f.Write(buf)
if err != nil {
return arrar.Annotate(err, "Write")
}
} }
// otherwise the node is assumed to have the same content return nil
return true }
func (node Node) createSymlinkAt(path string) error {
err := os.Symlink(node.LinkTarget, path)
if err != nil {
return arrar.Annotate(err, "Symlink")
}
return nil
} }
func (node Node) MarshalJSON() ([]byte, error) { func (node Node) MarshalJSON() ([]byte, error) {
@ -270,7 +229,6 @@ func (node *Node) UnmarshalJSON(data []byte) error {
} }
func (node Node) Equals(other Node) bool { func (node Node) Equals(other Node) bool {
// TODO: add generatored code for this
if node.Name != other.Name { if node.Name != other.Name {
return false return false
} }
@ -316,29 +274,37 @@ func (node Node) Equals(other Node) bool {
if node.Device != other.Device { if node.Device != other.Device {
return false return false
} }
if node.Content != nil && other.Content == nil { if !node.sameContent(other) {
return false return false
} else if node.Content == nil && other.Content != nil {
return false
} else if node.Content != nil && other.Content != nil {
if len(node.Content) != len(other.Content) {
return false
}
for i := 0; i < len(node.Content); i++ {
if !node.Content[i].Equal(other.Content[i]) {
return false
}
}
} }
if !node.Subtree.Equal(other.Subtree) { if !node.Subtree.Equal(other.Subtree) {
return false return false
} }
if node.Error != other.Error { if node.Error != other.Error {
return false return false
} }
return true 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
}
for i := 0; i < len(node.Content); i++ {
if !node.Content[i].Equal(other.Content[i]) {
return false
}
}
return true
}

View File

@ -15,10 +15,10 @@ func (node *Node) OpenForReading() (*os.File, error) {
return os.Open(node.path) 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) stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok { if !ok {
return return nil
} }
node.ChangeTime = time.Unix(stat.Ctimespec.Unix()) 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.UID = stat.Uid
node.GID = stat.Gid node.GID = stat.Gid
// TODO: cache uid lookup if u, err := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
if u, nil := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
node.User = u.Username 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 node.Inode = stat.Ino
var err error
switch node.Type { switch node.Type {
case "file": case "file":
node.Size = uint64(stat.Size) node.Size = uint64(stat.Size)
node.Links = uint64(stat.Nlink) node.Links = uint64(stat.Nlink)
case "dir": case "dir":
// nothing to do
case "symlink": case "symlink":
node.LinkTarget, err = os.Readlink(path) node.LinkTarget, err = os.Readlink(path)
case "dev": case "dev":
@ -51,11 +46,9 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) (err error) {
case "chardev": case "chardev":
node.Device = uint64(stat.Rdev) node.Device = uint64(stat.Rdev)
case "fifo": case "fifo":
// nothing to do
case "socket": case "socket":
// nothing to do
default: default:
panic(fmt.Sprintf("invalid node type %q", node.Type)) err = fmt.Errorf("invalid node type %q", node.Type)
} }
return err return err
@ -74,27 +67,22 @@ func (node *Node) createFifoAt(path string) error {
} }
func (node *Node) isNewer(path string, fi os.FileInfo) bool { 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" { if node.Type != "file" {
debug.Log("node.isNewer", "node %v is newer: not file", path) debug.Log("node.isNewer", "node %v is newer: not file", path)
return true return true
} }
// if the name or type has changed, this is surely something different tpe := nodeTypeFromFileInfo(fi)
tpe := nodeTypeFromFileInfo(path, fi)
if node.Name != fi.Name() || node.Type != tpe { if node.Name != fi.Name() || node.Type != tpe {
debug.Log("node.isNewer", "node %v is newer: name or type changed", path) debug.Log("node.isNewer", "node %v is newer: name or type changed", path)
return false return false
} }
// collect extended stat extendedStat := fi.Sys().(*syscall.Stat_t)
stat := 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() || if node.ModTime != fi.ModTime() ||
node.ChangeTime != changeTime || node.ChangeTime != changeTime ||
node.Inode != inode || node.Inode != inode ||
@ -103,7 +91,6 @@ func (node *Node) isNewer(path string, fi os.FileInfo) bool {
return false return false
} }
// otherwise the node is assumed to have the same content
debug.Log("node.isNewer", "node %v is not newer", path) debug.Log("node.isNewer", "node %v is not newer", path)
return false return false
} }

View File

@ -30,16 +30,10 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
node.UID = stat.Uid node.UID = stat.Uid
node.GID = stat.Gid node.GID = stat.Gid
// TODO: cache uid lookup
if u, err := 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 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 node.Inode = stat.Ino
var err error var err error
@ -49,7 +43,6 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
node.Size = uint64(stat.Size) node.Size = uint64(stat.Size)
node.Links = uint64(stat.Nlink) node.Links = uint64(stat.Nlink)
case "dir": case "dir":
// nothing to do
case "symlink": case "symlink":
node.LinkTarget, err = os.Readlink(path) node.LinkTarget, err = os.Readlink(path)
case "dev": case "dev":
@ -57,11 +50,9 @@ func (node *Node) fillExtra(path string, fi os.FileInfo) error {
case "chardev": case "chardev":
node.Device = stat.Rdev node.Device = stat.Rdev
case "fifo": case "fifo":
// nothing to do
case "socket": case "socket":
// nothing to do
default: default:
panic(fmt.Sprintf("invalid node type %q", node.Type)) err = fmt.Errorf("invalid node type %q", node.Type)
} }
return err return err
@ -80,27 +71,22 @@ func (node *Node) createFifoAt(path string) error {
} }
func (node *Node) isNewer(path string, fi os.FileInfo) bool { 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" { if node.Type != "file" {
debug.Log("node.isNewer", "node %v is newer: not file", path) debug.Log("node.isNewer", "node %v is newer: not file", path)
return true return true
} }
// if the name or type has changed, this is surely something different tpe := nodeTypeFromFileInfo(fi)
tpe := nodeTypeFromFileInfo(path, fi)
if node.Name != fi.Name() || node.Type != tpe { if node.Name != fi.Name() || node.Type != tpe {
debug.Log("node.isNewer", "node %v is newer: name or type changed", path) debug.Log("node.isNewer", "node %v is newer: name or type changed", path)
return true return true
} }
// collect extended stat extendedStat := fi.Sys().(*syscall.Stat_t)
stat := 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() || if node.ModTime != fi.ModTime() ||
node.ChangeTime != changeTime || node.ChangeTime != changeTime ||
node.Inode != inode || node.Inode != inode ||
@ -109,7 +95,6 @@ func (node *Node) isNewer(path string, fi os.FileInfo) bool {
return true return true
} }
// otherwise the node is assumed to have the same content
debug.Log("node.isNewer", "node %v is not newer", path) debug.Log("node.isNewer", "node %v is not newer", path)
return false return false
} }

View File

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