private static void updateZip(File zip, File entry, String entryName) throws IOException { Map<String, String> env = new HashMap<>(); String uriPath = "jar:" + zip.toURI().toString(); URI uri = URI.create(uriPath); try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { fs.provider().checkAccess(fs.getPath(entryName), AccessMode.READ); Path target = fs.getPath(entryName); Path source = entry.toPath(); Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw e; } }
/** * Ensures configured directory {@code path} exists. * @throws IOException if {@code path} exists, but is not a directory, not accessible, or broken symbolic link. */ static void ensureDirectoryExists(Path path) throws IOException { // this isn't atomic, but neither is createDirectories. if (Files.isDirectory(path)) { // verify access, following links (throws exception if something is wrong) // we only check READ as a sanity test path.getFileSystem().provider().checkAccess(path.toRealPath(), AccessMode.READ); } else { // doesn't exist, or not a directory try { Files.createDirectories(path); } catch (FileAlreadyExistsException e) { // convert optional specific exception so the context is clear IOException e2 = new NotDirectoryException(path.toString()); e2.addSuppressed(e); throw e2; } } }
@Override public void checkAccess(final Path path, final AccessMode... modes) throws IOException { delegate.checkAccess(path, modes); }
@Override public void checkAccess(Path path, Set<? extends AccessMode> modes, LinkOption... linkOptions) throws IOException { if (isFollowLinks(linkOptions)) { delegate.checkAccess(resolveRelative(path), modes.toArray(new AccessMode[modes.size()])); } else if (modes.isEmpty()) { delegate.readAttributes(path, "isRegularFile", LinkOption.NOFOLLOW_LINKS); } else { throw new UnsupportedOperationException("CheckAccess for NIO Provider is unsupported with non empty AccessMode and NOFOLLOW_LINKS."); } }
@Override public void checkAccess(Path path, AccessMode... modes) throws IOException { BundleFileSystem fs = (BundleFileSystem) path.getFileSystem(); origProvider(path).checkAccess(fs.unwrap(path), modes); }
@Override public void checkAccess(Path path, AccessMode... modes) throws IOException { Path r = unroot(path); FileSystemProvider p = provider(r); p.checkAccess(r, modes); }
@Override public void checkAccess( final BitField<FsAccessOption> options, final FsNodeName name, final BitField<Access> types) throws IOException { final Path file = target.resolve(name.getPath()); final AccessMode[] modes = modes(types); file.getFileSystem().provider().checkAccess(file, modes); }
@Override public void checkAccess(Path path, AccessMode... modes) throws IOException { Path r = unroot(path); FileSystemProvider p = provider(r); p.checkAccess(r, modes); }
@Override public void checkAccess(Path path, AccessMode... modes) throws IOException { Path r = unroot(path); FileSystemProvider p = provider(r); p.checkAccess(r, modes); }
@Test @Category( Readonly.class ) public void testCheckAccessDoesNotSupportesWrite() throws IOException { // should not throw UnsupportedOperationException assertThatThrownBy( () -> FS.provider().checkAccess( getFile(), AccessMode.WRITE )).isInstanceOf( AccessDeniedException.class ); }
@Override public RootedPath toRealPath(LinkOption... options) throws IOException { RootedPath absolute = toAbsolutePath(); FileSystem fs = getFileSystem(); FileSystemProvider provider = fs.provider(); provider.checkAccess(absolute); return absolute; } }
@Override public SftpPath toRealPath(LinkOption... options) throws IOException { // TODO: handle links SftpPath absolute = toAbsolutePath(); FileSystem fs = getFileSystem(); FileSystemProvider provider = fs.provider(); provider.checkAccess(absolute); return absolute; } }
@Override public RootedPath toRealPath(LinkOption... options) throws IOException { RootedPath absolute = toAbsolutePath(); FileSystem fs = getFileSystem(); FileSystemProvider provider = fs.provider(); provider.checkAccess(absolute); return absolute; } }
@Override public RootedPath toRealPath(LinkOption... options) throws IOException { RootedPath absolute = toAbsolutePath(); FileSystem fs = getFileSystem(); FileSystemProvider provider = fs.provider(); provider.checkAccess(absolute); return absolute; } }
@Test public void testCheckAccessSupportesExecute() throws IOException { try { FS.provider().checkAccess( getFile(), AccessMode.EXECUTE ); } catch( AccessDeniedException e ) { // } catch( UnsupportedOperationException e ) { fail( "checkAccess must support EXECUTE" ); } }
@Test public void testCheckAccessSupportesRead() throws IOException { try { FS.provider().checkAccess( getFile(), AccessMode.READ ); } catch( AccessDeniedException e ) { // } catch( UnsupportedOperationException e ) { fail( "checkAccess must support READ" ); } }
@Test @Category( Writable.class ) public void testCheckAccessSupportesWrite() throws IOException { try { FS.provider().checkAccess( fileTAB(), AccessMode.WRITE ); } catch( AccessDeniedException e ) { // } catch( UnsupportedOperationException e ) { fail( "checkAccess must support WRITE" ); } }
@Test public void testCheckAccessUnnormalizedPath() throws IOException { try { FS.provider().checkAccess( unnormalize( getFile() ) ); } catch( Exception e ) { fail( "checkAccess fails to normalize" ); } }
@Test( expected = ClosedFileSystemException.class ) @Category( Closable.class ) public void testClosedFSCheckAccess() throws IOException { getClosedFS(); FS.provider().checkAccess( getClosedFileA() ); }
@Test @Category( WorkingDirectoryInPlaygroundTree.class ) public void testCheckAccessRelativePath() throws IOException { try { FS.provider().checkAccess( relativize( getFile() ) ); } catch( IOException e ) { fail( "checkAccess does not work with relative paths" ); } }