github-advanced-security[bot] commented on code in PR #17547: URL: https://github.com/apache/dolphinscheduler/pull/17547#discussion_r2412780713
########## dolphinscheduler-storage-plugin/dolphinscheduler-storage-api/src/main/java/org/apache/dolphinscheduler/plugin/storage/api/local/LocalStorageOperator.java: ########## @@ -0,0 +1,206 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.storage.api.local; + +import org.apache.dolphinscheduler.plugin.storage.api.AbstractStorageOperator; +import org.apache.dolphinscheduler.plugin.storage.api.ResourceMetadata; +import org.apache.dolphinscheduler.plugin.storage.api.StorageEntity; +import org.apache.dolphinscheduler.plugin.storage.api.StorageOperator; + +import org.apache.commons.io.FileUtils; + +import java.io.Closeable; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +import com.google.common.collect.Lists; + +@Slf4j +public class LocalStorageOperator extends AbstractStorageOperator implements Closeable, StorageOperator { + + public LocalStorageOperator(String resourceBaseAbsolutePath) throws IOException { + super(resourceBaseAbsolutePath); + final Path path = Paths.get(resourceBaseAbsolutePath); + if (Files.exists(path)) { + if (!Files.isDirectory(path)) { + throw new IllegalArgumentException("The base path must be a directory: " + resourceBaseAbsolutePath); + } + } else { + Files.createDirectories(path); + } + } + + @SneakyThrows + @Override + public void createStorageDir(String directoryAbsolutePath) { + final Path path = Paths.get(directoryAbsolutePath); + if (exists(directoryAbsolutePath)) { + throw new FileAlreadyExistsException("Directory already exists: " + directoryAbsolutePath); + } + Files.createDirectories(path); + } + + @Override + public boolean exists(String resourceAbsolutePath) { + return Files.exists(Paths.get(resourceAbsolutePath)); + } + + @SneakyThrows + @Override + public void delete(String resourceAbsolutePath, boolean recursive) { + if (recursive) { + FileUtils.deleteQuietly(new File(resourceAbsolutePath)); + } else { + Files.deleteIfExists(Paths.get(resourceAbsolutePath)); + } + } + + @SneakyThrows + @Override + public void copy(String srcAbsolutePath, String dstAbsolutePath, boolean deleteSource, boolean overwrite) { + if (srcAbsolutePath.equals(dstAbsolutePath)) { + throw new IllegalArgumentException( + "Source path and destination path cannot be the same: " + srcAbsolutePath); + } + + if (!exists(srcAbsolutePath)) { + throw new FileNotFoundException("Source path does not exist: " + srcAbsolutePath); + } + + if (exists(dstAbsolutePath)) { + if (!overwrite) { + throw new FileAlreadyExistsException("Destination path already exists: " + dstAbsolutePath); + } + delete(dstAbsolutePath, true); + } + + final File srcFile = new File(srcAbsolutePath); + final File dstFile = new File(dstAbsolutePath); + if (FileUtils.isDirectory(srcFile)) { + FileUtils.copyDirectoryToDirectory(srcFile, dstFile); + } else { + FileUtils.copyFile(srcFile, dstFile); Review Comment: ## Uncontrolled data used in path expression This path depends on a [user-provided value](1). This path depends on a [user-provided value](2). This path depends on a [user-provided value](3). This path depends on a [user-provided value](4). This path depends on a [user-provided value](5). This path depends on a [user-provided value](6). This path depends on a [user-provided value](7). This path depends on a [user-provided value](8). [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/5555) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
