|
| 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