github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs/mountfs/mountfs_file.go (about)

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