HADOOP-17686. Avoid potential NPE by using Path#getParentPath API in hadoop-huaweicloud (#2990)
Signed-off-by: Takanobu Asanuma <tasanuma@apache.org>
This commit is contained in:
parent
b944084b32
commit
c80f07422f
|
@ -55,6 +55,7 @@ import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
@ -872,8 +873,12 @@ final class OBSObjectBucketUtils {
|
||||||
directories.add(p.toString());
|
directories.add(p.toString());
|
||||||
}
|
}
|
||||||
while (p.compareTo(sourcePath) > 0) {
|
while (p.compareTo(sourcePath) > 0) {
|
||||||
p = p.getParent();
|
Optional<Path> parent = p.getOptionalParentPath();
|
||||||
if (p.isRoot() || p.compareTo(sourcePath) == 0) {
|
if (!parent.isPresent()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
p = parent.get();
|
||||||
|
if (p.compareTo(sourcePath) == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
directories.add(p.toString());
|
directories.add(p.toString());
|
||||||
|
|
|
@ -24,6 +24,7 @@ import java.io.ObjectInputValidation;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.avro.reflect.Stringable;
|
import org.apache.avro.reflect.Stringable;
|
||||||
|
@ -420,22 +421,39 @@ public class Path
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the parent of a path or null if at root.
|
* Returns the parent of a path or null if at root. Better alternative is
|
||||||
|
* {@link #getOptionalParentPath()} to handle nullable value for root path.
|
||||||
|
*
|
||||||
* @return the parent of a path or null if at root
|
* @return the parent of a path or null if at root
|
||||||
*/
|
*/
|
||||||
public Path getParent() {
|
public Path getParent() {
|
||||||
|
return getParentUtil();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the parent of a path as {@link Optional} or
|
||||||
|
* {@link Optional#empty()} i.e an empty Optional if at root.
|
||||||
|
*
|
||||||
|
* @return Parent of path wrappen in {@link Optional}.
|
||||||
|
* {@link Optional#empty()} i.e an empty Optional if at root.
|
||||||
|
*/
|
||||||
|
public Optional<Path> getOptionalParentPath() {
|
||||||
|
return Optional.ofNullable(getParentUtil());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Path getParentUtil() {
|
||||||
String path = uri.getPath();
|
String path = uri.getPath();
|
||||||
int lastSlash = path.lastIndexOf('/');
|
int lastSlash = path.lastIndexOf('/');
|
||||||
int start = startPositionWithoutWindowsDrive(path);
|
int start = startPositionWithoutWindowsDrive(path);
|
||||||
if ((path.length() == start) || // empty path
|
if ((path.length() == start) || // empty path
|
||||||
(lastSlash == start && path.length() == start+1)) { // at root
|
(lastSlash == start && path.length() == start + 1)) { // at root
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
String parent;
|
String parent;
|
||||||
if (lastSlash==-1) {
|
if (lastSlash == -1) {
|
||||||
parent = CUR_DIR;
|
parent = CUR_DIR;
|
||||||
} else {
|
} else {
|
||||||
parent = path.substring(0, lastSlash==start?start+1:lastSlash);
|
parent = path.substring(0, lastSlash == start ? start + 1 : lastSlash);
|
||||||
}
|
}
|
||||||
return new Path(uri.getScheme(), uri.getAuthority(), parent);
|
return new Path(uri.getScheme(), uri.getAuthority(), parent);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue