1 /*
   2  * Copyright (c) 2008, 2011, 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.*;
  29 import java.io.IOException;
  30 import java.util.*;
  31 import static sun.nio.fs.LinuxNativeDispatcher.*;
  32 
  33 /**
  34  * Linux implementation of FileSystem
  35  */
  36 
  37 class LinuxFileSystem extends UnixFileSystem {
  38     LinuxFileSystem(UnixFileSystemProvider provider, String dir) {
  39         super(provider, dir);
  40     }
  41 
  42     @Override
  43     public WatchService newWatchService()
  44         throws IOException
  45     {
  46         // assume 2.6.13 or newer
  47         return new LinuxWatchService(this);
  48     }
  49 
  50 
  51     // lazy initialization of the list of supported attribute views
  52     private static class SupportedFileFileAttributeViewsHolder {
  53         static final Set<String> supportedFileAttributeViews =
  54             supportedFileAttributeViews();
  55         private static Set<String> supportedFileAttributeViews() {
  56             Set<String> result = new HashSet<>();
  57             result.addAll(standardFileAttributeViews());
  58             // additional Linux-specific views
  59             result.add("dos");
  60             result.add("user");
  61             return Collections.unmodifiableSet(result);
  62         }
  63     }
  64 
  65     @Override
  66     public Set<String> supportedFileAttributeViews() {
  67         return SupportedFileFileAttributeViewsHolder.supportedFileAttributeViews;
  68     }
  69 
  70     @Override
  71     void copyNonPosixAttributes(int ofd, int nfd) {
  72         LinuxUserDefinedFileAttributeView.copyExtendedAttributes(ofd, nfd);
  73     }
  74 
  75     /**
  76      * Returns object to iterate over the mount entries in the given fstab file.
  77      */
  78     Iterable<UnixMountEntry> getMountEntries(String fstab) {
  79         ArrayList<UnixMountEntry> entries = new ArrayList<>();
  80         try {
  81             long fp = setmntent(fstab.getBytes(), "r".getBytes());
  82             try {
  83                 for (;;) {
  84                     UnixMountEntry entry = new UnixMountEntry();
  85                     int res = getextmntent(fp, entry);
  86                     if (res < 0)
  87                         break;
  88                     entries.add(entry);
  89                 }
  90             } finally {
  91                 endmntent(fp);
  92             }
  93 
  94         } catch (UnixException x) {
  95             // nothing we can do
  96         }
  97         return entries;
  98     }
  99 
 100     /**
 101      * Returns object to iterate over the mount entries in /etc/mtab
 102      */
 103     @Override
 104     Iterable<UnixMountEntry> getMountEntries() {
 105         return getMountEntries("/etc/mtab");
 106     }
 107 
 108 
 109 
 110     @Override
 111     FileStore getFileStore(UnixMountEntry entry) throws IOException {
 112         return new LinuxFileStore(this, entry);
 113     }
 114 }