fs

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2020 License: BSD-2-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package fs implements an OS independent abstraction of a file system suitable for backup purposes.

Index

Constants

View Source
const (
	O_RDONLY   int = syscall.O_RDONLY   // open the file read-only.
	O_WRONLY   int = syscall.O_WRONLY   // open the file write-only.
	O_RDWR     int = syscall.O_RDWR     // open the file read-write.
	O_APPEND   int = syscall.O_APPEND   // append data to the file when writing.
	O_CREATE   int = syscall.O_CREAT    // create a new file if none exists.
	O_EXCL     int = syscall.O_EXCL     // used with O_CREATE, file must not exist
	O_SYNC     int = syscall.O_SYNC     // open for synchronous I/O.
	O_TRUNC    int = syscall.O_TRUNC    // if possible, truncate file when opened.
	O_NONBLOCK int = syscall.O_NONBLOCK // don't block open on fifos etc.
)

Flags to OpenFile wrapping those of the underlying system. Not all flags may be implemented on a given system.

View Source
const O_NOFOLLOW int = syscall.O_NOFOLLOW

O_NOFOLLOW instructs the kernel to not follow symlinks when opening a file.

Variables

View Source
var ErrFileEmpty = errors.New("no data read")

ErrFileEmpty is returned inside a *os.PathError by Read() for the file opened from the fs provided by Reader when no data could be read and AllowEmptyFile is not set.

Functions

func Chmod

func Chmod(name string, mode os.FileMode) error

Chmod changes the mode of the named file to mode.

func Chtimes added in v0.8.1

func Chtimes(name string, atime time.Time, mtime time.Time) error

Chtimes changes the access and modification times of the named file, similar to the Unix utime() or utimes() functions.

The underlying filesystem may truncate or round the values to a less precise time unit. If there is an error, it will be of type *PathError.

func Create

func Create(name string) (*os.File, error)

Create creates the named file with mode 0666 (before umask), truncating it if it already exists. If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError.

func DeviceID

func DeviceID(fi os.FileInfo) (deviceID uint64, err error)

DeviceID extracts the device ID from an os.FileInfo object by casting it to syscall.Stat_t

func HasPathPrefix

func HasPathPrefix(base, p string) bool

HasPathPrefix returns true if p is a subdir of (or a file within) base. It assumes a file system which is case sensitive. If the paths are not of the same type (one is relative, the other is absolute), false is returned.

func IsRegularFile added in v0.9.0

func IsRegularFile(fi os.FileInfo) bool

IsRegularFile returns true if fi belongs to a normal file. If fi is nil, false is returned.

func Link(oldname, newname string) error

Link creates newname as a hard link to oldname. If there is an error, it will be of type *LinkError.

func Lstat

func Lstat(name string) (os.FileInfo, error)

Lstat returns the FileInfo structure describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. If there is an error, it will be of type *PathError.

func Mkdir

func Mkdir(name string, perm os.FileMode) error

Mkdir creates a new directory with the specified name and permission bits. If there is an error, it will be of type *PathError.

func MkdirAll

func MkdirAll(path string, perm os.FileMode) error

MkdirAll creates a directory named path, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm are used for all directories that MkdirAll creates. If path is already a directory, MkdirAll does nothing and returns nil.

func OpenFile

func OpenFile(name string, flag int, perm os.FileMode) (*os.File, error)

OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.

func Readlink(name string) (string, error)

Readlink returns the destination of the named symbolic link. If there is an error, it will be of type *PathError.

func Remove

func Remove(name string) error

Remove removes the named file or directory. If there is an error, it will be of type *PathError.

func RemoveAll

func RemoveAll(path string) error

RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).

func RemoveIfExists

func RemoveIfExists(filename string) error

RemoveIfExists removes a file, returning no error if it does not exist.

func Rename

func Rename(oldpath, newpath string) error

Rename renames (moves) oldpath to newpath. If newpath already exists, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories. If there is an error, it will be of type *LinkError.

func Stat

func Stat(name string) (os.FileInfo, error)

Stat returns a FileInfo structure describing the named file. If there is an error, it will be of type *PathError.

func Symlink(oldname, newname string) error

Symlink creates newname as a symbolic link to oldname. If there is an error, it will be of type *LinkError.

func TempFile

func TempFile(dir, prefix string) (f *os.File, err error)

TempFile creates a temporary file which has already been deleted (on supported platforms)

func TestChdir added in v0.9.0

func TestChdir(t testing.TB, dest string) (back func())

TestChdir changes the current directory to dest, the function back returns to the previous directory.

func Walk

func Walk(root string, walkFn filepath.WalkFunc) error

Walk walks the file tree rooted at root, calling walkFn for each file or directory in the tree, including root. All errors that arise visiting files and directories are filtered by walkFn. The files are walked in lexical order, which makes the output deterministic but means that for very large directories Walk can be inefficient. Walk does not follow symbolic links.

Types

type ExtendedFileInfo added in v0.9.0

type ExtendedFileInfo struct {
	os.FileInfo

	DeviceID  uint64 // ID of device containing the file
	Inode     uint64 // Inode number
	Links     uint64 // Number of hard links
	UID       uint32 // owner user ID
	GID       uint32 // owner group ID
	Device    uint64 // Device ID (if this is a device file)
	BlockSize int64  // block size for filesystem IO
	Blocks    int64  // number of allocated filesystem blocks
	Size      int64  // file size in byte

	AccessTime time.Time // last access time stamp
	ModTime    time.Time // last (content) modification time stamp
	ChangeTime time.Time // last status change time stamp
}

ExtendedFileInfo is an extended stat_t, filled with attributes that are supported by most operating systems. The original FileInfo is embedded.

func ExtendedStat added in v0.9.0

func ExtendedStat(fi os.FileInfo) ExtendedFileInfo

ExtendedStat returns an ExtendedFileInfo constructed from the os.FileInfo.

type FS added in v0.9.0

type FS interface {
	Open(name string) (File, error)
	OpenFile(name string, flag int, perm os.FileMode) (File, error)
	Stat(name string) (os.FileInfo, error)
	Lstat(name string) (os.FileInfo, error)

	Join(elem ...string) string
	Separator() string
	Abs(path string) (string, error)
	Clean(path string) string
	VolumeName(path string) string
	IsAbs(path string) bool

	Dir(path string) string
	Base(path string) string
}

FS bundles all methods needed for a file system.

type File

type File interface {
	io.Reader
	io.Writer
	io.Closer

	Fd() uintptr
	Readdirnames(n int) ([]string, error)
	Readdir(int) ([]os.FileInfo, error)
	Seek(int64, int) (int64, error)
	Stat() (os.FileInfo, error)
	Name() string
}

File is an open file on a file system.

func Open

func Open(name string) (File, error)

Open opens a file for reading.

func TestTempFile added in v0.9.0

func TestTempFile(t testing.TB, prefix string) (File, func())

TestTempFile returns a new temporary file, which is removed when cleanup() is called.

type Local added in v0.9.0

type Local struct{}

Local is the local file system. Most methods are just passed on to the stdlib.

func (Local) Abs added in v0.9.0

func (fs Local) Abs(path string) (string, error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.

func (Local) Base added in v0.9.0

func (fs Local) Base(path string) string

Base returns the last element of path.

func (Local) Clean added in v0.9.0

func (fs Local) Clean(p string) string

Clean returns the cleaned path. For details, see filepath.Clean.

func (Local) Dir added in v0.9.0

func (fs Local) Dir(path string) string

Dir returns path without the last element.

func (Local) IsAbs added in v0.9.0

func (fs Local) IsAbs(path string) bool

IsAbs reports whether the path is absolute.

func (Local) Join added in v0.9.0

func (fs Local) Join(elem ...string) string

Join joins any number of path elements into a single path, adding a Separator if necessary. Join calls Clean on the result; in particular, all empty strings are ignored. On Windows, the result is a UNC path if and only if the first path element is a UNC path.

func (Local) Lstat added in v0.9.0

func (fs Local) Lstat(name string) (os.FileInfo, error)

Lstat returns the FileInfo structure describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. If there is an error, it will be of type *PathError.

func (Local) Open added in v0.9.0

func (fs Local) Open(name string) (File, error)

Open opens a file for reading.

func (Local) OpenFile added in v0.9.0

func (fs Local) OpenFile(name string, flag int, perm os.FileMode) (File, error)

OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.

func (Local) Separator added in v0.9.0

func (fs Local) Separator() string

Separator returns the OS and FS dependent separator for dirs/subdirs/files.

func (Local) Stat added in v0.9.0

func (fs Local) Stat(name string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

func (Local) VolumeName added in v0.9.0

func (fs Local) VolumeName(path string) string

VolumeName returns leading volume name. Given "C:\foo\bar" it returns "C:" on Windows. Given "\\host\share\foo" it returns "\\host\share". On other platforms it returns "".

type Reader added in v0.9.0

type Reader struct {
	Name string
	io.ReadCloser

	// for FileInfo
	Mode    os.FileMode
	ModTime time.Time
	Size    int64

	AllowEmptyFile bool
	// contains filtered or unexported fields
}

Reader is a file system which provides a directory with a single file. When this file is opened for reading, the reader is passed through. The file can be opened once, all subsequent open calls return syscall.EIO. For Lstat(), the provided FileInfo is returned.

func (*Reader) Abs added in v0.9.0

func (fs *Reader) Abs(p string) (string, error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.

For the Reader, all paths are absolute.

func (*Reader) Base added in v0.9.0

func (fs *Reader) Base(p string) string

Base returns the last element of p.

func (*Reader) Clean added in v0.9.0

func (fs *Reader) Clean(p string) string

Clean returns the cleaned path. For details, see filepath.Clean.

func (*Reader) Dir added in v0.9.0

func (fs *Reader) Dir(p string) string

Dir returns p without the last element.

func (*Reader) IsAbs added in v0.9.0

func (fs *Reader) IsAbs(p string) bool

IsAbs reports whether the path is absolute. For the Reader, this is always the case.

func (*Reader) Join added in v0.9.0

func (fs *Reader) Join(elem ...string) string

Join joins any number of path elements into a single path, adding a Separator if necessary. Join calls Clean on the result; in particular, all empty strings are ignored. On Windows, the result is a UNC path if and only if the first path element is a UNC path.

func (*Reader) Lstat added in v0.9.0

func (fs *Reader) Lstat(name string) (os.FileInfo, error)

Lstat returns the FileInfo structure describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. If there is an error, it will be of type *PathError.

func (*Reader) Open added in v0.9.0

func (fs *Reader) Open(name string) (f File, err error)

Open opens a file for reading.

func (*Reader) OpenFile added in v0.9.0

func (fs *Reader) OpenFile(name string, flag int, perm os.FileMode) (f File, err error)

OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm, (0666 etc.) if applicable. If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.

func (*Reader) Separator added in v0.9.0

func (fs *Reader) Separator() string

Separator returns the OS and FS dependent separator for dirs/subdirs/files.

func (*Reader) Stat added in v0.9.0

func (fs *Reader) Stat(name string) (os.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

func (*Reader) VolumeName added in v0.9.0

func (fs *Reader) VolumeName(path string) string

VolumeName returns leading volume name, for the Reader file system it's always the empty string.

type Track added in v0.9.0

type Track struct {
	FS
}

Track is a wrapper around another file system which installs finalizers for open files which call panic() when they are not closed when the garbage collector releases them. This can be used to find resource leaks via open files.

func (Track) Open added in v0.9.0

func (fs Track) Open(name string) (File, error)

Open wraps the Open method of the underlying file system.

func (Track) OpenFile added in v0.9.0

func (fs Track) OpenFile(name string, flag int, perm os.FileMode) (File, error)

OpenFile wraps the OpenFile method of the underlying file system.

Jump to

Keyboard shortcuts

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