fileutils

package
v1.23.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package fileutils contains the utility functions about file management

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendStringToFile

func AppendStringToFile(targetFile string, content string) (err error)

AppendStringToFile append the content of the given string to the end of the target file prepending new data with a carriage return

func CopyFile

func CopyFile(source, destination string) (err error)

CopyFile copy a file from a location to another one

func CreateEmptyFile

func CreateEmptyFile(fileName string) error

CreateEmptyFile create an empty file or return an error if the file already exist

func EnsureDirectoryExists added in v1.17.4

func EnsureDirectoryExists(destinationDir string) error

EnsureDirectoryExists check if the passed directory exists or not, and if it doesn't exist, create it using 0700 as permissions bits

func EnsureParentDirectoryExist

func EnsureParentDirectoryExist(fileName string) error

EnsureParentDirectoryExist check if the directory containing a certain file exist or not, and if is not existent will create the directory using 0700 as permissions bits

func EnsurePgDataPerms

func EnsurePgDataPerms(pgData string) error

EnsurePgDataPerms ensure PGDATA has 0700 permissions, which are required for PostgreSQL to successfully startup

func FileExists

func FileExists(fileName string) (bool, error)

FileExists check if a file exists, and return an error otherwise

func GetDirectoryContent

func GetDirectoryContent(dir string) (files []string, err error)

GetDirectoryContent return a slice of string with the name of the files in the dir directory

func GetFileSize

func GetFileSize(fileName string) (int64, error)

GetFileSize returns the size of a file or an error

func MoveDirectoryContent added in v1.17.0

func MoveDirectoryContent(sourceDirectory, destinationDirectory string) error

MoveDirectoryContent moves a directory from a source path to its destination by copying the source files or subdirectories recursively to the destination. Once this is done it deletes the files from the original location.

func MoveFile

func MoveFile(sourcePath, destPath string) (err error)

MoveFile moves a file from a source path to its destination by copying the source file to the destination and then removing it from the original location. This will work between different volumes too.

func OpenFileAsync

func OpenFileAsync(ctx context.Context, fileName string, flag int, perm os.FileMode) (f *os.File, err error)

OpenFileAsync opens a file exiting in case the given context.Context is closed while waiting for the OpenFile to terminate, this can be useful with FIFO files, as Open will not return until the File is not opened with write permissions by another process or goroutine.

func ReadFile

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

ReadFile reads source file and output the content as bytes. If the file does not exist, it returns an empty string with no error.

func ReadFileLines added in v1.19.6

func ReadFileLines(fileName string) (lines []string, err error)

ReadFileLines reads source file and output the content as a slice of strings. If the file does not exist, it returns an empty slice with no error.

func RemoveDirectory added in v1.21.5

func RemoveDirectory(dir string) error

RemoveDirectory remove the directory and all its content

func RemoveDirectoryContent

func RemoveDirectoryContent(dir string) (err error)

RemoveDirectoryContent removes all the files and directories inside the provided path. The directory itself is preserved.

func RemoveFile

func RemoveFile(fileName string) error

RemoveFile removes a specified file. Also works if a directory is empty.

func RemoveFiles added in v1.21.0

func RemoveFiles(ctx context.Context, basePath string, filePaths []string) error

RemoveFiles deletes the files and directories specified by the filePaths patterns relative to the basePath. If a pattern ends with "/*", it implies that all the contents of the directory (not the directory itself) matching the pattern should be removed. If a pattern does not end with "/*", then the files matching the pattern will be removed.

Parameters: - ctx: A context used for logging - basePath: The root directory where the filePaths are applied. - filePaths: List of relative paths or patterns to be removed.

Returns: - error: Any error encountered during the removal process, or nil if the operation was successful.

Example: basePath: "/path/to/directory" filePaths: ["file1.txt", "subdir/*"] This would remove "/path/to/directory/file1.txt" and the "path/to/directory/subdir" folder

func RemoveRestoreExcludedFiles added in v1.21.0

func RemoveRestoreExcludedFiles(ctx context.Context, basePath string) error

RemoveRestoreExcludedFiles removes files and directories that are excluded during a restore operation. It leverages the RemoveFiles function, using a predefined list of paths that are meant to be excluded.

Parameters: - ctx: A context used for logging. - basePath: The root path from which the exclusions should be applied.

Returns: - error: Any error encountered during the removal process, or nil if the operation was successful.

func WriteFileAtomic

func WriteFileAtomic(fileName string, contents []byte, perm os.FileMode) (bool, error)

WriteFileAtomic atomically replace the content of a file. If the file doesn't exist, it's created. Returns an error status and a flag telling if the file has been changed or not.

func WriteLinesToFile added in v1.19.6

func WriteLinesToFile(fileName string, lines []string) (changed bool, err error)

WriteLinesToFile replace the contents of a certain file with a slice of lines. If the file doesn't exist, it's created. Returns an error status and a flag telling if the file has been changed or not.

func WriteStringToFile

func WriteStringToFile(fileName string, contents string) (changed bool, err error)

WriteStringToFile replace the contents of a certain file with a string. If the file doesn't exist, it's created. Returns an error status and a flag telling if the file has been changed or not.

Types

This section is empty.

Directories

Path Synopsis
Package compatibility provides a layer to cross-compile with other OS than Linux
Package compatibility provides a layer to cross-compile with other OS than Linux

Jump to

Keyboard shortcuts

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