From 31dca43cdbaec861b2bfc5dc7407f50424216e3c Mon Sep 17 00:00:00 2001 From: lgcareer <18610854716@163.com> Date: Tue, 2 Jun 2020 18:33:06 +0800 Subject: [PATCH] fix #2860:copy files and create empty directory (#2876) * copy resources need get top directory first * copy resources need get top directory first * fix #2860:copy files and create empty directory --- .../api/service/UsersService.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java index b76ca8324e..815dcb0998 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java @@ -21,6 +21,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.dolphinscheduler.api.dto.resources.ResourceComponent; import org.apache.dolphinscheduler.api.dto.resources.visitor.ResourceTreeVisitor; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.utils.CheckUtils; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; @@ -37,6 +38,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @@ -338,9 +340,7 @@ public class UsersService extends BaseService { if (CollectionUtils.isNotEmpty(fileResourcesList)) { ResourceTreeVisitor resourceTreeVisitor = new ResourceTreeVisitor(fileResourcesList); ResourceComponent resourceComponent = resourceTreeVisitor.visit(); - for (ResourceComponent resource : resourceComponent.getChildren()) { - HadoopUtils.getInstance().copy(oldResourcePath + "/" + resource.getName(), newResourcePath, false, true); - } + copyResourceFiles(resourceComponent, oldResourcePath, newResourcePath); } //udf resources @@ -349,9 +349,7 @@ public class UsersService extends BaseService { if (CollectionUtils.isNotEmpty(udfResourceList)) { ResourceTreeVisitor resourceTreeVisitor = new ResourceTreeVisitor(udfResourceList); ResourceComponent resourceComponent = resourceTreeVisitor.visit(); - for (ResourceComponent resource : resourceComponent.getChildren()) { - HadoopUtils.getInstance().copy(oldUdfsPath + "/" + resource.getName(), newUdfsPath, false, true); - } + copyResourceFiles(resourceComponent, oldUdfsPath, newUdfsPath); } //Delete the user from the old tenant directory @@ -871,4 +869,40 @@ public class UsersService extends BaseService { return msg; } + + /** + * copy resource files + * @param resourceComponent resource component + * @param srcBasePath src base path + * @param dstBasePath dst base path + * @throws IOException io exception + */ + private void copyResourceFiles(ResourceComponent resourceComponent, String srcBasePath, String dstBasePath) throws IOException { + List components = resourceComponent.getChildren(); + + if (CollectionUtils.isNotEmpty(components)) { + for (ResourceComponent component:components) { + // verify whether exist + if (!HadoopUtils.getInstance().exists(String.format("%s/%s",srcBasePath,component.getFullName()))){ + logger.error("resource file: {} not exist,copy error",component.getFullName()); + throw new ServiceException(Status.RESOURCE_NOT_EXIST); + } + + if (!component.isDirctory()) { + // copy it to dst + HadoopUtils.getInstance().copy(String.format("%s/%s",srcBasePath,component.getFullName()),String.format("%s/%s",dstBasePath,component.getFullName()),false,true); + continue; + } + + if(CollectionUtils.isEmpty(component.getChildren())) { + // if not exist,need create it + if (!HadoopUtils.getInstance().exists(String.format("%s/%s",dstBasePath,component.getFullName()))) { + HadoopUtils.getInstance().mkdir(String.format("%s/%s",dstBasePath,component.getFullName())); + } + }else{ + copyResourceFiles(component,srcBasePath,dstBasePath); + } + } + } + } }