fileutils

package
v0.0.0-...-66c7a0d Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: Apache-2.0 Imports: 22 Imported by: 18

Documentation

Index

Constants

View Source
const (
	HAB_ROOT_CMD string = "HAB_LICENSE=accept-no-persist hab pkg path chef/deployment-service"
	HAB_DIR      string = "/hab"
	HAB_TMP_DIR  string = "/hab/tmp"
)

Variables

This section is empty.

Functions

func AtomicSymlink(oldname string, newname string) error

func AtomicWrite

func AtomicWrite(p string, r io.Reader, opts ...AtomicWriteOpt) error

AtomicWrite reads the content of r into the file at path p atomically. This is done by first writing into a temporary file, and then renaming that into place.

func AtomicWriter

func AtomicWriter(p string, w func(io.Writer) error, opts ...AtomicWriteOpt) error

AtomicWriter writes a file atomically into p. It provides a writer to given callback. If the callback returns an error, the file is not written.

func CalDirSizeInGB

func CalDirSizeInGB(path string) (float64, error)

func CheckSpaceAvailability

func CheckSpaceAvailability(dir string, minSpace float64) (bool, error)

func CopyDir

func CopyDir(srcDir string, dstDir string, options ...Opt) error

CopyDir copies all files that are not symlinks in a source directory to another.

func CopyFile

func CopyFile(srcPath, dstPath string, options ...Opt) error

CopyFile copies the file at the src into the dst path

func CreateDestinationAndCopy

func CreateDestinationAndCopy(binarySrcPath, binaryDestPath string) error

func CreateHabTmpDir

func CreateHabTmpDir() error

CreateHabTmpDir creates a temporary directory in the hab directory

func CreateTempFile

func CreateTempFile(content string, filename string, dir string) (string, error)

func CreateTomlFileFromConfig

func CreateTomlFileFromConfig(config interface{}, tomlFile string) (string, error)

createTomlFileFromConfig created a toml file where path and struct interface is provided

func DeleteFile

func DeleteFile(filePath string) error

func Executable

func Executable(uname string, path string) (bool, error)

Executable returns whether or not a path is executable by a given user. In addition to verifying that the executable bit is set for the base of the path, it traverses the path in reverse and verifies that the user has the exec bit through the entire path.

func GetFilePermission

func GetFilePermission(filePath string) (int64, error)

func GetFreeSpaceinGB

func GetFreeSpaceinGB(dir string) (float64, error)

func GetHabRootPath

func GetHabRootPath() string
func IsSymlink(path string) (bool, error)

IsSymlink returns true if the given path is a symbolic link and false otherwise. An error is returned on

func LogClose

func LogClose(c io.Closer, log logrus.FieldLogger, msg string)

LogCLose closes the given io.Closer, logging any error.

func MakeReadWriteExecutable

func MakeReadWriteExecutable(uname, path string) error

MakeReadWriteExecutable takes a user and path and attempts to modify file permissions to make it RWE in the least intrusive way. There are many ways to do this but what I've deemed the least intrusive generally is: If the base path isn't owned by the user or share group membership with the user we'll change the files ownership to the user instead of making it RWE for everybody. Next, we'll update the owner or group bits with RWE. Finally, we'll ensure that the user has exec bits through the entire path either via ownership, group membership, or everybody. NOTE: it's best to assume that the UID running this function is root.

func Move

func Move(sourceFile string, destinationFile string) error

Moves file from current/source directory to destination directory. Creates the directory, if not already exists

Example usage: err := fileutils.Move("file.txt", "/path/to/my/dir/", ""); Incase of retriving the old file name, leave renamedFileName as empty

func PathExists

func PathExists(path string) (bool, error)

PathExists returns true if the path exists and false if it doesn't exist. An error is returned if an unexpected error occurs. Callers who want behavior similar to Ruby's File.exist? or Rusts' path::exists functions can ignore the error.

func ReadFile

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

func ReadWritable

func ReadWritable(uname string, path string) (bool, error)

ReadWritable returns whether or not a path is RW by a given user

func ReadWriteExecutable

func ReadWriteExecutable(uname string, path string) (bool, error)

ReadWriteExecutable returns whether or not a path is RWE by a given user

func Readable

func Readable(uname string, path string) (bool, error)

Readable returns whether or not a path is readable by a given user

func RemoveFirstLine

func RemoveFirstLine(filePath string) error

func Writable

func Writable(uname string, path string) (bool, error)

Writable returns whether or not a path is writable by a given user

func WriteFile

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

func WriteToFile

func WriteToFile(filepath string, data []byte) error

Types

type AtomicWriteOpt

type AtomicWriteOpt func(*atomicWriteOpts)

AtomicWriteOpt allows setting options for writing a file

func WithAtomicWriteChmod

func WithAtomicWriteChmod(mode os.FileMode) AtomicWriteOpt

WithAtomicWriteChmod specifies the file mode the file must have. This is not affected by the umask

func WithAtomicWriteChown

func WithAtomicWriteChown(uid int, gid int) AtomicWriteOpt

func WithAtomicWriteFileMode

func WithAtomicWriteFileMode(mode os.FileMode) AtomicWriteOpt

WithAtomicWriteFileMode specifies the file mode the file must have This is affected by the umask

func WithAtomicWriteNoSync

func WithAtomicWriteNoSync(noSync bool) AtomicWriteOpt

WithAtomicWriterNoSync specifies if sync should be skipped. Skipping sync is not safe.

type FileSystemUtils

type FileSystemUtils struct{}

func (*FileSystemUtils) CalDirSizeInGB

func (fsu *FileSystemUtils) CalDirSizeInGB(path string) (float64, error)

func (*FileSystemUtils) CheckSpaceAvailability

func (fsu *FileSystemUtils) CheckSpaceAvailability(dir string, minSpace float64) (bool, error)

func (*FileSystemUtils) CreateTempFile

func (fsu *FileSystemUtils) CreateTempFile(content string, filename string, dir string) (string, error)

func (*FileSystemUtils) DeleteFile

func (fsu *FileSystemUtils) DeleteFile(filePath string) error

func (*FileSystemUtils) GetFilePermission

func (fsu *FileSystemUtils) GetFilePermission(filePath string) (int64, error)

func (*FileSystemUtils) GetFreeSpaceinGB

func (fsu *FileSystemUtils) GetFreeSpaceinGB(dir string) (float64, error)

func (*FileSystemUtils) GetHabRootPath

func (fsu *FileSystemUtils) GetHabRootPath() string
func (fsu *FileSystemUtils) IsSymlink(path string) (bool, error)

func (*FileSystemUtils) Move

func (fsu *FileSystemUtils) Move(sourceFile string, destinationFile string) error

func (*FileSystemUtils) PathExists

func (fsu *FileSystemUtils) PathExists(path string) (bool, error)

func (*FileSystemUtils) ReadFile

func (fsu *FileSystemUtils) ReadFile(filename string) ([]byte, error)

func (*FileSystemUtils) RemoveFile

func (fsu *FileSystemUtils) RemoveFile(filename string) error

func (*FileSystemUtils) RemoveFirstLine

func (fsu *FileSystemUtils) RemoveFirstLine(filePath string) error

func (*FileSystemUtils) Stat

func (fsu *FileSystemUtils) Stat(name string) (os.FileInfo, error)

func (*FileSystemUtils) WriteFile

func (fsu *FileSystemUtils) WriteFile(filepath string, data []byte, perm os.FileMode) error

func (*FileSystemUtils) WriteToFile

func (fsu *FileSystemUtils) WriteToFile(filepath string, data []byte) error

type FileUtils

type FileUtils interface {
	PathExists(path string) (bool, error)
	IsSymlink(path string) (bool, error)
	CalDirSizeInGB(path string) (float64, error)
	CheckSpaceAvailability(dir string, minSpace float64) (bool, error)
	GetFreeSpaceinGB(dir string) (float64, error)
	GetHabRootPath() string
	WriteToFile(filepath string, data []byte) error
	ReadFile(filename string) ([]byte, error)
	WriteFile(filepath string, data []byte, perm os.FileMode) error
	CreateTempFile(content string, filename string, dir string) (string, error)
	DeleteFile(fileName string) error
	Move(sourceFile string, destinationFile string) error
	RemoveFirstLine(filePath string) error
	GetFilePermission(filePath string) (int64, error)
	Stat(name string) (os.FileInfo, error)
	RemoveFile(filename string) error
}

func NewFileSystemUtils

func NewFileSystemUtils() FileUtils

type MockFileInfo

type MockFileInfo struct {
	NameFunc    func() string      // base name of the file
	SizeFunc    func() int64       // length in bytes for regular files; system-dependent for others
	ModeFunc    func() fs.FileMode // file mode bits
	ModTimeFunc func() time.Time   // modification time
	IsDirFunc   func() bool        // abbreviation for Mode().IsDir()
	SysFunc     func() any         // underlying data source (can return nil)
}

func (MockFileInfo) IsDir

func (mfi MockFileInfo) IsDir() bool

IsDir implements fs.FileInfo.

func (MockFileInfo) ModTime

func (mfi MockFileInfo) ModTime() time.Time

ModTime implements fs.FileInfo.

func (MockFileInfo) Mode

func (mfi MockFileInfo) Mode() fs.FileMode

Mode implements fs.FileInfo.

func (MockFileInfo) Name

func (mfi MockFileInfo) Name() string

Name implements fs.FileInfo.

func (MockFileInfo) Size

func (mfi MockFileInfo) Size() int64

Size implements fs.FileInfo.

func (MockFileInfo) Sys

func (mfi MockFileInfo) Sys() any

Sys implements fs.FileInfo.

type MockFileSystemUtils

type MockFileSystemUtils struct {
	PathExistsFunc             func(path string) (bool, error)
	IsSymlinkFunc              func(path string) (bool, error)
	CalDirSizeInGBFunc         func(path string) (float64, error)
	CheckSpaceAvailabilityFunc func(dir string, minSpace float64) (bool, error)
	GetFreeSpaceinGBFunc       func(dir string) (float64, error)
	GetHabRootPathFunc         func() string
	WriteToFileFunc            func(filepath string, data []byte) error
	ReadFileFunc               func(filepath string) ([]byte, error)
	WriteFileFunc              func(filepath string, data []byte, perm os.FileMode) error
	CreateTempFileFunc         func(content string, filename string, dir string) (string, error)
	DeleteTempFileFunc         func(tempFile string) error
	MoveFunc                   func(sourceFile string, destinationFile string) error
	RemoveFirstLineFunc        func(filePath string) error
	GetFilePermissionFunc      func(filePath string) (int64, error)
	StatFunc                   func(name string) (os.FileInfo, error)
	RemoveFileFunc             func(filename string) error
}

func (*MockFileSystemUtils) CalDirSizeInGB

func (fsu *MockFileSystemUtils) CalDirSizeInGB(path string) (float64, error)

func (*MockFileSystemUtils) CheckSpaceAvailability

func (fsu *MockFileSystemUtils) CheckSpaceAvailability(dir string, minSpace float64) (bool, error)

func (*MockFileSystemUtils) CreateTempFile

func (fsu *MockFileSystemUtils) CreateTempFile(content string, filename string, dir string) (string, error)

func (*MockFileSystemUtils) DeleteFile

func (fsu *MockFileSystemUtils) DeleteFile(tempFile string) error

func (*MockFileSystemUtils) GetFilePermission

func (fsu *MockFileSystemUtils) GetFilePermission(filePath string) (int64, error)

func (*MockFileSystemUtils) GetFreeSpaceinGB

func (fsu *MockFileSystemUtils) GetFreeSpaceinGB(dir string) (float64, error)

func (*MockFileSystemUtils) GetHabRootPath

func (fsu *MockFileSystemUtils) GetHabRootPath() string
func (fsu *MockFileSystemUtils) IsSymlink(path string) (bool, error)

func (*MockFileSystemUtils) Move

func (fsu *MockFileSystemUtils) Move(sourceFile string, destinationFile string) error

func (*MockFileSystemUtils) PathExists

func (fsu *MockFileSystemUtils) PathExists(path string) (bool, error)

func (*MockFileSystemUtils) ReadFile

func (fsu *MockFileSystemUtils) ReadFile(filepath string) ([]byte, error)

func (*MockFileSystemUtils) RemoveFile

func (fsu *MockFileSystemUtils) RemoveFile(name string) error

func (*MockFileSystemUtils) RemoveFirstLine

func (fsu *MockFileSystemUtils) RemoveFirstLine(filePath string) error

func (*MockFileSystemUtils) Stat

func (fsu *MockFileSystemUtils) Stat(name string) (os.FileInfo, error)

func (*MockFileSystemUtils) WriteFile

func (fsu *MockFileSystemUtils) WriteFile(filepath string, data []byte, perm os.FileMode) error

func (*MockFileSystemUtils) WriteToFile

func (fsu *MockFileSystemUtils) WriteToFile(filepath string, data []byte) error

type Opt

type Opt func(*copyOpts)

Opt is a functional option

func Depth

func Depth(depth int) Opt

Depth determines how far to recursively traverse if the function supports it

func NoRecursive

func NoRecursive() Opt

NoRecursive will not recursively traverse directories if the function supports it

func Overwrite

func Overwrite() Opt

Overwrite will overwrite the file and/or directory and files

type PathStat

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

PathStat contains a path and the corresponding syscall.Stat_t for the path.

type PathStats

type PathStats []PathStat

PathStats is a slice representing the stat results of path in reverse order starting with the base at index 0 all the way to / on the slice edge.

type WriteCloserFailer

type WriteCloserFailer interface {
	io.WriteCloser
	Fail(error) error
}

func NewAtomicWriter

func NewAtomicWriter(p string, opts ...AtomicWriteOpt) (WriteCloserFailer, error)

Jump to

Keyboard shortcuts

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