[ 
https://issues.apache.org/jira/browse/IO-860?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17892624#comment-17892624
 ] 

Stefan Feenstra commented on IO-860:
------------------------------------

Also not sure about one further discrepancy. 
According to the same documentation a filename should not end with a dot. 

In practice, it appears that applications like Java, windows file explorer etc 
will allow you to open or save a file with a dot at the end - however the 
created/read file will not contain the dot at the end of the filename,
In fact, if a file is present on the disk ending with a dot (This can be done 
by using the \\?\ prefix) most APIs will say the file does not exist when 
trying to open it.

I am not sure if Commons IO should mark these filenames as invalid? This could 
potentially cause problems in applications?
Example code:

{code:java}
        System.out.println("test.txt. exists: " + new 
File("test.txt.").exists());
        System.out.println("test.txt exists: " + new File("test.txt").exists());

        try (FileOutputStream fos = new FileOutputStream("test.txt.")) {
            fos.write("test".getBytes());
        }

        System.out.println("test.txt. exists: " + new 
File("test.txt.").exists());
        System.out.println("test.txt exists: " + new File("test.txt").exists());
{code}

One would expect only the first file to exist after the code is ran, the actual 
output is however:

{code}
test.txt. exists: false
test.txt exists: false
test.txt. exists: true
test.txt exists: true
{code}




> Missing reserved filenames for Windows in FileSystem.java (superscript digits 
> for COM and LPT) 
> -----------------------------------------------------------------------------------------------
>
>                 Key: IO-860
>                 URL: https://issues.apache.org/jira/browse/IO-860
>             Project: Commons IO
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 2.17.0
>            Reporter: Stefan Feenstra
>            Priority: Major
>
> Per Microsoft documentation, the following filenames are also reserved 
> filenames: LPT¹, LPT², LPT³, COM¹, COM² and COM³.
> As far as I can tell, only the non-superscript filenames are marked as 
> illegal in the FileSystem.WINDOWS enum. These six filenames should also be 
> added as reserved filenames, as they can not be used for regular files. (e.g. 
> trying to create a file called COM³ will fail).
>  
> Quote from the [microsoft 
> documentation|https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#file-and-directory-names]:
> {code:java}
> Windows recognizes the 8-bit ISO/IEC 8859-1
>  superscript digits ¹, ², and ³ as digits and treats them as valid parts
>  of COM# and LPT# device names, making them reserved in every directory. 
> {code}
> Testing code to reproduce: (there should be no output)
> {code:java}
> import org.apache.commons.io.FileSystem;
> public class FilenameTest {
>     public static void main(String[] args) {
>         char[] digitsWithSuperScript = {'1', '2', '3', '4', '5', '6', '7', 
> '8', '9', '¹', '²', '³'};
>         for (String filenamesPrefix : new String[]{"LPT", "COM"}) {
>             for (char digit : digitsWithSuperScript) {
>                 final String filename = filenamesPrefix + digit;
>                 checkFilename(filename, true);
>                 // Base filename should be reserved
>                 checkFilename(filename + ".tar.gz", true);
>                 // Extensions should be ignored
>                 checkFilename(filename + ".txt", true);
>                 // Non-extension suffixes are ok.
>                 checkFilename(filename + "additional_text", false);
>             }
>         }
>         for (String reservedFileName : new String[]{"CON", "PRN", "AUX", 
> "NUL", "CONIN$", "CONOUT$"}) {
>             // Base filename should be reserved
>             checkFilename(reservedFileName, true);
>             // Extensions should be ignored
>             checkFilename(reservedFileName + ".tar.gz", true);
>             // Non-extension suffixes are ok.
>             checkFilename(reservedFileName + "additional_text", false);
>         }
>     }
>     static void checkFilename(String filename, boolean isReserved) {
>         boolean isReservedAccordingToAPI = 
> FileSystem.WINDOWS.isReservedFileName(filename);
>         if (isReserved != isReservedAccordingToAPI) {
>             if (isReserved) {
>                 System.out.printf("'%s' is reserved according to the spec but 
> not according to FileSystem.isReservedFileName%n", filename);
>             } else {
>                 System.out.printf("'%s' is not reserved according to the spec 
> but is according to FileSystem.isReservedFileName%n", filename);
>             }
>         } else {
>             // Spec and API match
>         }
>     }
> }{code}
>  
> Link to relevant code:
> [https://github.com/apache/commons-io/blob/ec0fd2c98f0bf94d6d87c6adf237430e606e2e23/src/main/java/org/apache/commons/io/FileSystem.java#L91C16-L92C116]
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to