Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/avfsPublic

Another Virtual File System for Go

License

NotificationsYou must be signed in to change notification settings

avfs/avfs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Another Virtual File System for Go

CIcodecovPkgGoDevReleaseLicenseBuilt with Mage

Overview

AVFS is a virtual file system abstraction, inspired mostlybyAfero and Go standard library. It providesan abstraction layer to emulate the behavior of a file system that provides several features :

  • a set ofconstants,interfaces andtypes for all file systems
  • atest suite for all file systems (emulated or real)
  • each file system has itsown package
  • a very basicidentity manager allows testing of user related functions(Chown, Lchown) and file system permissions

Additionally, some file systems support :

  • user file creation mode mask (Umask) (MemFS, OrefaFS)
  • chroot (OSFS on Linux)
  • hard links (MemFS, OrefaFS)
  • symbolic links (MemFS)
  • multiple users concurrently (MemFS)
  • Linux andWindows emulation regardless of host operating system (MemFS, OrefaFS)

Installation

This package can be installed with the go install command :

go install github.com/avfs/avfs@latest

It is only tested with Go version >= 1.18

Getting started

To make an existing code work withAVFS :

  • replace all references ofos,path/filepathwith the variable used to initialize the file system (vfs in the followingexamples)
  • import the packages of the file systems and, if necessary, theavfs packageand initialize the file system variable.
  • some file systems provide specific options available at initialization. ForinstanceMemFS needsWithSystemDirs option to create/home,/rootand/tmp directories.

Examples

Symbolic links

The example below demonstrates the creation of a file, a symbolic link to thisfile, for a different file systems (depending on an environment variable). Errormanagement has been omitted for the sake of simplicity :

package mainimport ("bytes""log""os""github.com/avfs/avfs""github.com/avfs/avfs/vfs/memfs""github.com/avfs/avfs/vfs/osfs")funcmain() {varvfs avfs.VFSswitchos.Getenv("ENV") {case"PROD":// The real file system for production.vfs=osfs.NewWithNoIdm()default:// in memory for tests.vfs=memfs.New()}// From this point all references of 'os', 'path/filepath'// should be replaced by 'vfs'rootDir,_:=vfs.MkdirTemp("","avfs")defervfs.RemoveAll(rootDir)aFilePath:=vfs.Join(rootDir,"aFile.txt")content:= []byte("randomContent")_=vfs.WriteFile(aFilePath,content,0o644)aFilePathSl:=vfs.Join(rootDir,"aFileSymlink.txt")_=vfs.Symlink(aFilePath,aFilePathSl)gotContentSl,_:=vfs.ReadFile(aFilePathSl)if!bytes.Equal(content,gotContentSl) {log.Fatalf("Symlink %s : want content to be %v, got %v",aFilePathSl,content,gotContentSl)}log.Printf("content from symbolic link %s : %s",aFilePathSl,gotContentSl)}

Multiple users creating simultaneously directories

The example below demonstrates the concurrent creation of subdirectories under aroot directory by several users in different goroutines (works only withMemFS) :

package mainimport ("fmt""log""sync""github.com/avfs/avfs""github.com/avfs/avfs/idm/memidm""github.com/avfs/avfs/vfs/memfs")funcmain() {const (maxUsers=100groupName="test_users")idm:=memidm.New()vfs:=memfs.New()rootDir,_:=vfs.MkdirTemp("","avfs")vfs.Chmod(rootDir,0o777)g,_:=idm.GroupAdd(groupName)varwg sync.WaitGroupwg.Add(maxUsers)fori:=0;i<maxUsers;i++ {gofunc(iint) {deferwg.Done()userName:=fmt.Sprintf("user_%08d",i)idm.UserAdd(userName,g.Name())vfsU,_:=vfs.Sub("/")vfsU.SetUser(userName)path:=vfsU.Join(rootDir,userName)vfsU.Mkdir(path,avfs.DefaultDirPerm)}(i)}wg.Wait()entries,_:=vfs.ReadDir(rootDir)log.Println("number of dirs :",len(entries))for_,entry:=rangeentries {info,_:=entry.Info()sst:=vfs.ToSysStat(info)u,_:=idm.LookupUserId(sst.Uid())log.Println("dir :",info.Name(),", mode :",info.Mode(),", owner :",u.Name())}}

Status

Almost ready for Windows.

File systems

All file systems implement at leastavfs.FS andavfs.File interfaces.By default, each file system supported methods are the most commonly used frompackagesos andpath/filepath. All methods have identical names as theirfunctions counterparts.The following file systems are currently available :

File systemComments
BasePathFSfile system that restricts all operations to a given path within a file system
MemFSIn memory file system supporting major features of a linux file system (hard links, symbolic links, chroot, umask)
OrefaFSAfero like in memory file system
OsFSOperating system native file system
RoFSRead only file system

Supported methods

File system methods
avfs.VFS
Comments
Absequivalent tofilepath.Abs
Baseequivalent tofilepath.Base
Chdirequivalent toos.Chdir
Chmodequivalent toos.Chmod
Chownequivalent toos.Chown
Chtimesequivalent toos.Chtimes
Cleanequivalent tofilepath.Clean
Createequivalent toos.Create
CreateTempequivalent toos.CreateTemp
Direquivalent tofilepath.Dir
EvalSymlinksequivalent tofilepath.EvalSymlinks
FromSlashequivalent tofilepath.FromSlash
Featuresreturns the set of features provided by the file system or identity manager
Getwdequivalent toos.Getwd
Globequivalent tofilepath.Glob
HasFeaturereturns true if the file system or identity manager provides a given feature
Idmreturns the identity manager of the file system
IsAbsequivalent tofilepath.IsAbs
IsPathSeparatorequivalent tofilepath.IsPathSeparator
Joinequivalent tofilepath.Join
Lchownequivalent toos.Lchown
Linkequivalent toos.Link
Lstatequivalent toos.Lstat
Matchequivalent tofilepath.Match
Mkdirequivalent toos.Mkdir
MkdirAllequivalent toos.MkdirAll
MkdirTempequivalent toos.MkdirTemp
Openequivalent toos.Open
OpenFileequivalent toos.OpenFile
OSTypereturns the operating system type of the file system
PathSeparatorequivalent toos.PathSeparator
ReadDirequivalent toos.ReadDir
ReadFileequivalent toos.ReadFile
Readlinkequivalent toos.Readlink
Relequivalent tofilepath.Rel
Removeequivalent toos.Remove
RemoveAllequivalent toos.RemoveAll
Renameequivalent toos.Rename
SameFileequivalent toos.SameFile
SetUMasksets the file mode creation mask
SetUsersets and returns the current user
Splitequivalent tofilepath.Split
Statequivalent toos.Stat
Subequivalent tofs.Sub
Symlinkequivalent toos.Symlink
TempDirequivalent toos.TempDir
ToSlashequivalent tofilepath.ToSlash
ToSysStattakes a value from fs.FileInfo.Sys() and returns a value that implements interface avfs.SysStater
Truncateequivalent toos.Truncate
UMaskreturns the file mode creation mask
Userreturns the current user
Utilsreturns the file utils of the current file system
WalkDirequivalent tofilepath.WalkDir
WriteFileequivalent toos.WriteFile
File methods
avfs.File
Comments
Chdirequivalent toos.File.Chdir
Chmodequivalent toos.File.Chmod
Chownequivalent toos.File.Chown
Closeequivalent toos.File.Close
Fdequivalent toos.File.Fd
Nameequivalent toos.File.Name
Readequivalent toos.File.Read
ReadAtequivalent toos.File.ReadAt
ReadDirequivalent toos.File.ReadDir
Readdirnamesequivalent toos.File.Readdirnames
Seekequivalent toos.File.Seek
Statequivalent toos.File.Stat
Truncateequivalent toos.File.Truncate
Writeequivalent toos.File.Write
WriteAtequivalent toos.File.WriteAt
WriteStringequivalent toos.File.WriteString

Identity Managers

Identity managers allow users and groups management. The ones implementedinavfs are just here to allow testing of functions related to users (Chown,Lchown) and access rights, so they just allow one default group per user.

All file systems supporting identity manager implement by default the identitymanagerDummyIdmwhere all functions returnsavfs.ErrPermDenied.

Identity ManagerComments
DummyIdmdummy identity manager where all functions are not implemented
MemIdmIn memory identity manager
OsIdmIdentity manager using os functions
SQLiteIdmIdentity manager backed by a SQLite database
Identity Manager methods
avfs.FS
avfs.IdentityMgr
Comments
AdminGroupreturns the administrator group (root for Linux)
AdminUserreturns the administrator user (root for Linux)
GroupAddadds a new group
GroupDeldeletes an existing group
LookupGrouplooks up a group by name
LookupGroupIdlooks up a group by groupid
LookupUserlooks up a user by username
LookupUserIdlooks up a user by userid
UserAddadds a new user
UserDeldeletes an existing user

[8]ページ先頭

©2009-2025 Movatter.jp