java.nio.file.AccessMode Java Examples

The following examples show how to use java.nio.file.AccessMode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HadoopFileSystem.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static FsAction toFsAction(Set<AccessMode> mode) {
  final char[] perms = new char[]{'-', '-', '-'};
  for (AccessMode m : mode) {
    switch (m) {
      case READ:
        perms[0] = 'r';
        break;

      case WRITE:
        perms[1] = 'w';
        break;

      case EXECUTE:
        perms[2] = 'x';
        break;
    }
  }
  return FsAction.getFsAction(new String(perms));
}
 
Example #2
Source File: Security.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example #3
Source File: TypeScriptMoreCombinedTest.java    From doov with Apache License 2.0 6 votes vote down vote up
@Test
void and_and_and_match_any_and_and() throws IOException {
    result = when(enumField.eq(AccessMode.WRITE)
            .and(booleanField1.isFalse())
            .and(matchAny(booleanField1.isTrue(),
                    booleanField2.not()
                            .and(zeroField.between(0, 1))))
            .and(zeroField.eq(1)))
            .validate().withShortCircuit(false).executeOn(model);
    ruleTs = jestExtension.toTS(result);

    TypeScriptAssertionContext script = parseAs(ruleTs, TypeScriptParser::script);

    assertFalse(result.value());
    assertThat(script).numberOfSyntaxErrors().isEqualTo(0);
    assertThat(script).identifierNamesText().containsExactly("when", "eq", "WRITE", "and", "eq", "and", "matchAny", "eq", "not",
            "and", "greaterOrEquals", "and", "lesserThan", "and", "eq", "validate");
    assertThat(script).identifierReferencesText().containsExactly("DOOV", "enumField", "boolean1", "DOOV", "boolean1",
            "boolean2", "zero", "zero", "zero");
    assertThat(script).identifierExpressionsText().containsExactly("AccessMode");
    assertThat(script).literalsText().containsExactly("false", "true", "0", "1", "1");
    assertThat(script).arrayLiteralsText().isEmpty();
}
 
Example #4
Source File: DremioHadoopFileSystemWrapper.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static FsAction toFsAction(Set<AccessMode> mode) {
  final char[] perms = new char[] { '-', '-', '-'};
  for(AccessMode m: mode) {
    switch(m) {
    case READ:
      perms[0] = 'r';
      break;

    case WRITE:
      perms[1] = 'w';
      break;

    case EXECUTE:
      perms[2] = 'x';
      break;
    }
  }
  return FsAction.getFsAction(new String(perms));
}
 
Example #5
Source File: DremioHadoopFileSystemWrapper.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static FsAction toFsAction(Set<AccessMode> mode) {
  final char[] perms = new char[] { '-', '-', '-'};
  for(AccessMode m: mode) {
    switch(m) {
    case READ:
      perms[0] = 'r';
      break;

    case WRITE:
      perms[1] = 'w';
      break;

    case EXECUTE:
      perms[2] = 'x';
      break;
    }
  }
  return FsAction.getFsAction(new String(perms));
}
 
Example #6
Source File: JFileSystemProvider.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException
{
  JPath jPath = (JPath) path;
  Status status;

  try {
    status = jPath.getBfsFile().getStatus();
  }
  catch (Exception e) {
    throw createIOException(e);
  }

  if (status.getType() == Status.FileType.FILE) {
    // do nothing
  }
  else if (status.getType() == Status.FileType.DIRECTORY) {
    // do nothing
  }
  else {
    throw new NoSuchFileException(path.toUri().toString());
  }
}
 
Example #7
Source File: FaultyFileSystem.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}
 
Example #8
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    MCRStoredNode node = MCRFileSystemUtils.resolvePath(mcrPath);
    if (node == null) {
        throw new NoSuchFileException(mcrPath.toString());
    }
    if (node instanceof MCRDirectory) {
        checkDirectory((MCRDirectory) node, modes);
    } else {
        checkFile((MCRFile) node, modes);
    }
}
 
Example #9
Source File: DremioHadoopFileSystemWrapper.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void access(final Path path, final Set<AccessMode> mode) throws AccessControlException, FileNotFoundException, IOException {
  try (WaitRecorder recorder = OperatorStats.getWaitRecorder(operatorStats)) {
    checkAccessAllowed(toHadoopPath(path), toFsAction(mode));
  } catch(FSError e) {
    throw propagateFSError(e);
  }
}
 
Example #10
Source File: ToStringMoreCombinedTest.java    From doov with Apache License 2.0 5 votes vote down vote up
@Test
void and_and_and_match_any_and_and() {
    rule = when(enumField.eq(AccessMode.WRITE)
            .and(booleanField1.isFalse())
            .and(matchAny(booleanField1.isTrue(),
                    booleanField2.not()
                            .and(zeroField.between(0, 1))))
            .and(zeroField.eq(1))).validate();
    assertThat(rule.readable(LOCALE)).isEqualTo(
            "rule when (((enum = WRITE and boolean 1 is false) and match any [boolean 1 is true, (boolean 2 not and (zero >= 0 and zero < 1))]) and zero = 1) validate");
}
 
Example #11
Source File: FaultyFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}
 
Example #12
Source File: ToStringMoreCombinedTest.java    From doov with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void beforeEach() {
    this.model = new GenericModel();
    this.enumField = model.enumField(AccessMode.READ, "enum");
    this.dateField1 = model.localDateField(LocalDate.now(), "date 1");
    this.dateField2 = model.localDateField(LocalDate.now(), "date 2");
    this.booleanField1 = model.booleanField(false, "boolean 1");
    this.booleanField2 = model.booleanField(false, "boolean 2");
    this.zeroField = model.intField(0, "zero");
}
 
Example #13
Source File: FaultyFileSystem.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}
 
Example #14
Source File: FaultyFileSystem.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}
 
Example #15
Source File: HtmlMoreCombinedTest.java    From doov with Apache License 2.0 5 votes vote down vote up
@Test
void and_and_and_match_any_and_and() {
    result = when(enumField.eq(AccessMode.WRITE)
            .and(booleanField1.isFalse())
            .and(matchAny(booleanField1.isTrue(),
                    booleanField2.not()
                            .and(zeroField.between(0, 1))))
            .and(zeroField.eq(1)))
                    .validate().withShortCircuit(false).executeOn(model);
    doc = documentOf(result);

    assertFalse(result.value());
    assertThat(doc).nary_OL().hasSize(1);
    assertThat(doc).binary_LI().hasSize(3);
    assertThat(doc).nary_LI().isEmpty();
    assertThat(doc).leaf_LI().isEmpty();
    assertThat(doc).when_UL().isEmpty();
    assertThat(doc).binary_UL().isEmpty();
    assertThat(doc).binaryChild_UL().isEmpty();
    assertThat(doc).unary_UL().isEmpty();
    assertThat(doc).percentageValue_DIV()
            .containsExactly("0 %", "100 %", "100 %", "0 %", "100 %", "100 %", "100 %", "0 %");
    assertThat(doc).tokenOperator_SPAN()
            .containsExactly("=", "and", "is", "and", "is", "not", "and", ">=", "and", "<", "and", "=");
    assertThat(doc).tokenValue_SPAN().containsExactly("WRITE", "false", "true", "0", "1", "1");
    assertThat(doc).tokenNary_SPAN().containsExactly("match any");

}
 
Example #16
Source File: HtmlMoreCombinedTest.java    From doov with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void beforeEach() {
    this.model = new GenericModel();
    this.enumField = model.enumField(AccessMode.READ, "enum");
    this.dateField1 = model.localDateField(LocalDate.now(), "date 1");
    this.dateField2 = model.localDateField(LocalDate.now(), "date 2");
    this.booleanField1 = model.booleanField(false, "boolean 1");
    this.booleanField2 = model.booleanField(false, "boolean 2");
    this.zeroField = model.intField(0, "zero");
}
 
Example #17
Source File: TypeScriptMoreCombinedTest.java    From doov with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void beforeEach() {
    this.model = new GenericModel();
    this.enumField = model.enumField(AccessMode.READ, "enumField");
    this.dateField1 = model.localDateField(LocalDate.now(), "date 1");
    this.dateField2 = model.localDateField(LocalDate.now(), "date 2");
    this.booleanField1 = model.booleanField(false, "boolean 1");
    this.booleanField2 = model.booleanField(false, "boolean 2");
    this.zeroField = model.intField(0, "zero");
}
 
Example #18
Source File: FaultyFileSystem.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}
 
Example #19
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}
 
Example #20
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void checkFile(MCRFile file, AccessMode... modes) throws AccessDeniedException {
    for (AccessMode mode : modes) {
        switch (mode) {
            case READ:
            case WRITE:
                break;
            case EXECUTE:
                throw new AccessDeniedException(MCRFileSystemUtils.toPath(file).toString(), null,
                    "Unsupported AccessMode: " + mode);
            default:
                throw new UnsupportedOperationException("Unsupported AccessMode: " + mode);
        }
    }
}
 
Example #21
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
    MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(path);
    MCRFilesystemNode node = resolvePath(mcrPath);
    if (node == null) {
        throw new NoSuchFileException(mcrPath.toString());
    }
    if (node instanceof MCRDirectory) {
        checkDirectory((MCRDirectory) node, modes);
    } else {
        checkFile((MCRFile) node, modes);
    }
}
 
Example #22
Source File: MCRFileSystemProvider.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void checkFile(MCRFile file, AccessMode... modes) throws AccessDeniedException {
    for (AccessMode mode : modes) {
        switch (mode) {
            case READ:
            case WRITE:
                break;
            case EXECUTE:
                throw new AccessDeniedException(file.toPath().toString(), null, "Unsupported AccessMode: " + mode);
            default:
                throw new UnsupportedOperationException("Unsupported AccessMode: " + mode);
        }
    }
}
 
Example #23
Source File: FileProviderBoot.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException
{
  BootJar jar = jar(path);
  
  if (jar == null) {
    throw new FileNotFoundException(L.l("{0} does not exist", path));
  }
}
 
Example #24
Source File: FileProviderClasspath.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException
{
  URL url = getURL(path);
  
  if (url == null) {
    throw new IOException(L.l("{0} does not exist", path));
  }
}
 
Example #25
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
void checkAccess(SFTPPath path, AccessMode... modes) throws IOException {
    try (Channel channel = channelPool.get()) {
        SftpATTRS attributes = getAttributes(channel, path, true);
        for (AccessMode mode : modes) {
            if (!hasAccess(attributes, mode)) {
                throw new AccessDeniedException(path.path());
            }
        }
    }
}
 
Example #26
Source File: SFTPFileSystem.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
private boolean hasAccess(SftpATTRS attrs, AccessMode mode) {
    switch (mode) {
    case READ:
        return PosixFilePermissionSupport.hasPermission(attrs.getPermissions(), PosixFilePermission.OWNER_READ);
    case WRITE:
        return PosixFilePermissionSupport.hasPermission(attrs.getPermissions(), PosixFilePermission.OWNER_WRITE);
    case EXECUTE:
        return PosixFilePermissionSupport.hasPermission(attrs.getPermissions(), PosixFilePermission.OWNER_EXECUTE);
    default:
        return false;
    }
}
 
Example #27
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Disabled("On Windows, the permissions are not reported correctly, but always as rw-rw-rw-")
@Test
public void testCheckAccessOnlyWriteReadOnly() throws IOException {
    Path bar = addDirectory("/foo/bar");
    bar.toFile().setReadOnly();

    AccessDeniedException exception = assertThrows(AccessDeniedException.class,
            () -> fileSystem.checkAccess(createPath("/foo/bar"), AccessMode.WRITE));
    assertEquals("/foo/bar", exception.getFile());
}
 
Example #28
Source File: SFTPFileSystemTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckAccessOnlyExecute() throws IOException {
    Path bar = addFile("/foo/bar");
    bar.toFile().setReadOnly();

    AccessDeniedException exception = assertThrows(AccessDeniedException.class,
            () -> fileSystem.checkAccess(createPath("/foo/bar"), AccessMode.EXECUTE));
    assertEquals("/foo/bar", exception.getFile());
}
 
Example #29
Source File: IdentityTest.java    From sftp-fs with Apache License 2.0 5 votes vote down vote up
private void testLoginSuccess(Identity identity) throws IOException {
    SFTPEnvironment env = createEnv()
            .withUserInfo(null)
            .withIdentity(identity);
    try (FileSystem fileSystem = new SFTPFileSystemProvider().newFileSystem(getURI(), env)) {
        fileSystem.provider().checkAccess(createPath("/"), AccessMode.READ);
    }
}
 
Example #30
Source File: FaultyFileSystem.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void checkAccess(Path file, AccessMode... modes)
    throws IOException
{
    triggerEx(file, "checkAccess");
    // hack
    if (modes.length == 0) {
        if (Files.exists(unwrap(file)))
            return;
        else
            throw new NoSuchFileException(file.toString());
    }
    throw new RuntimeException("not implemented yet");
}