dir

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package dir provides support for folder operations within the 0g storage client. Since the 0g storage node does not natively support folder operations, this package introduces a way to represent and manage hierarchical directory structures, including encoding, decoding, and comparing directories and files. The folder structure is serialized into a binary format that includes a JSON representation of the data, which can be stored on and retrieved from the 0g storage node.

The main features of this package include:

  • Defining the FsNode structure, which models files and directories in a nested, hierarchical format.
  • Serializing an FsNode structure into a binary format that includes a JSON representation, suitable for storage on a 0g storage node.
  • Deserializing the binary format back into an FsNode structure for further manipulation and operations.
  • Supporting the comparison of two directory structures to identify differences such as added, removed, or modified files.

This package enables the 0g storage client to manage complex directory structures using an efficient and extensible data format, facilitating advanced file system operations beyond single file storage.

Index

Constants

This section is empty.

Variables

View Source
var (
	CodecVersion    = uint16(1)
	CodecMagicBytes = crypto.Keccak256([]byte("0g-storage-client-dir-codec"))
)

Functions

func PrettyPrint

func PrettyPrint(root *DiffNode)

PrettyPrint prints the DiffNode tree in a human-readable format with a tree skeleton structure.

Types

type DiffNode

type DiffNode struct {
	Node    *FsNode                  // The original node
	Status  DiffStatus               // Diff status of the node
	Entries *btree.BTreeG[*DiffNode] // Directory entries as a B-Tree
}

DiffNode represents a node in the diff structure with its status.

func Diff

func Diff(current, next *FsNode) (*DiffNode, error)

Diff compares two directories and returns a DiffNode tree with the differences.

func NewDiffNode

func NewDiffNode(node *FsNode, status DiffStatus) *DiffNode

NewDiffNode creates a new DiffNode.

type DiffStatus

type DiffStatus string

DiffStatus represents the status of a node in the diff.

const (
	DiffStatusAdded     DiffStatus = "added"
	DiffStatusRemoved   DiffStatus = "removed"
	DiffStatusModified  DiffStatus = "modified"
	DiffStatusUnchanged DiffStatus = "unchanged"
)

type FileType

type FileType string

FileType represents the file type in the FsNode structure.

const (
	FileTypeFile      FileType = "file"
	FileTypeDirectory FileType = "directory"
	FileTypeSymbolic  FileType = "symbolic"
)

type FsNode

type FsNode struct {
	Name    string    `json:"name"`              // File or directory name
	Type    FileType  `json:"type"`              // File type of the node
	Root    string    `json:"hash,omitempty"`    // Merkle root hash (only for regular files)
	Size    int64     `json:"size,omitempty"`    // File size in bytes (only for regular files)
	Link    string    `json:"link,omitempty"`    // Symbolic link target (only for symbolic links)
	Entries []*FsNode `json:"entries,omitempty"` // Directory entries (only for directories)
}

FsNode represents a node in the filesystem hierarchy.

func BuildFileTree

func BuildFileTree(path string) (*FsNode, error)

BuildFileTree recursively builds a file tree for the specified directory.

func NewDirFsNode

func NewDirFsNode(name string, entryNodes []*FsNode) *FsNode

NewDirFsNode creates a new FsNode representing a directory.

func NewFileFsNode

func NewFileFsNode(name string, rootHash common.Hash, size int64) *FsNode

NewFileFsNode creates a new FsNode representing a regular file.

func NewSymbolicFsNode

func NewSymbolicFsNode(name, link string) *FsNode

NewSymbolicFsNode creates a new FsNode representing a symbolic link.

func (*FsNode) Equal

func (node *FsNode) Equal(rhs *FsNode) bool

Equal compares two FsNode structures for equality.

func (*FsNode) Flatten

func (node *FsNode) Flatten(filterFunc ...func(*FsNode) bool) (result []*FsNode, relpaths []string)

Flatten recursively flattens the FsNode tree into a slice of FsNode pointers and a slice of relative paths. The filterFunc is applied to each node to determine if it should be included in the result.

func (*FsNode) Locate

func (node *FsNode) Locate(path string) (*FsNode, error)

Locate finds a sub-node within the FsNode tree based on the given path. The path can be a file or directory, and it should be relative to the current node.

func (*FsNode) MarshalBinary

func (node *FsNode) MarshalBinary() ([]byte, error)

MarshalBinary implements the encoding.BinaryMarshaler interface. It encodes the FsNode into a binary format.

func (*FsNode) Search

func (node *FsNode) Search(fileName string) (*FsNode, bool)

Search looks for a file by name in the current directory node's entries.

func (*FsNode) Traverse

func (node *FsNode) Traverse(actionFunc func(node *FsNode, relativePath string) error) error

Traverse recursively traverses the FsNode tree and applies the provided actionFunc to each node. This method only requires the user to handle relative paths.

Parameters:

  • actionFunc: A function that defines the action to perform on each node. The function takes the current node and its relative path as arguments. This function can perform any necessary operations, such as collecting nodes, uploading files, or logging information.

func (*FsNode) UnmarshalBinary

func (node *FsNode) UnmarshalBinary(data []byte) error

UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. It decodes the FsNode from a binary format.

Jump to

Keyboard shortcuts

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