Skip to content

Commit 5ef7203

Browse files
authored
Merge pull request #543 from datbeohbbh/datbeohbbh/helpers
Fixed recursive bug
2 parents 806856d + b3a364c commit 5ef7203

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed `internal/scheme/helpers/IsDirectoryExists(..)` recursive bug
2+
13
## v3.42.7
24
* Fixed `sugar.IsTableExists` with recursive check directory exists
35
* Added `sugar.IsDirectoryExists`

internal/scheme/helpers/check_exists.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@ func databaseName(c schemeClient) string {
2424
}
2525

2626
func IsDirectoryExists(ctx context.Context, c schemeClient, directory string) (exists bool, _ error) {
27+
if !strings.HasPrefix(directory, databaseName(c)) {
28+
return false, xerrors.WithStackTrace(fmt.Errorf(
29+
"path '%s' must be inside database '%s'",
30+
directory, databaseName(c),
31+
))
32+
}
33+
if directory == databaseName(c) {
34+
return true, nil
35+
}
2736
parentDirectory, childDirectory := path.Split(directory)
2837
parentDirectory = strings.TrimRight(parentDirectory, "/")
29-
if parentDirectory != databaseName(c) {
30-
if exists, err := IsDirectoryExists(ctx, c, parentDirectory); err != nil {
31-
return false, xerrors.WithStackTrace(err)
32-
} else if !exists {
33-
return false, nil
34-
}
38+
39+
if exists, err := IsDirectoryExists(ctx, c, parentDirectory); err != nil {
40+
return false, xerrors.WithStackTrace(err)
41+
} else if !exists {
42+
return false, nil
3543
}
44+
3645
d, err := c.ListDirectory(ctx, parentDirectory)
3746
if err != nil {
3847
return false, xerrors.WithStackTrace(err)
@@ -43,7 +52,7 @@ func IsDirectoryExists(ctx context.Context, c schemeClient, directory string) (e
4352
}
4453
if e.Type != scheme.EntryDirectory {
4554
return false, xerrors.WithStackTrace(fmt.Errorf(
46-
"entry '%s' in path '%s' is not a direectory: %s",
55+
"entry '%s' in path '%s' is not a directory: %s",
4756
childDirectory, parentDirectory, e.Type.String(),
4857
))
4958
}

internal/scheme/helpers/check_exists_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ func TestIsDirectoryExists(t *testing.T) {
5656
exists bool
5757
err bool
5858
}{
59+
{
60+
checkPath: "/c/d",
61+
client: isDirectoryExistsSchemeClient{"/a", "/a/b/"},
62+
exists: false,
63+
err: true,
64+
},
65+
{
66+
checkPath: "/a",
67+
client: isDirectoryExistsSchemeClient{"/a", "/a/b/"},
68+
exists: true,
69+
err: false,
70+
},
71+
{
72+
checkPath: "/a/b/c",
73+
client: isDirectoryExistsSchemeClient{"/a/b/c", "/a/b/c/d/"},
74+
exists: true,
75+
err: false,
76+
},
77+
{
78+
checkPath: "/a/b",
79+
client: isDirectoryExistsSchemeClient{"/a", "/a/b/"},
80+
exists: true,
81+
err: false,
82+
},
5983
{
6084
checkPath: "/a/b/c/d",
6185
client: isDirectoryExistsSchemeClient{"/a", "/a/"},

0 commit comments

Comments
 (0)