Revert "avoid file.Stat()"

This reverts commit 98c93ca465.
This commit is contained in:
Chris Lu 2021-02-20 12:39:33 -08:00
parent 1ad3200094
commit 7ef4c24f18

View file

@ -1,7 +1,6 @@
package backend
import (
"github.com/chrislusf/seaweedfs/weed/glog"
"os"
"time"
)
@ -13,21 +12,12 @@ var (
type DiskFile struct {
File *os.File
fullFilePath string
fileSize int64
modTime time.Time
}
func NewDiskFile(f *os.File) *DiskFile {
stat, err := f.Stat()
if err != nil {
glog.Fatalf("stat file %s: %v", f.Name(), err)
}
return &DiskFile{
fullFilePath: f.Name(),
File: f,
fileSize: stat.Size(),
modTime: stat.ModTime(),
}
}
@ -36,24 +26,11 @@ func (df *DiskFile) ReadAt(p []byte, off int64) (n int, err error) {
}
func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) {
n, err = df.File.WriteAt(p, off)
if err == nil {
waterMark := off + int64(n)
if waterMark > df.fileSize {
df.fileSize = waterMark
df.modTime = time.Now()
}
}
return
return df.File.WriteAt(p, off)
}
func (df *DiskFile) Truncate(off int64) error {
err := df.File.Truncate(off)
if err == nil {
df.fileSize = off
df.modTime = time.Now()
}
return err
return df.File.Truncate(off)
}
func (df *DiskFile) Close() error {
@ -61,7 +38,11 @@ func (df *DiskFile) Close() error {
}
func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) {
return df.fileSize, df.modTime, nil
stat, e := df.File.Stat()
if e == nil {
return stat.Size(), stat.ModTime(), nil
}
return 0, time.Time{}, err
}
func (df *DiskFile) Name() string {