Skip to content

Commit

Permalink
Fix filename sanitization on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Berlakovich committed Mar 15, 2024
1 parent 919a081 commit 17f5cf2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package jdk.graal.compiler.debug.test;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;

import jdk.graal.compiler.debug.StandardPathUtilitiesProvider;

public class StandardPathUtilitiesProviderTest {

@Test
public void sanitizesPathName() {
StandardPathUtilitiesProvider sut = new StandardPathUtilitiesProvider();
String sanitized = sut.sanitizeFileName("\0null");
Assert.assertEquals("_null", sanitized);

sanitized = sut.sanitizeFileName("path/with/slashes");
Assert.assertEquals("path_with_slashes", sanitized);
}

@Test
public void sanitizesPathNameOnWindows() {
Assume.assumeTrue(System.getProperty("os.name", "").startsWith("Windows"));

// Windows is pickier regarding path names
StandardPathUtilitiesProvider sut = new StandardPathUtilitiesProvider();
String sanitized = sut.sanitizeFileName("<lessthan>greaterthan:colon|bar*asterisk?question");
Assert.assertEquals("_lessthan_greaterthan_colon_bar_asterisk_question", sanitized);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,18 @@ public String sanitizeFileName(String name) {
}
buf.append('_');
}
return buf.toString();

/*
* On Windows, the original path might contain "/" as well (both "\" and "/" are equally
* valid path separators on Windows). Since File.separatorChar only reports "\" on Windows,
* we might have missed it during the previous sanitization. Paths.get should work now
* because we have removed all illegal characters and on Windows it canonicalizes the path
* to contain only "\". We thus replace any "/" that were converted to "\" here.
*/
String pathString = buf.toString();
String sanitizedPathString = Paths.get(pathString).toString();
sanitizedPathString = sanitizedPathString.replace(File.separatorChar, '_');
return sanitizedPathString;
}

@Override
Expand Down

0 comments on commit 17f5cf2

Please sign in to comment.