osutil

package
v0.0.0-...-677ed08 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2016 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSameState = fmt.Errorf("file state has not changed")

ErrSameState is returned when the state of a file has not changed.

Functions

func AddUser

func AddUser(name string, opts *AddUserOptions) 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 AtomicWriteFileChown

func AtomicWriteFileChown(filename string, data []byte, perm os.FileMode, flags AtomicWriteFlags, uid, gid int) (err error)

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 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 EnsureFileState

func EnsureFileState(filePath string, fileState *FileState) error

EnsureFileState ensures that the file is in the expected state. It will not attempt to remove the file if no content is provided.

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 FileDigest

func FileDigest(filename string, hash crypto.Hash) ([]byte, uint64, error)

FileDigest computes a hash digest of the file using the given hash. It also returns the file size.

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 MkdirAllChown

func MkdirAllChown(path string, perm os.FileMode, uid, gid int) error

MkdirAllChown is like os.MkdirAll but it calls os.Chown on any directories it creates.

func OutputErr

func OutputErr(output []byte, err error) error

OutputErr formats an error based on output if its length is not zero, or returns err otherwise.

func RealUser

func RealUser() (*user.User, error)

RealUser finds the user behind a sudo invocation, if applicable and possible.

Types

type AddUserOptions

type AddUserOptions struct {
	Sudoer     bool
	ExtraUsers bool
	Gecos      string
	SSHKeys    []string
}

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 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