2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

Merge remote-tracking branch 'fw42/refactor_node'

This commit is contained in:
Alexander Neumann 2015-04-29 23:11:00 +02:00
commit 66b81a2a8b
4 changed files with 186 additions and 300 deletions

298
node.go
View File

@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"os"
"os/user"
"strconv"
"syscall"
"time"
"github.com/juju/arrar"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug"
"github.com/restic/restic/server"
)
@ -66,7 +68,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 +77,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,155 +98,114 @@ 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")
}
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")
if err := node.createDirAt(path); err != nil {
return err
}
case "file":
// TODO: handle hard links
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
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")
if err := node.createFileAt(path, m, s); err != nil {
return err
}
case "symlink":
err := os.Symlink(node.LinkTarget, path)
if err != nil {
return arrar.Annotate(err, "Symlink")
if err := node.createSymlinkAt(path); err != nil {
return err
}
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 {
if err := node.createDevAt(path); err != nil {
return arrar.Annotate(err, "Mknod")
}
case "chardev":
err := node.createCharDevAt(path)
if err != nil {
if err := node.createCharDevAt(path); err != nil {
return arrar.Annotate(err, "Mknod")
}
case "fifo":
err := node.createFifoAt(path)
if err != nil {
if err := node.createFifoAt(path); 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)
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")
}
err = os.Chown(path, int(node.UID), int(node.GID))
if err != nil {
return arrar.Annotate(err, "Chown")
var utimes = []syscall.Timespec{
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
syscall.NsecToTimespec(node.ModTime.UnixNano()),
}
err = os.Chtimes(path, node.AccessTime, node.ModTime)
err = syscall.UtimesNano(path, utimes)
if err != nil {
return arrar.Annotate(err, "Chtimes")
return arrar.Annotate(err, "Utimesnano")
}
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
func (node Node) createDirAt(path string) error {
err := os.Mkdir(path, node.Mode)
if err != nil {
return arrar.Annotate(err, "Mkdir")
}
// if the name or type has changed, this is surely something different
if node.Name != olderNode.Name || node.Type != olderNode.Type {
return false
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")
}
// if timestamps or inodes differ, content has changed
if node.ModTime != olderNode.ModTime ||
node.ChangeTime != olderNode.ChangeTime ||
node.Inode != olderNode.Inode {
return false
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")
}
}
// otherwise the node is assumed to have the same content
return true
return nil
}
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) {
@ -270,7 +231,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,29 +276,111 @@ 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 {
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) {
return false
}
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
}
for i := 0; i < len(node.Content); i++ {
if !node.Content[i].Equal(other.Content[i]) {
return false
}
}
return true
}
func (node *Node) isNewer(path string, fi os.FileInfo) bool {
if node.Type != "file" {
debug.Log("node.isNewer", "node %v is newer: not file", path)
return true
}
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
}
extendedStat := fi.Sys().(*syscall.Stat_t)
inode := extendedStat.Ino
size := uint64(extendedStat.Size)
if node.ModTime != fi.ModTime() ||
node.ChangeTime != changeTime(extendedStat) ||
node.Inode != inode ||
node.Size != size {
debug.Log("node.isNewer", "node %v is newer: timestamp, size or inode changed", path)
return true
}
debug.Log("node.isNewer", "node %v is not newer", path)
return false
}
func (node *Node) fillUser(stat *syscall.Stat_t) error {
node.UID = stat.Uid
node.GID = stat.Gid
u, err := user.LookupId(strconv.Itoa(int(stat.Uid)))
if err != nil {
return err
}
node.User = u.Username
return nil
}
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return nil
}
node.Inode = stat.Ino
node.fillUser(stat)
node.fillTimes(stat)
var err error
switch node.Type {
case "file":
node.Size = uint64(stat.Size)
node.Links = uint64(stat.Nlink)
case "dir":
case "symlink":
node.LinkTarget, err = os.Readlink(path)
case "dev":
node.Device = uint64(stat.Rdev)
case "chardev":
node.Device = uint64(stat.Rdev)
case "fifo":
case "socket":
default:
err = fmt.Errorf("invalid node type %q", node.Type)
}
return err
}

View File

@ -1,66 +1,15 @@
package restic
import (
"fmt"
"os"
"os/user"
"strconv"
"syscall"
"time"
"github.com/restic/restic/debug"
)
func (node *Node) OpenForReading() (*os.File, error) {
return os.Open(node.path)
}
func (node *Node) fillExtra(path string, fi os.FileInfo) (err error) {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return
}
node.ChangeTime = time.Unix(stat.Ctimespec.Unix())
node.AccessTime = time.Unix(stat.Atimespec.Unix())
node.UID = stat.Uid
node.GID = stat.Gid
// TODO: cache uid lookup
if u, nil := 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
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":
node.Device = uint64(stat.Rdev)
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))
}
return err
}
func (node *Node) createDevAt(path string) error {
return syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device))
}
@ -73,37 +22,11 @@ func (node *Node) createFifoAt(path string) error {
return syscall.Mkfifo(path, 0600)
}
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)
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)
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 ||
node.Size != size {
debug.Log("node.isNewer", "node %v is newer: timestamp or inode changed", path)
return false
}
// otherwise the node is assumed to have the same content
debug.Log("node.isNewer", "node %v is not newer", path)
return false
func changeTime(stat *syscall.Stat_t) time.Time {
return time.Unix(stat.Ctimespec.Unix())
}
func (node *Node) fillTimes(stat *syscall.Stat_t) {
node.ChangeTime = time.Unix(stat.Ctimespec.Unix())
node.AccessTime = time.Unix(stat.Atimespec.Unix())
}

View File

@ -1,14 +1,9 @@
package restic
import (
"fmt"
"os"
"os/user"
"strconv"
"syscall"
"time"
"github.com/restic/restic/debug"
)
func (node *Node) OpenForReading() (*os.File, error) {
@ -19,54 +14,6 @@ func (node *Node) OpenForReading() (*os.File, error) {
return file, err
}
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
stat, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return nil
}
node.ChangeTime = time.Unix(stat.Ctim.Unix())
node.AccessTime = time.Unix(stat.Atim.Unix())
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
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":
node.Device = stat.Rdev
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))
}
return err
}
func (node *Node) createDevAt(path string) error {
return syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device))
}
@ -79,37 +26,11 @@ func (node *Node) createFifoAt(path string) error {
return syscall.Mkfifo(path, 0600)
}
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)
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)
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 ||
node.Size != size {
debug.Log("node.isNewer", "node %v is newer: timestamp, size or inode changed", path)
return true
}
// otherwise the node is assumed to have the same content
debug.Log("node.isNewer", "node %v is not newer", path)
return false
func (node *Node) fillTimes(stat *syscall.Stat_t) {
node.ChangeTime = time.Unix(stat.Ctim.Unix())
node.AccessTime = time.Unix(stat.Atim.Unix())
}
func changeTime(stat *syscall.Stat_t) time.Time {
return time.Unix(stat.Ctim.Unix())
}

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)
}
}