Documentation
¶
Index ¶
- Constants
- func ClearDir(dir string) error
- func CreateAndOpenFile(path string, fileName string, perms ...os.FileMode) (io.Writer, error)
- func FileCopy(src string, destination string, perms ...os.FileMode) error
- func FileExists(filename string) bool
- func GetAbsPath(path, defaultPath string) (string, error)
- func GetDirNamesInFolder(path string) ([]string, error)
- func GetFileNamesInFolder(path string) ([]string, error)
- func MkdirAll(path string, perm ...os.FileMode) error
- func ReadBinFile(path string, name string) ([]byte, error)
- func ReadStringFile(path string, name string) (string, error)
- func StatTimes(name string) (atime, mtime, ctime time.Time, err error)
- func WriteFileString(path string, name string, value string) error
Constants ¶
const OwnerWritePerm = os.FileMode(0o755)
OwnerWritePerm provides 0755 permission.
Variables ¶
This section is empty.
Functions ¶
func ClearDir ¶
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 ¶
CreateAndOpenFile creates file and open it foe recording.
func FileExists ¶
FileExists checks if a file exists and is not a directory before we try using it to prevent further errors.
func GetAbsPath ¶
GetAbsPath returns an absolute path based on the input path and a default path. It handles three cases for the input path:
- 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.
- If the path is empty, it uses the defaultPath instead.
- 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 ¶
GetDirNamesInFolder returns slice with directory names in path.
func GetFileNamesInFolder ¶
GetFileNamesInFolder returns slice with file names in path.
func MkdirAll ¶
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 ¶
ReadBinFile reads file as slice of bytes.
func ReadStringFile ¶
ReadStringFile reads file as string.
Types ¶
This section is empty.