本文整理汇总了Java中java.nio.file.SecureDirectoryStream类的典型用法代码示例。如果您正苦于以下问题:Java SecureDirectoryStream类的具体用法?Java SecureDirectoryStream怎么用?Java SecureDirectoryStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SecureDirectoryStream类属于java.nio.file包,在下文中一共展示了SecureDirectoryStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: deleteRecursivelySecure
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
/**
* Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of
* exceptions that occurred or null if no exceptions were thrown.
*/
@Nullable
private static Collection<IOException> deleteRecursivelySecure(
SecureDirectoryStream<Path> dir, Path path) {
Collection<IOException> exceptions = null;
try {
if (isDirectory(dir, path, NOFOLLOW_LINKS)) {
try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) {
exceptions = deleteDirectoryContentsSecure(childDir);
}
// If exceptions is not null, something went wrong trying to delete the contents of the
// directory, so we shouldn't try to delete the directory as it will probably fail.
if (exceptions == null) {
dir.deleteDirectory(path);
}
} else {
dir.deleteFile(path);
}
return exceptions;
} catch (IOException e) {
return addException(exceptions, e);
}
}
示例2: move
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Override
public void move(
Path srcpath,
SecureDirectoryStream<Path> targetdir,
Path targetpath) throws IOException {
EphemeralFsPath efsSrcPath = cast(srcpath);
EphemeralFsPath efsTargetPath = cast(targetpath);
EphemeralFsSecureDirectoryStream efsTargetDir = cast(targetdir);
synchronized(efsSrcPath.fs.fsLock) {
EphemeralFsPath actualSrcPath = translate(efsSrcPath);
EphemeralFsPath actualTargetPath = efsTargetDir.translate(efsTargetPath);
efsSrcPath.fs.move(actualSrcPath, actualTargetPath, new CopyOption[] {StandardCopyOption.ATOMIC_MOVE});
}
}
示例3: deleteRecursivelySecure
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
/**
* Secure recursive delete using {@code SecureDirectoryStream}. Returns a collection of exceptions
* that occurred or null if no exceptions were thrown.
*/
@NullableDecl
private static Collection<IOException> deleteRecursivelySecure(
SecureDirectoryStream<Path> dir, Path path) {
Collection<IOException> exceptions = null;
try {
if (isDirectory(dir, path, NOFOLLOW_LINKS)) {
try (SecureDirectoryStream<Path> childDir = dir.newDirectoryStream(path, NOFOLLOW_LINKS)) {
exceptions = deleteDirectoryContentsSecure(childDir);
}
// If exceptions is not null, something went wrong trying to delete the contents of the
// directory, so we shouldn't try to delete the directory as it will probably fail.
if (exceptions == null) {
dir.deleteDirectory(path);
}
} else {
dir.deleteFile(path);
}
return exceptions;
} catch (IOException e) {
return addException(exceptions, e);
}
}
示例4: move
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Override
public void move(Path srcPath, SecureDirectoryStream<Path> targetDir, Path targetPath)
throws IOException {
checkOpen();
JimfsPath checkedSrcPath = checkPath(srcPath);
JimfsPath checkedTargetPath = checkPath(targetPath);
if (!(targetDir instanceof JimfsSecureDirectoryStream)) {
throw new ProviderMismatchException(
"targetDir isn't a secure directory stream associated with this file system");
}
JimfsSecureDirectoryStream checkedTargetDir = (JimfsSecureDirectoryStream) targetDir;
view.copy(
checkedSrcPath,
checkedTargetDir.view,
checkedTargetPath,
ImmutableSet.<CopyOption>of(),
true);
}
示例5: testSecureDirectoryStreamBasedOnRelativePath
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Test
public void testSecureDirectoryStreamBasedOnRelativePath() throws IOException {
Files.createDirectories(path("foo"));
Files.createFile(path("foo/a"));
Files.createFile(path("foo/b"));
Files.createDirectory(path("foo/c"));
Files.createFile(path("foo/c/d"));
Files.createFile(path("foo/c/e"));
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path("foo"))) {
SecureDirectoryStream<Path> secureStream = (SecureDirectoryStream<Path>) stream;
assertThat(ImmutableList.copyOf(secureStream))
.containsExactly(path("foo/a"), path("foo/b"), path("foo/c"));
try (DirectoryStream<Path> stream2 = secureStream.newDirectoryStream(path("c"))) {
assertThat(ImmutableList.copyOf(stream2)).containsExactly(path("foo/c/d"), path("foo/c/e"));
}
}
}
示例6: deleteRecursively
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
/**
* Deletes the file or directory at the given {@code path} recursively. Deletes symbolic links,
* not their targets (subject to the caveat below).
*
* <p>If an I/O exception occurs attempting to read, open or delete any file under the given
* directory, this method skips that file and continues. All such exceptions are collected and,
* after attempting to delete all files, an {@code IOException} is thrown containing those
* exceptions as {@linkplain Throwable#getSuppressed() suppressed exceptions}.
*
* <h2>Warning: Security of recursive deletes</h2>
*
* <p>On a file system that supports symbolic links and does <i>not</i> support
* {@link SecureDirectoryStream}, it is possible for a recursive delete to delete files and
* directories that are <i>outside</i> the directory being deleted. This can happen if, after
* checking that a file is a directory (and not a symbolic link), that directory is replaced by a
* symbolic link to an outside directory before the call that opens the directory to read its
* entries.
*
* <p>By default, this method throws {@link InsecureRecursiveDeleteException} if it can't
* guarantee the security of recursive deletes. If you wish to allow the recursive deletes
* anyway, pass {@link RecursiveDeleteOption#ALLOW_INSECURE} to this method to override that
* behavior.
*
* @throws NoSuchFileException if {@code path} does not exist <i>(optional specific
* exception)</i>
* @throws InsecureRecursiveDeleteException if the security of recursive deletes can't be
* guaranteed for the file system and {@link RecursiveDeleteOption#ALLOW_INSECURE} was not
* specified
* @throws IOException if {@code path} or any file in the subtree rooted at it can't be deleted
* for any reason
*/
public static void deleteRecursively(
Path path, RecursiveDeleteOption... options) throws IOException {
Path parentPath = getParentPath(path);
if (parentPath == null) {
throw new FileSystemException(path.toString(), null, "can't delete recursively");
}
Collection<IOException> exceptions = null; // created lazily if needed
try {
boolean sdsSupported = false;
try (DirectoryStream<Path> parent = Files.newDirectoryStream(parentPath)) {
if (parent instanceof SecureDirectoryStream) {
sdsSupported = true;
exceptions = deleteRecursivelySecure(
(SecureDirectoryStream<Path>) parent, path.getFileName());
}
}
if (!sdsSupported) {
checkAllowsInsecure(path, options);
exceptions = deleteRecursivelyInsecure(path);
}
} catch (IOException e) {
if (exceptions == null) {
throw e;
} else {
exceptions.add(e);
}
}
if (exceptions != null) {
throwDeleteFailed(path, exceptions);
}
}
示例7: deleteDirectoryContentsSecure
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
/**
* Secure method for deleting the contents of a directory using {@code SecureDirectoryStream}.
* Returns a collection of exceptions that occurred or null if no exceptions were thrown.
*/
@Nullable
private static Collection<IOException> deleteDirectoryContentsSecure(
SecureDirectoryStream<Path> dir) {
Collection<IOException> exceptions = null;
try {
for (Path path : dir) {
exceptions = concat(exceptions, deleteRecursivelySecure(dir, path.getFileName()));
}
return exceptions;
} catch (DirectoryIteratorException e) {
return addException(exceptions, e.getCause());
}
}
示例8: isDirectory
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
/**
* Returns whether or not the file with the given name in the given dir is a directory.
*/
private static boolean isDirectory(
SecureDirectoryStream<Path> dir, Path name, LinkOption... options) throws IOException {
return dir.getFileAttributeView(name, BasicFileAttributeView.class, options)
.readAttributes()
.isDirectory();
}
示例9: newDirectoryStream
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Override
public SecureDirectoryStream<Path> newDirectoryStream(Path path, LinkOption... options) throws IOException {
checkClosed();
MCRPath mcrPath = checkFileSystem(path);
if (mcrPath.isAbsolute()) {
return (SecureDirectoryStream<Path>) Files.newDirectoryStream(mcrPath);
}
MCRFilesystemNode childByPath = dir.getChildByPath(mcrPath.toString());
if (childByPath == null || childByPath instanceof MCRFile) {
throw new NoSuchFileException(dir.toString(), path.toString(), "Does not exist or is a file.");
}
return new MCRDirectoryStream((MCRDirectory) childByPath, MCRPath.toMCRPath(path.resolve(mcrPath)));
}
示例10: move
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Override
public void move(Path srcpath, SecureDirectoryStream<Path> targetdir, Path targetpath) throws IOException {
checkClosed();
checkFileSystem(srcpath);
checkFileSystem(targetpath);
throw new AtomicMoveNotSupportedException(srcpath.toString(), targetpath.toString(),
"Currently not implemented");
}
示例11: newDirectoryStream
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Override
public SecureDirectoryStream<Path> newDirectoryStream(Path path,
LinkOption... options) throws IOException {
EphemeralFsPath efsPath = cast(path);
synchronized(efsPath.fs.fsLock) {
EphemeralFsPath actualPath = translate(efsPath);
for(LinkOption option : options) {
if(option == LinkOption.NOFOLLOW_LINKS) {
ResolvedPath resolved = ResolvedPath.resolve(actualPath, true);
if(resolved.resolvedToSymbolicLink()) {
throw new FileSystemException(path + ": Too many levels of symbolic links");
}
}
}
return (SecureDirectoryStream<Path>) actualPath.fs.newDirectoryStream(
actualPath,
efsPath.isAbsolute() ? efsPath : myPath.resolve(efsPath),
new Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return true;
}
} );
}
}
示例12: cast
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
private EphemeralFsSecureDirectoryStream cast(SecureDirectoryStream<Path> p) {
if(!(p instanceof EphemeralFsSecureDirectoryStream)) {
throw new IllegalStateException("wrong file system:" + p);
}
EphemeralFsSecureDirectoryStream answer = (EphemeralFsSecureDirectoryStream) p;
if(answer.myPath.fs != myPath.fs) {
throw new IllegalStateException("wrong fs");
}
return answer;
}
示例13: setUp
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Before
public void setUp() throws IOException {
dir = root.resolve("dir");
Files.createDirectories(dir);
fixture = (SecureDirectoryStream<Path>) Files.newDirectoryStream(dir);
moveTo = root.resolve("moveTo");
Files.move(dir, moveTo);
}
示例14: testNewDirectoryStreamAfterMove
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Test
public void testNewDirectoryStreamAfterMove() throws IOException {
Files.createFile(moveTo.resolve("newFile"));
try(SecureDirectoryStream<Path> newStream = fixture.newDirectoryStream(root.getFileSystem().getPath("."))) {
assertFound(newStream, dir.resolve(".").resolve("newFile"));
}
}
示例15: testNewDirectoryStreamAfterMoveFailsSymlink
import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Test
public void testNewDirectoryStreamAfterMoveFailsSymlink() throws IOException {
Files.createDirectory(moveTo.resolve("newDir"));
Files.createSymbolicLink(moveTo.resolve("link"), moveTo.resolve("newDir"));
try(SecureDirectoryStream<Path> newStream = fixture.newDirectoryStream(root.getFileSystem().getPath("link"), LinkOption.NOFOLLOW_LINKS)) {
fail();
} catch(FileSystemException e) {
assertTrue(
e.getMessage(),
e.getMessage().contains("Too many levels of symbolic links"));
}
}