Example usage for java.io File setExecutable

List of usage examples for java.io File setExecutable

Introduction

In this page you can find the example usage for java.io File setExecutable.

Prototype

public boolean setExecutable(boolean executable, boolean ownerOnly) 

Source Link

Document

Sets the owner's or everybody's execute permission for this abstract pathname.

Usage

From source file:org.robovm.gradle.tasks.AbstractRoboVMTask.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;/*from  www  .  j  a  v  a2 s . c  o m*/
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            f.setLastModified(entry.getLastModifiedDate().getTime());
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.android.tradefed.util.FileUtil.java

/**
 * Performs a best effort attempt to make given file group executable,
 * readable, and writable./*ww  w .  j  a  v  a2 s  . com*/
 * <p/ >
 * If 'chmod' system command is not supported by underlying OS, will attempt
 * to set permissions for all users.
 *
 * @param file
 *            the {@link File} to make owner and group writable
 * @return <code>true</code> if permissions were set successfully,
 *         <code>false</code> otherwise
 */
public static boolean chmodGroupRWX(File file) {
    if (chmod(file, "ug+rwx")) {
        return true;
    } else {
        Log.d(LOG_TAG,
                String.format("Failed chmod; attempting to set %s globally RWX", file.getAbsolutePath()));
        return file.setExecutable(true, false /* false == executable for all */)
                && file.setWritable(true, false /* false == writable for all */)
                && file.setReadable(true, false /* false == readable for all */);
    }
}

From source file:com.ghgande.j2mod.modbus.utils.TestUtils.java

/**
 * This method will extract the appropriate Modbus master tool into the
 * temp folder so that it can be used later
 *
 * @return The temporary location of the Modbus master tool.
 * @throws Exception/*from w  ww  .  jav a2s.  com*/
 */
public static File loadModPollTool() throws Exception {

    // Load the resource from the library

    String osName = System.getProperty("os.name");

    // Work out the correct name

    String exeName;
    if (osName.matches("(?is)windows.*")) {
        osName = "win32";
        exeName = "modpoll.exe";
    } else {
        osName = "linux";
        exeName = "modpoll";
    }

    // Copy the native modpoll library to a temporary directory in the build workspace to facilitate
    // execution on some platforms.
    File tmpDir = Files.createTempDirectory(Paths.get("."), "modpoll-").toFile();
    tmpDir.deleteOnExit();

    File nativeFile = new File(tmpDir, exeName);

    // Copy the library to the temporary folder

    InputStream in = null;
    String resourceName = String.format("/com/ghgande/j2mod/modbus/native/%s/%s", osName, exeName);

    try {
        in = SerialPort.class.getResourceAsStream(resourceName);
        if (in == null) {
            throw new Exception(String.format("Cannot find resource [%s]", resourceName));
        }
        pipeInputToOutputStream(in, nativeFile, false);
        nativeFile.deleteOnExit();

        // Set the correct privileges

        if (!nativeFile.setWritable(true, true)) {
            logger.warn("Cannot set modpoll native library to be writable");
        }
        if (!nativeFile.setReadable(true, false)) {
            logger.warn("Cannot set modpoll native library to be readable");
        }
        if (!nativeFile.setExecutable(true, false)) {
            logger.warn("Cannot set modpoll native library to be executable");
        }
    } catch (Exception e) {
        throw new Exception(
                String.format("Cannot locate modpoll native library [%s] - %s", exeName, e.getMessage()));
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error("Cannot close stream - {}", e.getMessage());
            }
        }
    }

    return nativeFile;
}

From source file:gov.jgi.meta.MetaUtils.java

/**
 * given a map of sequences indexed by sequence id, write out a fasta file to local file system.
 * files are created with rwx bits set to 777.
 *
 * @param seqList is the list of sequences to create the database with
 * @param tmpFileName the file name/path to create
 * @return the full path of the location of the database
 * @throws IOException if error occures in file creation
 *//*from  www.ja v a 2s .  c  o  m*/
public static String sequenceToLocalFile(Map<String, String> seqList, String tmpFileName) throws IOException {
    BufferedWriter out;
    File seqFile = null;

    /*
     * open temp file
     */
    seqFile = new File(tmpFileName);
    out = new BufferedWriter(new FileWriter(seqFile.getPath()));

    /*
     * write out the sequences to file
     */
    for (String key : seqList.keySet()) {
        assert (seqList.get(key) != null);
        out.write(">" + key + "\n");
        out.write(seqList.get(key) + "\n");
    }

    /*
     * close temp file
     */
    out.close();

    if (!(seqFile.setExecutable(true, false) && seqFile.setReadable(true, false)
            && seqFile.setWritable(true, false))) {
        throw new IOException("unable to set RWX bits to 777");
    }

    return (seqFile.getPath());
}

From source file:com.linkedin.gradle.python.util.entrypoint.EntryPointWriter.java

public void writeEntryPoint(File location, Map<String, String> properties)
        throws IOException, ClassNotFoundException {
    if (location.exists()) {
        location.delete();//from   w w w .  j a v a2 s .  co  m
    }

    location.createNewFile();

    if (isCliTool) {
        location.setExecutable(true, false);
        location.setReadable(true, false);
    } else {
        location.setExecutable(true);
    }

    SimpleTemplateEngine simpleTemplateEngine = new SimpleTemplateEngine();
    String rendered = simpleTemplateEngine.createTemplate(template).make(properties).toString();
    FileUtils.write(location, rendered);
}

From source file:io.sloeber.core.managers.InternalPackageManager.java

private static void chmod(File file, int mode) throws IOException, InterruptedException {
    String octal = Integer.toOctalString(mode);
    if (Platform.getOS().equals(Platform.OS_WIN32)) {
        boolean ownerExecute = (((mode / (8 * 8)) & 1) == 1);
        boolean ownerRead = (((mode / (8 * 8)) & 4) == 4);
        boolean ownerWrite = (((mode / (8 * 8)) & 2) == 2);
        boolean everyoneExecute = (((mode / 8) & 1) == 1);
        boolean everyoneRead = (((mode / 8) & 4) == 4);
        boolean everyoneWrite = (((mode / 8) & 2) == 2);
        file.setWritable(true, false);//from  w  ww .ja v a2 s. c  om
        file.setExecutable(ownerExecute, !everyoneExecute);
        file.setReadable(ownerRead, !everyoneRead);
        file.setWritable(ownerWrite, !everyoneWrite);
    } else {
        Process process = Runtime.getRuntime().exec(new String[] { "chmod", octal, file.getAbsolutePath() }, //$NON-NLS-1$
                null, null);
        process.waitFor();
    }
}

From source file:com.papteco.client.netty.SelProjectClientHandler.java

private void prepareLocalFolders(ProjectBean project) {
    File projectFolder = new File(EnvConstant.LCL_STORING_PATH, project.getProjectCde());
    if (!projectFolder.exists()) {
        projectFolder.mkdirs();/* w w  w  .j  a  va2 s  .  com*/
        projectFolder.setExecutable(true, false);
        projectFolder.setReadable(true, false);
        projectFolder.setWritable(true, false);
        // logger.info("Folder \"" + projectFolder.getName()
        // + "\" created!");
    } else {
        // logger.info("Folder \"" + projectFolder.getName()
        // + "\" existing already!");
    }

    for (FolderBean folder : project.getFolderTree()) {
        File sf = new File(projectFolder.getPath(), folder.getFolderName());
        if (!sf.exists()) {
            sf.mkdirs();
            sf.setExecutable(true, false);
            sf.setReadable(true, false);
            sf.setWritable(true, false);
            // logger.info("(execable, readable, writeable) - ("
            // + sf.canExecute() + ", " + sf.canRead() + ", "
            // + sf.canWrite() + ") - " + projectFolder.getPath()
            // + "/" + folder.getFolderName());
        } else {
            // logger.info("(execable, readable, writeable) - ("
            // + sf.canExecute() + ", " + sf.canRead() + ", "
            // + sf.canWrite() + ") - " + projectFolder.getPath()
            // + "/" + folder.getFolderName() + " [existing already]");
        }
    }
    logger.info("Folders creation finish.");
}

From source file:org.robovm.eclipse.RoboVMPlugin.java

private static void extractTarGz(File archive, File destDir) throws IOException {
    TarArchiveInputStream in = null;//from   w  w w . j  ava  2 s. c  om
    try {
        in = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(archive)));
        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            File f = new File(destDir, entry.getName());
            if (entry.isDirectory()) {
                f.mkdirs();
            } else {
                f.getParentFile().mkdirs();
                OutputStream out = null;
                try {
                    out = new FileOutputStream(f);
                    IOUtils.copy(in, out);
                } finally {
                    IOUtils.closeQuietly(out);
                }
            }
            if (entry instanceof TarArchiveEntry) {
                int mode = ((TarArchiveEntry) entry).getMode();
                if ((mode & 00100) > 0) {
                    // Preserve execute permissions
                    f.setExecutable(true, (mode & 00001) == 0);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.rhq.plugins.filetemplate.ProcessingRecipeContext.java

private void ensureExecutable(File scriptFile) {
    boolean success = scriptFile.setExecutable(true, true);
    if (!success) {
        String msg = "Cannot ensure that script [" + scriptFile + "] is executable";
        audit("ensureExecutable", BundleResourceDeploymentHistory.Status.FAILURE, msg);
        throw new RuntimeException(msg);
    }//w  w  w  . ja va 2s .c o m
    return;
}

From source file:org.apache.hadoop.io.crypto.tool.BeeCli.java

private void credentailsFileWriter(Credentials credentials, String path) throws Exception {
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(path));
    credentials.writeTokenStorageToStream(dos);
    File f = new File(path);
    f.setExecutable(false, false);
    f.setReadable(true, true);/*from   w  ww . j  av  a2 s  .co  m*/
    f.setWritable(true, true);
}