2017-01-31 23:17:33 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type currentPath struct {
|
|
|
|
path string
|
|
|
|
f os.FileInfo
|
|
|
|
fullPath string
|
|
|
|
}
|
|
|
|
|
2017-02-03 01:30:11 +00:00
|
|
|
func pathChange(lower, upper *currentPath) (ChangeKind, string) {
|
2017-01-31 23:17:33 +00:00
|
|
|
if lower == nil {
|
|
|
|
if upper == nil {
|
|
|
|
panic("cannot compare nil paths")
|
|
|
|
}
|
2017-02-03 01:30:11 +00:00
|
|
|
return ChangeKindAdd, upper.path
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
if upper == nil {
|
2017-02-03 01:30:11 +00:00
|
|
|
return ChangeKindDelete, lower.path
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
// TODO: compare by directory
|
|
|
|
|
|
|
|
switch i := strings.Compare(lower.path, upper.path); {
|
|
|
|
case i < 0:
|
|
|
|
// File in lower that is not in upper
|
2017-02-03 01:30:11 +00:00
|
|
|
return ChangeKindDelete, lower.path
|
2017-01-31 23:17:33 +00:00
|
|
|
case i > 0:
|
|
|
|
// File in upper that is not in lower
|
2017-02-03 01:30:11 +00:00
|
|
|
return ChangeKindAdd, upper.path
|
2017-01-31 23:17:33 +00:00
|
|
|
default:
|
2017-02-03 01:30:11 +00:00
|
|
|
return ChangeKindModify, upper.path
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameFile(f1, f2 *currentPath) (bool, error) {
|
|
|
|
if os.SameFile(f1.f, f2.f) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
equalStat, err := compareSysStat(f1.f.Sys(), f2.f.Sys())
|
|
|
|
if err != nil || !equalStat {
|
|
|
|
return equalStat, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if eq, err := compareCapabilities(f1.fullPath, f2.fullPath); err != nil || !eq {
|
|
|
|
return eq, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not a directory also check size, modtime, and content
|
|
|
|
if !f1.f.IsDir() {
|
|
|
|
if f1.f.Size() != f2.f.Size() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
t1 := f1.f.ModTime()
|
|
|
|
t2 := f2.f.ModTime()
|
|
|
|
|
|
|
|
if t1.Unix() != t2.Unix() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the timestamp may have been truncated in one of the
|
|
|
|
// files, check content of file to determine difference
|
|
|
|
if t1.Nanosecond() == 0 || t2.Nanosecond() == 0 {
|
|
|
|
if f1.f.Size() > 0 {
|
|
|
|
eq, err := compareFileContent(f1.fullPath, f2.fullPath)
|
|
|
|
if err != nil || !eq {
|
|
|
|
return eq, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if t1.Nanosecond() != t2.Nanosecond() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const compareChuckSize = 32 * 1024
|
|
|
|
|
2017-02-01 22:23:56 +00:00
|
|
|
// compareFileContent compares the content of 2 same sized files
|
|
|
|
// by comparing each byte.
|
2017-01-31 23:17:33 +00:00
|
|
|
func compareFileContent(p1, p2 string) (bool, error) {
|
|
|
|
f1, err := os.Open(p1)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer f1.Close()
|
|
|
|
f2, err := os.Open(p2)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer f2.Close()
|
|
|
|
|
|
|
|
b1 := make([]byte, compareChuckSize)
|
|
|
|
b2 := make([]byte, compareChuckSize)
|
|
|
|
for {
|
|
|
|
n1, err1 := f1.Read(b1)
|
|
|
|
if err1 != nil && err1 != io.EOF {
|
|
|
|
return false, err1
|
|
|
|
}
|
|
|
|
n2, err2 := f2.Read(b2)
|
|
|
|
if err2 != nil && err2 != io.EOF {
|
|
|
|
return false, err2
|
|
|
|
}
|
|
|
|
if n1 != n2 || !bytes.Equal(b1[:n1], b2[:n2]) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err1 == io.EOF && err2 == io.EOF {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
func pathWalk(ctx context.Context, root string, pathC chan<- *currentPath) error {
|
|
|
|
return filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-31 23:17:33 +00:00
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
// Rebase path
|
|
|
|
path, err = filepath.Rel(root, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-31 23:17:33 +00:00
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
path = filepath.Join(string(os.PathSeparator), path)
|
2017-01-31 23:17:33 +00:00
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
// Skip root
|
|
|
|
if path == string(os.PathSeparator) {
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-31 23:17:33 +00:00
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
p := ¤tPath{
|
|
|
|
path: path,
|
|
|
|
f: f,
|
|
|
|
fullPath: filepath.Join(root, path),
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
case pathC <- p:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
})
|
2017-01-31 23:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 00:37:04 +00:00
|
|
|
func nextPath(ctx context.Context, pathC <-chan *currentPath) (*currentPath, error) {
|
2017-01-31 23:17:33 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-02-03 00:37:04 +00:00
|
|
|
return nil, ctx.Err()
|
|
|
|
case p := <-pathC:
|
2017-01-31 23:17:33 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|