893N/A/*
3261N/A * Copyright (c) 2008, 2010, 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
1609N/Aimport java.nio.file.attribute.*;
893N/Aimport java.util.*;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * Linux implementation of FileStore
893N/A */
893N/A
893N/Aclass LinuxFileStore
893N/A extends UnixFileStore
893N/A{
893N/A // used when checking if extended attributes are enabled or not
893N/A private volatile boolean xattrChecked;
893N/A private volatile boolean xattrEnabled;
893N/A
893N/A LinuxFileStore(UnixPath file) throws IOException {
893N/A super(file);
893N/A }
893N/A
893N/A LinuxFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {
893N/A super(fs, entry);
893N/A }
893N/A
893N/A /**
893N/A * Finds, and returns, the mount entry for the file system where the file
893N/A * resides.
893N/A */
893N/A @Override
893N/A UnixMountEntry findMountEntry() throws IOException {
1319N/A LinuxFileSystem fs = (LinuxFileSystem)file().getFileSystem();
893N/A
893N/A // step 1: get realpath
893N/A UnixPath path = null;
893N/A try {
893N/A byte[] rp = UnixNativeDispatcher.realpath(file());
893N/A path = new UnixPath(fs, rp);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(file());
893N/A }
893N/A
893N/A // step 2: find mount point
893N/A UnixPath parent = path.getParent();
893N/A while (parent != null) {
893N/A UnixFileAttributes attrs = null;
893N/A try {
893N/A attrs = UnixFileAttributes.get(parent, true);
893N/A } catch (UnixException x) {
893N/A x.rethrowAsIOException(parent);
893N/A }
893N/A if (attrs.dev() != dev())
893N/A break;
893N/A path = parent;
893N/A parent = parent.getParent();
893N/A }
893N/A
1319N/A // step 3: lookup mounted file systems (use /proc/mounts to ensure we
1319N/A // find the file system even when not in /etc/mtab)
893N/A byte[] dir = path.asByteArray();
1319N/A for (UnixMountEntry entry: fs.getMountEntries("/proc/mounts")) {
893N/A if (Arrays.equals(dir, entry.dir()))
893N/A return entry;
893N/A }
893N/A
1319N/A throw new IOException("Mount point not found");
893N/A }
893N/A
893N/A // returns true if extended attributes enabled on file system where given
893N/A // file resides, returns false if disabled or unable to determine.
893N/A private boolean isExtendedAttributesEnabled(UnixPath path) {
893N/A try {
893N/A int fd = path.openForAttributeAccess(false);
893N/A try {
893N/A // fgetxattr returns size if called with size==0
893N/A LinuxNativeDispatcher.fgetxattr(fd, "user.java".getBytes(), 0L, 0);
893N/A return true;
893N/A } catch (UnixException e) {
893N/A // attribute does not exist
893N/A if (e.errno() == UnixConstants.ENODATA)
893N/A return true;
893N/A } finally {
893N/A UnixNativeDispatcher.close(fd);
893N/A }
893N/A } catch (IOException ignore) {
893N/A // nothing we can do
893N/A }
893N/A return false;
893N/A }
893N/A
893N/A @Override
1609N/A public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
1319N/A // support DosFileAttributeView and UserDefinedAttributeView if extended
893N/A // attributes enabled
1609N/A if (type == DosFileAttributeView.class ||
1609N/A type == UserDefinedFileAttributeView.class)
1609N/A {
893N/A // lookup fstypes.properties
893N/A FeatureStatus status = checkIfFeaturePresent("user_xattr");
893N/A if (status == FeatureStatus.PRESENT)
893N/A return true;
893N/A if (status == FeatureStatus.NOT_PRESENT)
893N/A return false;
893N/A
893N/A // if file system is mounted with user_xattr option then assume
893N/A // extended attributes are enabled
893N/A if ((entry().hasOption("user_xattr")))
893N/A return true;
893N/A
893N/A // user_xattr option not present but we special-case ext3/4 as we
893N/A // know that extended attributes are not enabled by default.
893N/A if (entry().fstype().equals("ext3") || entry().fstype().equals("ext4"))
893N/A return false;
893N/A
893N/A // not ext3/4 so probe mount point
893N/A if (!xattrChecked) {
893N/A UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir());
893N/A xattrEnabled = isExtendedAttributesEnabled(dir);
893N/A xattrChecked = true;
893N/A }
893N/A return xattrEnabled;
893N/A }
1609N/A return super.supportsFileAttributeView(type);
1609N/A }
893N/A
1609N/A @Override
1609N/A public boolean supportsFileAttributeView(String name) {
1609N/A if (name.equals("dos"))
1609N/A return supportsFileAttributeView(DosFileAttributeView.class);
1609N/A if (name.equals("user"))
1609N/A return supportsFileAttributeView(UserDefinedFileAttributeView.class);
893N/A return super.supportsFileAttributeView(name);
893N/A }
893N/A}