List of usage examples for java.nio.file.attribute PosixFilePermission GROUP_WRITE
PosixFilePermission GROUP_WRITE
To view the source code for java.nio.file.attribute PosixFilePermission GROUP_WRITE.
Click Source Link
From source file:Helper.Helper.java
public static Set<PosixFilePermission> intToSetPosix(int chmod) { Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); int other = chmod % 10, group = (chmod / 10) % 10, owner = (chmod / 100) % 10; if (owner >= 4) { perms.add(PosixFilePermission.OWNER_READ); owner -= 4;/*from ww w .j ava 2 s.co m*/ } if (owner >= 2) { perms.add(PosixFilePermission.OWNER_WRITE); owner -= 2; } if (owner >= 1) { perms.add(PosixFilePermission.OWNER_EXECUTE); owner -= 1; } if (group >= 4) { perms.add(PosixFilePermission.GROUP_READ); group -= 4; } if (group >= 2) { perms.add(PosixFilePermission.GROUP_WRITE); group -= 2; } if (group >= 1) { perms.add(PosixFilePermission.GROUP_EXECUTE); group -= 1; } if (other >= 4) { perms.add(PosixFilePermission.OTHERS_READ); other -= 4; } if (other >= 2) { perms.add(PosixFilePermission.OTHERS_WRITE); other -= 2; } if (other >= 1) { perms.add(PosixFilePermission.OTHERS_EXECUTE); other -= 1; } return perms; }
From source file:net.spinetrak.rpitft.command.Command.java
private String init(final String script_) { final String VAR_TMP = "/var/tmp"; InputStream scriptIn = null;/*from w ww .jav a 2s . c o m*/ OutputStream scriptOut = null; final File script = new File(VAR_TMP + script_); final String dir = script.getParent(); if (null != dir) { if (!new File(dir).mkdirs()) { LOGGER.error("Unable to create dirs for " + dir); } } try { scriptIn = Command.class.getResourceAsStream(script_); scriptOut = new FileOutputStream(script); IOUtils.copy(scriptIn, scriptOut); final Set<PosixFilePermission> perms = new HashSet<>(); perms.add(PosixFilePermission.OWNER_EXECUTE); perms.add(PosixFilePermission.GROUP_EXECUTE); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.GROUP_READ); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.GROUP_WRITE); Files.setPosixFilePermissions(Paths.get(script.getAbsolutePath()), perms); } catch (final IOException ex_) { LOGGER.error(ex_.getMessage()); } finally { if (scriptIn != null) { try { scriptIn.close(); } catch (final IOException ex_) { LOGGER.error(ex_.getMessage()); } } if (scriptOut != null) { try { scriptOut.close(); } catch (final IOException ex_) { LOGGER.error(ex_.getMessage()); } } } return script.getAbsolutePath(); }
From source file:net.krotscheck.util.ResourceUtilTest.java
/** * Assert that we can read a resource as a string. * * @throws Exception Should not be thrown. *///from w ww .j av a 2s . c o m @Test public void testGetResourceAsString() throws Exception { String name = "/valid-resource-file.txt"; String content = ResourceUtil.getResourceAsString(name); Assert.assertEquals("valid resource content", content); String invalidName = "/invalid-resource-file.txt"; String invalidContent = ResourceUtil.getResourceAsString(invalidName); Assert.assertEquals("", invalidContent); // Make the file write only File resource = ResourceUtil.getFileForResource(name); Set<PosixFilePermission> oldPerms = Files.getPosixFilePermissions(resource.toPath()); Set<PosixFilePermission> perms = new HashSet<>(); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_EXECUTE); perms.add(PosixFilePermission.GROUP_WRITE); perms.add(PosixFilePermission.GROUP_EXECUTE); perms.add(PosixFilePermission.OTHERS_WRITE); perms.add(PosixFilePermission.OTHERS_EXECUTE); // Write only... Files.setPosixFilePermissions(resource.toPath(), perms); String writeOnlyName = "/valid-resource-file.txt"; String writeOnlyContent = ResourceUtil.getResourceAsString(writeOnlyName); Assert.assertEquals("", writeOnlyContent); Files.setPosixFilePermissions(resource.toPath(), oldPerms); }
From source file:ch.psi.zmq.receiver.FileReceiver.java
/** * Receive ZMQ messages with pilatus-1.0 header type and write the data part * to disk/*from w ww . ja v a 2s . c om*/ */ public void receive(Integer numImages) { try { done = false; counter = 0; counterDropped = 0; receive = true; context = ZMQ.context(1); socket = context.socket(ZMQ.PULL); socket.connect("tcp://" + hostname + ":" + port); ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() { }; String path = ""; // User lookup service UserPrincipalLookupService lookupservice = FileSystems.getDefault().getUserPrincipalLookupService(); Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.GROUP_READ); perms.add(PosixFilePermission.GROUP_WRITE); while (receive) { try { byte[] message = socket.recv(); byte[] content = null; if (socket.hasReceiveMore()) { content = socket.recv(); } logger.info("Message received: " + new String(message)); Map<String, Object> h = mapper.readValue(message, typeRef); if (!"pilatus-1.0".equals(h.get("htype"))) { logger.warning("Message type [" + h.get("htype") + "] not supported - ignore message"); continue; } String username = (String) h.get("username"); // Save content to file (in basedir) String p = (String) h.get("path"); if (!p.startsWith("/")) { p = basedir + "/" + p; } File f = new File(p); // if(!f.exists()){ if (!path.equals(p)) { if (username == null) { logger.info("Create directory " + p + ""); f.mkdirs(); } else { logger.info("Create directory " + p + " for user " + username); try { Set<PosixFilePermission> permissions = new HashSet<PosixFilePermission>(); permissions.add(PosixFilePermission.OWNER_READ); permissions.add(PosixFilePermission.OWNER_WRITE); permissions.add(PosixFilePermission.OWNER_EXECUTE); permissions.add(PosixFilePermission.GROUP_READ); permissions.add(PosixFilePermission.GROUP_WRITE); permissions.add(PosixFilePermission.GROUP_EXECUTE); // username and groupname is the same by // convention mkdir(f, lookupservice.lookupPrincipalByName(username), lookupservice.lookupPrincipalByGroupName(username), permissions); } catch (IOException e) { throw new RuntimeException("Unable to create directory for user " + username + "", e); } } path = p; } File file = new File(f, (String) h.get("filename")); logger.finest("Write to " + file.getAbsolutePath()); try (FileOutputStream s = new FileOutputStream(file)) { s.write(content); } if (username != null) { Files.setOwner(file.toPath(), lookupservice.lookupPrincipalByName(username)); // username and groupname is the same by convention Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS) .setGroup(lookupservice.lookupPrincipalByGroupName(username)); Files.setPosixFilePermissions(file.toPath(), perms); } counter++; if (numImages != null && numImages == counter) { break; } } catch (IOException e) { logger.log(Level.SEVERE, "", e); counterDropped++; } } } catch (Exception e) { if (receive) { e.printStackTrace(); } } }
From source file:net.krotscheck.jersey2.configuration.Jersey2ToolkitConfigTest.java
/** * Assert that an unreadable file does not throw an error. * * @throws java.io.IOException File operation errors. *///from www . j a v a 2 s. co m @Test public void testUnreadableFile() throws IOException { // Get the prop file and store the old permissions. File propFile = ResourceUtil.getFileForResource("jersey2-toolkit.properties"); Set<PosixFilePermission> oldPerms = Files.getPosixFilePermissions(propFile.toPath()); // Apply writeonly permission set. Set<PosixFilePermission> writeonly = new HashSet<>(); writeonly.add(PosixFilePermission.OWNER_WRITE); writeonly.add(PosixFilePermission.GROUP_WRITE); Files.setPosixFilePermissions(propFile.toPath(), writeonly); // Add something to check System.setProperty("property3", "override3"); // If this throws an error, we've got a problem. Configuration config = new Jersey2ToolkitConfig(); Assert.assertFalse(config.containsKey("property1")); Assert.assertFalse(config.containsKey("property2")); Assert.assertTrue(config.containsKey("property3")); // Apply the correct permissions again Files.setPosixFilePermissions(propFile.toPath(), oldPerms); }
From source file:org.everit.osgi.dev.maven.util.FileManager.java
private static void setPermissionsOnFile(final File file, final ZipArchiveEntry entry) throws IOException { if (entry.getPlatform() == ZipArchiveEntry.PLATFORM_FAT) { return;/*from w w w . j ava 2s . co m*/ } int unixPermissions = entry.getUnixMode(); Set<PosixFilePermission> perms = new HashSet<>(); if ((unixPermissions & OWNER_EXECUTE_BITMASK) > 0) { perms.add(PosixFilePermission.OWNER_EXECUTE); } if ((unixPermissions & GROUP_EXECUTE_BITMASK) > 0) { perms.add(PosixFilePermission.GROUP_EXECUTE); } if ((unixPermissions & OTHERS_EXECUTE_BITMASK) > 0) { perms.add(PosixFilePermission.OTHERS_EXECUTE); } if ((unixPermissions & OWNER_READ_BITMASK) > 0) { perms.add(PosixFilePermission.OWNER_READ); } if ((unixPermissions & GROUP_READ_BITMASK) > 0) { perms.add(PosixFilePermission.GROUP_READ); } if ((unixPermissions & OTHERS_READ_BITMASK) > 0) { perms.add(PosixFilePermission.OTHERS_READ); } if ((unixPermissions & OWNER_WRITE_BITMASK) > 0) { perms.add(PosixFilePermission.OWNER_WRITE); } if ((unixPermissions & GROUP_WRITE_BITMASK) > 0) { perms.add(PosixFilePermission.GROUP_WRITE); } if ((unixPermissions & OTHERS_WRITE_BITMASK) > 0) { perms.add(PosixFilePermission.OTHERS_WRITE); } Path path = file.toPath(); if (path.getFileSystem().supportedFileAttributeViews().contains("posix")) { Files.setPosixFilePermissions(path, perms); } else { setPermissionsOnFileInNonPosixSystem(file, perms); } }
From source file:com.linkedin.pinot.core.segment.index.converter.SegmentV1V2ToV3FormatConverter.java
private void setDirectoryPermissions(File v3Directory) throws IOException { EnumSet<PosixFilePermission> permissions = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE); Files.setPosixFilePermissions(v3Directory.toPath(), permissions); }
From source file:org.kitodo.command.CommandTest.java
private static void setFileExecuteable(File file) throws IOException { Set<PosixFilePermission> perms = new HashSet<>(); perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_EXECUTE); perms.add(PosixFilePermission.OTHERS_READ); perms.add(PosixFilePermission.OTHERS_WRITE); perms.add(PosixFilePermission.OTHERS_EXECUTE); perms.add(PosixFilePermission.GROUP_READ); perms.add(PosixFilePermission.GROUP_WRITE); perms.add(PosixFilePermission.GROUP_EXECUTE); Files.setPosixFilePermissions(file.toPath(), perms); }
From source file:net.krotscheck.util.ResourceUtilTest.java
/** * Assert that we can read a resource as a stream. * * @throws Exception Should not be thrown. *///w w w. java2 s . c om @Test public void testGetResourceAsStream() throws Exception { String name = "/valid-resource-file.txt"; InputStream stream = ResourceUtil.getResourceAsStream(name); Assert.assertTrue(stream.available() > 0); String invalidName = "/invalid-resource-file.txt"; InputStream invalidContent = ResourceUtil.getResourceAsStream(invalidName); Assert.assertTrue(invalidContent instanceof NullInputStream); Assert.assertTrue(invalidContent.available() == 0); // Make the file write only File resource = ResourceUtil.getFileForResource(name); Set<PosixFilePermission> oldPerms = Files.getPosixFilePermissions(resource.toPath()); Set<PosixFilePermission> perms = new HashSet<>(); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_EXECUTE); perms.add(PosixFilePermission.GROUP_WRITE); perms.add(PosixFilePermission.GROUP_EXECUTE); perms.add(PosixFilePermission.OTHERS_WRITE); perms.add(PosixFilePermission.OTHERS_EXECUTE); // Write only... Files.setPosixFilePermissions(resource.toPath(), perms); String writeOnlyName = "/valid-resource-file.txt"; InputStream writeOnlyContent = ResourceUtil.getResourceAsStream(writeOnlyName); Assert.assertTrue(writeOnlyContent instanceof NullInputStream); Assert.assertTrue(writeOnlyContent.available() == 0); Files.setPosixFilePermissions(resource.toPath(), oldPerms); }
From source file:org.assertj.examples.PathAssertionsExamples.java
@Test public void path_rwx_assertion() throws Exception { assumeTrue(SystemUtils.IS_OS_UNIX);//from www . j a v a 2 s . c om // Create a file and set permissions to be readable by all. write(rwxFile, "rwx file".getBytes()); // using PosixFilePermission to set file permissions 777 Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); // add owners permission perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_EXECUTE); // add group permissions perms.add(PosixFilePermission.GROUP_READ); perms.add(PosixFilePermission.GROUP_WRITE); perms.add(PosixFilePermission.GROUP_EXECUTE); // add others permissions perms.add(PosixFilePermission.OTHERS_READ); perms.add(PosixFilePermission.OTHERS_WRITE); perms.add(PosixFilePermission.OTHERS_EXECUTE); Files.setPosixFilePermissions(rwxFile, perms); final Path symlinkToRwxFile = FileSystems.getDefault().getPath("symlink-to-rwxFile"); if (!Files.exists(symlinkToRwxFile)) { createSymbolicLink(symlinkToRwxFile, rwxFile); } // The following assertions succeed: assertThat(rwxFile).isReadable().isWritable().isExecutable(); assertThat(symlinkToRwxFile).isReadable().isWritable().isExecutable(); }