fs

package module
v0.0.0-...-a606985 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 30 Imported by: 30

README

go-fs: A unified file system for Go

Go Reference

The package is built around a File type that is a string underneath and interprets its value as a local file system path or as a URI.

TODO

  • MemFileSystem
  • ZipFileSystem
  • Return ErrFileSystemClosed from all closable FS
  • S3
  • Test dropboxfs
  • Test fptfs
  • TestFileReads and TestFileMetaData for all FS
  • add TestFileWrites

Introduction

FileSystem implementations can be registered with their URI qualifiers like file:// or http://.

The methods of File parse their string value for a qualifier and look up a FileSystem in the Registry. The only special rule is, that if no qualifier is present, then the string value is interpreted as a local file path.

The LocalFileSystem is registered by default.

Work with Local directly:

fs.Local.Separator() // Either `/` or `\`

fs.Local.IsSymbolicLink("~/file") // Tilde expands to user home dir

For example, create a FileSystem from a multi-part HTTP form request that contains an uploaded file:

import "github.com/ungerik/go-fs/multipartfs"

multipartFS, err := multipartfs.FromRequestForm(request, MaxUploadSize)

defer multipartFS.Close()

// Access form values as string
multipartFS.Form.Value["email"] 

// Access form files as fs.File
file, err := multipartFS.FormFile("file")

// Use like any other fs.File
bytes, err := file.ReadAll(ctx)

fs.File

type File string

As a string-type it's easy to assign string literals and it can be const which would be impossible if File was an interface or struct:

const fileConst fs.File = "~/file.a"

var fileVar fs.File = "~/file.b"

fileVar = fileConst
fileVar = "~/file.c"

Handy to pass string literals of local paths or URIs to functions:

func readFile(f fs.File) { /* ... */ }

readFile("../my-local-file.txt")

// HTTP reading works when httpfs is imported
import _ "github.com/ungerik/go-fs/httpfs"

readFile("https://example.com/file-via-uri.txt")

As a string type File naturally marshals/unmarshals as string path/URI without having to implement marshaling interfaces.

But it implements fmt.Stringer to add the name of the path/URI filesystem as debug information and gob.GobEncoder, gob.GobDecoder to encode filename and content instead of the path/URI value.

Path related methods:

file := fs.TempDir().Join("file.txt")

dir := file.Dir()   // "/tmp" == fs.TempDir()
name := file.Name() // "file.txt"
path := file.Path() // "/tmp/file.txt"
url := file.URL()   // "file:///tmp/file.txt"
ext := file.Ext()   // ".txt"

file2 := file.Dir().Join("a", "b", "c").Joinf("file%d.txt", 2)
path2 := file2.Path() // "/tmp/a/b/c/file2.txt"

abs := fs.File("~/some-dir/../file").AbsPath() // "/home/erik/file"

Meta information:

size := file.Size() // int64, 0 for non existing or dirs
isDir := dir.IsDir()      // true
exists := file.Exists()   // true
fileIsDir := file.IsDir() // false
modTime := file.ModTime()
hash, err := file.ContentHash()  // Dropbox hash algo
regular := file.Info().IsRegular // true
info := file.Info().FSFileInfo() // io/fs.FileInfo

Reading and writing files

Reading:

bytes, err := file.ReadAll(ctx)

str, err := file.ReadAllString(ctx)

var w io.Writer
n, err := file.WriteTo(w)

f, err := file.OpenReader()     // io/fs.File 
r, err := file.OpenReadSeeker() // fs.ReadSeekCloser

Writing:

err := file.WriteAll(ctx, []byte("Hello"))

err := file.WriteAllString(ctx, "Hello")

err := file.Append(ctx, []byte("Hello"))

err := file.AppendString(ctx, "Hello")

var r io.Reader
n, err := file.ReadFrom(r)

w, err := file.OpenWriter()       // io.WriteCloser
w, err := file.OpenAppendWriter() // io.WriteCloser

rw, err := file.OpenReadWriter() // fs.ReadWriteSeekCloser

fs.FileReader

For cases where a file should be passed only for reading, it's recommended to use the interface type FileReader. It has all the read-related methods of File, so a File can be assigned or passed as FileReader:

type FileReader interface { /* ... */ }
func readFile(f fs.FileReader) { /* ... */ }

// An untyped string literal does not work as interface,
// needs a concrete type like fs.File
readFile(fs.File("../my-local-file.txt"))

fs.MemFile

MemFile combines the buffered in-memory data of a file with a filename to implement fs.FileReader. It exposes FileName and FileData as exported struct fields to emphasize its simple nature as just a wrapper of a name and some bytes.

type MemFile struct {
	FileName string
	FileData []byte
}

The type exists because it's very common to build up a file in memory and/or pass around some buffered file bytes together with a filename:

func readFile(f fs.FileReader) { /* ... */ }

readFile(fs.NewMemFile("hello-world.txt", []byte("Hello World!")))

// Read another fs.FileReader into a fs.MemFile
// to have it buffered in memory
memFile, err := fs.ReadMemFile(ctx, fs.File("../my-local-file.txt"))

// Read all data similar to io.ReadAll from an io.Reader
var r io.Rader
memFile, err := fs.ReadAllMemFile(cxt, r, "in-mem-file.txt")

Note that MemFile is not a File because it doesn't have a path or URI. The in-memory FileName is not interpreted as a path and should not contain path separators.

Listing directories

// Print names of all JPEGs in dir
dir.ListDir(func(f fs.File) error {
	_, err := fmt.Println(f.Name())
	return err
})

// Print names of all JPEGs in dir and all recursive sub-dirs
// with cancelable context
dir.ListDirRecursiveContext(ctx, func(f fs.File) error {
	_, err := fmt.Println(f.Name())
	return err
}, "*.jpg", "*.jpeg")

// Get all files in dir without limit
files, err := dir.ListDirMax(-1)

// Get the first 100 JPEGs in dir
files, err := dir.ListDirMaxContext(ctx, 100, "*.jpg", "*.jpeg")

Watching the local file system

TODO description

Documentation

Index

Constants

View Source
const (
	// LocalPrefix is the prefix of the LocalFileSystem
	LocalPrefix = "file://"

	// Separator used in LocalFileSystem paths
	Separator = string(filepath.Separator)
)
View Source
const InvalidFile = File("")

InvalidFile is a file with an empty path and thus invalid.

View Source
const PrefixSeparator = "://"

Variables

View Source
var ErrEmptyPath = NewErrDoesNotExist("")

ErrEmptyPath indications an empty file path

Functions

func CopyFile

func CopyFile(ctx context.Context, src FileReader, dest File, perm ...Permissions) error

CopyFile copies a single file between different file systems. If dest has a path that does not exist, then all directories up to that path will be created. If dest is an existing directory, then a file with the base name of src will be created there.

func CopyFileBuf

func CopyFileBuf(ctx context.Context, src FileReader, dest File, buf *[]byte, perm ...Permissions) error

CopyFileBuf copies a single file between different file systems. If dest has a path that does not exist, then all directories up to that path will be created. If dest is an existing directory, then a file with the base name of src will be created there. An pointer to a []byte variable must be passed for buf. If that variable holds a non zero length byte slice then this slice will be used as buffer, else a byte slice will be allocated and assigned to the variable. Use this function to re-use buffers between CopyFileBuf calls.

func CopyRecursive

func CopyRecursive(ctx context.Context, src, dest File, patterns ...string) error

CopyRecursive can copy between files of different file systems. The filter patterns are applied on filename level, not the whole path.

func FileContentHash

func FileContentHash(ctx context.Context, fileReader FileReader, hashFunc ContentHashFunc) (string, error)

FileContentHash returns the hashFunc result for fileReader

func FileInfoToFileCallback

func FileInfoToFileCallback(fileCallback func(File) error) func(*FileInfo) error

FileInfoToFileCallback converts a File callback function into a FileInfo callback function that is calling the passed fileCallback with the FileInfo.File.

func FileReaderContentHashIndex

func FileReaderContentHashIndex(ctx context.Context, fileReades []FileReader, hash string) (int, error)

FileReaderContentHashIndex returns the slice index of the first FileReader with the passed hash returned from its ContentHashContext method or -1 in case of no match.

func FileReaderLocalPathIndex

func FileReaderLocalPathIndex(fileReades []FileReader, localPath string) int

FileReaderLocalPathIndex returns the slice index of the first FileReader with the passed localPath returned from its LocalPath method or -1 in case of no match.

func FileReaderNameIndex

func FileReaderNameIndex(fileReades []FileReader, filename string) int

FileReaderNameIndex returns the slice index of the first FileReader with the passed filename returned from its Name method or -1 in case of no match.

func FileReaderNotExistsIndex

func FileReaderNotExistsIndex(fileReades []FileReader, filename string) int

FileReaderNotExistsIndex returns the slice index of the first FileReader where the Exists method returned false or -1 in case of no match.

func FilesToNames

func FilesToNames(files []File) []string

FilesToNames returns a string slice with the names pars from the files

func FilesToPaths

func FilesToPaths(files []File) []string

FilesToPaths returns the FileSystem specific paths of a slice of Files.

func FilesToURLs

func FilesToURLs(files []File) []string

FilesToURLs returns the URLs of a slice of Files.

func IdenticalDirContents

func IdenticalDirContents(ctx context.Context, dirA, dirB File, recursive bool) (identical bool, err error)

IdenticalDirContents returns true if the files in dirA and dirB are identical in size and content. If recursive is true, then directories will be considered too.

func IdenticalFileContents

func IdenticalFileContents(ctx context.Context, files ...FileReader) (identical bool, err error)

IdenticalFileContents returns if the passed files have identical content. An error is returned if one of the files does not exist (ErrDoesNotExist) or if less than 2 files are passed. Compares files larger than 16MB via content hash to not allocate too much memory.

func IsRegistered

func IsRegistered(fs FileSystem) bool

IsRegistered returns true if the file system is registered with its prefix.

func Move

func Move(ctx context.Context, source, destination File) error

Move moves and/or renames source to destination. source and destination can be files or directories. If source is a directory, it will be moved with all its contents. If source and destination are using the same FileSystem, then FileSystem.Move will be used, else source will be copied recursively first to destination and then deleted.

func ReadAllContext

func ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error)

ReadAllContext reads all data from r until EOF is reached, another error is returned, or the context got canceled. It is identical to io.ReadAll except that it can be canceled via a context.

func Register

func Register(fs FileSystem) int

Register adds a file system or increments its reference count if it is already registered. The function returns the reference file system's reference count.

func Remove

func Remove(fileURIs ...string) error

Remove removes all files with fileURIs. If a file does not exist, then it is skipped and not reported as error.

func RemoveErrDoesNotExist

func RemoveErrDoesNotExist(err error) error

RemoveErrDoesNotExist returns nil if err wraps os.ErrNotExist, else err will be returned unchanged.

func RemoveFile

func RemoveFile(file File) error

RemoveFile removes a single file. It's just a wrapper for calling file.Remove(), useful mostly as callback for methods that list files to delete all files of a certain pattern. Or as a more elegant way to remove a file passed as string literal path:

fs.RemoveFile("/my/hardcoded.path")

func RemoveFiles

func RemoveFiles(files ...File) error

RemoveFiles removes all files. If a file does not exist, then it is skipped and not reported as error.

func SameFile

func SameFile(a, b File) bool

SameFile returns if a and b describe the same file or directory

func ServeFileHTTP

func ServeFileHTTP(response http.ResponseWriter, request *http.Request, file FileReader, contentType ...string)

ServeFileHTTP serves the passed file with a Content-Type header via HTTP. If no contentType is passed then it will be deduced from the filename and if that fails from the content. A status code 404 error is returned if the file does not exist and a status code 500 error if there was any other error while reading it.

Uses http.ServeContent under the hood.

func ServeFileHTTPHandler

func ServeFileHTTPHandler(file FileReader, contentType ...string) http.Handler

ServeFileHTTPHandler returns a http.Handler that serves the passed file with a Content-Type header via HTTP. If no contentType is passed then it will be deduced from the filename and if that fails from the content. A status code 404 error is returned if the file does not exist and a status code 500 error if there was any other error while reading it.

Uses http.ServeContent under the hood.

func SortByModified

func SortByModified(files []File)

func SortByModifiedDirsFirst

func SortByModifiedDirsFirst(files []File)

func SortByName

func SortByName(files []File)

func SortByNameDirsFirst

func SortByNameDirsFirst(files []File)

func SortByPath

func SortByPath(files []File)

func SortBySize

func SortBySize(files []File)

func Unregister

func Unregister(fs FileSystem) int

Unregister a file system decrements its reference count and removes it when the reference count reaches 0. If the file system is not registered, -1 is returned.

func WriteAllContext

func WriteAllContext(ctx context.Context, w io.Writer, data []byte) error

WriteAllContext writes all data wo the to w with a cancelable context.

Types

type AppendFileSystem

type AppendFileSystem interface {
	FileSystem

	Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error
}

type AppendWriterFileSystem

type AppendWriterFileSystem interface {
	FileSystem

	OpenAppendWriter(filePath string, perm []Permissions) (WriteCloser, error)
}

type ContentHashFunc

type ContentHashFunc func(ctx context.Context, reader io.Reader) (string, error)

ContentHashFunc is used tot return the string representation of a content hash by reading from an io.Reader until io.EOF or the context is cancelled.

var DefaultContentHash ContentHashFunc = fsimpl.DropboxContentHash

DefaultContentHash configures the default content hash function used by methods like File.ContentHash and FileReader.ContentHash.

func ContentHashFuncFrom

func ContentHashFuncFrom(h hash.Hash) ContentHashFunc

ContentHashFuncFrom returns a ContentHashFunc that uses a standard hash.Hash implementation to return the hash sum as hex encoded string.

type CopyFileSystem

type CopyFileSystem interface {
	FileSystem

	// CopyFile copies a single file.
	// buf must point to a []byte variable.
	// If that variable is initialized with a byte slice, then this slice will be used as buffer,
	// else a byte slice will be allocated for the variable.
	CopyFile(ctx context.Context, srcFile string, destFile string, buf *[]byte) error
}

CopyFileSystem can be implemented by file systems that have native file copying functionality.

If a file system does not implement this interface then it's functionality will be emulated with other methods.

type ErrAlreadyExists

type ErrAlreadyExists struct {
	// contains filtered or unexported fields
}

ErrAlreadyExists is returned when a file already exists. It wraps os.ErrExist, check for this error type with:

errors.Is(err, os.ErrExist)

func NewErrAlreadyExists

func NewErrAlreadyExists(file File) ErrAlreadyExists

NewErrAlreadyExists returns a new ErrAlreadyExists

func (ErrAlreadyExists) Error

func (err ErrAlreadyExists) Error() string

func (ErrAlreadyExists) File

func (err ErrAlreadyExists) File() File

File returns the file that already exists

func (ErrAlreadyExists) Unwrap

func (ErrAlreadyExists) Unwrap() error

Unwrap returns os.ErrExist

type ErrDoesNotExist

type ErrDoesNotExist struct {
	// contains filtered or unexported fields
}

ErrDoesNotExist is returned when a file does not exist and wraps os.ErrNotExist. Check for this error type with:

errors.Is(err, os.ErrNotExist)

Implements http.Handler by responding with http.NotFound.

func NewErrDoesNotExist

func NewErrDoesNotExist(file File) ErrDoesNotExist

NewErrDoesNotExist returns a new ErrDoesNotExist

func NewErrDoesNotExistFileReader

func NewErrDoesNotExistFileReader(fileReader FileReader) ErrDoesNotExist

NewErrDoesNotExistFileReader returns an ErrDoesNotExist error for a FileReader.

func NewErrPathDoesNotExist

func NewErrPathDoesNotExist(path string) ErrDoesNotExist

NewErrPathDoesNotExist returns an ErrDoesNotExist error for a file path.

func (ErrDoesNotExist) Error

func (err ErrDoesNotExist) Error() string

Error implements the error interface

func (ErrDoesNotExist) File

func (err ErrDoesNotExist) File() (file File, ok bool)

File returns the file that error concerns

func (ErrDoesNotExist) FileReader

func (err ErrDoesNotExist) FileReader() (file FileReader, ok bool)

func (ErrDoesNotExist) ServeHTTP

func (err ErrDoesNotExist) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (ErrDoesNotExist) Unwrap

func (ErrDoesNotExist) Unwrap() error

Unwrap returns os.ErrNotExist

type ErrIsDirectory

type ErrIsDirectory struct {
	// contains filtered or unexported fields
}

ErrIsDirectory is returned when an operation is not possible because a file is a directory.

func NewErrIsDirectory

func NewErrIsDirectory(file File) ErrIsDirectory

NewErrIsDirectory returns a new ErrIsDirectory

func (ErrIsDirectory) Error

func (err ErrIsDirectory) Error() string

func (ErrIsDirectory) File

func (err ErrIsDirectory) File() File

File returns the file that error concerns

type ErrIsNotDirectory

type ErrIsNotDirectory struct {
	// contains filtered or unexported fields
}

ErrIsNotDirectory is returned when an operation is not possible because a file is not a directory.

func NewErrIsNotDirectory

func NewErrIsNotDirectory(file File) ErrIsNotDirectory

NewErrIsNotDirectory returns a new ErrIsNotDirectory

func (ErrIsNotDirectory) Error

func (err ErrIsNotDirectory) Error() string

func (ErrIsNotDirectory) File

func (err ErrIsNotDirectory) File() File

File returns the file that error concerns

type ErrPermission

type ErrPermission struct {
	// contains filtered or unexported fields
}

ErrPermission is returned when an operation lacks permissions on a file. It wraps os.ErrPermission. Check for this error type with:

errors.Is(err, os.ErrPermission)

Implements http.Handler by responding with 403 Forbidden.

func NewErrPermission

func NewErrPermission(file File) ErrPermission

NewErrPermission returns a new ErrPermission

func (ErrPermission) Error

func (err ErrPermission) Error() string

func (ErrPermission) File

func (err ErrPermission) File() File

File returns the file that error concerns

func (ErrPermission) ServeHTTP

func (err ErrPermission) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (ErrPermission) Unwrap

func (ErrPermission) Unwrap() error

Unwrap returns os.ErrPermission

type ErrUnsupported

type ErrUnsupported struct {
	// contains filtered or unexported fields
}

func NewErrUnsupported

func NewErrUnsupported(fileSystem FileSystem, operation string) ErrUnsupported

NewErrUnsupported returns a new ErrUnsupported

func (ErrUnsupported) Error

func (err ErrUnsupported) Error() string

func (ErrUnsupported) Unwrap

func (ErrUnsupported) Unwrap() error

type Event

type Event int

Event reported for watched files.

This is a bitmask and some systems may send multiple operations at once. Use the Has... methods to check if an event has a certain operation.

func (Event) HasChmod

func (e Event) HasChmod() bool

func (Event) HasCreate

func (e Event) HasCreate() bool

func (Event) HasRemove

func (e Event) HasRemove() bool

func (Event) HasRename

func (e Event) HasRename() bool

func (Event) HasWrite

func (e Event) HasWrite() bool

func (Event) String

func (e Event) String() string

type ExistsFileSystem

type ExistsFileSystem interface {
	FileSystem

	// Exists returns if a file exists and is accessible.
	// Depending on the FileSystem implementation,
	// this could be faster than using Stat.
	// Note that a file could exist but might not be accessible.
	Exists(filePath string) bool
}

type File

type File string

File is a local file system path or a complete URI. It is a string underneath, so string literals can be passed everywhere a File is expected. Marshalling functions that use reflection will also work out of the box when they detect that File is of kind reflect.String. File implements FileReader.

func CleanFilePath

func CleanFilePath(uri string) File

CleanFilePath returns a File from uri with cleaned path and a file system prefix

func CurrentWorkingDir

func CurrentWorkingDir() File

CurrentWorkingDir returns the current working directory of the process. In case of an erorr, Exists() of the result File will return false.

func Executable

func Executable() File

Executable returns a File for the executable that started the current process. It wraps os.Executable, see https://golang.org/pkg/os/#Executable

func Filef

func Filef(format string, args ...any) File

Filef is a shortcut for File(fmt.Sprintf(format, args...))

func HomeDir

func HomeDir() File

HomeDir returns the home directory of the current user.

func JoinCleanFilePath

func JoinCleanFilePath(uriParts ...string) File

JoinCleanFilePath returns a File from joined and cleaned uriParts with a file system prefix

func MakeTempDir

func MakeTempDir() (File, error)

MakeTempDir makes and returns a new randomly named sub directory in TempDir(). Example:

tempDir, err := fs.MakeTempDir()
if err != nil {
    return err
}
defer tempDir.RemoveRecursive()
doThingsWith(tempDir)

func MustMakeTempDir

func MustMakeTempDir() File

MustMakeTempDir makes and returns a new randomly named sub directory in TempDir(). It panics on errors. Example:

tempDir := fs.MustMakeTempDir()
defer tempDir.RemoveRecursive()
doThingsWith(tempDir)

func StringsToFiles

func StringsToFiles(fileURIs []string) []File

StringsToFiles returns Files for the given fileURIs.

func TempDir

func TempDir() File

TempDir returns the temp directory of the operating system

func TempFile

func TempFile(ext ...string) File

TempFile returns a randomly named File with an optional extension in the temp directory of the operating system. The returned File does not exist yet, it's just a path.

func (File) AbsPath

func (file File) AbsPath() string

AbsPath returns the absolute path of the file depending on the file system.

func (File) Append

func (file File) Append(ctx context.Context, data []byte, perm ...Permissions) error

func (File) AppendString

func (file File) AppendString(ctx context.Context, str string, perm ...Permissions) error

func (File) CheckExists

func (file File) CheckExists() error

CheckExists return an ErrDoesNotExist error if the file does not exist or ErrEmptyPath if the file path is empty.

func (File) CheckIsDir

func (file File) CheckIsDir() error

CheckIsDir return an ErrDoesNotExist error if the file does not exist, ErrEmptyPath if the file path is empty, or ErrIsNotDirectory if a file exists, but is not a directory, or nil if the file is a directory.

func (File) ContentHash

func (file File) ContentHash() (string, error)

ContentHash returns the DefaultContentHash for the file. If the FileSystem implementation does not have this hash pre-computed, then the whole file is read to compute it. If the file is a directory, then an empty string will be returned.

func (File) ContentHashContext

func (file File) ContentHashContext(ctx context.Context) (string, error)

ContentHashContext returns the DefaultContentHash for the file. If the FileSystem implementation does not have this hash pre-computed, then the whole file is read to compute it. If the file is a directory, then an empty string will be returned.

func (File) Dir

func (file File) Dir() File

Dir returns the parent directory of the File.

func (File) DirAndName

func (file File) DirAndName() (dir File, name string)

DirAndName returns the parent directory of filePath and the name with that directory of the last filePath element. If filePath is the root of the file systeme, then an empty string will be returned for name.

func (File) Exists

func (file File) Exists() bool

Exists returns a file or directory with the path of File exists.

func (File) Ext

func (file File) Ext() string

Ext returns the extension of file name including the point, or an empty string.

Example:

File("image.png").Ext() == ".png"
File("dir.with.ext/file").Ext() == ""
File("dir.with.ext/file.ext").Ext() == ".ext"

func (File) ExtLower

func (file File) ExtLower() string

ExtLower returns the lower case extension of file name including the point, or an empty string. Example: File("Image.PNG").ExtLower() == ".png"

func (File) FileSystem

func (file File) FileSystem() FileSystem

FileSystem returns the FileSystem of the File. Defaults to Local if not a complete URI, or Invalid for an empty path.

func (File) GobDecode

func (file File) GobDecode(gobBytes []byte) error

GobDecode decodes a file name and content from gobBytes and writes the content to this file ignoring the decoded name. Implements encoding/gob.GobDecoder.

func (File) GobEncode

func (file File) GobEncode() ([]byte, error)

GobEncode reads and gob encodes the file name and content, implementing encoding/gob.GobEncoder.

func (File) Group

func (file File) Group() (string, error)

func (File) HasAbsPath

func (file File) HasAbsPath() bool

HasAbsPath returns wether the file has an absolute path depending on the file system.

func (File) Info

func (file File) Info() *FileInfo

Info returns FileInfo.

Use File.Stat to get a standard library io/fs.FileInfo.

func (File) IsDir

func (file File) IsDir() bool

IsDir returns a directory with the path of File exists.

func (File) IsEmptyDir

func (file File) IsEmptyDir() bool

IsEmptyDir returns if file is an empty directory.

func (File) IsHidden

func (file File) IsHidden() bool

IsHidden returns true if the filename begins with a dot, or if on Windows the hidden file attribute is set.

func (File) IsReadable

func (file File) IsReadable() bool

IsReadable returns if the file exists and is readable.

func (File) IsRegular

func (file File) IsRegular() bool

IsRegular reports if this is a regular file.

func (file File) IsSymbolicLink() bool

IsSymbolicLink returns if the file is a symbolic link Use LocalFileSystem.CreateSymbolicLink and LocalFileSystem.ReadSymbolicLink to handle symbolic links.

func (File) IsWriteable

func (file File) IsWriteable() bool

IsWriteable returns if the file exists and is writeable or in case it doesn't exist, if the parent directory exists and is writeable.

func (File) Join

func (file File) Join(pathParts ...string) File

Join returns a new File with pathParts cleaned and joined to the current File's URI. Every element of pathParts is a subsequent directory or file that will be appended to the File URI with a path separator. The resulting URI path will be cleaned, removing relative directory names like "..".

func (File) Joinf

func (file File) Joinf(format string, args ...any) File

Joinf returns a new File with smf.Sprintf(format, args...) cleaned and joined to the current File's URI. The resulting URI path will be cleaned, removing relative directory names like "..".

func (File) ListDir

func (file File) ListDir(callback func(File) error, patterns ...string) error

ListDir calls the passed callback function for every file and directory in dirPath. If any patterns are passed, then only files with a name that matches at least one of the patterns are returned.

func (File) ListDirChan

func (file File) ListDirChan(cancel <-chan error, patterns ...string) (<-chan File, <-chan error)

ListDirChan returns listed files over a channel. An error or nil will returned from the error channel. The file channel will be closed after sending all files. If cancel is not nil and an error is sent to this channel, then the listing will be canceled and the error returned in the error channel returned by the method. See pipeline pattern: http://blog.golang.org/pipelines

func (File) ListDirContext

func (file File) ListDirContext(ctx context.Context, callback func(File) error, patterns ...string) error

ListDirContext calls the passed callback function for every file and directory in dirPath. If any patterns are passed, then only files with a name that matches at least one of the patterns are returned.

func (File) ListDirInfo

func (file File) ListDirInfo(callback func(*FileInfo) error, patterns ...string) error

ListDirInfo calls the passed callback function for every file and directory in dirPath. If any patterns are passed, then only files with a name that matches at least one of the patterns are returned.

func (File) ListDirInfoContext

func (file File) ListDirInfoContext(ctx context.Context, callback func(*FileInfo) error, patterns ...string) error

ListDirInfoContext calls the passed callback function for every file and directory in dirPath. If any patterns are passed, then only files with a name that matches at least one of the patterns are returned.

func (File) ListDirInfoRecursive

func (file File) ListDirInfoRecursive(callback func(*FileInfo) error, patterns ...string) error

ListDirInfoRecursive calls the passed callback function for every file (not directory) in dirPath recursing into all sub-directories. If any patterns are passed, then only files (not directories) with a name that matches at least one of the patterns are returned.

func (File) ListDirInfoRecursiveContext

func (file File) ListDirInfoRecursiveContext(ctx context.Context, callback func(*FileInfo) error, patterns ...string) error

ListDirInfoRecursiveContext calls the passed callback function for every file (not directory) in dirPath recursing into all sub-directories. If any patterns are passed, then only files (not directories) with a name that matches at least one of the patterns are returned.

func (File) ListDirMax

func (file File) ListDirMax(max int, patterns ...string) (files []File, err error)

ListDirMax returns at most max files and directories in dirPath. A max value of -1 returns all files. If any patterns are passed, then only files or directories with a name that matches at least one of the patterns are returned.

func (File) ListDirMaxContext

func (file File) ListDirMaxContext(ctx context.Context, max int, patterns ...string) (files []File, err error)

ListDirMaxContext returns at most max files and directories in dirPath. A max value of -1 returns all files. If any patterns are passed, then only files or directories with a name that matches at least one of the patterns are returned.

func (File) ListDirRecursive

func (file File) ListDirRecursive(callback func(File) error, patterns ...string) error

ListDirRecursive returns only files. patterns are only applied to files, not to directories

func (File) ListDirRecursiveChan

func (file File) ListDirRecursiveChan(cancel <-chan error, patterns ...string) (<-chan File, <-chan error)

ListDirRecursiveChan returns listed files over a channel. An error or nil will returned from the error channel. The file channel will be closed after sending all files. If cancel is not nil and an error is sent to this channel, then the listing will be canceled and the error returned in the error channel returned by the method. See pipeline pattern: http://blog.golang.org/pipelines

func (File) ListDirRecursiveContext

func (file File) ListDirRecursiveContext(ctx context.Context, callback func(File) error, patterns ...string) error

ListDirRecursiveContext returns only files. patterns are only applied to files, not to directories

func (File) ListDirRecursiveMax

func (file File) ListDirRecursiveMax(max int, patterns ...string) (files []File, err error)

func (File) ListDirRecursiveMaxContext

func (file File) ListDirRecursiveMaxContext(ctx context.Context, max int, patterns ...string) (files []File, err error)

func (File) LocalPath

func (file File) LocalPath() string

LocalPath returns the cleaned local file-system path of the file, or an empty string if it is not on the local file system.

func (File) MakeAllDirs

func (file File) MakeAllDirs(perm ...Permissions) error

MakeAllDirs creates all directories up to this one, does not return an error if the directories already exist

func (File) MakeDir

func (file File) MakeDir(perm ...Permissions) error

MakeDir creates a directory if it does not exist yet. No error is returned if the directory already exists.

func (File) Modified

func (file File) Modified() time.Time

func (File) MoveTo

func (file File) MoveTo(destination File) error

MoveTo moves and/or renames the file to destination. destination can be a directory or file-path and can be on another FileSystem.

func (File) MustLocalPath

func (file File) MustLocalPath() string

MustLocalPath returns the cleaned local file-system path of the file, or panics if it is not on the local file system or an empty path.

func (File) Name

func (file File) Name() string

Name returns the name part of the file path, which is usually the string after the last path Separator.

func (File) OpenAppendWriter

func (file File) OpenAppendWriter(perm ...Permissions) (WriteCloser, error)

func (File) OpenReadSeeker

func (file File) OpenReadSeeker() (ReadSeekCloser, error)

OpenReadSeeker opens the file and returns a ReadSeekCloser. If the FileSystem implementation doesn't support ReadSeekCloser, then the complete file is read into memory and wrapped with a ReadSeekCloser. Warning: this can use up a lot of memory for big files.

func (File) OpenReadWriter

func (file File) OpenReadWriter(perm ...Permissions) (ReadWriteSeekCloser, error)

func (File) OpenReader

func (file File) OpenReader() (ReadCloser, error)

OpenReader opens the file and returns a io/fs.File that has to be closed after reading

func (File) OpenWriter

func (file File) OpenWriter(perm ...Permissions) (WriteCloser, error)

func (File) ParseRawURI

func (file File) ParseRawURI() (fs FileSystem, fsPath string)

ParseRawURI returns a FileSystem for the passed URI and the path component within that file system. Returns the local file system if no other file system could be identified.

func (File) Path

func (file File) Path() string

Path returns the cleaned path of the file. It may differ from the string value of File because it will be cleaned depending on the FileSystem

func (File) PathWithSlashes

func (file File) PathWithSlashes() string

PathWithSlashes returns the cleaned path of the file always using the slash '/' as separator. It may differ from the string value of File because it will be cleaned depending on the FileSystem

func (File) Permissions

func (file File) Permissions() Permissions

func (File) RawURI

func (file File) RawURI() string

RawURI rurns the string value of File.

func (File) ReadAll

func (file File) ReadAll() (data []byte, err error)

ReadAll reads and returns all bytes of the file

func (File) ReadAllContentHash

func (file File) ReadAllContentHash(ctx context.Context) (data []byte, hash string, err error)

ReadAllContentHash reads and returns all bytes of the file together with the DefaultContentHash.

func (File) ReadAllContext

func (file File) ReadAllContext(ctx context.Context) (data []byte, err error)

ReadAllContext reads and returns all bytes of the file

func (File) ReadAllString

func (file File) ReadAllString() (string, error)

ReadAllString reads the complete file and returns the content as string.

func (File) ReadAllStringContext

func (file File) ReadAllStringContext(ctx context.Context) (string, error)

ReadAllStringContext reads the complete file and returns the content as string.

func (File) ReadFrom

func (file File) ReadFrom(reader io.Reader) (n int64, err error)

ReadFrom implements the io.ReaderFrom interface, the file is writter with the existing permissions if it exists, or with the default write permissions if it does not exist yet.

func (File) ReadJSON

func (file File) ReadJSON(ctx context.Context, output any) error

ReadJSON reads and unmarshalles the JSON content of the file to output.

func (File) ReadXML

func (file File) ReadXML(ctx context.Context, output any) error

ReadXML reads and unmarshalles the XML content of the file to output.

func (File) Remove

func (file File) Remove() error

Remove deletes the file.

func (File) RemoveDirContents

func (file File) RemoveDirContents(patterns ...string) error

RemoveDirContents deletes all files in this directory, or if given all files with patterns from the this directory.

func (File) RemoveDirContentsContext

func (file File) RemoveDirContentsContext(ctx context.Context, patterns ...string) error

RemoveDirContentsContext deletes all files in this directory, or if given all files with patterns from the this directory.

func (File) RemoveDirContentsRecursive

func (file File) RemoveDirContentsRecursive() error

RemoveDirContentsRecursive deletes all files and directories in this directory recursively.

func (File) RemoveDirContentsRecursiveContext

func (file File) RemoveDirContentsRecursiveContext(ctx context.Context) error

RemoveDirContentsRecursiveContext deletes all files and directories in this directory recursively.

func (File) RemoveRecursive

func (file File) RemoveRecursive() error

RemoveRecursive deletes the file or if it's a directory the complete recursive directory tree.

func (File) RemoveRecursiveContext

func (file File) RemoveRecursiveContext(ctx context.Context) error

RemoveRecursiveContext deletes the file or if it's a directory the complete recursive directory tree.

func (File) Rename

func (file File) Rename(newName string) (renamedFile File, err error)

Rename changes the name of a file where newName is the name part after file.Dir(). Note: this does not move the file like in other rename implementations, it only changes the name of the file within its directdory.

func (File) Renamef

func (file File) Renamef(newNameFormat string, args ...any) (renamedFile File, err error)

Renamef changes the name of a file where fmt.Sprintf(newNameFormat, args...) is the name part after file.Dir(). Note: this does not move the file like in other rename implementations, it only changes the name of the file within its directdory.

func (File) SetGroup

func (file File) SetGroup(group string) error

func (File) SetPermissions

func (file File) SetPermissions(perm Permissions) error

func (File) SetUser

func (file File) SetUser(user string) error

func (File) Size

func (file File) Size() int64

Size returns the size of the file or 0 if it does not exist or is a directory.

func (File) Stat

func (file File) Stat() (iofs.FileInfo, error)

Stat returns a standard library io/fs.FileInfo describing the file.

func (File) StdDirEntry

func (file File) StdDirEntry() StdDirEntry

StdDirEntry wraps the file as a StdDirEntry struct that implements the io/fs.DirEntry interface from the standard library for a File.

func (File) StdFS

func (file File) StdFS() StdFS

StdFS wraps the file as a StdFS struct that implements the io/fs.FS interface of the standard library for a File.

StdFS implements the following interfaces:

  • io/fs.FS
  • io/fs.SubFS
  • io/fs.StatFS
  • io/fs.GlobFS
  • io/fs.ReadDirFS
  • io/fs.ReadFileFS

func (File) String

func (file File) String() string

String returns information about the File and its FileSystem. String implements the fmt.Stringer interface.

func (File) ToAbsPath

func (file File) ToAbsPath() File

ToAbsPath returns the file with an absolute path depending on the file system.

func (File) Touch

func (file File) Touch(perm ...Permissions) error

func (File) TrimExt

func (file File) TrimExt() File

TrimExt returns a File with a path where the extension is removed. Note that this does not rename an actual existing file.

func (File) Truncate

func (file File) Truncate(newSize int64) error

func (File) URL

func (file File) URL() string

URL of the file

func (File) User

func (file File) User() (string, error)

func (File) VolumeName

func (file File) VolumeName() string

VolumeName returns the name of the volume at the beginning of the file path, or an empty string if the path has no volume. A volume is for example "C:" on Windows

func (File) Watch

func (file File) Watch(onEvent func(File, Event)) (cancel func() error, err error)

Watch a file or directory for changes. If the file describes a directory then changes directly within it will be reported. This does not apply changes in deeper recursive sub-directories.

It is valid to watch a file with multiple callbacks, calling the returned cancel function will cancel a particular watch.

func (File) WriteAll

func (file File) WriteAll(data []byte, perm ...Permissions) error

func (File) WriteAllContext

func (file File) WriteAllContext(ctx context.Context, data []byte, perm ...Permissions) error

func (File) WriteAllString

func (file File) WriteAllString(str string, perm ...Permissions) error

func (File) WriteAllStringContext

func (file File) WriteAllStringContext(ctx context.Context, str string, perm ...Permissions) error

func (File) WriteJSON

func (file File) WriteJSON(ctx context.Context, input any, indent ...string) (err error)

WriteJSON mashalles input to JSON and writes it as the file. Any indent arguments will be concanated and used as JSON line indentation.

func (File) WriteTo

func (file File) WriteTo(writer io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface

func (File) WriteXML

func (file File) WriteXML(ctx context.Context, input any, indent ...string) (err error)

WriteXML mashalles input to XML and writes it as the file. Any indent arguments will be concanated and used as XML line indentation.

type FileInfo

type FileInfo struct {
	File        File
	Name        string
	Exists      bool
	IsDir       bool
	IsRegular   bool
	IsHidden    bool
	Size        int64
	Modified    time.Time
	Permissions Permissions
}

FileInfo is a snapshot of a file's stat information. In comparison to io/fs.FileInfo it's not an interface but a struct with public fields.

func NewFileInfo

func NewFileInfo(file File, info iofs.FileInfo, hidden bool) *FileInfo

NewFileInfo returns a FileInfo using the data from an io/fs.FileInfo as snapshot of an existing file. Use NewNonExistingFileInfo to get a FileInfo for non existing file.

func NewNonExistingFileInfo

func NewNonExistingFileInfo(file File) *FileInfo

NewNonExistingFileInfo returns a FileInfo for a potentially non existing file. FileInfo.Exists will be false, but the file may exist at any point of time. IsHidden will be true if the name starts with a dot.

func (*FileInfo) StdFileInfo

func (i *FileInfo) StdFileInfo() iofs.FileInfo

StdFileInfo returns an io/fs.FileInfo wrapper for the data stored in the FileInfo struct.

func (*FileInfo) Validate

func (i *FileInfo) Validate() error

Validate returns an error if the FileInfo is invalid.

type FileInfoCache

type FileInfoCache struct {
	// contains filtered or unexported fields
}

FileInfoCache is a cache with timeout for FileInfo data.

func NewFileInfoCache

func NewFileInfoCache(timeout time.Duration) *FileInfoCache

NewFileInfoCache returns a new FileInfoCache with timeout, or nil if timeout is zero. It is valid to call the methods of FileInfoCache for a nil pointer.

func (*FileInfoCache) Delete

func (cache *FileInfoCache) Delete(path string)

Delete deletes the FileInfo with path if was cached.

func (*FileInfoCache) Get

func (cache *FileInfoCache) Get(path string) (info *FileInfo, ok bool)

Get returns the FileInfo for a path or nil and false if there is no FileInfo for the path or the FileInfo has timed out.

func (*FileInfoCache) Put

func (cache *FileInfoCache) Put(path string, info *FileInfo)

Put puts or updates a FileInfo for a path.

type FileReader

type FileReader interface {
	// String returns the name and meta information for the FileReader.
	String() string

	// Name returns the name of the file
	Name() string

	// Ext returns the extension of file name including the point, or an empty string.
	Ext() string

	// LocalPath returns the cleaned local file-system path of the file backing the FileReader,
	// or an empty string if the FileReader is not backed by a local file.
	LocalPath() string

	// Size returns the size of the file
	Size() int64

	// Exists returns if file or data for the implementation exists
	Exists() bool

	// CheckExists return an ErrDoesNotExist error
	// if the file does not exist.
	CheckExists() error

	// ContentHash returns the DefaultContentHash for the file.
	ContentHash() (string, error)

	// ContentHashContext returns the DefaultContentHash for the file.
	ContentHashContext(ctx context.Context) (string, error)

	// ReadAll reads and returns all bytes of the file
	ReadAll() (data []byte, err error)

	// ReadAllContext reads and returns all bytes of the file
	ReadAllContext(context.Context) (data []byte, err error)

	// ReadAllContentHash reads and returns all bytes of the file
	// together with the DefaultContentHash.
	ReadAllContentHash(context.Context) (data []byte, hash string, err error)

	// ReadAllString reads the complete file and returns the content as string.
	ReadAllString() (string, error)

	// ReadAllStringContext reads the complete file and returns the content as string.
	ReadAllStringContext(context.Context) (string, error)

	// WriteTo implements the io.WriterTo interface
	WriteTo(writer io.Writer) (n int64, err error)

	// OpenReader opens the file and returns a ReadCloser that has to be closed after reading
	OpenReader() (ReadCloser, error)

	// OpenReadSeeker opens the file and returns a ReadSeekCloser.
	// Use OpenReader if seeking is not necessary because implementations
	// may need additional buffering to support seeking or not support it at all.
	OpenReadSeeker() (ReadSeekCloser, error)

	// ReadJSON reads and unmarshalles the JSON content of the file to output.
	ReadJSON(ctx context.Context, output any) error

	// ReadXML reads and unmarshalles the XML content of the file to output.
	ReadXML(ctx context.Context, output any) error

	// GobEncode reads and gob encodes the file name and content,
	// implementing encoding/gob.GobEncoder.
	GobEncode() ([]byte, error)
}

func FileReaderWithName

func FileReaderWithName(fileReader FileReader, name string) FileReader

FileReaderWithName returns a new FileReader that wraps the passed fileReader, but the Name() method returns the passed name instead of name of the wrapped fileReader.

func FilesToFileReaders

func FilesToFileReaders(files []File) []FileReader

FilesToFileReaders converts a slice of File to a slice of FileReader

func MemFilesAsFileReaders

func MemFilesAsFileReaders(memFiles []MemFile) []FileReader

MemFilesAsFileReaders converts []MemFile to []FileReader

func StringsToFileReaders

func StringsToFileReaders(fileURIs []string) []FileReader

StringsToFileReaders returns FileReaders for the given fileURIs.

type FileSystem

type FileSystem interface {
	ReadableWritable() (readable, writable bool)

	// RootDir returns the file system root directory
	RootDir() File

	// ID returns a unique identifyer for the FileSystem
	ID() (string, error)

	Prefix() string

	// Name returns the name of the FileSystem implementation
	Name() string

	// String returns a descriptive string for the FileSystem implementation
	String() string

	// URL returns a full URL wich is Prefix() + cleanPath.
	// Note that the passed cleanPath will not be cleaned
	// by the FileSystem implementation.
	URL(cleanPath string) string

	// CleanPathFromURI returns the clean path part of an URI
	// specific to the implementation of the FileSystem.
	// It's the inverse of the URL method.
	CleanPathFromURI(uri string) string

	// JoinCleanFile joins the file system prefix with uriParts
	// into a File with clean path and prefix
	JoinCleanFile(uriParts ...string) File

	// JoinCleanPath joins the uriParts into a cleaned path
	// of the file system style without the file system prefix
	JoinCleanPath(uriParts ...string) string

	// SplitPath returns all Separator() delimited components of filePath
	// without the file system prefix.
	SplitPath(filePath string) []string

	// Separator for paths of the file system
	Separator() string

	// IsAbsPath indicates if the passed filePath is absolute
	IsAbsPath(filePath string) bool

	// AbsPath returns the passe filePath in absolute form
	AbsPath(filePath string) string

	// MatchAnyPattern returns true if name matches any of patterns,
	// or if len(patterns) == 0.
	// The match per pattern works like path.Match or filepath.Match
	MatchAnyPattern(name string, patterns []string) (bool, error)

	// SplitDirAndName returns the parent directory of filePath and the name with that directory of the last filePath element.
	// If filePath is the root of the file systeme, then an empty string will be returned for name.
	SplitDirAndName(filePath string) (dir, name string)

	Stat(filePath string) (iofs.FileInfo, error)

	// IsHidden returns if a file is hidden depending
	// on the definition of hidden files of the file system,
	// but it will always return true if the name of the file starts with a dot.
	IsHidden(filePath string) bool // TODO

	// IsSymbolicLink returns if a file is a symbolic link
	IsSymbolicLink(filePath string) bool // TODO

	// ListDirInfo calls the passed callback function for every file and directory in dirPath.
	// If any patterns are passed, then only files or directores with a name that matches
	// at least one of the patterns are returned.
	ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error

	MakeDir(dirPath string, perm []Permissions) error

	OpenReader(filePath string) (ReadCloser, error)
	OpenWriter(filePath string, perm []Permissions) (WriteCloser, error)
	OpenReadWriter(filePath string, perm []Permissions) (ReadWriteSeekCloser, error)

	// Remove deletes the file.
	Remove(filePath string) error

	// Close the file system or do nothing if it is not closable
	Close() error
}

FileSystem is an interface that has to be implemented for a file system to be accessable via this package.

func GetFileSystem

func GetFileSystem(uriParts ...string) FileSystem

GetFileSystem returns a FileSystem for the passed URI. Returns the local file system if no other file system could be identified. The URI can be passed as parts that will be joined according to the file system.

func GetFileSystemByPrefixOrNil

func GetFileSystemByPrefixOrNil(prefix string) FileSystem

GetFileSystemByPrefixOrNil returns the file system registered with the passed prefix, or nil if it can't be found.

func ParseRawURI

func ParseRawURI(uri string) (fs FileSystem, fsPath string)

ParseRawURI returns a FileSystem for the passed URI and the path component within that file system. Returns the local file system if no other file system could be identified.

func RegisteredFileSystems

func RegisteredFileSystems() []FileSystem

RegisteredFileSystems returns the registered file systems sorted by their prefix.

type GroupFileSystem

type GroupFileSystem interface {
	FileSystem

	Group(filePath string) (string, error)
	SetGroup(filePath string, group string) error
}

type InvalidFileSystem

type InvalidFileSystem string

InvalidFileSystem is a file system where all operations are invalid. A File with an empty path defaults to this FS.

The underlying string value is the optional name of the file system and will be added to the URI prefix. It can be used to register different dummy file systems for debugging or testing purposes.

var (
	// Local is the local file system
	Local = &LocalFileSystem{
		DefaultCreatePermissions:    UserAndGroupReadWrite,
		DefaultCreateDirPermissions: UserAndGroupReadWrite,
	}

	Invalid InvalidFileSystem
)

func (InvalidFileSystem) AbsPath

func (fs InvalidFileSystem) AbsPath(filePath string) string

func (InvalidFileSystem) Append

func (InvalidFileSystem) Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error

func (InvalidFileSystem) CleanPathFromURI

func (fs InvalidFileSystem) CleanPathFromURI(uri string) string

func (InvalidFileSystem) Close

func (InvalidFileSystem) Close() error

func (InvalidFileSystem) CopyFile

func (InvalidFileSystem) CopyFile(ctx context.Context, srcFile string, destFile string, buf *[]byte) error

func (InvalidFileSystem) Exists

func (InvalidFileSystem) Exists(filePath string) bool

func (InvalidFileSystem) Group

func (InvalidFileSystem) Group(filePath string) (string, error)

func (InvalidFileSystem) ID

func (fs InvalidFileSystem) ID() (string, error)

func (InvalidFileSystem) IsAbsPath

func (InvalidFileSystem) IsAbsPath(filePath string) bool

func (InvalidFileSystem) IsHidden

func (InvalidFileSystem) IsHidden(filePath string) bool
func (InvalidFileSystem) IsSymbolicLink(filePath string) bool

func (InvalidFileSystem) JoinCleanFile

func (fs InvalidFileSystem) JoinCleanFile(uri ...string) File

func (InvalidFileSystem) JoinCleanPath

func (fs InvalidFileSystem) JoinCleanPath(uriParts ...string) string

func (InvalidFileSystem) ListDirInfo

func (InvalidFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error

func (InvalidFileSystem) ListDirInfoRecursive

func (InvalidFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error

func (InvalidFileSystem) ListDirMax

func (InvalidFileSystem) ListDirMax(ctx context.Context, dirPath string, n int, patterns []string) (files []File, err error)

func (InvalidFileSystem) MakeAllDirs

func (InvalidFileSystem) MakeAllDirs(dirPath string, perm []Permissions) error

func (InvalidFileSystem) MakeDir

func (InvalidFileSystem) MakeDir(dirPath string, perm []Permissions) error

func (InvalidFileSystem) MatchAnyPattern

func (InvalidFileSystem) MatchAnyPattern(name string, patterns []string) (bool, error)

func (InvalidFileSystem) Move

func (InvalidFileSystem) Move(filePath string, destPath string) error

func (InvalidFileSystem) Name

func (fs InvalidFileSystem) Name() string

func (InvalidFileSystem) OpenAppendWriter

func (InvalidFileSystem) OpenAppendWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (InvalidFileSystem) OpenReadWriter

func (InvalidFileSystem) OpenReadWriter(filePath string, perm []Permissions) (ReadWriteSeekCloser, error)

func (InvalidFileSystem) OpenReader

func (InvalidFileSystem) OpenReader(filePath string) (ReadCloser, error)

func (InvalidFileSystem) OpenWriter

func (InvalidFileSystem) OpenWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (InvalidFileSystem) Prefix

func (fs InvalidFileSystem) Prefix() string

func (InvalidFileSystem) ReadAll

func (InvalidFileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error)

func (InvalidFileSystem) ReadableWritable

func (InvalidFileSystem) ReadableWritable() (readable, writable bool)

func (InvalidFileSystem) Remove

func (InvalidFileSystem) Remove(filePath string) error

func (InvalidFileSystem) Rename

func (InvalidFileSystem) Rename(filePath string, newName string) (string, error)

func (InvalidFileSystem) RootDir

func (InvalidFileSystem) RootDir() File

func (InvalidFileSystem) Separator

func (InvalidFileSystem) Separator() string

func (InvalidFileSystem) SetGroup

func (InvalidFileSystem) SetGroup(filePath string, group string) error

func (InvalidFileSystem) SetPermissions

func (InvalidFileSystem) SetPermissions(filePath string, perm Permissions) error

func (InvalidFileSystem) SetUser

func (InvalidFileSystem) SetUser(filePath string, user string) error

func (InvalidFileSystem) SplitDirAndName

func (fs InvalidFileSystem) SplitDirAndName(filePath string) (dir, name string)

func (InvalidFileSystem) SplitPath

func (fs InvalidFileSystem) SplitPath(filePath string) []string

func (InvalidFileSystem) Stat

func (InvalidFileSystem) Stat(filePath string) (iofs.FileInfo, error)

func (InvalidFileSystem) String

func (fs InvalidFileSystem) String() string

func (InvalidFileSystem) Touch

func (InvalidFileSystem) Touch(filePath string, perm []Permissions) error

func (InvalidFileSystem) Truncate

func (InvalidFileSystem) Truncate(filePath string, size int64) error

func (InvalidFileSystem) URL

func (fs InvalidFileSystem) URL(cleanPath string) string

func (InvalidFileSystem) User

func (InvalidFileSystem) User(filePath string) (string, error)

func (InvalidFileSystem) VolumeName

func (InvalidFileSystem) VolumeName(filePath string) string

func (InvalidFileSystem) Watch

func (InvalidFileSystem) Watch(filePath string, onEvent func(File, Event)) (cancel func() error, err error)

func (InvalidFileSystem) WriteAll

func (InvalidFileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error

type ListDirMaxFileSystem

type ListDirMaxFileSystem interface {
	// ListDirMax returns at most max files and directories in dirPath.
	// A max value of -1 returns all files.
	// If any patterns are passed, then only files or directories with a name that matches
	// at least one of the patterns are returned.
	ListDirMax(ctx context.Context, dirPath string, max int, patterns []string) ([]File, error)
}

type ListDirRecursiveFileSystem

type ListDirRecursiveFileSystem interface {
	// ListDirInfoRecursive calls the passed callback function for every file (not directory) in dirPath
	// recursing into all sub-directories.
	// If any patterns are passed, then only files (not directories) with a name that matches
	// at least one of the patterns are returned.
	ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error
}

type LocalFileSystem

type LocalFileSystem struct {
	// DefaultCreatePermissions are the default file permissions used for creating new files
	DefaultCreatePermissions Permissions
	// DefaultCreateDirPermissions are the default file permissions used for creating new directories
	DefaultCreateDirPermissions Permissions

	WatchEventLogger Logger
	WatchErrorLogger Logger
	// contains filtered or unexported fields
}

LocalFileSystem implements FileSystem for the local file system.

func (*LocalFileSystem) AbsPath

func (local *LocalFileSystem) AbsPath(filePath string) string

func (*LocalFileSystem) Append

func (local *LocalFileSystem) Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error

func (*LocalFileSystem) CleanPathFromURI

func (local *LocalFileSystem) CleanPathFromURI(uri string) string

func (*LocalFileSystem) Close

func (*LocalFileSystem) Close() error

func (*LocalFileSystem) CopyFile

func (local *LocalFileSystem) CopyFile(ctx context.Context, srcFilePath string, destFilePath string, buf *[]byte) error
func (local *LocalFileSystem) CreateSymbolicLink(oldFile, newFile File) error

func (*LocalFileSystem) Group

func (local *LocalFileSystem) Group(filePath string) (string, error)

func (*LocalFileSystem) ID

func (local *LocalFileSystem) ID() (string, error)

func (*LocalFileSystem) IsAbsPath

func (local *LocalFileSystem) IsAbsPath(filePath string) bool

func (*LocalFileSystem) IsHidden

func (local *LocalFileSystem) IsHidden(filePath string) bool
func (local *LocalFileSystem) IsSymbolicLink(filePath string) bool

func (*LocalFileSystem) JoinCleanFile

func (local *LocalFileSystem) JoinCleanFile(uri ...string) File

func (*LocalFileSystem) JoinCleanPath

func (local *LocalFileSystem) JoinCleanPath(uriParts ...string) string

func (*LocalFileSystem) ListDirInfo

func (local *LocalFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) (err error)

func (*LocalFileSystem) ListDirMax

func (local *LocalFileSystem) ListDirMax(ctx context.Context, dirPath string, max int, patterns []string) (files []File, err error)

func (*LocalFileSystem) MakeAllDirs

func (local *LocalFileSystem) MakeAllDirs(dirPath string, perm []Permissions) error

func (*LocalFileSystem) MakeDir

func (local *LocalFileSystem) MakeDir(dirPath string, perm []Permissions) error

func (*LocalFileSystem) MatchAnyPattern

func (local *LocalFileSystem) MatchAnyPattern(name string, patterns []string) (bool, error)

MatchAnyPattern returns true if name matches any of patterns, or if len(patterns) == 0. The match per pattern works like path.Match or filepath.Match

func (*LocalFileSystem) Move

func (local *LocalFileSystem) Move(filePath string, destPath string) error

func (*LocalFileSystem) Name

func (local *LocalFileSystem) Name() string

func (*LocalFileSystem) OpenAppendWriter

func (local *LocalFileSystem) OpenAppendWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (*LocalFileSystem) OpenReadWriter

func (local *LocalFileSystem) OpenReadWriter(filePath string, perm []Permissions) (ReadWriteSeekCloser, error)

func (*LocalFileSystem) OpenReader

func (local *LocalFileSystem) OpenReader(filePath string) (ReadCloser, error)

func (*LocalFileSystem) OpenWriter

func (local *LocalFileSystem) OpenWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (*LocalFileSystem) Prefix

func (local *LocalFileSystem) Prefix() string

func (*LocalFileSystem) ReadAll

func (local *LocalFileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error)
func (local *LocalFileSystem) ReadSymbolicLink(file File) (linked File, err error)

func (*LocalFileSystem) ReadableWritable

func (local *LocalFileSystem) ReadableWritable() (readable, writable bool)

func (*LocalFileSystem) Remove

func (local *LocalFileSystem) Remove(filePath string) error

func (*LocalFileSystem) Rename

func (local *LocalFileSystem) Rename(filePath string, newName string) (newPath string, err error)

func (*LocalFileSystem) RootDir

func (local *LocalFileSystem) RootDir() File

func (*LocalFileSystem) Separator

func (local *LocalFileSystem) Separator() string

func (*LocalFileSystem) SetGroup

func (local *LocalFileSystem) SetGroup(filePath string, group string) error

func (*LocalFileSystem) SetPermissions

func (local *LocalFileSystem) SetPermissions(filePath string, perm Permissions) error

func (*LocalFileSystem) SetUser

func (local *LocalFileSystem) SetUser(filePath string, username string) error

func (*LocalFileSystem) SplitDirAndName

func (*LocalFileSystem) SplitDirAndName(filePath string) (dir, name string)

func (*LocalFileSystem) SplitPath

func (local *LocalFileSystem) SplitPath(filePath string) []string

func (*LocalFileSystem) Stat

func (local *LocalFileSystem) Stat(filePath string) (iofs.FileInfo, error)

func (*LocalFileSystem) String

func (local *LocalFileSystem) String() string

String implements the fmt.Stringer interface.

func (*LocalFileSystem) Touch

func (local *LocalFileSystem) Touch(filePath string, perm []Permissions) error

func (*LocalFileSystem) Truncate

func (local *LocalFileSystem) Truncate(filePath string, newSize int64) error

func (*LocalFileSystem) URL

func (local *LocalFileSystem) URL(cleanPath string) string

func (*LocalFileSystem) User

func (local *LocalFileSystem) User(filePath string) (string, error)

func (*LocalFileSystem) VolumeName

func (local *LocalFileSystem) VolumeName(filePath string) string

func (*LocalFileSystem) Watch

func (local *LocalFileSystem) Watch(filePath string, onEvent func(File, Event)) (cancel func() error, err error)

func (*LocalFileSystem) WriteAll

func (local *LocalFileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error

type Logger

type Logger interface {
	Printf(format string, args ...any)
}

Logger is an interface that can be implemented to log errors

type LoggerFunc

type LoggerFunc func(format string, args ...any)

LoggerFunc implements Logger as higher order function

func (LoggerFunc) Printf

func (f LoggerFunc) Printf(format string, args ...any)

type MakeAllDirsFileSystem

type MakeAllDirsFileSystem interface {
	FileSystem

	MakeAllDirs(dirPath string, perm []Permissions) error
}

type MemFile

type MemFile struct {
	FileName string `json:"filename"`
	FileData []byte `json:"data,omitempty"`
}

MemFile implements FileReader with a filename and an in memory byte slice. It exposes FileName and FileData as exported struct fields to emphasize its simple nature as just an wrapper around a name and some bytes.

As a small an simple struct MemFile is usually passed by value. This is why NewMemFile does not return a pointer.

Note that the ReadAll and ReadAllContext methods return FileData directly without copying it to optimized performance. So be careful when modifying the FileData bytes of a MemFile.

MemFile implements the following interfaces:

  • FileReader
  • io.Writer
  • io.WriterTo
  • io.ReaderAt
  • json.Marshaler
  • json.Unmarshaler
  • gob.GobEncoder
  • gob.GobDecoder
  • fmt.Stringer

func NewMemFile

func NewMemFile(name string, data []byte) MemFile

NewMemFile returns a new MemFile

func ReadAllMemFile

func ReadAllMemFile(ctx context.Context, r io.Reader, name string) (MemFile, error)

ReadAllMemFile returns a new MemFile with the data from ReadAllContext(r) and the passed name. It reads all data from r until EOF is reached, another error is returned, or the context got canceled.

func ReadMemFile

func ReadMemFile(ctx context.Context, fileReader FileReader) (MemFile, error)

ReadMemFile returns a new MemFile with name and data from fileReader. If the passed fileReader is a MemFile then its FileData is used directly without copying it.

func ReadMemFileRename

func ReadMemFileRename(ctx context.Context, fileReader FileReader, name string) (MemFile, error)

ReadMemFileRename returns a new MemFile with the data from fileReader and the passed name. If the passed fileReader is a MemFile then its FileData is used directly without copying it.

func UnzipToMemFiles

func UnzipToMemFiles(ctx context.Context, zipFile FileReader) ([]MemFile, error)

UnzipToMemFiles unzips the passed zipFile as MemFiles.

func (MemFile) CheckExists

func (f MemFile) CheckExists() error

CheckExists return an ErrDoesNotExist error if the file does not exist.

func (MemFile) ContentHash

func (f MemFile) ContentHash() (string, error)

ContentHash returns the DefaultContentHash for the file.

func (MemFile) ContentHashContext

func (f MemFile) ContentHashContext(ctx context.Context) (string, error)

ContentHashContext returns the DefaultContentHash for the file.

func (MemFile) Exists

func (f MemFile) Exists() bool

Exists returns true if the MemFile has non empty FileName. It's valid to call this method on a nil pointer, will return false in this case.

func (MemFile) Ext

func (f MemFile) Ext() string

Ext returns the extension of file name including the point, or an empty string.

func (*MemFile) GobDecode

func (f *MemFile) GobDecode(gobBytes []byte) error

GobDecode decodes gobBytes file name and content, implementing encoding/gob.GobDecoder.

func (MemFile) GobEncode

func (f MemFile) GobEncode() ([]byte, error)

GobEncode gob encodes the file name and content, implementing encoding/gob.GobEncoder.

func (MemFile) LocalPath

func (MemFile) LocalPath() string

LocalPath always returns an empty string for a MemFile.

func (MemFile) Name

func (f MemFile) Name() string

Name returns the name of the file. If FileName contains a slash or backslash then only the part after it will be returned.

func (MemFile) OpenReadSeeker

func (f MemFile) OpenReadSeeker() (ReadSeekCloser, error)

OpenReadSeeker opens the file and returns a ReadSeekCloser. Use OpenReader if seeking is not necessary because implementations may need additional buffering to support seeking or not support it at all.

func (MemFile) OpenReader

func (f MemFile) OpenReader() (ReadCloser, error)

OpenReader opens the file and returns a io/fs.File that has to be closed after reading

func (MemFile) ReadAll

func (f MemFile) ReadAll() (data []byte, err error)

ReadAll returns the FileData without copying it.

func (MemFile) ReadAllContentHash

func (f MemFile) ReadAllContentHash(ctx context.Context) (data []byte, hash string, err error)

ReadAllContentHash returns the FileData without copying it together with the DefaultContentHash.

func (MemFile) ReadAllContext

func (f MemFile) ReadAllContext(ctx context.Context) (data []byte, err error)

ReadAllContext returns the FileData without copying it.

func (MemFile) ReadAllString

func (f MemFile) ReadAllString() (string, error)

ReadAllString returns the FileData as string.

func (MemFile) ReadAllStringContext

func (f MemFile) ReadAllStringContext(ctx context.Context) (string, error)

ReadAllStringContext returns the FileData as string.

func (MemFile) ReadAt

func (f MemFile) ReadAt(p []byte, off int64) (n int, err error)

ReadAt reads len(p) bytes into p starting at offset off in the underlying input source. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered.

When ReadAt returns n < len(p), it returns a non-nil error explaining why more bytes were not returned. In this respect, ReadAt is stricter than Read.

If the n = len(p) bytes returned by ReadAt are at the end of the input source, ReadAt returns err == nil.

Clients of ReadAt can execute parallel ReadAt calls on the same input source.

ReadAt implements the interface io.ReaderAt.

func (MemFile) ReadJSON

func (f MemFile) ReadJSON(ctx context.Context, output any) error

ReadJSON reads and unmarshalles the JSON content of the file to output.

func (MemFile) ReadXML

func (f MemFile) ReadXML(ctx context.Context, output any) error

ReadXML reads and unmarshalles the XML content of the file to output.

func (MemFile) Size

func (f MemFile) Size() int64

Size returns the size of the file

func (MemFile) Stat

func (f MemFile) Stat() (iofs.FileInfo, error)

Stat returns a io/fs.FileInfo describing the MemFile.

func (MemFile) String

func (f MemFile) String() string

String returns the name and meta information for the FileReader. String implements the fmt.Stringer interface.

func (*MemFile) Write

func (f *MemFile) Write(b []byte) (int, error)

Write appends the passed bytes to the FileData, implementing the io.Writer interface.

func (MemFile) WriteTo

func (f MemFile) WriteTo(writer io.Writer) (n int64, err error)

WriteTo implements the io.WriterTo interface

type MemFileSystem

type MemFileSystem struct {
	// contains filtered or unexported fields
}

MemFileSystem is a fully featured thread-safe file system living in random access memory.

Usefull as mock file system for tests or caching of slow file systems.

func NewMemFileSystem

func NewMemFileSystem(separator string, initialFiles ...MemFile) (*MemFileSystem, error)

func (*MemFileSystem) AbsPath

func (fs *MemFileSystem) AbsPath(filePath string) string

func (*MemFileSystem) AddMemFile

func (fs *MemFileSystem) AddMemFile(f MemFile, modified time.Time) (File, error)

AddMemFile adds a MemFile to the file system with the given modified time. The MemFile.FileName can be a path with the path separator of the MemFileSystem, in which case all directories of the path are created.

func (*MemFileSystem) Append

func (fs *MemFileSystem) Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error

func (*MemFileSystem) CleanPathFromURI

func (fs *MemFileSystem) CleanPathFromURI(uri string) string

func (*MemFileSystem) Clear

func (fs *MemFileSystem) Clear()

func (*MemFileSystem) Close

func (fs *MemFileSystem) Close() error

func (*MemFileSystem) CopyFile

func (fs *MemFileSystem) CopyFile(ctx context.Context, srcFile string, destFile string, buf *[]byte) error

func (*MemFileSystem) Exists

func (fs *MemFileSystem) Exists(filePath string) bool

func (*MemFileSystem) ID

func (fs *MemFileSystem) ID() (string, error)

func (*MemFileSystem) IsAbsPath

func (fs *MemFileSystem) IsAbsPath(filePath string) bool

func (*MemFileSystem) IsHidden

func (*MemFileSystem) IsHidden(filePath string) bool
func (*MemFileSystem) IsSymbolicLink(filePath string) bool

func (*MemFileSystem) JoinCleanFile

func (fs *MemFileSystem) JoinCleanFile(uri ...string) File

func (*MemFileSystem) JoinCleanPath

func (fs *MemFileSystem) JoinCleanPath(uriParts ...string) string

func (*MemFileSystem) ListDirInfo

func (*MemFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(*FileInfo) error, patterns []string) error

func (*MemFileSystem) MakeAllDirs

func (fs *MemFileSystem) MakeAllDirs(dirPath string, perm []Permissions) error

func (*MemFileSystem) MakeDir

func (fs *MemFileSystem) MakeDir(dirPath string, perm []Permissions) error

func (*MemFileSystem) MatchAnyPattern

func (*MemFileSystem) MatchAnyPattern(name string, patterns []string) (bool, error)

func (*MemFileSystem) Move

func (fs *MemFileSystem) Move(filePath string, destPath string) error

func (*MemFileSystem) Name

func (*MemFileSystem) Name() string

func (*MemFileSystem) OpenAppendWriter

func (fs *MemFileSystem) OpenAppendWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (*MemFileSystem) OpenReadWriter

func (fs *MemFileSystem) OpenReadWriter(filePath string, perm []Permissions) (ReadWriteSeekCloser, error)

func (*MemFileSystem) OpenReader

func (fs *MemFileSystem) OpenReader(filePath string) (iofs.File, error)

func (*MemFileSystem) OpenWriter

func (fs *MemFileSystem) OpenWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (*MemFileSystem) Prefix

func (fs *MemFileSystem) Prefix() string

func (*MemFileSystem) ReadAll

func (fs *MemFileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error)

func (*MemFileSystem) ReadableWritable

func (fs *MemFileSystem) ReadableWritable() (readable, writable bool)

func (*MemFileSystem) Remove

func (fs *MemFileSystem) Remove(filePath string) error

func (*MemFileSystem) Rename

func (fs *MemFileSystem) Rename(filePath string, newName string) (string, error)

func (*MemFileSystem) RootDir

func (fs *MemFileSystem) RootDir() File

func (*MemFileSystem) Separator

func (fs *MemFileSystem) Separator() string

func (*MemFileSystem) SetReadOnly

func (fs *MemFileSystem) SetReadOnly(readOnly bool)

func (*MemFileSystem) SplitDirAndName

func (fs *MemFileSystem) SplitDirAndName(filePath string) (dir, name string)

func (*MemFileSystem) SplitPath

func (fs *MemFileSystem) SplitPath(filePath string) []string

func (*MemFileSystem) Stat

func (fs *MemFileSystem) Stat(filePath string) (iofs.FileInfo, error)

func (*MemFileSystem) String

func (fs *MemFileSystem) String() string

func (*MemFileSystem) Touch

func (fs *MemFileSystem) Touch(filePath string, perm []Permissions) error

func (*MemFileSystem) Truncate

func (fs *MemFileSystem) Truncate(filePath string, newSize int64) error

func (*MemFileSystem) URL

func (fs *MemFileSystem) URL(cleanPath string) string

func (*MemFileSystem) Volume

func (fs *MemFileSystem) Volume() string

func (*MemFileSystem) VolumeName

func (fs *MemFileSystem) VolumeName(filePath string) string

func (*MemFileSystem) Watch

func (fs *MemFileSystem) Watch(filePath string, onEvent func(File, Event)) (cancel func() error, err error)

func (*MemFileSystem) WithID

func (fs *MemFileSystem) WithID(id string) *MemFileSystem

func (*MemFileSystem) WithVolume

func (fs *MemFileSystem) WithVolume(volume string) *MemFileSystem

func (*MemFileSystem) WriteAll

func (fs *MemFileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error

type MoveFileSystem

type MoveFileSystem interface {
	FileSystem

	// Move moves and/or renames the file to destination.
	// destination can be a directory or file-path.
	// If successful, this also changes the path of this File's implementation.
	Move(filePath string, destinationPath string) error
}

MoveFileSystem can be implemented by file systems that have native file moving functionality.

If a file system does not implement this interface then it's functionality will be emulated with other methods.

type Permissions

type Permissions int

Permissions for a file, follows the Unix/os.FileMode bit schema.

var (
	NoPermissions Permissions = 0

	UserExecute          Permissions = 0100
	UserWrite            Permissions = 0200
	UserRead             Permissions = 0400
	UserReadWrite        Permissions = UserRead | UserWrite
	UserReadWriteExecute Permissions = UserRead | UserWrite | UserExecute

	GroupExecute          Permissions = 0010
	GroupWrite            Permissions = 0020
	GroupRead             Permissions = 0040
	GroupReadWrite        Permissions = GroupRead | GroupWrite
	GroupReadWriteExecute Permissions = GroupRead | GroupWrite | GroupExecute

	UserAndGroupRead             Permissions = UserRead | GroupRead
	UserAndGroupReadWrite        Permissions = UserReadWrite | GroupReadWrite
	UserAndGroupReadWriteExecute Permissions = UserReadWriteExecute | GroupReadWriteExecute

	OthersExecute          Permissions = 0001
	OthersWrite            Permissions = 0002
	OthersRead             Permissions = 0004
	OthersReadWrite        Permissions = OthersRead | OthersWrite
	OthersReadWriteExecute Permissions = OthersRead | OthersWrite | OthersExecute

	AllRead      = UserRead | GroupRead | OthersRead
	AllWrite     = UserWrite | GroupWrite | OthersWrite
	AllExecute   = UserExecute | GroupExecute | OthersExecute
	AllReadWrite = UserReadWrite | GroupReadWrite | OthersReadWrite
)

func JoinPermissions

func JoinPermissions(perms []Permissions, defaultPerm Permissions) (result Permissions)

func PermissionsFromStdFileInfo

func PermissionsFromStdFileInfo(info iofs.FileInfo) Permissions

func (Permissions) Can

func (perm Permissions) Can(p Permissions) bool

func (Permissions) CanAllExecute

func (perm Permissions) CanAllExecute() bool

func (Permissions) CanAllRead

func (perm Permissions) CanAllRead() bool

func (Permissions) CanAllReadWrite

func (perm Permissions) CanAllReadWrite() bool

func (Permissions) CanAllWrite

func (perm Permissions) CanAllWrite() bool

func (Permissions) CanGroupExecute

func (perm Permissions) CanGroupExecute() bool

func (Permissions) CanGroupRead

func (perm Permissions) CanGroupRead() bool

func (Permissions) CanGroupReadWrite

func (perm Permissions) CanGroupReadWrite() bool

func (Permissions) CanGroupWrite

func (perm Permissions) CanGroupWrite() bool

func (Permissions) CanOthersExecute

func (perm Permissions) CanOthersExecute() bool

func (Permissions) CanOthersRead

func (perm Permissions) CanOthersRead() bool

func (Permissions) CanOthersReadWrite

func (perm Permissions) CanOthersReadWrite() bool

func (Permissions) CanOthersWrite

func (perm Permissions) CanOthersWrite() bool

func (Permissions) CanUserAndGroupRead

func (perm Permissions) CanUserAndGroupRead() bool

func (Permissions) CanUserAndGroupReadWrite

func (perm Permissions) CanUserAndGroupReadWrite() bool

func (Permissions) CanUserExecute

func (perm Permissions) CanUserExecute() bool

func (Permissions) CanUserRead

func (perm Permissions) CanUserRead() bool

func (Permissions) CanUserReadWrite

func (perm Permissions) CanUserReadWrite() bool

func (Permissions) CanUserWrite

func (perm Permissions) CanUserWrite() bool

func (Permissions) Executable

func (perm Permissions) Executable() (user, group, others bool)

func (Permissions) FileMode

func (perm Permissions) FileMode(isDir bool) os.FileMode

FileMode returns an os.FileMode for the given permissions together with the information if the file is a directory.

func (Permissions) Readable

func (perm Permissions) Readable() (user, group, others bool)

func (Permissions) Writable

func (perm Permissions) Writable() (user, group, others bool)

type PermissionsFileSystem

type PermissionsFileSystem interface {
	FileSystem

	SetPermissions(filePath string, perm Permissions) error
}

type ReadAllFileSystem

type ReadAllFileSystem interface {
	FileSystem

	ReadAll(ctx context.Context, filePath string) ([]byte, error)
}

type ReadCloser

type ReadCloser = iofs.File

type ReadOnlyBase

type ReadOnlyBase struct{}

ReadOnlyBase implements the writing methods of the FileSystem interface to do nothing and return ErrReadOnlyFileSystem. Intended to be used as base for read only file systems, so that only the read methods have to be implemented.

func (*ReadOnlyBase) MakeDir

func (*ReadOnlyBase) MakeDir(dirPath string, perm []Permissions) error

func (*ReadOnlyBase) MatchAnyPattern

func (*ReadOnlyBase) MatchAnyPattern(name string, patterns []string) (bool, error)

MatchAnyPattern returns true if name matches any of patterns, or if len(patterns) == 0. The match per pattern works like path.Match or filepath.Match

func (*ReadOnlyBase) OpenReadWriter

func (*ReadOnlyBase) OpenReadWriter(filePath string, perm []Permissions) (ReadWriteSeekCloser, error)

func (*ReadOnlyBase) OpenWriter

func (*ReadOnlyBase) OpenWriter(filePath string, perm []Permissions) (WriteCloser, error)

func (*ReadOnlyBase) ReadableWritable

func (*ReadOnlyBase) ReadableWritable() (readable, writable bool)

func (*ReadOnlyBase) Remove

func (*ReadOnlyBase) Remove(filePath string) error

func (*ReadOnlyBase) SetGroup

func (*ReadOnlyBase) SetGroup(filePath string, group string) error

func (*ReadOnlyBase) SetPermissions

func (*ReadOnlyBase) SetPermissions(filePath string, perm Permissions) error

func (*ReadOnlyBase) SetUser

func (*ReadOnlyBase) SetUser(filePath string, user string) error

type ReadSeekCloser

type ReadSeekCloser interface {
	io.Reader
	io.ReaderAt
	io.Seeker
	io.Closer
}

ReadSeekCloser combines the interfaces io.Reader io.ReaderAt io.Seeker io.Closer

type ReadWriteSeekCloser

type ReadWriteSeekCloser interface {
	io.Reader
	io.ReaderAt
	io.Writer
	io.WriterAt
	io.Seeker
	io.Closer
}

ReadWriteSeekCloser combines the interfaces io.Reader io.ReaderAt io.Writer io.WriterAt io.Seeker io.Closer

type RenameFileSystem

type RenameFileSystem interface {
	FileSystem

	// Rename only renames the file in its base directory
	// but does not move it into another directory.
	// If successful, this also changes the path of this File's implementation.
	Rename(filePath string, newName string) (newPath string, err error)
}

RenameFileSystem can be implemented by file systems that have native file renaming functionality.

If a file system does not implement this interface then it's functionality will be emulated with other methods.

type SentinelError

type SentinelError string

SentinelError is used for const sentinel errors

const (
	// ErrReadOnlyFileSystem is returned when a file system doesn't support writes
	ErrReadOnlyFileSystem SentinelError = "file system is read-only"

	// ErrWriteOnlyFileSystem is returned when a file system doesn't support reads
	ErrWriteOnlyFileSystem SentinelError = "file system is write-only"

	// ErrInvalidFileSystem indicates an invalid file system
	ErrInvalidFileSystem SentinelError = "invalid file system"

	// ErrFileSystemClosed is returned after a file system Close method was called
	ErrFileSystemClosed SentinelError = "file system is closed"
)

func (SentinelError) Error

func (e SentinelError) Error() string

type StdDirEntry

type StdDirEntry struct {
	File File
}

StdDirEntry implements the io/fs.DirEntry interface from the standard library for a File.

func (StdDirEntry) Info

func (de StdDirEntry) Info() (iofs.FileInfo, error)

Info returns the FileInfo for the file or subdirectory described by the entry. The returned FileInfo may be from the time of the original directory read or from the time of the call to Info. If the file has been removed or renamed since the directory read, Info may return an error satisfying errors.Is(err, ErrNotExist). If the entry denotes a symbolic link, Info reports the information about the link itself, not the link's target.

func (StdDirEntry) IsDir

func (de StdDirEntry) IsDir() bool

IsDir reports whether the entry describes a directory.

func (StdDirEntry) Name

func (de StdDirEntry) Name() string

Name returns the name of the file (or subdirectory) described by the entry. This name is only the final element of the path (the base name), not the entire path. For example, Name would return "hello.go" not "/home/gopher/hello.go".

func (StdDirEntry) Type

func (de StdDirEntry) Type() iofs.FileMode

Type returns the type bits for the entry. The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method.

type StdFS

type StdFS struct {
	File File
}

StdFS implements the io/fs.FS interface of the standard library for a File.

StdFS implements the following interfaces:

  • io/fs.FS
  • io/fs.SubFS
  • io/fs.StatFS
  • io/fs.GlobFS
  • io/fs.ReadDirFS
  • io/fs.ReadFileFS

func (StdFS) Glob

func (f StdFS) Glob(pattern string) (names []string, err error)

Glob returns the names of all files matching pattern, providing an implementation of the top-level Glob function.

This method implements the io/fs.GlobFS interface.

func (StdFS) Open

func (f StdFS) Open(name string) (iofs.File, error)

Open opens the named file.

This method implements the io/fs.FS interface.

func (StdFS) ReadDir

func (f StdFS) ReadDir(name string) ([]iofs.DirEntry, error)

ReadDir reads the named directory and returns a list of directory entries sorted by filename.

This method implements the io/fs.ReadDirFS interface.

func (StdFS) ReadFile

func (f StdFS) ReadFile(name string) ([]byte, error)

ReadFile reads the named file and returns its contents.

This method implements the io/fs.ReadFileFS interface.

func (StdFS) Stat

func (f StdFS) Stat(name string) (iofs.FileInfo, error)

Stat returns a io/fs.FileInfo describing the file.

This method implements the io/fs.StatFS interface.

func (StdFS) Sub

func (f StdFS) Sub(dir string) (iofs.FS, error)

Sub returns an io/fs.FS corresponding to the subtree rooted at dir.

This method implements the io/fs.SubFS interface.

type TouchFileSystem

type TouchFileSystem interface {
	FileSystem

	Touch(filePath string, perm []Permissions) error
}

type TruncateFileSystem

type TruncateFileSystem interface {
	FileSystem

	// Truncate resizes a file by not only
	// truncating to a smaller size but also
	// appending zeros to a bigger size
	// than the current one.
	Truncate(filePath string, size int64) error
}

type UserFileSystem

type UserFileSystem interface {
	FileSystem

	User(filePath string) (string, error)
	SetUser(filePath string, user string) error
}

type VolumeNameFileSystem

type VolumeNameFileSystem interface {
	FileSystem
	// VolumeName returns the name of the volume at the beginning of the filePath,
	// or an empty string if the filePath has no volume.
	// A volume is for example "C:" on Windows
	VolumeName(filePath string) string
}

VolumeNameFileSystem should be implemented by file systems that have volume names.

type WatchFileSystem

type WatchFileSystem interface {
	FileSystem

	// Watch a file or directory for changes.
	// If filePath describes a directory then
	// changes directly within it will be reported.
	// This does not apply changes in deeper
	// recursive sub-directories.
	//
	// It is valid to watch a file with multiple
	// callbacks, calling the returned cancel function
	// will cancel a particular watch.
	Watch(filePath string, onEvent func(File, Event)) (cancel func() error, err error)
}

WatchFileSystem can be implemented by file systems that have file watching functionality.

type WriteAllFileSystem

type WriteAllFileSystem interface {
	FileSystem

	WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error
}

type WriteCloser

type WriteCloser = io.WriteCloser

type WriteSeekCloser

type WriteSeekCloser interface {
	io.Writer
	io.WriterAt
	io.Seeker
	io.Closer
}

WriteSeekCloser combines the interfaces io.Writer io.WriterAt io.Seeker io.Closer

Directories

Path Synopsis
dropboxfs module
fsimpl contains helper functions for implementing a fs.FileSystem
fsimpl contains helper functions for implementing a fs.FileSystem
ftpfs module
Package httpfs implements a read only file system for HTTP URLs.
Package httpfs implements a read only file system for HTTP URLs.
s3fs module
sftpfs module
Package uuiddirs provides functions to split up a UUID into a series of sub-directories so that an unlimited number of UUIDs can be used as directories.
Package uuiddirs provides functions to split up a UUID into a series of sub-directories so that an unlimited number of UUIDs can be used as directories.

Jump to

Keyboard shortcuts

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