• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java SecureDirectoryStream类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中java.nio.file.SecureDirectoryStream的典型用法代码示例。如果您正苦于以下问题:Java SecureDirectoryStream类的具体用法?Java SecureDirectoryStream怎么用?Java SecureDirectoryStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



SecureDirectoryStream类属于java.nio.file包,在下文中一共展示了SecureDirectoryStream类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:29,代码来源:MoreFiles.java


示例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});
    }
    
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:18,代码来源:EphemeralFsSecureDirectoryStream.java


示例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);
  }
}
 
开发者ID:google,项目名称:guava,代码行数:29,代码来源:MoreFiles.java


示例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);
}
 
开发者ID:google,项目名称:jimfs,代码行数:22,代码来源:JimfsSecureDirectoryStream.java


示例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"));
    }
  }
}
 
开发者ID:google,项目名称:jimfs,代码行数:21,代码来源:JimfsUnixLikeFileSystemTest.java


示例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);
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:66,代码来源:MoreFiles.java


示例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());
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:19,代码来源:MoreFiles.java


示例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();
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:10,代码来源:MoreFiles.java


示例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)));
}
 
开发者ID:MyCoRe-Org,项目名称:mycore,代码行数:14,代码来源:MCRDirectoryStream.java


示例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");
}
 
开发者ID:MyCoRe-Org,项目名称:mycore,代码行数:9,代码来源:MCRDirectoryStream.java


示例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;
            }
        } );
    }
    
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:28,代码来源:EphemeralFsSecureDirectoryStream.java


示例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;
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:11,代码来源:EphemeralFsSecureDirectoryStream.java


示例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);
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:10,代码来源:SecureDirectoryStreamTest.java


示例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"));
    }
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:9,代码来源:SecureDirectoryStreamTest.java


示例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"));
    }
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:15,代码来源:SecureDirectoryStreamTest.java


示例16: testNewDirectoryStreamAfterMoveMultiLevel

import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Test
public void testNewDirectoryStreamAfterMoveMultiLevel() throws IOException {
    Files.createDirectory(moveTo.resolve("child"));
    
    Files.createFile(moveTo.resolve("child").resolve("newFile"));
    
    try(SecureDirectoryStream<Path> newStream = fixture.newDirectoryStream(root.getFileSystem().getPath("child"))) {
        assertFound(newStream, dir.resolve("child").resolve("newFile"));
    }
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:11,代码来源:SecureDirectoryStreamTest.java


示例17: testDirectoryStreamIsNotSecure

import java.nio.file.SecureDirectoryStream; //导入依赖的package包/类
@Test
public void testDirectoryStreamIsNotSecure() throws Exception {
	try(DirectoryStream<Path> stream = Files.newDirectoryStream(root)) {
		assertFalse(stream instanceof SecureDirectoryStream);
		
	}
}
 
开发者ID:sbridges,项目名称:ephemeralfs,代码行数:8,代码来源:WindowsTest.java


示例18: 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);
  }
}
 
开发者ID:google,项目名称:guava,代码行数:63,代码来源:MoreFiles.java


示例19: 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.
 */
@NullableDecl
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());
  }
}
 
开发者ID:google,项目名称:guava,代码行数:19,代码来源:MoreFiles.java


示例20: 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();
}
 
开发者ID:google,项目名称:guava,代码行数:8,代码来源:MoreFiles.java



注:本文中的java.nio.file.SecureDirectoryStream类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java Receipt类代码示例发布时间:2022-05-23
下一篇:
Java IdentityrefTypeDefinition类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap