From 1af89c63999881244a1e26ef87f8be4ca905e2d6 Mon Sep 17 00:00:00 2001 From: qydysky Date: Sun, 16 Feb 2025 04:42:39 +0800 Subject: [PATCH] 1 (#29) --- file/FileWR.go | 7 ++++++- file/FileWR_test.go | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/file/FileWR.go b/file/FileWR.go index feba846..3c39b97 100644 --- a/file/FileWR.go +++ b/file/FileWR.go @@ -118,8 +118,13 @@ func New(filePath string, curIndex int64, autoClose bool) *File { } } -func (t *File) InRoot(root string) *File { +func (t *File) CheckRoot(root string) *File { t.Config.root = root + if rel, e := filepath.Rel(root, t.Config.FilePath); e != nil { + panic(e) + } else { + t.Config.FilePath = rel + } return t } diff --git a/file/FileWR_test.go b/file/FileWR_test.go index 4ebddfd..8a8104d 100644 --- a/file/FileWR_test.go +++ b/file/FileWR_test.go @@ -308,8 +308,11 @@ func TestCreate(t *testing.T) { func TestIsRoot(t *testing.T) { sf := New("../t.txt", 0, true) - sf.InRoot("testdata") + sf.CheckRoot("testdata") if !strings.HasSuffix(sf.Delete().Error(), "path escapes from parent") { t.Fatal() } + + sf = New("testdata/1.txt", 0, true).CheckRoot("testdata") + t.Log(sf.IsExist()) } -- 2.39.2