< prev index next >

src/java.base/windows/classes/sun/nio/fs/WindowsFileAttributes.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 sun.nio.fs;
  27 

  28 import java.nio.file.attribute.*;
  29 import java.util.concurrent.TimeUnit;
  30 import jdk.internal.misc.Unsafe;
  31 import sun.security.action.GetPropertyAction;
  32 
  33 import static sun.nio.fs.WindowsNativeDispatcher.*;
  34 import static sun.nio.fs.WindowsConstants.*;
  35 
  36 /**
  37  * Windows implementation of DosFileAttributes/BasicFileAttributes
  38  */
  39 
  40 class WindowsFileAttributes
  41     implements DosFileAttributes
  42 {
  43     private static final Unsafe unsafe = Unsafe.getUnsafe();
  44 
  45     /*
  46      * typedef struct _BY_HANDLE_FILE_INFORMATION {
  47      *     DWORD    dwFileAttributes;


  91      *   FILETIME ftCreationTime;
  92      *   FILETIME ftLastAccessTime;
  93      *   FILETIME ftLastWriteTime;
  94      *   DWORD nFileSizeHigh;
  95      *   DWORD nFileSizeLow;
  96      *   DWORD dwReserved0;
  97      *   DWORD dwReserved1;
  98      *   TCHAR cFileName[MAX_PATH];
  99      *   TCHAR cAlternateFileName[14];
 100      * } WIN32_FIND_DATA;
 101      */
 102     private static final short SIZEOF_FIND_DATA = 592;
 103     private static final short OFFSETOF_FIND_DATA_ATTRIBUTES = 0;
 104     private static final short OFFSETOF_FIND_DATA_CREATETIME = 4;
 105     private static final short OFFSETOF_FIND_DATA_LASTACCESSTIME = 12;
 106     private static final short OFFSETOF_FIND_DATA_LASTWRITETIME = 20;
 107     private static final short OFFSETOF_FIND_DATA_SIZEHIGH = 28;
 108     private static final short OFFSETOF_FIND_DATA_SIZELOW = 32;
 109     private static final short OFFSETOF_FIND_DATA_RESERVED0 = 36;
 110 
 111     // used to adjust values between Windows and java epoch
 112     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;







 113 
 114     // indicates if accurate metadata is required (interesting on NTFS only)
 115     private static final boolean ensureAccurateMetadata;
 116     static {
 117         String propValue = GetPropertyAction.privilegedGetProperty(
 118             "sun.nio.fs.ensureAccurateMetadata", "false");
 119         ensureAccurateMetadata = propValue.isEmpty() ? true : Boolean.parseBoolean(propValue);
 120     }
 121 
 122     // attributes
 123     private final int fileAttrs;
 124     private final long creationTime;
 125     private final long lastAccessTime;
 126     private final long lastWriteTime;
 127     private final long size;
 128     private final int reparseTag;
 129 
 130     // additional attributes when using GetFileInformationByHandle
 131     private final int volSerialNumber;
 132     private final int fileIndexHigh;
 133     private final int fileIndexLow;
 134 
 135     /**
 136      * Convert 64-bit value representing the number of 100-nanosecond intervals
 137      * since January 1, 1601 to a FileTime.
 138      */
 139     static FileTime toFileTime(long time) {
 140         // 100ns -> us
 141         time /= 10L;

 142         // adjust to java epoch
 143         time += WINDOWS_EPOCH_IN_MICROSECONDS;
 144         return FileTime.from(time, TimeUnit.MICROSECONDS);
 145     }
 146 
 147     /**
 148      * Convert FileTime to 64-bit value representing the number of 100-nanosecond
 149      * intervals since January 1, 1601.
 150      */
 151     static long toWindowsTime(FileTime time) {
 152         long value = time.to(TimeUnit.MICROSECONDS);
 153         // adjust to Windows epoch+= 11644473600000000L;
 154         value -= WINDOWS_EPOCH_IN_MICROSECONDS;
 155         // us -> 100ns
 156         value *= 10L;
 157         return value;
 158     }
 159 
 160     /**
 161      * Initialize a new instance of this class
 162      */
 163     private WindowsFileAttributes(int fileAttrs,
 164                                   long creationTime,
 165                                   long lastAccessTime,
 166                                   long lastWriteTime,
 167                                   long size,
 168                                   int reparseTag,
 169                                   int volSerialNumber,
 170                                   int fileIndexHigh,
 171                                   int fileIndexLow)
 172     {
 173         this.fileAttrs = fileAttrs;
 174         this.creationTime = creationTime;
 175         this.lastAccessTime = lastAccessTime;
 176         this.lastWriteTime = lastWriteTime;
 177         this.size = size;


   1 /*
   2  * Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  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 sun.nio.fs;
  27 
  28 import java.math.BigInteger;
  29 import java.nio.file.attribute.*;
  30 import java.util.concurrent.TimeUnit;
  31 import jdk.internal.misc.Unsafe;
  32 import sun.security.action.GetPropertyAction;
  33 
  34 import static sun.nio.fs.WindowsNativeDispatcher.*;
  35 import static sun.nio.fs.WindowsConstants.*;
  36 
  37 /**
  38  * Windows implementation of DosFileAttributes/BasicFileAttributes
  39  */
  40 
  41 class WindowsFileAttributes
  42     implements DosFileAttributes
  43 {
  44     private static final Unsafe unsafe = Unsafe.getUnsafe();
  45 
  46     /*
  47      * typedef struct _BY_HANDLE_FILE_INFORMATION {
  48      *     DWORD    dwFileAttributes;


  92      *   FILETIME ftCreationTime;
  93      *   FILETIME ftLastAccessTime;
  94      *   FILETIME ftLastWriteTime;
  95      *   DWORD nFileSizeHigh;
  96      *   DWORD nFileSizeLow;
  97      *   DWORD dwReserved0;
  98      *   DWORD dwReserved1;
  99      *   TCHAR cFileName[MAX_PATH];
 100      *   TCHAR cAlternateFileName[14];
 101      * } WIN32_FIND_DATA;
 102      */
 103     private static final short SIZEOF_FIND_DATA = 592;
 104     private static final short OFFSETOF_FIND_DATA_ATTRIBUTES = 0;
 105     private static final short OFFSETOF_FIND_DATA_CREATETIME = 4;
 106     private static final short OFFSETOF_FIND_DATA_LASTACCESSTIME = 12;
 107     private static final short OFFSETOF_FIND_DATA_LASTWRITETIME = 20;
 108     private static final short OFFSETOF_FIND_DATA_SIZEHIGH = 28;
 109     private static final short OFFSETOF_FIND_DATA_SIZELOW = 32;
 110     private static final short OFFSETOF_FIND_DATA_RESERVED0 = 36;
 111 
 112     // used to adjust values between Windows and java epochs
 113     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
 114     private static final BigInteger WINDOWS_EPOCH_IN_NANOSECONDS;
 115     static {
 116         // 1000*WINDOWS_EPOCH_IN_MICROSECONDS would overflow Long
 117         WINDOWS_EPOCH_IN_NANOSECONDS =
 118             BigInteger.valueOf(WINDOWS_EPOCH_IN_MICROSECONDS).
 119                 multiply(BigInteger.valueOf(1000L));
 120     };
 121 
 122     // indicates if accurate metadata is required (interesting on NTFS only)
 123     private static final boolean ensureAccurateMetadata;
 124     static {
 125         String propValue = GetPropertyAction.privilegedGetProperty(
 126             "sun.nio.fs.ensureAccurateMetadata", "false");
 127         ensureAccurateMetadata = propValue.isEmpty() ? true : Boolean.parseBoolean(propValue);
 128     }
 129 
 130     // attributes
 131     private final int fileAttrs;
 132     private final long creationTime;
 133     private final long lastAccessTime;
 134     private final long lastWriteTime;
 135     private final long size;
 136     private final int reparseTag;
 137 
 138     // additional attributes when using GetFileInformationByHandle
 139     private final int volSerialNumber;
 140     private final int fileIndexHigh;
 141     private final int fileIndexLow;
 142 
 143     /**
 144      * Convert 64-bit value representing the number of 100-nanosecond intervals
 145      * since January 1, 1601 to a FileTime.
 146      */
 147     static FileTime toFileTime(long time) {
 148         // 100ns -> ns
 149         BigInteger value = BigInteger.valueOf(time);
 150         value = value.multiply(BigInteger.valueOf(100L));
 151         // adjust to java epoch
 152         value = value.add(WINDOWS_EPOCH_IN_NANOSECONDS);
 153         return FileTime.from(value.longValueExact(), TimeUnit.NANOSECONDS);
 154     }
 155 
 156     /**
 157      * Convert FileTime to 64-bit value representing the number of
 158      * 100-nanosecond intervals since January 1, 1601.
 159      */
 160     static long toWindowsTime(FileTime time) {
 161         BigInteger value = BigInteger.valueOf(time.to(TimeUnit.NANOSECONDS));
 162         // adjust to Windows epoch+= 11644473600000000000L;
 163         value = value.subtract(WINDOWS_EPOCH_IN_NANOSECONDS);
 164         // ns -> 100ns
 165         value = value.divide(BigInteger.valueOf(100L));
 166         return value.longValueExact();
 167     }
 168 
 169     /**
 170      * Initialize a new instance of this class
 171      */
 172     private WindowsFileAttributes(int fileAttrs,
 173                                   long creationTime,
 174                                   long lastAccessTime,
 175                                   long lastWriteTime,
 176                                   long size,
 177                                   int reparseTag,
 178                                   int volSerialNumber,
 179                                   int fileIndexHigh,
 180                                   int fileIndexLow)
 181     {
 182         this.fileAttrs = fileAttrs;
 183         this.creationTime = creationTime;
 184         this.lastAccessTime = lastAccessTime;
 185         this.lastWriteTime = lastWriteTime;
 186         this.size = size;


< prev index next >