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.io.IOException;
  29 import java.nio.file.attribute.DosFileAttributeView;
  30 import java.nio.file.attribute.FileAttributeView;
  31 import java.nio.file.attribute.PosixFileAttributeView;
  32 import java.nio.file.attribute.UserDefinedFileAttributeView;
  33 import java.util.Arrays;
  34 import java.util.List;
  35 import java.util.regex.Pattern;
  36 
  37 /**
  38  * Linux implementation of FileStore
  39  */
  40 
  41 class LinuxFileStore
  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     LinuxFileStore(UnixPath file) throws IOException {
  49         super(file);
  50     }
  51 
  52     LinuxFileStore(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         LinuxFileSystem fs = (LinuxFileSystem)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         List<UnixMountEntry> procMountsEntries =
  75             fs.getMountEntries("/proc/mounts");
  76         UnixPath parent = path.getParent();
  77         while (parent != null) {
  78             UnixFileAttributes attrs = null;
  79             try {
  80                 attrs = UnixFileAttributes.get(parent, true);
  81             } catch (UnixException x) {
  82                 x.rethrowAsIOException(parent);
  83             }
  84             if (attrs.dev() != dev()) {
  85                 // step 3: lookup mounted file systems (use /proc/mounts to
  86                 // ensure we find the file system even when not in /etc/mtab)
  87                 byte[] dir = path.asByteArray();
  88                 for (UnixMountEntry entry : procMountsEntries) {
  89                     if (Arrays.equals(dir, entry.dir()))
  90                         return entry;
  91                 }
  92             }
  93             path = parent;
  94             parent = parent.getParent();
  95         }
  96 
  97         // step 3: lookup mounted file systems (use /proc/mounts to
  98         // ensure we find the file system even when not in /etc/mtab)
  99         byte[] dir = path.asByteArray();
 100         for (UnixMountEntry entry : procMountsEntries) {
 101             if (Arrays.equals(dir, entry.dir()))
 102                 return entry;
 103         }
 104 
 105         throw new IOException("Mount point not found");
 106     }
 107 
 108     // returns true if extended attributes enabled on file system where given
 109     // file resides, returns false if disabled or unable to determine.
 110     private boolean isExtendedAttributesEnabled(UnixPath path) {
 111         int fd = -1;
 112         try {
 113             fd = path.openForAttributeAccess(false);
 114 
 115             // fgetxattr returns size if called with size==0
 116             byte[] name = Util.toBytes("user.java");
 117             LinuxNativeDispatcher.fgetxattr(fd, name, 0L, 0);
 118             return true;
 119         } catch (UnixException e) {
 120             // attribute does not exist
 121             if (e.errno() == UnixConstants.ENODATA)
 122                 return true;
 123         } finally {
 124             UnixNativeDispatcher.close(fd);
 125         }
 126         return false;
 127     }
 128 
 129     // get kernel version as a three element array {major, minor, micro}
 130     private static int[] getKernelVersion() {
 131         Pattern pattern = Pattern.compile("\\D+");
 132         String[] matches = pattern.split(System.getProperty("os.version"));
 133         int[] majorMinorMicro = new int[3];
 134         int length = Math.min(matches.length, majorMinorMicro.length);
 135         for (int i = 0; i < length; i++) {
 136             majorMinorMicro[i] = Integer.valueOf(matches[i]);
 137         }
 138         return majorMinorMicro;
 139     }
 140 
 141     @Override
 142     public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
 143         // support DosFileAttributeView and UserDefinedAttributeView if extended
 144         // attributes enabled
 145         if (type == DosFileAttributeView.class ||
 146             type == UserDefinedFileAttributeView.class)
 147         {
 148             // lookup fstypes.properties
 149             FeatureStatus status = checkIfFeaturePresent("user_xattr");
 150             if (status == FeatureStatus.PRESENT)
 151                 return true;
 152             if (status == FeatureStatus.NOT_PRESENT)
 153                 return false;
 154 
 155             // if file system is mounted with user_xattr option then assume
 156             // extended attributes are enabled
 157             if ((entry().hasOption("user_xattr")))
 158                 return true;
 159 
 160             // check for explicit disabling of extended attributes
 161             if (entry().hasOption("nouser_xattr")) {
 162                 return false;
 163             }
 164 
 165             // user_{no}xattr options not present but we special-case ext3 as
 166             // we know that extended attributes are not enabled by default.
 167             if (entry().fstype().equals("ext3")) {
 168                 return false;
 169             }
 170 
 171             // user_xattr option not present but we special-case ext4 as we
 172             // know that extended attributes are enabled by default for
 173             // kernel version >= 2.6.39
 174             if (entry().fstype().equals("ext4")) {
 175                 if (!xattrChecked) {
 176                     // check kernel version
 177                     int[] kernelVersion = getKernelVersion();
 178                     xattrEnabled = kernelVersion[0] > 2 ||
 179                         (kernelVersion[0] == 2 && kernelVersion[1] > 6) ||
 180                         (kernelVersion[0] == 2 && kernelVersion[1] == 6 &&
 181                             kernelVersion[2] >= 39);
 182                     xattrChecked = true;
 183                 }
 184                 return xattrEnabled;
 185             }
 186 
 187             // not ext3/4 so probe mount point
 188             if (!xattrChecked) {
 189                 UnixPath dir = new UnixPath(file().getFileSystem(), entry().dir());
 190                 xattrEnabled = isExtendedAttributesEnabled(dir);
 191                 xattrChecked = true;
 192             }
 193             return xattrEnabled;
 194         }
 195         // POSIX attributes not supported on FAT
 196         if (type == PosixFileAttributeView.class && entry().fstype().equals("vfat"))
 197             return false;
 198         return super.supportsFileAttributeView(type);
 199     }
 200 
 201     @Override
 202     public boolean supportsFileAttributeView(String name) {
 203         if (name.equals("dos"))
 204             return supportsFileAttributeView(DosFileAttributeView.class);
 205         if (name.equals("user"))
 206             return supportsFileAttributeView(UserDefinedFileAttributeView.class);
 207         return super.supportsFileAttributeView(name);
 208     }
 209 }