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