OpenJDK / nio / nio / jdk
changeset 711:928099dfe108
FileSystem#getRootDirectories/getFileStores return iterator over
same elements
FileSystemProvider#installedProviders should return List
author | alanb |
---|---|
date | Tue, 14 Oct 2008 09:59:39 +0100 |
parents | 6c350c38b283 |
children | 251f70664db0 |
files | src/share/classes/java/nio/file/FileSystem.java src/share/classes/java/nio/file/spi/FileSystemProvider.java src/solaris/classes/sun/nio/fs/UnixFileSystem.java src/windows/classes/sun/nio/fs/WindowsFileSystem.java |
diffstat | 4 files changed, 53 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/src/share/classes/java/nio/file/FileSystem.java Tue Oct 14 09:40:04 2008 +0100 +++ b/src/share/classes/java/nio/file/FileSystem.java Tue Oct 14 09:59:39 2008 +0100 @@ -180,12 +180,12 @@ * may result in the creation of a new file hierarchy with its own * top-level directory. * - * <p> When a security manager is installed, and it denies an unspecified - * permission to access the root directory, then the root directory is - * not included in the list returned by this method. In the case of the - * default provider, the {@link SecurityManager#checkRead(String)} method is - * invoked to check read access to the root directory in each file store - * hierarchy. + * <p> When a security manager is installed, it is invoked to check access + * to the each root directory. If denied, the root directory is not returned + * by the iterator. In the case of the default provider, the {@link + * SecurityManager#checkRead(String)} method is invoked to check read access + * to each root directory. It is system dependent if the permission checks + * are done when the iterator is obtained or during iteration. * * @return An object to iterate over the root directories */ @@ -198,13 +198,15 @@ * FileStore FileStores} for this file system. The order of the elements is * not defined and the file stores may change during the lifetime of the * Java virtual machine. When an I/O error occurs, perhaps because a file - * store is not accessible, then it is not returned by the iterator. When a - * security manager is installed, and it denies access to a file store, then - * the file store is not included. In the case of the default provider, the - * {@link SecurityManager#checkRead(String)} method is invoked to check read - * access to the <em>top-most</em> directory of the file system. Whether that - * directory corresponds to the root directory of a distinct file hierarchy - * is implementation specific. + * store is not accessible, then it is not returned by the iterator. + * + * <p> When a security manager is installed, it is invoked to check access + * to each file store. If denied, the file store is not returned by the + * iterator. In the case of the default provider, the {@link + * SecurityManager#checkRead(String)} method is invoked to check read access + * to the file store's <em>top-most</em> directory. It is system dependent + * if the permission checks are done when the iterator is obtained or during + * iteration. * * <p> <b>Usage Example:</b> * Suppose we want to print the space usage for all file stores:
--- a/src/share/classes/java/nio/file/spi/FileSystemProvider.java Tue Oct 14 09:40:04 2008 +0100 +++ b/src/share/classes/java/nio/file/spi/FileSystemProvider.java Tue Oct 14 09:59:39 2008 +0100 @@ -140,20 +140,20 @@ } /** - * Returns an object to iterate over the installed file system providers. + * Returns a list of the installed file system providers. * * <p> The first invocation of this method causes the default provider to be * initialized (if not already initialized) and loads any other installed * providers as described by the {@link FileSystems} class. * - * @return An object to iterate over the installed file system providers. The - * iterator returns at least one element, that is the default file + * @return An unmodifiable list of the installed file system providers. The + * list contains at least one element, that is the default file * system provider * * @throws ServiceConfigurationError * When an error occurs while loading a service provider */ - public static Iterable<FileSystemProvider> installedProviders() { + public static List<FileSystemProvider> installedProviders() { if (installedProviders == null) { // ensure default provider is initialized FileSystemProvider defaultProvider = FileSystems.getDefault().provider();
--- a/src/solaris/classes/sun/nio/fs/UnixFileSystem.java Tue Oct 14 09:40:04 2008 +0100 +++ b/src/solaris/classes/sun/nio/fs/UnixFileSystem.java Tue Oct 14 09:59:39 2008 +0100 @@ -131,16 +131,21 @@ */ @Override public final Iterable<Path> getRootDirectories() { - List<Path> roots; - try { - SecurityManager sm = System.getSecurityManager(); - if (sm != null) - sm.checkRead(rootDirectory.toString()); - roots = Collections.unmodifiableList(Arrays.asList((Path)rootDirectory)); - } catch (SecurityException x) { - roots = Collections.emptyList(); - } - return roots; + final List<Path> allowedList = + Collections.unmodifiableList(Arrays.asList((Path)rootDirectory)); + return new Iterable<Path>() { + public Iterator<Path> iterator() { + try { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) + sm.checkRead(rootDirectory.toString()); + return allowedList.iterator(); + } catch (SecurityException x) { + List<Path> disallowed = Collections.emptyList(); + return disallowed.iterator(); + } + } + }; } /** @@ -167,9 +172,8 @@ private final Iterator<UnixMountEntry> entries; private FileStore next; - FileStoreIterator(Iterator<UnixMountEntry> entries) { - this.entries = entries; - this.next = null; + FileStoreIterator() { + this.entries = getMountEntries().iterator(); } private FileStore readNext() { @@ -225,11 +229,9 @@ @Override public final Iterable<FileStore> getFileStores() { - Iterator<UnixMountEntry> entries = getMountEntries().iterator(); - final FileStoreIterator iterator = new FileStoreIterator(entries); return new Iterable<FileStore>() { public Iterator<FileStore> iterator() { - return iterator; + return new FileStoreIterator(); } }; }
--- a/src/windows/classes/sun/nio/fs/WindowsFileSystem.java Tue Oct 14 09:40:04 2008 +0100 +++ b/src/windows/classes/sun/nio/fs/WindowsFileSystem.java Tue Oct 14 09:59:39 2008 +0100 @@ -119,8 +119,8 @@ throw new UnsupportedOperationException(); } - @Override - public Iterable<Path> getRootDirectories() { + // return new iterator over root directories + private Iterator<Path> rootDirectoryIterator() { // FIXME - we should replace this with calls to FindFirstVolume and // FindNextVolume int drives = 0; @@ -150,19 +150,27 @@ result.add(WindowsPath.createFromNormalizedPath(this, root)); } } - return Collections.unmodifiableList(result); + return Collections.unmodifiableList(result).iterator(); + } + + @Override + public Iterable<Path> getRootDirectories() { + return new Iterable<Path>() { + public Iterator<Path> iterator() { + return rootDirectoryIterator(); + } + }; } /** * Iterator returned by getFileStores method. */ - private static class FileStoreIterator implements Iterator<FileStore> { + private class FileStoreIterator implements Iterator<FileStore> { private final Iterator<Path> roots; private FileStore next; - FileStoreIterator(Iterator<Path> roots) { - this.roots = roots; - this.next = null; + FileStoreIterator() { + this.roots = getRootDirectories().iterator(); } private FileStore readNext() { @@ -216,11 +224,9 @@ @Override public Iterable<FileStore> getFileStores() { - Iterator<Path> rootsIterator = getRootDirectories().iterator(); - final FileStoreIterator iterator = new FileStoreIterator(rootsIterator); return new Iterable<FileStore>() { public Iterator<FileStore> iterator() { - return iterator; + return new FileStoreIterator(); } }; }