Path: blob/main/vendor/github.com/spf13/afero/mem/file.go
2880 views
// Copyright © 2015 Steve Francia <[email protected]>.1// Copyright 2013 tsuru authors. All rights reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314package mem1516import (17"bytes"18"errors"19"io"20"io/fs"21"os"22"path/filepath"23"sync"24"sync/atomic"25"time"2627"github.com/spf13/afero/internal/common"28)2930const FilePathSeparator = string(filepath.Separator)3132var _ fs.ReadDirFile = &File{}3334type File struct {35// atomic requires 64-bit alignment for struct field access36at int6437readDirCount int6438closed bool39readOnly bool40fileData *FileData41}4243func NewFileHandle(data *FileData) *File {44return &File{fileData: data}45}4647func NewReadOnlyFileHandle(data *FileData) *File {48return &File{fileData: data, readOnly: true}49}5051func (f File) Data() *FileData {52return f.fileData53}5455type FileData struct {56sync.Mutex57name string58data []byte59memDir Dir60dir bool61mode os.FileMode62modtime time.Time63uid int64gid int65}6667func (d *FileData) Name() string {68d.Lock()69defer d.Unlock()70return d.name71}7273func CreateFile(name string) *FileData {74return &FileData{name: name, mode: os.ModeTemporary, modtime: time.Now()}75}7677func CreateDir(name string) *FileData {78return &FileData{name: name, memDir: &DirMap{}, dir: true, modtime: time.Now()}79}8081func ChangeFileName(f *FileData, newname string) {82f.Lock()83f.name = newname84f.Unlock()85}8687func SetMode(f *FileData, mode os.FileMode) {88f.Lock()89f.mode = mode90f.Unlock()91}9293func SetModTime(f *FileData, mtime time.Time) {94f.Lock()95setModTime(f, mtime)96f.Unlock()97}9899func setModTime(f *FileData, mtime time.Time) {100f.modtime = mtime101}102103func SetUID(f *FileData, uid int) {104f.Lock()105f.uid = uid106f.Unlock()107}108109func SetGID(f *FileData, gid int) {110f.Lock()111f.gid = gid112f.Unlock()113}114115func GetFileInfo(f *FileData) *FileInfo {116return &FileInfo{f}117}118119func (f *File) Open() error {120atomic.StoreInt64(&f.at, 0)121atomic.StoreInt64(&f.readDirCount, 0)122f.fileData.Lock()123f.closed = false124f.fileData.Unlock()125return nil126}127128func (f *File) Close() error {129f.fileData.Lock()130f.closed = true131if !f.readOnly {132setModTime(f.fileData, time.Now())133}134f.fileData.Unlock()135return nil136}137138func (f *File) Name() string {139return f.fileData.Name()140}141142func (f *File) Stat() (os.FileInfo, error) {143return &FileInfo{f.fileData}, nil144}145146func (f *File) Sync() error {147return nil148}149150func (f *File) Readdir(count int) (res []os.FileInfo, err error) {151if !f.fileData.dir {152return nil, &os.PathError{153Op: "readdir",154Path: f.fileData.name,155Err: errors.New("not a dir"),156}157}158var outLength int64159160f.fileData.Lock()161files := f.fileData.memDir.Files()[f.readDirCount:]162if count > 0 {163if len(files) < count {164outLength = int64(len(files))165} else {166outLength = int64(count)167}168if len(files) == 0 {169err = io.EOF170}171} else {172outLength = int64(len(files))173}174f.readDirCount += outLength175f.fileData.Unlock()176177res = make([]os.FileInfo, outLength)178for i := range res {179res[i] = &FileInfo{files[i]}180}181182return res, err183}184185func (f *File) Readdirnames(n int) (names []string, err error) {186fi, err := f.Readdir(n)187names = make([]string, len(fi))188for i, f := range fi {189_, names[i] = filepath.Split(f.Name())190}191return names, err192}193194// Implements fs.ReadDirFile195func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {196fi, err := f.Readdir(n)197if err != nil {198return nil, err199}200di := make([]fs.DirEntry, len(fi))201for i, f := range fi {202di[i] = common.FileInfoDirEntry{FileInfo: f}203}204return di, nil205}206207func (f *File) Read(b []byte) (n int, err error) {208f.fileData.Lock()209defer f.fileData.Unlock()210if f.closed {211return 0, ErrFileClosed212}213if len(b) > 0 && int(f.at) == len(f.fileData.data) {214return 0, io.EOF215}216if int(f.at) > len(f.fileData.data) {217return 0, io.ErrUnexpectedEOF218}219if len(f.fileData.data)-int(f.at) >= len(b) {220n = len(b)221} else {222n = len(f.fileData.data) - int(f.at)223}224copy(b, f.fileData.data[f.at:f.at+int64(n)])225atomic.AddInt64(&f.at, int64(n))226return227}228229func (f *File) ReadAt(b []byte, off int64) (n int, err error) {230prev := atomic.LoadInt64(&f.at)231atomic.StoreInt64(&f.at, off)232n, err = f.Read(b)233atomic.StoreInt64(&f.at, prev)234return235}236237func (f *File) Truncate(size int64) error {238if f.closed {239return ErrFileClosed240}241if f.readOnly {242return &os.PathError{243Op: "truncate",244Path: f.fileData.name,245Err: errors.New("file handle is read only"),246}247}248if size < 0 {249return ErrOutOfRange250}251f.fileData.Lock()252defer f.fileData.Unlock()253if size > int64(len(f.fileData.data)) {254diff := size - int64(len(f.fileData.data))255f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{0o0}, int(diff))...)256} else {257f.fileData.data = f.fileData.data[0:size]258}259setModTime(f.fileData, time.Now())260return nil261}262263func (f *File) Seek(offset int64, whence int) (int64, error) {264if f.closed {265return 0, ErrFileClosed266}267switch whence {268case io.SeekStart:269atomic.StoreInt64(&f.at, offset)270case io.SeekCurrent:271atomic.AddInt64(&f.at, offset)272case io.SeekEnd:273atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)274}275return f.at, nil276}277278func (f *File) Write(b []byte) (n int, err error) {279if f.closed {280return 0, ErrFileClosed281}282if f.readOnly {283return 0, &os.PathError{284Op: "write",285Path: f.fileData.name,286Err: errors.New("file handle is read only"),287}288}289n = len(b)290cur := atomic.LoadInt64(&f.at)291f.fileData.Lock()292defer f.fileData.Unlock()293diff := cur - int64(len(f.fileData.data))294var tail []byte295if n+int(cur) < len(f.fileData.data) {296tail = f.fileData.data[n+int(cur):]297}298if diff > 0 {299f.fileData.data = append(300f.fileData.data,301append(bytes.Repeat([]byte{0o0}, int(diff)), b...)...)302f.fileData.data = append(f.fileData.data, tail...)303} else {304f.fileData.data = append(f.fileData.data[:cur], b...)305f.fileData.data = append(f.fileData.data, tail...)306}307setModTime(f.fileData, time.Now())308309atomic.AddInt64(&f.at, int64(n))310return311}312313func (f *File) WriteAt(b []byte, off int64) (n int, err error) {314atomic.StoreInt64(&f.at, off)315return f.Write(b)316}317318func (f *File) WriteString(s string) (ret int, err error) {319return f.Write([]byte(s))320}321322func (f *File) Info() *FileInfo {323return &FileInfo{f.fileData}324}325326type FileInfo struct {327*FileData328}329330// Implements os.FileInfo331func (s *FileInfo) Name() string {332s.Lock()333_, name := filepath.Split(s.name)334s.Unlock()335return name336}337338func (s *FileInfo) Mode() os.FileMode {339s.Lock()340defer s.Unlock()341return s.mode342}343344func (s *FileInfo) ModTime() time.Time {345s.Lock()346defer s.Unlock()347return s.modtime348}349350func (s *FileInfo) IsDir() bool {351s.Lock()352defer s.Unlock()353return s.dir354}355func (s *FileInfo) Sys() interface{} { return nil }356func (s *FileInfo) Size() int64 {357if s.IsDir() {358return int64(42)359}360s.Lock()361defer s.Unlock()362return int64(len(s.data))363}364365var (366ErrFileClosed = errors.New("File is closed")367ErrOutOfRange = errors.New("out of range")368ErrTooLarge = errors.New("too large")369ErrFileNotFound = os.ErrNotExist370ErrFileExists = os.ErrExist371ErrDestinationExists = os.ErrExist372)373374375