From 1da693a7d79f1a02dab75f56d49b165c6bfa45b7 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 3 Feb 2015 21:18:19 +0100 Subject: [PATCH] Move platform specific code to different files --- node.go | 63 ++++++--------------------------------------- node_darwin.go | 19 ++++++++++++++ node_linux.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ node_windows.go | 19 ++++++++++++++ 4 files changed, 114 insertions(+), 55 deletions(-) create mode 100644 node_darwin.go create mode 100644 node_linux.go create mode 100644 node_windows.go diff --git a/node.go b/node.go index 10907ea3c..f044382f7 100644 --- a/node.go +++ b/node.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "os" - "os/user" "strconv" "syscall" "time" @@ -57,52 +56,6 @@ func (node Node) Tree() *Tree { return node.tree } -func (node *Node) fill_extra(path string, fi os.FileInfo) (err error) { - stat, ok := fi.Sys().(*syscall.Stat_t) - if !ok { - return - } - - 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, 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 = 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 NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) { node := GetNode() node.path = path @@ -203,11 +156,11 @@ func CreateNodeAt(node *Node, m *Map, s Server, path string) error { 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") - } + // 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{ @@ -221,17 +174,17 @@ func CreateNodeAt(node *Node, m *Map, s Server, path string) error { return nil case "dev": - err := syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device)) + err := node.createDevAt(path) if err != nil { return arrar.Annotate(err, "Mknod") } case "chardev": - err := syscall.Mknod(path, syscall.S_IFCHR|0600, int(node.Device)) + err := node.createCharDevAt(path) if err != nil { return arrar.Annotate(err, "Mknod") } case "fifo": - err := syscall.Mkfifo(path, 0600) + err := node.createFifoAt(path) if err != nil { return arrar.Annotate(err, "Mkfifo") } diff --git a/node_darwin.go b/node_darwin.go new file mode 100644 index 000000000..884c33d1c --- /dev/null +++ b/node_darwin.go @@ -0,0 +1,19 @@ +package restic + +import "os" + +func (node *Node) fill_extra(path string, fi os.FileInfo) error { + return nil +} + +func (node *Node) createDevAt(path string) error { + return nil +} + +func (node *Node) createCharDevAt(path string) error { + return nil +} + +func (node *Node) createFifoAt(path string) error { + return nil +} diff --git a/node_linux.go b/node_linux.go new file mode 100644 index 000000000..d44a546cc --- /dev/null +++ b/node_linux.go @@ -0,0 +1,68 @@ +package restic + +import ( + "fmt" + "os" + "os/user" + "strconv" + "syscall" + "time" +) + +func (node *Node) fill_extra(path string, fi os.FileInfo) (err error) { + stat, ok := fi.Sys().(*syscall.Stat_t) + if !ok { + return + } + + 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, 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 = 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)) +} + +func (node *Node) createCharDevAt(path string) error { + return syscall.Mknod(path, syscall.S_IFCHR|0600, int(node.Device)) +} + +func (node *Node) createFifoAt(path string) error { + return syscall.Mkfifo(path, 0600) +} diff --git a/node_windows.go b/node_windows.go new file mode 100644 index 000000000..884c33d1c --- /dev/null +++ b/node_windows.go @@ -0,0 +1,19 @@ +package restic + +import "os" + +func (node *Node) fill_extra(path string, fi os.FileInfo) error { + return nil +} + +func (node *Node) createDevAt(path string) error { + return nil +} + +func (node *Node) createCharDevAt(path string) error { + return nil +} + +func (node *Node) createFifoAt(path string) error { + return nil +}