]> 127.0.0.1 Git - part/.git/commitdiff
1 (#42) v0.28.20250406180726
authorqydysky <qydysky@foxmail.com>
Sun, 6 Apr 2025 18:07:20 +0000 (02:07 +0800)
committerGitHub <noreply@github.com>
Sun, 6 Apr 2025 18:07:20 +0000 (02:07 +0800)
file/FileWR.go
file/FileWR_test.go

index 88ae50f52f2af9b1d9ab26d1787110082905bdb5..4c82e38d7f305a23fb9cb98ac2cd6df3b9cbc85b 100644 (file)
@@ -108,6 +108,48 @@ type Config struct {
        Coder encoder.Encoding
 }
 
+type FS interface {
+       fs.FS
+       OpenFile(name string) (*File, error)
+}
+
+type dirFS string
+
+func (dir dirFS) Open(name string) (fs.File, error) {
+       return dir.OpenFile(name)
+}
+
+func (dir dirFS) OpenFile(name string) (*File, error) {
+       fullname, err := dir.join(name)
+       if err != nil {
+               return nil, &fs.PathError{Op: "open", Path: name, Err: err}
+       }
+       return Open(fullname).CheckRoot(string(dir)), nil
+}
+
+// join returns the path for name in dir.
+func (dir dirFS) join(name string) (string, error) {
+       if dir == "" {
+               return "", errors.New("os: DirFS with empty root")
+       }
+       name, err := filepath.Localize(name)
+       if err != nil {
+               return "", fs.ErrInvalid
+       }
+       if os.IsPathSeparator(dir[len(dir)-1]) {
+               return string(dir) + name, nil
+       }
+       return string(dir) + string(os.PathSeparator) + name, nil
+}
+
+func DirFS(dir string) FS {
+       return dirFS(dir)
+}
+
+func Open(filePath string) *File {
+       return New(filePath, 0, false)
+}
+
 func New(filePath string, curIndex int64, autoClose bool) *File {
        return &File{
                Config: Config{
@@ -125,6 +167,16 @@ func (t *File) CheckRoot(root string) *File {
        } else {
                t.Config.FilePath = rel
        }
+       newPath(dos{
+               create:    os.Create,
+               lstat:     os.Lstat,
+               mkdir:     os.Mkdir,
+               open:      os.Open,
+               openFile:  os.OpenFile,
+               remove:    os.Remove,
+               removeAll: os.RemoveAll,
+               stat:      os.Stat,
+       }, t.Config.root+"/.t", fs.ModeDir)
        return t
 }
 
@@ -650,6 +702,10 @@ func (t *File) IsExist() bool {
        return err == nil
 }
 
+func IsExist(path string) bool {
+       return Open(path).IsExist()
+}
+
 func (t *File) IsDir() bool {
        info, err := t.Stat()
        if err != nil {
index 452af23a4b3f03e1dcd13f5b1f639eba44591975..3d1dc132311495153100622a03c00d9e88930892 100644 (file)
@@ -16,6 +16,23 @@ import (
        "golang.org/x/text/encoding/unicode"
 )
 
+func TestDir(t *testing.T) {
+       Open("test2").Delete()
+       if Open("test2").IsExist() {
+               t.Fatal()
+       }
+       if f, e := DirFS("test2").Open("FileWR.go"); e != nil {
+               t.Fatal(e)
+       } else if _, e := f.(*File).Write([]byte{'1'}, false); e != nil {
+               t.Fatal(e)
+       } else {
+               f.(*File).Delete()
+       }
+       if !Open("test2").IsExist() {
+               t.Fatal()
+       }
+}
+
 func TestMain(t *testing.T) {
        if runtime.GOOS == `windows` {
                if filepath.Join(`c:\`, "s/as") != `c:\s\as` {