walk

package module
v0.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: GPL-2.0 Imports: 10 Imported by: 3

README

What is this

This is a concurrent directory traversal library. It returns results via a channel that the caller must service. The caller can specify what entries are interesting:

  • Files
  • Directories
  • Special files (symlinks, device nodes etc.)
  • All of the above

It can optionally follow symlinks and detect mount-point crossings.

How can I use it?

Here is an example program:


    dirs := []string{"/etc", "/usr", "/bin", "/sbin", "/lib"}
    opt := walk.Options{
            OneFS: true,
            FollowSymlinks: true,
    }

    ch, errch := walk.Walk(dirs, walk.FILE, &opt)

    go func() {
        for err := range errch {
            fmt.Printf("walk: %s\n", err)
        }
    }()

    // harvest results
    for r := range ch {
        fmt.Printf("%s: %d bytes\n", r.Path, r.Stat.Size())
    }

Who's using this?

go-du is a simplified du(1) that uses this library. It sorts the output from largest size utilization to smaller ones.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(names []string, opt *Options) (chan Result, chan error)

Walk traverses the entries in 'names' in a concurrent fashion and returns results in a channel of Result. The caller must service the channel. Any errors encountered during the walk are returned in the error channel.

func WalkFunc added in v0.4.0

func WalkFunc(names []string, opt *Options, apply func(r Result) error) []error

WalkFunc traverses the entries in 'names' in a concurrent fashion and calls 'apply' for entries that match criteria in 'opt'. The apply function must be concurrency-safe ie it will be called concurrently from multiple go-routines. Any errors reported by 'apply' will be returned from WalkFunc().

Types

type Options

type Options struct {
	// Follow symlinks if set
	FollowSymlinks bool

	// stay within the same file-system
	OneFS bool

	// if set, return xattr for every returned result
	Xattr bool

	// Types of entries to return
	Type Type

	// Excludes is a list of shell-glob patterns to exclude from
	// the walk. If a dir matches the prefix, go-walk does
	// not descend that subdirectory.
	Excludes []string

	// Filter is an optional caller provided callback
	// This function must return True if this entry should
	// no longer be processed. ie filtered out.
	Filter func(nm string, fi os.FileInfo) bool
}

Options control the behavior of the filesystem walk

type Result

type Result struct {
	// path relative to the supplied argument
	Path string

	// stat(2) info
	Stat os.FileInfo

	// extended attributes for this file
	// set only if user requests it
	Xattr Xattr
}

Result is the data returned as part of the directory walk

type Type

type Type uint
const (
	FILE Type = 1 << iota
	DIR
	SYMLINK
	DEVICE
	SPECIAL

	// This is a short cut for "give me all entries"
	ALL = FILE | DIR | SYMLINK | DEVICE | SPECIAL
)

func (Type) String added in v0.5.1

func (t Type) String() string

Stringer for walk filter Type

type Xattr added in v0.6.0

type Xattr map[string]string

func (Xattr) String added in v0.6.0

func (x Xattr) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL