]> 127.0.0.1 Git - part/.git/commitdiff
Add DirFiles v0.28.0+20230627b163176
authorqydysky <qydysky@foxmail.com>
Tue, 27 Jun 2023 08:51:03 +0000 (08:51 +0000)
committerqydysky <qydysky@foxmail.com>
Tue, 27 Jun 2023 08:51:03 +0000 (08:51 +0000)
file/FileWR.go
file/FileWR_test.go
file/testdata/1.txt [new file with mode: 0644]

index 21570a0cabf659495d7e37dea67bcf29bbad9f2d..a08d630ea7a810b4ba171106ce0c473ef44e26b4 100644 (file)
@@ -6,6 +6,7 @@ import (
        "io"
        "io/fs"
        "os"
+       "path"
        "path/filepath"
        "strings"
        "sync"
@@ -19,6 +20,7 @@ var (
        ErrNewFileCantSeed  = errors.New("ErrNewFileCantSeed")
        ErrFailToLock       = errors.New("ErrFailToLock")
        ErrMaxReadSizeReach = errors.New("ErrMaxReadSizeReach")
+       ErrNoDir            = errors.New("ErrNoDir")
 )
 
 type File struct {
@@ -292,6 +294,24 @@ func (t *File) IsDir() bool {
        return info.IsDir()
 }
 
+func (t *File) DirFiles() (files []string, err error) {
+       if !t.IsDir() {
+               err = ErrNoDir
+               return
+       }
+
+       f := t.File()
+       if fis, e := f.Readdir(0); e != nil {
+               err = e
+               return
+       } else {
+               for i := 0; i < len(fis); i++ {
+                       files = append(files, path.Clean(f.Name())+string(os.PathSeparator)+fis[i].Name())
+               }
+       }
+       return
+}
+
 func (t *File) File() *os.File {
        t.getRWCloser()
        return t.file
@@ -324,35 +344,51 @@ func (t *File) getRWCloser(mode ...fs.FileMode) {
        if t.Config.AutoClose || t.file == nil {
                if !t.IsExist() {
                        newPath(t.Config.FilePath, fs.ModeDir|fmode)
-                       if f, e := os.OpenFile(t.Config.FilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode); e != nil {
-                               panic(e)
+                       if t.IsDir() {
+                               if f, e := os.OpenFile(t.Config.FilePath, os.O_RDONLY|os.O_EXCL, fmode); e != nil {
+                                       panic(e)
+                               } else {
+                                       t.file = f
+                               }
                        } else {
-                               if t.Config.CurIndex > 0 {
-                                       t.cu = t.Config.CurIndex
-                                       t.cu, e = f.Seek(t.cu, int(AtOrigin))
-                                       if e != nil {
-                                               panic(e)
+                               if f, e := os.OpenFile(t.Config.FilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode); e != nil {
+                                       panic(e)
+                               } else {
+                                       if t.Config.CurIndex > 0 {
+                                               t.cu = t.Config.CurIndex
+                                               t.cu, e = f.Seek(t.cu, int(AtOrigin))
+                                               if e != nil {
+                                                       panic(e)
+                                               }
                                        }
+                                       t.file = f
                                }
-                               t.file = f
                        }
                } else {
-                       if f, e := os.OpenFile(t.Config.FilePath, os.O_RDWR|os.O_EXCL, fmode); e != nil {
-                               panic(e)
+                       if t.IsDir() {
+                               if f, e := os.OpenFile(t.Config.FilePath, os.O_RDONLY|os.O_EXCL, fmode); e != nil {
+                                       panic(e)
+                               } else {
+                                       t.file = f
+                               }
                        } else {
-                               if t.Config.CurIndex != 0 {
-                                       t.cu = t.Config.CurIndex
-                                       whenc := AtOrigin
-                                       if t.Config.CurIndex < 0 {
-                                               t.cu = t.cu + 1
-                                               whenc = AtEnd
-                                       }
-                                       t.cu, e = f.Seek(t.cu, int(whenc))
-                                       if e != nil {
-                                               panic(e)
+                               if f, e := os.OpenFile(t.Config.FilePath, os.O_RDWR|os.O_EXCL, fmode); e != nil {
+                                       panic(e)
+                               } else {
+                                       if t.Config.CurIndex != 0 {
+                                               t.cu = t.Config.CurIndex
+                                               whenc := AtOrigin
+                                               if t.Config.CurIndex < 0 {
+                                                       t.cu = t.cu + 1
+                                                       whenc = AtEnd
+                                               }
+                                               t.cu, e = f.Seek(t.cu, int(whenc))
+                                               if e != nil {
+                                                       panic(e)
+                                               }
                                        }
+                                       t.file = f
                                }
-                               t.file = f
                        }
                }
        }
index a9bec77da700ef668ac18b84fbe5a876f5bbfe02..4f010aeecc4133fd47f1d9deeb776d4153af4df2 100644 (file)
@@ -13,6 +13,20 @@ import (
        "golang.org/x/text/encoding/unicode"
 )
 
+func TestDirFs(t *testing.T) {
+       f := New("./testdata", 0, true)
+       if fs, err := f.DirFiles(); err != nil {
+               t.Fatal(err)
+       } else {
+               if len(fs) != 1 {
+                       t.Fatal()
+               }
+               if fs[0] != "testdata/1.txt" {
+                       t.Fatal()
+               }
+       }
+}
+
 func TestNewPath2(t *testing.T) {
        os.RemoveAll("./test")
        time.Sleep(time.Second)
diff --git a/file/testdata/1.txt b/file/testdata/1.txt
new file mode 100644 (file)
index 0000000..e69de29