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