2014-07-28 18:20:32 +00:00
|
|
|
package khepri
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
import (
|
2014-08-11 20:47:24 +00:00
|
|
|
"fmt"
|
2014-04-21 21:25:31 +00:00
|
|
|
"os"
|
2014-08-11 20:47:24 +00:00
|
|
|
"os/user"
|
|
|
|
"strconv"
|
2014-09-23 20:39:12 +00:00
|
|
|
"strings"
|
2014-04-21 21:25:31 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2014-09-18 20:41:24 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
"github.com/fd0/khepri/backend"
|
2014-11-16 11:05:54 +00:00
|
|
|
"github.com/juju/arrar"
|
2014-04-21 21:25:31 +00:00
|
|
|
)
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
type Tree []*Node
|
2014-04-21 21:25:31 +00:00
|
|
|
|
|
|
|
type Node struct {
|
2014-09-23 20:39:12 +00:00
|
|
|
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,omitempty"`
|
|
|
|
Subtree backend.ID `json:"subtree,omitempty"`
|
|
|
|
|
2014-11-16 20:29:11 +00:00
|
|
|
Tree *Tree `json:"-"`
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
path string
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
type Blob struct {
|
|
|
|
ID backend.ID `json:"id,omitempty"`
|
|
|
|
Size uint64 `json:"size,omitempty"`
|
|
|
|
Storage backend.ID `json:"sid,omitempty"` // encrypted ID
|
|
|
|
StorageSize uint64 `json:"ssize,omitempty"` // encrypted Size
|
2014-09-18 20:41:24 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 20:21:44 +00:00
|
|
|
type Blobs []Blob
|
2014-08-11 21:14:40 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
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)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
return fmt.Sprintf("<Node(%s) %s>", n.Type, n.Name)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func (t Tree) String() string {
|
|
|
|
s := []string{}
|
|
|
|
for _, n := range t {
|
|
|
|
s = append(s, n.String())
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
return strings.Join(s, "\n")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
if u, nil := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
|
|
|
|
node.User = u.Username
|
|
|
|
}
|
|
|
|
|
2014-10-05 14:22:39 +00:00
|
|
|
// TODO: implement getgrnam() or use https://github.com/kless/osutil
|
2014-08-11 20:47:24 +00:00
|
|
|
// 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
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
func NodeFromFileInfo(path string, fi os.FileInfo) (*Node, error) {
|
|
|
|
node := &Node{
|
2014-09-23 20:39:12 +00:00
|
|
|
path: path,
|
2014-04-21 21:25:31 +00:00
|
|
|
Name: fi.Name(),
|
2014-08-11 20:47:24 +00:00
|
|
|
Mode: fi.Mode() & os.ModePerm,
|
2014-04-21 21:25:31 +00:00
|
|
|
ModTime: fi.ModTime(),
|
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
switch fi.Mode() & (os.ModeType | os.ModeCharDevice) {
|
|
|
|
case 0:
|
|
|
|
node.Type = "file"
|
|
|
|
case os.ModeDir:
|
|
|
|
node.Type = "dir"
|
|
|
|
case os.ModeSymlink:
|
|
|
|
node.Type = "symlink"
|
|
|
|
case os.ModeDevice | os.ModeCharDevice:
|
|
|
|
node.Type = "chardev"
|
|
|
|
case os.ModeDevice:
|
|
|
|
node.Type = "dev"
|
|
|
|
case os.ModeNamedPipe:
|
|
|
|
node.Type = "fifo"
|
|
|
|
case os.ModeSocket:
|
|
|
|
node.Type = "socket"
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 20:47:24 +00:00
|
|
|
err := node.fill_extra(path, fi)
|
|
|
|
return node, err
|
2014-04-21 21:25:31 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func (node *Node) CreateAt(ch *ContentHandler, path string) error {
|
2014-08-11 21:14:40 +00:00
|
|
|
switch node.Type {
|
2014-09-23 20:39:12 +00:00
|
|
|
case "dir":
|
|
|
|
err := os.Mkdir(path, node.Mode)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mkdir")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Lchown")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var utimes = []syscall.Timespec{
|
|
|
|
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
|
|
|
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
|
|
|
}
|
|
|
|
err = syscall.UtimesNano(path, utimes)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Utimesnano")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
case "file":
|
|
|
|
// TODO: handle hard links
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "OpenFile")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-09-18 20:41:24 +00:00
|
|
|
for _, blobid := range node.Content {
|
2014-11-16 12:22:19 +00:00
|
|
|
buf, err := ch.Load(backend.Data, blobid)
|
2014-09-18 20:41:24 +00:00
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Load")
|
2014-09-18 20:41:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
_, err = f.Write(buf)
|
2014-09-18 20:41:24 +00:00
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Write")
|
2014-09-18 20:41:24 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Lchown")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var utimes = []syscall.Timespec{
|
|
|
|
syscall.NsecToTimespec(node.AccessTime.UnixNano()),
|
|
|
|
syscall.NsecToTimespec(node.ModTime.UnixNano()),
|
|
|
|
}
|
|
|
|
err = syscall.UtimesNano(path, utimes)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Utimesnano")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
2014-08-11 21:14:40 +00:00
|
|
|
case "symlink":
|
|
|
|
err := os.Symlink(node.LinkTarget, path)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Symlink")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Lchown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Lchown")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, O_PATH|syscall.O_NOFOLLOW, 0600)
|
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "OpenFile")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 11:05:54 +00:00
|
|
|
// 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")
|
|
|
|
// }
|
2014-08-11 21:14:40 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
case "dev":
|
|
|
|
err := syscall.Mknod(path, syscall.S_IFBLK|0600, int(node.Device))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mknod")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
case "chardev":
|
|
|
|
err := syscall.Mknod(path, syscall.S_IFCHR|0600, int(node.Device))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mknod")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
case "fifo":
|
|
|
|
err := syscall.Mkfifo(path, 0600)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Mkfifo")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
case "socket":
|
|
|
|
// nothing to do, we do not restore sockets
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("filetype %q not implemented!\n", node.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Chmod(path, node.Mode)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Chmod")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chown(path, int(node.UID), int(node.GID))
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Chown")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chtimes(path, node.AccessTime, node.ModTime)
|
|
|
|
if err != nil {
|
2014-11-16 11:05:54 +00:00
|
|
|
return arrar.Annotate(err, "Chtimes")
|
2014-08-11 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|