From 56106401f947ed813fb3b8fb7ae0faecddc67089 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Tue, 10 May 2022 15:36:16 +0800 Subject: [PATCH] when under root dir, do not print parent link in DirectoryBrowser. #2173 --- .../shell/term/impl/http/DirectoryBrowser.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java index bab30e5e..63b68c55 100644 --- a/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java +++ b/core/src/main/java/com/taobao/arthas/core/shell/term/impl/http/DirectoryBrowser.java @@ -83,14 +83,16 @@ public class DirectoryBrowser { private static String linePart1Str = ""; private static String linePart2Str = "%-60s"; - static String renderDir(File dir) { + static String renderDir(File dir, boolean printParentLink) { File[] listFiles = dir.listFiles(); StringBuilder sb = new StringBuilder(8192); String dirName = dir.getName() + "/"; sb.append(String.format(pageHeader, dirName, dirName)); - sb.append("../\n"); + if (printParentLink) { + sb.append("../\n"); + } if (listFiles != null) { Arrays.sort(listFiles); @@ -161,8 +163,7 @@ public class DirectoryBrowser { if (!path.endsWith("/")) { fullResp.setStatus(HttpResponseStatus.FOUND).headers().set(HttpHeaderNames.LOCATION, "/" + path + "/"); } - - String renderResult = renderDir(file); + String renderResult = renderDir(file, !isSameFile(dir, file)); fullResp.content().writeBytes(renderResult.getBytes("utf-8")); fullResp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=utf-8"); ctx.write(fullResp); @@ -275,4 +276,11 @@ public class DirectoryBrowser { return false; } + public static boolean isSameFile(File a, File b) { + try { + return a.getCanonicalPath().equals(b.getCanonicalPath()); + } catch (IOException e) { + return false; + } + } }