/** * Equivalent to setReadable(readable, true). * @see #setReadable(boolean, boolean) * @since 1.6 */ public boolean setReadable(boolean readable) { return setReadable(readable, true); }
/** * create directory * * @param directoryPath */ public static void createDirectory(final String directoryPath) { File file = new File(directoryPath); if (!file.exists()) { file.setReadable(true, false); file.setWritable(true, true); file.mkdirs(); } }
private static void writeYarnProperties(Properties properties, File propertiesFile) { try (final OutputStream out = new FileOutputStream(propertiesFile)) { properties.store(out, "Generated YARN properties file"); } catch (IOException e) { throw new RuntimeException("Error writing the properties file", e); } propertiesFile.setReadable(true, false); // readable for all. }
@SuppressWarnings("ResultOfMethodCallIgnored") //intended, nothing useful can be done @SuppressLint("SetWorldReadable") //intended, default permission private static void setFilePermissions(File outputFile) { // Try change permission to rwxr-xr-x outputFile.setReadable(true, false); outputFile.setExecutable(true, false); outputFile.setWritable(true); }
@Override protected void plusRWX(File file) { file.setReadable(true, false); file.setWritable(true, false); file.setExecutable(true, false); } }
public static Path createSecuredTempDirectory(String prefix) throws IOException { Path tempDir = Files.createTempDirectory(prefix); tempDir.toFile().setExecutable(true, true); tempDir.toFile().setWritable(true, true); tempDir.toFile().setReadable(true, true); return tempDir; } }
/** * create file,full filename,signle empty file. * * @param fullFilename * @return boolean */ public static boolean createFile(final String fullFilename) { boolean result = false; File file = new File(fullFilename); createDirectory(file.getParent()); try { file.setReadable(true, false); file.setWritable(true, true); result = file.createNewFile(); } catch (Exception e) { throw new FileUtilException(e); } return result; }
private void writePem( String type, byte[] encodedContent, File path ) throws IOException { path.getParentFile().mkdirs(); try ( PemWriter writer = new PemWriter( new FileWriter( path ) ) ) { writer.writeObject( new PemObject( type, encodedContent ) ); writer.flush(); } path.setReadable( false, false ); path.setWritable( false, false ); path.setReadable( true ); path.setWritable( true ); } }
private void shareHeapDump(BlockInfoEx blockInfo) { File heapDumpFile = blockInfo.logFile; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { heapDumpFile.setReadable(true, false); } Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("application/octet-stream"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(heapDumpFile)); startActivity(Intent.createChooser(intent, getString(R.string.block_canary_share_with))); }
@Override public void run() { //noinspection ResultOfMethodCallIgnored heapDumpFile.setReadable(true, false); final Uri heapDumpUri = getUriForFile(getBaseContext(), "com.squareup.leakcanary.fileprovider." + getApplication().getPackageName(), heapDumpFile); runOnUiThread(new Runnable() { @Override public void run() { startShareIntentChooser(heapDumpUri); } }); } });
@BeforeClass public static void setupContent() throws FileNotFoundException { contentFolder.mkdir(); contentFolder.setReadable(true, false); contentFolder.setWritable(true, false); contentFolder.setExecutable(true, false); File indexFile = new File(contentFolder, "index.html"); indexFile.setReadable(true, false); indexFile.setWritable(true, false); indexFile.setExecutable(true, false); @Cleanup PrintStream printStream = new PrintStream(new FileOutputStream(indexFile)); printStream.println("<html><body>This worked</body></html>"); }
@Before public void prepDestDirectory() throws IOException { final File targetDir = new File("target/move-target"); if (!targetDir.exists()) { Files.createDirectories(targetDir.toPath()); return; } targetDir.setReadable(true); for (final File file : targetDir.listFiles()) { Files.delete(file.toPath()); } }
@SuppressWarnings({"Duplicates", "ResultOfMethodCallIgnored"}) @BeforeClass public static void setupContent() throws FileNotFoundException { contentFolder.mkdir(); contentFolder.setReadable(true, false); contentFolder.setWritable(true, false); contentFolder.setExecutable(true, false); File indexFile = new File(contentFolder, "index.html"); indexFile.setReadable(true, false); indexFile.setWritable(true, false); indexFile.setExecutable(true, false); @Cleanup PrintStream printStream = new PrintStream(new FileOutputStream(indexFile)); printStream.println("<html><body>This worked</body></html>"); }
public void testAReadableFile() { // Not all OSes will allow setting readability so have to be forgiving here. file.setReadable(true); assertMatches("matches readable file", FileMatchers.aReadableFile(), file); if (file.setReadable(false)) { assertDoesNotMatch("doesn't match unreadable file", FileMatchers.aReadableFile(), file); } }
@Test public void testUnreadableFileInput() throws Exception { //skip this test on Windows, coverage on Linux assumeTrue(!org.apache.zookeeper.Shell.WINDOWS); File file = File.createTempFile("test", ".junit", testData); file.setReadable(false, false); file.deleteOnExit(); String absolutePath = file.getAbsolutePath(); String error = ZKUtil.validateFileInput(absolutePath); assertNotNull(error); String expectedMessage = "Read permission is denied on the file '" + absolutePath + "'"; assertEquals(expectedMessage, error); }
@Test void lockFileAndFailToDeleteDirectory() { File nonDeletableDirectory = directory.directory( "c" ); CONTEXT.setValue( LOCKED_TEST_FILE_KEY, nonDeletableDirectory ); assertTrue( nonDeletableDirectory.setReadable( false, false ) ); }
@Test public void shouldLogIfConfigFileCouldNotBeRead() throws IOException { Log log = mock( Log.class ); File confFile = testDirectory.file( "test.conf" ); assertTrue( confFile.createNewFile() ); assumeTrue( confFile.setReadable( false ) ); Config config = Config.fromFile( confFile ).withNoThrowOnFileLoadFailure().build(); config.setLogger( log ); verify( log ).error( "Unable to load config file [%s]: %s", confFile, confFile + " (Permission denied)" ); }
@Test( expected = ConfigLoadIOException.class ) public void mustThrowIfConfigFileCoutNotBeRead() throws IOException { File confFile = testDirectory.file( "test.conf" ); assertTrue( confFile.createNewFile() ); assumeTrue( confFile.setReadable( false ) ); Config.fromFile( confFile ).build(); }
@Test public void ignoreUnreadableFile() throws Exception { File file = createZipFile(); file.deleteOnExit(); file.setReadable(false); assumeFalse("File cannot be marked as unreadable, skipping test.", file.canRead()); FilesystemCodeBaseLocator locator = buildLocator(file); assertHasNoCodeBase(ClassFactory.createFilesystemCodeBase(locator)); }
@Test @EnabledOnOs( OS.LINUX ) void exceptionOnDirectoryDeletionIncludeTestDisplayName() throws IOException { CONTEXT.clear(); FailedTestExecutionListener failedTestListener = new FailedTestExecutionListener(); execute( "lockFileAndFailToDeleteDirectory", failedTestListener ); File lockedFile = CONTEXT.getValue( LOCKED_TEST_FILE_KEY ); assertNotNull( lockedFile ); assertTrue( lockedFile.setReadable( true, true ) ); FileUtils.deleteRecursively( lockedFile ); failedTestListener.assertTestObserver(); }