List of usage examples for java.io File setExecutable
public boolean setExecutable(boolean executable)
From source file:testjar.GenerateTaskChildProcess.java
/** * It uses for creating the child processes for a task. * @param conf configuration for a job.//from w w w.jav a 2 s .co m * @param jobName the name of the mapper job. * @throws IOException if an I/O error occurs. */ private static void createChildProcess(JobConf conf, String jobName) throws IOException { FileSystem fs = FileSystem.getLocal(conf); File TMP_ROOT_DIR = new File("/tmp"); String TEST_ROOT_DIR = TMP_ROOT_DIR.getAbsolutePath() + Path.SEPARATOR + "ChildProc_" + jobName; Path scriptDir = new Path(TEST_ROOT_DIR); int numOfChildProcesses = 2; if (fs.exists(scriptDir)) { fs.delete(scriptDir, true); } fs.mkdirs(scriptDir); fs.setPermission(scriptDir, new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL)); String scriptDirName = scriptDir.toUri().getPath(); Random rm = new Random(); String scriptName = "ShellScript_" + jobName + "_" + rm.nextInt() + ".sh"; Path scriptPath = new Path(scriptDirName, scriptName); String shellScript = scriptPath.toString(); String script = null; if (jobName.equals("AppendStr")) { script = "#!/bin/sh\n" + "umask 000\n" + "StrVal=\"Hadoop is framework for data intensive " + "distributed applications.\"\n" + "StrVal=\"${StrVal}Hadoop enables applications to work " + "with thousands of nodes.\"\n" + "echo $StrVal\n" + "if [ \"X$1\" != \"X0\" ]\nthen\n" + " sh " + shellScript + " $(($1-1))\n" + "else\n" + " while(true)\n" + " do\n" + " StrVal=\"$StrVal Hadoop \"\n" + " done\n" + "fi"; } else if (jobName.equals("DispStr")) { script = "#!/bin/sh\n" + "umask 000\n" + "msg=Welcome\n" + "echo $msg\n" + " if [ \"X$1\" != \"X0\" ]\nthen\n" + " sh " + shellScript + " $(($1-1))\n" + "else\n" + " while(true)\n" + " do\n" + " sleep 2 \n" + " done\n" + "fi"; } else { script = "#!/bin/sh\n" + "umask 000\n" + "msg=Welcome\n" + "echo $msg\n" + " if [ \"X$1\" != \"X0\" ]\nthen\n" + " sh " + shellScript + " $(($1-1))\n" + "else\n" + " for count in {1..1000}\n" + " do\n" + " echo \"$msg_$count\" \n" + " done\n" + "fi"; } DataOutputStream file = fs.create(scriptPath); file.writeBytes(script); file.close(); File scriptFile = new File(scriptDirName, scriptName); scriptFile.setExecutable(true); LOG.info("script absolute path:" + scriptFile.getAbsolutePath()); String[] cmd = new String[] { scriptFile.getAbsolutePath(), String.valueOf(numOfChildProcesses) }; ShellCommandExecutor shellExec = new ShellCommandExecutor(cmd); shellExec.execute(); }
From source file:org.geopublishing.geopublisher.GpUtil.java
/** * this method checks for permission on java.io.tmpdir and changes it to a * given path if files in current tmpDir may not be executed. (Possibly due * to noexec mountoption on /tmp)/* w w w.j a v a2 s. com*/ */ public static void checkAndResetTmpDir(String path) { if (SystemUtils.IS_OS_UNIX) { String tmpDir = System.getProperty("java.io.tmpdir"); try { File file = File.createTempFile("geopublisher", "tmpDirTest", new File(tmpDir)); file.setExecutable(true); if (!file.canExecute()) { System.setProperty("java.io.tmpdir", path); LOGGER.debug("tmpDir has no execute rights, changing to " + path); } FileUtils.deleteQuietly(file); } catch (IOException e) { LOGGER.log(Level.ERROR, e); } } }
From source file:org.metaeffekt.dcc.commons.DccUtils.java
public static void prepareFoldersForWriting(File... folders) { for (File file : folders) { if (!file.exists()) { file.mkdirs();//from www .java 2 s. c o m file.setWritable(true); file.setExecutable(true); } else if (!file.isDirectory()) { throw new IllegalStateException(String.format( "Cannot create directory [%s] because a file with the same name already exists!", file)); } else if (!file.canWrite()) { file.setWritable(true); if (!file.canWrite()) { throw new IllegalStateException(String.format("[%s] is not writable!", file)); } } } }
From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java
public static String createTempFileFromResource(final String script, final String extension, final boolean executable) throws IOException { final InputStream scriptInputStream = UnixUtils.class.getResourceAsStream("/" + script); // create a temporary file final File scriptTempFile = File.createTempFile("unix-utils-", extension); scriptTempFile.deleteOnExit();/*from w w w. j av a 2s . co m*/ try (FileOutputStream out = new FileOutputStream(scriptTempFile)) { IOUtils.copy(scriptInputStream, out); } scriptTempFile.setExecutable(executable); final String command = scriptTempFile.getAbsolutePath(); return command; }
From source file:org.benetech.secureapp.generator.Fdroid.java
static public void copyApkToFDroid(HttpSession session, File apkCreated) throws Exception { try {/*from www . j av a 2s . c o m*/ long startTime = System.currentTimeMillis(); File baseDir = setupTempFDroidRepo(session); File repoDir = new File(baseDir, FDROID_REPO_DIR); File destination = new File(repoDir, apkCreated.getName()); SagLogger.logInfo(session, "Copy to FDroid Repo: " + destination.getAbsolutePath()); FileUtils.copyFile(apkCreated, destination); destination.setExecutable(true); destination.setWritable(true); String fDroidCommand = getFDroidCommand("--version"); SecureAppGeneratorApplication.executeCommand(session, fDroidCommand, baseDir); fDroidCommand = getFDroidCommand("update --create-metadata -v"); SecureAppGeneratorApplication.executeCommand(session, fDroidCommand, baseDir); fDroidCommand = getFDroidCommand("server update -v"); SecureAppGeneratorApplication.executeCommand(session, fDroidCommand, baseDir); long endTime = System.currentTimeMillis(); String timeToBuild = SagLogger.getElapsedTime(startTime, endTime); SagLogger.logInfo(session, "Fdroid Build took" + timeToBuild); } finally { // try { // TODO: add this back once tested on server. // if(baseDir != null) // FileUtils.deleteDirectory(baseDir); } // catch (IOException e) { // Logger.logException(e); } } }
From source file:com.stratio.connector.cassandra.CCMHandler.java
/** * Start a test Cassandra cluster to execute the unit tests. The method creates a * temporal file with the contents of {@code /com/stratio/meta/test/test.sh} and proceeds * with its execution.//from w ww . j a v a 2 s. c o m */ public static void startCCM() { BufferedReader in = null; try { File tempFile = File.createTempFile("stratio-start-ccm", ".sh"); InputStream resourceStream = CCMHandler.class .getResourceAsStream("/com/stratio/connector/cassandra/test.sh"); FileUtils.copyInputStreamToFile(resourceStream, tempFile); tempFile.setExecutable(true); Process p = Runtime.getRuntime().exec(tempFile.getAbsolutePath()); in = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName("UTF-8"))); String line; while ((line = in.readLine()) != null) { LOG.debug(line); } FileUtils.forceDeleteOnExit(tempFile); } catch (IOException e) { LOG.error("Error starting CCM", e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { LOG.error("IO exception closing ccm output.", e); } } } }
From source file:se.dibbler.backend.generics.DibblerImageUtil.java
private static void setFileAccess(File imgOutFile, FileAccess access) { switch (access) { case READONLY: imgOutFile.setReadable(true);/* ww w.j a va2 s. c o m*/ imgOutFile.setWritable(false); imgOutFile.setExecutable(false); break; case EXECUTEONLY: imgOutFile.setReadable(false); imgOutFile.setWritable(false); imgOutFile.setExecutable(true); break; case WRITEONLY: imgOutFile.setReadable(false); imgOutFile.setWritable(true); imgOutFile.setExecutable(false); break; case READ_AND_EXECUTE: imgOutFile.setReadable(true); imgOutFile.setWritable(false); imgOutFile.setExecutable(true); break; case WRITE_AND_EXECUTE: imgOutFile.setReadable(false); imgOutFile.setWritable(true); imgOutFile.setExecutable(true); break; case READ_AND_WRITE: imgOutFile.setReadable(true); imgOutFile.setWritable(true); imgOutFile.setExecutable(false); break; default: LOG.error("[ Wrong file access type or not implemented in method ]"); } }
From source file:eu.scape_project.tool.toolwrapper.core.utils.Utils.java
/** * Method useful to copy a file from one place to another * /*w w w. j av a 2 s . c o m*/ * @param source * file path being copied * @param destination * file path where to the file is being copied * @return true if the file was successfully copied, false otherwise * */ public static boolean copyFile(File source, File destination, boolean setExecutable) { boolean success = true; if (source.exists()) { try { FileUtils.copyFile(source, destination); if (!destination.setExecutable(setExecutable)) { log.error("Error while setting execution permissions on \"" + destination + "\"..."); } } catch (IOException e) { log.error("Error while copying file \"" + source + "\" to \"" + destination + "\"..."); success = false; } } else { log.error("File \"" + source + "\" does not exists..."); success = false; } return success; }
From source file:org.silverpeas.security.web.CipherKeyResourceTest.java
@BeforeClass public static void createSecurityDirectoryAndSetupJCEProviders() throws IOException { String securityPath = FileRepositoryManager.getSecurityDirPath(); File securityDir = new File(securityPath); if (!securityDir.exists()) { FileUtils.forceMkdir(securityDir); }/*w ww . j a v a 2 s .co m*/ securityDir.setWritable(true); securityDir.setExecutable(true); securityDir.setReadable(true); if (System.getProperty("os.name").toLowerCase().contains("windows")) { Runtime.getRuntime().exec("attrib +H " + securityPath); } Security.addProvider(new BouncyCastleProvider()); }
From source file:org.cloudifysource.utilitydomain.context.blockstorage.VolumeUtils.java
private static boolean verifyDevicePartitioning(final String device, final long timeoutInMillis) throws LocalStorageOperationException, TimeoutException { boolean deviceFormatted = false; try {/*from w w w .ja va 2 s . c o m*/ File tempDeviceListScript = File.createTempFile("getPartitionStatus", ".sh"); FileUtils.writeStringToFile(tempDeviceListScript, "sudo fdisk -l | grep " + device); tempDeviceListScript.setExecutable(true); tempDeviceListScript.deleteOnExit(); String fdiskOutput = executeSilentCommandLineReturnOutput(tempDeviceListScript.getAbsolutePath(), timeoutInMillis); if (fdiskOutput.contains(device + ":") && !fdiskOutput.contains(device + " doesn't contain a valid partition table")) { deviceFormatted = true; } } catch (Exception e) { throw new LocalStorageOperationException("Failed verifying paritioning of device: " + device, e); } return deviceFormatted; }