2019-10-29 07:35:16 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2022-08-10 15:21:57 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2022-08-15 22:35:31 +00:00
|
|
|
"os"
|
2022-10-10 14:28:02 +00:00
|
|
|
"runtime"
|
2022-08-15 22:35:31 +00:00
|
|
|
"time"
|
2019-10-29 07:35:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-29 02:33:18 +00:00
|
|
|
_ BackendStorageFile = &DiskFile{}
|
2019-10-29 07:35:16 +00:00
|
|
|
)
|
|
|
|
|
2022-10-10 14:28:02 +00:00
|
|
|
const isMac = runtime.GOOS == "darwin"
|
|
|
|
|
2019-10-29 07:35:16 +00:00
|
|
|
type DiskFile struct {
|
|
|
|
File *os.File
|
|
|
|
fullFilePath string
|
2021-02-20 20:42:09 +00:00
|
|
|
fileSize int64
|
|
|
|
modTime time.Time
|
2019-10-29 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 05:37:36 +00:00
|
|
|
func NewDiskFile(f *os.File) *DiskFile {
|
2021-02-20 20:42:09 +00:00
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
glog.Fatalf("stat file %s: %v", f.Name(), err)
|
|
|
|
}
|
2021-02-21 04:06:06 +00:00
|
|
|
offset := stat.Size()
|
|
|
|
if offset%NeedlePaddingSize != 0 {
|
|
|
|
offset = offset + (NeedlePaddingSize - offset%NeedlePaddingSize)
|
|
|
|
}
|
2021-02-20 20:42:09 +00:00
|
|
|
|
2019-10-29 07:35:16 +00:00
|
|
|
return &DiskFile{
|
2019-10-30 05:37:36 +00:00
|
|
|
fullFilePath: f.Name(),
|
2019-10-29 07:35:16 +00:00
|
|
|
File: f,
|
2021-02-21 04:06:06 +00:00
|
|
|
fileSize: offset,
|
2021-02-20 20:42:09 +00:00
|
|
|
modTime: stat.ModTime(),
|
2019-10-29 07:35:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) ReadAt(p []byte, off int64) (n int, err error) {
|
2023-06-03 00:16:45 +00:00
|
|
|
if df.File == nil {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2019-10-29 07:35:16 +00:00
|
|
|
return df.File.ReadAt(p, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) WriteAt(p []byte, off int64) (n int, err error) {
|
2023-06-03 00:16:45 +00:00
|
|
|
if df.File == nil {
|
|
|
|
return 0, os.ErrClosed
|
|
|
|
}
|
2021-02-20 20:42:09 +00: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 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 10:29:38 +00:00
|
|
|
func (df *DiskFile) Write(p []byte) (n int, err error) {
|
2021-02-21 04:06:06 +00:00
|
|
|
return df.WriteAt(p, df.fileSize)
|
|
|
|
}
|
|
|
|
|
2019-10-29 07:35:16 +00:00
|
|
|
func (df *DiskFile) Truncate(off int64) error {
|
2023-06-03 00:16:45 +00:00
|
|
|
if df.File == nil {
|
|
|
|
return os.ErrClosed
|
|
|
|
}
|
2021-02-20 20:42:09 +00:00
|
|
|
err := df.File.Truncate(off)
|
|
|
|
if err == nil {
|
|
|
|
df.fileSize = off
|
|
|
|
df.modTime = time.Now()
|
|
|
|
}
|
|
|
|
return err
|
2019-10-29 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) Close() error {
|
2023-06-03 00:16:45 +00:00
|
|
|
if df.File == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-13 07:51:20 +00:00
|
|
|
if err := df.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-03 00:16:45 +00:00
|
|
|
if err := df.File.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
df.File = nil
|
|
|
|
return nil
|
2019-10-29 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (df *DiskFile) GetStat() (datSize int64, modTime time.Time, err error) {
|
2022-03-10 13:58:56 +00:00
|
|
|
if df.File == nil {
|
2023-06-03 00:16:45 +00:00
|
|
|
err = os.ErrClosed
|
2022-03-10 13:58:56 +00:00
|
|
|
}
|
|
|
|
return df.fileSize, df.modTime, err
|
2019-10-29 07:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 03:44:16 +00:00
|
|
|
func (df *DiskFile) Name() string {
|
2019-10-29 07:35:16 +00:00
|
|
|
return df.fullFilePath
|
|
|
|
}
|
2022-08-15 22:35:31 +00:00
|
|
|
|
|
|
|
func (df *DiskFile) Sync() error {
|
2022-10-10 14:28:02 +00:00
|
|
|
if df.File == nil {
|
2023-06-03 00:16:45 +00:00
|
|
|
return os.ErrClosed
|
2022-10-10 14:28:02 +00:00
|
|
|
}
|
|
|
|
if isMac {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return df.File.Sync()
|
2022-08-15 22:35:31 +00:00
|
|
|
}
|