github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/memfs/memfs_types.go (about) 1 // 2 // Copyright 2020 The AVFS authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 package memfs 18 19 import ( 20 "io/fs" 21 "sync" 22 "time" 23 24 "github.com/avfs/avfs" 25 ) 26 27 const ( 28 // Maximum number of symlinks in a path. 29 slCountMax = 64 30 ) 31 32 // MemIOFS implements a memory file system using the avfs.IOFS interface. 33 type MemIOFS struct { 34 MemFS 35 } 36 37 // MemFS implements a memory file system using the avfs.VFS interface. 38 type MemFS struct { 39 rootNode *dirNode // rootNode represent the root directory of the file system. 40 err avfs.Errors // err regroups errors depending on the OS emulated. 41 volumes volumes // volumes contains the volume names (for Windows only). 42 dirMode fs.FileMode // dirMode is the default fs.FileMode for a directory. 43 fileMode fs.FileMode // fileMode is de default fs.FileMode for a file. 44 lastId *uint64 // lastId is the last unique id used to identify files uniquely. 45 name string // name is the name of the file system. 46 avfs.CurDirFn // CurDirFn provides current directory functions to a file system. 47 avfs.CurUserFn // CurUserFn provides current user functions to a file system. 48 avfs.IdmFn // IdmFn provides identity manager functions to a file system. 49 avfs.UMaskFn // UMaskFn provides UMask functions to file systems. 50 avfs.FeaturesFn // FeaturesFn provides features functions to a file system or an identity manager. 51 avfs.OSTypeFn // OSTypeFn provides OS type functions to a file system or an identity manager. 52 } 53 54 // MemFile represents an open file descriptor. 55 type MemFile struct { 56 nd node // nd is node of the file. 57 vfs *MemFS // vfs is the memory file system of the file. 58 name string // name is the name of the file. 59 dirEntries []fs.DirEntry // dirEntries stores the file information returned by ReadDir function. 60 dirNames []string // dirNames stores the names of the file returned by Readdirnames function. 61 at int64 // at is current position in the file used by Read and Write functions. 62 dirIndex int // dirIndex is the position of the current index for dirEntries ou dirNames slices. 63 mu sync.RWMutex // mu is the RWMutex used to access content of MemFile. 64 openMode avfs.OpenMode // openMode defines the permissions to check for OpenFile and CheckPermission functions. 65 } 66 67 // Options defines the initialization options of MemFS. 68 type Options struct { 69 Idm avfs.IdentityMgr // Idm is the identity manager of the file system. 70 User avfs.UserReader // User is the current user of the file system. 71 Name string // Name is the name of the file system. 72 OSType avfs.OSType // OSType defines the operating system type. 73 SystemDirs []avfs.DirInfo // SystemDirs contains data to create system directories. 74 } 75 76 // node is the interface implemented by dirNode, fileNode and symlinkNode. 77 type node interface { 78 sync.Locker 79 80 // checkPermission returns true if the current user has the desired permissions (perm) on the node. 81 checkPermission(perm avfs.OpenMode, u avfs.UserReader) bool 82 83 // delete removes all information from the node. 84 delete() 85 86 // fillStatFrom returns a *MemInfo (implementation of fs.FileInfo) from a node named name. 87 fillStatFrom(name string) *MemInfo 88 89 // setMode sets the permissions of the node. 90 setMode(mode fs.FileMode, u avfs.UserReader) bool 91 92 // setModTime sets the modification time of the node. 93 setModTime(mtime time.Time, u avfs.UserReader) bool 94 95 // setOwner sets the owner of the node. 96 setOwner(uid, gid int) 97 98 // size returns the size of the node. 99 size() int64 100 } 101 102 // volumes are the volumes names for Windows. 103 type volumes map[string]*dirNode 104 105 // dirNode is the structure for a directory. 106 type dirNode struct { 107 children children // children are the nodes present in the directory. 108 baseNode // baseNode is the common structure of directories, files and symbolic links. 109 } 110 111 // children are the children of a directory. 112 type children = map[string]node 113 114 // fileNode is the structure for a file. 115 type fileNode struct { 116 data []byte // data is the file content. 117 baseNode // baseNode is the common structure of directories, files and symbolic links. 118 id uint64 // id is a unique id to identify a file (used by SameFile function). 119 nlink int // nlink is the number of hardlinks to this fileNode. 120 } 121 122 // symlinkNode is the structure for a symbolic link. 123 type symlinkNode struct { 124 link string // link is the symbolic link value. 125 baseNode // baseNode is the common structure of directories, files and symbolic links. 126 } 127 128 // baseNode is the common structure of directories, files and symbolic links. 129 type baseNode struct { 130 mu sync.RWMutex // mu is the RWMutex used to access the content of the node. 131 mtime int64 // mtime is the modification time. 132 mode fs.FileMode // mode represents a file's mode and permission bits. 133 uid int // uid is the user id. 134 gid int // gid is the group id. 135 } 136 137 // slMode defines the behavior of searchNode function relatively to symlinks. 138 type slMode int 139 140 const ( 141 slmLstat slMode = iota + 1 // slmLstat makes searchNode function follow symbolic links like Lstat. 142 slmStat // slmStat makes searchNode function follow symbolic links like Stat. 143 slmEval // slmEval makes searchNode function follow symbolic links like EvalSymlink. 144 ) 145 146 // MemInfo is the implementation of fs.DirEntry (returned by ReadDir) and fs.FileInfo (returned by Stat and Lstat). 147 type MemInfo struct { 148 name string // name is the name of the file. 149 id uint64 // id is a unique id to identify a file (used by SameFile function). 150 size int64 // size is the size of the file. 151 mtime int64 // mtime is the modification time. 152 uid int // uid is the user id. 153 gid int // gid is the group id. 154 nlink int // nlink is the number of hardlinks to this fileNode. 155 mode fs.FileMode // mode represents a file's mode and permission bits. 156 }