github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/basepathfs/basepathfs_file.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 basepathfs 18 19 import ( 20 "io/fs" 21 ) 22 23 // Chdir changes the current working directory to the file, 24 // which must be a directory. 25 // If there is an error, it will be of type *PathError. 26 func (f *BasePathFile) Chdir() error { 27 err := f.baseFile.Chdir() 28 29 return f.vfs.restoreError(err) 30 } 31 32 // Chmod changes the mode of the file to mode. 33 // If there is an error, it will be of type *PathError. 34 func (f *BasePathFile) Chmod(mode fs.FileMode) error { 35 err := f.baseFile.Chmod(mode) 36 37 return f.vfs.restoreError(err) 38 } 39 40 // Chown changes the numeric uid and gid of the named file. 41 // If there is an error, it will be of type *PathError. 42 // 43 // On Windows, it always returns the syscall.EWINDOWS error, wrapped 44 // in *PathError. 45 func (f *BasePathFile) Chown(uid, gid int) error { 46 err := f.baseFile.Chown(uid, gid) 47 48 return f.vfs.restoreError(err) 49 } 50 51 // Close closes the File, rendering it unusable for I/O. 52 // On files that support SetDeadline, any pending I/O operations will 53 // be canceled and return immediately with an error. 54 func (f *BasePathFile) Close() error { 55 err := f.baseFile.Close() 56 57 return f.vfs.restoreError(err) 58 } 59 60 // Fd returns the integer Unix file descriptor referencing the open file. 61 // The file descriptor is valid only until f.Close is called or f is garbage collected. 62 // On Unix systems this will cause the SetDeadline methods to stop working. 63 func (f *BasePathFile) Fd() uintptr { 64 return f.baseFile.Fd() 65 } 66 67 // Name returns the link of the file as presented to Open. 68 func (f *BasePathFile) Name() string { 69 return f.vfs.fromBasePath(f.baseFile.Name()) 70 } 71 72 // Read reads up to len(b) bytes from the MemFile. 73 // It returns the number of bytes read and any error encountered. 74 // At end of file, Read returns 0, io.EOF. 75 func (f *BasePathFile) Read(b []byte) (n int, err error) { 76 n, err = f.baseFile.Read(b) 77 78 return n, f.vfs.restoreError(err) 79 } 80 81 // ReadAt reads len(b) bytes from the File starting at byte offset off. 82 // It returns the number of bytes read and the error, if any. 83 // ReadAt always returns a non-nil error when n < len(b). 84 // At end of file, that error is io.EOF. 85 func (f *BasePathFile) ReadAt(b []byte, off int64) (n int, err error) { 86 n, err = f.baseFile.ReadAt(b, off) 87 88 return n, f.vfs.restoreError(err) 89 } 90 91 // ReadDir reads the contents of the directory associated with the file f 92 // and returns a slice of DirEntry values in directory order. 93 // Subsequent calls on the same file will yield later DirEntry records in the directory. 94 // 95 // If n > 0, ReadDir returns at most n DirEntry records. 96 // In this case, if ReadDir returns an empty slice, it will return an error explaining why. 97 // At the end of a directory, the error is io.EOF. 98 // 99 // If n <= 0, ReadDir returns all the DirEntry records remaining in the directory. 100 // When it succeeds, it returns a nil error (not io.EOF). 101 func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error) { 102 de, err := f.baseFile.ReadDir(n) 103 104 return de, f.vfs.restoreError(err) 105 } 106 107 // Readdirnames reads and returns a slice of names from the directory f. 108 // 109 // If n > 0, Readdirnames returns at most n names. In this case, if 110 // Readdirnames returns an empty slice, it will return a non-nil error 111 // explaining why. At the end of a directory, the error is io.EOF. 112 // 113 // If n <= 0, Readdirnames returns all the names from the directory in 114 // a single slice. In this case, if Readdirnames succeeds (reads all 115 // the way to the end of the directory), it returns the slice and a 116 // nil error. If it encounters an error before the end of the 117 // directory, Readdirnames returns the names read until that point and 118 // a non-nil error. 119 func (f *BasePathFile) Readdirnames(n int) (names []string, err error) { 120 names, err = f.baseFile.Readdirnames(n) 121 122 return names, f.vfs.restoreError(err) 123 } 124 125 // Seek sets the offset for the next Read or Write on file to offset, interpreted 126 // according to whence: 0 means relative to the origin of the file, 1 means 127 // relative to the current offset, and 2 means relative to the end. 128 // It returns the new offset and an error, if any. 129 // The behavior of Seek on a file opened with O_APPEND is not specified. 130 func (f *BasePathFile) Seek(offset int64, whence int) (ret int64, err error) { 131 ret, err = f.baseFile.Seek(offset, whence) 132 133 return ret, f.vfs.restoreError(err) 134 } 135 136 // Stat returns the FileInfo structure describing file. 137 // If there is an error, it will be of type *PathError. 138 func (f *BasePathFile) Stat() (fs.FileInfo, error) { 139 info, err := f.baseFile.Stat() 140 141 return info, f.vfs.restoreError(err) 142 } 143 144 // Sync commits the current contents of the file to stable storage. 145 // Typically, this means flushing the file system's in-memory copy 146 // of recently written data to disk. 147 func (f *BasePathFile) Sync() error { 148 err := f.baseFile.Sync() 149 150 return f.vfs.restoreError(err) 151 } 152 153 // Truncate changes the size of the file. 154 // It does not change the I/O offset. 155 // If there is an error, it will be of type *PathError. 156 func (f *BasePathFile) Truncate(size int64) error { 157 err := f.baseFile.Truncate(size) 158 159 return f.vfs.restoreError(err) 160 } 161 162 // Write writes len(b) bytes to the File. 163 // It returns the number of bytes written and an error, if any. 164 // Write returns a non-nil error when n != len(b). 165 func (f *BasePathFile) Write(b []byte) (n int, err error) { 166 n, err = f.baseFile.Write(b) 167 168 return n, f.vfs.restoreError(err) 169 } 170 171 // WriteAt writes len(b) bytes to the File starting at byte offset off. 172 // It returns the number of bytes written and an error, if any. 173 // WriteAt returns a non-nil error when n != len(b). 174 func (f *BasePathFile) WriteAt(b []byte, off int64) (n int, err error) { 175 n, err = f.baseFile.WriteAt(b, off) 176 177 return n, f.vfs.restoreError(err) 178 } 179 180 // WriteString is like Write, but writes the contents of string s rather than 181 // a slice of bytes. 182 func (f *BasePathFile) WriteString(s string) (n int, err error) { 183 return f.Write([]byte(s)) 184 }