fs

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package fs provides utility functions for directory operations, such as listing files with a specific extension.

Package fs provides utility functions for secure file operations, including path validation and extension whitelisting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListFiles

func ListFiles(ctx context.Context, dirPath, ext string) ([]string, error)

ListFiles returns absolute file paths under dirPath whose extension matches ext.

Parameters:

  • dirPath: the directory to search
  • ext: the file extension to match (e.g., ".json")

Returns:

  • []string: absolute file paths matching the extension
  • error: if the directory cannot be read

func SafeReadFile

func SafeReadFile(path string, baseDir string, allowExt map[string]struct{}) ([]byte, error)

SafeReadFile reads a file from disk securely.

It enforces: - Path cleaning (removes ../ etc.) - Absolute path resolution - Ensures the file is within the trusted baseDir - Only allows files with extensions in allowExt (e.g. {".json":{}, ".yaml":{}})

Parameters:

  • path: the file path to read
  • baseDir: the trusted base directory
  • allowExt: a set of allowed file extensions

Returns:

  • file contents as []byte
  • error if the file cannot be read or does not meet security checks

Types

This section is empty.

Jump to

Keyboard shortcuts

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