893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
893N/Aimport java.nio.file.*;
893N/Aimport java.nio.file.attribute.*;
893N/Aimport java.util.*;
893N/Aimport java.util.concurrent.TimeUnit;
893N/Aimport java.io.IOException;
893N/A
893N/Aimport static sun.nio.fs.UnixNativeDispatcher.*;
893N/A
893N/Aclass UnixFileAttributeViews {
893N/A
893N/A static class Basic extends AbstractBasicFileAttributeView {
893N/A protected final UnixPath file;
893N/A protected final boolean followLinks;
893N/A
893N/A Basic(UnixPath file, boolean followLinks) {
893N/A this.file = file;
893N/A this.followLinks = followLinks;
893N/A }
893N/A
893N/A @Override
893N/A public BasicFileAttributes readAttributes() throws IOException {
893N/A file.checkRead();
893N/A try {
893N/A UnixFileAttributes attrs =
893N/A UnixFileAttributes.get(file, followLinks);
893N/A return attrs.asBasicFileAttributes();
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return null; // keep compiler happy
893N/A }
893N/A }
1319N/A
893N/A @Override
1319N/A public void setTimes(FileTime lastModifiedTime,
1319N/A FileTime lastAccessTime,
1319N/A FileTime createTime) throws IOException
893N/A {
893N/A // null => don't change
893N/A if (lastModifiedTime == null && lastAccessTime == null) {
893N/A // no effect
893N/A return;
893N/A }
893N/A
1319N/A // permission check
893N/A file.checkWrite();
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A // if not changing both attributes then need existing attributes
893N/A if (lastModifiedTime == null || lastAccessTime == null) {
893N/A try {
1319N/A UnixFileAttributes attrs = UnixFileAttributes.get(fd);
1319N/A if (lastModifiedTime == null)
1319N/A lastModifiedTime = attrs.lastModifiedTime();
1319N/A if (lastAccessTime == null)
1319N/A lastAccessTime = attrs.lastAccessTime();
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A }
893N/A
1319N/A // uptime times
1319N/A long modValue = lastModifiedTime.to(TimeUnit.MICROSECONDS);
1319N/A long accessValue= lastAccessTime.to(TimeUnit.MICROSECONDS);
1319N/A
1319N/A boolean retry = false;
1319N/A try {
1319N/A futimes(fd, accessValue, modValue);
1319N/A } catch (UnixException x) {
1319N/A // if futimes fails with EINVAL and one/both of the times is
1319N/A // negative then we adjust the value to the epoch and retry.
1319N/A if (x.errno() == UnixConstants.EINVAL &&
1319N/A (modValue < 0L || accessValue < 0L)) {
1319N/A retry = true;
893N/A } else {
1319N/A x.rethrowAsIOException(file);
893N/A }
893N/A }
1319N/A if (retry) {
1319N/A if (modValue < 0L) modValue = 0L;
1319N/A if (accessValue < 0L) accessValue= 0L;
1319N/A try {
1319N/A futimes(fd, accessValue, modValue);
1319N/A } catch (UnixException x) {
1319N/A x.rethrowAsIOException(file);
893N/A }
893N/A }
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A }
893N/A
893N/A private static class Posix extends Basic implements PosixFileAttributeView {
893N/A private static final String PERMISSIONS_NAME = "permissions";
893N/A private static final String OWNER_NAME = "owner";
893N/A private static final String GROUP_NAME = "group";
893N/A
3779N/A // the names of the posix attributes (incudes basic)
3779N/A static final Set<String> posixAttributeNames =
3779N/A Util.newSet(basicAttributeNames, PERMISSIONS_NAME, OWNER_NAME, GROUP_NAME);
3779N/A
893N/A Posix(UnixPath file, boolean followLinks) {
893N/A super(file, followLinks);
893N/A }
893N/A
893N/A final void checkReadExtended() {
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A file.checkRead();
893N/A sm.checkPermission(new RuntimePermission("accessUserInformation"));
893N/A }
893N/A }
893N/A
893N/A final void checkWriteExtended() {
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A file.checkWrite();
893N/A sm.checkPermission(new RuntimePermission("accessUserInformation"));
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return "posix";
893N/A }
893N/A
893N/A @Override
893N/A @SuppressWarnings("unchecked")
893N/A public void setAttribute(String attribute, Object value)
893N/A throws IOException
893N/A {
893N/A if (attribute.equals(PERMISSIONS_NAME)) {
893N/A setPermissions((Set<PosixFilePermission>)value);
893N/A return;
893N/A }
893N/A if (attribute.equals(OWNER_NAME)) {
893N/A setOwner((UserPrincipal)value);
893N/A return;
893N/A }
893N/A if (attribute.equals(GROUP_NAME)) {
893N/A setGroup((GroupPrincipal)value);
893N/A return;
893N/A }
893N/A super.setAttribute(attribute, value);
893N/A }
893N/A
893N/A /**
893N/A * Invoked by readAttributes or sub-classes to add all matching posix
893N/A * attributes to the builder
893N/A */
3779N/A final void addRequestedPosixAttributes(PosixFileAttributes attrs,
893N/A AttributesBuilder builder)
893N/A {
3779N/A addRequestedBasicAttributes(attrs, builder);
893N/A if (builder.match(PERMISSIONS_NAME))
893N/A builder.add(PERMISSIONS_NAME, attrs.permissions());
893N/A if (builder.match(OWNER_NAME))
893N/A builder.add(OWNER_NAME, attrs.owner());
893N/A if (builder.match(GROUP_NAME))
893N/A builder.add(GROUP_NAME, attrs.group());
893N/A }
893N/A
893N/A @Override
3779N/A public Map<String,Object> readAttributes(String[] requested)
893N/A throws IOException
893N/A {
3779N/A AttributesBuilder builder =
3779N/A AttributesBuilder.create(posixAttributeNames, requested);
893N/A PosixFileAttributes attrs = readAttributes();
3779N/A addRequestedPosixAttributes(attrs, builder);
893N/A return builder.unmodifiableMap();
893N/A }
893N/A
893N/A @Override
893N/A public UnixFileAttributes readAttributes() throws IOException {
893N/A checkReadExtended();
893N/A try {
893N/A return UnixFileAttributes.get(file, followLinks);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return null; // keep compiler happy
893N/A }
893N/A }
893N/A
893N/A // chmod
893N/A final void setMode(int mode) throws IOException {
893N/A checkWriteExtended();
893N/A try {
893N/A if (followLinks) {
893N/A chmod(file, mode);
893N/A } else {
893N/A int fd = file.openForAttributeAccess(false);
893N/A try {
893N/A fchmod(fd, mode);
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A }
893N/A
893N/A // chown
893N/A final void setOwners(int uid, int gid) throws IOException {
893N/A checkWriteExtended();
893N/A try {
893N/A if (followLinks) {
893N/A chown(file, uid, gid);
893N/A } else {
893N/A lchown(file, uid, gid);
893N/A }
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void setPermissions(Set<PosixFilePermission> perms)
893N/A throws IOException
893N/A {
893N/A setMode(UnixFileModeAttribute.toUnixMode(perms));
893N/A }
893N/A
893N/A @Override
893N/A public void setOwner(UserPrincipal owner)
893N/A throws IOException
893N/A {
893N/A if (owner == null)
893N/A throw new NullPointerException("'owner' is null");
893N/A if (!(owner instanceof UnixUserPrincipals.User))
893N/A throw new ProviderMismatchException();
893N/A if (owner instanceof UnixUserPrincipals.Group)
893N/A throw new IOException("'owner' parameter can't be a group");
893N/A int uid = ((UnixUserPrincipals.User)owner).uid();
893N/A setOwners(uid, -1);
893N/A }
893N/A
893N/A @Override
893N/A public UserPrincipal getOwner() throws IOException {
893N/A return readAttributes().owner();
893N/A }
893N/A
893N/A @Override
893N/A public void setGroup(GroupPrincipal group)
893N/A throws IOException
893N/A {
893N/A if (group == null)
893N/A throw new NullPointerException("'owner' is null");
893N/A if (!(group instanceof UnixUserPrincipals.Group))
893N/A throw new ProviderMismatchException();
893N/A int gid = ((UnixUserPrincipals.Group)group).gid();
893N/A setOwners(-1, gid);
893N/A }
893N/A }
893N/A
893N/A private static class Unix extends Posix {
893N/A private static final String MODE_NAME = "mode";
893N/A private static final String INO_NAME = "ino";
893N/A private static final String DEV_NAME = "dev";
893N/A private static final String RDEV_NAME = "rdev";
1319N/A private static final String NLINK_NAME = "nlink";
893N/A private static final String UID_NAME = "uid";
893N/A private static final String GID_NAME = "gid";
893N/A private static final String CTIME_NAME = "ctime";
893N/A
3779N/A // the names of the unix attributes (including posix)
3779N/A static final Set<String> unixAttributeNames =
3779N/A Util.newSet(posixAttributeNames,
3779N/A MODE_NAME, INO_NAME, DEV_NAME, RDEV_NAME,
3779N/A NLINK_NAME, UID_NAME, GID_NAME, CTIME_NAME);
3779N/A
893N/A Unix(UnixPath file, boolean followLinks) {
893N/A super(file, followLinks);
893N/A }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return "unix";
893N/A }
893N/A
893N/A @Override
893N/A public void setAttribute(String attribute, Object value)
893N/A throws IOException
893N/A {
893N/A if (attribute.equals(MODE_NAME)) {
893N/A setMode((Integer)value);
893N/A return;
893N/A }
893N/A if (attribute.equals(UID_NAME)) {
893N/A setOwners((Integer)value, -1);
893N/A return;
893N/A }
893N/A if (attribute.equals(GID_NAME)) {
893N/A setOwners(-1, (Integer)value);
893N/A return;
893N/A }
893N/A super.setAttribute(attribute, value);
893N/A }
893N/A
893N/A @Override
3779N/A public Map<String,Object> readAttributes(String[] requested)
893N/A throws IOException
893N/A {
3779N/A AttributesBuilder builder =
3779N/A AttributesBuilder.create(unixAttributeNames, requested);
893N/A UnixFileAttributes attrs = readAttributes();
3779N/A addRequestedPosixAttributes(attrs, builder);
893N/A if (builder.match(MODE_NAME))
893N/A builder.add(MODE_NAME, attrs.mode());
893N/A if (builder.match(INO_NAME))
893N/A builder.add(INO_NAME, attrs.ino());
893N/A if (builder.match(DEV_NAME))
893N/A builder.add(DEV_NAME, attrs.dev());
893N/A if (builder.match(RDEV_NAME))
893N/A builder.add(RDEV_NAME, attrs.rdev());
1319N/A if (builder.match(NLINK_NAME))
1319N/A builder.add(NLINK_NAME, attrs.nlink());
893N/A if (builder.match(UID_NAME))
893N/A builder.add(UID_NAME, attrs.uid());
893N/A if (builder.match(GID_NAME))
893N/A builder.add(GID_NAME, attrs.gid());
893N/A if (builder.match(CTIME_NAME))
893N/A builder.add(CTIME_NAME, attrs.ctime());
893N/A return builder.unmodifiableMap();
893N/A }
893N/A }
893N/A
1319N/A static Basic createBasicView(UnixPath file, boolean followLinks) {
893N/A return new Basic(file, followLinks);
893N/A }
893N/A
1319N/A static Posix createPosixView(UnixPath file, boolean followLinks) {
893N/A return new Posix(file, followLinks);
893N/A }
893N/A
1319N/A static Unix createUnixView(UnixPath file, boolean followLinks) {
893N/A return new Unix(file, followLinks);
893N/A }
893N/A
1319N/A static FileOwnerAttributeViewImpl createOwnerView(UnixPath file, boolean followLinks) {
893N/A return new FileOwnerAttributeViewImpl(createPosixView(file, followLinks));
893N/A }
893N/A}