fsutil

package
v0.0.0-...-fd57926 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

fsutil/directory.go

fsutil/files.go

fsutil/locations.go

fsutil/paths.go

fsutil/permissions.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStringToFile

func AppendStringToFile(path string, content string) error

AppendStringToFile appends a string to an existing file

func AppendToFile

func AppendToFile(path string, data []byte) error

AppendToFile appends data to an existing file

func CleanDir

func CleanDir(path string) error

CleanDir removes all contents from a directory without removing the directory itself

func CleanPath

func CleanPath(path string) string

CleanPath cleans a path by removing redundant separators and resolving ".." and "."

func ConvertToOSPath

func ConvertToOSPath(path string) string

ConvertToOSPath converts a path to use the current OS path format

func CopyDir

func CopyDir(src, dst string) error

CopyDir recursively copies a directory and its contents

func CopyDirContents

func CopyDirContents(src, dst string) error

CopyDirContents copies the contents of a directory without recreating the source directory

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from source to destination

func CopySymlink(src, dst string) error

CopySymlink copies a symlink from source to destination

func CreateDir

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

CreateDir creates a directory if it doesn't exist

func CreateDirIfNotExists

func CreateDirIfNotExists(path string) error

CreateDirIfNotExists creates a directory with standard permissions if it doesn't exist

func CreateTempDir

func CreateTempDir(prefix string) (string, error)

CreateTempDir creates a temporary directory with a prefix

func CreateTempDirIn

func CreateTempDirIn(dir, prefix string) (string, error)

CreateTempDirIn creates a temporary directory with a prefix in a specific directory

func DeleteDir

func DeleteDir(path string) error

DeleteDir removes an empty directory

func DeleteDirRecursive

func DeleteDirRecursive(path string) error

DeleteDirRecursive removes a directory and all its contents

func DeleteFile

func DeleteFile(path string) error

DeleteFile deletes a file if it exists

func DirExists

func DirExists(path string) bool

DirExists checks if a directory exists

func EnsureEmptyDir

func EnsureEmptyDir(path string) error

EnsureEmptyDir ensures a directory exists and is empty

func ExpandTilde

func ExpandTilde(path string) (string, error)

ExpandTilde expands the tilde in paths to the user's home directory

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists and is not a directory

func FindFiles

func FindFiles(root, pattern string) ([]string, error)

FindFiles finds all files matching a pattern in a directory (recursive)

func FindFilesByExt

func FindFilesByExt(root, ext string) ([]string, error)

FindFilesByExt finds all files with a specific extension in a directory (recursive)

func GetCacheDir

func GetCacheDir(appName string) (string, error)

GetCacheDir returns the appropriate cache directory for the application

func GetConfigDir

func GetConfigDir(appName string) (string, error)

GetConfigDir returns the appropriate configuration directory for the application

func GetDataDir

func GetDataDir(appName string) (string, error)

GetDataDir returns the appropriate data directory for the application

func GetDir

func GetDir(path string) string

GetDir returns the directory of a given file path This is a pure path operation that doesn't access the filesystem, so no locking needed

func GetDirSize

func GetDirSize(path string) (int64, error)

GetDirSize calculates the total size of a directory and its contents

func GetExecutableDir

func GetExecutableDir() (string, error)

GetExecutableDir returns the directory of the current executable

func GetExtension

func GetExtension(path string) string

GetExtension returns the file extension with the dot (e.g., ".txt")

func GetFileAttributes

func GetFileAttributes(path string) (string, error)

GetFileAttributes gets platform-specific file attributes

func GetFileHash

func GetFileHash(path string, hashType string) (string, error)

GetFileHash calculates a file's hash using the specified algorithm

func GetFileNameWithoutExt

func GetFileNameWithoutExt(path string) string

GetFileNameWithoutExt returns the file name without its extension

func GetFreeDiskSpace

func GetFreeDiskSpace(path string) (uint64, error)

GetFreeDiskSpace returns the available disk space in bytes for a given path

func GetGroup

func GetGroup(path string) (string, error)

GetGroup gets the group of a file or directory

func GetHomeDir

func GetHomeDir() (string, error)

GetHomeDir returns the user's home directory

func GetLogDir

func GetLogDir(appName string) (string, error)

GetLogDir returns the appropriate log directory for the application

func GetOwner

func GetOwner(path string) (string, error)

GetOwner gets the owner of a file or directory

func GetPathDepth

func GetPathDepth(path string) int

GetPathDepth returns the number of path components (levels of depth)

func GetPathMutex

func GetPathMutex(path string) *sync.Mutex

GetPathMutex returns a mutex for the given path

func GetPermissions

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

GetPermissions retrieves the permissions of a file or directory

func GetRuntimeDir

func GetRuntimeDir(appName string) (string, error)

GetRuntimeDir returns a directory for runtime files (sockets, PIDs, etc.)

func GetSystemConfigDir

func GetSystemConfigDir(appName string) (string, error)

GetSystemConfigDir returns the system-wide configuration directory

func GetSystemDataDir

func GetSystemDataDir(appName string) (string, error)

GetSystemDataDir returns the system-wide data directory

func GetSystemLogDir

func GetSystemLogDir(appName string) (string, error)

GetSystemLogDir returns the system-wide log directory

func GetTempCompressionDir

func GetTempCompressionDir() (string, error)

GetTempCompressionDir returns a temporary directory for compression/extraction operations

func GetTempDir

func GetTempDir(appName string) (string, error)

GetTempDir returns a temporary directory for the application

func GetWorkingDir

func GetWorkingDir() (string, error)

GetWorkingDir returns the current working directory

func HasEnoughDiskSpace

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

HasEnoughDiskSpace checks if there is sufficient free space for a file operation

func IsAbsPath

func IsAbsPath(path string) bool

IsAbsPath checks if a path is absolute

func IsDirEmpty

func IsDirEmpty(path string) (bool, error)

IsDirEmpty checks if a directory is empty

func IsExecutable

func IsExecutable(path string) bool

IsExecutable checks if a file is executable by the current user

func IsFileBinary

func IsFileBinary(path string) (bool, error)

IsFileBinary makes a best-effort determination of whether a file is binary or text

func IsFileEmpty

func IsFileEmpty(path string) (bool, error)

IsFileEmpty checks if a file is empty (size of 0 bytes)

func IsFileExecutable

func IsFileExecutable(path string) bool

IsFileExecutable checks if a file is executable

func IsHidden

func IsHidden(path string) (bool, error)

IsHidden checks if a file is hidden

func IsReadable

func IsReadable(path string) bool

IsReadable checks if a file or directory is readable by the current user

func IsSymlink(path string) (bool, error)

IsSymlink checks if a path is a symlink

func IsSymlinkTo

func IsSymlinkTo(path, target string) (bool, error)

IsSymlinkTo checks if a path is a symlink pointing to a specific target

func IsUnixPath

func IsUnixPath(path string) bool

IsUnixPath checks if a path appears to be a Unix-style path

func IsWindowsPath

func IsWindowsPath(path string) bool

IsWindowsPath checks if a path appears to be a Windows-style path

func IsWritable

func IsWritable(path string) bool

IsWritable checks if a file or directory is writable by the current user

func JoinPath

func JoinPath(elem ...string) string

JoinPath joins path elements using the OS-specific separator

func MakeHidden

func MakeHidden(path string) error

MakeHidden makes a file hidden

func MakeReadOnly

func MakeReadOnly(path string) error

MakeReadOnly makes a file or directory read-only

func MakeWritable

func MakeWritable(path string) error

MakeWritable makes a file or directory writable

func MoveDir

func MoveDir(src, dst string) error

MoveDir moves a directory from source to destination

func MoveFile

func MoveFile(src, dst string) error

MoveFile moves a file from source to destination

func NormalizePath

func NormalizePath(path string) string

NormalizePath normalizes a path for the current OS This is particularly useful when dealing with paths that might have been created on a different OS

func PathSeparator

func PathSeparator() string

PathSeparator returns the OS-specific path separator character

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads an entire file into memory

func ReadFileChunked

func ReadFileChunked(path string, chunkSize int, processChunk func([]byte) error) error

ReadFileChunked reads a file in chunks, calling the provided function for each chunk

func ReadFileChunkedWithLimit

func ReadFileChunkedWithLimit(path string, chunkSize int, maxBytes int64, processChunk func([]byte, int64) error) error

ReadFileChunkedWithLimit reads a file in chunks, stopping after maxBytes or EOF

func ReadFileHeader

func ReadFileHeader(path string, n int) ([]byte, error)

ReadFileHeader reads the first n bytes of a file

func ReadFileString

func ReadFileString(path string) (string, error)

ReadFileString reads a file and returns its contents as a string

func ReadLines

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

ReadLines reads a file line by line and returns an array of strings

func RelativePath

func RelativePath(basePath, targetPath string) (string, error)

RelativePath returns a relative path from base to target

func SetFileExecutable

func SetFileExecutable(path string, executable bool) error

SetFileExecutable sets or removes the executable bit on a file

func SetGroup

func SetGroup(path string, groupname string) error

SetGroup sets the group of a file or directory

func SetOwner

func SetOwner(path string, username string) error

SetOwner sets the owner of a file or directory

func SetPermissions

func SetPermissions(path string, mode os.FileMode) error

SetPermissions sets the permissions of a file or directory

func SetRecursivePermissions

func SetRecursivePermissions(path string, dirMode, fileMode os.FileMode) error

SetRecursivePermissions sets permissions recursively on a directory

func SetWindowsAttributes

func SetWindowsAttributes(path string, attrs WindowsFileAttributes) error

SetWindowsAttributes sets Windows-specific file attributes

func SplitPath

func SplitPath(path string) (dir, file string)

SplitPath splits a path into directory and file components

func ToAbsPath

func ToAbsPath(path string) (string, error)

ToAbsPath converts a relative path to an absolute path

func TouchFile

func TouchFile(path string) error

TouchFile updates a file's access and modification times, creating it if it doesn't exist

func WalkDir

func WalkDir(root string, fn func(path string, info fs.FileInfo, err error) error) error

WalkDir walks a directory recursively and calls a function for each entry

func WriteFile

func WriteFile(path string, data []byte, perm os.FileMode) error

WriteFile writes data to a file, creating it if necessary

func WriteFileString

func WriteFileString(path string, content string, perm os.FileMode) error

WriteFileString writes a string to a file

Types

type DirEntry

type DirEntry struct {
	Path     string
	Name     string
	IsDir    bool
	Size     int64
	Mode     os.FileMode
	ModTime  time.Time
	FullPath string
}

DirEntry represents an entry in a directory (file or subdirectory)

func GetNewestFile

func GetNewestFile(path string) (*DirEntry, error)

GetNewestFile finds the most recently modified file in a directory (non-recursive)

func GetOldestFile

func GetOldestFile(path string) (*DirEntry, error)

GetOldestFile finds the oldest file in a directory (non-recursive)

func ListDir

func ListDir(path string) ([]DirEntry, error)

ListDir returns a list of all files and directories in a directory (non-recursive)

func ListDirs

func ListDirs(path string) ([]DirEntry, error)

ListDirs returns a list of subdirectories in a directory (non-recursive, no files)

func ListFiles

func ListFiles(path string) ([]DirEntry, error)

ListFiles returns a list of files in a directory (non-recursive, no directories)

type FileInfo

type FileInfo struct {
	Path        string
	Size        int64
	Mode        os.FileMode
	IsDir       bool
	ModTime     time.Time
	ContentType string
}

FileInfo represents metadata about a file

func GetFileInfo

func GetFileInfo(path string) (*FileInfo, error)

GetFileInfo retrieves file information

type PermissionMode

type PermissionMode uint32

PermissionMode represents a file permission mode

const (
	PermUserRead  PermissionMode = 0400
	PermUserWrite PermissionMode = 0200
	PermUserExec  PermissionMode = 0100
	PermUserRWX   PermissionMode = 0700

	PermGroupRead  PermissionMode = 0040
	PermGroupWrite PermissionMode = 0020
	PermGroupExec  PermissionMode = 0010
	PermGroupRWX   PermissionMode = 0070

	PermOtherRead  PermissionMode = 0004
	PermOtherWrite PermissionMode = 0002
	PermOtherExec  PermissionMode = 0001
	PermOtherRWX   PermissionMode = 0007

	PermAllRead   PermissionMode = 0444
	PermAllWrite  PermissionMode = 0222
	PermAllExec   PermissionMode = 0111
	PermAllRWX    PermissionMode = 0777
	PermStandard  PermissionMode = 0644
	PermStdExec   PermissionMode = 0755
	PermDirectory PermissionMode = 0755
)

Permission constants

type WindowsFileAttributes

type WindowsFileAttributes uint32

WindowsFileAttributes represents common Windows file attributes

const (
	WinAttrReadOnly  WindowsFileAttributes = 0x1
	WinAttrHidden    WindowsFileAttributes = 0x2
	WinAttrSystem    WindowsFileAttributes = 0x4
	WinAttrDirectory WindowsFileAttributes = 0x10
	WinAttrArchive   WindowsFileAttributes = 0x20
	WinAttrNormal    WindowsFileAttributes = 0x80
)

Windows attribute constants - defined with their actual values even though we won't use golang.org/x/sys/windows

func GetWindowsAttributes

func GetWindowsAttributes(path string) (WindowsFileAttributes, error)

GetWindowsAttributes returns Windows-specific file attributes as a uint32

Jump to

Keyboard shortcuts

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