Skip to content

Commit

Permalink
HIVE-26685: Improve path name escaping/unescaping (apache#3721)
Browse files Browse the repository at this point in the history
  • Loading branch information
pettyjamesm authored Dec 5, 2022
1 parent 10805bc commit dbe2a32
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
38 changes: 34 additions & 4 deletions common/src/java/org/apache/hadoop/hive/common/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ public static String makeListBucketingDirName(List<String> lbCols, List<String>
}
}

/**
* Hex encoding characters indexed by integer value
*/
private static final char[] HEX_UPPER_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

static boolean needsEscaping(char c) {
return c < charToEscape.size() && charToEscape.get(c);
}
Expand Down Expand Up @@ -287,12 +292,28 @@ public static String escapePathName(String path, String defaultPath) {
}
}

StringBuilder sb = new StringBuilder();
// Fast-path detection, no escaping and therefore no copying necessary
int firstEscapeIndex = -1;
for (int i = 0; i < path.length(); i++) {
if (needsEscaping(path.charAt(i))) {
firstEscapeIndex = i;
break;
}
}
if (firstEscapeIndex == -1) {
return path;
}

// slow path, escape beyond the first required escape character into a new string
StringBuilder sb = new StringBuilder();
if (firstEscapeIndex > 0) {
sb.append(path, 0, firstEscapeIndex);
}

for (int i = firstEscapeIndex; i < path.length(); i++) {
char c = path.charAt(i);
if (needsEscaping(c)) {
sb.append('%');
sb.append(String.format("%1$02X", (int) c));
sb.append('%').append(HEX_UPPER_CHARS[(0xF0 & c) >>> 4]).append(HEX_UPPER_CHARS[(0x0F & c)]);
} else {
sb.append(c);
}
Expand All @@ -301,8 +322,17 @@ public static String escapePathName(String path, String defaultPath) {
}

public static String unescapePathName(String path) {
int firstUnescapeIndex = path.indexOf('%');
if (firstUnescapeIndex == -1) {
return path;
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < path.length(); i++) {
if (firstUnescapeIndex > 0) {
sb.append(path, 0, firstUnescapeIndex);
}

for (int i = firstUnescapeIndex; i < path.length(); i++) {
char c = path.charAt(i);
if (c == '%' && i + 2 < path.length()) {
int code = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ public void testListStatusIterator() throws Exception {
assertEquals(1, assertExpectedFilePaths(itr, Collections.singletonList("mock:/tmp/dummy")));
}

@Test
public void testPathEscapeChars() {
StringBuilder sb = new StringBuilder();
FileUtils.charToEscape.stream().forEach(integer -> sb.append((char) integer));
String path = sb.toString();
assertEquals(path, FileUtils.unescapePathName(FileUtils.escapePathName(path)));
}

private int assertExpectedFilePaths(RemoteIterator<? extends FileStatus> lfs, List<String> expectedPaths)
throws Exception {
int count = 0;
Expand Down

0 comments on commit dbe2a32

Please sign in to comment.