filesys

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2020 License: Apache-2.0 Imports: 12 Imported by: 57

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 InsertPathPart added in v0.3.3

func InsertPathPart(path string, pos int, part string) string

InsertPathPart inserts 'part' at position 'pos' in the given filepath. The first position is 0.

E.g. if part == 'PEACH'

             OLD : NEW                    : POS
 --------------------------------------------------------
         {empty} : PEACH                  : irrelevant
               / : /PEACH                 : irrelevant
             pie : PEACH/pie              : 0 (or negative)
            /pie : /PEACH/pie             : 0 (or negative)
             raw : raw/PEACH              : 1 (or larger)
            /raw : /raw/PEACH             : 1 (or larger)
 a/nice/warm/pie : a/nice/warm/PEACH/pie  : 3
/a/nice/warm/pie : /a/nice/warm/PEACH/pie : 3

* An empty part results in no change.

  • Absolute paths get their leading '/' stripped, treated like relative paths, and the leading '/' is re-added on output. The meaning of pos is intentionally the same in either absolute or relative paths; if it weren't, this function could convert absolute paths to relative paths, which is not desirable.
  • For robustness (liberal input, conservative output) Pos values that that are too small (large) to index the split filepath result in a prefix (postfix) rather than an error. Use extreme position values to assure a prefix or postfix (e.g. 0 will always prefix, and 9999 will presumably always postfix).

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 PathJoin added in v0.3.3

func PathJoin(incoming []string) string

PathJoin converts a slice of string to a file path. If the first entry is an empty string, then the returned path is absolute (it has a leading slash). Desired: path == PathJoin(PathSplit(path))

func PathSplit added in v0.3.3

func PathSplit(incoming string) []string

PathSplit converts a file path to a slice of string. If the path is absolute (if the path has a leading slash), then the first entry in the result is an empty string. Desired: path == PathJoin(PathSplit(path))

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