Debian Codegraph @

fs.go

raw symbols SCIP
1 package billy
2
3 import (
4 "errors"
5 "io"
6 "io/fs"
7 "time"
8 )
9
10 var (
11 ErrReadOnly = errors.New("read-only filesystem")
12 ErrNotSupported = errors.New("feature not supported")
13 ErrCrossedBoundary = errors.New("chroot boundary crossed")
14 ErrBaseDirCannotBeRemoved = errors.New("base dir cannot be removed")
15 ErrBaseDirCannotBeRenamed = errors.New("base dir cannot be renamed")
16 )
17
18 // Capability holds the supported features of a billy filesystem. This does
19 // not mean that the capability has to be supported by the underlying storage.
20 // For example, a billy filesystem may support WriteCapability but the
21 // storage be mounted in read only mode.
22 type Capability uint64
23
24 const (
25 // WriteCapability means that the fs is writable.
26 WriteCapability Capability = 1 << iota
27 // ReadCapability means that the fs is readable.
28 ReadCapability
29 // ReadAndWriteCapability is the ability to open a file in read and write mode.
30 ReadAndWriteCapability
31 // SeekCapability means it is able to move position inside the file.
32 SeekCapability
33 // TruncateCapability means that a file can be truncated.
34 TruncateCapability
35 // LockCapability is the ability to lock a file.
36 LockCapability
37 // SyncCapability is the ability to synchronize file contents to stable storage.
38 SyncCapability
39
40 // DefaultCapabilities lists all capable features supported by filesystems
41 // without Capability interface. This list should not be changed until a
42 // major version is released.
43 DefaultCapabilities Capability = WriteCapability | ReadCapability |
44 ReadAndWriteCapability | SeekCapability | TruncateCapability |
45 LockCapability
46
47 // AllCapabilities lists all capable features.
48 AllCapabilities Capability = WriteCapability | ReadCapability |
49 ReadAndWriteCapability | SeekCapability | TruncateCapability |
50 LockCapability | SyncCapability
51 )
52
53 // Filesystem abstract the operations in a storage-agnostic interface.
54 // Each method implementation mimics the behavior of the equivalent functions
55 // at the os package from the standard library.
56 type Filesystem interface {
57 Basic
58 TempFile
59 Dir
60 Symlink
61 Chroot
62 }
63
64 // Basic abstract the basic operations in a storage-agnostic interface as
65 // an extension to the Basic interface.
66 type Basic interface {
67 // Create creates the named file with mode 0666 (before umask), truncating
68 // it if it already exists. If successful, methods on the returned File can
69 // be used for I/O; the associated file descriptor has mode O_RDWR.
70 Create(filename string) (File, error)
71 // Open opens the named file for reading. If successful, methods on the
72 // returned file can be used for reading; the associated file descriptor has
73 // mode O_RDONLY.
74 Open(filename string) (File, error)
75 // OpenFile is the generalized open call; most users will use Open or Create
76 // instead. It opens the named file with specified flag (O_RDONLY etc.) and
77 // perm, (0666 etc.) if applicable. If successful, methods on the returned
78 // File can be used for I/O.
79 OpenFile(filename string, flag int, perm fs.FileMode) (File, error)
80 // Stat returns a FileInfo describing the named file.
81 Stat(filename string) (fs.FileInfo, error)
82 // Rename renames (moves) oldpath to newpath. If newpath already exists and
83 // is not a directory, Rename replaces it. OS-specific restrictions may
84 // apply when oldpath and newpath are in different directories.
85 Rename(oldpath, newpath string) error
86 // Remove removes the named file or directory.
87 Remove(filename string) error
88 // Join joins any number of path elements into a single path, adding a
89 // Separator if necessary. Join calls filepath.Clean on the result; in
90 // particular, all empty strings are ignored. On Windows, the result is a
91 // UNC path if and only if the first path element is a UNC path.
92 Join(elem ...string) string
93 }
94
95 type TempFile interface {
96 // TempFile creates a new temporary file in the directory dir with a name
97 // beginning with prefix, opens the file for reading and writing, and
98 // returns the resulting *os.File. If dir is the empty string, TempFile
99 // uses the default directory for temporary files (see os.TempDir).
100 // Multiple programs calling TempFile simultaneously will not choose the
101 // same file. The caller can use f.Name() to find the pathname of the file.
102 // It is the caller's responsibility to remove the file when no longer
103 // needed.
104 TempFile(dir, prefix string) (File, error)
105 }
106
107 // Dir abstract the dir related operations in a storage-agnostic interface as
108 // an extension to the Basic interface.
109 type Dir interface {
110 // ReadDir reads the directory named by dirname and returns a list of
111 // directory entries sorted by filename.
112 ReadDir(path string) ([]fs.DirEntry, error)
113 // MkdirAll creates a directory named path, along with any necessary
114 // parents, and returns nil, or else returns an error. The permission bits
115 // perm are used for all directories that MkdirAll creates. If path is/
116 // already a directory, MkdirAll does nothing and returns nil.
117 MkdirAll(filename string, perm fs.FileMode) error
118 }
119
120 // Symlink abstract the symlink related operations in a storage-agnostic
121 // interface as an extension to the Basic interface.
122 type Symlink interface {
123 // Lstat returns a FileInfo describing the named file. If the file is a
124 // symbolic link, the returned FileInfo describes the symbolic link. Lstat
125 // makes no attempt to follow the link.
126 Lstat(filename string) (fs.FileInfo, error)
127 // Symlink creates a symbolic-link from link to target. target may be an
128 // absolute or relative path, and need not refer to an existing node.
129 // Parent directories of link are created as necessary.
130 Symlink(target, link string) error
131 // Readlink returns the target path of link.
132 Readlink(link string) (string, error)
133 }
134
135 // Chmod abstracts the logic around changing file modes.
136 type Chmod interface {
137 // Chmod changes the mode of the named file to mode. If the file is a
138 // symbolic link, it changes the mode of the link's target.
139 Chmod(name string, mode fs.FileMode) error
140 }
141
142 // Change abstract the FileInfo change related operations in a storage-agnostic
143 // interface as an extension to the Basic interface
144 type Change interface {
145 Chmod
146
147 // Lchown changes the numeric uid and gid of the named file. If the file is
148 // a symbolic link, it changes the uid and gid of the link itself.
149 Lchown(name string, uid, gid int) error
150 // Chown changes the numeric uid and gid of the named file. If the file is a
151 // symbolic link, it changes the uid and gid of the link's target.
152 Chown(name string, uid, gid int) error
153 // Chtimes changes the access and modification times of the named file,
154 // similar to the Unix utime() or utimes() functions.
155 //
156 // The underlying filesystem may truncate or round the values to a less
157 // precise time unit.
158 Chtimes(name string, atime time.Time, mtime time.Time) error
159 }
160
161 // Chroot abstract the chroot related operations in a storage-agnostic interface
162 // as an extension to the Basic interface.
163 type Chroot interface {
164 // Chroot returns a new filesystem from the same type where the new root is
165 // the given path. Files outside of the designated directory tree cannot be
166 // accessed.
167 Chroot(path string) (Filesystem, error)
168 // Root returns the root path of the filesystem.
169 Root() string
170 }
171
172 // File represent a file, being a subset of the os.File
173 type File interface {
174 fs.File
175
176 // Name returns the name of the file as presented to Open.
177 Name() string
178 io.Writer
179 io.WriterAt
180 // ReadAt must be safe for concurrent calls from multiple
181 // goroutines on the same handle, matching the [io.ReaderAt]
182 // contract documented in package io. Callers performing
183 // concurrent random-access reads on a shared File rely on
184 // this; backings that cannot honour it must not satisfy
185 // [billy.File].
186 io.ReaderAt
187 io.Seeker
188 // Truncate the file.
189 Truncate(size int64) error
190 }
191
192 // Locker abstracts the lock and unlock of a File within the filesystem. Not
193 // all filesystem implementations support it.
194 type Locker interface {
195 // Lock locks the file like e.g. flock. It protects against access from
196 // other processes.
197 Lock() error
198 // Unlock unlocks the file.
199 Unlock() error
200 }
201
202 // Syncer interface can be implemented by filesystems that support syncing.
203 type Syncer interface {
204 // Sync commits the current contents of the file to stable storage.
205 Sync() error
206 }
207
208 // Capable interface can return the available features of a filesystem.
209 type Capable interface {
210 // Capabilities returns the capabilities of a filesystem in bit flags.
211 Capabilities() Capability
212 }
213
214 // Capabilities returns the features supported by a filesystem. If the FS
215 // does not implement Capable interface it returns all features.
216 func Capabilities(fs Basic) Capability {
217 capable, ok := fs.(Capable)
218 if !ok {
219 return DefaultCapabilities
220 }
221
222 return capable.Capabilities()
223 }
224
225 // CapabilityCheck tests the filesystem for the provided capabilities and
226 // returns true in case it supports all of them.
227 func CapabilityCheck(fs Basic, capabilities Capability) bool {
228 fsCaps := Capabilities(fs)
229 return fsCaps&capabilities == capabilities
230 }