commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject commons-io git commit: [IO-555] Add reserved file names. Impl for Windows only for now.
Date Fri, 17 Nov 2017 23:18:00 GMT
Repository: commons-io
Updated Branches:
  refs/heads/master 2bacf6392 -> 4f50c3750


[IO-555] Add reserved file names. Impl for Windows only for now.

Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/4f50c375
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/4f50c375
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/4f50c375

Branch: refs/heads/master
Commit: 4f50c37506de036bb34094095aaa6d4d831bfbb7
Parents: 2bacf63
Author: Gary Gregory <ggregory@apache.org>
Authored: Fri Nov 17 16:17:57 2017 -0700
Committer: Gary Gregory <ggregory@apache.org>
Committed: Fri Nov 17 16:17:57 2017 -0700

----------------------------------------------------------------------
 .../java/org/apache/commons/io/FileSystem.java  | 47 +++++++++++++++-----
 .../apache/commons/io/FileSystemTestCase.java   | 35 ++++++++++-----
 2 files changed, 59 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-io/blob/4f50c375/src/main/java/org/apache/commons/io/FileSystem.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/io/FileSystem.java b/src/main/java/org/apache/commons/io/FileSystem.java
index d0c4a51..a2d4409 100644
--- a/src/main/java/org/apache/commons/io/FileSystem.java
+++ b/src/main/java/org/apache/commons/io/FileSystem.java
@@ -39,7 +39,7 @@ public enum FileSystem {
             0,
              '/'
             // @formatter:on
-    }),
+    }, new String[] {}),
 
     MAC_OSX(255, 1024, new char[] {
             // KEEP THIS ARRAY SORTED!
@@ -49,9 +49,9 @@ public enum FileSystem {
             '/',
              ':'
             // @formatter:on
-    }),
+    }, new String[] {}),
 
-    GENERIC(Integer.MAX_VALUE, Integer.MAX_VALUE, new char[] { 0 }),
+    GENERIC(Integer.MAX_VALUE, Integer.MAX_VALUE, new char[] { 0 }, new String[] {}),
 
     WINDOWS(255, 32000, new char[] {
             // KEEP THIS ARRAY SORTED!
@@ -63,7 +63,10 @@ public enum FileSystem {
             29, 30, 31,
             '"', '*', '/', ':', '<', '>', '?', '\\', '|'
             // @formatter:on
-    });
+    },
+            // KEEP THIS ARRAY SORTED!
+            new String[] { "AUX", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7",
"COM8", "COM9", "CON", "LPT1",
+                    "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", "NUL",
"PRN" });
 
     /**
      * The prefix String for all Windows OS.
@@ -176,13 +179,16 @@ public enum FileSystem {
 
     private final char[] illegalFileNameChars;
     private final int maxFileNameLength;
-
     private final int maxPathLength;
+    private final String[] reservedFileNames;
 
-    private FileSystem(final int maxFileLength, final int maxPathLength, final char[] illegalFileNameChars)
{
+    private FileSystem(final int maxFileLength, final int maxPathLength, final char[] illegalFileNameChars,
+            final String[] reservedFileNames) {
         this.maxFileNameLength = maxFileLength;
         this.maxPathLength = maxPathLength;
         this.illegalFileNameChars = Objects.requireNonNull(illegalFileNameChars, "illegalFileNameChars");
+        this.reservedFileNames = Objects.requireNonNull(reservedFileNames, "reservedFileNames");
+        ;
     }
 
     /**
@@ -217,9 +223,21 @@ public enum FileSystem {
     }
 
     /**
+     * Returns whether the given string is a reserved file name.
+     * 
+     * @param candidate
+     *            the string to test
+     * @return {@code true} if the given string is a reserved file name.
+     */
+    public boolean isReservedFileName(final CharSequence candidate) {
+        return Arrays.binarySearch(reservedFileNames, candidate) >= 0;
+    }
+
+    /**
      * Converts a candidate file name (without a path) like {@code "filename.ext"} or {@code
"filename"} to a legal file
      * name. Illegal characters in the candidate name are replaced by the {@code replacement}
character. If the file
-     * name length exceeds {@link #getMaxFileNameLength()}, then the name is truncated to
{@link #getMaxFileNameLength()}.
+     * name length exceeds {@link #getMaxFileNameLength()}, then the name is truncated to
+     * {@link #getMaxFileNameLength()}.
      *
      * @param candidate
      *            a candidate file name (without a path) like {@code "filename.ext"} or {@code
"filename"}
@@ -248,11 +266,9 @@ public enum FileSystem {
     }
 
     /**
-     * Checks if a candidate file name (without a path)
-     * such as {@code "filename.ext"} or {@code "filename"}
-     * is a potentially legal file name.
-     * If the file name length exceeds {@link #getMaxFileNameLength()},
-     * or if it contains an illegal character then the check fails.
+     * Checks if a candidate file name (without a path) such as {@code "filename.ext"} or
{@code "filename"} is a
+     * potentially legal file name. If the file name length exceeds {@link #getMaxFileNameLength()},
or if it contains
+     * an illegal character then the check fails.
      *
      * @param candidate
      *            a candidate file name (without a path) like {@code "filename.ext"} or {@code
"filename"}
@@ -262,6 +278,9 @@ public enum FileSystem {
         if (candidate == null || candidate.length() == 0 || candidate.length() > maxFileNameLength)
{
             return false;
         }
+        if (isReservedFileName(candidate)) {
+            return false;
+        }
         for (int i = 0; i < candidate.length(); i++) {
             if (isIllegalFileNameChar(candidate.charAt(i))) {
                 return false;
@@ -269,4 +288,8 @@ public enum FileSystem {
         }
         return true;
     }
+
+    public String[] getReservedFileNames() {
+        return reservedFileNames;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/commons-io/blob/4f50c375/src/test/java/org/apache/commons/io/FileSystemTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/FileSystemTestCase.java b/src/test/java/org/apache/commons/io/FileSystemTestCase.java
index 53a13d4..9656711 100644
--- a/src/test/java/org/apache/commons/io/FileSystemTestCase.java
+++ b/src/test/java/org/apache/commons/io/FileSystemTestCase.java
@@ -24,22 +24,22 @@ public class FileSystemTestCase {
 
     @Test
     public void testSorted() {
-        for (FileSystem fs : FileSystem.values()) {
-            char[] chars=fs.getIllegalFileNameChars();
-            for (int i=0; i < chars.length - 1; i++) {
-                Assert.assertTrue(fs.name(), chars[i] < chars[i+1]);
+        for (final FileSystem fs : FileSystem.values()) {
+            final char[] chars = fs.getIllegalFileNameChars();
+            for (int i = 0; i < chars.length - 1; i++) {
+                Assert.assertTrue(fs.name(), chars[i] < chars[i + 1]);
             }
         }
-    }    
+    }
 
     @Test
     public void testToLegalFileNameWindows() {
-        FileSystem fs = FileSystem.WINDOWS;
-        char replacement = '-';
+        final FileSystem fs = FileSystem.WINDOWS;
+        final char replacement = '-';
         for (char i = 0; i < 32; i++) {
             Assert.assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
         }
-        char[] illegal = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?', '*'
};
+        final char[] illegal = new char[] { '<', '>', ':', '"', '/', '\\', '|', '?',
'*' };
         for (char i = 0; i < illegal.length; i++) {
             Assert.assertEquals(replacement, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
         }
@@ -56,20 +56,33 @@ public class FileSystemTestCase {
 
     @Test
     public void testIsLegalName() {
-        for (FileSystem fs : FileSystem.values()) {
+        for (final FileSystem fs : FileSystem.values()) {
             Assert.assertFalse(fs.name(), fs.isLegalFileName("")); // Empty is always illegal
             Assert.assertFalse(fs.name(), fs.isLegalFileName(null)); // null is always illegal
             Assert.assertFalse(fs.name(), fs.isLegalFileName("\0")); // Assume NUL is always
illegal
             Assert.assertTrue(fs.name(), fs.isLegalFileName("0")); // Assume simple name
always legal
+            for (final String candidate : fs.getReservedFileNames()) {
+                // Reserved file names are not legal
+                Assert.assertFalse(fs.isLegalFileName(candidate));
+            }
+        }
+    }
+
+    @Test
+    public void testIsReservedFileName() {
+        for (final FileSystem fs : FileSystem.values()) {
+            for (final String candidate : fs.getReservedFileNames()) {
+                Assert.assertTrue(fs.isReservedFileName(candidate));
+            }
         }
     }
 
     @Test
     public void testReplacementWithNUL() {
-        for (FileSystem fs : FileSystem.values()) {
+        for (final FileSystem fs : FileSystem.values()) {
             try {
                 fs.toLegalFileName("Test", '\0'); // Assume NUL is always illegal
-            } catch (IllegalArgumentException iae) {
+            } catch (final IllegalArgumentException iae) {
                 Assert.assertTrue(iae.getMessage(), iae.getMessage().startsWith("The replacement
character '\\0'"));
             }
         }


Mime
View raw message