seaweedfs/weed/storage/backend/disk_file.go

86 lines
1.6 KiB
Go
Raw Normal View History

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