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.io.IOException;
893N/Aimport java.util.*;
893N/Aimport sun.misc.Unsafe;
893N/A
893N/Aimport static sun.nio.fs.UnixConstants.*;
893N/Aimport static sun.nio.fs.LinuxNativeDispatcher.*;
893N/A
893N/A/**
893N/A * Linux implementation of UserDefinedFileAttributeView using extended attributes.
893N/A */
893N/A
893N/Aclass LinuxUserDefinedFileAttributeView
893N/A extends AbstractUserDefinedFileAttributeView
893N/A{
893N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
893N/A
893N/A // namespace for extended user attributes
893N/A private static final String USER_NAMESPACE = "user.";
893N/A
893N/A // maximum bytes in extended attribute name (includes namespace)
893N/A private static final int XATTR_NAME_MAX = 255;
893N/A
893N/A private byte[] nameAsBytes(UnixPath file, String name) throws IOException {
893N/A if (name == null)
893N/A throw new NullPointerException("'name' is null");
893N/A name = USER_NAMESPACE + name;
893N/A byte[] bytes = name.getBytes();
893N/A if (bytes.length > XATTR_NAME_MAX) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "'" + name + "' is too big");
893N/A }
893N/A return bytes;
893N/A }
893N/A
893N/A // Parses buffer as array of NULL-terminated C strings.
893N/A private List<String> asList(long address, int size) {
3471N/A List<String> list = new ArrayList<>();
893N/A int start = 0;
893N/A int pos = 0;
893N/A while (pos < size) {
893N/A if (unsafe.getByte(address + pos) == 0) {
893N/A int len = pos - start;
893N/A byte[] value = new byte[len];
893N/A unsafe.copyMemory(null, address+start, value,
893N/A Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
893N/A String s = new String(value);
893N/A if (s.startsWith(USER_NAMESPACE)) {
893N/A s = s.substring(USER_NAMESPACE.length());
893N/A list.add(s);
893N/A }
893N/A start = pos + 1;
893N/A }
893N/A pos++;
893N/A }
893N/A return list;
893N/A }
893N/A
893N/A private final UnixPath file;
893N/A private final boolean followLinks;
893N/A
893N/A LinuxUserDefinedFileAttributeView(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 NativeBuffer buffer = null;
893N/A try {
893N/A int size = 1024;
893N/A buffer = NativeBuffers.getNativeBuffer(size);
893N/A for (;;) {
893N/A try {
893N/A int n = flistxattr(fd, buffer.address(), size);
893N/A List<String> list = asList(buffer.address(), n);
893N/A return Collections.unmodifiableList(list);
893N/A } catch (UnixException x) {
893N/A // allocate larger buffer if required
893N/A if (x.errno() == ERANGE && size < 32*1024) {
893N/A buffer.release();
893N/A size *= 2;
893N/A buffer = null;
893N/A buffer = NativeBuffers.getNativeBuffer(size);
893N/A continue;
893N/A }
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 }
893N/A } finally {
893N/A if (buffer != null)
893N/A buffer.release();
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 // fgetxattr returns size if called with size==0
893N/A return fgetxattr(fd, nameAsBytes(file,name), 0L, 0);
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 } 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 if (dst.isReadOnly())
893N/A throw new IllegalArgumentException("Read-only buffer");
893N/A int pos = dst.position();
893N/A int lim = dst.limit();
893N/A assert (pos <= lim);
893N/A int rem = (pos <= lim ? lim - pos : 0);
893N/A
893N/A NativeBuffer nb;
893N/A long address;
893N/A if (dst instanceof sun.nio.ch.DirectBuffer) {
893N/A nb = null;
893N/A address = ((sun.nio.ch.DirectBuffer)dst).address() + pos;
893N/A } else {
893N/A // substitute with native buffer
893N/A nb = NativeBuffers.getNativeBuffer(rem);
893N/A address = nb.address();
893N/A }
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A try {
893N/A int n = fgetxattr(fd, nameAsBytes(file,name), address, rem);
893N/A
893N/A // if remaining is zero then fgetxattr returns the size
893N/A if (rem == 0) {
893N/A if (n > 0)
893N/A throw new UnixException(ERANGE);
893N/A return 0;
893N/A }
893N/A
893N/A // copy from buffer into backing array if necessary
893N/A if (nb != null) {
893N/A int off = dst.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
893N/A unsafe.copyMemory(null, address, dst.array(), off, n);
893N/A }
893N/A dst.position(pos + n);
893N/A return n;
893N/A } catch (UnixException x) {
893N/A String msg = (x.errno() == ERANGE) ?
893N/A "Insufficient space in buffer" : x.getMessage();
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Error reading extended attribute '" + name + "': " + msg);
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A } finally {
893N/A if (nb != null)
893N/A nb.release();
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 pos = src.position();
893N/A int lim = src.limit();
893N/A assert (pos <= lim);
893N/A int rem = (pos <= lim ? lim - pos : 0);
893N/A
893N/A NativeBuffer nb;
893N/A long address;
893N/A if (src instanceof sun.nio.ch.DirectBuffer) {
893N/A nb = null;
893N/A address = ((sun.nio.ch.DirectBuffer)src).address() + pos;
893N/A } else {
893N/A // substitute with native buffer
893N/A nb = NativeBuffers.getNativeBuffer(rem);
893N/A address = nb.address();
893N/A
893N/A if (src.hasArray()) {
893N/A // copy from backing array into buffer
893N/A int off = src.arrayOffset() + pos + Unsafe.ARRAY_BYTE_BASE_OFFSET;
893N/A unsafe.copyMemory(src.array(), off, null, address, rem);
893N/A } else {
893N/A // backing array not accessible so transfer via temporary array
893N/A byte[] tmp = new byte[rem];
893N/A src.get(tmp);
893N/A src.position(pos); // reset position as write may fail
893N/A unsafe.copyMemory(tmp, Unsafe.ARRAY_BYTE_BASE_OFFSET, null,
893N/A address, rem);
893N/A }
893N/A }
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A try {
893N/A fsetxattr(fd, nameAsBytes(file,name), address, rem);
893N/A src.position(pos + rem);
893N/A return rem;
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Error writing extended attribute '" + name + "': " +
893N/A x.getMessage());
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A } finally {
893N/A if (nb != null)
893N/A nb.release();
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 fremovexattr(fd, nameAsBytes(file,name));
893N/A } catch (UnixException x) {
5195N/A throw new FileSystemException(file.getPathForExceptionMessage(),
893N/A null, "Unable to delete extended attribute '" + name + "': " + 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 NativeBuffer buffer = null;
893N/A try {
893N/A
893N/A // call flistxattr to get list of extended attributes.
893N/A int size = 1024;
893N/A buffer = NativeBuffers.getNativeBuffer(size);
893N/A for (;;) {
893N/A try {
893N/A size = flistxattr(ofd, buffer.address(), size);
893N/A break;
893N/A } catch (UnixException x) {
893N/A // allocate larger buffer if required
893N/A if (x.errno() == ERANGE && size < 32*1024) {
893N/A buffer.release();
893N/A size *= 2;
893N/A buffer = null;
893N/A buffer = NativeBuffers.getNativeBuffer(size);
893N/A continue;
893N/A }
893N/A
893N/A // unable to get list of attributes
893N/A return;
893N/A }
893N/A }
893N/A
893N/A // parse buffer as array of NULL-terminated C strings.
893N/A long address = buffer.address();
893N/A int start = 0;
893N/A int pos = 0;
893N/A while (pos < size) {
893N/A if (unsafe.getByte(address + pos) == 0) {
893N/A // extract attribute name and copy attribute to target.
893N/A // FIXME: We can avoid needless copying by using address+pos
893N/A // as the address of the name.
893N/A int len = pos - start;
893N/A byte[] name = new byte[len];
893N/A unsafe.copyMemory(null, address+start, name,
893N/A Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
893N/A try {
893N/A copyExtendedAttribute(ofd, name, nfd);
893N/A } catch (UnixException ignore) {
893N/A // ignore
893N/A }
893N/A start = pos + 1;
893N/A }
893N/A pos++;
893N/A }
893N/A
893N/A } finally {
893N/A if (buffer != null)
893N/A buffer.release();
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 int size = fgetxattr(ofd, name, 0L, 0);
893N/A NativeBuffer buffer = NativeBuffers.getNativeBuffer(size);
893N/A try {
893N/A long address = buffer.address();
893N/A size = fgetxattr(ofd, name, address, size);
893N/A fsetxattr(nfd, name, address, size);
893N/A } finally {
893N/A buffer.release();
893N/A }
893N/A }
893N/A}