From 9e36981511a2e407bd8b286c3f41d5091c2b6575 Mon Sep 17 00:00:00 2001 From: qydysky Date: Wed, 26 Feb 2025 00:10:53 +0800 Subject: [PATCH] 1 --- file/FileWR.go | 31 +++++-------------------------- file/FileWR_test.go | 27 +++++++++++++++++++-------- 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/file/FileWR.go b/file/FileWR.go index 9afe1c5..88ae50f 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -791,7 +791,7 @@ func (t *File) getRWCloser(mode ...fs.FileMode) error { return e } if !t.IsExist() { - t.newPath(t.Config.FilePath, fs.ModeDir|fmode) + newPath(fos, t.Config.FilePath, fs.ModeDir|fmode) if t.IsDir() { if f, e := fos.OpenFile(t.Config.FilePath, os.O_RDONLY|os.O_EXCL, fmode); e != nil { return e @@ -842,33 +842,12 @@ func (t *File) getRWCloser(mode ...fs.FileMode) error { return nil } -func (t *File) newPath(path string, mode fs.FileMode) { - rawPath := "" +func newPath(fos fosi, path string, mode fs.FileMode) { if !filepath.IsAbs(path) { - rawPath, _ = os.Getwd() - } - rawPs := strings.Split(strings.ReplaceAll(path, `\`, `/`), `/`) - for n, p := range rawPs { - if p == "" || p == "." { - continue - } - if n == len(rawPs)-1 { - break - } - - if rawPath != "" { - rawPath += string(os.PathSeparator) - } - rawPath += p - - fos, e := t.getOs() - if e != nil { - panic(e) - } - if _, err := fos.Stat(rawPath); os.IsNotExist(err) { - _ = fos.Mkdir(rawPath, mode) - } + wd, _ := os.Getwd() + path = filepath.Join(wd, path) } + _ = fos.Mkdir(filepath.Dir(filepath.Clean(path)), mode) } func (t *File) write() io.Writer { diff --git a/file/FileWR_test.go b/file/FileWR_test.go index 3ab50d0..452af23 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "path/filepath" "runtime" "strings" "testing" @@ -15,6 +16,19 @@ import ( "golang.org/x/text/encoding/unicode" ) +func TestMain(t *testing.T) { + if runtime.GOOS == `windows` { + if filepath.Join(`c:\`, "s/as") != `c:\s\as` { + t.Fatal() + } + } + if runtime.GOOS == `linux` { + if filepath.Join(`/`, "s/as") != `/s/as` { + t.Fatal() + } + } +} + func TestDirFs(t *testing.T) { f := New("./testdata", 0, true) if fs, err := f.DirFiles(); err != nil { @@ -23,22 +37,19 @@ func TestDirFs(t *testing.T) { if len(fs) != 1 { t.Fatal() } - if fs[0] != "testdata/1.txt" { - t.Fatal() + if fs[0] != "testdata"+string(os.PathSeparator)+"1.txt" { + t.Fatal(fs[0]) } } } -func TestPathSeparator(t *testing.T) { - New("./testdata/l/0.txt", 0, false).Create() -} - func TestNewPath2(t *testing.T) { os.RemoveAll("./test") time.Sleep(time.Second) - go New("./test/test.log", 0, true).Create() - go New("./test/test2.log", 0, true).Create() + New("./test/test.log", 0, true).Create() + New("./test/test2.log", 0, true).Create() time.Sleep(time.Second) + os.RemoveAll("./test") } func TestNewPath(t *testing.T) { -- 2.39.2