github-code-scanning[bot] commented on code in PR #13343:
URL:
https://github.com/apache/dolphinscheduler/pull/13343#discussion_r1062165459
##########
dolphinscheduler-worker/src/main/java/org/apache/dolphinscheduler/server/worker/utils/TaskFilesTransferUtils.java:
##########
@@ -285,4 +312,77 @@
}
return newPath;
}
+
+ /**
+ * If have enough temporary storage, move output file to tmp dir and
replace srcPath with scp command.
+ *
+ * @param taskExecutionContext is the context of task
+ * @param srcPath is original source path
+ * @return new source path after using tmp storage (remain unchanged if
not use)
+ */
+ public static String moveIfUseTmpStorage(TaskExecutionContext
taskExecutionContext, String srcPath) {
+ long maxTmpFileSize = PropertyUtils.getLong(TMP_TRANSFER_FILE_SIZE,
100) * 1024 * 1024;
+ try {
+ String tmpDir = FileUtils.getTmpDir(
+ FileUtils.getTmpBaseDir(),
+ taskExecutionContext.getTenantCode(),
+ taskExecutionContext.getProjectCode(),
+ taskExecutionContext.getProcessDefineCode(),
+ taskExecutionContext.getProcessDefineVersion(),
+ taskExecutionContext.getProcessInstanceId());
+ long tmpDirSize = FileUtils.getDirectorySize(tmpDir);
+ if (tmpDirSize < maxTmpFileSize) {
+ String fileName =
srcPath.substring(srcPath.lastIndexOf(SINGLE_SLASH) + 1);
+ String tmpPath = String.format(FORMAT_S_S, tmpDir, fileName);
+ FileUtils.renameTo(srcPath, tmpPath);
+ logger.info("Save output files in temporary path: {}",
tmpPath);
+ // fill in partial scp command template
+ String preparedCommand = String.format(SCP_COMMAND_TEMPLATE,
+ taskExecutionContext.getTenantCode(),
+ taskExecutionContext.getHost().split(":")[0],
+ tmpPath,
+ FORMAT_S);
+ logger.info("Generate scp command template: {}",
preparedCommand);
+ FileUtils.writeContent2File(preparedCommand, srcPath);
+
+ String templatePath = srcPath + TEMPLATE_SUFFIX;
+ FileUtils.renameTo(srcPath, templatePath);
+ srcPath = templatePath;
+ }
+ } catch (IOException ex) {
+ throw new TaskException(ex.getMessage(), ex);
+ }
+ return srcPath;
+ }
+
+ /**
+ * Complete and execute scp command
+ *
+ * @param downloadPath is where to save scp files
+ * @param targetPath zip file will be unpacked later, rather, move to
targetPath directly
+ * @return true if fetch a zip file
+ * @throws IOException
+ * @throws InterruptedException
+ */
+ public static boolean scpFetchFile(String downloadPath,
+ String targetPath) throws IOException,
InterruptedException {
+ String commandString = FileUtils.readFile2Str(new
FileInputStream(downloadPath));
Review Comment:
## Potential input resource leak
This FileInputStream is not always closed on method exit.
[Show more
details](https://github.com/apache/dolphinscheduler/security/code-scanning/2471)
##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/FileUtils.java:
##########
@@ -299,4 +333,34 @@
return crcString;
}
+ /**
+ * Get total size of directory
+ *
+ * @param path
+ * @return
+ * @throws IOException
+ */
+ public static long getDirectorySize(String path) throws IOException {
+ File dir = new File(path);
+ if (!dir.exists()) {
+ createWorkDirIfAbsent(path);
+ }
+ return org.apache.commons.io.FileUtils.sizeOfDirectory(dir);
+ }
+
+ /**
+ * Rename file / directory
+ *
+ * @param oldPath old path
+ * @param newPath new path
+ */
+ public static void renameTo(String oldPath, String newPath) throws
IOException {
+ File newFile = new File(newPath);
+ if (newFile.exists()) {
+ org.apache.commons.io.FileUtils.forceDelete(newFile);
+ }
+ File oldFile = new File(oldPath);
+ oldFile.renameTo(new File(newPath));
Review Comment:
## Ignored error status of call
Method renameTo ignores exceptional return value of File.renameTo.
[Show more
details](https://github.com/apache/dolphinscheduler/security/code-scanning/2472)
--
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]