Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functional optimize:use canMkdir() method relpalce isOwner() method in LocalFileSystem #1036

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ public boolean mkdirs(FsPath dest) throws IOException {
dirsToMake.push(parent);
parent = parent.getParentFile();
}
if(!isOwner(parent.getPath())) {
throw new IOException("only owner can mkdir path " + path);
if(!canMkdir(new FsPath(parent.getPath()))) {
throw new IOException("no permission to mkdir path " + path);
}
while (!dirsToMake.empty()) {
File dirToMake = dirsToMake.pop();
Expand All @@ -157,6 +157,19 @@ public boolean mkdirs(FsPath dest) throws IOException {
return true;
}

public boolean canMkdir(FsPath destParentDir) throws IOException {
if (!StorageUtils.isIOProxy()){
LOG.debug("io not proxy, not check ownerer, just check if hava write permission ");
return this.canWrite(destParentDir);
}else{
LOG.info("io proxy, check owner ");
if(!isOwner(destParentDir.getPath())) {
throw new IOException("current user:" + user + ", parentPath:"+ destParentDir.getPath() +", only owner can mkdir path " + destParentDir);
}
}
return false;
}

@Override
public boolean copy(String origin, String dest) throws IOException {
File file = new File(dest);
Expand All @@ -183,17 +196,29 @@ public boolean copy(String origin, String dest) throws IOException {
@Override
public boolean setPermission(FsPath dest, String permission) throws IOException {
if (!StorageUtils.isIOProxy()){
LOG.info("io not proxy, setPermission skip");
LOG.info("io not proxy, setPermission as parent.");
try {
PosixFileAttributes attr = Files.readAttributes(Paths.get(dest.getParent().getPath()), PosixFileAttributes.class);
LOG.debug("parent permissions: attr: " + attr);
Files.setPosixFilePermissions(Paths.get(dest.getPath()), attr.permissions());

}catch (NoSuchFileException e){
LOG.error("File or folder does not exist or file name is garbled(文件或者文件夹不存在或者文件名乱码)",e);
throw new StorageWarnException(51001,e.getMessage());
}
return true;

}
String path = dest.getPath();
if(StringUtils.isNumeric(permission)) {
permission = FsPath.permissionFormatted(permission);
}
Files.setPosixFilePermissions(Paths.get(path), PosixFilePermissions.fromString(permission));
return true;

}


@Override
public FsPathListWithError listPathWithError(FsPath path) throws IOException {
File file = new File(path.getPath());
Expand Down Expand Up @@ -402,4 +427,4 @@ private String getOwner(String path) throws IOException {
Files.readAttributes(Paths.get(path), PosixFileAttributes.class);
return attr.owner().getName();
}
}
}