< prev index next >

src/java.base/windows/classes/java/io/WinNTFileSystem.java

Print this page

 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.io;
 27 
 28 import java.nio.file.InvalidPathException;
 29 import java.nio.file.Path;
 30 import java.util.BitSet;
 31 import java.util.Locale;
 32 import java.util.Properties;
 33 import jdk.internal.misc.Blocker;
 34 import sun.security.action.GetPropertyAction;
 35 
 36 /**
 37  * Unicode-aware FileSystem for Windows NT/2000.
 38  *
 39  * @author Konstantin Kladko
 40  * @since 1.4
 41  */
 42 final class WinNTFileSystem extends FileSystem {
 43 
 44     private static final String LONG_PATH_PREFIX = "\\\\?\\";
 45 
 46     private final char slash;
 47     private final char altSlash;
 48     private final char semicolon;
 49     private final String userDir;
 50 
 51     // Whether to enable alternative data streams (ADS) by suppressing
 52     // checking the path for invalid characters, in particular ":".
 53     // By default, ADS support is enabled and will be disabled if and

474         assert !path.startsWith(LONG_PATH_PREFIX);
475 
476         // If path is a drive letter only then skip canonicalization
477         int len = path.length();
478         if ((len == 2) &&
479             (isLetter(path.charAt(0))) &&
480             (path.charAt(1) == ':')) {
481             char c = path.charAt(0);
482             if ((c >= 'A') && (c <= 'Z'))
483                 return path;
484             return "" + ((char) (c-32)) + ':';
485         } else if ((len == 3) &&
486                    (isLetter(path.charAt(0))) &&
487                    (path.charAt(1) == ':') &&
488                    (path.charAt(2) == '\\')) {
489             char c = path.charAt(0);
490             if ((c >= 'A') && (c <= 'Z'))
491                 return path;
492             return "" + ((char) (c-32)) + ':' + '\\';
493         }
494         long comp = Blocker.begin();
495         try {
496             return canonicalize0(path);
497         } finally {
498             Blocker.end(comp);
499         }
500     }
501 
502     private native String canonicalize0(String path)
503             throws IOException;
504 
505 
506     /* -- Attribute accessors -- */
507 
508     @Override
509     public int getBooleanAttributes(File f) {
510         long comp = Blocker.begin();
511         try {
512             return getBooleanAttributes0(f);
513         } finally {
514             Blocker.end(comp);
515         }
516     }
517     private native int getBooleanAttributes0(File f);
518 
519     @Override
520     public boolean checkAccess(File f, int access) {
521         long comp = Blocker.begin();
522         try {
523             return checkAccess0(f, access);
524         } finally {
525             Blocker.end(comp);
526         }
527     }
528     private native boolean checkAccess0(File f, int access);
529 
530     @Override
531     public long getLastModifiedTime(File f) {
532         long comp = Blocker.begin();
533         try {
534             return getLastModifiedTime0(f);
535         } finally {
536             Blocker.end(comp);
537         }
538     }
539     private native long getLastModifiedTime0(File f);
540 
541     @Override
542     public long getLength(File f) {
543         long comp = Blocker.begin();
544         try {
545             return getLength0(f);
546         } finally {
547             Blocker.end(comp);
548         }
549     }
550     private native long getLength0(File f);
551 
552     @Override
553     public boolean setPermission(File f, int access, boolean enable, boolean owneronly) {
554         long comp = Blocker.begin();
555         try {
556             return setPermission0(f, access, enable, owneronly);
557         } finally {
558             Blocker.end(comp);
559         }
560     }
561     private native boolean setPermission0(File f, int access, boolean enable, boolean owneronly);
562 
563     /* -- File operations -- */
564 
565     @Override
566     public boolean createFileExclusively(String path) throws IOException {
567         long comp = Blocker.begin();
568         try {
569             return createFileExclusively0(path);
570         } finally {
571             Blocker.end(comp);
572         }
573     }
574     private native boolean createFileExclusively0(String path) throws IOException;
575 
576     @Override
577     public String[] list(File f) {
578         long comp = Blocker.begin();
579         try {
580             return list0(f);
581         } finally {
582             Blocker.end(comp);
583         }
584     }
585     private native String[] list0(File f);
586 
587     @Override
588     public boolean createDirectory(File f) {
589         long comp = Blocker.begin();
590         try {
591             return createDirectory0(f);
592         } finally {
593             Blocker.end(comp);
594         }
595     }
596     private native boolean createDirectory0(File f);
597 
598     @Override
599     public boolean setLastModifiedTime(File f, long time) {
600         long comp = Blocker.begin();
601         try {
602             return setLastModifiedTime0(f, time);
603         } finally {
604             Blocker.end(comp);
605         }
606     }
607     private native boolean setLastModifiedTime0(File f, long time);
608 
609     @Override
610     public boolean setReadOnly(File f) {
611         long comp = Blocker.begin();
612         try {
613             return setReadOnly0(f);
614         } finally {
615             Blocker.end(comp);
616         }
617     }
618     private native boolean setReadOnly0(File f);
619 
620     @Override
621     public boolean delete(File f) {
622         long comp = Blocker.begin();
623         try {
624             return delete0(f);
625         } finally {
626             Blocker.end(comp);
627         }
628     }
629     private native boolean delete0(File f);
630 
631     @Override
632     public boolean rename(File f1, File f2) {
633         long comp = Blocker.begin();
634         try {
635             return rename0(f1, f2);
636         } finally {
637             Blocker.end(comp);
638         }
639     }
640     private native boolean rename0(File f1, File f2);
641 
642     /* -- Filesystem interface -- */
643 
644     @Override
645     public File[] listRoots() {
646         return BitSet
647             .valueOf(new long[] {listRoots0()})
648             .stream()
649             .mapToObj(i -> new File((char)('A' + i) + ":" + slash))
650             .filter(f -> access(f.getPath()))
651             .toArray(File[]::new);
652     }
653     private static native int listRoots0();
654 
655     private boolean access(String path) {
656         try {
657             @SuppressWarnings("removal")
658             SecurityManager security = System.getSecurityManager();

 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.io;
 27 
 28 import java.nio.file.InvalidPathException;
 29 import java.nio.file.Path;
 30 import java.util.BitSet;
 31 import java.util.Locale;
 32 import java.util.Properties;

 33 import sun.security.action.GetPropertyAction;
 34 
 35 /**
 36  * Unicode-aware FileSystem for Windows NT/2000.
 37  *
 38  * @author Konstantin Kladko
 39  * @since 1.4
 40  */
 41 final class WinNTFileSystem extends FileSystem {
 42 
 43     private static final String LONG_PATH_PREFIX = "\\\\?\\";
 44 
 45     private final char slash;
 46     private final char altSlash;
 47     private final char semicolon;
 48     private final String userDir;
 49 
 50     // Whether to enable alternative data streams (ADS) by suppressing
 51     // checking the path for invalid characters, in particular ":".
 52     // By default, ADS support is enabled and will be disabled if and

473         assert !path.startsWith(LONG_PATH_PREFIX);
474 
475         // If path is a drive letter only then skip canonicalization
476         int len = path.length();
477         if ((len == 2) &&
478             (isLetter(path.charAt(0))) &&
479             (path.charAt(1) == ':')) {
480             char c = path.charAt(0);
481             if ((c >= 'A') && (c <= 'Z'))
482                 return path;
483             return "" + ((char) (c-32)) + ':';
484         } else if ((len == 3) &&
485                    (isLetter(path.charAt(0))) &&
486                    (path.charAt(1) == ':') &&
487                    (path.charAt(2) == '\\')) {
488             char c = path.charAt(0);
489             if ((c >= 'A') && (c <= 'Z'))
490                 return path;
491             return "" + ((char) (c-32)) + ':' + '\\';
492         }
493         return canonicalize0(path);





494     }
495 
496     private native String canonicalize0(String path)
497             throws IOException;
498 
499 
500     /* -- Attribute accessors -- */
501 
502     @Override
503     public int getBooleanAttributes(File f) {
504         return getBooleanAttributes0(f);





505     }
506     private native int getBooleanAttributes0(File f);
507 
508     @Override
509     public boolean checkAccess(File f, int access) {
510         return checkAccess0(f, access);





511     }
512     private native boolean checkAccess0(File f, int access);
513 
514     @Override
515     public long getLastModifiedTime(File f) {
516         return getLastModifiedTime0(f);





517     }
518     private native long getLastModifiedTime0(File f);
519 
520     @Override
521     public long getLength(File f) {
522         return getLength0(f);





523     }
524     private native long getLength0(File f);
525 
526     @Override
527     public boolean setPermission(File f, int access, boolean enable, boolean owneronly) {
528         return setPermission0(f, access, enable, owneronly);





529     }
530     private native boolean setPermission0(File f, int access, boolean enable, boolean owneronly);
531 
532     /* -- File operations -- */
533 
534     @Override
535     public boolean createFileExclusively(String path) throws IOException {
536         return createFileExclusively0(path);





537     }
538     private native boolean createFileExclusively0(String path) throws IOException;
539 
540     @Override
541     public String[] list(File f) {
542         return list0(f);





543     }
544     private native String[] list0(File f);
545 
546     @Override
547     public boolean createDirectory(File f) {
548         return createDirectory0(f);





549     }
550     private native boolean createDirectory0(File f);
551 
552     @Override
553     public boolean setLastModifiedTime(File f, long time) {
554         return setLastModifiedTime0(f, time);





555     }
556     private native boolean setLastModifiedTime0(File f, long time);
557 
558     @Override
559     public boolean setReadOnly(File f) {
560         return setReadOnly0(f);





561     }
562     private native boolean setReadOnly0(File f);
563 
564     @Override
565     public boolean delete(File f) {
566         return delete0(f);





567     }
568     private native boolean delete0(File f);
569 
570     @Override
571     public boolean rename(File f1, File f2) {
572         return rename0(f1, f2);





573     }
574     private native boolean rename0(File f1, File f2);
575 
576     /* -- Filesystem interface -- */
577 
578     @Override
579     public File[] listRoots() {
580         return BitSet
581             .valueOf(new long[] {listRoots0()})
582             .stream()
583             .mapToObj(i -> new File((char)('A' + i) + ":" + slash))
584             .filter(f -> access(f.getPath()))
585             .toArray(File[]::new);
586     }
587     private static native int listRoots0();
588 
589     private boolean access(String path) {
590         try {
591             @SuppressWarnings("removal")
592             SecurityManager security = System.getSecurityManager();
< prev index next >