Move platform specific code to different files

This commit is contained in:
Alexander Neumann 2015-02-03 21:18:19 +01:00
parent 460ebebeef
commit 1da693a7d7
4 changed files with 114 additions and 55 deletions

63
node.go
View File

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

19
node_darwin.go Normal file
View File

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

68
node_linux.go Normal file
View File

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

19
node_windows.go Normal file
View File

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