1 /* 2 * Copyright (c) 2008, 2009, 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 UnixPath parent = path.getParent(); 70 while (parent != null) { 71 UnixFileAttributes attrs = null; 72 try { 73 attrs = UnixFileAttributes.get(parent, true); 74 } catch (UnixException x) { 75 x.rethrowAsIOException(parent); 76 } 77 if (attrs.dev() != dev()) 78 break; 79 path = parent; 80 parent = parent.getParent(); 81 } 82 83 // step 3: lookup mounted file systems (use /proc/mounts to ensure we 84 // find the file system even when not in /etc/mtab) 85 byte[] dir = path.asByteArray(); 86 for (UnixMountEntry entry: fs.getMountEntries("/proc/mounts")) { 87 if (Arrays.equals(dir, entry.dir())) 88 return entry; 89 } 90 91 throw new IOException("Mount point not found"); 92 } 93 94 // returns true if extended attributes enabled on file system where given 95 // file resides, returns false if disabled or unable to determine. 96 private boolean isExtendedAttributesEnabled(UnixPath path) { 97 try { 98 int fd = path.openForAttributeAccess(false); 99 try { 100 // fgetxattr returns size if called with size==0 101 LinuxNativeDispatcher.fgetxattr(fd, "user.java".getBytes(), 0L, 0); 102 return true; 103 } catch (UnixException e) { 104 // attribute does not exist 105 if (e.errno() == UnixConstants.ENODATA) 106 return true; 107 } finally { 108 UnixNativeDispatcher.close(fd); 109 } 110 } catch (IOException ignore) { 111 // nothing we can do 112 } 113 return false; 114 } 115 116 @Override 117 public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) { 118 // support DosFileAttributeView and UserDefinedAttributeView if extended 119 // attributes enabled 120 if (type == DosFileAttributeView.class || 121 type == UserDefinedFileAttributeView.class) 122 { 123 // lookup fstypes.properties 124 FeatureStatus status = checkIfFeaturePresent("user_xattr"); 125 if (status == FeatureStatus.PRESENT) 126 return true; 127 if (status == FeatureStatus.NOT_PRESENT) 128 return false; 129 130 // if file system is mounted with user_xattr option then assume 131 // extended attributes are enabled 132 if ((entry().hasOption("user_xattr"))) 133 return true; 134 135 // user_xattr option not present but we special-case ext3/4 as we 136 // know that extended attributes are not enabled by default. 137 if (entry().fstype().equals("ext3") || entry().fstype().equals("ext4")) 138 return false; 139 140 // not ext3/4 so probe mount point 141 if (!xattrChecked) { 142 UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir()); 143 xattrEnabled = isExtendedAttributesEnabled(dir); 144 xattrChecked = true; 145 } 146 return xattrEnabled; 147 } 148 return super.supportsFileAttributeView(type); 149 } 150 151 @Override 152 public boolean supportsFileAttributeView(String name) { 153 if (name.equals("dos")) 154 return supportsFileAttributeView(DosFileAttributeView.class); 155 if (name.equals("user")) 156 return supportsFileAttributeView(UserDefinedFileAttributeView.class); 157 return super.supportsFileAttributeView(name); 158 } 159 }