Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixFileAttributes.java
32288 views
/*1* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.nio.file.attribute.*;28import java.util.concurrent.TimeUnit;29import java.util.Set;30import java.util.HashSet;3132/**33* Unix implementation of PosixFileAttributes.34*/3536class UnixFileAttributes37implements PosixFileAttributes38{39private int st_mode;40private long st_ino;41private long st_dev;42private long st_rdev;43private int st_nlink;44private int st_uid;45private int st_gid;46private long st_size;47private long st_atime_sec;48private long st_atime_nsec;49private long st_mtime_sec;50private long st_mtime_nsec;51private long st_ctime_sec;52private long st_ctime_nsec;53private long st_birthtime_sec;5455// created lazily56private volatile UserPrincipal owner;57private volatile GroupPrincipal group;58private volatile UnixFileKey key;5960private UnixFileAttributes() {61}6263// get the UnixFileAttributes for a given file64static UnixFileAttributes get(UnixPath path, boolean followLinks)65throws UnixException66{67UnixFileAttributes attrs = new UnixFileAttributes();68if (followLinks) {69UnixNativeDispatcher.stat(path, attrs);70} else {71UnixNativeDispatcher.lstat(path, attrs);72}73return attrs;74}7576// get the UnixFileAttributes for an open file77static UnixFileAttributes get(int fd) throws UnixException {78UnixFileAttributes attrs = new UnixFileAttributes();79UnixNativeDispatcher.fstat(fd, attrs);80return attrs;81}8283// get the UnixFileAttributes for a given file, relative to open directory84static UnixFileAttributes get(int dfd, UnixPath path, boolean followLinks)85throws UnixException86{87UnixFileAttributes attrs = new UnixFileAttributes();88int flag = (followLinks) ? 0 : UnixConstants.AT_SYMLINK_NOFOLLOW;89UnixNativeDispatcher.fstatat(dfd, path.asByteArray(), flag, attrs);90return attrs;91}9293// package-private94boolean isSameFile(UnixFileAttributes attrs) {95return ((st_ino == attrs.st_ino) && (st_dev == attrs.st_dev));96}9798// package-private99int mode() { return st_mode; }100long ino() { return st_ino; }101long dev() { return st_dev; }102long rdev() { return st_rdev; }103int nlink() { return st_nlink; }104int uid() { return st_uid; }105int gid() { return st_gid; }106107private static FileTime toFileTime(long sec, long nsec) {108if (nsec == 0) {109return FileTime.from(sec, TimeUnit.SECONDS);110} else {111// truncate to microseconds to avoid overflow with timestamps112// way out into the future. We can re-visit this if FileTime113// is updated to define a from(secs,nsecs) method.114long micro = sec*1000000L + nsec/1000L;115return FileTime.from(micro, TimeUnit.MICROSECONDS);116}117}118119FileTime ctime() {120return toFileTime(st_ctime_sec, st_ctime_nsec);121}122123boolean isDevice() {124int type = st_mode & UnixConstants.S_IFMT;125return (type == UnixConstants.S_IFCHR ||126type == UnixConstants.S_IFBLK ||127type == UnixConstants.S_IFIFO);128}129130@Override131public FileTime lastModifiedTime() {132return toFileTime(st_mtime_sec, st_mtime_nsec);133}134135@Override136public FileTime lastAccessTime() {137return toFileTime(st_atime_sec, st_atime_nsec);138}139140@Override141public FileTime creationTime() {142if (UnixNativeDispatcher.birthtimeSupported()) {143return FileTime.from(st_birthtime_sec, TimeUnit.SECONDS);144} else {145// return last modified when birth time not supported146return lastModifiedTime();147}148}149150@Override151public boolean isRegularFile() {152return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFREG);153}154155@Override156public boolean isDirectory() {157return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFDIR);158}159160@Override161public boolean isSymbolicLink() {162return ((st_mode & UnixConstants.S_IFMT) == UnixConstants.S_IFLNK);163}164165@Override166public boolean isOther() {167int type = st_mode & UnixConstants.S_IFMT;168return (type != UnixConstants.S_IFREG &&169type != UnixConstants.S_IFDIR &&170type != UnixConstants.S_IFLNK);171}172173@Override174public long size() {175return st_size;176}177178@Override179public UnixFileKey fileKey() {180if (key == null) {181synchronized (this) {182if (key == null) {183key = new UnixFileKey(st_dev, st_ino);184}185}186}187return key;188}189190@Override191public UserPrincipal owner() {192if (owner == null) {193synchronized (this) {194if (owner == null) {195owner = UnixUserPrincipals.fromUid(st_uid);196}197}198}199return owner;200}201202@Override203public GroupPrincipal group() {204if (group == null) {205synchronized (this) {206if (group == null) {207group = UnixUserPrincipals.fromGid(st_gid);208}209}210}211return group;212}213214@Override215public Set<PosixFilePermission> permissions() {216int bits = (st_mode & UnixConstants.S_IAMB);217HashSet<PosixFilePermission> perms = new HashSet<>();218219if ((bits & UnixConstants.S_IRUSR) > 0)220perms.add(PosixFilePermission.OWNER_READ);221if ((bits & UnixConstants.S_IWUSR) > 0)222perms.add(PosixFilePermission.OWNER_WRITE);223if ((bits & UnixConstants.S_IXUSR) > 0)224perms.add(PosixFilePermission.OWNER_EXECUTE);225226if ((bits & UnixConstants.S_IRGRP) > 0)227perms.add(PosixFilePermission.GROUP_READ);228if ((bits & UnixConstants.S_IWGRP) > 0)229perms.add(PosixFilePermission.GROUP_WRITE);230if ((bits & UnixConstants.S_IXGRP) > 0)231perms.add(PosixFilePermission.GROUP_EXECUTE);232233if ((bits & UnixConstants.S_IROTH) > 0)234perms.add(PosixFilePermission.OTHERS_READ);235if ((bits & UnixConstants.S_IWOTH) > 0)236perms.add(PosixFilePermission.OTHERS_WRITE);237if ((bits & UnixConstants.S_IXOTH) > 0)238perms.add(PosixFilePermission.OTHERS_EXECUTE);239240return perms;241}242243// wrap this object with BasicFileAttributes object to prevent leaking of244// user information245BasicFileAttributes asBasicFileAttributes() {246return UnixAsBasicFileAttributes.wrap(this);247}248249// unwrap BasicFileAttributes to get the underlying UnixFileAttributes250// object. Returns null is not wrapped.251static UnixFileAttributes toUnixFileAttributes(BasicFileAttributes attrs) {252if (attrs instanceof UnixFileAttributes)253return (UnixFileAttributes)attrs;254if (attrs instanceof UnixAsBasicFileAttributes) {255return ((UnixAsBasicFileAttributes)attrs).unwrap();256}257return null;258}259260// wrap a UnixFileAttributes object as a BasicFileAttributes261private static class UnixAsBasicFileAttributes implements BasicFileAttributes {262private final UnixFileAttributes attrs;263264private UnixAsBasicFileAttributes(UnixFileAttributes attrs) {265this.attrs = attrs;266}267268static UnixAsBasicFileAttributes wrap(UnixFileAttributes attrs) {269return new UnixAsBasicFileAttributes(attrs);270}271272UnixFileAttributes unwrap() {273return attrs;274}275276@Override277public FileTime lastModifiedTime() {278return attrs.lastModifiedTime();279}280@Override281public FileTime lastAccessTime() {282return attrs.lastAccessTime();283}284@Override285public FileTime creationTime() {286return attrs.creationTime();287}288@Override289public boolean isRegularFile() {290return attrs.isRegularFile();291}292@Override293public boolean isDirectory() {294return attrs.isDirectory();295}296@Override297public boolean isSymbolicLink() {298return attrs.isSymbolicLink();299}300@Override301public boolean isOther() {302return attrs.isOther();303}304@Override305public long size() {306return attrs.size();307}308@Override309public Object fileKey() {310return attrs.fileKey();311}312}313}314315316