jdk/src/solaris/classes/sun/nio/fs/UnixFileAttributes.java
changeset 2057 3acf8e5e2ca0
child 3065 452aaa2899fc
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package sun.nio.fs;
       
    27 
       
    28 import java.nio.file.attribute.*;
       
    29 import java.util.concurrent.TimeUnit;
       
    30 import java.util.Set;
       
    31 import java.util.HashSet;
       
    32 
       
    33 /**
       
    34  * Unix implementation of PosixFileAttributes.
       
    35  */
       
    36 
       
    37 class UnixFileAttributes
       
    38     implements PosixFileAttributes
       
    39 {
       
    40     private int     st_mode;
       
    41     private long    st_ino;
       
    42     private long    st_dev;
       
    43     private long    st_rdev;
       
    44     private int     st_nlink;
       
    45     private int     st_uid;
       
    46     private int     st_gid;
       
    47     private long    st_size;
       
    48     private long    st_atime;
       
    49     private long    st_mtime;
       
    50     private long    st_ctime;
       
    51 
       
    52     // created lazily
       
    53     private volatile UserPrincipal owner;
       
    54     private volatile GroupPrincipal group;
       
    55     private volatile UnixFileKey key;
       
    56 
       
    57     private UnixFileAttributes() {
       
    58     }
       
    59 
       
    60     // get the UnixFileAttributes for a given file
       
    61     static UnixFileAttributes get(UnixPath path, boolean followLinks)
       
    62         throws UnixException
       
    63     {
       
    64         UnixFileAttributes attrs = new UnixFileAttributes();
       
    65         if (followLinks) {
       
    66             UnixNativeDispatcher.stat(path, attrs);
       
    67         } else {
       
    68             UnixNativeDispatcher.lstat(path, attrs);
       
    69         }
       
    70         return attrs;
       
    71     }
       
    72 
       
    73     // get the UnixFileAttributes for an open file
       
    74     static UnixFileAttributes get(int fd) throws UnixException {
       
    75         UnixFileAttributes attrs = new UnixFileAttributes();
       
    76         UnixNativeDispatcher.fstat(fd, attrs);
       
    77         return attrs;
       
    78     }
       
    79 
       
    80     // get the UnixFileAttributes for a given file, relative to open directory
       
    81     static UnixFileAttributes get(int dfd, UnixPath path, boolean followLinks)
       
    82         throws UnixException
       
    83     {
       
    84         UnixFileAttributes attrs = new UnixFileAttributes();
       
    85         int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW;
       
    86         UnixNativeDispatcher.fstatat(dfd, path.asByteArray(), flag, attrs);
       
    87         return attrs;
       
    88     }
       
    89 
       
    90     // package-private
       
    91     boolean isSameFile(UnixFileAttributes attrs) {
       
    92         return ((st_ino == attrs.st_ino) && (st_dev == attrs.st_dev));
       
    93     }
       
    94 
       
    95     // package-private
       
    96     int mode()  { return st_mode; }
       
    97     long ino()  { return st_ino; }
       
    98     long dev()  { return st_dev; }
       
    99     long rdev() { return st_rdev; }
       
   100     int uid()   { return st_uid; }
       
   101     int gid()   { return st_gid; }
       
   102     long ctime() { return st_ctime; }
       
   103 
       
   104     boolean isDevice() {
       
   105         int type = st_mode & UnixConstants.S_IFMT;
       
   106         return (type == UnixConstants.S_IFCHR ||
       
   107                 type == UnixConstants.S_IFBLK  ||
       
   108                 type == UnixConstants.S_IFIFO);
       
   109     }
       
   110 
       
   111     @Override
       
   112     public long lastModifiedTime() {
       
   113         return st_mtime;
       
   114     }
       
   115 
       
   116     @Override
       
   117     public long lastAccessTime() {
       
   118         return st_atime;
       
   119     }
       
   120 
       
   121     @Override
       
   122     public long creationTime() {
       
   123         return -1L;
       
   124     }
       
   125 
       
   126     @Override
       
   127     public TimeUnit resolution() {
       
   128         return TimeUnit.MILLISECONDS;
       
   129     }
       
   130 
       
   131     @Override
       
   132     public boolean isRegularFile() {
       
   133        return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFREG);
       
   134     }
       
   135 
       
   136     @Override
       
   137     public boolean isDirectory() {
       
   138         return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFDIR);
       
   139     }
       
   140 
       
   141     @Override
       
   142     public boolean isSymbolicLink() {
       
   143         return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFLNK);
       
   144     }
       
   145 
       
   146     @Override
       
   147     public boolean isOther() {
       
   148         int type = st_mode & UnixConstants.S_IFMT;
       
   149         return (type != UnixConstants.S_IFREG &&
       
   150                 type != UnixConstants.S_IFDIR &&
       
   151                 type != UnixConstants.S_IFLNK);
       
   152     }
       
   153 
       
   154     @Override
       
   155     public long size() {
       
   156         return st_size;
       
   157     }
       
   158 
       
   159     @Override
       
   160     public int linkCount() {
       
   161         return st_nlink;
       
   162     }
       
   163 
       
   164     @Override
       
   165     public UnixFileKey fileKey() {
       
   166         if (key == null) {
       
   167             synchronized (this) {
       
   168                 if (key == null) {
       
   169                     key = new UnixFileKey(st_dev, st_ino);
       
   170                 }
       
   171             }
       
   172         }
       
   173         return key;
       
   174     }
       
   175 
       
   176     @Override
       
   177     public UserPrincipal owner() {
       
   178         if (owner == null) {
       
   179             synchronized (this) {
       
   180                 if (owner == null) {
       
   181                     owner = UnixUserPrincipals.fromUid(st_uid);
       
   182                 }
       
   183             }
       
   184         }
       
   185         return owner;
       
   186     }
       
   187 
       
   188     @Override
       
   189     public GroupPrincipal group() {
       
   190         if (group == null) {
       
   191             synchronized (this) {
       
   192                 if (group == null) {
       
   193                     group = UnixUserPrincipals.fromGid(st_gid);
       
   194                 }
       
   195             }
       
   196         }
       
   197         return group;
       
   198     }
       
   199 
       
   200     @Override
       
   201     public Set<PosixFilePermission> permissions() {
       
   202         int bits = (st_mode & UnixConstants.S_IAMB);
       
   203         HashSet<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
       
   204 
       
   205         if ((bits & UnixConstants.S_IRUSR) > 0)
       
   206             perms.add(PosixFilePermission.OWNER_READ);
       
   207         if ((bits & UnixConstants.S_IWUSR) > 0)
       
   208             perms.add(PosixFilePermission.OWNER_WRITE);
       
   209         if ((bits & UnixConstants.S_IXUSR) > 0)
       
   210             perms.add(PosixFilePermission.OWNER_EXECUTE);
       
   211 
       
   212         if ((bits & UnixConstants.S_IRGRP) > 0)
       
   213             perms.add(PosixFilePermission.GROUP_READ);
       
   214         if ((bits & UnixConstants.S_IWGRP) > 0)
       
   215             perms.add(PosixFilePermission.GROUP_WRITE);
       
   216         if ((bits & UnixConstants.S_IXGRP) > 0)
       
   217             perms.add(PosixFilePermission.GROUP_EXECUTE);
       
   218 
       
   219         if ((bits & UnixConstants.S_IROTH) > 0)
       
   220             perms.add(PosixFilePermission.OTHERS_READ);
       
   221         if ((bits & UnixConstants.S_IWOTH) > 0)
       
   222             perms.add(PosixFilePermission.OTHERS_WRITE);
       
   223         if ((bits & UnixConstants.S_IXOTH) > 0)
       
   224             perms.add(PosixFilePermission.OTHERS_EXECUTE);
       
   225 
       
   226         return perms;
       
   227     }
       
   228 
       
   229     // wrap this object with BasicFileAttributes object to prevent leaking of
       
   230     // user information
       
   231     BasicFileAttributes asBasicFileAttributes() {
       
   232         return UnixAsBasicFileAttributes.wrap(this);
       
   233     }
       
   234 
       
   235     // unwrap BasicFileAttributes to get the underlying UnixFileAttributes
       
   236     // object. Returns null is not wrapped.
       
   237     static UnixFileAttributes toUnixFileAttributes(BasicFileAttributes attrs) {
       
   238         if (attrs instanceof UnixFileAttributes)
       
   239             return (UnixFileAttributes)attrs;
       
   240         if (attrs instanceof UnixAsBasicFileAttributes) {
       
   241             return ((UnixAsBasicFileAttributes)attrs).unwrap();
       
   242         }
       
   243         return null;
       
   244     }
       
   245 
       
   246     // wrap a UnixFileAttributes object as a BasicFileAttributes
       
   247     private static class UnixAsBasicFileAttributes implements BasicFileAttributes {
       
   248         private final UnixFileAttributes attrs;
       
   249 
       
   250         private UnixAsBasicFileAttributes(UnixFileAttributes attrs) {
       
   251             this.attrs = attrs;
       
   252         }
       
   253 
       
   254         static UnixAsBasicFileAttributes wrap(UnixFileAttributes attrs) {
       
   255             return new UnixAsBasicFileAttributes(attrs);
       
   256         }
       
   257 
       
   258         UnixFileAttributes unwrap() {
       
   259             return attrs;
       
   260         }
       
   261 
       
   262         @Override
       
   263         public long lastModifiedTime() {
       
   264             return attrs.lastModifiedTime();
       
   265         }
       
   266         @Override
       
   267         public long lastAccessTime() {
       
   268             return attrs.lastAccessTime();
       
   269         }
       
   270         @Override
       
   271         public long creationTime() {
       
   272             return attrs.creationTime();
       
   273         }
       
   274         @Override
       
   275         public TimeUnit resolution() {
       
   276             return attrs.resolution();
       
   277         }
       
   278         @Override
       
   279         public boolean isRegularFile() {
       
   280             return attrs.isRegularFile();
       
   281         }
       
   282         @Override
       
   283         public boolean isDirectory() {
       
   284             return attrs.isDirectory();
       
   285         }
       
   286         @Override
       
   287         public boolean isSymbolicLink() {
       
   288             return attrs.isSymbolicLink();
       
   289         }
       
   290         @Override
       
   291         public boolean isOther() {
       
   292             return attrs.isOther();
       
   293         }
       
   294         @Override
       
   295         public long size() {
       
   296             return attrs.size();
       
   297         }
       
   298         @Override
       
   299         public int linkCount() {
       
   300             return attrs.linkCount();
       
   301         }
       
   302         @Override
       
   303         public Object fileKey() {
       
   304             return attrs.fileKey();
       
   305         }
       
   306     }
       
   307 }