List of usage examples for java.nio.file.attribute PosixFilePermission GROUP_READ
PosixFilePermission GROUP_READ
To view the source code for java.nio.file.attribute PosixFilePermission GROUP_READ.
Click Source Link
From source file:Main.java
public static void updatePermissions(PosixFileAttributeView posixView) throws Exception { Set<PosixFilePermission> permissions = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ); posixView.setPermissions(permissions); System.out.println("Permissions set successfully."); }
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.ja v a 2s . c om*/ } 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:org.esa.s2tbx.dataio.openjpeg.OpenJPEGActivator.java
private static void setExecutablePermissions(Path executablePathName) { if (IS_OS_UNIX) { Set<PosixFilePermission> permissions = new HashSet<>(Arrays.asList(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE)); try {/*from w w w . jav a 2 s . c om*/ Files.setPosixFilePermissions(executablePathName, permissions); } catch (IOException e) { // can't set the permissions for this file, eg. the file was installed as root // send a warning message, user will have to do that by hand. SystemUtils.LOG .severe("Can't set execution permissions for executable " + executablePathName.toString() + ". If required, please ask an authorised user to make the file executable."); } } }
From source file:uk.ac.sanger.cgp.wwdocker.Config.java
public static void protectedFileCheck(String filename) throws AccessControlException, IOException { Path path = Paths.get(filename); Set<PosixFilePermission> permset = Files.getPosixFilePermissions(path); Iterator perm = permset.iterator(); while (perm.hasNext()) { PosixFilePermission p = (PosixFilePermission) perm.next(); if (p.equals(PosixFilePermission.GROUP_READ) || p.equals(PosixFilePermission.OTHERS_READ)) { throw new AccessControlException( "Your configuration file (which contains passwords) is readable to others:\n" + "\tFile: " + filename.toString() + "\tPerm: " + PosixFilePermissions.toString(permset)); }/*from ww w . j a va 2 s. c om*/ } }
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 w w w . ja v a2 s . com 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:net.spinetrak.rpitft.command.Command.java
private String init(final String script_) { final String VAR_TMP = "/var/tmp"; InputStream scriptIn = null;// ww w.j a v a 2s . co 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:com.streamsets.pipeline.stage.origin.spooldir.TestWholeFileSpoolDirSource.java
@Test public void testWholeFileRecordsForFile() throws Exception { Path sourcePath = Paths.get(testDir + "/source.txt"); Files.write(sourcePath, "Sample Text 1".getBytes()); Files.setAttribute(sourcePath, "posix:permissions", ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ)); SpoolDirSource source = createSource(); PushSourceRunner runner = new PushSourceRunner.Builder(SpoolDirDSource.class, source).addOutputLane("lane") .setOnRecordError(OnRecordError.TO_ERROR).build(); final List<Record> records = Collections.synchronizedList(new ArrayList<>(10)); AtomicInteger batchCount = new AtomicInteger(0); runner.runInit();// ww w. jav a2 s.co m try { runner.runProduce(new HashMap<>(), 10, output2 -> { synchronized (records) { records.addAll(output2.getRecords().get("lane")); } batchCount.incrementAndGet(); runner.setStop(); }); runner.waitOnProduce(); Assert.assertNotNull(records); Assert.assertEquals(1, records.size()); Record record = records.get(0); Assert.assertTrue(record.has(FileRefUtil.FILE_INFO_FIELD_PATH)); Assert.assertTrue(record.has(FileRefUtil.FILE_REF_FIELD_PATH)); Assert.assertEquals(Field.Type.FILE_REF, record.get(FileRefUtil.FILE_REF_FIELD_PATH).getType()); Assert.assertEquals(Field.Type.MAP, record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getType()); Map<String, Object> metadata = Files.readAttributes(sourcePath, "posix:*"); Assert.assertTrue(record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getValueAsMap().keySet() .containsAll(metadata.keySet())); //Check permissions Assert.assertTrue(record.has(FileRefUtil.FILE_INFO_FIELD_PATH + "/" + SpoolDirRunnable.PERMISSIONS)); Assert.assertEquals("rwxr-----", record .get(FileRefUtil.FILE_INFO_FIELD_PATH + "/" + SpoolDirRunnable.PERMISSIONS).getValueAsString()); Assert.assertEquals(Field.Type.FILE_REF, record.get(FileRefUtil.FILE_REF_FIELD_PATH).getType()); Assert.assertEquals(Field.Type.MAP, record.get(FileRefUtil.FILE_INFO_FIELD_PATH).getType()); } finally { runner.runDestroy(); } }
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//ww w. j a v a 2 s .co m */ 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: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 ww.ja v a 2s .c o 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:org.darkware.wpman.wpcli.WPCLI.java
/** * Update the local WP-CLI tool to the most recent version. *//*from ww w . j a v a 2 s .co m*/ public static void update() { try { WPManager.log.info("Downloading new version of WP-CLI."); CloseableHttpClient httpclient = HttpClients.createDefault(); URI pharURI = new URIBuilder().setScheme("http").setHost("raw.githubusercontent.com") .setPath("/wp-cli/builds/gh-pages/phar/wp-cli.phar").build(); WPManager.log.info("Downloading from: {}", pharURI); HttpGet downloadRequest = new HttpGet(pharURI); CloseableHttpResponse response = httpclient.execute(downloadRequest); WPManager.log.info("Download response: {}", response.getStatusLine()); WPManager.log.info("Download content type: {}", response.getFirstHeader("Content-Type").getValue()); FileChannel wpcliFile = FileChannel.open(WPCLI.toolPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); response.getEntity().writeTo(Channels.newOutputStream(wpcliFile)); wpcliFile.close(); Set<PosixFilePermission> wpcliPerms = new HashSet<>(); wpcliPerms.add(PosixFilePermission.OWNER_READ); wpcliPerms.add(PosixFilePermission.OWNER_WRITE); wpcliPerms.add(PosixFilePermission.OWNER_EXECUTE); wpcliPerms.add(PosixFilePermission.GROUP_READ); wpcliPerms.add(PosixFilePermission.GROUP_EXECUTE); Files.setPosixFilePermissions(WPCLI.toolPath, wpcliPerms); } catch (URISyntaxException e) { WPManager.log.error("Failure building URL for WPCLI download.", e); System.exit(1); } catch (IOException e) { WPManager.log.error("Error while downloading WPCLI client.", e); e.printStackTrace(); System.exit(1); } }