Skip to content

Commit 6784f4b

Browse files
committed
fix for ^M added to files on Windows (fixes 3014) and bulletproofing for save with temp files (fixes 2923)
1 parent 2a61c6c commit 6784f4b

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

app/src/processing/app/Base.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,26 +2499,36 @@ static public String loadFile(File file) throws IOException {
24992499
*/
25002500
static public void saveFile(String str, File file) throws IOException {
25012501
File temp = File.createTempFile(file.getName(), null, file.getParentFile());
2502-
try{
2502+
try {
25032503
// fix from cjwant to prevent symlinks from being destroyed.
25042504
File canon = file.getCanonicalFile();
2505+
// assign the var as second step since previous line may throw exception
25052506
file = canon;
25062507
} catch (IOException e) {
25072508
throw new IOException("Could not resolve canonical representation of " +
25082509
file.getAbsolutePath());
25092510
}
2510-
PApplet.saveStrings(temp, new String[] { str });
2511+
// Can't use saveStrings() here b/c Windows will add a ^M to the file
2512+
PrintWriter writer = PApplet.createWriter(temp);
2513+
writer.print(str);
2514+
boolean error = writer.checkError(); // calls flush()
2515+
writer.close(); // attempt to close regardless
2516+
if (error) {
2517+
throw new IOException("Error while trying to save " + file);
2518+
}
2519+
2520+
// remove the old file before renaming the temp file
25112521
if (file.exists()) {
25122522
boolean result = file.delete();
25132523
if (!result) {
25142524
throw new IOException("Could not remove old version of " +
2515-
file.getAbsolutePath());
2525+
file.getAbsolutePath());
25162526
}
25172527
}
25182528
boolean result = temp.renameTo(file);
25192529
if (!result) {
2520-
throw new IOException("Could not replace " +
2521-
file.getAbsolutePath());
2530+
throw new IOException("Could not replace " + file.getAbsolutePath() +
2531+
" with " + temp.getAbsolutePath());
25222532
}
25232533
}
25242534

0 commit comments

Comments
 (0)