]> 127.0.0.1 Git - part/.git/commitdiff
update v0.24.8
authorqydysky <qydysky@foxmail.com>
Sun, 9 Apr 2023 11:41:51 +0000 (19:41 +0800)
committerqydysky <qydysky@foxmail.com>
Sun, 9 Apr 2023 11:41:51 +0000 (19:41 +0800)
file/FileWR.go
file/FileWR_test.go

index 2d44ae41246392579d5c6fb519c18fce05f81da9..d6b297744010f9b6773564ccf213a4decbbb7c99 100644 (file)
@@ -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
                        }
index c836785adb0ca0766cc3e86f4a83cbf8d55514f4..11ee154b11c024fccf8fb7e50814fde9ea722ad2 100644 (file)
@@ -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 {