From 9c0554ef05d4259c2d1cd7979bc4f11e7e79f8aa Mon Sep 17 00:00:00 2001 From: Andrey Beletsky Date: Sun, 8 May 2022 15:24:55 +0700 Subject: [PATCH] Do not use direction package on macos --- store/disk.go | 38 -------------------------- store/diskstore_put_darwin.go | 42 +++++++++++++++++++++++++++++ store/diskstore_put_linux.go | 51 +++++++++++++++++++++++++++++++++++ store/flags_darwin.go | 9 ------- store/flags_linux.go | 10 ------- 5 files changed, 93 insertions(+), 57 deletions(-) create mode 100644 store/diskstore_put_darwin.go create mode 100644 store/diskstore_put_linux.go delete mode 100644 store/flags_darwin.go delete mode 100644 store/flags_linux.go diff --git a/store/disk.go b/store/disk.go index 1640659..30af889 100644 --- a/store/disk.go +++ b/store/disk.go @@ -1,8 +1,6 @@ package store import ( - "bytes" - "io" "io/ioutil" "os" "path" @@ -13,8 +11,6 @@ import ( "github.com/lbryio/lbry.go/v2/extras/errors" "github.com/lbryio/lbry.go/v2/stream" - - "github.com/brk0v/directio" ) // DiskStore stores blobs on a local disk @@ -76,40 +72,6 @@ func (d *DiskStore) Get(hash string) (stream.Blob, shared.BlobTrace, error) { return blob, shared.NewBlobTrace(time.Since(start), d.Name()), nil } -// Put stores the blob on disk -func (d *DiskStore) Put(hash string, blob stream.Blob) error { - err := d.initOnce() - if err != nil { - return err - } - - err = d.ensureDirExists(d.dir(hash)) - if err != nil { - return err - } - - // Open file with O_DIRECT - f, err := os.OpenFile(d.tmpPath(hash), openFileFlags, 0644) - if err != nil { - return errors.Err(err) - } - defer f.Close() - - // Use directio writer - dio, err := directio.New(f) - if err != nil { - return errors.Err(err) - } - defer dio.Flush() - // Write the body to file - _, err = io.Copy(dio, bytes.NewReader(blob)) - if err != nil { - return errors.Err(err) - } - err = os.Rename(d.tmpPath(hash), d.path(hash)) - return errors.Err(err) -} - // PutSD stores the sd blob on the disk func (d *DiskStore) PutSD(hash string, blob stream.Blob) error { return d.Put(hash, blob) diff --git a/store/diskstore_put_darwin.go b/store/diskstore_put_darwin.go new file mode 100644 index 0000000..cbf013d --- /dev/null +++ b/store/diskstore_put_darwin.go @@ -0,0 +1,42 @@ +//go:build darwin +// +build darwin + +package store + +import ( + "bytes" + "io" + "os" + + "github.com/lbryio/lbry.go/v2/extras/errors" + "github.com/lbryio/lbry.go/v2/stream" +) + +var openFileFlags = os.O_WRONLY | os.O_CREATE + +// Put stores the blob on disk +func (d *DiskStore) Put(hash string, blob stream.Blob) error { + err := d.initOnce() + if err != nil { + return err + } + + err = d.ensureDirExists(d.dir(hash)) + if err != nil { + return err + } + + // Open file with O_DIRECT + f, err := os.OpenFile(d.tmpPath(hash), openFileFlags, 0644) + if err != nil { + return errors.Err(err) + } + defer f.Close() + + _, err = io.Copy(f, bytes.NewReader(blob)) + if err != nil { + return errors.Err(err) + } + err = os.Rename(d.tmpPath(hash), d.path(hash)) + return errors.Err(err) +} diff --git a/store/diskstore_put_linux.go b/store/diskstore_put_linux.go new file mode 100644 index 0000000..354794d --- /dev/null +++ b/store/diskstore_put_linux.go @@ -0,0 +1,51 @@ +//go:build linux +// +build linux + +package store + +import ( + "bytes" + "io" + "os" + "syscall" + + "github.com/lbryio/lbry.go/v2/extras/errors" + "github.com/lbryio/lbry.go/v2/stream" + + "github.com/brk0v/directio" +) + +var openFileFlags = os.O_WRONLY | os.O_CREATE | syscall.O_DIRECT + +// Put stores the blob on disk +func (d *DiskStore) Put(hash string, blob stream.Blob) error { + err := d.initOnce() + if err != nil { + return err + } + + err = d.ensureDirExists(d.dir(hash)) + if err != nil { + return err + } + + // Open file with O_DIRECT + f, err := os.OpenFile(d.tmpPath(hash), openFileFlags, 0644) + if err != nil { + return errors.Err(err) + } + defer f.Close() + + dio, err := directio.New(f) + if err != nil { + return errors.Err(err) + } + defer dio.Flush() + + _, err = io.Copy(dio, bytes.NewReader(blob)) + if err != nil { + return errors.Err(err) + } + err = os.Rename(d.tmpPath(hash), d.path(hash)) + return errors.Err(err) +} diff --git a/store/flags_darwin.go b/store/flags_darwin.go deleted file mode 100644 index 273d1b6..0000000 --- a/store/flags_darwin.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build darwin - -package store - -import ( - "os" -) - -var openFileFlags = os.O_WRONLY | os.O_CREATE diff --git a/store/flags_linux.go b/store/flags_linux.go deleted file mode 100644 index 2edaceb..0000000 --- a/store/flags_linux.go +++ /dev/null @@ -1,10 +0,0 @@ -// +build linux - -package store - -import ( - "os" - "syscall" -) - -var openFileFlags = os.O_WRONLY | os.O_CREATE | syscall.O_DIRECT -- 2.45.2