Refactor: break out walkRegular method

This commit is contained in:
Jakob Borg 2015-11-20 10:32:16 +01:00
parent 69dcdafb3d
commit f2459e61dd

View File

@ -257,9 +257,6 @@ func (w *Walker) walkAndHashFiles(fchan, dchan chan protocol.FileInfo) filepath.
return skip return skip
} }
var cf protocol.FileInfo
var ok bool
// Index wise symlinks are always files, regardless of what the target // Index wise symlinks are always files, regardless of what the target
// is, because symlinks carry their target path as their content. // is, because symlinks carry their target path as their content.
if info.Mode()&os.ModeSymlink == os.ModeSymlink { if info.Mode()&os.ModeSymlink == os.ModeSymlink {
@ -277,11 +274,22 @@ func (w *Walker) walkAndHashFiles(fchan, dchan chan protocol.FileInfo) filepath.
} }
if info.Mode().IsRegular() { if info.Mode().IsRegular() {
if err := w.walkRegular(relPath, info, mtime, fchan); err != nil {
return err
}
}
return nil
}
}
func (w *Walker) walkRegular(relPath string, info os.FileInfo, mtime time.Time, fchan chan protocol.FileInfo) error {
curMode := uint32(info.Mode()) curMode := uint32(info.Mode())
if runtime.GOOS == "windows" && osutil.IsWindowsExecutable(relPath) { if runtime.GOOS == "windows" && osutil.IsWindowsExecutable(relPath) {
curMode |= 0111 curMode |= 0111
} }
var currentVersion protocol.Vector
if w.CurrentFiler != nil { if w.CurrentFiler != nil {
// A file is "unchanged", if it // A file is "unchanged", if it
// - exists // - exists
@ -292,12 +300,13 @@ func (w *Walker) walkAndHashFiles(fchan, dchan chan protocol.FileInfo) filepath.
// - was not a symlink (since it's a file now) // - was not a symlink (since it's a file now)
// - was not invalid (since it looks valid now) // - was not invalid (since it looks valid now)
// - has the same size as previously // - has the same size as previously
cf, ok = w.CurrentFiler.CurrentFile(relPath) cf, ok := w.CurrentFiler.CurrentFile(relPath)
permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, curMode) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, curMode)
if ok && permUnchanged && !cf.IsDeleted() && cf.Modified == mtime.Unix() && !cf.IsDirectory() && if ok && permUnchanged && !cf.IsDeleted() && cf.Modified == mtime.Unix() && !cf.IsDirectory() &&
!cf.IsSymlink() && !cf.IsInvalid() && cf.Size() == info.Size() { !cf.IsSymlink() && !cf.IsInvalid() && cf.Size() == info.Size() {
return nil return nil
} }
currentVersion = cf.Version
l.Debugln("rescan:", cf, mtime.Unix(), info.Mode()&os.ModePerm) l.Debugln("rescan:", cf, mtime.Unix(), info.Mode()&os.ModePerm)
} }
@ -309,23 +318,21 @@ func (w *Walker) walkAndHashFiles(fchan, dchan chan protocol.FileInfo) filepath.
f := protocol.FileInfo{ f := protocol.FileInfo{
Name: relPath, Name: relPath,
Version: cf.Version.Update(w.ShortID), Version: currentVersion.Update(w.ShortID),
Flags: flags, Flags: flags,
Modified: mtime.Unix(), Modified: mtime.Unix(),
CachedSize: info.Size(), CachedSize: info.Size(),
} }
l.Debugln("to hash:", absPath, f) l.Debugln("to hash:", relPath, f)
select { select {
case fchan <- f: case fchan <- f:
case <-w.Cancel: case <-w.Cancel:
return errors.New("cancelled") return errors.New("cancelled")
} }
}
return nil return nil
} }
}
func (w *Walker) walkDir(relPath string, info os.FileInfo, mtime time.Time, dchan chan protocol.FileInfo) error { func (w *Walker) walkDir(relPath string, info os.FileInfo, mtime time.Time, dchan chan protocol.FileInfo) error {
var currentVersion protocol.Vector var currentVersion protocol.Vector