func startWalker(dir string, res chan<- fileInfo, abort <-chan struct{}) { walker := func(path string, info os.FileInfo, err error) error { if err != nil { return err } rn, _ := filepath.Rel(dir, path) if rn == "." || rn == ".stfolder" { return nil } var f fileInfo if ok, err := symlinks.IsSymlink(path); err == nil && ok { f = fileInfo{ name: rn, mode: os.ModeSymlink, } tgt, _, err := symlinks.Read(path) if err != nil { return err } h := md5.New() h.Write([]byte(tgt)) hash := h.Sum(nil) copy(f.hash[:], hash) } else if info.IsDir() { f = fileInfo{ name: rn, mode: info.Mode(), // hash and modtime zero for directories } } else { f = fileInfo{ name: rn, mode: info.Mode(), mod: info.ModTime().Unix(), } sum, err := md5file(path) if err != nil { return err } f.hash = sum } select { case res <- f: return nil case <-abort: return errors.New("abort") } } go func() { filepath.Walk(dir, walker) close(res) }() }
// Request returns the specified data segment by reading it from local disk. // Implements the protocol.Model interface. func (m *Model) Request(deviceID protocol.DeviceID, folder, name string, offset int64, size int) ([]byte, error) { // Verify that the requested file exists in the local model. m.fmut.RLock() r, ok := m.folderFiles[folder] m.fmut.RUnlock() if !ok { l.Warnf("Request from %s for file %s in nonexistent folder %q", deviceID, name, folder) return nil, ErrNoSuchFile } lf := r.Get(protocol.LocalDeviceID, name) if lf.IsInvalid() || lf.IsDeleted() { if debug { l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d; invalid: %v", m, deviceID, folder, name, offset, size, lf) } return nil, ErrInvalid } if offset > lf.Size() { if debug { l.Debugf("%v REQ(in; nonexistent): %s: %q o=%d s=%d", m, deviceID, name, offset, size) } return nil, ErrNoSuchFile } if debug && deviceID != protocol.LocalDeviceID { l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d", m, deviceID, folder, name, offset, size) } m.fmut.RLock() fn := filepath.Join(m.folderCfgs[folder].Path, name) m.fmut.RUnlock() var reader io.ReaderAt var err error if lf.IsSymlink() { target, _, err := symlinks.Read(fn) if err != nil { return nil, err } reader = strings.NewReader(target) } else { reader, err = os.Open(fn) // XXX: Inefficient, should cache fd? if err != nil { return nil, err } defer reader.(*os.File).Close() } buf := make([]byte, size) _, err = reader.ReadAt(buf, offset) if err != nil { return nil, err } return buf, nil }
// Request returns the specified data segment by reading it from local disk. // Implements the protocol.Model interface. func (m *Model) Request(deviceID protocol.DeviceID, folder, name string, offset int64, size int, hash []byte, flags uint32, options []protocol.Option) ([]byte, error) { if offset < 0 || size < 0 { return nil, protocol.ErrNoSuchFile } if !m.folderSharedWith(folder, deviceID) { l.Warnf("Request from %s for file %s in unshared folder %q", deviceID, name, folder) return nil, protocol.ErrNoSuchFile } if flags != 0 { // We don't currently support or expect any flags. return nil, fmt.Errorf("protocol error: unknown flags 0x%x in Request message", flags) } // Verify that the requested file exists in the local model. We only need // to validate this file if we haven't done so recently, so we keep a // cache of successfull results. "Recently" can be quite a long time, as // we remove validation cache entries when we detect local changes. If // we're out of sync here and the file actually doesn't exist any more, or // has shrunk or something, then we'll anyway get a read error that we // pass on to the other side. m.rvmut.RLock() validated := m.reqValidationCache[folder+"/"+name] m.rvmut.RUnlock() if time.Since(validated) > reqValidationTime { m.fmut.RLock() folderFiles, ok := m.folderFiles[folder] m.fmut.RUnlock() if !ok { l.Warnf("Request from %s for file %s in nonexistent folder %q", deviceID, name, folder) return nil, protocol.ErrNoSuchFile } // This call is really expensive for large files, as we load the full // block list which may be megabytes and megabytes of data to allocate // space for, read, and deserialize. lf, ok := folderFiles.Get(protocol.LocalDeviceID, name) if !ok { return nil, protocol.ErrNoSuchFile } if lf.IsInvalid() || lf.IsDeleted() { if debug { l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d; invalid: %v", m, deviceID, folder, name, offset, size, lf) } return nil, protocol.ErrInvalid } if offset > lf.Size() { if debug { l.Debugf("%v REQ(in; nonexistent): %s: %q o=%d s=%d", m, deviceID, name, offset, size) } return nil, protocol.ErrNoSuchFile } m.rvmut.Lock() m.reqValidationCache[folder+"/"+name] = time.Now() if len(m.reqValidationCache) > reqValidationCacheSize { // Don't let the cache grow infinitely for name, validated := range m.reqValidationCache { if time.Since(validated) > time.Minute { delete(m.reqValidationCache, name) } } } m.rvmut.Unlock() } if debug && deviceID != protocol.LocalDeviceID { l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d", m, deviceID, folder, name, offset, size) } m.fmut.RLock() fn := filepath.Join(m.folderCfgs[folder].Path(), name) m.fmut.RUnlock() var reader io.ReaderAt var err error if info, err := os.Lstat(fn); err == nil && info.Mode()&os.ModeSymlink != 0 { target, _, err := symlinks.Read(fn) if err != nil { return nil, err } reader = strings.NewReader(target) } else { // Cannot easily cache fd's because we might need to delete the file // at any moment. reader, err = os.Open(fn) if err != nil { return nil, err } defer reader.(*os.File).Close() } buf := make([]byte, size) _, err = reader.ReadAt(buf, offset) if err != nil { return nil, err } return buf, nil }
func (w *Walker) walkAndHashFiles(fchan chan protocol.FileInfo) filepath.WalkFunc { now := time.Now() return func(p string, info os.FileInfo, err error) error { // Return value used when we are returning early and don't want to // process the item. For directories, this means do-not-descend. var skip error // nil // info nil when error is not nil if info != nil && info.IsDir() { skip = filepath.SkipDir } if err != nil { if debug { l.Debugln("error:", p, info, err) } return skip } rn, err := filepath.Rel(w.Dir, p) if err != nil { if debug { l.Debugln("rel error:", p, err) } return skip } if rn == "." { return nil } mtime := info.ModTime() if w.MtimeRepo != nil { mtime = w.MtimeRepo.GetMtime(rn, mtime) } if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) { // A temporary file if debug { l.Debugln("temporary:", rn) } if info.Mode().IsRegular() && mtime.Add(w.TempLifetime).Before(now) { os.Remove(p) if debug { l.Debugln("removing temporary:", rn, mtime) } } return nil } if sn := filepath.Base(rn); sn == ".stignore" || sn == ".stfolder" || strings.HasPrefix(rn, ".stversions") || w.Matcher.Match(rn) { // An ignored file if debug { l.Debugln("ignored:", rn) } return skip } if !utf8.ValidString(rn) { l.Warnf("File name %q is not in UTF8 encoding; skipping.", rn) return skip } var normalizedRn string if runtime.GOOS == "darwin" { // Mac OS X file names should always be NFD normalized. normalizedRn = norm.NFD.String(rn) } else { // Every other OS in the known universe uses NFC or just plain // doesn't bother to define an encoding. In our case *we* do care, // so we enforce NFC regardless. normalizedRn = norm.NFC.String(rn) } if rn != normalizedRn { // The file name was not normalized. if !w.AutoNormalize { // We're not authorized to do anything about it, so complain and skip. l.Warnf("File name %q is not in the correct UTF8 normalization form; skipping.", rn) return skip } // We will attempt to normalize it. normalizedPath := filepath.Join(w.Dir, normalizedRn) if _, err := osutil.Lstat(normalizedPath); os.IsNotExist(err) { // Nothing exists with the normalized filename. Good. if err = os.Rename(p, normalizedPath); err != nil { l.Infof(`Error normalizing UTF8 encoding of file "%s": %v`, rn, err) return skip } l.Infof(`Normalized UTF8 encoding of file name "%s".`, rn) } else { // There is something already in the way at the normalized // file name. l.Infof(`File "%s" has UTF8 encoding conflict with another file; ignoring.`, rn) return skip } rn = normalizedRn } var cf protocol.FileInfo var ok bool // Index wise symlinks are always files, regardless of what the target // is, because symlinks carry their target path as their content. if info.Mode()&os.ModeSymlink == os.ModeSymlink { // If the target is a directory, do NOT descend down there. This // will cause files to get tracked, and removing the symlink will // as a result remove files in their real location. if !symlinks.Supported { return skip } // We always rehash symlinks as they have no modtime or // permissions. We check if they point to the old target by // checking that their existing blocks match with the blocks in // the index. target, flags, err := symlinks.Read(p) flags = flags & protocol.SymlinkTypeMask if err != nil { if debug { l.Debugln("readlink error:", p, err) } return skip } blocks, err := Blocks(strings.NewReader(target), w.BlockSize, 0) if err != nil { if debug { l.Debugln("hash link error:", p, err) } return skip } if w.CurrentFiler != nil { // A symlink is "unchanged", if // - it exists // - it wasn't deleted (because it isn't now) // - it was a symlink // - it wasn't invalid // - the symlink type (file/dir) was the same // - the block list (i.e. hash of target) was the same cf, ok = w.CurrentFiler.CurrentFile(rn) if ok && !cf.IsDeleted() && cf.IsSymlink() && !cf.IsInvalid() && SymlinkTypeEqual(flags, cf.Flags) && BlocksEqual(cf.Blocks, blocks) { return skip } } f := protocol.FileInfo{ Name: rn, Version: cf.Version.Update(w.ShortID), Flags: protocol.FlagSymlink | flags | protocol.FlagNoPermBits | 0666, Modified: 0, Blocks: blocks, } if debug { l.Debugln("symlink to hash:", p, f) } fchan <- f return skip } if info.Mode().IsDir() { if w.CurrentFiler != nil { // A directory is "unchanged", if it // - exists // - has the same permissions as previously, unless we are ignoring permissions // - was not marked deleted (since it apparently exists now) // - was a directory previously (not a file or something else) // - was not a symlink (since it's a directory now) // - was not invalid (since it looks valid now) cf, ok = w.CurrentFiler.CurrentFile(rn) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, uint32(info.Mode())) if ok && permUnchanged && !cf.IsDeleted() && cf.IsDirectory() && !cf.IsSymlink() && !cf.IsInvalid() { return nil } } flags := uint32(protocol.FlagDirectory) if w.IgnorePerms { flags |= protocol.FlagNoPermBits | 0777 } else { flags |= uint32(info.Mode() & maskModePerm) } f := protocol.FileInfo{ Name: rn, Version: cf.Version.Update(w.ShortID), Flags: flags, Modified: mtime.Unix(), } if debug { l.Debugln("dir:", p, f) } fchan <- f return nil } if info.Mode().IsRegular() { curMode := uint32(info.Mode()) if runtime.GOOS == "windows" && osutil.IsWindowsExecutable(rn) { curMode |= 0111 } if w.CurrentFiler != nil { // A file is "unchanged", if it // - exists // - has the same permissions as previously, unless we are ignoring permissions // - was not marked deleted (since it apparently exists now) // - had the same modification time as it has now // - was not a directory previously (since it's a file now) // - was not a symlink (since it's a file now) // - was not invalid (since it looks valid now) // - has the same size as previously cf, ok = w.CurrentFiler.CurrentFile(rn) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, curMode) if ok && permUnchanged && !cf.IsDeleted() && cf.Modified == mtime.Unix() && !cf.IsDirectory() && !cf.IsSymlink() && !cf.IsInvalid() && cf.Size() == info.Size() { return nil } if debug { l.Debugln("rescan:", cf, mtime.Unix(), info.Mode()&os.ModePerm) } } var flags = curMode & uint32(maskModePerm) if w.IgnorePerms { flags = protocol.FlagNoPermBits | 0666 } f := protocol.FileInfo{ Name: rn, Version: cf.Version.Update(w.ShortID), Flags: flags, Modified: mtime.Unix(), } if debug { l.Debugln("to hash:", p, f) } fchan <- f } return nil } }
func (w *Walker) walkAndHashFiles(fchan chan protocol.FileInfo) filepath.WalkFunc { return func(p string, info os.FileInfo, err error) error { if err != nil { if debug { l.Debugln("error:", p, info, err) } return nil } rn, err := filepath.Rel(w.Dir, p) if err != nil { if debug { l.Debugln("rel error:", p, err) } return nil } if rn == "." { return nil } if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) { // A temporary file if debug { l.Debugln("temporary:", rn) } return nil } if sn := filepath.Base(rn); sn == ".stignore" || sn == ".stfolder" || strings.HasPrefix(rn, ".stversions") || (w.Matcher != nil && w.Matcher.Match(rn)) { // An ignored file if debug { l.Debugln("ignored:", rn) } if info.IsDir() { return filepath.SkipDir } return nil } if (runtime.GOOS == "linux" || runtime.GOOS == "windows") && !norm.NFC.IsNormalString(rn) { l.Warnf("File %q contains non-NFC UTF-8 sequences and cannot be synced. Consider renaming.", rn) return nil } // We must perform this check, as symlinks on Windows are always // .IsRegular or .IsDir unlike on Unix. // Index wise symlinks are always files, regardless of what the target // is, because symlinks carry their target path as their content. isSymlink, _ := symlinks.IsSymlink(p) if isSymlink { var rval error // If the target is a directory, do NOT descend down there. // This will cause files to get tracked, and removing the symlink // will as a result remove files in their real location. // But do not SkipDir if the target is not a directory, as it will // stop scanning the current directory. if info.IsDir() { rval = filepath.SkipDir } // We always rehash symlinks as they have no modtime or // permissions. // We check if they point to the old target by checking that // their existing blocks match with the blocks in the index. // If we don't have a filer or don't support symlinks, skip. if w.CurrentFiler == nil || !symlinks.Supported { return rval } target, flags, err := symlinks.Read(p) flags = flags & protocol.SymlinkTypeMask if err != nil { if debug { l.Debugln("readlink error:", p, err) } return rval } blocks, err := Blocks(strings.NewReader(target), w.BlockSize, 0) if err != nil { if debug { l.Debugln("hash link error:", p, err) } return rval } cf := w.CurrentFiler.CurrentFile(rn) if !cf.IsDeleted() && cf.IsSymlink() && SymlinkTypeEqual(flags, cf.Flags) && BlocksEqual(cf.Blocks, blocks) { return rval } f := protocol.FileInfo{ Name: rn, Version: lamport.Default.Tick(0), Flags: protocol.FlagSymlink | flags | protocol.FlagNoPermBits | 0666, Modified: 0, Blocks: blocks, } if debug { l.Debugln("symlink to hash:", p, f) } fchan <- f return rval } if info.Mode().IsDir() { if w.CurrentFiler != nil { cf := w.CurrentFiler.CurrentFile(rn) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, uint32(info.Mode())) if !cf.IsDeleted() && cf.IsDirectory() && permUnchanged { return nil } } var flags uint32 = protocol.FlagDirectory if w.IgnorePerms { flags |= protocol.FlagNoPermBits | 0777 } else { flags |= uint32(info.Mode() & os.ModePerm) } f := protocol.FileInfo{ Name: rn, Version: lamport.Default.Tick(0), Flags: flags, Modified: info.ModTime().Unix(), } if debug { l.Debugln("dir:", p, f) } fchan <- f return nil } if info.Mode().IsRegular() { if w.CurrentFiler != nil { cf := w.CurrentFiler.CurrentFile(rn) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, uint32(info.Mode())) if !cf.IsDeleted() && cf.Modified == info.ModTime().Unix() && permUnchanged { return nil } if debug { l.Debugln("rescan:", cf, info.ModTime().Unix(), info.Mode()&os.ModePerm) } } var flags = uint32(info.Mode() & os.ModePerm) if w.IgnorePerms { flags = protocol.FlagNoPermBits | 0666 } f := protocol.FileInfo{ Name: rn, Version: lamport.Default.Tick(0), Flags: flags, Modified: info.ModTime().Unix(), } if debug { l.Debugln("to hash:", p, f) } fchan <- f } return nil } }
func (w *Walker) walkAndHashFiles(fchan chan protocol.FileInfo) filepath.WalkFunc { now := time.Now() return func(p string, info os.FileInfo, err error) error { if err != nil { if debug { l.Debugln("error:", p, info, err) } return nil } rn, err := filepath.Rel(w.Dir, p) if err != nil { if debug { l.Debugln("rel error:", p, err) } return nil } if rn == "." { return nil } if w.TempNamer != nil && w.TempNamer.IsTemporary(rn) { // A temporary file if debug { l.Debugln("temporary:", rn) } if info.Mode().IsRegular() && info.ModTime().Add(w.TempLifetime).Before(now) { os.Remove(p) if debug { l.Debugln("removing temporary:", rn, info.ModTime()) } } return nil } if sn := filepath.Base(rn); sn == ".stignore" || sn == ".stfolder" || strings.HasPrefix(rn, ".stversions") || (w.Matcher != nil && w.Matcher.Match(rn)) { // An ignored file if debug { l.Debugln("ignored:", rn) } if info.IsDir() { return filepath.SkipDir } return nil } if (runtime.GOOS == "linux" || runtime.GOOS == "windows") && !norm.NFC.IsNormalString(rn) { l.Warnf("File %q contains non-NFC UTF-8 sequences and cannot be synced. Consider renaming.", rn) return nil } // Index wise symlinks are always files, regardless of what the target // is, because symlinks carry their target path as their content. if info.Mode()&os.ModeSymlink == os.ModeSymlink { var rval error // If the target is a directory, do NOT descend down there. This // will cause files to get tracked, and removing the symlink will // as a result remove files in their real location. But do not // SkipDir if the target is not a directory, as it will stop // scanning the current directory. if info.IsDir() { rval = filepath.SkipDir } // If we don't support symlinks, skip. if !symlinks.Supported { return rval } // We always rehash symlinks as they have no modtime or // permissions. We check if they point to the old target by // checking that their existing blocks match with the blocks in // the index. target, flags, err := symlinks.Read(p) flags = flags & protocol.SymlinkTypeMask if err != nil { if debug { l.Debugln("readlink error:", p, err) } return rval } blocks, err := Blocks(strings.NewReader(target), w.BlockSize, 0) if err != nil { if debug { l.Debugln("hash link error:", p, err) } return rval } if w.CurrentFiler != nil { // A symlink is "unchanged", if // - it exists // - it wasn't deleted (because it isn't now) // - it was a symlink // - it wasn't invalid // - the symlink type (file/dir) was the same // - the block list (i.e. hash of target) was the same cf, ok := w.CurrentFiler.CurrentFile(rn) if ok && !cf.IsDeleted() && cf.IsSymlink() && !cf.IsInvalid() && SymlinkTypeEqual(flags, cf.Flags) && BlocksEqual(cf.Blocks, blocks) { return rval } } f := protocol.FileInfo{ Name: rn, Version: lamport.Default.Tick(0), Flags: protocol.FlagSymlink | flags | protocol.FlagNoPermBits | 0666, Modified: 0, Blocks: blocks, } if debug { l.Debugln("symlink to hash:", p, f) } fchan <- f return rval } if info.Mode().IsDir() { if w.CurrentFiler != nil { // A directory is "unchanged", if it // - exists // - has the same permissions as previously, unless we are ignoring permissions // - was not marked deleted (since it apparently exists now) // - was a directory previously (not a file or something else) // - was not a symlink (since it's a directory now) // - was not invalid (since it looks valid now) cf, ok := w.CurrentFiler.CurrentFile(rn) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, uint32(info.Mode())) if ok && permUnchanged && !cf.IsDeleted() && cf.IsDirectory() && !cf.IsSymlink() && !cf.IsInvalid() { return nil } } flags := uint32(protocol.FlagDirectory) if w.IgnorePerms { flags |= protocol.FlagNoPermBits | 0777 } else { flags |= uint32(info.Mode() & os.ModePerm) } f := protocol.FileInfo{ Name: rn, Version: lamport.Default.Tick(0), Flags: flags, Modified: info.ModTime().Unix(), } if debug { l.Debugln("dir:", p, f) } fchan <- f return nil } if info.Mode().IsRegular() { if w.CurrentFiler != nil { // A file is "unchanged", if it // - exists // - has the same permissions as previously, unless we are ignoring permissions // - was not marked deleted (since it apparently exists now) // - had the same modification time as it has now // - was not a directory previously (since it's a file now) // - was not a symlink (since it's a file now) // - was not invalid (since it looks valid now) // - has the same size as previously cf, ok := w.CurrentFiler.CurrentFile(rn) permUnchanged := w.IgnorePerms || !cf.HasPermissionBits() || PermsEqual(cf.Flags, uint32(info.Mode())) if ok && permUnchanged && !cf.IsDeleted() && cf.Modified == info.ModTime().Unix() && !cf.IsDirectory() && !cf.IsSymlink() && !cf.IsInvalid() && cf.Size() == info.Size() { return nil } if debug { l.Debugln("rescan:", cf, info.ModTime().Unix(), info.Mode()&os.ModePerm) } } var flags = uint32(info.Mode() & os.ModePerm) if w.IgnorePerms { flags = protocol.FlagNoPermBits | 0666 } f := protocol.FileInfo{ Name: rn, Version: lamport.Default.Tick(0), Flags: flags, Modified: info.ModTime().Unix(), } if debug { l.Debugln("to hash:", p, f) } fchan <- f } return nil } }