restic/tree.go

56 lines
979 B
Go
Raw Normal View History

2014-07-28 18:20:32 +00:00
package khepri
2014-04-21 21:25:31 +00:00
import (
"encoding/json"
"io"
"os"
"syscall"
"time"
)
type Tree struct {
Nodes []Node `json:"nodes"`
}
type Node struct {
2014-04-27 21:59:58 +00:00
Name string `json:"name"`
Mode os.FileMode `json:"mode"`
ModTime time.Time `json:"mtime"`
AccessTime time.Time `json:"atime"`
User uint32 `json:"user"`
Group uint32 `json:"group"`
Content ID `json:"content,omitempty"`
2014-04-21 21:25:31 +00:00
}
func NewTree() *Tree {
return &Tree{
Nodes: []Node{},
}
}
func (t *Tree) Restore(r io.Reader) error {
dec := json.NewDecoder(r)
return dec.Decode(t)
}
func (t *Tree) Save(w io.Writer) error {
enc := json.NewEncoder(w)
return enc.Encode(t)
}
func NodeFromFileInfo(fi os.FileInfo) Node {
node := Node{
Name: fi.Name(),
Mode: fi.Mode(),
ModTime: fi.ModTime(),
}
if stat, ok := fi.Sys().(*syscall.Stat_t); ok {
node.User = stat.Uid
node.Group = stat.Gid
2014-04-27 21:59:58 +00:00
node.AccessTime = time.Unix(stat.Atim.Unix())
2014-04-21 21:25:31 +00:00
}
return node
}