1/*
2 * Copyright (c) 2007, 2017, 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
26package java.nio.file;
27
28import java.nio.file.attribute.*;
29import java.nio.file.spi.FileSystemProvider;
30import java.util.Set;
31import java.io.Closeable;
32import java.io.IOException;
33
34/**
35 * Provides an interface to a file system and is the factory for objects to
36 * access files and other objects in the file system.
37 *
38 * <p> The default file system, obtained by invoking the {@link FileSystems#getDefault
39 * FileSystems.getDefault} method, provides access to the file system that is
40 * accessible to the Java virtual machine. The {@link FileSystems} class defines
41 * methods to create file systems that provide access to other types of (custom)
42 * file systems.
43 *
44 * <p> A file system is the factory for several types of objects:
45 *
46 * <ul>
47 *   <li><p> The {@link #getPath getPath} method converts a system dependent
48 *     <em>path string</em>, returning a {@link Path} object that may be used
49 *     to locate and access a file. </p></li>
50 *   <li><p> The {@link #getPathMatcher  getPathMatcher} method is used
51 *     to create a {@link PathMatcher} that performs match operations on
52 *     paths. </p></li>
53 *   <li><p> The {@link #getFileStores getFileStores} method returns an iterator
54 *     over the underlying {@link FileStore file-stores}. </p></li>
55 *   <li><p> The {@link #getUserPrincipalLookupService getUserPrincipalLookupService}
56 *     method returns the {@link UserPrincipalLookupService} to lookup users or
57 *     groups by name. </p></li>
58 *   <li><p> The {@link #newWatchService newWatchService} method creates a
59 *     {@link WatchService} that may be used to watch objects for changes and
60 *     events. </p></li>
61 * </ul>
62 *
63 * <p> File systems vary greatly. In some cases the file system is a single
64 * hierarchy of files with one top-level root directory. In other cases it may
65 * have several distinct file hierarchies, each with its own top-level root
66 * directory. The {@link #getRootDirectories getRootDirectories} method may be
67 * used to iterate over the root directories in the file system. A file system
68 * is typically composed of one or more underlying {@link FileStore file-stores}
69 * that provide the storage for the files. Theses file stores can also vary in
70 * the features they support, and the file attributes or <em>meta-data</em> that
71 * they associate with files.
72 *
73 * <p> A file system is open upon creation and can be closed by invoking its
74 * {@link #close() close} method. Once closed, any further attempt to access
75 * objects in the file system cause {@link ClosedFileSystemException} to be
76 * thrown. File systems created by the default {@link FileSystemProvider provider}
77 * cannot be closed.
78 *
79 * <p> A {@code FileSystem} can provide read-only or read-write access to the
80 * file system. Whether or not a file system provides read-only access is
81 * established when the {@code FileSystem} is created and can be tested by invoking
82 * its {@link #isReadOnly() isReadOnly} method. Attempts to write to file stores
83 * by means of an object associated with a read-only file system throws {@link
84 * ReadOnlyFileSystemException}.
85 *
86 * <p> File systems are safe for use by multiple concurrent threads. The {@link
87 * #close close} method may be invoked at any time to close a file system but
88 * whether a file system is <i>asynchronously closeable</i> is provider specific
89 * and therefore unspecified. In other words, if a thread is accessing an
90 * object in a file system, and another thread invokes the {@code close} method
91 * then it may require to block until the first operation is complete. Closing
92 * a file system causes all open channels, watch services, and other {@link
93 * Closeable closeable} objects associated with the file system to be closed.
94 *
95 * @since 1.7
96 */
97
98public abstract class FileSystem
99    implements Closeable
100{
101    /**
102     * Initializes a new instance of this class.
103     */
104    protected FileSystem() {
105    }
106
107    /**
108     * Returns the provider that created this file system.
109     *
110     * @return  The provider that created this file system.
111     */
112    public abstract FileSystemProvider provider();
113
114    /**
115     * Closes this file system.
116     *
117     * <p> After a file system is closed then all subsequent access to the file
118     * system, either by methods defined by this class or on objects associated
119     * with this file system, throw {@link ClosedFileSystemException}. If the
120     * file system is already closed then invoking this method has no effect.
121     *
122     * <p> Closing a file system will close all open {@link
123     * java.nio.channels.Channel channels}, {@link DirectoryStream directory-streams},
124     * {@link WatchService watch-service}, and other closeable objects associated
125     * with this file system. The {@link FileSystems#getDefault default} file
126     * system cannot be closed.
127     *
128     * @throws  IOException
129     *          If an I/O error occurs
130     * @throws  UnsupportedOperationException
131     *          Thrown in the case of the default file system
132     */
133    @Override
134    public abstract void close() throws IOException;
135
136    /**
137     * Tells whether or not this file system is open.
138     *
139     * <p> File systems created by the default provider are always open.
140     *
141     * @return  {@code true} if, and only if, this file system is open
142     */
143    public abstract boolean isOpen();
144
145    /**
146     * Tells whether or not this file system allows only read-only access to
147     * its file stores.
148     *
149     * @return  {@code true} if, and only if, this file system provides
150     *          read-only access
151     */
152    public abstract boolean isReadOnly();
153
154    /**
155     * Returns the name separator, represented as a string.
156     *
157     * <p> The name separator is used to separate names in a path string. An
158     * implementation may support multiple name separators in which case this
159     * method returns an implementation specific <em>default</em> name separator.
160     * This separator is used when creating path strings by invoking the {@link
161     * Path#toString() toString()} method.
162     *
163     * <p> In the case of the default provider, this method returns the same
164     * separator as {@link java.io.File#separator}.
165     *
166     * @return  The name separator
167     */
168    public abstract String getSeparator();
169
170    /**
171     * Returns an object to iterate over the paths of the root directories.
172     *
173     * <p> A file system provides access to a file store that may be composed
174     * of a number of distinct file hierarchies, each with its own top-level
175     * root directory. Unless denied by the security manager, each element in
176     * the returned iterator corresponds to the root directory of a distinct
177     * file hierarchy. The order of the elements is not defined. The file
178     * hierarchies may change during the lifetime of the Java virtual machine.
179     * For example, in some implementations, the insertion of removable media
180     * may result in the creation of a new file hierarchy with its own
181     * top-level directory.
182     *
183     * <p> When a security manager is installed, it is invoked to check access
184     * to the each root directory. If denied, the root directory is not returned
185     * by the iterator. In the case of the default provider, the {@link
186     * SecurityManager#checkRead(String)} method is invoked to check read access
187     * to each root directory. It is system dependent if the permission checks
188     * are done when the iterator is obtained or during iteration.
189     *
190     * @return  An object to iterate over the root directories
191     */
192    public abstract Iterable<Path> getRootDirectories();
193
194    /**
195     * Returns an object to iterate over the underlying file stores.
196     *
197     * <p> The elements of the returned iterator are the {@link
198     * FileStore FileStores} for this file system. The order of the elements is
199     * not defined and the file stores may change during the lifetime of the
200     * Java virtual machine. When an I/O error occurs, perhaps because a file
201     * store is not accessible, then it is not returned by the iterator.
202     *
203     * <p> In the case of the default provider, and a security manager is
204     * installed, the security manager is invoked to check {@link
205     * RuntimePermission}{@code ("getFileStoreAttributes")}. If denied, then
206     * no file stores are returned by the iterator. In addition, the security
207     * manager's {@link SecurityManager#checkRead(String)} method is invoked to
208     * check read access to the file store's <em>top-most</em> directory. If
209     * denied, the file store is not returned by the iterator. It is system
210     * dependent if the permission checks are done when the iterator is obtained
211     * or during iteration.
212     *
213     * <p> <b>Usage Example:</b>
214     * Suppose we want to print the space usage for all file stores:
215     * <pre>
216     *     for (FileStore store: FileSystems.getDefault().getFileStores()) {
217     *         long total = store.getTotalSpace() / 1024;
218     *         long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;
219     *         long avail = store.getUsableSpace() / 1024;
220     *         System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);
221     *     }
222     * </pre>
223     *
224     * @return  An object to iterate over the backing file stores
225     */
226    public abstract Iterable<FileStore> getFileStores();
227
228    /**
229     * Returns the set of the {@link FileAttributeView#name names} of the file
230     * attribute views supported by this {@code FileSystem}.
231     *
232     * <p> The {@link BasicFileAttributeView} is required to be supported and
233     * therefore the set contains at least one element, "basic".
234     *
235     * <p> The {@link FileStore#supportsFileAttributeView(String)
236     * supportsFileAttributeView(String)} method may be used to test if an
237     * underlying {@link FileStore} supports the file attributes identified by a
238     * file attribute view.
239     *
240     * @return  An unmodifiable set of the names of the supported file attribute
241     *          views
242     */
243    public abstract Set<String> supportedFileAttributeViews();
244
245    /**
246     * Converts a path string, or a sequence of strings that when joined form
247     * a path string, to a {@code Path}. If {@code more} does not specify any
248     * elements then the value of the {@code first} parameter is the path string
249     * to convert. If {@code more} specifies one or more elements then each
250     * non-empty string, including {@code first}, is considered to be a sequence
251     * of name elements (see {@link Path}) and is joined to form a path string.
252     * The details as to how the Strings are joined is provider specific but
253     * typically they will be joined using the {@link #getSeparator
254     * name-separator} as the separator. For example, if the name separator is
255     * "{@code /}" and {@code getPath("/foo","bar","gus")} is invoked, then the
256     * path string {@code "/foo/bar/gus"} is converted to a {@code Path}.
257     * A {@code Path} representing an empty path is returned if {@code first}
258     * is the empty string and {@code more} does not contain any non-empty
259     * strings.
260     *
261     * <p> The parsing and conversion to a path object is inherently
262     * implementation dependent. In the simplest case, the path string is rejected,
263     * and {@link InvalidPathException} thrown, if the path string contains
264     * characters that cannot be converted to characters that are <em>legal</em>
265     * to the file store. For example, on UNIX systems, the NUL (&#92;u0000)
266     * character is not allowed to be present in a path. An implementation may
267     * choose to reject path strings that contain names that are longer than those
268     * allowed by any file store, and where an implementation supports a complex
269     * path syntax, it may choose to reject path strings that are <em>badly
270     * formed</em>.
271     *
272     * <p> In the case of the default provider, path strings are parsed based
273     * on the definition of paths at the platform or virtual file system level.
274     * For example, an operating system may not allow specific characters to be
275     * present in a file name, but a specific underlying file store may impose
276     * different or additional restrictions on the set of legal
277     * characters.
278     *
279     * <p> This method throws {@link InvalidPathException} when the path string
280     * cannot be converted to a path. Where possible, and where applicable,
281     * the exception is created with an {@link InvalidPathException#getIndex
282     * index} value indicating the first position in the {@code path} parameter
283     * that caused the path string to be rejected.
284     *
285     * @param   first
286     *          the path string or initial part of the path string
287     * @param   more
288     *          additional strings to be joined to form the path string
289     *
290     * @return  the resulting {@code Path}
291     *
292     * @throws  InvalidPathException
293     *          If the path string cannot be converted
294     */
295    public abstract Path getPath(String first, String... more);
296
297    /**
298     * Returns a {@code PathMatcher} that performs match operations on the
299     * {@code String} representation of {@link Path} objects by interpreting a
300     * given pattern.
301     *
302     * The {@code syntaxAndPattern} parameter identifies the syntax and the
303     * pattern and takes the form:
304     * <blockquote><pre>
305     * <i>syntax</i><b>:</b><i>pattern</i>
306     * </pre></blockquote>
307     * where {@code ':'} stands for itself.
308     *
309     * <p> A {@code FileSystem} implementation supports the "{@code glob}" and
310     * "{@code regex}" syntaxes, and may support others. The value of the syntax
311     * component is compared without regard to case.
312     *
313     * <p> When the syntax is "{@code glob}" then the {@code String}
314     * representation of the path is matched using a limited pattern language
315     * that resembles regular expressions but with a simpler syntax. For example:
316     *
317     * <table class="striped" style="text-align:left; margin-left:2em">
318     * <caption style="display:none">Pattern Language</caption>
319     * <thead>
320     * <tr>
321     *   <th scope="col">Example
322     *   <th scope="col">Description
323     * </tr>
324     * </thead>
325     * <tbody>
326     * <tr>
327     *   <th scope="row">{@code *.java}</th>
328     *   <td>Matches a path that represents a file name ending in {@code .java}</td>
329     * </tr>
330     * <tr>
331     *   <th scope="row">{@code *.*}</th>
332     *   <td>Matches file names containing a dot</td>
333     * </tr>
334     * <tr>
335     *   <th scope="row">{@code *.{java,class}}</th>
336     *   <td>Matches file names ending with {@code .java} or {@code .class}</td>
337     * </tr>
338     * <tr>
339     *   <th scope="row">{@code foo.?}</th>
340     *   <td>Matches file names starting with {@code foo.} and a single
341     *   character extension</td>
342     * </tr>
343     * <tr>
344     *   <th scope="row"><code>&#47;home&#47;*&#47;*</code>
345     *   <td>Matches <code>&#47;home&#47;gus&#47;data</code> on UNIX platforms</td>
346     * </tr>
347     * <tr>
348     *   <th scope="row"><code>&#47;home&#47;**</code>
349     *   <td>Matches <code>&#47;home&#47;gus</code> and
350     *   <code>&#47;home&#47;gus&#47;data</code> on UNIX platforms</td>
351     * </tr>
352     * <tr>
353     *   <th scope="row"><code>C:&#92;&#92;*</code>
354     *   <td>Matches <code>C:&#92;foo</code> and <code>C:&#92;bar</code> on the Windows
355     *   platform (note that the backslash is escaped; as a string literal in the
356     *   Java Language the pattern would be <code>"C:&#92;&#92;&#92;&#92;*"</code>) </td>
357     * </tr>
358     * </tbody>
359     * </table>
360     *
361     * <p> The following rules are used to interpret glob patterns:
362     *
363     * <ul>
364     *   <li><p> The {@code *} character matches zero or more {@link Character
365     *   characters} of a {@link Path#getName(int) name} component without
366     *   crossing directory boundaries. </p></li>
367     *
368     *   <li><p> The {@code **} characters matches zero or more {@link Character
369     *   characters} crossing directory boundaries. </p></li>
370     *
371     *   <li><p> The {@code ?} character matches exactly one character of a
372     *   name component.</p></li>
373     *
374     *   <li><p> The backslash character ({@code \}) is used to escape characters
375     *   that would otherwise be interpreted as special characters. The expression
376     *   {@code \\} matches a single backslash and "\{" matches a left brace
377     *   for example.  </p></li>
378     *
379     *   <li><p> The {@code [ ]} characters are a <i>bracket expression</i> that
380     *   match a single character of a name component out of a set of characters.
381     *   For example, {@code [abc]} matches {@code "a"}, {@code "b"}, or {@code "c"}.
382     *   The hyphen ({@code -}) may be used to specify a range so {@code [a-z]}
383     *   specifies a range that matches from {@code "a"} to {@code "z"} (inclusive).
384     *   These forms can be mixed so [abce-g] matches {@code "a"}, {@code "b"},
385     *   {@code "c"}, {@code "e"}, {@code "f"} or {@code "g"}. If the character
386     *   after the {@code [} is a {@code !} then it is used for negation so {@code
387     *   [!a-c]} matches any character except {@code "a"}, {@code "b"}, or {@code
388     *   "c"}.
389     *   <p> Within a bracket expression the {@code *}, {@code ?} and {@code \}
390     *   characters match themselves. The ({@code -}) character matches itself if
391     *   it is the first character within the brackets, or the first character
392     *   after the {@code !} if negating.</p></li>
393     *
394     *   <li><p> The {@code { }} characters are a group of subpatterns, where
395     *   the group matches if any subpattern in the group matches. The {@code ","}
396     *   character is used to separate the subpatterns. Groups cannot be nested.
397     *   </p></li>
398     *
399     *   <li><p> Leading period<code>&#47;</code>dot characters in file name are
400     *   treated as regular characters in match operations. For example,
401     *   the {@code "*"} glob pattern matches file name {@code ".login"}.
402     *   The {@link Files#isHidden} method may be used to test whether a file
403     *   is considered hidden.
404     *   </p></li>
405     *
406     *   <li><p> All other characters match themselves in an implementation
407     *   dependent manner. This includes characters representing any {@link
408     *   FileSystem#getSeparator name-separators}. </p></li>
409     *
410     *   <li><p> The matching of {@link Path#getRoot root} components is highly
411     *   implementation-dependent and is not specified. </p></li>
412     *
413     * </ul>
414     *
415     * <p> When the syntax is "{@code regex}" then the pattern component is a
416     * regular expression as defined by the {@link java.util.regex.Pattern}
417     * class.
418     *
419     * <p>  For both the glob and regex syntaxes, the matching details, such as
420     * whether the matching is case sensitive, are implementation-dependent
421     * and therefore not specified.
422     *
423     * @param   syntaxAndPattern
424     *          The syntax and pattern
425     *
426     * @return  A path matcher that may be used to match paths against the pattern
427     *
428     * @throws  IllegalArgumentException
429     *          If the parameter does not take the form: {@code syntax:pattern}
430     * @throws  java.util.regex.PatternSyntaxException
431     *          If the pattern is invalid
432     * @throws  UnsupportedOperationException
433     *          If the pattern syntax is not known to the implementation
434     *
435     * @see Files#newDirectoryStream(Path,String)
436     */
437    public abstract PathMatcher getPathMatcher(String syntaxAndPattern);
438
439    /**
440     * Returns the {@code UserPrincipalLookupService} for this file system
441     * <i>(optional operation)</i>. The resulting lookup service may be used to
442     * lookup user or group names.
443     *
444     * <p> <b>Usage Example:</b>
445     * Suppose we want to make "joe" the owner of a file:
446     * <pre>
447     *     UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
448     *     Files.setOwner(path, lookupService.lookupPrincipalByName("joe"));
449     * </pre>
450     *
451     * @throws  UnsupportedOperationException
452     *          If this {@code FileSystem} does not does have a lookup service
453     *
454     * @return  The {@code UserPrincipalLookupService} for this file system
455     */
456    public abstract UserPrincipalLookupService getUserPrincipalLookupService();
457
458    /**
459     * Constructs a new {@link WatchService} <i>(optional operation)</i>.
460     *
461     * <p> This method constructs a new watch service that may be used to watch
462     * registered objects for changes and events.
463     *
464     * @return  a new watch service
465     *
466     * @throws  UnsupportedOperationException
467     *          If this {@code FileSystem} does not support watching file system
468     *          objects for changes and events. This exception is not thrown
469     *          by {@code FileSystems} created by the default provider.
470     * @throws  IOException
471     *          If an I/O error occurs
472     */
473    public abstract WatchService newWatchService() throws IOException;
474}
475