Skip to content

Commit bbf8ff6

Browse files
authored
<fix>(precompiled): fix bfs isExist bug. (#735)
1 parent 6ea3c82 commit bbf8ff6

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

src/main/java/org/fisco/bcos/sdk/v3/contract/precompiled/bfs/BFSService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ public Tuple2<BigInteger, List<BFSInfo>> listBFSInfo(
126126
*/
127127
public BFSInfo isExist(String absolutePath) throws ContractException {
128128
PrecompiledVersionCheck.LS_PAGE_VERSION.checkVersion(currentVersion);
129-
if (absolutePath.endsWith("/")) {
130-
absolutePath = absolutePath.substring(0, absolutePath.length() - 1);
129+
Tuple2<String, String> parentPathAndBaseName =
130+
BFSUtils.getParentPathAndBaseName(absolutePath);
131+
String parent = parentPathAndBaseName.getValue1();
132+
String child = parentPathAndBaseName.getValue2();
133+
if (BFSUtils.BFS_SYSTEM_PATH.contains(absolutePath)) {
134+
return new BFSInfo(child, BFSUtils.BFS_TYPE_DIR);
131135
}
132-
int index = absolutePath.lastIndexOf('/');
133-
String parent = absolutePath.substring(0, index);
134-
String child = absolutePath.substring(index + 1);
135136
int offset = 0;
136137
int limit = 500;
137138
while (true) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.fisco.bcos.sdk.v3.contract.precompiled.bfs;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
import java.util.Stack;
9+
import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple2;
10+
11+
public class BFSUtils {
12+
private BFSUtils() {}
13+
14+
public static final String BFS_TYPE_DIR = "directory";
15+
public static final String BFS_TYPE_CON = "contract";
16+
public static final String BFS_TYPE_LNK = "link";
17+
18+
public static final String BFS_ROOT = "/";
19+
public static final String BFS_APPS = "/apps";
20+
public static final String BFS_SYS = "/sys";
21+
public static final String BFS_TABLES = "/tables";
22+
public static final String BFS_USER = "/usr";
23+
24+
public static final Set<String> BFS_SYSTEM_PATH =
25+
new HashSet<>(Arrays.asList(BFS_ROOT, BFS_APPS, BFS_SYS, BFS_TABLES, BFS_USER));
26+
27+
public static Tuple2<String, String> getParentPathAndBaseName(String path) {
28+
if (path.equals("/")) return new Tuple2<>("/", "/");
29+
List<String> path2Level = path2Level(path);
30+
String baseName = path2Level.get(path2Level.size() - 1);
31+
String parentPath = '/' + String.join("/", path2Level.subList(0, path2Level.size() - 1));
32+
return new Tuple2<>(parentPath, baseName);
33+
}
34+
35+
public static List<String> path2Level(String absolutePath) {
36+
Stack<String> pathStack = new Stack<>();
37+
for (String s : absolutePath.split("/")) {
38+
if (s.isEmpty() || s.equals(".")) {
39+
continue;
40+
}
41+
if (s.equals("..")) {
42+
if (!pathStack.isEmpty()) {
43+
pathStack.pop();
44+
}
45+
continue;
46+
}
47+
pathStack.push(s);
48+
}
49+
return new ArrayList<>(pathStack);
50+
}
51+
}

0 commit comments

Comments
 (0)