mirror of
https://github.com/octoleo/restic.git
synced 2024-11-26 14:56:29 +00:00
24b14e21cc
This removes the allocation pools for nodes and IDs. I feel they aren't really needed. Benchmarks: benchmark old ns/op new ns/op delta BenchmarkChunkEncrypt 197890867 198616293 +0.37% BenchmarkChunkEncryptParallel 196127004 198819818 +1.37% BenchmarkArchiveDirectory 1098848419 1087237723 -1.06% BenchmarkPreload 30464455 29910239 -1.82% BenchmarkLoadTree 3265092 3088543 -5.41% BenchmarkEncryptWriter 37213511 37134683 -0.21% BenchmarkEncrypt 36037879 36166546 +0.36% BenchmarkDecryptReader 38165659 38556734 +1.02% BenchmarkEncryptDecryptReader 77027044 77194987 +0.22% BenchmarkDecrypt 36017602 35937888 -0.22% BenchmarkSaveJSON 47906 50270 +4.93% BenchmarkSaveFrom 49775973 50520969 +1.50% BenchmarkLoadJSONID 105290245 107281849 +1.89% BenchmarkChunkerWithSHA256 151501430 148264078 -2.14% BenchmarkChunkerWithMD5 93606346 94036392 +0.46% BenchmarkChunker 74285431 75933882 +2.22% BenchmarkPipelineWalker 387689 346467 -10.63% benchmark old MB/s new MB/s speedup BenchmarkChunkEncrypt 52.99 52.79 1.00x BenchmarkChunkEncryptParallel 53.46 52.74 0.99x BenchmarkEncryptWriter 225.42 225.90 1.00x BenchmarkEncrypt 232.77 231.94 1.00x BenchmarkDecryptReader 219.79 217.57 0.99x BenchmarkEncryptDecryptReader 108.90 108.67 1.00x BenchmarkDecrypt 232.90 233.42 1.00x BenchmarkSaveFrom 84.26 83.02 0.99x BenchmarkChunkerWithSHA256 69.21 70.72 1.02x BenchmarkChunkerWithMD5 112.02 111.51 1.00x BenchmarkChunker 141.15 138.09 0.98x benchmark old allocs new allocs delta BenchmarkChunkEncrypt 110 110 +0.00% BenchmarkChunkEncryptParallel 100 100 +0.00% BenchmarkArchiveDirectory 475591 476635 +0.22% BenchmarkPreload 28059 24182 -13.82% BenchmarkLoadTree 3124 2889 -7.52% BenchmarkEncryptWriter 19 19 +0.00% BenchmarkEncrypt 13 13 +0.00% BenchmarkDecryptReader 16 15 -6.25% BenchmarkEncryptDecryptReader 39 39 +0.00% BenchmarkDecrypt 11 11 +0.00% BenchmarkSaveJSON 74 74 +0.00% BenchmarkSaveFrom 109 112 +2.75% BenchmarkLoadJSONID 103630 97849 -5.58% BenchmarkChunkerWithSHA256 13 13 +0.00% BenchmarkChunkerWithMD5 12 12 +0.00% BenchmarkChunker 6 6 +0.00% BenchmarkPipelineWalker 212 165 -22.17% benchmark old bytes new bytes delta BenchmarkChunkEncrypt 64697 64697 +0.00% BenchmarkChunkEncryptParallel 64681 64681 +0.00% BenchmarkArchiveDirectory 193385504 193790864 +0.21% BenchmarkPreload 4064701 3942000 -3.02% BenchmarkLoadTree 344954 325396 -5.67% BenchmarkEncryptWriter 12793 12793 +0.00% BenchmarkEncrypt 1950 1950 +0.00% BenchmarkDecryptReader 3120 2774 -11.09% BenchmarkEncryptDecryptReader 1528036 1528036 +0.00% BenchmarkDecrypt 1919 1919 +0.00% BenchmarkSaveJSON 5524 5524 +0.00% BenchmarkSaveFrom 31353 40804 +30.14% BenchmarkLoadJSONID 12872020 16010968 +24.39% BenchmarkChunkerWithSHA256 26821 26821 +0.00% BenchmarkChunkerWithMD5 13554 13554 +0.00% BenchmarkChunker 13458 13458 +0.00% BenchmarkPipelineWalker 58584 55560 -5.16%
344 lines
7.5 KiB
Go
344 lines
7.5 KiB
Go
package restic
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/juju/arrar"
|
|
"github.com/restic/restic/backend"
|
|
)
|
|
|
|
type Node struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Mode os.FileMode `json:"mode,omitempty"`
|
|
ModTime time.Time `json:"mtime,omitempty"`
|
|
AccessTime time.Time `json:"atime,omitempty"`
|
|
ChangeTime time.Time `json:"ctime,omitempty"`
|
|
UID uint32 `json:"uid"`
|
|
GID uint32 `json:"gid"`
|
|
User string `json:"user,omitempty"`
|
|
Group string `json:"group,omitempty"`
|
|
Inode uint64 `json:"inode,omitempty"`
|
|
Size uint64 `json:"size,omitempty"`
|
|
Links uint64 `json:"links,omitempty"`
|
|
LinkTarget string `json:"linktarget,omitempty"`
|
|
Device uint64 `json:"device,omitempty"`
|
|
Content []backend.ID `json:"content"`
|
|
Subtree backend.ID `json:"subtree,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
tree *Tree
|
|
|
|
path string
|
|
err error
|
|
blobs Blobs
|
|
}
|
|
|
|
func (n Node) String() string {
|
|
switch n.Type {
|
|
case "file":
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
|
n.Mode, n.UID, n.GID, n.Size, n.ModTime, n.Name)
|
|
case "dir":
|
|
return fmt.Sprintf("%s %5d %5d %6d %s %s",
|
|
n.Mode|os.ModeDir, n.UID, n.GID, n.Size, n.ModTime, n.Name)
|
|
}
|
|
|
|
return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name)
|
|
}
|
|
|
|
func (node Node) Tree() *Tree {
|
|
return node.tree
|
|
}
|
|
|
|
func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
|
|
node := &Node{
|
|
path: path,
|
|
Name: fi.Name(),
|
|
Mode: fi.Mode() & os.ModePerm,
|
|
ModTime: fi.ModTime(),
|
|
}
|
|
|
|
node.Type = nodeTypeFromFileInfo(path, fi)
|
|
if node.Type == "file" {
|
|
node.Size = uint64(fi.Size())
|
|
}
|
|
|
|
err := node.fill_extra(path, fi)
|
|
return node, err
|
|
}
|
|
|
|
func nodeTypeFromFileInfo(path string, fi os.FileInfo) string {
|
|
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
|
|
case 0:
|
|
return "file"
|
|
case os.ModeDir:
|
|
return "dir"
|
|
case os.ModeSymlink:
|
|
return "symlink"
|
|
case os.ModeDevice | os.ModeCharDevice:
|
|
return "chardev"
|
|
case os.ModeDevice:
|
|
return "dev"
|
|
case os.ModeNamedPipe:
|
|
return "fifo"
|
|
case os.ModeSocket:
|
|
return "socket"
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
func CreateNodeAt(node *Node, m *Map, s Server, path string) 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")
|
|
}
|
|
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")
|
|
}
|
|
case "symlink":
|
|
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) {
|
|
type nodeJSON Node
|
|
nj := nodeJSON(node)
|
|
name := strconv.Quote(node.Name)
|
|
nj.Name = name[1 : len(name)-1]
|
|
|
|
return json.Marshal(nj)
|
|
}
|
|
|
|
func (node *Node) UnmarshalJSON(data []byte) error {
|
|
type nodeJSON Node
|
|
var nj *nodeJSON = (*nodeJSON)(node)
|
|
|
|
err := json.Unmarshal(data, nj)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
nj.Name, err = strconv.Unquote(`"` + nj.Name + `"`)
|
|
return err
|
|
}
|
|
|
|
func (node Node) Equals(other Node) bool {
|
|
// TODO: add generatored code for this
|
|
if node.Name != other.Name {
|
|
return false
|
|
}
|
|
if node.Type != other.Type {
|
|
return false
|
|
}
|
|
if node.Mode != other.Mode {
|
|
return false
|
|
}
|
|
if node.ModTime != other.ModTime {
|
|
return false
|
|
}
|
|
if node.AccessTime != other.AccessTime {
|
|
return false
|
|
}
|
|
if node.ChangeTime != other.ChangeTime {
|
|
return false
|
|
}
|
|
if node.UID != other.UID {
|
|
return false
|
|
}
|
|
if node.GID != other.GID {
|
|
return false
|
|
}
|
|
if node.User != other.User {
|
|
return false
|
|
}
|
|
if node.Group != other.Group {
|
|
return false
|
|
}
|
|
if node.Inode != other.Inode {
|
|
return false
|
|
}
|
|
if node.Size != other.Size {
|
|
return false
|
|
}
|
|
if node.Links != other.Links {
|
|
return false
|
|
}
|
|
if node.LinkTarget != other.LinkTarget {
|
|
return false
|
|
}
|
|
if node.Device != other.Device {
|
|
return false
|
|
}
|
|
if node.Content != nil && other.Content == nil {
|
|
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
|
|
}
|