Path: blob/master/src/java.base/macosx/classes/sun/nio/fs/BsdFileStore.java
41137 views
/*1* Copyright (c) 2008, 2019, 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.FileAttributeView;28import java.nio.file.attribute.UserDefinedFileAttributeView;29import java.io.IOException;30import java.util.Arrays;31import sun.security.action.GetPropertyAction;3233/**34* Bsd implementation of FileStore35*/3637class BsdFileStore38extends UnixFileStore39{40BsdFileStore(UnixPath file) throws IOException {41super(file);42}4344BsdFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException {45super(fs, entry);46}4748/**49* Finds, and returns, the mount entry for the file system where the file50* resides.51*/52@Override53UnixMountEntry findMountEntry() throws IOException {54UnixFileSystem fs = file().getFileSystem();5556// step 1: get realpath57UnixPath path = null;58try {59byte[] rp = UnixNativeDispatcher.realpath(file());60path = new UnixPath(fs, rp);61} catch (UnixException x) {62x.rethrowAsIOException(file());63}6465// step 2: find mount point66byte[] dir = null;67try {68dir = BsdNativeDispatcher.getmntonname(path);69} catch (UnixException x) {70x.rethrowAsIOException(path);71}7273// step 3: lookup mounted file systems74for (UnixMountEntry entry: fs.getMountEntries()) {75if (Arrays.equals(dir, entry.dir()))76return entry;77}7879throw new IOException("Mount point not found in fstab");80}8182@Override83public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {84// support UserDefinedAttributeView if extended attributes enabled85if (type == UserDefinedFileAttributeView.class) {86// lookup fstypes.properties87FeatureStatus status = checkIfFeaturePresent("user_xattr");88if (status == FeatureStatus.PRESENT)89return true;90if (status == FeatureStatus.NOT_PRESENT)91return false;9293// typical macOS file system types that are known to support xattr94String fstype = entry().fstype();95if ("hfs".equals(fstype))96return true;97if ("apfs".equals(fstype)) {98// fgetxattr broken on APFS prior to 10.1499return isOsVersionGte(10, 14);100}101102// probe file system capabilities103UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir());104return isExtendedAttributesEnabled(dir);105}106return super.supportsFileAttributeView(type);107}108109@Override110public boolean supportsFileAttributeView(String name) {111if (name.equals("user"))112return supportsFileAttributeView(UserDefinedFileAttributeView.class);113return super.supportsFileAttributeView(name);114}115116/**117* Returns true if the OS major/minor version is greater than, or equal, to the118* given major/minor version.119*/120private static boolean isOsVersionGte(int requiredMajor, int requiredMinor) {121String osVersion = GetPropertyAction.privilegedGetProperty("os.version");122String[] vers = Util.split(osVersion, '.');123int majorVersion = Integer.parseInt(vers[0]);124int minorVersion = Integer.parseInt(vers[1]);125return (majorVersion > requiredMajor)126|| (majorVersion == requiredMajor && minorVersion >= requiredMinor);127}128}129130131