2014-01-06 11:11:18 +01:00
|
|
|
package model
|
2013-12-15 11:43:31 +01:00
|
|
|
|
|
|
|
import (
|
2014-01-06 21:17:18 +01:00
|
|
|
"bytes"
|
2013-12-15 11:43:31 +01:00
|
|
|
"fmt"
|
2014-01-06 21:17:18 +01:00
|
|
|
"io/ioutil"
|
2014-01-06 11:11:18 +01:00
|
|
|
"log"
|
2013-12-15 11:43:31 +01:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2014-01-07 16:10:38 +01:00
|
|
|
"time"
|
2014-01-07 22:44:21 +01:00
|
|
|
|
|
|
|
"github.com/calmh/syncthing/protocol"
|
2013-12-15 11:43:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const BlockSize = 128 * 1024
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
Name string
|
|
|
|
Flags uint32
|
|
|
|
Modified int64
|
2014-01-07 22:44:21 +01:00
|
|
|
Version uint32
|
2014-01-06 11:11:18 +01:00
|
|
|
Blocks []Block
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
2013-12-30 15:30:29 +01:00
|
|
|
func (f File) Size() (bytes int) {
|
|
|
|
for _, b := range f.Blocks {
|
|
|
|
bytes += int(b.Length)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-07 22:44:21 +01:00
|
|
|
func (f File) Equals(o File) bool {
|
|
|
|
return f.Modified == o.Modified && f.Version == o.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f File) NewerThan(o File) bool {
|
|
|
|
return f.Modified > o.Modified || (f.Modified == o.Modified && f.Version > o.Version)
|
|
|
|
}
|
|
|
|
|
2013-12-15 11:43:31 +01:00
|
|
|
func isTempName(name string) bool {
|
|
|
|
return strings.HasPrefix(path.Base(name), ".syncthing.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func tempName(name string, modified int64) string {
|
|
|
|
tdir := path.Dir(name)
|
|
|
|
tname := fmt.Sprintf(".syncthing.%s.%d", path.Base(name), modified)
|
|
|
|
return path.Join(tdir, tname)
|
|
|
|
}
|
|
|
|
|
2014-01-09 22:46:01 +01:00
|
|
|
func (m *Model) loadIgnoreFiles(ign map[string][]string) filepath.WalkFunc {
|
2013-12-15 11:43:31 +01:00
|
|
|
return func(p string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2013-12-31 01:50:04 +01:00
|
|
|
return nil
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 21:17:18 +01:00
|
|
|
rn, err := filepath.Rel(m.dir, p)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if pn, sn := path.Split(rn); sn == ".stignore" {
|
|
|
|
pn := strings.Trim(pn, "/")
|
|
|
|
bs, _ := ioutil.ReadFile(p)
|
|
|
|
lines := bytes.Split(bs, []byte("\n"))
|
|
|
|
var patterns []string
|
|
|
|
for _, line := range lines {
|
|
|
|
if len(line) > 0 {
|
|
|
|
patterns = append(patterns, string(line))
|
|
|
|
}
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
2014-01-06 21:17:18 +01:00
|
|
|
ign[pn] = patterns
|
2014-01-09 22:46:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) walkAndHashFiles(res *[]File, ign map[string][]string) filepath.WalkFunc {
|
|
|
|
return func(p string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if isTempName(p) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rn, err := filepath.Rel(m.dir, p)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2014-01-09 23:00:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, sn := path.Split(rn); sn == ".stignore" {
|
|
|
|
// We never sync the .stignore files
|
|
|
|
return nil
|
2014-01-09 22:46:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ignoreFile(ign, rn) {
|
|
|
|
if m.trace["file"] {
|
|
|
|
log.Println("FILE: IGNORE:", rn)
|
|
|
|
}
|
2014-01-06 21:17:18 +01:00
|
|
|
return nil
|
|
|
|
}
|
2013-12-15 11:43:31 +01:00
|
|
|
|
2014-01-06 21:17:18 +01:00
|
|
|
if info.Mode()&os.ModeType == 0 {
|
2013-12-15 11:43:31 +01:00
|
|
|
fi, err := os.Stat(p)
|
|
|
|
if err != nil {
|
2013-12-31 01:50:04 +01:00
|
|
|
return nil
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
modified := fi.ModTime().Unix()
|
|
|
|
|
2014-01-06 11:11:18 +01:00
|
|
|
m.RLock()
|
|
|
|
hf, ok := m.local[rn]
|
|
|
|
m.RUnlock()
|
|
|
|
|
2013-12-15 11:43:31 +01:00
|
|
|
if ok && hf.Modified == modified {
|
2014-01-07 22:44:21 +01:00
|
|
|
if nf := uint32(info.Mode()); nf != hf.Flags {
|
|
|
|
hf.Flags = nf
|
|
|
|
hf.Version++
|
|
|
|
}
|
2013-12-15 11:43:31 +01:00
|
|
|
*res = append(*res, hf)
|
|
|
|
} else {
|
2014-01-07 16:10:38 +01:00
|
|
|
m.Lock()
|
|
|
|
if m.shouldSuppressChange(rn) {
|
|
|
|
if m.trace["file"] {
|
|
|
|
log.Println("FILE: SUPPRESS:", rn, m.fileWasSuppressed[rn], time.Since(m.fileLastChanged[rn]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2014-01-07 22:44:21 +01:00
|
|
|
hf.Flags = protocol.FlagInvalid
|
|
|
|
hf.Version++
|
2014-01-07 16:10:38 +01:00
|
|
|
*res = append(*res, hf)
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m.Unlock()
|
|
|
|
|
2014-01-06 11:11:18 +01:00
|
|
|
if m.trace["file"] {
|
|
|
|
log.Printf("FILE: Hash %q", p)
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
fd, err := os.Open(p)
|
|
|
|
if err != nil {
|
2014-01-07 16:10:38 +01:00
|
|
|
if m.trace["file"] {
|
|
|
|
log.Printf("FILE: %q: %v", p, err)
|
|
|
|
}
|
2013-12-31 01:50:04 +01:00
|
|
|
return nil
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
blocks, err := Blocks(fd, BlockSize)
|
|
|
|
if err != nil {
|
2014-01-07 16:10:38 +01:00
|
|
|
if m.trace["file"] {
|
|
|
|
log.Printf("FILE: %q: %v", p, err)
|
|
|
|
}
|
2013-12-31 01:50:04 +01:00
|
|
|
return nil
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
f := File{
|
|
|
|
Name: rn,
|
|
|
|
Flags: uint32(info.Mode()),
|
|
|
|
Modified: modified,
|
|
|
|
Blocks: blocks,
|
|
|
|
}
|
|
|
|
*res = append(*res, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 11:11:18 +01:00
|
|
|
// Walk returns the list of files found in the local repository by scanning the
|
|
|
|
// file system. Files are blockwise hashed.
|
2014-01-06 21:17:18 +01:00
|
|
|
func (m *Model) Walk(followSymlinks bool) (files []File, ignore map[string][]string) {
|
|
|
|
ignore = make(map[string][]string)
|
2014-01-09 22:46:01 +01:00
|
|
|
|
|
|
|
hashFiles := m.walkAndHashFiles(&files, ignore)
|
|
|
|
|
|
|
|
filepath.Walk(m.dir, m.loadIgnoreFiles(ignore))
|
|
|
|
filepath.Walk(m.dir, hashFiles)
|
2013-12-15 21:20:50 +01:00
|
|
|
|
2014-01-06 11:11:18 +01:00
|
|
|
if followSymlinks {
|
|
|
|
d, err := os.Open(m.dir)
|
2013-12-15 21:20:50 +01:00
|
|
|
if err != nil {
|
2014-01-06 21:17:18 +01:00
|
|
|
return
|
2013-12-15 21:20:50 +01:00
|
|
|
}
|
2013-12-31 01:30:59 +01:00
|
|
|
defer d.Close()
|
|
|
|
|
2013-12-15 21:20:50 +01:00
|
|
|
fis, err := d.Readdir(-1)
|
|
|
|
if err != nil {
|
2014-01-06 21:17:18 +01:00
|
|
|
return
|
2013-12-15 21:20:50 +01:00
|
|
|
}
|
2013-12-31 01:30:59 +01:00
|
|
|
|
2013-12-15 21:20:50 +01:00
|
|
|
for _, fi := range fis {
|
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
2014-01-09 22:46:01 +01:00
|
|
|
dir := path.Join(m.dir, fi.Name()) + "/"
|
|
|
|
filepath.Walk(dir, m.loadIgnoreFiles(ignore))
|
|
|
|
filepath.Walk(dir, hashFiles)
|
2013-12-15 21:20:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-06 21:17:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-06 11:11:18 +01:00
|
|
|
func (m *Model) cleanTempFile(path string, info os.FileInfo, err error) error {
|
2013-12-15 11:43:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Mode()&os.ModeType == 0 && isTempName(path) {
|
2014-01-06 11:11:18 +01:00
|
|
|
if m.trace["file"] {
|
|
|
|
log.Printf("FILE: Remove %q", path)
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-06 11:11:18 +01:00
|
|
|
func (m *Model) cleanTempFiles() {
|
|
|
|
filepath.Walk(m.dir, m.cleanTempFile)
|
2013-12-15 11:43:31 +01:00
|
|
|
}
|
2014-01-06 21:17:18 +01:00
|
|
|
|
|
|
|
func ignoreFilter(patterns map[string][]string, files []File) (filtered []File) {
|
|
|
|
for _, f := range files {
|
2014-01-09 22:46:01 +01:00
|
|
|
if !ignoreFile(patterns, f.Name) {
|
|
|
|
filtered = append(filtered, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
|
|
|
func ignoreFile(patterns map[string][]string, file string) bool {
|
|
|
|
first, last := path.Split(file)
|
|
|
|
for prefix, pats := range patterns {
|
|
|
|
if len(prefix) == 0 || prefix == first || strings.HasPrefix(first, prefix+"/") {
|
|
|
|
for _, pattern := range pats {
|
|
|
|
if match, _ := path.Match(pattern, last); match {
|
|
|
|
return true
|
2014-01-06 21:17:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 22:46:01 +01:00
|
|
|
return false
|
2014-01-06 21:17:18 +01:00
|
|
|
}
|