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.attribute.*;
1319N/Aimport java.util.Map;
3779N/Aimport java.util.Set;
893N/Aimport java.io.IOException;
893N/Aimport sun.misc.Unsafe;
893N/A
893N/Aimport static sun.nio.fs.UnixNativeDispatcher.*;
893N/Aimport static sun.nio.fs.UnixConstants.*;
893N/A
893N/A/**
893N/A * Linux implementation of DosFileAttributeView for use on file systems such
893N/A * as ext3 that have extended attributes enabled and SAMBA configured to store
893N/A * DOS attributes.
893N/A */
893N/A
893N/Aclass LinuxDosFileAttributeView
893N/A extends UnixFileAttributeViews.Basic implements DosFileAttributeView
893N/A{
893N/A private static final Unsafe unsafe = Unsafe.getUnsafe();
893N/A
893N/A private static final String READONLY_NAME = "readonly";
893N/A private static final String ARCHIVE_NAME = "archive";
893N/A private static final String SYSTEM_NAME = "system";
893N/A private static final String HIDDEN_NAME = "hidden";
893N/A
893N/A private static final String DOS_XATTR_NAME = "user.DOSATTRIB";
893N/A private static final byte[] DOS_XATTR_NAME_AS_BYTES = DOS_XATTR_NAME.getBytes();
893N/A
893N/A private static final int DOS_XATTR_READONLY = 0x01;
893N/A private static final int DOS_XATTR_HIDDEN = 0x02;
893N/A private static final int DOS_XATTR_SYSTEM = 0x04;
893N/A private static final int DOS_XATTR_ARCHIVE = 0x20;
893N/A
3779N/A // the names of the DOS attributes (includes basic)
3779N/A private static final Set<String> dosAttributeNames =
3779N/A Util.newSet(basicAttributeNames, READONLY_NAME, ARCHIVE_NAME, SYSTEM_NAME, HIDDEN_NAME);
3779N/A
893N/A LinuxDosFileAttributeView(UnixPath file, boolean followLinks) {
893N/A super(file, followLinks);
893N/A }
893N/A
893N/A @Override
893N/A public String name() {
893N/A return "dos";
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(READONLY_NAME)) {
893N/A setReadOnly((Boolean)value);
893N/A return;
893N/A }
893N/A if (attribute.equals(ARCHIVE_NAME)) {
893N/A setArchive((Boolean)value);
893N/A return;
893N/A }
893N/A if (attribute.equals(SYSTEM_NAME)) {
893N/A setSystem((Boolean)value);
893N/A return;
893N/A }
893N/A if (attribute.equals(HIDDEN_NAME)) {
893N/A setHidden((Boolean)value);
893N/A return;
893N/A }
893N/A super.setAttribute(attribute, value);
893N/A }
893N/A
893N/A @Override
3471N/A public Map<String,Object> readAttributes(String[] attributes)
893N/A throws IOException
893N/A {
3779N/A AttributesBuilder builder =
3779N/A AttributesBuilder.create(dosAttributeNames, attributes);
893N/A DosFileAttributes attrs = readAttributes();
3779N/A addRequestedBasicAttributes(attrs, builder);
893N/A if (builder.match(READONLY_NAME))
893N/A builder.add(READONLY_NAME, attrs.isReadOnly());
893N/A if (builder.match(ARCHIVE_NAME))
893N/A builder.add(ARCHIVE_NAME, attrs.isArchive());
893N/A if (builder.match(SYSTEM_NAME))
893N/A builder.add(SYSTEM_NAME, attrs.isSystem());
893N/A if (builder.match(HIDDEN_NAME))
893N/A builder.add(HIDDEN_NAME, attrs.isHidden());
893N/A return builder.unmodifiableMap();
893N/A }
893N/A
893N/A @Override
893N/A public DosFileAttributes readAttributes() throws IOException {
893N/A file.checkRead();
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A final UnixFileAttributes attrs = UnixFileAttributes.get(fd);
893N/A final int dosAttribute = getDosAttribute(fd);
893N/A
893N/A return new DosFileAttributes() {
893N/A @Override
1319N/A public FileTime lastModifiedTime() {
893N/A return attrs.lastModifiedTime();
893N/A }
893N/A @Override
1319N/A public FileTime lastAccessTime() {
893N/A return attrs.lastAccessTime();
893N/A }
893N/A @Override
1319N/A public FileTime creationTime() {
893N/A return attrs.creationTime();
893N/A }
893N/A @Override
893N/A public boolean isRegularFile() {
893N/A return attrs.isRegularFile();
893N/A }
893N/A @Override
893N/A public boolean isDirectory() {
893N/A return attrs.isDirectory();
893N/A }
893N/A @Override
893N/A public boolean isSymbolicLink() {
893N/A return attrs.isSymbolicLink();
893N/A }
893N/A @Override
893N/A public boolean isOther() {
893N/A return attrs.isOther();
893N/A }
893N/A @Override
893N/A public long size() {
893N/A return attrs.size();
893N/A }
893N/A @Override
893N/A public Object fileKey() {
893N/A return attrs.fileKey();
893N/A }
893N/A @Override
893N/A public boolean isReadOnly() {
893N/A return (dosAttribute & DOS_XATTR_READONLY) != 0;
893N/A }
893N/A @Override
893N/A public boolean isHidden() {
893N/A return (dosAttribute & DOS_XATTR_HIDDEN) != 0;
893N/A }
893N/A @Override
893N/A public boolean isArchive() {
893N/A return (dosAttribute & DOS_XATTR_ARCHIVE) != 0;
893N/A }
893N/A @Override
893N/A public boolean isSystem() {
893N/A return (dosAttribute & DOS_XATTR_SYSTEM) != 0;
893N/A }
893N/A };
893N/A
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A return null; // keep compiler happy
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A
893N/A @Override
893N/A public void setReadOnly(boolean value) throws IOException {
893N/A updateDosAttribute(DOS_XATTR_READONLY, value);
893N/A }
893N/A
893N/A @Override
893N/A public void setHidden(boolean value) throws IOException {
893N/A updateDosAttribute(DOS_XATTR_HIDDEN, value);
893N/A }
893N/A
893N/A @Override
893N/A public void setArchive(boolean value) throws IOException {
893N/A updateDosAttribute(DOS_XATTR_ARCHIVE, value);
893N/A }
893N/A
893N/A @Override
893N/A public void setSystem(boolean value) throws IOException {
893N/A updateDosAttribute(DOS_XATTR_SYSTEM, value);
893N/A }
893N/A
893N/A /**
893N/A * Reads the value of the user.DOSATTRIB extended attribute
893N/A */
893N/A private int getDosAttribute(int fd) throws UnixException {
893N/A final int size = 24;
893N/A
893N/A NativeBuffer buffer = NativeBuffers.getNativeBuffer(size);
893N/A try {
893N/A int len = LinuxNativeDispatcher
893N/A .fgetxattr(fd, DOS_XATTR_NAME_AS_BYTES, buffer.address(), size);
893N/A
893N/A if (len > 0) {
893N/A // ignore null terminator
893N/A if (unsafe.getByte(buffer.address()+len-1) == 0)
893N/A len--;
893N/A
893N/A // convert to String and parse
893N/A byte[] buf = new byte[len];
893N/A unsafe.copyMemory(null, buffer.address(), buf,
893N/A Unsafe.ARRAY_BYTE_BASE_OFFSET, len);
893N/A String value = new String(buf); // platform encoding
893N/A
893N/A // should be something like 0x20
893N/A if (value.length() >= 3 && value.startsWith("0x")) {
893N/A try {
893N/A return Integer.parseInt(value.substring(2), 16);
893N/A } catch (NumberFormatException x) {
893N/A // ignore
893N/A }
893N/A }
893N/A }
893N/A throw new UnixException("Value of " + DOS_XATTR_NAME + " attribute is invalid");
893N/A } catch (UnixException x) {
893N/A // default value when attribute does not exist
893N/A if (x.errno() == ENODATA)
893N/A return 0;
893N/A throw x;
893N/A } finally {
893N/A buffer.release();
893N/A }
893N/A }
893N/A
893N/A /**
893N/A * Updates the value of the user.DOSATTRIB extended attribute
893N/A */
893N/A private void updateDosAttribute(int flag, boolean enable) throws IOException {
893N/A file.checkWrite();
893N/A
893N/A int fd = file.openForAttributeAccess(followLinks);
893N/A try {
893N/A int oldValue = getDosAttribute(fd);
893N/A int newValue = oldValue;
893N/A if (enable) {
893N/A newValue |= flag;
893N/A } else {
893N/A newValue &= ~flag;
893N/A }
893N/A if (newValue != oldValue) {
893N/A byte[] value = ("0x" + Integer.toHexString(newValue)).getBytes();
893N/A NativeBuffer buffer = NativeBuffers.asNativeBuffer(value);
893N/A try {
893N/A LinuxNativeDispatcher.fsetxattr(fd, DOS_XATTR_NAME_AS_BYTES,
893N/A buffer.address(), value.length+1);
893N/A } finally {
893N/A buffer.release();
893N/A }
893N/A }
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file);
893N/A } finally {
893N/A close(fd);
893N/A }
893N/A }
893N/A}