vendor/github.com/spf13/afero/mem/file.go
changeset 256 6d9efbef00a9
parent 242 2a9ec03fe5a1
child 260 445e01aede7e
--- a/vendor/github.com/spf13/afero/mem/file.go	Mon Jun 07 20:58:18 2021 +0200
+++ b/vendor/github.com/spf13/afero/mem/file.go	Sun Jul 11 10:35:56 2021 +0200
@@ -22,10 +22,9 @@
 	"path/filepath"
 	"sync"
 	"sync/atomic"
+	"time"
 )
 
-import "time"
-
 const FilePathSeparator = string(filepath.Separator)
 
 type File struct {
@@ -57,6 +56,8 @@
 	dir     bool
 	mode    os.FileMode
 	modtime time.Time
+	uid     int
+	gid     int
 }
 
 func (d *FileData) Name() string {
@@ -95,6 +96,18 @@
 	f.modtime = mtime
 }
 
+func SetUID(f *FileData, uid int) {
+	f.Lock()
+	f.uid = uid
+	f.Unlock()
+}
+
+func SetGID(f *FileData, gid int) {
+	f.Lock()
+	f.gid = gid
+	f.Unlock()
+}
+
 func GetFileInfo(f *FileData) *FileInfo {
 	return &FileInfo{f}
 }
@@ -193,8 +206,11 @@
 }
 
 func (f *File) ReadAt(b []byte, off int64) (n int, err error) {
+	prev := atomic.LoadInt64(&f.at)
 	atomic.StoreInt64(&f.at, off)
-	return f.Read(b)
+	n, err = f.Read(b)
+	atomic.StoreInt64(&f.at, prev)
+	return
 }
 
 func (f *File) Truncate(size int64) error {
@@ -207,6 +223,8 @@
 	if size < 0 {
 		return ErrOutOfRange
 	}
+	f.fileData.Lock()
+	defer f.fileData.Unlock()
 	if size > int64(len(f.fileData.data)) {
 		diff := size - int64(len(f.fileData.data))
 		f.fileData.data = append(f.fileData.data, bytes.Repeat([]byte{00}, int(diff))...)
@@ -222,17 +240,20 @@
 		return 0, ErrFileClosed
 	}
 	switch whence {
-	case 0:
+	case io.SeekStart:
 		atomic.StoreInt64(&f.at, offset)
-	case 1:
-		atomic.AddInt64(&f.at, int64(offset))
-	case 2:
+	case io.SeekCurrent:
+		atomic.AddInt64(&f.at, offset)
+	case io.SeekEnd:
 		atomic.StoreInt64(&f.at, int64(len(f.fileData.data))+offset)
 	}
 	return f.at, nil
 }
 
 func (f *File) Write(b []byte) (n int, err error) {
+	if f.closed == true {
+		return 0, ErrFileClosed
+	}
 	if f.readOnly {
 		return 0, &os.PathError{Op: "write", Path: f.fileData.name, Err: errors.New("file handle is read only")}
 	}
@@ -246,7 +267,7 @@
 		tail = f.fileData.data[n+int(cur):]
 	}
 	if diff > 0 {
-		f.fileData.data = append(bytes.Repeat([]byte{00}, int(diff)), b...)
+		f.fileData.data = append(f.fileData.data, append(bytes.Repeat([]byte{00}, int(diff)), b...)...)
 		f.fileData.data = append(f.fileData.data, tail...)
 	} else {
 		f.fileData.data = append(f.fileData.data[:cur], b...)
@@ -254,7 +275,7 @@
 	}
 	setModTime(f.fileData, time.Now())
 
-	atomic.StoreInt64(&f.at, int64(len(f.fileData.data)))
+	atomic.AddInt64(&f.at, int64(n))
 	return
 }