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

Code before:
		}

		if !utf8.ValidString(path) {
			l.Warnf("File name %q is not in UTF8 encoding; skipping.", path)
			return skip
		}

		if w.Matcher.Match(path).IsIgnored() {
			// If the parent wasn't ignored already, set this path as the "highest" ignored parent
			if ignoredParent == "" || !strings.HasPrefix(path, ignoredParent+string(fs.PathSeparator)) {
				ignoredParent = path
			}
			l.Debugln("ignored (patterns):", path)
			if !w.Matcher.SkipIgnoredDirs() {
				return nil
			}
			return skip
		}
		if ignoredParent != "" {
			// Add parent directories of the current, not ignored path if needed
			if rel := strings.TrimPrefix(path, ignoredParent+string(fs.PathSeparator)); rel != path {
				for _, name := range strings.Split(filepath.Base(rel), string(fs.PathSeparator)) {
					if err = handleItem(ignoredParent); err != nil {
						return err
					}
					ignoredParent = filepath.Join(ignoredParent, name)
				}
			}
			ignoredParent = ""
		}

		return handleItem(path)
	}
}

func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileInfo, fchan chan protocol.FileInfo) error {
	curMode := uint32(info.Mode())
	if runtime.GOOS == "windows" && osutil.IsWindowsExecutable(relPath) {
		curMode |= 0111
	}

	cf, ok := w.CurrentFiler.CurrentFile(relPath)

	f := protocol.FileInfo{
		Name:          relPath,
		Type:          protocol.FileInfoTypeFile,
		Version:       cf.Version.Update(w.ShortID),
		Permissions:   curMode & uint32(maskModePerm),
		NoPermissions: w.IgnorePerms,
		ModifiedS:     info.ModTime().Unix(),
		ModifiedNs:    int32(info.ModTime().Nanosecond()),

Review comment (suggestion):
Like, how does this work. 

If I am inside a parent that is ignored (`a/b/c`), trim the prefix and get relative path to ignored parent (if I am `a/b/c/d` I get d), yet `path` is always the full path (`a/b/c/d`), so why are we comparing these?

Then we do some split on the filepath.Base, filepath base returns the last element after the last path separator, so what's the point of the split?

The we call ourselves with the parent?
Why can't this be handled by the walk itself, why do we have to help walk?

I am reading this but this makes no sense to me.



Code after:
		// The previously ignored parent directories of the current, not
		// ignored path need to be handled as well.
		if err = w.handleItem(ctx, ignoredParent, fchan, dchan, skip); err != nil {
			return err
		}
		for _, name := range strings.Split(rel, string(fs.PathSeparator)) {
			ignoredParent = filepath.Join(ignoredParent, name)
			if err = w.handleItem(ctx, ignoredParent, fchan, dchan, skip); err != nil {
				return err
			}
		}
		ignoredParent = ""

		return nil
	}
}

func (w *walker) handleItem(ctx context.Context, path string, fchan, dchan chan protocol.FileInfo, skip error) error {
	info, err := w.Filesystem.Lstat(path)
	// An error here would be weird as we've already gotten to this point, but act on it nonetheless
	if err != nil {
		return skip
	}

	path, shouldSkip := w.normalizePath(path, info)
	if shouldSkip {
		return skip
	}

	switch {
	case info.IsSymlink():
		if err := w.walkSymlink(ctx, path, dchan); err != nil {
			return err
		}
		if info.IsDir() {
			// under no circumstances shall we descend into a symlink
			return fs.SkipDir
		}
		return nil

	case info.IsDir():
		err = w.walkDir(ctx, path, info, dchan)

	case info.IsRegular():
		err = w.walkRegular(ctx, path, info, fchan)
	}

	return err
}

func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileInfo, fchan chan protocol.FileInfo) error {