2019-10-29 15:35:16 +08:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2021-02-21 04:42:09 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2019-10-29 15:35:16 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-29 10:33:18 +08:00
|
|
|
_ BackendStorageFile = &DiskFile{}
|
2019-10-29 15:35:16 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DiskFile struct {
|
|
|
|
File *os.File
|
|
|
|
fullFilePath string
|
2021-02-21 04:42:09 +08:00
|
|
|
fileSize int64
|
|
|
|
modTime time.Time
|
2019-10-29 15:35:16 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:37:36 +08:00
|
|
|
func NewDiskFile(f *os.File) *DiskFile {
|
2021-02-21 04:42:09 +08:00
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("stat file %s: %v", f.Name(), err)
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:35:16 +08:00
|
|
|
return &DiskFile{
|
2019-10-30 13:37:36 +08:00
|
|
|
fullFilePath: f.Name(),
|
2019-10-29 15:35:16 +08:00
|
|
|
File: f,
|
2021-02-21 04:42:09 +08:00
|
|
|
fileSize: stat.Size(),
|
|
|
|
modTime: stat.ModTime(),
|
2019-10-29 15:35:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
return df.File.ReadAt(p, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) {
|
2021-02-21 04:42:09 +08:00
|
|
|
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
|
2019-10-29 15:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) Truncate(off int64) error {
|
2021-02-21 04:42:09 +08:00
|
|
|
err := df.File.Truncate(off)
|
|
|
|
if err == nil {
|
|
|
|
df.fileSize = off
|
|
|
|
df.modTime = time.Now()
|
|
|
|
}
|
|
|
|
return err
|
2019-10-29 15:35:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) Close() error {
|
|
|
|
return df.File.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) {
|
2021-02-21 04:42:09 +08:00
|
|
|
return df.fileSize, df.modTime, nil
|
2019-10-29 15:35:16 +08:00
|
|
|
}
|
|
|
|
|
2019-12-09 11:44:16 +08:00
|
|
|
func (df *DiskFile) Name() string {
|
2019-10-29 15:35:16 +08:00
|
|
|
return df.fullFilePath
|
|
|
|
}
|
2020-03-20 14:54:52 +08:00
|
|
|
|
|
|
|
func (df *DiskFile) Sync() error {
|
|
|
|
return df.File.Sync()
|
|
|
|
}
|