List of usage examples for java.nio.file Files setPosixFilePermissions
public static Path setPosixFilePermissions(Path path, Set<PosixFilePermission> perms) throws IOException
From source file:com.lithium.flow.store.FileStore.java
private void write(@Nonnull Map<String, String> map) { try {/* w w w .j a va 2 s. com*/ mapper.writeValue(file, map); Files.setPosixFilePermissions(file.toPath(), Sets.newHashSet(OWNER_READ, OWNER_WRITE, GROUP_READ, GROUP_WRITE)); } catch (IOException e) { throw new StoreException("failed to write " + file, e); } }
From source file:org.syncany.tests.scenarios.ChangedAttributesScenarioTest.java
@Test public void testChangeAttributes() throws Exception { // Setup //from w ww . ja v a2s . c o m TransferSettings testConnection = TestConfigUtil.createTestLocalConnection(); TestClient clientA = new TestClient("A", testConnection); TestClient clientB = new TestClient("B", testConnection); // Run clientA.createNewFile("file1.jpg"); clientA.upWithForceChecksum(); clientB.down(); File bFile = clientB.getLocalFile("file1.jpg"); Path bFilePath = Paths.get(bFile.getAbsolutePath()); if (EnvironmentUtil.isWindows()) { Files.setAttribute(bFilePath, "dos:readonly", true); } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) { Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx")); } StatusOperationResult statusResult = clientB.status(); assertNotNull(statusResult); ChangeSet changes = statusResult.getChangeSet(); assertTrue("Status-Operation should return changes.", changes.hasChanges()); UpOperationResult upResult = clientB.up(); StatusOperationResult statusResultFromUp = upResult.getStatusResult(); // Test 1: Check result sets for inconsistencies assertTrue("Status should return changes.", statusResultFromUp.getChangeSet().hasChanges()); assertTrue("File should be uploaded.", upResult.getChangeSet().hasChanges()); // Test 2: Check database for inconsistencies SqlDatabase database = clientB.loadLocalDatabase(); assertNotNull("File should be uploaded.", database.getFileVersionByPath("file1.jpg")); assertEquals("There should be a new database version, because file should not have been added.", 2, database.getLocalDatabaseBranch().size()); // B down clientA.down(); // Test 1: file1.jpg permissions File aFile = clientA.getLocalFile("file1.jpg"); Path aFilePath = Paths.get(aFile.getAbsolutePath()); if (EnvironmentUtil.isWindows()) { Object readOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly"); assertTrue("Read-only should be true.", (Boolean) readOnlyAttribute); } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) { Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(aFilePath); assertEquals("Should be rwxrwxrwx.", "rwxrwxrwx", PosixFilePermissions.toString(posixFilePermissions)); } // Test 2: The rest assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead()); assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile()); // Tear down clientA.deleteTestData(); clientB.deleteTestData(); }
From source file:org.syncany.tests.integration.scenarios.ChangedAttributesScenarioTest.java
@Test public void testChangeAttributes() throws Exception { // Setup /*from w w w. j av a 2 s. c o m*/ TransferSettings testConnection = TestConfigUtil.createTestLocalConnection(); TestClient clientA = new TestClient("A", testConnection); TestClient clientB = new TestClient("B", testConnection); // Run clientA.createNewFile("file1.jpg"); clientA.upWithForceChecksum(); clientB.down(); File bFile = clientB.getLocalFile("file1.jpg"); Path bFilePath = Paths.get(bFile.getAbsolutePath()); if (EnvironmentUtil.isWindows()) { Files.setAttribute(bFilePath, "dos:readonly", true); } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) { Files.setPosixFilePermissions(bFilePath, PosixFilePermissions.fromString("rwxrwxrwx")); } StatusOperationResult statusResult = clientB.status(); assertNotNull(statusResult); ChangeSet changes = statusResult.getChangeSet(); assertTrue("Status-Operation should return changes.", changes.hasChanges()); UpOperationResult upResult = clientB.up(); StatusOperationResult statusResultFromUp = upResult.getStatusResult(); // Test 1: Check result sets for inconsistencies assertTrue("Status should return changes.", statusResultFromUp.getChangeSet().hasChanges()); assertTrue("File should be uploaded.", upResult.getChangeSet().hasChanges()); // Test 2: Check database for inconsistencies SqlDatabase database = clientB.loadLocalDatabase(); assertEquals("File should be uploaded.", 1, database.getFileList("file1.jpg", null, false, false, false, null).size()); assertEquals("There should be a new database version, because file should not have been added.", 2, database.getLocalDatabaseBranch().size()); // B down clientA.down(); // Test 1: file1.jpg permissions File aFile = clientA.getLocalFile("file1.jpg"); Path aFilePath = Paths.get(aFile.getAbsolutePath()); if (EnvironmentUtil.isWindows()) { Object readOnlyAttribute = Files.getAttribute(aFilePath, "dos:readonly"); assertTrue("Read-only should be true.", (Boolean) readOnlyAttribute); } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) { Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(aFilePath); assertEquals("Should be rwxrwxrwx.", "rwxrwxrwx", PosixFilePermissions.toString(posixFilePermissions)); } // Test 2: The rest assertFileListEquals(clientA.getLocalFilesExcludeLockedAndNoRead(), clientB.getLocalFilesExcludeLockedAndNoRead()); assertSqlDatabaseEquals(clientA.getDatabaseFile(), clientB.getDatabaseFile()); // Tear down clientA.deleteTestData(); clientB.deleteTestData(); }
From source file:com.sonar.scanner.api.it.tools.CommandExecutor.java
public int execute(String[] args, Map<String, String> env, @Nullable Path workingDir) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }//from w ww. j a va2s . c o m watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args, false); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); return exec.execute(cmd, env); }
From source file:com.spectralogic.ds3client.metadata.MetadataAccessImpl_Test.java
@Test public void testGettingMetadata() throws IOException, InterruptedException { final String tempPathPrefix = null; final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix); final String fileName = "Gracie.txt"; try {//from w w w . j a v a 2 s .c o m final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName)); if (!Platform.isWindows()) { final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class); final Set<PosixFilePermission> permissions = attributes.permissions(); permissions.clear(); permissions.add(PosixFilePermission.OWNER_READ); permissions.add(PosixFilePermission.OWNER_WRITE); Files.setPosixFilePermissions(filePath, permissions); } final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder(); fileMapper.put(filePath.toString(), filePath); final Map<String, String> metadata = new MetadataAccessImpl(fileMapper.build()) .getMetadataValue(filePath.toString()); if (Platform.isWindows()) { assertEquals(metadata.size(), 13); } else { assertEquals(metadata.size(), 10); } if (Platform.isMac()) { assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS) .toLowerCase().startsWith(Platform.MAC_SYSTEM_NAME)); } else if (Platform.isLinux()) { assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS) .toLowerCase().startsWith(Platform.LINUX_SYSTEM_NAME)); } else if (Platform.isWindows()) { assertTrue(metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_OS) .toLowerCase().startsWith(Platform.WINDOWS_SYSTEM_NAME)); } if (Platform.isWindows()) { assertEquals("A", metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_FLAGS)); } else { assertEquals("100600", metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_MODE)); assertEquals("600(rw-------)", metadata.get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_PERMISSION)); } } finally { FileUtils.deleteDirectory(tempDirectory.toFile()); } }
From source file:its.tools.CommandExecutor.java
public void execute(String[] args, @Nullable Path workingDir) throws IOException { if (!Files.isExecutable(file)) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(file, perms); }//from w w w. j ava 2 s. com watchdog = new ExecuteWatchdog(TIMEOUT); CommandLine cmd = new CommandLine(file.toFile()); cmd.addArguments(args); DefaultExecutor exec = new DefaultExecutor(); exec.setWatchdog(watchdog); exec.setStreamHandler(createStreamHandler()); exec.setExitValues(null); if (workingDir != null) { exec.setWorkingDirectory(workingDir.toFile()); } in.close(); LOG.info("Executing: {}", cmd.toString()); exec.execute(cmd, new ResultHander()); }
From source file:com.spectralogic.ds3client.metadata.MetadataReceivedListenerImpl_Test.java
@Test public void testGettingMetadata() throws IOException, InterruptedException { final String tempPathPrefix = null; final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix); final String fileName = "Gracie.txt"; final Path filePath = Files.createFile(Paths.get(tempDirectory.toString(), fileName)); try {//w ww . j a v a2s .c o m // set permissions if (!Platform.isWindows()) { final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class); final Set<PosixFilePermission> permissions = attributes.permissions(); permissions.clear(); permissions.add(PosixFilePermission.OWNER_READ); permissions.add(PosixFilePermission.OWNER_WRITE); Files.setPosixFilePermissions(filePath, permissions); } // get permissions final ImmutableMap.Builder<String, Path> fileMapper = ImmutableMap.builder(); fileMapper.put(filePath.toString(), filePath); final ImmutableMap<String, Path> immutableFileMapper = fileMapper.build(); final Map<String, String> metadataFromFile = new MetadataAccessImpl(immutableFileMapper) .getMetadataValue(filePath.toString()); // change permissions if (Platform.isWindows()) { Runtime.getRuntime().exec("attrib -A " + filePath.toString()).waitFor(); } else { final PosixFileAttributes attributes = Files.readAttributes(filePath, PosixFileAttributes.class); final Set<PosixFilePermission> permissions = attributes.permissions(); permissions.clear(); permissions.add(PosixFilePermission.OWNER_READ); permissions.add(PosixFilePermission.OWNER_WRITE); permissions.add(PosixFilePermission.OWNER_EXECUTE); Files.setPosixFilePermissions(filePath, permissions); } // put old permissions back final Metadata metadata = new MetadataImpl(new MockedHeadersReturningKeys(metadataFromFile)); new MetadataReceivedListenerImpl(tempDirectory.toString()).metadataReceived(fileName, metadata); // see that we put back the original metadata fileMapper.put(filePath.toString(), filePath); final Map<String, String> metadataFromUpdatedFile = new MetadataAccessImpl(immutableFileMapper) .getMetadataValue(filePath.toString()); if (Platform.isWindows()) { assertEquals("A", metadataFromUpdatedFile .get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_FLAGS)); } else { assertEquals("100600", metadataFromUpdatedFile .get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_MODE)); assertEquals("600(rw-------)", metadataFromUpdatedFile .get(MetadataKeyConstants.METADATA_PREFIX + MetadataKeyConstants.KEY_PERMISSION)); } } finally { FileUtils.deleteDirectory(tempDirectory.toFile()); } }
From source file:de.tudarmstadt.ukp.dkpro.discourse.pdtbparser.PDTBParserWrapper.java
public PDTBParserWrapper() throws IOException { tempDirectory = Files.createTempDirectory("temp_pdtb"); // FileUtils.copyFileToDirectory(); File tempDir = tempDirectory.toFile(); File tmpFile = File.createTempFile("tmp_pdtb", ".zip"); InputStream stream = getClass().getClassLoader().getResourceAsStream("pdtb-parser-v120415.zip"); FileUtils.copyInputStreamToFile(stream, tmpFile); ZipFile zipFile;//from www . ja v a 2 s . co m try { zipFile = new ZipFile(tmpFile); zipFile.extractAll(tempDir.getAbsolutePath()); } catch (ZipException e) { throw new IOException(e); } // delete temp file FileUtils.forceDelete(tmpFile); String folderPrefix = "/pdtb-parser-v120415/src"; String srcDir = tempDir.getCanonicalPath() + folderPrefix; // copy rewritten rb files copyFiles(new File(srcDir), "article.rb", "parser.rb"); Files.walkFileTree(tempDirectory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Set<PosixFilePermission> permissions = new HashSet<>(); permissions.add(PosixFilePermission.OWNER_EXECUTE); permissions.add(PosixFilePermission.GROUP_EXECUTE); permissions.add(PosixFilePermission.OTHERS_EXECUTE); permissions.add(PosixFilePermission.OWNER_READ); permissions.add(PosixFilePermission.GROUP_READ); permissions.add(PosixFilePermission.OTHERS_READ); Files.setPosixFilePermissions(file, permissions); return super.visitFile(file, attrs); } }); parserRubyScript = srcDir + "/parser.rb"; System.out.println(parserRubyScript); }
From source file:org.jenkinsci.sshd.SshdTest.java
/** * Get encrypted Private Key File// w w w. j a va2 s .c om */ private File getEncryptedPrivateKey() { if (privateKeyEnc == null) { try { privateKeyEnc = File.createTempFile("ssh_enc", "key"); privateKeyEnc.deleteOnExit(); FileUtils.copyURLToFile(SshdTest.class.getResource("/sshd/unsafe_enc_key"), privateKeyEnc); Files.setPosixFilePermissions(privateKeyEnc.toPath(), EnumSet.of(OWNER_READ)); } catch (IOException e) { throw new RuntimeException( "Not able to get the encrypted SSH key file. Missing file, wrong file permissions?!"); } } return privateKeyEnc; }
From source file:org.apache.zeppelin.interpreter.InterpreterInfoSaving.java
public void saveToFile(Path file) throws IOException { if (!Files.exists(file)) { Files.createFile(file);/* w w w .ja va 2s. c o m*/ try { Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE); Files.setPosixFilePermissions(file, permissions); } catch (UnsupportedOperationException e) { // File system does not support Posix file permissions (likely windows) - continue anyway. LOGGER.warn("unable to setPosixFilePermissions on '{}'.", file); } ; } LOGGER.info("Save Interpreter Settings to " + file); IOUtils.write(this.toJson(), new FileOutputStream(file.toFile())); }