From: qydysky Date: Sun, 9 Apr 2023 11:41:51 +0000 (+0800) Subject: update X-Git-Tag: v0.24.8 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=dc3b56be33e78a02c0de25b5db7db5038643e367;p=part%2F.git update --- diff --git a/file/FileWR.go b/file/FileWR.go index 2d44ae4..d6b2977 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -5,6 +5,7 @@ import ( "errors" "io" "os" + "path/filepath" "strings" "sync" @@ -350,7 +351,6 @@ func (t *File) getRWCloser() { } func (t *File) newPath() error { - /* 如果filename路径不存在,就新建它 */ @@ -359,17 +359,22 @@ func (t *File) newPath() error { return err == nil || os.IsExist(err) } - for i := 0; true; { - a := strings.Index(t.Config.FilePath[i:], "/") - if a == -1 { + rawPath := "" + if !filepath.IsAbs(t.Config.FilePath) { + rawPath, _ = os.Getwd() + } + rawPs := strings.Split(t.Config.FilePath, string(os.PathSeparator)) + for n, p := range rawPs { + if p == "" || p == "." { + continue + } + if n == len(rawPs)-1 { break } - if a == 0 { - a = 1 - } //bug fix 当绝对路径时开头的/导致问题 - i = i + a + 1 - if !exist(t.Config.FilePath[:i-1]) { - err := os.Mkdir(t.Config.FilePath[:i-1], os.ModePerm) + rawPath += string(os.PathSeparator) + p + + if !exist(rawPath) { + err := os.Mkdir(rawPath, os.ModePerm) if err != nil { return err } diff --git a/file/FileWR_test.go b/file/FileWR_test.go index c836785..11ee154 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -4,12 +4,40 @@ import ( "bytes" "errors" "io" + "runtime" "testing" "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/encoding/unicode" ) +func TestNewPath(t *testing.T) { + if runtime.GOOS == "linux" { + f := New("/tmp/test/test.log", 0, true) + f.Create() + if !f.IsExist() { + t.Fatal() + } + f.Delete() + } + if runtime.GOOS == "windows" { + f := New("C:\\test\\test.log", 0, true) + f.Create() + if !f.IsExist() { + t.Fatal() + } + f.Delete() + } + { + f := New("./test/test.log", 0, true) + f.Create() + if !f.IsExist() { + t.Fatal() + } + f.Delete() + } +} + func TestWriteReadDelSync(t *testing.T) { f := New("rwd.txt", -6, true) if i, e := f.Write([]byte("sssa\n"), true); i == 0 || e != nil {