Repository: syncthing/syncthing
File: lib/scanner/walk.go (Go)

Code before:
		// We don't want to use the standard syncthing prefix here, as that will result in the file being ignored
		// and eventually deleted by Syncthing if the rename back fails.

		tempPath := fs.TempNameWithPrefix(normPath, "")
		if err = w.Filesystem.Rename(path, tempPath); err != nil {
			l.Infof(`Error during normalizing UTF8 encoding of file "%s" (renamed to "%s"): %v`, path, tempPath, err)
			return "", true
		}
		if err = w.Filesystem.Rename(tempPath, normPath); err != nil {
			// I don't ever expect this to happen, but if it does, we should probably tell our caller that the normalized
			// path is the temp path: that way at least the user's data still gets synced.
			l.Warnf(`Error renaming "%s" to "%s" while normalizating UTF8 encoding: %v. You will want to rename this file back manually`, tempPath, normPath, err)
			return tempPath, false
		}
	} else {
		// There is something already in the way at the normalized
		// file name.
		l.Infof(`File "%s" path has UTF8 encoding conflict with another file; ignoring.`, path)
		return "", true
	}

	return normPath, false
}

// updateFileInfo updates walker specific members of protocol.FileInfo
func (w *walker) updateFileInfo(file, curFile protocol.FileInfo, hasCurFile bool) protocol.FileInfo {
	if hasCurFile && file.Type == protocol.FileInfoTypeFile && runtime.GOOS == "windows" {
		// If we have an existing index entry, copy the executable bits
		// from there.
		file.Permissions |= (curFile.Permissions & 0111)
	}
	file.Version = curFile.Version.Update(w.ShortID)
	file.ModifiedBy = w.ShortID
	file.LocalFlags = w.LocalFlags
	return file
}

// A byteCounter gets bytes added to it via Update() and then provides the
// Total() and one minute moving average Rate() in bytes per second.
type byteCounter struct {
	total int64
	metrics.EWMA
	stop chan struct{}
}

func newByteCounter() *byteCounter {
	c := &byteCounter{
		EWMA: metrics.NewEWMA1(), // a one minute exponentially weighted moving average
		stop: make(chan struct{}),
	}
	go c.ticker()

Review comment (security):
No need to pass the `hasCurFile` really. If we didn't have it, the permissions on the passed `curFile` would be zero and the `|` would be a no-op. But it doesn't matter greatly either way.

Code after:
		// We don't want to use the standard syncthing prefix here, as that will result in the file being ignored
		// and eventually deleted by Syncthing if the rename back fails.

		tempPath := fs.TempNameWithPrefix(normPath, "")
		if err = w.Filesystem.Rename(path, tempPath); err != nil {
			l.Infof(`Error during normalizing UTF8 encoding of file "%s" (renamed to "%s"): %v`, path, tempPath, err)
			return "", true
		}
		if err = w.Filesystem.Rename(tempPath, normPath); err != nil {
			// I don't ever expect this to happen, but if it does, we should probably tell our caller that the normalized
			// path is the temp path: that way at least the user's data still gets synced.
			l.Warnf(`Error renaming "%s" to "%s" while normalizating UTF8 encoding: %v. You will want to rename this file back manually`, tempPath, normPath, err)
			return tempPath, false
		}
	} else {
		// There is something already in the way at the normalized
		// file name.
		l.Infof(`File "%s" path has UTF8 encoding conflict with another file; ignoring.`, path)
		return "", true
	}

	return normPath, false
}

// updateFileInfo updates walker specific members of protocol.FileInfo that do not depend on type
func (w *walker) updateFileInfo(file, curFile protocol.FileInfo) protocol.FileInfo {
	if file.Type == protocol.FileInfoTypeFile && runtime.GOOS == "windows" {
		// If we have an existing index entry, copy the executable bits
		// from there.
		file.Permissions |= (curFile.Permissions & 0111)
	}
	file.Version = curFile.Version.Update(w.ShortID)
	file.ModifiedBy = w.ShortID
	file.LocalFlags = w.LocalFlags
	return file
}

// A byteCounter gets bytes added to it via Update() and then provides the
// Total() and one minute moving average Rate() in bytes per second.
type byteCounter struct {
	total int64
	metrics.EWMA
	stop chan struct{}
}

func newByteCounter() *byteCounter {
	c := &byteCounter{
		EWMA: metrics.NewEWMA1(), // a one minute exponentially weighted moving average
		stop: make(chan struct{}),
	}
	go c.ticker()