Path: blob/master/src/java.base/linux/classes/sun/nio/fs/LinuxFileStore.java
40948 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.fs;2627import java.io.IOException;28import java.nio.file.attribute.DosFileAttributeView;29import java.nio.file.attribute.FileAttributeView;30import java.nio.file.attribute.PosixFileAttributeView;31import java.nio.file.attribute.UserDefinedFileAttributeView;32import java.util.Arrays;33import java.util.List;34import java.util.regex.Pattern;3536/**37* Linux implementation of FileStore38*/3940class LinuxFileStore41extends UnixFileStore42{43// used when checking if extended attributes are enabled or not44private volatile boolean xattrChecked;45private volatile boolean xattrEnabled;4647LinuxFileStore(UnixPath file) throws IOException {48super(file);49}5051LinuxFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {52super(fs, entry);53}5455/**56* Finds, and returns, the mount entry for the file system where the file57* resides.58*/59@Override60UnixMountEntry findMountEntry() throws IOException {61LinuxFileSystem fs = (LinuxFileSystem)file().getFileSystem();6263// step 1: get realpath64UnixPath path = null;65try {66byte[] rp = UnixNativeDispatcher.realpath(file());67path = new UnixPath(fs, rp);68} catch (UnixException x) {69x.rethrowAsIOException(file());70}7172// step 2: find mount point73List<UnixMountEntry> procMountsEntries =74fs.getMountEntries("/proc/mounts");75UnixPath parent = path.getParent();76while (parent != null) {77UnixFileAttributes attrs = null;78try {79attrs = UnixFileAttributes.get(parent, true);80} catch (UnixException x) {81x.rethrowAsIOException(parent);82}83if (attrs.dev() != dev()) {84// step 3: lookup mounted file systems (use /proc/mounts to85// ensure we find the file system even when not in /etc/mtab)86byte[] dir = path.asByteArray();87for (UnixMountEntry entry : procMountsEntries) {88if (Arrays.equals(dir, entry.dir()))89return entry;90}91}92path = parent;93parent = parent.getParent();94}9596// step 3: lookup mounted file systems (use /proc/mounts to97// ensure we find the file system even when not in /etc/mtab)98byte[] dir = path.asByteArray();99for (UnixMountEntry entry : procMountsEntries) {100if (Arrays.equals(dir, entry.dir()))101return entry;102}103104throw new IOException("Mount point not found");105}106107// get kernel version as a three element array {major, minor, micro}108private static int[] getKernelVersion() {109Pattern pattern = Pattern.compile("\\D+");110String[] matches = pattern.split(System.getProperty("os.version"));111int[] majorMinorMicro = new int[3];112int length = Math.min(matches.length, majorMinorMicro.length);113for (int i = 0; i < length; i++) {114majorMinorMicro[i] = Integer.valueOf(matches[i]);115}116return majorMinorMicro;117}118119@Override120public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {121// support DosFileAttributeView and UserDefinedAttributeView if extended122// attributes enabled123if (type == DosFileAttributeView.class ||124type == UserDefinedFileAttributeView.class)125{126// lookup fstypes.properties127FeatureStatus status = checkIfFeaturePresent("user_xattr");128if (status == FeatureStatus.PRESENT)129return true;130if (status == FeatureStatus.NOT_PRESENT)131return false;132133// if file system is mounted with user_xattr option then assume134// extended attributes are enabled135if ((entry().hasOption("user_xattr")))136return true;137138// check for explicit disabling of extended attributes139if (entry().hasOption("nouser_xattr")) {140return false;141}142143// user_xattr option not present but we special-case ext4 as we144// know that extended attributes are enabled by default for145// kernel version >= 2.6.39146if (entry().fstype().equals("ext4")) {147if (!xattrChecked) {148// check kernel version149int[] kernelVersion = getKernelVersion();150xattrEnabled = kernelVersion[0] > 2 ||151(kernelVersion[0] == 2 && kernelVersion[1] > 6) ||152(kernelVersion[0] == 2 && kernelVersion[1] == 6 &&153kernelVersion[2] >= 39);154xattrChecked = true;155}156return xattrEnabled;157}158159// not ext4 so probe mount point160if (!xattrChecked) {161UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir());162xattrEnabled = isExtendedAttributesEnabled(dir);163xattrChecked = true;164}165return xattrEnabled;166}167// POSIX attributes not supported on FAT168if (type == PosixFileAttributeView.class && entry().fstype().equals("vfat"))169return false;170return super.supportsFileAttributeView(type);171}172173@Override174public boolean supportsFileAttributeView(String name) {175if (name.equals("dos"))176return supportsFileAttributeView(DosFileAttributeView.class);177if (name.equals("user"))178return supportsFileAttributeView(UserDefinedFileAttributeView.class);179return super.supportsFileAttributeView(name);180}181}182183184