godirwalk – github.com/karrick/godirwalk Index | Files | Directories

package godirwalk

import "github.com/karrick/godirwalk"

Index

Functions

func WalkFileInfo

func WalkFileInfo(osDirname string, walkFn WalkFileInfoFunc) error

WalkFileInfo walks the file tree rooted at the specified directory, calling the specified callback function for each file system node in the tree, including root, symbolic links, and other node types. The nodes are walked in lexical order, which makes the output deterministic but means that for very large directories this function can be inefficient.

This function is often much faster than filepath.Walk because it does not need to os.Stat every node it encounters, but rather gets the file system node type when it reads the parent directory.

func WalkFileMode

func WalkFileMode(osDirname string, walkFn WalkFileModeFunc) error

WalkFileMode walks the file tree rooted at the specified directory, calling the specified callback function for each file system node in the tree, including root, symbolic links, and other node types. The nodes are walked in lexical order, which makes the output deterministic but means that for very large directories this function can be inefficient.

This function is often much faster than filepath.Walk because it does not need to os.Stat every node it encounters, but rather gets the file system node type when it reads the parent directory.

func WalkFileModeFollowSymlinks(osDirname string, walkFn WalkFileModeFunc) error

WalkFileModeFollowSymlinks walks the file tree rooted at the specified directory, calling the specified callback function for each file system node in the tree, including root, symbolic links, and other node types. The nodes are walked in lexical order, which makes the output deterministic but means that for very large directories this function can be inefficient.

This function is often much faster than filepath.Walk because it does not need to os.Stat every node it encounters, but rather gets the file system node type when it reads the parent directory.

This function also follows symbolic links that point to directories, and ought to be used with caution, as calling it may cause an infinite loop in cases where the file system includes a logical loop of symbolic links.

Types

type Dirent

type Dirent struct {
	// Name is the filename of the file system entry, relative to its parent.
	Name string

	// ModeType is the mode bits that specify the file system entry type. We
	// could make our own enum-like data type for encoding the file type, but
	// Go's runtime already gives us architecture independent file modes, as
	// discussed in `os/types.go`:
	//
	//    Go's runtime FileMode type has same definition on all systems, so
	//    that information about files can be moved from one system to
	//    another portably.
	ModeType os.FileMode
}

Dirent stores the name and file system mode type of discovered file system entries.

type Dirents

type Dirents []*Dirent

Dirents represents a slice of direntType pointers, which are sortable by name. This type satisfies the `sort.Sortable` interface.

func GetDirectoryEntries

func GetDirectoryEntries(osDirname string) (Dirents, error)

GetDirectoryEntries returns a slice of pointers to Dirent structures, representing the file system children of the specified directory. If the specified directory is a symbolic link, it will be resolved.

func GetDirectoryEntriesBuffer

func GetDirectoryEntriesBuffer(osDirname string, optionalScratchBuffer []byte) (Dirents, error)

GetDirectoryEntriesBuffer returns a slice of pointers to Dirent structures, representing the file system children of the specified directory. If the specified directory is a symbolic link, it will be resolved. If the optional scratch buffer is provided, it will be used to avoid excess allocations; otherwise, a one-time scratch buffer will be allocated and released when this function exits.

func (Dirents) Len

func (l Dirents) Len() int

Len returns the count of Dirent structures in the slice.

func (Dirents) Less

func (l Dirents) Less(i, j int) bool

Less returns true if and only if the Name of the element specified by the first index is lexicographically less than that of the second index.

func (Dirents) Swap

func (l Dirents) Swap(i, j int)

Swap exchanges the two Dirent entries specified by the two provided indexes.

type WalkFileInfoFunc

type WalkFileInfoFunc func(osPathname string, info os.FileInfo, err error) error

WalkFileInfoFunc is the type of the function called for each file system node visited by WalkFileInfo. The path argument contains the argument to WalkFileInfo as a prefix; that is, if WalkFileInfo is called with "dir", which is a directory containing the file "a", the walk function will be called with the argument "dir/a", using the correct os.PathSeparator for the Go Operating System architecture, GOOS. The info argument is the os.FileInfo for the named path.

If there was a problem walking to the file or directory named by path, the incoming error will describe the problem and the function can decide how to handle that error (and WalkFileInfo will not descend into that directory). If an error is returned, processing stops. The sole exception is when the function returns the special value filepath.SkipDir. If the function returns filepath.SkipDir when invoked on a directory, WalkFileInfo skips the directory's contents entirely. If the function returns filepath.SkipDir when invoked on a non-directory file system node, WalkFileInfo skips the remaining files in the containing directory.

type WalkFileModeFunc

type WalkFileModeFunc func(osPathname string, mode os.FileMode) error

WalkFileModeFunc is the type of the function called for each file system node visited by WalkFileMode. The path argument contains the argument to WalkFileMode as a prefix; that is, if WalkFileMode is called with "dir", which is a directory containing the file "a", the walk function will be called with the argument "dir/a", using the correct os.PathSeparator for the Go Operating System architecture, GOOS. The mode argument is the os.FileMode for the named path, masked to the bits that identify the file system node type, i.e., os.ModeType.

If an error is returned by the walk function, processing stops. The sole exception is when the function returns the special value filepath.SkipDir. If the function returns filepath.SkipDir when invoked on a directory, WalkFileMode skips the directory's contents entirely. If the function returns filepath.SkipDir when invoked on a non-directory file system node, WalkFileMode skips the remaining files in the containing directory.

Source Files

dirent.go info.go mode.go

Directories

PathSynopsis
examples
examples/walk-fast
examples/walk-stdlib
Version
v0.0.1
Published
Aug 22, 2017
Platform
js/wasm
Imports
9 packages
Last checked
3 days ago

Tools for package owners.