]> 127.0.0.1 Git - part/.git/commitdiff
Add Create File v0.23.7
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 4 Mar 2023 09:19:30 +0000 (17:19 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 4 Mar 2023 09:19:30 +0000 (17:19 +0800)
file/FileWR.go
file/FileWR_test.go

index 15c2cf80d46c98b2bcba429236a285e55e443cc5..f60340c0a0d2afc4ab8d735ab9d4a6d263d70de2 100644 (file)
@@ -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)
index 63b9c57e0f6bc946556622ea662176fe8f936179..05c9556ced302b11eff76d5d2fceb958445d12c5 100644 (file)
@@ -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()
+       }
+}