Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/UnixFileAttributeViews.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.*;28import java.nio.file.attribute.*;29import java.util.*;30import java.util.concurrent.TimeUnit;31import java.io.IOException;3233import static sun.nio.fs.UnixNativeDispatcher.*;3435class UnixFileAttributeViews {3637static class Basic extends AbstractBasicFileAttributeView {38protected final UnixPath file;39protected final boolean followLinks;4041Basic(UnixPath file, boolean followLinks) {42this.file = file;43this.followLinks = followLinks;44}4546@Override47public BasicFileAttributes readAttributes() throws IOException {48file.checkRead();49try {50UnixFileAttributes attrs =51UnixFileAttributes.get(file, followLinks);52return attrs.asBasicFileAttributes();53} catch (UnixException x) {54x.rethrowAsIOException(file);55return null; // keep compiler happy56}57}5859@Override60public void setTimes(FileTime lastModifiedTime,61FileTime lastAccessTime,62FileTime createTime) throws IOException63{64// null => don't change65if (lastModifiedTime == null && lastAccessTime == null) {66// no effect67return;68}6970// permission check71file.checkWrite();7273int fd = file.openForAttributeAccess(followLinks);74try {75// assert followLinks || !UnixFileAttributes.get(fd).isSymbolicLink();7677// if not changing both attributes then need existing attributes78if (lastModifiedTime == null || lastAccessTime == null) {79try {80UnixFileAttributes attrs = UnixFileAttributes.get(fd);81if (lastModifiedTime == null)82lastModifiedTime = attrs.lastModifiedTime();83if (lastAccessTime == null)84lastAccessTime = attrs.lastAccessTime();85} catch (UnixException x) {86x.rethrowAsIOException(file);87}88}8990// uptime times91long modValue = lastModifiedTime.to(TimeUnit.MICROSECONDS);92long accessValue= lastAccessTime.to(TimeUnit.MICROSECONDS);9394boolean retry = false;95try {96if (futimesSupported()) {97futimes(fd, accessValue, modValue);98} else {99utimes(file, accessValue, modValue);100}101} catch (UnixException x) {102// if futimes/utimes fails with EINVAL and one/both of the times is103// negative then we adjust the value to the epoch and retry.104if (x.errno() == UnixConstants.EINVAL &&105(modValue < 0L || accessValue < 0L)) {106retry = true;107} else {108x.rethrowAsIOException(file);109}110}111if (retry) {112if (modValue < 0L) modValue = 0L;113if (accessValue < 0L) accessValue= 0L;114try {115if (futimesSupported()) {116futimes(fd, accessValue, modValue);117} else {118utimes(file, accessValue, modValue);119}120} catch (UnixException x) {121x.rethrowAsIOException(file);122}123}124} finally {125close(fd);126}127}128}129130private static class Posix extends Basic implements PosixFileAttributeView {131private static final String PERMISSIONS_NAME = "permissions";132private static final String OWNER_NAME = "owner";133private static final String GROUP_NAME = "group";134135// the names of the posix attributes (incudes basic)136static final Set<String> posixAttributeNames =137Util.newSet(basicAttributeNames, PERMISSIONS_NAME, OWNER_NAME, GROUP_NAME);138139Posix(UnixPath file, boolean followLinks) {140super(file, followLinks);141}142143final void checkReadExtended() {144SecurityManager sm = System.getSecurityManager();145if (sm != null) {146file.checkRead();147sm.checkPermission(new RuntimePermission("accessUserInformation"));148}149}150151final void checkWriteExtended() {152SecurityManager sm = System.getSecurityManager();153if (sm != null) {154file.checkWrite();155sm.checkPermission(new RuntimePermission("accessUserInformation"));156}157}158159@Override160public String name() {161return "posix";162}163164@Override165@SuppressWarnings("unchecked")166public void setAttribute(String attribute, Object value)167throws IOException168{169if (attribute.equals(PERMISSIONS_NAME)) {170setPermissions((Set<PosixFilePermission>)value);171return;172}173if (attribute.equals(OWNER_NAME)) {174setOwner((UserPrincipal)value);175return;176}177if (attribute.equals(GROUP_NAME)) {178setGroup((GroupPrincipal)value);179return;180}181super.setAttribute(attribute, value);182}183184/**185* Invoked by readAttributes or sub-classes to add all matching posix186* attributes to the builder187*/188final void addRequestedPosixAttributes(PosixFileAttributes attrs,189AttributesBuilder builder)190{191addRequestedBasicAttributes(attrs, builder);192if (builder.match(PERMISSIONS_NAME))193builder.add(PERMISSIONS_NAME, attrs.permissions());194if (builder.match(OWNER_NAME))195builder.add(OWNER_NAME, attrs.owner());196if (builder.match(GROUP_NAME))197builder.add(GROUP_NAME, attrs.group());198}199200@Override201public Map<String,Object> readAttributes(String[] requested)202throws IOException203{204AttributesBuilder builder =205AttributesBuilder.create(posixAttributeNames, requested);206PosixFileAttributes attrs = readAttributes();207addRequestedPosixAttributes(attrs, builder);208return builder.unmodifiableMap();209}210211@Override212public UnixFileAttributes readAttributes() throws IOException {213checkReadExtended();214try {215return UnixFileAttributes.get(file, followLinks);216} catch (UnixException x) {217x.rethrowAsIOException(file);218return null; // keep compiler happy219}220}221222// chmod223final void setMode(int mode) throws IOException {224checkWriteExtended();225try {226if (followLinks) {227chmod(file, mode);228} else {229int fd = file.openForAttributeAccess(false);230try {231fchmod(fd, mode);232} finally {233close(fd);234}235}236} catch (UnixException x) {237x.rethrowAsIOException(file);238}239}240241// chown242final void setOwners(int uid, int gid) throws IOException {243checkWriteExtended();244try {245if (followLinks) {246chown(file, uid, gid);247} else {248lchown(file, uid, gid);249}250} catch (UnixException x) {251x.rethrowAsIOException(file);252}253}254255@Override256public void setPermissions(Set<PosixFilePermission> perms)257throws IOException258{259setMode(UnixFileModeAttribute.toUnixMode(perms));260}261262@Override263public void setOwner(UserPrincipal owner)264throws IOException265{266if (owner == null)267throw new NullPointerException("'owner' is null");268if (!(owner instanceof UnixUserPrincipals.User))269throw new ProviderMismatchException();270if (owner instanceof UnixUserPrincipals.Group)271throw new IOException("'owner' parameter can't be a group");272int uid = ((UnixUserPrincipals.User)owner).uid();273setOwners(uid, -1);274}275276@Override277public UserPrincipal getOwner() throws IOException {278return readAttributes().owner();279}280281@Override282public void setGroup(GroupPrincipal group)283throws IOException284{285if (group == null)286throw new NullPointerException("'owner' is null");287if (!(group instanceof UnixUserPrincipals.Group))288throw new ProviderMismatchException();289int gid = ((UnixUserPrincipals.Group)group).gid();290setOwners(-1, gid);291}292}293294private static class Unix extends Posix {295private static final String MODE_NAME = "mode";296private static final String INO_NAME = "ino";297private static final String DEV_NAME = "dev";298private static final String RDEV_NAME = "rdev";299private static final String NLINK_NAME = "nlink";300private static final String UID_NAME = "uid";301private static final String GID_NAME = "gid";302private static final String CTIME_NAME = "ctime";303304// the names of the unix attributes (including posix)305static final Set<String> unixAttributeNames =306Util.newSet(posixAttributeNames,307MODE_NAME, INO_NAME, DEV_NAME, RDEV_NAME,308NLINK_NAME, UID_NAME, GID_NAME, CTIME_NAME);309310Unix(UnixPath file, boolean followLinks) {311super(file, followLinks);312}313314@Override315public String name() {316return "unix";317}318319@Override320public void setAttribute(String attribute, Object value)321throws IOException322{323if (attribute.equals(MODE_NAME)) {324setMode((Integer)value);325return;326}327if (attribute.equals(UID_NAME)) {328setOwners((Integer)value, -1);329return;330}331if (attribute.equals(GID_NAME)) {332setOwners(-1, (Integer)value);333return;334}335super.setAttribute(attribute, value);336}337338@Override339public Map<String,Object> readAttributes(String[] requested)340throws IOException341{342AttributesBuilder builder =343AttributesBuilder.create(unixAttributeNames, requested);344UnixFileAttributes attrs = readAttributes();345addRequestedPosixAttributes(attrs, builder);346if (builder.match(MODE_NAME))347builder.add(MODE_NAME, attrs.mode());348if (builder.match(INO_NAME))349builder.add(INO_NAME, attrs.ino());350if (builder.match(DEV_NAME))351builder.add(DEV_NAME, attrs.dev());352if (builder.match(RDEV_NAME))353builder.add(RDEV_NAME, attrs.rdev());354if (builder.match(NLINK_NAME))355builder.add(NLINK_NAME, attrs.nlink());356if (builder.match(UID_NAME))357builder.add(UID_NAME, attrs.uid());358if (builder.match(GID_NAME))359builder.add(GID_NAME, attrs.gid());360if (builder.match(CTIME_NAME))361builder.add(CTIME_NAME, attrs.ctime());362return builder.unmodifiableMap();363}364}365366static Basic createBasicView(UnixPath file, boolean followLinks) {367return new Basic(file, followLinks);368}369370static Posix createPosixView(UnixPath file, boolean followLinks) {371return new Posix(file, followLinks);372}373374static Unix createUnixView(UnixPath file, boolean followLinks) {375return new Unix(file, followLinks);376}377378static FileOwnerAttributeViewImpl createOwnerView(UnixPath file, boolean followLinks) {379return new FileOwnerAttributeViewImpl(createPosixView(file, followLinks));380}381}382383384