1 /* 2 * Copyright (c) 2008, 2019, 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 List<UnixMountEntry> getMountEntries(String fstab) { 79 ArrayList<UnixMountEntry> entries = new ArrayList<>(); 80 try { 81 long fp = setmntent(Util.toBytes(fstab), Util.toBytes("r")); 82 int maxLineSize = 1024; 83 try { 84 for (;;) { 85 int lineSize = getlinelen(fp); 86 if (lineSize == -1) 87 break; 88 if (lineSize > maxLineSize) 89 maxLineSize = lineSize; 90 } 91 } catch (UnixException x) { 92 // nothing we need to do 93 } finally { 94 rewind(fp); 95 } 96 97 try { 98 for (;;) { 99 UnixMountEntry entry = new UnixMountEntry(); 100 // count in NUL character at the end 101 int res = getmntent(fp, entry, maxLineSize + 1); 102 if (res < 0) 103 break; 104 entries.add(entry); 105 } 106 } finally { 107 endmntent(fp); 108 } 109 110 } catch (UnixException x) { 111 // nothing we can do 112 } 113 return entries; 114 } 115 116 /** 117 * Returns object to iterate over the mount entries in /etc/mtab 118 */ 119 @Override 120 List<UnixMountEntry> getMountEntries() { 121 return getMountEntries("/etc/mtab"); 122 } 123 124 125 126 @Override 127 FileStore getFileStore(UnixMountEntry entry) throws IOException { 128 return new LinuxFileStore(this, entry); 129 } 130 }