1 /*
2  * Copyright (c) 2011, 2020, 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.Path;
29 import java.nio.file.LinkOption;
30 import java.nio.file.attribute.BasicFileAttributes;
31 import java.nio.file.spi.FileSystemProvider;
32 import java.io.IOException;
33 import java.util.Map;
34 
35 /**
36  * Base implementation class of FileSystemProvider
37  */
38 
39 public abstract class AbstractFileSystemProvider extends FileSystemProvider {
AbstractFileSystemProvider()40     protected AbstractFileSystemProvider() { }
41 
42     /**
43      * Splits the given attribute name into the name of an attribute view and
44      * the attribute. If the attribute view is not identified then it assumed
45      * to be "basic".
46      */
split(String attribute)47     private static String[] split(String attribute) {
48         String[] s = new String[2];
49         int pos = attribute.indexOf(':');
50         if (pos == -1) {
51             s[0] = "basic";
52             s[1] = attribute;
53         } else {
54             s[0] = attribute.substring(0, pos++);
55             s[1] = (pos == attribute.length()) ? "" : attribute.substring(pos);
56         }
57         return s;
58     }
59 
60     /**
61      * Gets a DynamicFileAttributeView by name. Returns {@code null} if the
62      * view is not available.
63      */
getFileAttributeView(Path file, String name, LinkOption... options)64     abstract DynamicFileAttributeView getFileAttributeView(Path file,
65                                                            String name,
66                                                            LinkOption... options);
67 
68     @Override
setAttribute(Path file, String attribute, Object value, LinkOption... options)69     public final void setAttribute(Path file,
70                                    String attribute,
71                                    Object value,
72                                    LinkOption... options)
73         throws IOException
74     {
75         String[] s = split(attribute);
76         if (s[0].isEmpty())
77             throw new IllegalArgumentException(attribute);
78         DynamicFileAttributeView view = getFileAttributeView(file, s[0], options);
79         if (view == null)
80             throw new UnsupportedOperationException("View '" + s[0] + "' not available");
81         view.setAttribute(s[1], value);
82     }
83 
84     @Override
readAttributes(Path file, String attributes, LinkOption... options)85     public final Map<String,Object> readAttributes(Path file, String attributes, LinkOption... options)
86         throws IOException
87     {
88         String[] s = split(attributes);
89         if (s[0].isEmpty())
90             throw new IllegalArgumentException(attributes);
91         DynamicFileAttributeView view = getFileAttributeView(file, s[0], options);
92         if (view == null)
93             throw new UnsupportedOperationException("View '" + s[0] + "' not available");
94         return view.readAttributes(s[1].split(","));
95     }
96 
97     /**
98      * Deletes a file. The {@code failIfNotExists} parameters determines if an
99      * {@code IOException} is thrown when the file does not exist.
100      */
implDelete(Path file, boolean failIfNotExists)101     abstract boolean implDelete(Path file, boolean failIfNotExists) throws IOException;
102 
103     @Override
delete(Path file)104     public final void delete(Path file) throws IOException {
105         implDelete(file, true);
106     }
107 
108     @Override
deleteIfExists(Path file)109     public final boolean deleteIfExists(Path file) throws IOException {
110         return implDelete(file, false);
111     }
112 
113     /**
114      * Tests whether a file is a directory.
115      *
116      * @return  {@code true} if the file is a directory; {@code false} if
117      *          the file does not exist, is not a directory, or it cannot
118      *          be determined if the file is a directory or not.
119      */
isDirectory(Path file)120     public boolean isDirectory(Path file) {
121         try {
122             return readAttributes(file, BasicFileAttributes.class).isDirectory();
123         } catch (IOException ioe) {
124             return false;
125         }
126     }
127 
128     /**
129      * Tests whether a file is a regular file with opaque content.
130      *
131      * @return  {@code true} if the file is a regular file; {@code false} if
132      *          the file does not exist, is not a regular file, or it
133      *          cannot be determined if the file is a regular file or not.
134      */
isRegularFile(Path file)135     public boolean isRegularFile(Path file) {
136         try {
137             return readAttributes(file, BasicFileAttributes.class).isRegularFile();
138         } catch (IOException ioe) {
139             return false;
140         }
141     }
142 
143     /**
144      * Checks the existence of a file.
145      *
146      * @return  {@code true} if the file exists; {@code false} if the file does
147      *          not exist or its existence cannot be determined.
148      */
exists(Path file)149     public boolean exists(Path file) {
150         try {
151             checkAccess(file);
152             return true;
153         } catch (IOException ioe) {
154             return false;
155         }
156     }
157 
158     /**
159      * Returns a path name as bytes for a Unix domain socket.
160      * Different encodings may be used for these names on some platforms.
161      * If path is empty, then an empty byte[] is returned.
162      */
getSunPathForSocketFile(Path path)163     public abstract byte[] getSunPathForSocketFile(Path path);
164 }
165