Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/nio/fs/LinuxFileStore.java
32288 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.nio.file.attribute.*;28import java.util.*;29import java.io.IOException;3031/**32* Linux implementation of FileStore33*/3435class LinuxFileStore36extends UnixFileStore37{38// used when checking if extended attributes are enabled or not39private volatile boolean xattrChecked;40private volatile boolean xattrEnabled;4142LinuxFileStore(UnixPath file) throws IOException {43super(file);44}4546LinuxFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {47super(fs, entry);48}4950/**51* Finds, and returns, the mount entry for the file system where the file52* resides.53*/54@Override55UnixMountEntry findMountEntry() throws IOException {56LinuxFileSystem fs = (LinuxFileSystem)file().getFileSystem();5758// step 1: get realpath59UnixPath path = null;60try {61byte[] rp = UnixNativeDispatcher.realpath(file());62path = new UnixPath(fs, rp);63} catch (UnixException x) {64x.rethrowAsIOException(file());65}6667// step 2: find mount point68List<UnixMountEntry> procMountsEntries =69fs.getMountEntries("/proc/mounts");70UnixPath parent = path.getParent();71while (parent != null) {72UnixFileAttributes attrs = null;73try {74attrs = UnixFileAttributes.get(parent, true);75} catch (UnixException x) {76x.rethrowAsIOException(parent);77}78if (attrs.dev() != dev()) {79// step 3: lookup mounted file systems (use /proc/mounts to80// ensure we find the file system even when not in /etc/mtab)81byte[] dir = path.asByteArray();82for (UnixMountEntry entry : procMountsEntries) {83if (Arrays.equals(dir, entry.dir()))84return entry;85}86}87path = parent;88parent = parent.getParent();89}9091// step 3: lookup mounted file systems (use /proc/mounts to92// ensure we find the file system even when not in /etc/mtab)93byte[] dir = path.asByteArray();94for (UnixMountEntry entry : procMountsEntries) {95if (Arrays.equals(dir, entry.dir()))96return entry;97}9899throw new IOException("Mount point not found");100}101102// returns true if extended attributes enabled on file system where given103// file resides, returns false if disabled or unable to determine.104private boolean isExtendedAttributesEnabled(UnixPath path) {105try {106int fd = path.openForAttributeAccess(false);107try {108// fgetxattr returns size if called with size==0109byte[] name = Util.toBytes("user.java");110LinuxNativeDispatcher.fgetxattr(fd, name, 0L, 0);111return true;112} catch (UnixException e) {113// attribute does not exist114if (e.errno() == UnixConstants.ENODATA)115return true;116} finally {117UnixNativeDispatcher.close(fd);118}119} catch (IOException ignore) {120// nothing we can do121}122return false;123}124125@Override126public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {127// support DosFileAttributeView and UserDefinedAttributeView if extended128// attributes enabled129if (type == DosFileAttributeView.class ||130type == UserDefinedFileAttributeView.class)131{132// lookup fstypes.properties133FeatureStatus status = checkIfFeaturePresent("user_xattr");134if (status == FeatureStatus.PRESENT)135return true;136if (status == FeatureStatus.NOT_PRESENT)137return false;138139// if file system is mounted with user_xattr option then assume140// extended attributes are enabled141if ((entry().hasOption("user_xattr")))142return true;143144// user_xattr option not present but we special-case ext3/4 as we145// know that extended attributes are not enabled by default.146if (entry().fstype().equals("ext3") || entry().fstype().equals("ext4"))147return false;148149// not ext3/4 so probe mount point150if (!xattrChecked) {151UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir());152xattrEnabled = isExtendedAttributesEnabled(dir);153xattrChecked = true;154}155return xattrEnabled;156}157// POSIX attributes not supported on FAT158if (type == PosixFileAttributeView.class && entry().fstype().equals("vfat"))159return false;160return super.supportsFileAttributeView(type);161}162163@Override164public boolean supportsFileAttributeView(String name) {165if (name.equals("dos"))166return supportsFileAttributeView(DosFileAttributeView.class);167if (name.equals("user"))168return supportsFileAttributeView(UserDefinedFileAttributeView.class);169return super.supportsFileAttributeView(name);170}171}172173174