Movatterモバイル変換


[0]ホーム

URL:


afero

packagemodule
v1.15.0Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License:Apache-2.0Imports:24Imported by:8,182

Details

Repository

github.com/spf13/afero

Links

README

afero logo-sm

GitHub Workflow StatusGoDocGo Report CardGo Version

Afero: The Universal Filesystem Abstraction for Go

Afero is a powerful and extensible filesystem abstraction system for Go. It provides a single, unified API for interacting with diverse filesystems—including the local disk, memory, archives, and network storage.

Afero acts as a drop-in replacement for the standardos package, enabling you to write modular code that is agnostic to the underlying storage, dramatically simplifies testing, and allows for sophisticated architectural patterns through filesystem composition.

Why Afero?

Afero elevates filesystem interaction beyond simple file reading and writing, offering solutions for testability, flexibility, and advanced architecture.

🔑Key Features:

  • Universal API: Write your code once. Run it against the local OS, in-memory storage, ZIP/TAR archives, or remote systems (SFTP, GCS).
  • Ultimate Testability: UtilizeMemMapFs, a fully concurrent-safe, read/write in-memory filesystem. Write fast, isolated, and reliable unit tests without touching the physical disk or worrying about cleanup.
  • Powerful Composition: Afero's hidden superpower. Layer filesystems on top of each other to create sophisticated behaviors:
    • Sandboxing: UseCopyOnWriteFs to create temporary scratch spaces that isolate changes from the base filesystem.
    • Caching: UseCacheOnReadFs to automatically layer a fast cache (like memory) over a slow backend (like a network drive).
    • Security Jails: UseBasePathFs to restrict application access to a specific subdirectory (chroot).
  • os Package Compatibility: Afero mirrors the functions in the standardos package, making adoption and refactoring seamless.
  • io/fs Compatibility: Fully compatible with the Go standard library'sio/fs interfaces.

Installation

go get github.com/spf13/afero
import "github.com/spf13/afero"

Quick Start: The Power of Abstraction

The core of Afero is theafero.Fs interface. By designing your functions to accept this interface rather than callingos.* functions directly, your code instantly becomes more flexible and testable.

1. Refactor Your Code

Change functions that rely on theos package to acceptafero.Fs.

// Before: Coupled to the OS and difficult to test// func ProcessConfiguration(path string) error {//     data, err := os.ReadFile(path)//     ...// }import "github.com/spf13/afero"// After: Decoupled, flexible, and testablefunc ProcessConfiguration(fs afero.Fs, path string) error {    // Use Afero utility functions which mirror os/ioutil    data, err := afero.ReadFile(fs, path)    // ... process the data    return err}
2. Usage in Production

In your production environment, inject theOsFs backend, which wraps the standard operating system calls.

func main() {    // Use the real OS filesystem    AppFs := afero.NewOsFs()    ProcessConfiguration(AppFs, "/etc/myapp.conf")}
3. Usage in Testing

In your tests, injectMemMapFs. This provides a blazing-fast, isolated, in-memory filesystem that requires no disk I/O and no cleanup.

func TestProcessConfiguration(t *testing.T) {    // Use the in-memory filesystem    AppFs := afero.NewMemMapFs()        // Pre-populate the memory filesystem for the test    configPath := "/test/config.json"    afero.WriteFile(AppFs, configPath, []byte(`{"feature": true}`), 0644)    // Run the test entirely in memory    err := ProcessConfiguration(AppFs, configPath)    if err != nil {        t.Fatal(err)    }}

Afero's Superpower: Composition

Afero's most unique feature is its ability to combine filesystems. This allows you to build complex behaviors out of simple components, keeping your application logic clean.

Example 1: Sandboxing with Copy-on-Write

Create a temporary environment where an application can "modify" system files without affecting the actual disk.

// 1. The base layer is the real OS, made read-only for safety.baseFs := afero.NewReadOnlyFs(afero.NewOsFs())// 2. The overlay layer is a temporary in-memory filesystem for changes.overlayFs := afero.NewMemMapFs()// 3. Combine them. Reads fall through to the base; writes only hit the overlay.sandboxFs := afero.NewCopyOnWriteFs(baseFs, overlayFs)// The application can now "modify" /etc/hosts, but the changes are isolated in memory.afero.WriteFile(sandboxFs, "/etc/hosts", []byte("127.0.0.1 sandboxed-app"), 0644)// The real /etc/hosts on disk is untouched.
Example 2: Caching a Slow Filesystem

Improve performance by layering a fast cache (like memory) over a slow backend (like a network drive or cloud storage).

import "time"// Assume 'remoteFs' is a slow backend (e.g., SFTP or GCS)var remoteFs afero.Fs // 'cacheFs' is a fast in-memory backendcacheFs := afero.NewMemMapFs()// Create the caching layer. Cache items for 5 minutes upon first read.cachedFs := afero.NewCacheOnReadFs(remoteFs, cacheFs, 5*time.Minute)// The first read is slow (fetches from remote, then caches)data1, _ := afero.ReadFile(cachedFs, "data.json")// The second read is instant (serves from memory cache)data2, _ := afero.ReadFile(cachedFs, "data.json")
Example 3: Security Jails (chroot)

Restrict an application component's access to a specific subdirectory.

osFs := afero.NewOsFs()// Create a filesystem rooted at /home/user/public// The application cannot access anything above this directory.jailedFs := afero.NewBasePathFs(osFs, "/home/user/public")// To the application, this is reading "/"// In reality, it's reading "/home/user/public/"dirInfo, err := afero.ReadDir(jailedFs, "/")// Attempts to access parent directories fail_, err = jailedFs.Open("../secrets.txt") // Returns an error

Real-World Use Cases

Build Cloud-Agnostic Applications

Write applications that seamlessly work with different storage backends:

type DocumentProcessor struct {    fs afero.Fs}func NewDocumentProcessor(fs afero.Fs) *DocumentProcessor {    return &DocumentProcessor{fs: fs}}func (p *DocumentProcessor) Process(inputPath, outputPath string) error {    // This code works whether fs is local disk, cloud storage, or memory    content, err := afero.ReadFile(p.fs, inputPath)    if err != nil {        return err    }        processed := processContent(content)    return afero.WriteFile(p.fs, outputPath, processed, 0644)}// Use with local filesystemprocessor := NewDocumentProcessor(afero.NewOsFs())// Use with Google Cloud Storageprocessor := NewDocumentProcessor(gcsFS)// Use with in-memory filesystem for testingprocessor := NewDocumentProcessor(afero.NewMemMapFs())
Treating Archives as Filesystems

Read files directly from.zip or.tar archives without unpacking them to disk first.

import (    "archive/zip"    "github.com/spf13/afero/zipfs")// Assume 'zipReader' is a *zip.Reader initialized from a file or memoryvar zipReader *zip.Reader // Create a read-only ZipFsarchiveFS := zipfs.New(zipReader)// Read a file from within the archive using the standard Afero APIcontent, err := afero.ReadFile(archiveFS, "/docs/readme.md")
Serving Any Filesystem over HTTP

UseHttpFs to expose any Afero filesystem—even one created dynamically in memory—through a standard Go web server.

import (    "net/http"    "github.com/spf13/afero")func main() {    memFS := afero.NewMemMapFs()    afero.WriteFile(memFS, "index.html", []byte("<h1>Hello from Memory!</h1>"), 0644)    // Wrap the memory filesystem to make it compatible with http.FileServer.    httpFS := afero.NewHttpFs(memFS)    http.Handle("/", http.FileServer(httpFS.Dir("/")))    http.ListenAndServe(":8080", nil)}
Testing Made Simple

One of Afero's greatest strengths is making filesystem-dependent code easily testable:

func SaveUserData(fs afero.Fs, userID string, data []byte) error {    filename := fmt.Sprintf("users/%s.json", userID)    return afero.WriteFile(fs, filename, data, 0644)}func TestSaveUserData(t *testing.T) {    // Create a clean, fast, in-memory filesystem for testing    testFS := afero.NewMemMapFs()        userData := []byte(`{"name": "John", "email": "john@example.com"}`)    err := SaveUserData(testFS, "123", userData)        if err != nil {        t.Fatalf("SaveUserData failed: %v", err)    }        // Verify the file was saved correctly    saved, err := afero.ReadFile(testFS, "users/123.json")    if err != nil {        t.Fatalf("Failed to read saved file: %v", err)    }        if string(saved) != string(userData) {        t.Errorf("Data mismatch: got %s, want %s", saved, userData)    }}

Benefits of testing with Afero:

  • Fast - No disk I/O, tests run in memory
  • 🔄Reliable - Each test starts with a clean slate
  • 🧹No cleanup - Memory is automatically freed
  • 🔒Safe - Can't accidentally modify real files
  • 🏃Parallel - Tests can run concurrently without conflicts

Backend Reference

TypeBackendConstructorDescriptionStatus
CoreOsFsafero.NewOsFs()Interacts with the real operating system filesystem. Use in production.✅ Official
MemMapFsafero.NewMemMapFs()A fast, atomic, concurrent-safe, in-memory filesystem. Ideal for testing.✅ Official
CompositionCopyOnWriteFsafero.NewCopyOnWriteFs(base, overlay)A read-only base with a writable overlay. Ideal for sandboxing.✅ Official
CacheOnReadFsafero.NewCacheOnReadFs(base, cache, ttl)Lazily caches files from a slow base into a fast layer on first read.✅ Official
BasePathFsafero.NewBasePathFs(source, path)Restricts operations to a subdirectory (chroot/jail).✅ Official
ReadOnlyFsafero.NewReadOnlyFs(source)Provides a read-only view, preventing any modifications.✅ Official
RegexpFsafero.NewRegexpFs(source, regexp)Filters a filesystem, only showing files that match a regex.✅ Official
UtilityHttpFsafero.NewHttpFs(source)Wraps any Afero filesystem to be served viahttp.FileServer.✅ Official
ArchivesZipFszipfs.New(zipReader)Read-only access to files within a ZIP archive.✅ Official
TarFstarfs.New(tarReader)Read-only access to files within a TAR archive.✅ Official
NetworkGcsFsgcsfs.NewGcsFs(...)Google Cloud Storage backend.⚡ Experimental
SftpFssftpfs.New(...)SFTP backend.⚡ Experimental
3rd Party CloudS3Fsfclairamb/afero-s3Production-ready S3 backend built on official AWS SDK.🔹 3rd Party
MinioFscpyun/afero-minioMinIO object storage backend with S3 compatibility.🔹 3rd Party
DriveFsfclairamb/afero-gdriveGoogle Drive backend with streaming support.🔹 3rd Party
DropboxFsfclairamb/afero-dropboxDropbox backend with streaming support.🔹 3rd Party
3rd Party SpecializedGitFstobiash/go-gitfsGit repository filesystem (read-only, Afero compatible).🔹 3rd Party
DockerFsunmango/aferoxDocker container filesystem access.🔹 3rd Party
GitHubFsunmango/aferoxGitHub repository and releases filesystem.🔹 3rd Party
FilterFsunmango/aferoxFilesystem filtering with predicates.🔹 3rd Party
IgnoreFsunmango/aferox.gitignore-aware filtering filesystem.🔹 3rd Party
FUSEFsJakWai01/sile-fystemGeneric FUSE implementation using any Afero backend.🔹 3rd Party

Afero vs.io/fs (Go 1.16+)

Go 1.16 introduced theio/fs package, which provides a standard abstraction forread-only filesystems.

Afero complementsio/fs by focusing on different needs:

  • Useio/fs when: You only need to read files and want to conform strictly to the standard library interfaces.
  • Use Afero when:
    • Your application needs tocreate, write, modify, or delete files.
    • You need to test complex read/write interactions (e.g., renaming, concurrent writes).
    • You need advanced compositional features (Copy-on-Write, Caching, etc.).

Afero is fully compatible withio/fs. You can wrap any Afero filesystem to satisfy thefs.FS interface usingafero.NewIOFS:

import "io/fs"// Create an Afero filesystem (writable)var myAferoFs afero.Fs = afero.NewMemMapFs()// Convert it to a standard library fs.FS (read-only view)var myIoFs fs.FS = afero.NewIOFS(myAferoFs)

Third-Party Backends & Ecosystem

The Afero community has developed numerous backends and tools that extend the library's capabilities. Below are curated, well-maintained options organized by maturity and reliability.

Featured Community Backends

These are mature, reliable backends that we can confidently recommend for production use:

Amazon S3 -fclairamb/afero-s3

Production-ready S3 backend built on the official AWS SDK for Go.

import "github.com/fclairamb/afero-s3"s3fs := s3.NewFs(bucket, session)
MinIO -cpyun/afero-minio

MinIO object storage backend providing S3-compatible object storage with deduplication and optimization features.

import "github.com/cpyun/afero-minio"minioFs := miniofs.NewMinioFs(ctx, "minio://endpoint/bucket")
Community & Specialized Backends
Cloud Storage
Version Control Systems
  • Git Repositories -tobiash/go-gitfs
    Read-only filesystem abstraction for Git repositories. Works with bare repositories and provides filesystem view of any git reference. Uses go-git for repository access.
Container and Remote Systems
  • Docker Containers -unmango/aferox
    Access Docker container filesystems as if they were local filesystems

  • GitHub API -unmango/aferox
    Turn GitHub repositories, releases, and assets into browsable filesystems

FUSE Integration
  • Generic FUSE -JakWai01/sile-fystem
    Mount any Afero filesystem as a FUSE filesystem, allowing any Afero backend to be used as a real mounted filesystem
Specialized Filesystems
  • FAT32 Support -aligator/GoFAT
    Pure Go FAT filesystem implementation (currently read-only)
Interface Adapters & Utilities

Cross-Interface Compatibility:

Working Directory Management:

Advanced Filtering:

  • unmango/aferox includes multiple specialized filesystems:
    • FilterFs - Predicate-based file filtering
    • IgnoreFs - .gitignore-aware filtering
    • WriterFs - Dump writes to io.Writer for debugging
Developer Tools & Utilities

nhatthm Utility Suite - Essential tools for Afero development:

Ecosystem Showcase

Windows Virtual Drives -balazsgrill/potatodrive
Mount any Afero filesystem as a Windows drive letter. Brilliant demonstration of Afero's power!

Modern Asset Embedding (Go 1.16+)

Instead of third-party tools, use Go's native//go:embed with Afero:

import (    "embed"    "github.com/spf13/afero")//go:embed assets/*var assetsFS embed.FSfunc main() {    // Convert embedded files to Afero filesystem    fs := afero.FromIOFS(assetsFS)        // Use like any other Afero filesystem    content, _ := afero.ReadFile(fs, "assets/config.json")}

Contributing

We welcome contributions! The project is mature, but we are actively looking for contributors to help implement and stabilize network/cloud backends.

  • 🔥Microsoft Azure Blob Storage
  • 🔒Modern Encryption Backend - Built on secure, contemporary crypto (not legacy EncFS)
  • 🐙Canonical go-git Adapter - Unified solution for Git integration
  • 📡SSH/SCP Backend - Secure remote file operations
  • Stabilization of existing experimental backends (GCS, SFTP)

To contribute:

  1. Fork the repository
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

📄 License

Afero is released under the Apache 2.0 license. SeeLICENSE.txt for details.

🔗 Additional Resources


Afero comes from the Latin roots Ad-Facere, meaning "to make" or "to do" - fitting for a library that empowers you to make and do amazing things with filesystems.

Documentation

Index

Constants

View Source
const FilePathSeparator =string(filepath.Separator)

Filepath separator defined by os.Separator.

Variables

View Source
var (ErrFileClosed        =errors.New("File is closed")ErrOutOfRange        =errors.New("out of range")ErrTooLarge          =errors.New("too large")ErrFileNotFound      =os.ErrNotExistErrFileExists        =os.ErrExistErrDestinationExists =os.ErrExist)
View Source
var ErrNoReadlink =errors.New("readlink not supported")

ErrNoReadlink is the error that will be wrapped in an os.Path if a file systemdoes not support the readlink operation either directly or through its delegated filesystem.As expressed by support for the LinkReader interface.

View Source
var ErrNoSymlink =errors.New("symlink not supported")

ErrNoSymlink is the error that will be wrapped in an os.LinkError if a file systemdoes not support Symlink's either directly or through its delegated filesystem.As expressed by support for the Linker interface.

Functions

funcDirExists

func DirExists(fsFs, pathstring) (bool,error)

DirExists checks if a path exists and is a directory.

funcExists

func Exists(fsFs, pathstring) (bool,error)

Check if a file or directory exists.

funcFileContainsAnyBytes

func FileContainsAnyBytes(fsFs, filenamestring, subslices [][]byte) (bool,error)

Check if a file contains any of the specified byte slices.

funcFileContainsBytes

func FileContainsBytes(fsFs, filenamestring, subslice []byte) (bool,error)

Check if a file contains a specified byte slice.

funcFullBaseFsPath

func FullBaseFsPath(basePathFs *BasePathFs, relativePathstring)string

funcGetTempDir

func GetTempDir(fsFs, subPathstring)string

GetTempDir returns the default temp directory with trailing slashif subPath is not empty then it will be created recursively with mode 777 rwx rwx rwx

funcGlob

func Glob(fsFs, patternstring) (matches []string, errerror)

Glob returns the names of all files matching pattern or nilif there is no matching file. The syntax of patterns is the sameas in Match. The pattern may describe hierarchical names such as/usr/*/bin/ed (assuming the Separator is '/').

Glob ignores file system errors such as I/O errors reading directories.The only possible returned error is ErrBadPattern, when patternis malformed.

This was adapted from (http://golang.org/pkg/path/filepath) and uses severalbuilt-ins from that package.

funcIsDir

func IsDir(fsFs, pathstring) (bool,error)

IsDir checks if a given path is a directory.

funcIsEmpty

func IsEmpty(fsFs, pathstring) (bool,error)

IsEmpty checks if a given file or directory is empty.

funcNeuterAccents

func NeuterAccents(sstring)string

Transform characters with accents into plain forms.

funcReadAll

func ReadAll(rio.Reader) ([]byte,error)

ReadAll reads from r until an error or EOF and returns the data it read.A successful call returns err == nil, not err == EOF. Because ReadAll isdefined to read from src until EOF, it does not treat an EOF from Readas an error to be reported.

funcReadDir

func ReadDir(fsFs, dirnamestring) ([]os.FileInfo,error)

funcReadFile

func ReadFile(fsFs, filenamestring) ([]byte,error)

funcSafeWriteReader

func SafeWriteReader(fsFs, pathstring, rio.Reader) (errerror)

funcTempDir

func TempDir(fsFs, dir, prefixstring) (namestring, errerror)

funcUnicodeSanitize

func UnicodeSanitize(sstring)string

Rewrite string to remove non-standard path characters

funcWalk

func Walk(fsFs, rootstring, walkFnfilepath.WalkFunc)error

funcWriteFile

func WriteFile(fsFs, filenamestring, data []byte, permos.FileMode)error

funcWriteReader

func WriteReader(fsFs, pathstring, rio.Reader) (errerror)

Types

typeAfero

type Afero struct {Fs}

func (Afero)DirExists

func (aAfero) DirExists(pathstring) (bool,error)

func (Afero)Exists

func (aAfero) Exists(pathstring) (bool,error)

func (Afero)FileContainsAnyBytes

func (aAfero) FileContainsAnyBytes(filenamestring, subslices [][]byte) (bool,error)

func (Afero)FileContainsBytes

func (aAfero) FileContainsBytes(filenamestring, subslice []byte) (bool,error)

func (Afero)GetTempDir

func (aAfero) GetTempDir(subPathstring)string

func (Afero)IsDir

func (aAfero) IsDir(pathstring) (bool,error)

func (Afero)IsEmpty

func (aAfero) IsEmpty(pathstring) (bool,error)

func (Afero)ReadDir

func (aAfero) ReadDir(dirnamestring) ([]os.FileInfo,error)

ReadDir reads the directory named by dirname and returnsa list of sorted directory entries.

func (Afero)ReadFile

func (aAfero) ReadFile(filenamestring) ([]byte,error)

ReadFile reads the file named by filename and returns the contents.A successful call returns err == nil, not err == EOF. Because ReadFilereads the whole file, it does not treat an EOF from Read as an errorto be reported.

func (Afero)SafeWriteReader

func (aAfero) SafeWriteReader(pathstring, rio.Reader) (errerror)

Same as WriteReader but checks to see if file/directory already exists.

func (Afero)TempDir

func (aAfero) TempDir(dir, prefixstring) (namestring, errerror)

TempDir creates a new temporary directory in the directory dirwith a name beginning with prefix and returns the path of thenew directory. If dir is the empty string, TempDir uses thedefault directory for temporary files (see os.TempDir).Multiple programs calling TempDir simultaneouslywill not choose the same directory. It is the caller's responsibilityto remove the directory when no longer needed.

func (Afero)TempFile

func (aAfero) TempFile(dir, patternstring) (fFile, errerror)

TempFile creates a new temporary file in the directory dir,opens the file for reading and writing, and returns the resulting *os.File.The filename is generated by taking pattern and adding a randomstring to the end. If pattern includes a "*", the random stringreplaces the last "*".If dir is the empty string, TempFile uses the default directoryfor temporary files (see os.TempDir).Multiple programs calling TempFile simultaneouslywill not choose the same file. The caller can use f.Name()to find the pathname of the file. It is the caller's responsibilityto remove the file when no longer needed.

func (Afero)Walk

func (aAfero) Walk(rootstring, walkFnfilepath.WalkFunc)error

func (Afero)WriteFile

func (aAfero) WriteFile(filenamestring, data []byte, permos.FileMode)error

WriteFile writes data to a file named by filename.If the file does not exist, WriteFile creates it with permissions perm;otherwise WriteFile truncates it before writing.

func (Afero)WriteReader

func (aAfero) WriteReader(pathstring, rio.Reader) (errerror)

Takes a reader and a path and writes the content

typeBasePathFileadded inv1.1.0

type BasePathFile struct {File// contains filtered or unexported fields}

func (*BasePathFile)Nameadded inv1.1.0

func (f *BasePathFile) Name()string

func (*BasePathFile)ReadDiradded inv1.9.0

func (f *BasePathFile) ReadDir(nint) ([]fs.DirEntry,error)

typeBasePathFs

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

The BasePathFs restricts all operations to a given path within an Fs.The given file name to the operations on this Fs will be prepended withthe base path before calling the base Fs.Any file name (after filepath.Clean()) outside this base path will betreated as non existing file.

Note that it does not clean the error messages on return, so you mayreveal the real path on errors.

func (*BasePathFs)Chmod

func (b *BasePathFs) Chmod(namestring, modeos.FileMode) (errerror)

func (*BasePathFs)Chownadded inv1.5.0

func (b *BasePathFs) Chown(namestring, uid, gidint) (errerror)

func (*BasePathFs)Chtimes

func (b *BasePathFs) Chtimes(namestring, atime, mtimetime.Time) (errerror)

func (*BasePathFs)Create

func (b *BasePathFs) Create(namestring) (fFile, errerror)

func (*BasePathFs)LstatIfPossibleadded inv1.1.0

func (b *BasePathFs) LstatIfPossible(namestring) (os.FileInfo,bool,error)

func (*BasePathFs)Mkdir

func (b *BasePathFs) Mkdir(namestring, modeos.FileMode) (errerror)

func (*BasePathFs)MkdirAll

func (b *BasePathFs) MkdirAll(namestring, modeos.FileMode) (errerror)

func (*BasePathFs)Name

func (b *BasePathFs) Name()string

func (*BasePathFs)Open

func (b *BasePathFs) Open(namestring) (fFile, errerror)

func (*BasePathFs)OpenFile

func (b *BasePathFs) OpenFile(namestring, flagint, modeos.FileMode) (fFile, errerror)

func (*BasePathFs)ReadlinkIfPossibleadded inv1.3.0

func (b *BasePathFs) ReadlinkIfPossible(namestring) (string,error)

func (*BasePathFs)RealPath

func (b *BasePathFs) RealPath(namestring) (pathstring, errerror)

on a file outside the base path it returns the given file name and an error,else the given file with the base path prepended

func (*BasePathFs)Remove

func (b *BasePathFs) Remove(namestring) (errerror)

func (*BasePathFs)RemoveAll

func (b *BasePathFs) RemoveAll(namestring) (errerror)

func (*BasePathFs)Rename

func (b *BasePathFs) Rename(oldname, newnamestring) (errerror)

func (*BasePathFs)Stat

func (b *BasePathFs) Stat(namestring) (fios.FileInfo, errerror)

func (*BasePathFs)SymlinkIfPossibleadded inv1.3.0

func (b *BasePathFs) SymlinkIfPossible(oldname, newnamestring)error

typeCacheOnReadFs

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

If the cache duration is 0, cache time will be unlimited, i.e. oncea file is in the layer, the base will never be read again for this file.

For cache times greater than 0, the modification time of a file ischecked. Note that a lot of file system implementations only allow aresolution of a second for timestamps... or as the godoc for os.Chtimes()states: "The underlying filesystem may truncate or round the values to aless precise time unit."

This caching union will forward all write calls also to the base filesystem first. To prevent writing to the base Fs, wrap it in a read-onlyfilter - Note: this will also make the overlay read-only, for writing filesin the overlay, use the overlay Fs directly, not via the union Fs.

func (*CacheOnReadFs)Chmod

func (u *CacheOnReadFs) Chmod(namestring, modeos.FileMode)error

func (*CacheOnReadFs)Chownadded inv1.5.0

func (u *CacheOnReadFs) Chown(namestring, uid, gidint)error

func (*CacheOnReadFs)Chtimes

func (u *CacheOnReadFs) Chtimes(namestring, atime, mtimetime.Time)error

func (*CacheOnReadFs)Create

func (u *CacheOnReadFs) Create(namestring) (File,error)

func (*CacheOnReadFs)Mkdir

func (u *CacheOnReadFs) Mkdir(namestring, permos.FileMode)error

func (*CacheOnReadFs)MkdirAll

func (u *CacheOnReadFs) MkdirAll(namestring, permos.FileMode)error

func (*CacheOnReadFs)Name

func (u *CacheOnReadFs) Name()string

func (*CacheOnReadFs)Open

func (u *CacheOnReadFs) Open(namestring) (File,error)

func (*CacheOnReadFs)OpenFile

func (u *CacheOnReadFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (*CacheOnReadFs)Remove

func (u *CacheOnReadFs) Remove(namestring)error

func (*CacheOnReadFs)RemoveAll

func (u *CacheOnReadFs) RemoveAll(namestring)error

func (*CacheOnReadFs)Rename

func (u *CacheOnReadFs) Rename(oldname, newnamestring)error

func (*CacheOnReadFs)Stat

func (u *CacheOnReadFs) Stat(namestring) (os.FileInfo,error)

typeCopyOnWriteFs

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

The CopyOnWriteFs is a union filesystem: a read only base file system witha possibly writeable layer on top. Changes to the file system will onlybe made in the overlay: Changing an existing file in the base layer whichis not present in the overlay will copy the file to the overlay ("changing"includes also calls to e.g. Chtimes(), Chmod() and Chown()).

Reading directories is currently only supported via Open(), not OpenFile().

func (*CopyOnWriteFs)Chmod

func (u *CopyOnWriteFs) Chmod(namestring, modeos.FileMode)error

func (*CopyOnWriteFs)Chownadded inv1.5.0

func (u *CopyOnWriteFs) Chown(namestring, uid, gidint)error

func (*CopyOnWriteFs)Chtimes

func (u *CopyOnWriteFs) Chtimes(namestring, atime, mtimetime.Time)error

func (*CopyOnWriteFs)Create

func (u *CopyOnWriteFs) Create(namestring) (File,error)

func (*CopyOnWriteFs)LstatIfPossibleadded inv1.1.0

func (u *CopyOnWriteFs) LstatIfPossible(namestring) (os.FileInfo,bool,error)

func (*CopyOnWriteFs)Mkdir

func (u *CopyOnWriteFs) Mkdir(namestring, permos.FileMode)error

func (*CopyOnWriteFs)MkdirAll

func (u *CopyOnWriteFs) MkdirAll(namestring, permos.FileMode)error

func (*CopyOnWriteFs)Name

func (u *CopyOnWriteFs) Name()string

func (*CopyOnWriteFs)Open

func (u *CopyOnWriteFs) Open(namestring) (File,error)

This function handles the 9 different possibilities causedby the union which are the intersection of the following...

layer: doesn't exist, exists as a file, and exists as a directorybase:  doesn't exist, exists as a file, and exists as a directory

func (*CopyOnWriteFs)OpenFile

func (u *CopyOnWriteFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (*CopyOnWriteFs)ReadlinkIfPossibleadded inv1.3.0

func (u *CopyOnWriteFs) ReadlinkIfPossible(namestring) (string,error)

func (*CopyOnWriteFs)Remove

func (u *CopyOnWriteFs) Remove(namestring)error

Removing files present only in the base layer is not permitted. Ifa file is present in the base layer and the overlay, only the overlaywill be removed.

func (*CopyOnWriteFs)RemoveAll

func (u *CopyOnWriteFs) RemoveAll(namestring)error

func (*CopyOnWriteFs)Rename

func (u *CopyOnWriteFs) Rename(oldname, newnamestring)error

Renaming files present only in the base layer is not permitted

func (*CopyOnWriteFs)Stat

func (u *CopyOnWriteFs) Stat(namestring) (os.FileInfo,error)

func (*CopyOnWriteFs)SymlinkIfPossibleadded inv1.3.0

func (u *CopyOnWriteFs) SymlinkIfPossible(oldname, newnamestring)error

typeDirsMergeradded inv1.1.0

type DirsMerger func(lofi, bofi []os.FileInfo) ([]os.FileInfo,error)

DirsMerger is how UnionFile weaves two directories together.It takes the FileInfo slices from the layer and the base and returns asingle view.

typeFile

type File interface {io.Closerio.Readerio.ReaderAtio.Seekerio.Writerio.WriterAtName()stringReaddir(countint) ([]os.FileInfo,error)Readdirnames(nint) ([]string,error)Stat() (os.FileInfo,error)Sync()errorTruncate(sizeint64)errorWriteString(sstring) (retint, errerror)}

File represents a file in the filesystem.

funcTempFile

func TempFile(fsFs, dir, patternstring) (fFile, errerror)

typeFromIOFSadded inv1.6.0

type FromIOFS struct {fs.FS}

FromIOFS adopts io/fs.FS to use it as afero.FsNote that io/fs.FS is read-only so all mutating methods will return fs.PathError with fs.ErrPermissionTo store modifications you may use afero.CopyOnWriteFs

func (FromIOFS)Chmodadded inv1.6.0

func (fFromIOFS) Chmod(namestring, modeos.FileMode)error

func (FromIOFS)Chownadded inv1.6.0

func (fFromIOFS) Chown(namestring, uid, gidint)error

func (FromIOFS)Chtimesadded inv1.6.0

func (fFromIOFS) Chtimes(namestring, atimetime.Time, mtimetime.Time)error

func (FromIOFS)Createadded inv1.6.0

func (fFromIOFS) Create(namestring) (File,error)

func (FromIOFS)Mkdiradded inv1.6.0

func (fFromIOFS) Mkdir(namestring,permos.FileMode,)error

func (FromIOFS)MkdirAlladded inv1.6.0

func (fFromIOFS) MkdirAll(pathstring, permos.FileMode)error

func (FromIOFS)Nameadded inv1.6.0

func (fFromIOFS) Name()string

func (FromIOFS)Openadded inv1.6.0

func (fFromIOFS) Open(namestring) (File,error)

func (FromIOFS)OpenFileadded inv1.6.0

func (fFromIOFS) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (FromIOFS)Removeadded inv1.6.0

func (fFromIOFS) Remove(namestring)error

func (FromIOFS)RemoveAlladded inv1.6.0

func (fFromIOFS) RemoveAll(pathstring)error

func (FromIOFS)Renameadded inv1.6.0

func (fFromIOFS) Rename(oldname, newnamestring)error

func (FromIOFS)Statadded inv1.6.0

func (fFromIOFS) Stat(namestring) (os.FileInfo,error)

typeFs

type Fs interface {// Create creates a file in the filesystem, returning the file and an// error, if any happens.Create(namestring) (File,error)// Mkdir creates a directory in the filesystem, return an error if any// happens.Mkdir(namestring, permos.FileMode)error// MkdirAll creates a directory path and all parents that does not exist// yet.MkdirAll(pathstring, permos.FileMode)error// Open opens a file, returning it or an error, if any happens.Open(namestring) (File,error)// OpenFile opens a file using the given flags and the given mode.OpenFile(namestring, flagint, permos.FileMode) (File,error)// Remove removes a file identified by name, returning an error, if any// happens.Remove(namestring)error// RemoveAll removes a directory path and any children it contains. It// does not fail if the path does not exist (return nil).RemoveAll(pathstring)error// Rename renames a file.Rename(oldname, newnamestring)error// Stat returns a FileInfo describing the named file, or an error, if any// happens.Stat(namestring) (os.FileInfo,error)// The name of this FileSystemName()string// Chmod changes the mode of the named file to mode.Chmod(namestring, modeos.FileMode)error// Chown changes the uid and gid of the named file.Chown(namestring, uid, gidint)error// Chtimes changes the access and modification times of the named fileChtimes(namestring, atimetime.Time, mtimetime.Time)error}

Fs is the filesystem interface.

Any simulated or real filesystem should implement this interface.

funcNewBasePathFs

func NewBasePathFs(sourceFs, pathstring)Fs

funcNewCacheOnReadFs

func NewCacheOnReadFs(baseFs, layerFs, cacheTimetime.Duration)Fs

funcNewCopyOnWriteFs

func NewCopyOnWriteFs(baseFs, layerFs)Fs

funcNewMemMapFs

func NewMemMapFs()Fs

funcNewOsFs

func NewOsFs()Fs

funcNewReadOnlyFs

func NewReadOnlyFs(sourceFs)Fs

funcNewRegexpFs

func NewRegexpFs(sourceFs, re *regexp.Regexp)Fs

typeHttpFs

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

funcNewHttpFs

func NewHttpFs(sourceFs) *HttpFs

func (HttpFs)Chmod

func (hHttpFs) Chmod(namestring, modeos.FileMode)error

func (HttpFs)Chownadded inv1.5.0

func (hHttpFs) Chown(namestring, uid, gidint)error

func (HttpFs)Chtimes

func (hHttpFs) Chtimes(namestring, atimetime.Time, mtimetime.Time)error

func (HttpFs)Create

func (hHttpFs) Create(namestring) (File,error)

func (HttpFs)Dir

func (hHttpFs) Dir(sstring) *httpDir

func (HttpFs)Mkdir

func (hHttpFs) Mkdir(namestring, permos.FileMode)error

func (HttpFs)MkdirAll

func (hHttpFs) MkdirAll(pathstring, permos.FileMode)error

func (HttpFs)Name

func (hHttpFs) Name()string

func (HttpFs)Open

func (hHttpFs) Open(namestring) (http.File,error)

func (HttpFs)OpenFile

func (hHttpFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (HttpFs)Remove

func (hHttpFs) Remove(namestring)error

func (HttpFs)RemoveAll

func (hHttpFs) RemoveAll(pathstring)error

func (HttpFs)Rename

func (hHttpFs) Rename(oldname, newnamestring)error

func (HttpFs)Stat

func (hHttpFs) Stat(namestring) (os.FileInfo,error)

typeIOFSadded inv1.6.0

type IOFS struct {Fs}

IOFS adopts afero.Fs to stdlib io/fs.FS

funcNewIOFSadded inv1.6.0

func NewIOFS(fsFs)IOFS

func (IOFS)Globadded inv1.6.0

func (iofsIOFS) Glob(patternstring) ([]string,error)

func (IOFS)Openadded inv1.6.0

func (iofsIOFS) Open(namestring) (fs.File,error)

func (IOFS)ReadDiradded inv1.6.0

func (iofsIOFS) ReadDir(namestring) ([]fs.DirEntry,error)

func (IOFS)ReadFileadded inv1.6.0

func (iofsIOFS) ReadFile(namestring) ([]byte,error)

func (IOFS)Subadded inv1.6.0

func (iofsIOFS) Sub(dirstring) (fs.FS,error)

typeLinkReaderadded inv1.3.0

type LinkReader interface {ReadlinkIfPossible(namestring) (string,error)}

LinkReader is an optional interface in Afero. It is only implemented by thefilesystems saying so.

typeLinkeradded inv1.3.0

type Linker interface {SymlinkIfPossible(oldname, newnamestring)error}

Linker is an optional interface in Afero. It is only implemented by thefilesystems saying so.It will call Symlink if the filesystem itself is, or it delegates to, the os filesystem,or the filesystem otherwise supports Symlink's.

typeLstateradded inv1.1.0

type Lstater interface {LstatIfPossible(namestring) (os.FileInfo,bool,error)}

Lstater is an optional interface in Afero. It is only implemented by thefilesystems saying so.It will call Lstat if the filesystem itself is, or it delegates to, the os filesystem.Else it will call Stat.In addition to the FileInfo, it will return a boolean telling whether Lstat was called or not.

typeMemMapFs

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

func (*MemMapFs)Chmod

func (m *MemMapFs) Chmod(namestring, modeos.FileMode)error

func (*MemMapFs)Chownadded inv1.5.0

func (m *MemMapFs) Chown(namestring, uid, gidint)error

func (*MemMapFs)Chtimes

func (m *MemMapFs) Chtimes(namestring, atimetime.Time, mtimetime.Time)error

func (*MemMapFs)Create

func (m *MemMapFs) Create(namestring) (File,error)

func (*MemMapFs)List

func (m *MemMapFs) List()

func (*MemMapFs)LstatIfPossibleadded inv1.4.0

func (m *MemMapFs) LstatIfPossible(namestring) (os.FileInfo,bool,error)

func (*MemMapFs)Mkdir

func (m *MemMapFs) Mkdir(namestring, permos.FileMode)error

func (*MemMapFs)MkdirAll

func (m *MemMapFs) MkdirAll(pathstring, permos.FileMode)error

func (*MemMapFs)Name

func (*MemMapFs) Name()string

func (*MemMapFs)Open

func (m *MemMapFs) Open(namestring) (File,error)

func (*MemMapFs)OpenFile

func (m *MemMapFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (*MemMapFs)Remove

func (m *MemMapFs) Remove(namestring)error

func (*MemMapFs)RemoveAll

func (m *MemMapFs) RemoveAll(pathstring)error

func (*MemMapFs)Rename

func (m *MemMapFs) Rename(oldname, newnamestring)error

func (*MemMapFs)Stat

func (m *MemMapFs) Stat(namestring) (os.FileInfo,error)

typeOsFs

type OsFs struct{}

OsFs is a Fs implementation that uses functions provided by the os package.

For details in any method, check the documentation of the os package(http://golang.org/pkg/os/).

func (OsFs)Chmod

func (OsFs) Chmod(namestring, modeos.FileMode)error

func (OsFs)Chownadded inv1.5.0

func (OsFs) Chown(namestring, uid, gidint)error

func (OsFs)Chtimes

func (OsFs) Chtimes(namestring, atimetime.Time, mtimetime.Time)error

func (OsFs)Create

func (OsFs) Create(namestring) (File,error)

func (OsFs)LstatIfPossibleadded inv1.1.0

func (OsFs) LstatIfPossible(namestring) (os.FileInfo,bool,error)

func (OsFs)Mkdir

func (OsFs) Mkdir(namestring, permos.FileMode)error

func (OsFs)MkdirAll

func (OsFs) MkdirAll(pathstring, permos.FileMode)error

func (OsFs)Name

func (OsFs) Name()string

func (OsFs)Open

func (OsFs) Open(namestring) (File,error)

func (OsFs)OpenFile

func (OsFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (OsFs)ReadlinkIfPossibleadded inv1.3.0

func (OsFs) ReadlinkIfPossible(namestring) (string,error)

func (OsFs)Remove

func (OsFs) Remove(namestring)error

func (OsFs)RemoveAll

func (OsFs) RemoveAll(pathstring)error

func (OsFs)Rename

func (OsFs) Rename(oldname, newnamestring)error

func (OsFs)Stat

func (OsFs) Stat(namestring) (os.FileInfo,error)

func (OsFs)SymlinkIfPossibleadded inv1.3.0

func (OsFs) SymlinkIfPossible(oldname, newnamestring)error

typeReadOnlyFs

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

func (*ReadOnlyFs)Chmod

func (r *ReadOnlyFs) Chmod(nstring, mos.FileMode)error

func (*ReadOnlyFs)Chownadded inv1.5.0

func (r *ReadOnlyFs) Chown(nstring, uid, gidint)error

func (*ReadOnlyFs)Chtimes

func (r *ReadOnlyFs) Chtimes(nstring, a, mtime.Time)error

func (*ReadOnlyFs)Create

func (r *ReadOnlyFs) Create(nstring) (File,error)

func (*ReadOnlyFs)LstatIfPossibleadded inv1.1.0

func (r *ReadOnlyFs) LstatIfPossible(namestring) (os.FileInfo,bool,error)

func (*ReadOnlyFs)Mkdir

func (r *ReadOnlyFs) Mkdir(nstring, pos.FileMode)error

func (*ReadOnlyFs)MkdirAll

func (r *ReadOnlyFs) MkdirAll(nstring, pos.FileMode)error

func (*ReadOnlyFs)Name

func (r *ReadOnlyFs) Name()string

func (*ReadOnlyFs)Open

func (r *ReadOnlyFs) Open(nstring) (File,error)

func (*ReadOnlyFs)OpenFile

func (r *ReadOnlyFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (*ReadOnlyFs)ReadDir

func (r *ReadOnlyFs) ReadDir(namestring) ([]os.FileInfo,error)

func (*ReadOnlyFs)ReadlinkIfPossibleadded inv1.3.0

func (r *ReadOnlyFs) ReadlinkIfPossible(namestring) (string,error)

func (*ReadOnlyFs)Remove

func (r *ReadOnlyFs) Remove(nstring)error

func (*ReadOnlyFs)RemoveAll

func (r *ReadOnlyFs) RemoveAll(pstring)error

func (*ReadOnlyFs)Rename

func (r *ReadOnlyFs) Rename(o, nstring)error

func (*ReadOnlyFs)Stat

func (r *ReadOnlyFs) Stat(namestring) (os.FileInfo,error)

func (*ReadOnlyFs)SymlinkIfPossibleadded inv1.3.0

func (r *ReadOnlyFs) SymlinkIfPossible(oldname, newnamestring)error

typeRegexpFile

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

func (*RegexpFile)Close

func (f *RegexpFile) Close()error

func (*RegexpFile)Name

func (f *RegexpFile) Name()string

func (*RegexpFile)Read

func (f *RegexpFile) Read(s []byte) (int,error)

func (*RegexpFile)ReadAt

func (f *RegexpFile) ReadAt(s []byte, oint64) (int,error)

func (*RegexpFile)Readdir

func (f *RegexpFile) Readdir(cint) (fi []os.FileInfo, errerror)

func (*RegexpFile)Readdirnames

func (f *RegexpFile) Readdirnames(cint) (n []string, errerror)

func (*RegexpFile)Seek

func (f *RegexpFile) Seek(oint64, wint) (int64,error)

func (*RegexpFile)Stat

func (f *RegexpFile) Stat() (os.FileInfo,error)

func (*RegexpFile)Sync

func (f *RegexpFile) Sync()error

func (*RegexpFile)Truncate

func (f *RegexpFile) Truncate(sint64)error

func (*RegexpFile)Write

func (f *RegexpFile) Write(s []byte) (int,error)

func (*RegexpFile)WriteAt

func (f *RegexpFile) WriteAt(s []byte, oint64) (int,error)

func (*RegexpFile)WriteString

func (f *RegexpFile) WriteString(sstring) (int,error)

typeRegexpFs

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

The RegexpFs filters files (not directories) by regular expression. Onlyfiles matching the given regexp will be allowed, all others get a ENOENT error ("No such file or directory").

func (*RegexpFs)Chmod

func (r *RegexpFs) Chmod(namestring, modeos.FileMode)error

func (*RegexpFs)Chownadded inv1.5.0

func (r *RegexpFs) Chown(namestring, uid, gidint)error

func (*RegexpFs)Chtimes

func (r *RegexpFs) Chtimes(namestring, a, mtime.Time)error

func (*RegexpFs)Create

func (r *RegexpFs) Create(namestring) (File,error)

func (*RegexpFs)Mkdir

func (r *RegexpFs) Mkdir(nstring, pos.FileMode)error

func (*RegexpFs)MkdirAll

func (r *RegexpFs) MkdirAll(nstring, pos.FileMode)error

func (*RegexpFs)Name

func (r *RegexpFs) Name()string

func (*RegexpFs)Open

func (r *RegexpFs) Open(namestring) (File,error)

func (*RegexpFs)OpenFile

func (r *RegexpFs) OpenFile(namestring, flagint, permos.FileMode) (File,error)

func (*RegexpFs)Remove

func (r *RegexpFs) Remove(namestring)error

func (*RegexpFs)RemoveAll

func (r *RegexpFs) RemoveAll(pstring)error

func (*RegexpFs)Rename

func (r *RegexpFs) Rename(oldname, newnamestring)error

func (*RegexpFs)Stat

func (r *RegexpFs) Stat(namestring) (os.FileInfo,error)

typeSymlinkeradded inv1.3.0

type Symlinker interface {LstaterLinkerLinkReader}

Symlinker is an optional interface in Afero. It is only implemented by thefilesystems saying so.It indicates support for 3 symlink related interfaces that implement thebehaviors of the os methods:

  • Lstat
  • Symlink, and
  • Readlink

typeUnionFile

type UnionFile struct {BaseFileLayerFileMergerDirsMerger// contains filtered or unexported fields}

The UnionFile implements the afero.File interface and will be returnedwhen reading a directory present at least in the overlay or opening a filefor writing.

The calls toReaddir() and Readdirnames() merge the file os.FileInfo / names from thebase and the overlay - for files present in both layers, only thosefrom the overlay will be used.

When opening files for writing (Create() / OpenFile() with the right flags)the operations will be done in both layers, starting with the overlay. Asuccessful read in the overlay will move the cursor position in the base layerby the number of bytes read.

func (*UnionFile)Close

func (f *UnionFile) Close()error

func (*UnionFile)Name

func (f *UnionFile) Name()string

func (*UnionFile)Read

func (f *UnionFile) Read(s []byte) (int,error)

func (*UnionFile)ReadAt

func (f *UnionFile) ReadAt(s []byte, oint64) (int,error)

func (*UnionFile)Readdir

func (f *UnionFile) Readdir(cint) (ofi []os.FileInfo, errerror)

Readdir will weave the two directories together andreturn a single view of the overlayed directories.At the end of the directory view, the error is io.EOF if c > 0.

func (*UnionFile)Readdirnames

func (f *UnionFile) Readdirnames(cint) ([]string,error)

func (*UnionFile)Seek

func (f *UnionFile) Seek(oint64, wint) (posint64, errerror)

func (*UnionFile)Stat

func (f *UnionFile) Stat() (os.FileInfo,error)

func (*UnionFile)Sync

func (f *UnionFile) Sync() (errerror)

func (*UnionFile)Truncate

func (f *UnionFile) Truncate(sint64) (errerror)

func (*UnionFile)Write

func (f *UnionFile) Write(s []byte) (nint, errerror)

func (*UnionFile)WriteAt

func (f *UnionFile) WriteAt(s []byte, oint64) (nint, errerror)

func (*UnionFile)WriteString

func (f *UnionFile) WriteString(sstring) (nint, errerror)

Source Files

View all Source files

Directories

PathSynopsis
gcsfsmodule
internal
sftpfsmodule
package tarfs implements a read-only in-memory representation of a tar archive
package tarfs implements a read-only in-memory representation of a tar archive

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp