Documentation
¶
Index ¶
- func CalSha256AndSize(absPath string, sizeToRead int64) (string, int64, error)
- func CheckNetworkBlackList(server string, blacklist []string) bool
- func CheckReadPath(path string) bool
- func DeleteDir(dir string) bool
- func FloatSecFromTime(t time.Time) float64
- func GetAllFilePaths(root string, options *SymWalkOptions) ([]string, error)
- func GetFileSize(filepath string) (int64, error)
- func GetFileSizeNoFollow(filepath string) (int64, error)
- func GetParentFolder(path string) string
- func GetRealFileInfo(path string) (string, os.FileInfo, error)
- func GetStringOrDefault(defaultStr string, strs ...string) string
- func IsSymlink(fi os.FileInfo) bool
- func IsValidDeviceID(deviceID string) bool
- func NormalizeFloatTimestamp(timestamp float64) (int64, int32)
- func ParseYAML(path string, v interface{}) error
- func SymWalk(root string, walkFn filepath.WalkFunc, options *SymWalkOptions) error
- func TimeFromFloat(timestamp float64) time.Time
- type SymWalkOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalSha256AndSize ¶
func CheckNetworkBlackList ¶ added in v1.1.9
func CheckReadPath ¶
CheckReadPath checks if a path is readable. Uses Lstat for better performance and then Access to check actual readability.
func FloatSecFromTime ¶
FloatSecFromTime converts a time.Time instance to a floating-point timestamp in seconds.
func GetAllFilePaths ¶ added in v1.1.6
func GetAllFilePaths(root string, options *SymWalkOptions) ([]string, error)
GetAllFilePaths walks the file tree rooted at root and returns a slice of absolute paths for all files (not directories) found. It uses the same symbolic link handling and permission error handling as SymWalk. By default, empty files (size 0) are skipped through SymWalk's SkipEmptyFiles option.
root: the starting path for traversal options: configuration options, if nil, DefaultSymWalkOptions() is used Returns: slice of absolute file paths and any error encountered.
Example (CustomOptions) ¶
Example usage of GetAllFilePaths with custom options.
// Configure options to not follow symlinks and skip permission errors
options := &SymWalkOptions{
FollowSymlinks: false,
SkipPermissionErrors: true,
}
paths, err := GetAllFilePaths(".", options)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
// Filter for specific file types
var goFiles []string
for _, path := range paths {
if filepath.Ext(path) == ".go" {
goFiles = append(goFiles, path)
}
}
fmt.Printf("Found %d Go files:\n", len(goFiles))
for _, path := range goFiles {
fmt.Printf("- %s\n", filepath.Base(path))
}
Output: Found 11 Go files: - conf.go - file.go - file_test.go - net.go - net_test.go - symwalk.go - symwalk_test.go - timestamp.go - timestamp_test.go - utils.go - utils_test.go
func GetFileSize ¶
GetFileSize returns the size of the file at the given path. For symbolic links, it follows the link and returns the target file size.
func GetFileSizeNoFollow ¶ added in v1.1.6
GetFileSizeNoFollow returns the size of the file at the given path. For symbolic links, it returns the size of the link itself, not the target.
func GetParentFolder ¶
func GetRealFileInfo ¶ added in v1.1.10
func GetStringOrDefault ¶ added in v1.1.1
func IsValidDeviceID ¶ added in v1.1.8
IsValidDeviceID validates that a device ID contains only alphanumeric characters, numbers and allowed symbols. Allowed characters: letters (a-z, A-Z), numbers (0-9), and symbols. Device ID must be between 1 and 64 characters long.
func NormalizeFloatTimestamp ¶
NormalizeFloatTimestamp normalizes a floating-point timestamp to seconds and nanoseconds. keep the precision of the timestamp with microseconds precision.
func ParseYAML ¶ added in v1.1.6
ParseYAML Parse load koanf config from specified local path into v.
func SymWalk ¶ added in v1.1.6
func SymWalk(root string, walkFn filepath.WalkFunc, options *SymWalkOptions) error
SymWalk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. It supports following symbolic links safely by preventing infinite loops. The files are walked in lexical order, which makes the output deterministic. By default, empty files (size 0) are skipped to save processing time and memory.
This implementation uses os.DirEntry to reduce system calls and improve performance.
root: the starting path for traversal walkFn: the function called for each file or directory (uses standard filepath.WalkFunc) options: configuration options, if nil, DefaultSymWalkOptions() is used
Example (Custom) ¶
Example: custom configuration.
// Custom configuration
options := &SymWalkOptions{
FollowSymlinks: false, // don't follow symlinks
SkipPermissionErrors: true, // skip permission errors
}
err := SymWalk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil //nolint:nilerr // skip errors in example
}
// only process .txt files
if !info.IsDir() && filepath.Ext(path) == ".txt" {
fmt.Printf("Found text file: %s\n", path)
}
return nil
}, options)
if err != nil {
fmt.Printf("Traversal failed: %v\n", err)
}
Example (FindFiles) ¶
Real-world usage example: find specific files.
var foundFiles []string
// Find all .go files
err := SymWalk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil //nolint:nilerr // skip errors in example
}
if !info.IsDir() && filepath.Ext(path) == ".go" {
foundFiles = append(foundFiles, path)
}
return nil
}, DefaultSymWalkOptions())
if err != nil {
fmt.Printf("Search failed: %v\n", err)
return
}
// Sort for consistent output
sort.Strings(foundFiles)
fmt.Printf("Found %d Go files\n", len(foundFiles))
for _, file := range foundFiles {
fmt.Printf("- %s\n", file)
}
Output: Found 11 Go files - conf.go - file.go - file_test.go - net.go - net_test.go - symwalk.go - symwalk_test.go - timestamp.go - timestamp_test.go - utils.go - utils_test.go
func TimeFromFloat ¶
TimeFromFloat converts a floating-point timestamp to a time.Time instance.
Types ¶
type SymWalkOptions ¶ added in v1.1.6
type SymWalkOptions struct {
// FollowSymlinks when true, follows symbolic links. When false, treats symlinks as regular files.
FollowSymlinks bool
// SkipPermissionErrors when true, skips directories/files that cannot be accessed due to permission errors
// instead of returning an error.
SkipPermissionErrors bool
// MaxFiles limits the maximum number of files to collect in GetAllFilePaths.
// When set to 0, no limit is applied. Default is 10000.
// This helps prevent memory issues when traversing large directory structures
// or when symbolic links point to root directories.
MaxFiles int
// SkipEmptyFiles when true, skips files with size 0 to save processing time and memory.
// This is useful when empty files are not relevant for the use case.
SkipEmptyFiles bool
}
SymWalkOptions contains simple configuration options for SymWalk.
func DefaultSymWalkOptions ¶ added in v1.1.6
func DefaultSymWalkOptions() *SymWalkOptions
DefaultSymWalkOptions returns a default configuration.