Documentation ¶
Index ¶
- Constants
- func AtomicSymlink(oldname string, newname string) error
- func AtomicWrite(p string, r io.Reader, opts ...AtomicWriteOpt) error
- func AtomicWriter(p string, w func(io.Writer) error, opts ...AtomicWriteOpt) error
- func CalDirSizeInGB(path string) (float64, error)
- func CheckSpaceAvailability(dir string, minSpace float64) (bool, error)
- func CopyDir(srcDir string, dstDir string, options ...Opt) error
- func CopyFile(srcPath, dstPath string, options ...Opt) error
- func CreateDestinationAndCopy(binarySrcPath, binaryDestPath string) error
- func CreateHabTmpDir() error
- func CreateTempFile(content string, filename string, dir string) (string, error)
- func CreateTomlFileFromConfig(config interface{}, tomlFile string) (string, error)
- func DeleteFile(filePath string) error
- func Executable(uname string, path string) (bool, error)
- func GetFilePermission(filePath string) (int64, error)
- func GetFreeSpaceinGB(dir string) (float64, error)
- func GetHabRootPath() string
- func IsSymlink(path string) (bool, error)
- func LogClose(c io.Closer, log logrus.FieldLogger, msg string)
- func MakeReadWriteExecutable(uname, path string) error
- func Move(sourceFile string, destinationFile string) error
- func PathExists(path string) (bool, error)
- func ReadFile(filename string) ([]byte, error)
- func ReadWritable(uname string, path string) (bool, error)
- func ReadWriteExecutable(uname string, path string) (bool, error)
- func Readable(uname string, path string) (bool, error)
- func RemoveFirstLine(filePath string) error
- func Writable(uname string, path string) (bool, error)
- func WriteFile(filepath string, data []byte, perm os.FileMode) error
- func WriteToFile(filepath string, data []byte) error
- type AtomicWriteOpt
- type FileSystemUtils
- func (fsu *FileSystemUtils) CalDirSizeInGB(path string) (float64, error)
- func (fsu *FileSystemUtils) CheckSpaceAvailability(dir string, minSpace float64) (bool, error)
- func (fsu *FileSystemUtils) CreateTempFile(content string, filename string, dir string) (string, error)
- func (fsu *FileSystemUtils) DeleteFile(filePath string) error
- func (fsu *FileSystemUtils) GetFilePermission(filePath string) (int64, error)
- func (fsu *FileSystemUtils) GetFreeSpaceinGB(dir string) (float64, error)
- func (fsu *FileSystemUtils) GetHabRootPath() string
- func (fsu *FileSystemUtils) IsSymlink(path string) (bool, error)
- func (fsu *FileSystemUtils) Move(sourceFile string, destinationFile string) error
- func (fsu *FileSystemUtils) PathExists(path string) (bool, error)
- func (fsu *FileSystemUtils) ReadFile(filename string) ([]byte, error)
- func (fsu *FileSystemUtils) RemoveFile(filename string) error
- func (fsu *FileSystemUtils) RemoveFirstLine(filePath string) error
- func (fsu *FileSystemUtils) Stat(name string) (os.FileInfo, error)
- func (fsu *FileSystemUtils) WriteFile(filepath string, data []byte, perm os.FileMode) error
- func (fsu *FileSystemUtils) WriteToFile(filepath string, data []byte) error
- type FileUtils
- type MockFileInfo
- type MockFileSystemUtils
- func (fsu *MockFileSystemUtils) CalDirSizeInGB(path string) (float64, error)
- func (fsu *MockFileSystemUtils) CheckSpaceAvailability(dir string, minSpace float64) (bool, error)
- func (fsu *MockFileSystemUtils) CreateTempFile(content string, filename string, dir string) (string, error)
- func (fsu *MockFileSystemUtils) DeleteFile(tempFile string) error
- func (fsu *MockFileSystemUtils) GetFilePermission(filePath string) (int64, error)
- func (fsu *MockFileSystemUtils) GetFreeSpaceinGB(dir string) (float64, error)
- func (fsu *MockFileSystemUtils) GetHabRootPath() string
- func (fsu *MockFileSystemUtils) IsSymlink(path string) (bool, error)
- func (fsu *MockFileSystemUtils) Move(sourceFile string, destinationFile string) error
- func (fsu *MockFileSystemUtils) PathExists(path string) (bool, error)
- func (fsu *MockFileSystemUtils) ReadFile(filepath string) ([]byte, error)
- func (fsu *MockFileSystemUtils) RemoveFile(name string) error
- func (fsu *MockFileSystemUtils) RemoveFirstLine(filePath string) error
- func (fsu *MockFileSystemUtils) Stat(name string) (os.FileInfo, error)
- func (fsu *MockFileSystemUtils) WriteFile(filepath string, data []byte, perm os.FileMode) error
- func (fsu *MockFileSystemUtils) WriteToFile(filepath string, data []byte) error
- type Opt
- type PathStat
- type PathStats
- type WriteCloserFailer
Constants ¶
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 ¶
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 ¶
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 CheckSpaceAvailability ¶
func CreateHabTmpDir ¶
func CreateHabTmpDir() error
CreateHabTmpDir creates a temporary directory in the hab directory
func CreateTempFile ¶
func CreateTomlFileFromConfig ¶
createTomlFileFromConfig created a toml file where path and struct interface is provided
func DeleteFile ¶
func Executable ¶
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 GetFreeSpaceinGB ¶
func GetHabRootPath ¶
func GetHabRootPath() string
func IsSymlink ¶
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 ¶
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 ¶
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 ¶
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 ReadWritable ¶
ReadWritable returns whether or not a path is RW by a given user
func ReadWriteExecutable ¶
ReadWriteExecutable returns whether or not a path is RWE by a given user
func RemoveFirstLine ¶
func WriteToFile ¶
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 (*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 (*FileSystemUtils) IsSymlink ¶
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) 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) ModTime ¶
func (mfi MockFileInfo) ModTime() time.Time
ModTime 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 (*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 (*MockFileSystemUtils) IsSymlink ¶
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) WriteToFile ¶
func (fsu *MockFileSystemUtils) WriteToFile(filepath string, data []byte) error
type Opt ¶
type Opt func(*copyOpts)
Opt is a functional option
func NoRecursive ¶
func NoRecursive() Opt
NoRecursive will not recursively traverse directories if the function supports it
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)