2015-02-03 20:18:19 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2015-03-07 10:53:32 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/debug"
|
2015-02-03 20:18:19 +00:00
|
|
|
)
|
|
|
|
|
2015-04-25 00:36:54 +00:00
|
|
|
func (node *Node) OpenForReading() (*os.File, error) {
|
2015-04-26 00:54:33 +00:00
|
|
|
file, err := os.OpenFile(node.path, os.O_RDONLY|syscall.O_NOATIME, 0)
|
|
|
|
if os.IsPermission(err) {
|
|
|
|
return os.OpenFile(node.path, os.O_RDONLY, 0)
|
|
|
|
}
|
|
|
|
return file, err
|
2015-04-25 00:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:39:32 +00:00
|
|
|
func (node *Node) fillExtra(path string, fi os.FileInfo) error {
|
2015-02-03 20:18:19 +00:00
|
|
|
stat, ok := fi.Sys().(*syscall.Stat_t)
|
|
|
|
if !ok {
|
2015-03-21 13:43:33 +00:00
|
|
|
return nil
|
2015-02-03 20:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node.ChangeTime = time.Unix(stat.Ctim.Unix())
|
|
|
|
node.AccessTime = time.Unix(stat.Atim.Unix())
|
|
|
|
node.UID = stat.Uid
|
|
|
|
node.GID = stat.Gid
|
|
|
|
|
2015-03-21 13:43:33 +00:00
|
|
|
if u, err := user.LookupId(strconv.Itoa(int(stat.Uid))); err == nil {
|
2015-02-03 20:18:19 +00:00
|
|
|
node.User = u.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Inode = stat.Ino
|
|
|
|
|
2015-03-21 13:43:33 +00:00
|
|
|
var err error
|
|
|
|
|
2015-02-03 20:18:19 +00:00
|
|
|
switch node.Type {
|
|
|
|
case "file":
|
|
|
|
node.Size = uint64(stat.Size)
|
2015-02-03 21:06:06 +00:00
|
|
|
node.Links = uint64(stat.Nlink)
|
2015-02-03 20:18:19 +00:00
|
|
|
case "dir":
|
|
|
|
case "symlink":
|
|
|
|
node.LinkTarget, err = os.Readlink(path)
|
|
|
|
case "dev":
|
|
|
|
node.Device = stat.Rdev
|
|
|
|
case "chardev":
|
|
|
|
node.Device = stat.Rdev
|
|
|
|
case "fifo":
|
|
|
|
case "socket":
|
|
|
|
default:
|
2015-04-29 01:24:43 +00:00
|
|
|
err = fmt.Errorf("invalid node type %q", node.Type)
|
2015-02-03 20:18:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2015-03-07 10:53:32 +00:00
|
|
|
|
|
|
|
func (node *Node) isNewer(path string, fi os.FileInfo) bool {
|
|
|
|
if node.Type != "file" {
|
|
|
|
debug.Log("node.isNewer", "node %v is newer: not file", path)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
tpe := nodeTypeFromFileInfo(fi)
|
2015-03-07 10:53:32 +00:00
|
|
|
if node.Name != fi.Name() || node.Type != tpe {
|
|
|
|
debug.Log("node.isNewer", "node %v is newer: name or type changed", path)
|
2015-03-22 14:05:34 +00:00
|
|
|
return true
|
2015-03-07 10:53:32 +00:00
|
|
|
}
|
|
|
|
|
2015-04-29 01:24:43 +00:00
|
|
|
extendedStat := fi.Sys().(*syscall.Stat_t)
|
|
|
|
changeTime := time.Unix(extendedStat.Ctim.Unix())
|
|
|
|
inode := extendedStat.Ino
|
|
|
|
size := uint64(extendedStat.Size)
|
2015-03-07 10:53:32 +00:00
|
|
|
|
|
|
|
if node.ModTime != fi.ModTime() ||
|
|
|
|
node.ChangeTime != changeTime ||
|
|
|
|
node.Inode != inode ||
|
|
|
|
node.Size != size {
|
2015-03-22 14:05:34 +00:00
|
|
|
debug.Log("node.isNewer", "node %v is newer: timestamp, size or inode changed", path)
|
|
|
|
return true
|
2015-03-07 10:53:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("node.isNewer", "node %v is not newer", path)
|
|
|
|
return false
|
|
|
|
}
|