files

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const OwnerWritePerm = os.FileMode(0o755)

OwnerWritePerm provides 0755 permission.

Variables

This section is empty.

Functions

func ClearDir

func ClearDir(dir string) error

ClearDir removes all contents of the specified directory while preserving the directory itself. It traverses the directory recursively, deleting all files, subdirectories, and symbolic links.

Parameters:

  • dir - string path to the directory to be cleared

Returns:

  • error - nil on success, or any error encountered during the operation

Behavior details:

  • Preserves the original directory (only removes its contents)
  • Handles nested directory structures recursively
  • Follows symbolic links when deleting (removes link targets)
  • Stops and returns on the first error encountered
  • Returns nil if directory doesn't exist (consistent with os.RemoveAll)

Example usage:

err := ClearDir("/tmp/workdir")
if err != nil {
    log.Fatal("Failed to clear directory:", err)
}

Warning:

  • This is a destructive operation - deleted files cannot be recovered
  • The function will remove ALL contents without confirmation
  • Ensure proper permissions exist for all files/subdirectories.

func CreateAndOpenFile

func CreateAndOpenFile(path string, fileName string, perms ...os.FileMode) (io.Writer, error)

CreateAndOpenFile creates file and open it foe recording.

func FileCopy

func FileCopy(src string, destination string, perms ...os.FileMode) error

FileCopy copies src file to destination path.

func FileExists

func FileExists(filename string) bool

FileExists checks if a file exists and is not a directory before we try using it to prevent further errors.

func GetAbsPath

func GetAbsPath(path, defaultPath string) (string, error)

GetAbsPath returns an absolute path based on the input path and a default path. It handles three cases for the input path:

  1. If the path is already absolute (starts with "/"), home-relative (starts with "~/"), or relative to current directory (starts with "./"), it returns the path as-is.
  2. If the path is empty, it uses the defaultPath instead.
  3. For all other cases, it treats the path as relative to the executable's directory.

Parameters:

  • path: The input path to process (can be empty, absolute, or relative)
  • defaultPath: The default path to use if input path is empty

Returns:

  • string: The resulting absolute path
  • error: Any error that occurred while getting the executable's directory

Example usage:

absPath, err := GetAbsPath("config.json", "/etc/default/config.json")
// Returns "/path/to/executable/config.json" if no error

func GetDirNamesInFolder

func GetDirNamesInFolder(path string) ([]string, error)

GetDirNamesInFolder returns slice with directory names in path.

func GetFileNamesInFolder

func GetFileNamesInFolder(path string) ([]string, error)

GetFileNamesInFolder returns slice with file names in path.

func MkdirAll

func MkdirAll(path string, perm ...os.FileMode) error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates.

func ReadBinFile

func ReadBinFile(path string, name string) ([]byte, error)

ReadBinFile reads file as slice of bytes.

func ReadStringFile

func ReadStringFile(path string, name string) (string, error)

ReadStringFile reads file as string.

func StatTimes

func StatTimes(name string) (atime, mtime, ctime time.Time, err error)

StatTimes gets file stats info.

func WriteFileString

func WriteFileString(path string, name string, value string) error

WriteFileString writes string content to text file.

Types

This section is empty.

Jump to

Keyboard shortcuts

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