From: qydysky Date: Tue, 27 Jun 2023 08:51:03 +0000 (+0000) Subject: Add DirFiles X-Git-Tag: v0.28.0+20230627b163176 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=b1631761d65ea117082be0f7b5cd1442754bd2b3;p=part%2F.git Add DirFiles --- diff --git a/file/FileWR.go b/file/FileWR.go index 21570a0..a08d630 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -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 } } } diff --git a/file/FileWR_test.go b/file/FileWR_test.go index a9bec77..4f010ae 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -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 index 0000000..e69de29