893N/A/*
5195N/A * Copyright (c) 2008, 2012, 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.ByteBuffer;
893N/Aimport java.nio.channels.FileChannel;
893N/Aimport java.io.IOException;
893N/Aimport java.util.*;
893N/A
893N/Aimport static sun.nio.fs.UnixNativeDispatcher.*;
893N/Aimport static sun.nio.fs.UnixConstants.*;
893N/Aimport static sun.nio.fs.SolarisConstants.*;
893N/A
893N/A/**
893N/A * Solaris emulation of NamedAttributeView using extended attributes.
893N/A */
893N/A
893N/Aclass SolarisUserDefinedFileAttributeView
893N/A extends AbstractUserDefinedFileAttributeView
893N/A{
893N/A private byte[] nameAsBytes(UnixPath file, String name) throws IOException {
893N/A byte[] bytes = name.getBytes();
893N/A // "", "." and ".." not allowed
893N/A if (bytes.length == 0 || bytes[0] == '.') {
893N/A if (bytes.length <= 1 ||
893N/A (bytes.length == 2 && bytes[1] == '.'))
893N/A {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "'" + name + "' is not a valid name");
893N/A }
893N/A }
893N/A return bytes;
893N/A }
893N/A
893N/A private final UnixPath file;
893N/A private final boolean followLinks;
893N/A
893N/A SolarisUserDefinedFileAttributeView(UnixPath file, boolean followLinks) {
893N/A this.file = file;
893N/A this.followLinks = followLinks;
893N/A }
893N/A
893N/A @Override
893N/A public List<String> list() throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), true, false);
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A try {
893N/A // open extended attribute directory
893N/A int dfd = openat(fd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
893N/A long dp;
893N/A try {
893N/A dp = fdopendir(dfd);
893N/A } catch (UnixException x) {
893N/A close(dfd);
893N/A throw x;
893N/A }
893N/A
893N/A // read list of extended attributes
3471N/A List<String> list = new ArrayList<>();
893N/A try {
893N/A byte[] name;
893N/A while ((name = readdir(dp)) != null) {
893N/A String s = new String(name);
893N/A if (!s.equals(".") && !s.equals(".."))
893N/A list.add(s);
893N/A }
893N/A } finally {
893N/A closedir(dp);
893N/A }
893N/A return Collections.unmodifiableList(list);
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Unable to get list of extended attributes: " +
893N/A x.getMessage());
893N/A }
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public int size(String name) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), true, false);
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A try {
893N/A // open attribute file
893N/A int afd = openat(fd, nameAsBytes(file,name), (O_RDONLY|O_XATTR), 0);
893N/A try {
893N/A // read attribute's attributes
893N/A UnixFileAttributes attrs = UnixFileAttributes.get(afd);
893N/A long size = attrs.size();
893N/A if (size > Integer.MAX_VALUE)
893N/A throw new ArithmeticException("Extended attribute value too large");
893N/A return (int)size;
893N/A } finally {
893N/A close(afd);
893N/A }
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Unable to get size of extended attribute '" + name +
893N/A "': " + x.getMessage());
893N/A }
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public int read(String name, ByteBuffer dst) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), true, false);
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A try {
893N/A // open attribute file
893N/A int afd = openat(fd, nameAsBytes(file,name), (O_RDONLY|O_XATTR), 0);
893N/A
893N/A // wrap with channel
5503N/A FileChannel fc = UnixChannelFactory.newFileChannel(afd, file.toString(), true, false);
893N/A
893N/A // read to EOF (nothing we can do if I/O error occurs)
893N/A try {
893N/A if (fc.size() > dst.remaining())
893N/A throw new IOException("Extended attribute file too large");
893N/A int total = 0;
893N/A while (dst.hasRemaining()) {
893N/A int n = fc.read(dst);
893N/A if (n < 0)
893N/A break;
893N/A total += n;
893N/A }
893N/A return total;
893N/A } finally {
893N/A fc.close();
893N/A }
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Unable to read extended attribute '" + name +
893N/A "': " + x.getMessage());
893N/A }
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public int write(String name, ByteBuffer src) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), false, true);
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A try {
893N/A // open/create attribute file
893N/A int afd = openat(fd, nameAsBytes(file,name),
893N/A (O_CREAT|O_WRONLY|O_TRUNC|O_XATTR),
893N/A UnixFileModeAttribute.ALL_PERMISSIONS);
893N/A
893N/A // wrap with channel
5503N/A FileChannel fc = UnixChannelFactory.newFileChannel(afd, file.toString(), false, true);
893N/A
893N/A // write value (nothing we can do if I/O error occurs)
893N/A try {
893N/A int rem = src.remaining();
893N/A while (src.hasRemaining()) {
893N/A fc.write(src);
893N/A }
893N/A return rem;
893N/A } finally {
893N/A fc.close();
893N/A }
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Unable to write extended attribute '" + name +
893N/A "': " + x.getMessage());
893N/A }
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void delete(String name) throws IOException {
893N/A if (System.getSecurityManager() != null)
893N/A checkAccess(file.getPathForPermissionCheck(), false, true);
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A int dfd = openat(fd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
893N/A try {
893N/A unlinkat(dfd, nameAsBytes(file,name), 0);
893N/A } finally {
893N/A close(dfd);
893N/A }
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Unable to delete extended attribute '" + name +
893N/A "': " + x.getMessage());
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Used by copyTo/moveTo to copy extended attributes from source to target.
893N/A *
893N/A * @param ofd
893N/A * file descriptor for source file
893N/A * @param nfd
893N/A * file descriptor for target file
893N/A */
893N/A static void copyExtendedAttributes(int ofd, int nfd) {
893N/A try {
893N/A // open extended attribute directory
893N/A int dfd = openat(ofd, ".".getBytes(), (O_RDONLY|O_XATTR), 0);
893N/A long dp = 0L;
893N/A try {
893N/A dp = fdopendir(dfd);
893N/A } catch (UnixException x) {
893N/A close(dfd);
893N/A throw x;
893N/A }
893N/A
893N/A // copy each extended attribute
893N/A try {
893N/A byte[] name;
893N/A while ((name = readdir(dp)) != null) {
893N/A // ignore "." and ".."
893N/A if (name[0] == '.') {
893N/A if (name.length == 1)
893N/A continue;
893N/A if (name.length == 2 && name[1] == '.')
893N/A continue;
893N/A }
893N/A copyExtendedAttribute(ofd, name, nfd);
893N/A }
893N/A } finally {
893N/A closedir(dp);
893N/A }
893N/A } catch (UnixException ignore) {
893N/A }
893N/A }
893N/A
893N/A private static void copyExtendedAttribute(int ofd, byte[] name, int nfd)
893N/A throws UnixException
893N/A {
893N/A // open source attribute file
893N/A int src = openat(ofd, name, (O_RDONLY|O_XATTR), 0);
893N/A try {
893N/A // create target attribute file
893N/A int dst = openat(nfd, name, (O_CREAT|O_WRONLY|O_TRUNC|O_XATTR),
893N/A UnixFileModeAttribute.ALL_PERMISSIONS);
893N/A try {
893N/A UnixCopyFile.transfer(dst, src, 0L);
893N/A } finally {
893N/A close(dst);
893N/A }
893N/A } finally {
893N/A close(src);
893N/A }
893N/A }
893N/A}