filesys

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 11 Imported by: 228

Documentation

Overview

Package filesys provides a file system abstraction, a subset of that provided by golang.org/pkg/os, with an on-disk and in-memory representation.

Index

Constants

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

Variables

This section is empty.

Functions

func InsertPathPart

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 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 IsHiddenFilePath added in v0.13.0

func IsHiddenFilePath(pattern string) bool

func MakeEmptyDirInMemory

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

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

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 RemoveHiddenFiles added in v0.13.0

func RemoveHiddenFiles(paths []string) []string

Removes paths containing hidden files/folders from a list of paths

func RootedPath

func RootedPath(elem ...string) string

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

func StripLeadingSeps

func StripLeadingSeps(s string) string

StripLeadingSeps trims leading filepath separators from input.

func StripTrailingSeps

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 ConfirmDir added in v0.13.8

func ConfirmDir(fSys FileSystem, path string) (ConfirmedDir, error)

ConfirmDir returns an error if the user-specified path is not an existing directory on fSys. Otherwise, ConfirmDir returns path, which can be relative, as a ConfirmedDir and all that implies.

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

	// ReadDir returns a list of files and directories within a directory.
	ReadDir(path string) ([]string, error)

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

type FileSystemOrOnDisk

type FileSystemOrOnDisk struct {
	FileSystem FileSystem
}

FileSystemOrOnDisk satisfies the FileSystem interface by forwarding all of its method calls to the given FileSystem whenever it's not nil. If it's nil, the call is forwarded to the OS's underlying file system.

func (FileSystemOrOnDisk) CleanedAbs

func (fs FileSystemOrOnDisk) CleanedAbs(path string) (ConfirmedDir, string, error)

func (FileSystemOrOnDisk) Create

func (fs FileSystemOrOnDisk) Create(path string) (File, error)

func (FileSystemOrOnDisk) Exists

func (fs FileSystemOrOnDisk) Exists(path string) bool

func (FileSystemOrOnDisk) Glob

func (fs FileSystemOrOnDisk) Glob(pattern string) ([]string, error)

func (FileSystemOrOnDisk) IsDir

func (fs FileSystemOrOnDisk) IsDir(path string) bool

func (FileSystemOrOnDisk) Mkdir

func (fs FileSystemOrOnDisk) Mkdir(path string) error

func (FileSystemOrOnDisk) MkdirAll

func (fs FileSystemOrOnDisk) MkdirAll(path string) error

func (FileSystemOrOnDisk) Open

func (fs FileSystemOrOnDisk) Open(path string) (File, error)

func (FileSystemOrOnDisk) ReadDir

func (fs FileSystemOrOnDisk) ReadDir(path string) ([]string, error)

func (FileSystemOrOnDisk) ReadFile

func (fs FileSystemOrOnDisk) ReadFile(path string) ([]byte, error)

func (FileSystemOrOnDisk) RemoveAll

func (fs FileSystemOrOnDisk) RemoveAll(path string) error

func (*FileSystemOrOnDisk) Set

func (fs *FileSystemOrOnDisk) Set(f FileSystem)

Set sets the given FileSystem as the target for all the FileSystem method calls.

func (FileSystemOrOnDisk) Walk

func (fs FileSystemOrOnDisk) Walk(path string, walkFn filepath.WalkFunc) error

func (FileSystemOrOnDisk) WriteFile

func (fs FileSystemOrOnDisk) WriteFile(path string, data []byte) error

Jump to

Keyboard shortcuts

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