filesys

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2019 License: Apache-2.0 Imports: 12 Imported by: 59

Documentation

Overview

Package filesys provides a file system abstraction layer.

Index

Constants

View Source
const (
	Separator = string(filepath.Separator)
	SelfDir   = "."
	ParentDir = ".."
)

Variables

This section is empty.

Functions

func MakeEmptyDirInMemory added in v0.3.0

func MakeEmptyDirInMemory() *fsNode

MakeEmptyDirInMemory returns an empty directory. The paths of nodes in this object will never report a leading Separator, meaning they aren't "absolute" in the sense defined by https://golang.org/pkg/path/filepath/#IsAbs.

func RootedPath

func RootedPath(elem ...string) string

RootedPath returns a rooted path, e.g. "/foo/bar" as opposed to "foo/bar".

func StripLeadingSeps added in v0.3.0

func StripLeadingSeps(s string) string

StripLeadingSeps trims leading filepath separators from input.

func StripTrailingSeps added in v0.3.0

func StripTrailingSeps(s string) string

StripTrailingSeps trims trailing filepath separators from input.

Types

type ConfirmedDir

type ConfirmedDir string

ConfirmedDir is a clean, absolute, delinkified path that was confirmed to point to an existing directory.

func NewTmpConfirmedDir

func NewTmpConfirmedDir() (ConfirmedDir, error)

NewTmpConfirmedDir returns a temporary dir, else error. The directory is cleaned, no symlinks, etc. so it's returned as a ConfirmedDir.

func (ConfirmedDir) HasPrefix

func (d ConfirmedDir) HasPrefix(path ConfirmedDir) bool

HasPrefix returns true if the directory argument is a prefix of self (d) from the point of view of a file system.

I.e., it's true if the argument equals or contains self (d) in a file path sense.

HasPrefix emulates the semantics of strings.HasPrefix such that the following are true:

strings.HasPrefix("foobar", "foobar")
strings.HasPrefix("foobar", "foo")
strings.HasPrefix("foobar", "")

d := fSys.ConfirmDir("/foo/bar")
d.HasPrefix("/foo/bar")
d.HasPrefix("/foo")
d.HasPrefix("/")

Not contacting a file system here to check for actual path existence.

This is tested on linux, but will have trouble on other operating systems. TODO(monopole) Refactor when #golang/go/18358 closes. See also:

https://github.com/golang/go/issues/18358
https://github.com/golang/dep/issues/296
https://github.com/golang/dep/blob/master/internal/fs/fs.go#L33
https://codereview.appspot.com/5712045

func (ConfirmedDir) Join

func (d ConfirmedDir) Join(path string) string

func (ConfirmedDir) String

func (d ConfirmedDir) String() string

type File

type File interface {
	io.ReadWriteCloser
	Stat() (os.FileInfo, error)
}

File groups the basic os.File methods.

type FileSystem

type FileSystem interface {
	// Create a file.
	Create(path string) (File, error)
	// MkDir makes a directory.
	Mkdir(path string) error
	// MkDirAll makes a directory path, creating intervening directories.
	MkdirAll(path string) error
	// RemoveAll removes path and any children it contains.
	RemoveAll(path string) error
	// Open opens the named file for reading.
	Open(path string) (File, error)
	// IsDir returns true if the path is a directory.
	IsDir(path string) bool
	// CleanedAbs converts the given path into a
	// directory and a file name, where the directory
	// is represented as a ConfirmedDir and all that implies.
	// If the entire path is a directory, the file component
	// is an empty string.
	CleanedAbs(path string) (ConfirmedDir, string, error)
	// Exists is true if the path exists in the file system.
	Exists(path string) bool
	// Glob returns the list of matching files,
	// emulating https://golang.org/pkg/path/filepath/#Glob
	Glob(pattern string) ([]string, error)
	// ReadFile returns the contents of the file at the given path.
	ReadFile(path string) ([]byte, error)
	// WriteFile writes the data to a file at the given path,
	// overwriting anything that's already there.
	WriteFile(path string, data []byte) error
	// Walk walks the file system with the given WalkFunc.
	Walk(path string, walkFn filepath.WalkFunc) error
}

FileSystem groups basic os filesystem methods. It's supposed be functional subset of https://golang.org/pkg/os

func MakeFsInMemory

func MakeFsInMemory() FileSystem

MakeFsInMemory returns an empty 'file system'. The paths of nodes in this object will always report a leading Separator, meaning they are "absolute" in the sense defined by https://golang.org/pkg/path/filepath/#IsAbs. This is a relevant difference when using Walk, Glob, Match, etc.

func MakeFsOnDisk

func MakeFsOnDisk() FileSystem

MakeFsOnDisk makes an instance of fsOnDisk.

Jump to

Keyboard shortcuts

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