Documentation ¶
Overview ¶
Package fileutils contains the utility functions about file management
Index ¶
- func AppendStringToFile(targetFile string, content string) (err error)
- func CopyFile(source, destination string) (err error)
- func CreateEmptyFile(fileName string) error
- func EnsureDirectoryExists(destinationDir string) error
- func EnsureParentDirectoryExist(fileName string) error
- func EnsurePgDataPerms(pgData string) error
- func FileExists(fileName string) (bool, error)
- func GetDirectoryContent(dir string) (files []string, err error)
- func GetFileSize(fileName string) (int64, error)
- func MoveDirectoryContent(sourceDirectory, destinationDirectory string) error
- func MoveFile(sourcePath, destPath string) (err error)
- func OpenFileAsync(ctx context.Context, fileName string, flag int, perm os.FileMode) (f *os.File, err error)
- func ReadFile(fileName string) ([]byte, error)
- func ReadFileLines(fileName string) (lines []string, err error)
- func RemoveDirectoryContent(dir string) (err error)
- func RemoveFile(fileName string) error
- func RemoveFiles(ctx context.Context, basePath string, filePaths []string) error
- func RemoveRestoreExcludedFiles(ctx context.Context, basePath string) error
- func WriteFileAtomic(fileName string, contents []byte, perm os.FileMode) (bool, error)
- func WriteLinesToFile(fileName string, lines []string) (changed bool, err error)
- func WriteStringToFile(fileName string, contents string) (changed bool, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendStringToFile ¶
AppendStringToFile append the content of the given string to the end of the target file prepending new data with a carriage return
func CreateEmptyFile ¶
CreateEmptyFile create an empty file or return an error if the file already exist
func EnsureDirectoryExists ¶ added in v1.17.4
EnsureDirectoryExists check if the passed directory exists or not, and if it doesn't exist, create it using 0700 as permissions bits
func EnsureParentDirectoryExist ¶
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 ¶
EnsurePgDataPerms ensure PGDATA has 0700 permissions, which are required for PostgreSQL to successfully startup
func FileExists ¶
FileExists check if a file exists, and return an error otherwise
func GetDirectoryContent ¶
GetDirectoryContent return a slice of string with the name of the files in the dir directory
func GetFileSize ¶
GetFileSize returns the size of a file or an error
func MoveDirectoryContent ¶ added in v1.17.0
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 ¶
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 ¶
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
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 RemoveDirectoryContent ¶
RemoveDirectoryContent removes all the files and directories inside the provided path. The directory itself is preserved.
func RemoveFile ¶
RemoveFile removes a specified file. Also works if a directory is empty.
func RemoveFiles ¶ added in v1.21.0
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
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 ¶
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
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 ¶
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 |