ramitg254 commented on code in PR #471:
URL: https://github.com/apache/tez/pull/471#discussion_r3000612211
##########
tez-tools/analyzers/job-analyzer/src/main/java/org/apache/tez/analyzer/CSVResult.java:
##########
@@ -93,31 +95,52 @@ public void setComments(String comments) {
'}';
}
- //For testing
- public void dumpToFile(String fileName) throws IOException {
- OutputStreamWriter writer = new OutputStreamWriter(
- new FileOutputStream(new File(fileName)),
- Charset.forName("UTF-8").newEncoder());
- BufferedWriter bw = new BufferedWriter(writer);
- bw.write(Joiner.on(",").join(headers));
- bw.newLine();
- for (String[] record : recordsList) {
-
- if (record.length != headers.length) {
- continue; //LOG error msg?
- }
+ private static File validateOutputFile(String fileName) throws IOException {
+ //null or empty string check
+ if (fileName == null || fileName.trim().isEmpty()) {
+ throw new IllegalArgumentException("fileName must not be null or empty");
+ }
+
+ // resolve relative path
+ File target = new File(fileName).getCanonicalFile();
+ File parent = target.getParentFile();
+
+ // check if the parent directory exists
+ if (parent == null || !parent.isDirectory()) {
+ throw new IOException("Parent directory does not exist: " + parent);
+ }
+ // check if the parent directory is writable
+ if (!parent.canWrite()) {
+ throw new IOException("No write permission on directory: " + parent);
+ }
+ return target;
+ }
- StringBuilder sb = new StringBuilder();
- for(int i=0;i<record.length;i++) {
- sb.append(!Strings.isNullOrEmpty(record[i]) ? record[i] : " ");
- if (i < record.length - 1) {
- sb.append(",");
+ public void dumpToFile(String fileName) throws IOException {
+ File target = validateOutputFile(fileName);
+ Path targetPath = target.toPath();
+
+ try (BufferedWriter bw = new BufferedWriter(
+ // atomically create new file if it doesn't exist, and if exists,
throw an exception
Review Comment:
done
--
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]