osutil

package
v0.0.0-...-7cedb2b Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2016 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddExtraUser

func AddExtraUser(name string, sshKeys []string) error

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 Note that this will only work reliable in a single-threaded context.

func CopyFile

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

CopyFile copies src to dst

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 EnsureDirState

func EnsureDirState(dir, glob string, content map[string]*FileState) (changed, removed []string, err error)

EnsureDirState ensures that directory content matches expectations.

EnsureDirState enumerates all the files in the specified directory that match the provided pattern (glob). Each enumerated file is checked to ensure that the contents, permissions are what is desired. Unexpected files are removed. Missing files are created and differing files are corrected. Files not matching the pattern are ignored.

Note that EnsureDirState only checks for permissions and content. Other security mechanisms, including file ownership and extended attributes are *not* supported.

The content map describes each of the files that are intended to exist in the directory. Map keys must be file names relative to the directory. Sub-directories in the name are not allowed.

If writing any of the files fails, EnsureDirState switches to erase mode where *all* of the files managed by the glob pattern are removed (including those that may have been already written). The return value is an empty list of changed files, the real list of removed files and the first error.

If an error happens while removing files then such a file is not removed but the removal continues until the set of managed files matching the glob is exhausted.

In all cases, the function returns the first error it has encountered.

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 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(path string) bool

IsSymlink returns true if the given file is a symlink

func IsUIDInAny

func IsUIDInAny(uid uint32, groups ...string) bool

IsUIDInAny checks whether the given user belongs to any of the given groups

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 FileState

type FileState struct {
	Content []byte
	Mode    os.FileMode
}

FileState describes the expected content and meta data of a single file.

type Group

type Group struct {
	Name   string
	Passwd string
	Gid    uint
	Mem    []string
}

Group implements the grp.h struct group

func Getgrnam

func Getgrnam(name string) (result Group, err error)

Getgrnam returns a lit of groups for the given groupname

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