fileutils

package
v0.0.0-...-2e3af86 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2024 License: Apache-2.0 Imports: 15 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

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. No permissions check is performed if the directory already exists.

func EnsureParentDirectoryExists

func EnsureParentDirectoryExists(fileName string) error

EnsureParentDirectoryExists check whether the directory containing a certain file exists, and if it does not exist, create it using 0700 as permissions bits. No permissions check is performed if the directory already exists.

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 FormatFriendlyTimestamp

func FormatFriendlyTimestamp(t time.Time) string

FormatFriendlyTimestamp formats a timestamp in a filename-friendly way.

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

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

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

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

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

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

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

type DiskProbe

type DiskProbe struct {
	// contains filtered or unexported fields
}

DiskProbe represents a filesystem directory and provides methods to interact with it, such as checking for available disk space by attempting to create a file of a specified size.

func NewDiskProbe

func NewDiskProbe(path string) *DiskProbe

NewDiskProbe creates and returns a new Directory instance for the specified path.

func (DiskProbe) HasStorageAvailable

func (d DiskProbe) HasStorageAvailable(ctx context.Context, size int) (bool, error)

HasStorageAvailable checks if there's enough disk space to store a file with a specified size inside the directory. It does that by using createFileFunc to create such a file in the directory and then removing it.

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