github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/fs/file.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package fsutils
     4  
     5  import "os"
     6  
     7  const FlagRead = 0x1
     8  const FlagWrite = 0x2
     9  
    10  type File struct {
    11  	rawFile  *os.File
    12  	readonly bool
    13  }
    14  
    15  func NewFile(rawFile *os.File, flags int) *File {
    16  	return &File{
    17  		rawFile:  rawFile,
    18  		readonly: flags&FlagRead == FlagRead,
    19  	}
    20  }
    21  
    22  func (this *File) Name() string {
    23  	return this.rawFile.Name()
    24  }
    25  
    26  func (this *File) Fd() uintptr {
    27  	return this.rawFile.Fd()
    28  }
    29  
    30  func (this *File) Raw() *os.File {
    31  	return this.rawFile
    32  }
    33  
    34  func (this *File) Stat() (os.FileInfo, error) {
    35  	return this.rawFile.Stat()
    36  }
    37  
    38  func (this *File) Seek(offset int64, whence int) (ret int64, err error) {
    39  	ret, err = this.rawFile.Seek(offset, whence)
    40  	return
    41  }
    42  
    43  func (this *File) Read(b []byte) (n int, err error) {
    44  	ReaderLimiter.Ack()
    45  	n, err = this.rawFile.Read(b)
    46  	ReaderLimiter.Release()
    47  	return
    48  }
    49  
    50  func (this *File) ReadAt(b []byte, off int64) (n int, err error) {
    51  	ReaderLimiter.Ack()
    52  	n, err = this.rawFile.ReadAt(b, off)
    53  	ReaderLimiter.Release()
    54  	return
    55  }
    56  
    57  func (this *File) Write(b []byte) (n int, err error) {
    58  	WriterLimiter.Ack()
    59  	n, err = this.rawFile.Write(b)
    60  	WriterLimiter.Release()
    61  	return
    62  }
    63  
    64  func (this *File) WriteAt(b []byte, off int64) (n int, err error) {
    65  	WriterLimiter.Ack()
    66  	n, err = this.rawFile.WriteAt(b, off)
    67  	WriterLimiter.Release()
    68  	return
    69  }
    70  
    71  func (this *File) Sync() (err error) {
    72  	WriterLimiter.Ack()
    73  	err = this.rawFile.Sync()
    74  	WriterLimiter.Release()
    75  	return
    76  }
    77  
    78  func (this *File) Truncate(size int64) (err error) {
    79  	WriterLimiter.Ack()
    80  	err = this.rawFile.Truncate(size)
    81  	WriterLimiter.Release()
    82  	return
    83  }
    84  
    85  func (this *File) Close() (err error) {
    86  	if !this.readonly {
    87  		WriterLimiter.Ack()
    88  	}
    89  	err = this.rawFile.Close()
    90  	if !this.readonly {
    91  		WriterLimiter.Release()
    92  	}
    93  	return
    94  }