dtree

package module
v0.0.0-...-d7652b8 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: MIT Imports: 9 Imported by: 1

README

go-dtree

Go library for creating an in-memory copy of a directory tree.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("file not found")
)

Functions

This section is empty.

Types

type EqualSizes

type EqualSizes struct {
	Count int
	Files []*Node
}

EqualSizes represents a collection of files that share the same size.

type FileInfo

type FileInfo struct {
	Name      string      `json:"name"`
	Size      int64       `json:"size"`
	IsDir     bool        `json:"is_dir"`
	Mode      os.FileMode `json:"mode"`
	ModTime   time.Time   `json:"mod_time"`
	Extension string      `json:"ext"`
}

FileInfo is a struct created from os.FileInfo interface for serialization.

func FileInfoFromInterface

func FileInfoFromInterface(v os.FileInfo) *FileInfo

FileInfoFromInterface is a helper function to create a local FileInfo struct from os.FileInfo interface.

func GetFileInfo

func GetFileInfo(filePath string) (*FileInfo, error)

GetFileInfo retrieves file information for the file at the given path and returns it as a FileInfo struct.

type Node

type Node struct {
	FullPath string    `json:"path"`
	Info     *FileInfo `json:"info"`
	Children []*Node   `json:"children"`
	Parent   *Node     `json:"-"`
}

Node represents a node in a directory tree.

func BuildFileTree

func BuildFileTree(parents map[string]*Node) *Node

BuildFileTree constructs a hierarchical file tree from a map of nodes, identifying the root and linking children. It associates parent nodes with their children and sorts the tree nodes recursively. Panics if no root node is identified.

func Collect

func Collect(root string) (*Node, error)

Collect traverses a directory structure starting at the given root path and constructs a hierarchical representation. Returns the root node of the constructed file tree or an error if traversal fails.

func (*Node) EqualSizes

func (node *Node) EqualSizes() map[int64]*EqualSizes

EqualSizes returns a map of files with the same size.

func (*Node) GetBiggest

func (node *Node) GetBiggest(biggest *Node) *Node

GetBiggest traverses the node hierarchy to find the file with the largest size. Returns the largest file encountered.

func (*Node) GetFile

func (node *Node) GetFile(name string) (*Node, error)

GetFile returns a single file by its name.

func (*Node) GetFileByAbsolutePath

func (node *Node) GetFileByAbsolutePath(path string) (*Node, error)

GetFileByAbsolutePath returns a single file by its absolute path.

func (*Node) GetFileByPath

func (node *Node) GetFileByPath(filePath string) (*Node, error)

GetFileByPath returns a single file by its path.

func (*Node) GetFileByPatternAndExt

func (node *Node) GetFileByPatternAndExt(pattern string, ext string) (*Node, error)

GetFileByPatternAndExt returns the first match for a given pattern and extension.

func (*Node) GetFileBySizeAndExt

func (node *Node) GetFileBySizeAndExt(size int64, ext string, exclude ...string) (*Node, error)

GetFileBySizeAndExt returns a single file by its size and extension.

func (*Node) GetFiles

func (node *Node) GetFiles(ext ...string) []*Node

GetFiles returns all files or only files with a given extension. The extension must include the dot, e.g. ".mkv".

func (*Node) Remove

func (node *Node) Remove() error

Remove removes a file

func (*Node) UpdateFileInfo

func (node *Node) UpdateFileInfo() error

UpdateFileInfo updates the node's FileInfo by retrieving and parsing the file metadata of its associated path.

Jump to

Keyboard shortcuts

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