Selaa lähdekoodia

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
lgcareer 4 vuotta sitten
vanhempi
commit
31dca43cdb

+ 40 - 6
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<ResourceComponent> 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);
+                }
+            }
+        }
+    }
 }