helpers

package
v0.0.0-...-921daf7 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2016 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotAbsPath = errors.New("not an absolute path")

ErrNotAbsPath is returned when an absolute path is needed but the received path is not.

View Source
var MakeRandomString = func(length int) string {

	out := ""
	for i := 0; i < length; i++ {
		out += string(letters[rand.Intn(len(letters))])
	}

	return out
}

MakeRandomString returns a random string of length length

Functions

func AtomicWriteFile

func AtomicWriteFile(filename string, data []byte, perm os.FileMode, flags AtomicWriteFlags) (err error)

AtomicWriteFile updates the filename atomically and works otherwise like io/ioutil.WriteFile()

Note that it won't follow symlinks and will replace existing symlinks with the real file

func ChDir

func ChDir(newDir string, f func() error) (err error)

ChDir runs runs "f" inside the given directory

func CopyFile

func CopyFile(src, dst string, flags CopyFlag) (err error)

CopyFile copies src to dst

func CopyIfDifferent

func CopyIfDifferent(src, dst string) error

CopyIfDifferent copies src to dst only if dst is different that src

func CopySpecialFile

func CopySpecialFile(path, dest string) error

CopySpecialFile is used to copy all the things that are not files (like device nodes, named pipes etc)

func CurrentHomeDir

func CurrentHomeDir() (string, error)

CurrentHomeDir returns the homedir of the current user. It looks at $HOME first and then at passwd

func DirUpdated

func DirUpdated(dirA, dirB, pfx string) map[string]bool

DirUpdated compares two directories, and returns which files present in both have been updated, with the given prefix prepended.

Subdirectories are ignored.

This function is to compare the policies and templates in a (framework) snap to be installed, against the policies and templates of one already installed, to then determine what changed. The prefix is because policies and templates are specified with the framework name.

func ExitCode

func ExitCode(runErr error) (e int, err error)

ExitCode extract the exit code from the error of a failed cmd.Run() or the original error if its not a exec.ExitError

func FileExists

func FileExists(path string) bool

FileExists return true if given path can be stat()ed by us. Note that it may return false on e.g. permission issues.

func FilesAreEqual

func FilesAreEqual(a, b string) bool

FilesAreEqual compares the two files' contents and returns whether they are the same.

func GetBasicSnapEnvVars

func GetBasicSnapEnvVars(desc interface{}) []string

GetBasicSnapEnvVars returns the app-level environment variables for a snap. Despite this being a bit snap-specific, this is in helpers.go because it's used by so many other modules, we run into circular dependencies if it's somewhere more reasonable like the snappy module.

func GetDeprecatedBasicSnapEnvVars

func GetDeprecatedBasicSnapEnvVars(desc interface{}) []string

GetDeprecatedBasicSnapEnvVars returns the app-level deprecated environment variables for a snap. Despite this being a bit snap-specific, this is in helpers.go because it's used by so many other modules, we run into circular dependencies if it's somewhere more reasonable like the snappy module.

func GetDeprecatedUserSnapEnvVars

func GetDeprecatedUserSnapEnvVars(desc interface{}) []string

GetDeprecatedUserSnapEnvVars returns the user-level deprecated environment variables for a snap. Despite this being a bit snap-specific, this is in helpers.go because it's used by so many other modules, we run into circular dependencies if it's somewhere more reasonable like the snappy module.

func GetUserSnapEnvVars

func GetUserSnapEnvVars(desc interface{}) []string

GetUserSnapEnvVars returns the user-level environment variables for a snap. Despite this being a bit snap-specific, this is in helpers.go because it's used by so many other modules, we run into circular dependencies if it's somewhere more reasonable like the snappy module.

func Getattr

func Getattr(i interface{}, name string) interface{}

Getattr get the attribute of the given name

func IsDevice

func IsDevice(mode os.FileMode) bool

IsDevice checks if the given os.FileMode coresponds to a device (char/block)

func IsDirectory

func IsDirectory(path string) bool

IsDirectory return true if the given path can be stat()ed by us and is a directory. Note that it may return false on e.g. permission issues.

func IsSymlink(mode os.FileMode) bool

IsSymlink checks whether the given os.FileMode corresponds to a symlink

func MajorMinor

func MajorMinor(info os.FileInfo) (uint32, uint32, error)

MajorMinor returns the major/minor number of the given os.FileInfo

func MakeMapFromEnvList

func MakeMapFromEnvList(env []string) map[string]string

MakeMapFromEnvList takes a string list of the form "key=value" and returns a map[string]string from that list This is useful for os.Environ() manipulation

func Makedev

func Makedev(major, minor uint32) uint32

Makedev implements makedev(3)

func NewSideloadVersion

func NewSideloadVersion() string

NewSideloadVersion returns a version number such that later calls should return versions that compare larger.

func RSyncWithDelete

func RSyncWithDelete(srcDirName, destDirName string) error

RSyncWithDelete syncs srcDir to destDir

func Sha512sum

func Sha512sum(infile string) (hexdigest string, err error)

Sha512sum returns the sha512 of the given file as a hexdigest

func ShouldDropPrivs

func ShouldDropPrivs() bool

ShouldDropPrivs returns true if the application runs with sufficient privileges so that it should drop them

func TarIterate

func TarIterate(r io.Reader, fn TarIterFunc) error

TarIterate will take a io.Reader and call the fn callback on each tar archive member

func UnpackTar

func UnpackTar(r io.Reader, targetDir string, fn UnpackTarTransformFunc) error

UnpackTar unpacks the given tar file into the target directory

func UpdateTimestamp

func UpdateTimestamp(pathname string) error

UpdateTimestamp updates the timestamp of the file at pathname. It does not create it if it does not exist. It does not dereference it if it is a symlink. It's like `touch -c -h pathname`.

pathname must be absolute.

Types

type AtomicWriteFlags

type AtomicWriteFlags uint

AtomicWriteFlags are a bitfield of flags for AtomicWriteFile

const (
	// AtomicWriteFollow makes AtomicWriteFile follow symlinks
	AtomicWriteFollow AtomicWriteFlags = 1 << iota
)

type CopyFlag

type CopyFlag uint8

CopyFlag is used to tweak the behaviour of CopyFile

const (
	// CopyFlagDefault is the default behaviour
	CopyFlagDefault CopyFlag = 0
	// CopyFlagSync does a sync after copying the files
	CopyFlagSync CopyFlag = 1 << iota
	// CopyFlagOverwrite overwrites the target if it exists
	CopyFlagOverwrite
	// CopyFlagPreserveAll preserves mode,owner,time attributes
	CopyFlagPreserveAll
)

type ErrCopySpecialFile

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

ErrCopySpecialFile is returned if a special file copy fails

func (ErrCopySpecialFile) Error

func (e ErrCopySpecialFile) Error() string

type ErrUnsupportedFileType

type ErrUnsupportedFileType struct {
	Name string
	Mode os.FileMode
}

ErrUnsupportedFileType is returned when trying to extract a file that is not a regular file, a directory, or a symlink.

func (ErrUnsupportedFileType) Error

func (e ErrUnsupportedFileType) Error() string

type TarIterFunc

type TarIterFunc func(r *tar.Reader, hdr *tar.Header) error

TarIterFunc is called for each file inside a tar archive

type UnpackTarTransformFunc

type UnpackTarTransformFunc func(path string) (newPath string, err error)

UnpackTarTransformFunc can be used to change the names during unpack or to return a error for files that are not acceptable

type Winsize

type Winsize struct {
	Row uint16
	Col uint16

	Ypixel uint16 // unused
	// contains filtered or unexported fields
}

Winsize is from tty_ioctl(4)

func GetTermWinsize

func GetTermWinsize() (*Winsize, error)

GetTermWinsize performs the TIOCGWINSZ ioctl on stdout

Jump to

Keyboard shortcuts

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