1 /* 2 * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.nio.fs; 27 28 import java.nio.file.attribute.*; 29 import java.util.*; 30 import java.io.IOException; 31 32 /** 33 * Linux implementation of FileStore 34 */ 35 36 class LinuxFileStore 37 extends UnixFileStore 38 { 39 // used when checking if extended attributes are enabled or not 40 private volatile boolean xattrChecked; 41 private volatile boolean xattrEnabled; 42 43 LinuxFileStore(UnixPath file) throws IOException { 44 super(file); 45 } 46 47 LinuxFileStore(UnixFileSystem fs, UnixMountEntry entry) throws IOException { 48 super(fs, entry); 49 } 50 51 /** 52 * Finds, and returns, the mount entry for the file system where the file 53 * resides. 54 */ 55 @Override 56 UnixMountEntry findMountEntry() throws IOException { 57 LinuxFileSystem fs = (LinuxFileSystem)file().getFileSystem(); 58 59 // step 1: get realpath 60 UnixPath path = null; 61 try { 62 byte[] rp = UnixNativeDispatcher.realpath(file()); 63 path = new UnixPath(fs, rp); 64 } catch (UnixException x) { 65 x.rethrowAsIOException(file()); 66 } 67 68 // step 2: find mount point 69 List<UnixMountEntry> procMountsEntries = 70 fs.getMountEntries("/proc/mounts"); 71 UnixPath parent = path.getParent(); 72 while (parent != null) { 73 UnixFileAttributes attrs = null; 74 try { 75 attrs = UnixFileAttributes.get(parent, true); 76 } catch (UnixException x) { 77 x.rethrowAsIOException(parent); 78 } 79 if (attrs.dev() != dev()) { 80 // step 3: lookup mounted file systems (use /proc/mounts to 81 // ensure we find the file system even when not in /etc/mtab) 82 byte[] dir = path.asByteArray(); 83 for (UnixMountEntry entry : procMountsEntries) { 84 if (Arrays.equals(dir, entry.dir())) 85 return entry; 86 } 87 } 88 path = parent; 89 parent = parent.getParent(); 90 } 91 92 // step 3: lookup mounted file systems (use /proc/mounts to 93 // ensure we find the file system even when not in /etc/mtab) 94 byte[] dir = path.asByteArray(); 95 for (UnixMountEntry entry : procMountsEntries) { 96 if (Arrays.equals(dir, entry.dir())) 97 return entry; 98 } 99 100 throw new IOException("Mount point not found"); 101 } 102 103 // returns true if extended attributes enabled on file system where given 104 // file resides, returns false if disabled or unable to determine. 105 private boolean isExtendedAttributesEnabled(UnixPath path) { 106 int fd = -1; 107 try { 108 fd = path.openForAttributeAccess(false); 109 110 // fgetxattr returns size if called with size==0 111 byte[] name = Util.toBytes("user.java"); 112 LinuxNativeDispatcher.fgetxattr(fd, name, 0L, 0); 113 return true; 114 } catch (UnixException e) { 115 // attribute does not exist 116 if (e.errno() == UnixConstants.ENODATA) 117 return true; 118 } finally { 119 UnixNativeDispatcher.close(fd); 120 } 121 return false; 122 } 123 124 @Override 125 public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) { 126 // support DosFileAttributeView and UserDefinedAttributeView if extended 127 // attributes enabled 128 if (type == DosFileAttributeView.class || 129 type == UserDefinedFileAttributeView.class) 130 { 131 // lookup fstypes.properties 132 FeatureStatus status = checkIfFeaturePresent("user_xattr"); 133 if (status == FeatureStatus.PRESENT) 134 return true; 135 if (status == FeatureStatus.NOT_PRESENT) 136 return false; 137 138 // if file system is mounted with user_xattr option then assume 139 // extended attributes are enabled 140 if ((entry().hasOption("user_xattr"))) 141 return true; 142 143 // for ext3 and ext4 user_xattr option is enabled by default so 144 // check for explicit disabling of this option 145 if (entry().fstype().equals("ext3") || 146 entry().fstype().equals("ext4")) { 147 return !entry().hasOption("nouser_xattr"); 148 } 149 150 // not ext3/4 so probe mount point 151 if (!xattrChecked) { 152 UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir()); 153 xattrEnabled = isExtendedAttributesEnabled(dir); 154 xattrChecked = true; 155 } 156 return xattrEnabled; 157 } 158 // POSIX attributes not supported on FAT 159 if (type == PosixFileAttributeView.class && entry().fstype().equals("vfat")) 160 return false; 161 return super.supportsFileAttributeView(type); 162 } 163 164 @Override 165 public boolean supportsFileAttributeView(String name) { 166 if (name.equals("dos")) 167 return supportsFileAttributeView(DosFileAttributeView.class); 168 if (name.equals("user")) 169 return supportsFileAttributeView(UserDefinedFileAttributeView.class); 170 return super.supportsFileAttributeView(name); 171 } 172 }