fs

package module
v0.0.0-...-1ec5e51 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 7 Imported by: 0

README

Go-Core / Filesystem

Aeon Digital
rianna@aeondigital.com.br

 

A robust, cross-platform filesystem utility package for Go applications. This module strictly enforces absolute path resolution for every internal operation, preventing working directory dependencies and ensuring predictable behavior across Windows, Linux, and macOS.

   


Features

  • Absolute Path Isolation: Automatically resolves relative paths, tilde prefixes (~), and dot markers (. or ..) into clean, fully-qualified absolute system paths.
  • Smart Permission Management: Dynamically handles native Unix permission octals while safely managing the "Read-Only" file attribute semantics within Windows environments.
  • Cross-Platform Volume Checks: Native system call wrappers (sys/unix and Win32 API) to query raw available storage bytes without low-level type mismatches.
  • Safe Traversal & Deletions: Explicit directory-guard logic built into atomic helper actions to eliminate accidental or recursive structure deletion vulnerabilities.

   


Import Path

To use this module in your Go project, include it via:

import "://github.com"

   


Install

Open your shell and use go get pointing directly to this module subfolder path:

  go get ://github.com@latest

   


Quick Usage Example

package main

import (
	"fmt"
	"log"
	
	"://github.com"
)

func main() {
	// Paths starting with "~" or relative structures are automatically fully-resolved
	targetDir := "~/.config/aeon/data"

	// Create entire directory path with custom permissions (0700)
	err := fs.CreateDirPath(targetDir, 0700)
	if err != nil {
		log.Fatalf("failed creating directories: %v", err)
	}

	// Safely verify storage limitations before committing heavy write procedures
	hasSpace, err := fs.HasSpaceAvailable(targetDir, 1024*1024*500) // 500 MB
	if err != nil {
		log.Fatalf("failed checking storage boundaries: %v", err)
	}

	if hasSpace {
		fmt.Println("Volume has sufficient space available for processing.")
	}
}

   


Test Suite

To run tests targeting this module locally, navigate to the fs/ directory and execute:

  go test -v -cover ./...

   


Licence

This project is offered under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateDir

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

CreateDir creates a single directory at the specified path. It automatically expands the tilde prefix ("~") before creating the folder and fails if the parent directory structure does not exist. An optional os.FileMode can be passed; if omitted, it defaults to standard permissions (0755). Returns nil on success, or an error if path expansion fails or the directory cannot be created.

func CreateDirPath

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

CreateDirPath creates a directory path along with any necessary parent directories. It automatically expands the tilde prefix ("~") before creating the folder structure. An optional os.FileMode can be passed; if omitted, it defaults to standard permissions (0755). If the target path already exists, it does nothing and returns nil.

func CreateFile

func CreateFile(path string, perm ...os.FileMode) (*os.File, error)

CreateFile creates a new file at the specified path, truncating it if it already exists. It automatically expands the tilde prefix ("~") before executing the operation. An optional os.FileMode can be passed; if omitted, it defaults to standard system permissions (0666). Returns a pointer to the opened file object (*os.File) ready for writing, or an error if path expansion or file creation fails.

func DeleteDir

func DeleteDir(path string, recursive bool) error

DeleteDir removes a directory at the specified path, with optional recursive deletion of all its contents. It automatically expands the tilde prefix ("~") before executing any filesystem validation or removal steps. If recursive is true, it uses os.RemoveAll to forcefully delete the folder and everything inside it; otherwise, it uses os.Remove and fails if the directory is not completely empty. Returns nil on success, or an error if path expansion fails, the path is not a directory, or the system removal operation is denied.

func DeleteFile

func DeleteFile(path string) error

DeleteFile removes a regular file at the specified path. It automatically expands the tilde prefix ("~") before performing the check and deletion. It explicitly fails if the targeted path is a directory to prevent accidental directory structural loss. Returns nil on success, or an error if path expansion fails, the path points to a directory, or the system removal operation is denied.

func Exists

func Exists(path string) bool

Exists checks if a file or directory exists at the given path. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before executing the check. Returns true if the target path is found on the system, or false if it does not exist or if path expansion fails.

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize returns the size of the file in bytes at the specified path. It automatically expands the tilde prefix ("~") before checking the path. It explicitly returns an error if the path points to a directory or if the file does not exist. Returns the file size in bytes as an int64, or an error if path expansion, system calls, or validations fail.

func GetParentPath

func GetParentPath(filePath string) string

GetParentPath extracts and returns the parent directory path from a given file path. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before isolating the layout. Returns the parent directory string, or an empty string if the input filePath is empty or if path expansion fails.

func GetPermission

func GetPermission(path string) (os.FileMode, error)

GetPermission retrieves the system permission bits (os.FileMode) for the specified file or directory. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before executing the check. Returns the file mode permissions, or an error if path expansion fails or if the path does not exist.

func GetUserConfigDir

func GetUserConfigDir() (string, error)

GetUserConfigDir retrieves the absolute filesystem path to the current user's configuration directory. It wraps Go's native standard library call to return the appropriate standard path based on the operating system guidelines. Returns the configuration directory path string, or an error if the path cannot be determined.

func GetUserDataDir

func GetUserDataDir(appName string) string

GetUserDataDir determines the standard platform-specific directory for storing application data. It accepts the application name (appName) to append to the base system path. It automatically falls back to the system's temporary directory if the user's home directory cannot be resolved. Returns the fully constructed and cleaned absolute path tailored to the target operating system guidelines.

func GetUserHomeDir

func GetUserHomeDir() (string, error)

GetUserHomeDir retrieves the absolute filesystem path to the current user's home directory. It acts as a wrapper around Go's native standard library call, ensuring cross-platform compatibility. Returns the home directory path string, or an error if the system environment variables or user profile cannot be resolved.

func GetUserLogDir

func GetUserLogDir(appName string) string

GetUserLogDir determines the standard platform-specific directory for storing application log files. It accepts the application name (appName) to append to the base system path. It automatically falls back to the system's temporary directory if the user's home directory cannot be resolved. Returns the fully constructed and cleaned absolute path tailored to the target operating system guidelines.

func HasSpaceAvailable

func HasSpaceAvailable(path string, requiredBytes uint64) (bool, error)

HasSpaceAvailable checks if the storage volume containing the specified path has at least the required number of bytes free. It accepts a filesystem path (path) and the minimum required space in bytes (requiredBytes). If the target path does not exist, it traverses upward to find the closest existing parent directory to accurately target the underlying volume. Returns true if the volume has sufficient space available for the current user, or false along with an error if the path expansion, UTF-16 conversion, or Win32 GetDiskFreeSpaceEx API call fails.

func IsDir

func IsDir(path string) bool

IsDir checks if the specified path exists and points to a directory. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before executing the check. Returns true if the path is a directory, or false if it does not exist, is a regular file/symlink, or if system calls fail.

func IsEmptyDir

func IsEmptyDir(path string) (bool, error)

IsEmptyDir checks if the specified directory exists and contains no files or subdirectories. It automatically expands the tilde prefix ("~") before evaluating the target directory path. Returns true if the directory is completely empty, or false along with an error if the path does not exist, is not a directory, or if read permissions are denied.

func IsFile

func IsFile(path string) bool

IsFile checks if the specified path exists and points to a regular file. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before executing the check. Returns true if the path is a regular file, or false if it does not exist, is a directory/symlink, or if system calls fail.

func IsFileDirExists

func IsFileDirExists(filePath string) bool

IsFileDirExists extracts the parent directory path from a given file path and checks if it exists as a valid directory. It automatically expands the tilde prefix ("~") before isolating the parent directory layout. Returns true if the parent directory structure exists on the system, or false if the path is empty, expansion fails, or the directory does not exist.

func IsReadable

func IsReadable(path string) bool

IsReadable checks if the current application process has sufficient permissions to read the file or directory at the specified path. It automatically expands the tilde prefix ("~") and robustly handles both regular files and directory permission boundaries across different operating systems. Returns true if the path can be successfully read, or false if permissions are denied or if the path does not exist.

func IsSameFile

func IsSameFile(pathA, pathB string) (bool, error)

IsSameFile checks if two different paths point to the exact same physical file or directory on the storage device. It automatically expands the tilde prefix ("~") on both inputs and resolves any symbolic links encountered. It relies on Go's native os.SameFile mechanism to compare low-level system identities like inodes or file IDs. Returns true if both paths target the identical filesystem resource, or false along with an error if metadata retrieval fails.

func IsWritable

func IsWritable(path string) bool

IsWritable checks if the current application process has sufficient permissions to modify or write to the file or directory at the specified path. It automatically expands the tilde prefix ("~") and tests directories by attempting to create a temporary file inside them, or files by opening them in write-only append mode. Returns true if the path can be written to, or false if permissions are denied, if the path does not exist, or if the test operations fail.

func OpenFileRead

func OpenFileRead(path string) (*os.File, error)

OpenFileRead opens an existing file exclusively in read-only mode. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before attempting to open the path. Returns a pointer to the opened file object (*os.File) ready for reading, or an error if path expansion fails or if the file does not exist.

func OpenFileWrite

func OpenFileWrite(path string, truncate bool, perm ...os.FileMode) (*os.File, error)

OpenFileWrite opens a file for writing, creating it with the specified or default permissions (0644) if it does not exist. It automatically expands the tilde prefix ("~") before opening the path. If truncate is true, the file's existing content is cleared upon opening; otherwise, new data is appended to the end. An optional os.FileMode can be passed to override the default creation permissions. Returns a pointer to the opened file object (*os.File) ready for writing, or an error if path expansion or file opening fails.

func RetrieveFullPath

func RetrieveFullPath(path string) (string, error)

RetrieveFullPath replaces the tilde prefix ("~") with the user's home directory and strictly resolves the final result into a clean, absolute filesystem path. If the path is relative, it anchors it to the current working directory, removing any redundancies like "." or "..". Returns the fully resolved absolute path, or an error if system paths cannot be determined.

func SetPermission

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

SetPermission updates the filesystem permission bits (os.FileMode) for the specified file or directory. It automatically expands the tilde prefix ("~") using the RetrieveFullPath function before applying the changes. Returns nil on success, or an error if path expansion fails, the path does not exist, or the operation is denied.

Types

This section is empty.

Jump to

Keyboard shortcuts

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