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.nio.channels.SeekableByteChannel;
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/Aimport static sun.nio.fs.UnixConstants.*;
893N/A
893N/A/**
893N/A * Unix implementation of SecureDirectoryStream.
893N/A */
893N/A
893N/Aclass UnixSecureDirectoryStream
3471N/A implements SecureDirectoryStream<Path>
893N/A{
893N/A private final UnixDirectoryStream ds;
893N/A private final int dfd;
893N/A
893N/A UnixSecureDirectoryStream(UnixPath dir,
893N/A long dp,
893N/A int dfd,
893N/A DirectoryStream.Filter<? super Path> filter)
893N/A {
893N/A this.ds = new UnixDirectoryStream(dir, dp, filter);
893N/A this.dfd = dfd;
893N/A }
893N/A
893N/A @Override
893N/A public void close()
893N/A throws IOException
893N/A {
893N/A ds.writeLock().lock();
893N/A try {
893N/A if (ds.closeImpl()) {
893N/A UnixNativeDispatcher.close(dfd);
893N/A }
893N/A } finally {
893N/A ds.writeLock().unlock();
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public Iterator<Path> iterator() {
893N/A return ds.iterator(this);
893N/A }
893N/A
893N/A private UnixPath getName(Path obj) {
893N/A if (obj == null)
893N/A throw new NullPointerException();
893N/A if (!(obj instanceof UnixPath))
893N/A throw new ProviderMismatchException();
893N/A return (UnixPath)obj;
893N/A }
893N/A
893N/A /**
893N/A * Opens sub-directory in this directory
893N/A */
893N/A @Override
1319N/A public SecureDirectoryStream<Path> newDirectoryStream(Path obj,
1319N/A LinkOption... options)
893N/A throws IOException
893N/A {
893N/A UnixPath file = getName(obj);
893N/A UnixPath child = ds.directory().resolve(file);
3899N/A boolean followLinks = Util.followLinks(options);
893N/A
893N/A // permission check using name resolved against original path of directory
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A child.checkRead();
893N/A }
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A // open directory and create new secure directory stream
893N/A int newdfd1 = -1;
893N/A int newdfd2 = -1;
893N/A long ptr = 0L;
893N/A try {
893N/A int flags = O_RDONLY;
893N/A if (!followLinks)
893N/A flags |= O_NOFOLLOW;
893N/A newdfd1 = openat(dfd, file.asByteArray(), flags , 0);
893N/A newdfd2 = dup(newdfd1);
893N/A ptr = fdopendir(newdfd1);
893N/A } catch (UnixException x) {
893N/A if (newdfd1 != -1)
893N/A UnixNativeDispatcher.close(newdfd1);
893N/A if (newdfd2 != -1)
893N/A UnixNativeDispatcher.close(newdfd2);
893N/A if (x.errno() == UnixConstants.ENOTDIR)
893N/A throw new NotDirectoryException(file.toString());
893N/A x.rethrowAsIOException(file);
893N/A }
1319N/A return new UnixSecureDirectoryStream(child, ptr, newdfd2, null);
893N/A } finally {
893N/A ds.readLock().unlock();
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Opens file in this directory
893N/A */
893N/A @Override
893N/A public SeekableByteChannel newByteChannel(Path obj,
893N/A Set<? extends OpenOption> options,
893N/A FileAttribute<?>... attrs)
893N/A throws IOException
893N/A {
893N/A UnixPath file = getName(obj);
893N/A
893N/A int mode = UnixFileModeAttribute
893N/A .toUnixMode(UnixFileModeAttribute.ALL_READWRITE, attrs);
893N/A
893N/A // path for permission check
893N/A String pathToCheck = ds.directory().resolve(file).getPathForPermissionCheck();
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A try {
893N/A return UnixChannelFactory.newFileChannel(dfd, file, pathToCheck, options, mode);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return null; // keep compiler happy
893N/A }
893N/A } finally {
893N/A ds.readLock().unlock();
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Deletes file/directory in this directory. Works in a race-free manner
893N/A * when invoked with flags.
893N/A */
2748N/A private void implDelete(Path obj, boolean haveFlags, int flags)
893N/A throws IOException
893N/A {
893N/A UnixPath file = getName(obj);
893N/A
893N/A // permission check using name resolved against original path of directory
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A ds.directory().resolve(file).checkDelete();
893N/A }
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A if (!haveFlags) {
893N/A // need file attribute to know if file is directory. This creates
893N/A // a race in that the file may be replaced by a directory or a
893N/A // directory replaced by a file between the time we query the
893N/A // file type and unlink it.
893N/A UnixFileAttributes attrs = null;
893N/A try {
893N/A attrs = UnixFileAttributes.get(dfd, file, false);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A flags = (attrs.isDirectory()) ? AT_REMOVEDIR : 0;
893N/A }
893N/A
893N/A try {
893N/A unlinkat(dfd, file.asByteArray(), flags);
893N/A } catch (UnixException x) {
893N/A if ((flags & AT_REMOVEDIR) != 0) {
893N/A if (x.errno() == EEXIST || x.errno() == ENOTEMPTY) {
893N/A throw new DirectoryNotEmptyException(null);
893N/A }
893N/A }
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A } finally {
893N/A ds.readLock().unlock();
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void deleteFile(Path file) throws IOException {
893N/A implDelete(file, true, 0);
893N/A }
893N/A
893N/A @Override
893N/A public void deleteDirectory(Path dir) throws IOException {
893N/A implDelete(dir, true, AT_REMOVEDIR);
893N/A }
893N/A
893N/A /**
893N/A * Rename/move file in this directory to another (open) directory
893N/A */
893N/A @Override
1319N/A public void move(Path fromObj, SecureDirectoryStream<Path> dir, Path toObj)
893N/A throws IOException
893N/A {
893N/A UnixPath from = getName(fromObj);
893N/A UnixPath to = getName(toObj);
893N/A if (dir == null)
893N/A throw new NullPointerException();
893N/A if (!(dir instanceof UnixSecureDirectoryStream))
893N/A throw new ProviderMismatchException();
893N/A UnixSecureDirectoryStream that = (UnixSecureDirectoryStream)dir;
893N/A
893N/A // permission check
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A this.ds.directory().resolve(from).checkWrite();
893N/A that.ds.directory().resolve(to).checkWrite();
893N/A }
893N/A
893N/A // lock ordering doesn't matter
893N/A this.ds.readLock().lock();
893N/A try {
893N/A that.ds.readLock().lock();
893N/A try {
893N/A if (!this.ds.isOpen() || !that.ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A try {
893N/A renameat(this.dfd, from.asByteArray(), that.dfd, to.asByteArray());
893N/A } catch (UnixException x) {
893N/A if (x.errno() == EXDEV) {
893N/A throw new AtomicMoveNotSupportedException(
893N/A from.toString(), to.toString(), x.errorString());
893N/A }
893N/A x.rethrowAsIOException(from, to);
893N/A }
893N/A } finally {
893N/A that.ds.readLock().unlock();
893N/A }
893N/A } finally {
893N/A this.ds.readLock().unlock();
893N/A }
893N/A }
893N/A
893N/A @SuppressWarnings("unchecked")
893N/A private <V extends FileAttributeView> V getFileAttributeViewImpl(UnixPath file,
893N/A Class<V> type,
893N/A boolean followLinks)
893N/A {
893N/A if (type == null)
893N/A throw new NullPointerException();
893N/A Class<?> c = type;
893N/A if (c == BasicFileAttributeView.class) {
893N/A return (V) new BasicFileAttributeViewImpl(file, followLinks);
893N/A }
893N/A if (c == PosixFileAttributeView.class || c == FileOwnerAttributeView.class) {
893N/A return (V) new PosixFileAttributeViewImpl(file, followLinks);
893N/A }
893N/A // TBD - should also support AclFileAttributeView
893N/A return (V) null;
893N/A }
893N/A
893N/A /**
893N/A * Returns file attribute view bound to this directory
893N/A */
893N/A @Override
893N/A public <V extends FileAttributeView> V getFileAttributeView(Class<V> type) {
893N/A return getFileAttributeViewImpl(null, type, false);
893N/A }
893N/A
893N/A /**
893N/A * Returns file attribute view bound to dfd/filename.
893N/A */
893N/A @Override
893N/A public <V extends FileAttributeView> V getFileAttributeView(Path obj,
893N/A Class<V> type,
893N/A LinkOption... options)
893N/A {
893N/A UnixPath file = getName(obj);
3899N/A boolean followLinks = Util.followLinks(options);
893N/A return getFileAttributeViewImpl(file, type, followLinks);
893N/A }
893N/A
893N/A /**
893N/A * A BasicFileAttributeView implementation that using a dfd/name pair.
893N/A */
893N/A private class BasicFileAttributeViewImpl
1319N/A implements BasicFileAttributeView
893N/A {
893N/A final UnixPath file;
893N/A final boolean followLinks;
893N/A
893N/A BasicFileAttributeViewImpl(UnixPath file, boolean followLinks)
893N/A {
893N/A this.file = file;
893N/A this.followLinks = followLinks;
893N/A }
893N/A
893N/A int open() throws IOException {
893N/A int oflags = O_RDONLY;
893N/A if (!followLinks)
893N/A oflags |= O_NOFOLLOW;
893N/A try {
893N/A return openat(dfd, file.asByteArray(), oflags, 0);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return -1; // keep compiler happy
893N/A }
893N/A }
893N/A
893N/A private void checkWriteAccess() {
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
3471N/A if (file == null) {
3471N/A ds.directory().checkWrite();
3471N/A } else {
3471N/A ds.directory().resolve(file).checkWrite();
3471N/A }
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return "basic";
893N/A }
893N/A
893N/A @Override
893N/A public BasicFileAttributes readAttributes() throws IOException {
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A if (file == null) {
893N/A ds.directory().checkRead();
893N/A } else {
893N/A ds.directory().resolve(file).checkRead();
893N/A }
893N/A }
893N/A try {
893N/A UnixFileAttributes attrs = (file == null) ?
893N/A UnixFileAttributes.get(dfd) :
893N/A UnixFileAttributes.get(dfd, file, followLinks);
893N/A
893N/A // SECURITY: must return as BasicFileAttribute
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 } finally {
893N/A ds.readLock().unlock();
893N/A }
893N/A }
893N/A
893N/A @Override
1319N/A public void setTimes(FileTime lastModifiedTime,
1319N/A FileTime lastAccessTime,
1319N/A FileTime createTime) // ignore
893N/A throws IOException
893N/A {
893N/A checkWriteAccess();
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A int fd = (file == null) ? dfd : open();
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 }
1319N/A // update times
893N/A try {
1319N/A futimes(fd,
1319N/A lastAccessTime.to(TimeUnit.MICROSECONDS),
1319N/A lastModifiedTime.to(TimeUnit.MICROSECONDS));
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A }
893N/A } finally {
893N/A if (file != null)
893N/A UnixNativeDispatcher.close(fd);
893N/A }
893N/A } finally {
893N/A ds.readLock().unlock();
893N/A }
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * A PosixFileAttributeView implementation that using a dfd/name pair.
893N/A */
893N/A private class PosixFileAttributeViewImpl
893N/A extends BasicFileAttributeViewImpl implements PosixFileAttributeView
893N/A {
893N/A PosixFileAttributeViewImpl(UnixPath file, boolean followLinks) {
893N/A super(file, followLinks);
893N/A }
893N/A
893N/A private void checkWriteAndUserAccess() {
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A super.checkWriteAccess();
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 public PosixFileAttributes readAttributes() throws IOException {
893N/A SecurityManager sm = System.getSecurityManager();
893N/A if (sm != null) {
893N/A if (file == null)
893N/A ds.directory().checkRead();
893N/A else
893N/A ds.directory().resolve(file).checkRead();
893N/A sm.checkPermission(new RuntimePermission("accessUserInformation"));
893N/A }
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A try {
893N/A UnixFileAttributes attrs = (file == null) ?
893N/A UnixFileAttributes.get(dfd) :
893N/A UnixFileAttributes.get(dfd, file, followLinks);
893N/A return attrs;
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return null; // keep compiler happy
893N/A }
893N/A } finally {
893N/A ds.readLock().unlock();
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 // permission check
893N/A checkWriteAndUserAccess();
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A int fd = (file == null) ? dfd : open();
893N/A try {
893N/A fchmod(fd, UnixFileModeAttribute.toUnixMode(perms));
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A } finally {
893N/A if (file != null && fd >= 0)
893N/A UnixNativeDispatcher.close(fd);
893N/A }
893N/A } finally {
893N/A ds.readLock().unlock();
893N/A }
893N/A }
893N/A
893N/A private void setOwners(int uid, int gid) throws IOException {
893N/A // permission check
893N/A checkWriteAndUserAccess();
893N/A
893N/A ds.readLock().lock();
893N/A try {
893N/A if (!ds.isOpen())
893N/A throw new ClosedDirectoryStreamException();
893N/A
893N/A int fd = (file == null) ? dfd : open();
893N/A try {
893N/A fchown(fd, uid, gid);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A } finally {
893N/A if (file != null && fd >= 0)
893N/A UnixNativeDispatcher.close(fd);
893N/A }
893N/A } finally {
893N/A ds.readLock().unlock();
893N/A }
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 setOwner(UserPrincipal owner)
893N/A throws IOException
893N/A {
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 void setGroup(GroupPrincipal group)
893N/A throws IOException
893N/A {
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}