From 1cfc4fae418a5512ce86e604229b543551aee84c Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sat, 4 Mar 2023 17:19:30 +0800 Subject: [PATCH] Add Create File --- file/FileWR.go | 62 ++++++++++++++++++++++++--------------------- file/FileWR_test.go | 21 ++++++++++++--- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/file/FileWR.go b/file/FileWR.go index 15c2cf8..f60340c 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -25,7 +25,7 @@ type File struct { wr io.Writer rr io.Reader cu int64 - sync.RWMutex + l sync.RWMutex } type Config struct { @@ -54,10 +54,10 @@ func (t *File) CopyTo(to *File, byteInSec int64, tryLock bool) error { defer t.Close() } - if !t.TryRLock() { + if !t.l.TryRLock() { return ErrFailToLock } - defer t.RUnlock() + defer t.l.RUnlock() to.getRWCloser() if t.Config.AutoClose { @@ -65,13 +65,13 @@ func (t *File) CopyTo(to *File, byteInSec int64, tryLock bool) error { } if tryLock { - if !to.TryLock() { + if !to.l.TryLock() { return ErrFailToLock } } else { - to.Lock() + to.l.Lock() } - defer to.Unlock() + defer to.l.Unlock() return transferIO(t.read(), to.write(), byteInSec) } @@ -82,10 +82,10 @@ func (t *File) CopyToIoWriter(to io.Writer, byteInSec int64, tryLock bool) error defer t.Close() } - if !t.TryRLock() { + if !t.l.TryRLock() { return ErrFailToLock } - defer t.RUnlock() + defer t.l.RUnlock() return transferIO(t.read(), to, byteInSec) } @@ -97,13 +97,13 @@ func (t *File) Write(data []byte, tryLock bool) (int, error) { } if tryLock { - if !t.TryLock() { + if !t.l.TryLock() { return 0, ErrFailToLock } } else { - t.Lock() + t.l.Lock() } - defer t.Unlock() + defer t.l.Unlock() return t.write().Write(data) } @@ -114,10 +114,10 @@ func (t *File) Read(data []byte) (int, error) { defer t.Close() } - if !t.TryRLock() { + if !t.l.TryRLock() { return 0, ErrFailToLock } - defer t.RUnlock() + defer t.l.RUnlock() return t.read().Read(data) } @@ -128,10 +128,10 @@ func (t *File) ReadUntil(separation byte, perReadSize int, maxReadSize int) (dat defer t.Close() } - if !t.TryRLock() { + if !t.l.TryRLock() { return nil, ErrFailToLock } - defer t.RUnlock() + defer t.l.RUnlock() var ( tmpArea = make([]byte, perReadSize) @@ -174,10 +174,10 @@ func (t *File) ReadAll(perReadSize int, maxReadSize int) (data []byte, e error) defer t.Close() } - if !t.TryRLock() { + if !t.l.TryRLock() { return nil, ErrFailToLock } - defer t.RUnlock() + defer t.l.RUnlock() var ( tmpArea = make([]byte, perReadSize) @@ -204,22 +204,19 @@ func (t *File) ReadAll(perReadSize int, maxReadSize int) (data []byte, e error) return } -func (t *File) Seed(index int64) (e error) { +// Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified. +func (t *File) Seed(index int64, whence int) (e error) { t.getRWCloser() if t.Config.AutoClose { defer t.Close() } - if !t.TryLock() { + if !t.l.TryLock() { return ErrFailToLock } - defer t.Unlock() + defer t.l.Unlock() - whenc := 0 - if index < 0 { - whenc = 2 - } - t.cu, e = t.file.Seek(index, whenc) + t.cu, e = t.file.Seek(index, whence) return nil } @@ -230,19 +227,26 @@ func (t *File) Sync() (e error) { defer t.Close() } - if !t.TryRLock() { + if !t.l.TryRLock() { return ErrFailToLock } - defer t.RUnlock() + defer t.l.RUnlock() return t.file.Sync() } +func (t *File) Create(tryLock bool) { + t.getRWCloser() + if t.Config.AutoClose { + defer t.Close() + } +} + func (t *File) Delete() error { - if !t.TryLock() { + if !t.l.TryLock() { return ErrFailToLock } - defer t.Unlock() + defer t.l.Unlock() if t.IsDir() { return os.RemoveAll(t.Config.FilePath) diff --git a/file/FileWR_test.go b/file/FileWR_test.go index 63b9c57..05c9556 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -45,7 +45,7 @@ func TestWriteReadDel(t *testing.T) { t.Fatal(e) } - if e := f.Seed(0); e != nil { + if e := f.Seed(0, 0); e != nil { t.Fatal(e) } @@ -77,7 +77,7 @@ func TestSeed(t *testing.T) { t.Fatal(e) } - if e := f.Seed(1); e != nil { + if e := f.Seed(1, 0); e != nil { t.Fatal(e) } @@ -90,7 +90,7 @@ func TestSeed(t *testing.T) { } } - if e := f.Seed(-1); e != nil { + if e := f.Seed(-1, 2); e != nil { t.Fatal(e) } @@ -136,7 +136,7 @@ func TestReadUntil(t *testing.T) { t.Fatal(e) } - if e := f.Seed(0); e != nil { + if e := f.Seed(0, 0); e != nil { t.Fatal(e) } @@ -204,3 +204,16 @@ func TestReadAll(t *testing.T) { t.Fatal(e, string(data)) } } + +func TestCreate(t *testing.T) { + sf := New("t.txt", 0, true) + defer sf.Delete() + + if sf.IsExist() { + t.Fatal() + } + sf.Create(false) + if !sf.IsExist() { + t.Fatal() + } +} -- 2.39.2