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:brooklyn.util.io.FileUtil.java

public static void setFilePermissionsTo700(File file) throws IOException {
    file.createNewFile();/*  w  ww .  j  av  a  2  s  .  c  o  m*/
    boolean setRead = file.setReadable(false, false) & file.setReadable(true, true);
    boolean setWrite = file.setWritable(false, false) & file.setWritable(true, true);
    boolean setExec = file.setExecutable(false, false) & file.setExecutable(true, true);

    if (setRead && setWrite && setExec) {
        if (LOG.isTraceEnabled())
            LOG.trace("Set permissions to 700 for file {}", file.getAbsolutePath());
    } else {
        if (loggedSetFilePermissionsWarning) {
            if (LOG.isTraceEnabled())
                LOG.trace(
                        "Failed to set permissions to 700 for file {}: setRead={}, setWrite={}, setExecutable={}",
                        new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
        } else {
            if (Os.isMicrosoftWindows()) {
                if (LOG.isDebugEnabled())
                    LOG.debug(
                            "Failed to set permissions to 700 for file {}; expected behaviour on Windows; setRead={}, setWrite={}, setExecutable={}; subsequent failures (on any file) will be logged at trace",
                            new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
            } else {
                LOG.warn(
                        "Failed to set permissions to 700 for file {}: setRead={}, setWrite={}, setExecutable={}; subsequent failures (on any file) will be logged at trace",
                        new Object[] { file.getAbsolutePath(), setRead, setWrite, setExec });
            }
            loggedSetFilePermissionsWarning = true;
        }
    }
}

From source file:org.spoutcraft.launcher.entrypoint.Mover.java

private static void execute(String[] args, boolean exe) throws Exception {
    File temp;/* ww w.j  a  va  2  s  .  co m*/
    if (exe) {
        temp = new File(Utils.getSettingsDirectory(), "temp.exe");
    } else {
        temp = new File(Utils.getSettingsDirectory(), "temp.jar");
    }
    File codeSource = new File(args[0]);
    codeSource.delete();
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(temp);
        fos = new FileOutputStream(codeSource);
        IOUtils.copy(fis, fos);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }

    codeSource.setExecutable(true, true);

    ProcessBuilder processBuilder = new ProcessBuilder();
    ArrayList<String> commands = new ArrayList<String>();
    if (!exe) {
        if (OperatingSystem.getOperatingSystem().equals(OperatingSystem.WINDOWS)) {
            commands.add("javaw");
        } else {
            commands.add("java");
        }
        commands.add("-Xmx256m");
        commands.add("-cp");
        commands.add(codeSource.getAbsolutePath());
        commands.add(SpoutcraftLauncher.class.getName());
    } else {
        commands.add(temp.getAbsolutePath());
        commands.add("-Launcher");
    }
    commands.addAll(Arrays.asList(args));
    processBuilder.command(commands);

    processBuilder.start();
}

From source file:com.microsoft.alm.plugin.idea.common.utils.IdeaHelper.java

/**
 * Check if a file is executable and if not sets it to be executable. When the plugin is unzipped,
 * the permissions of a file is not persisted so that is why a check is needed.
 *
 * @param executable executable file for application
 * @throws FileNotFoundException//from   w  ww .jav a 2s  .co m
 */
public static void setExecutablePermissions(final File executable) throws FileNotFoundException {
    if (!executable.exists()) {
        throw new FileNotFoundException(executable.getPath() + " not found while trying to set permissions.");
    }

    // set the executable to execute for all users
    if (!executable.canExecute()) {
        executable.setExecutable(true, false);
    }
}

From source file:com.asakusafw.shafu.core.util.IoUtils.java

private static void setFileMode(File file, int unixMode) {
    if (unixMode <= 0) {
        return;// w ww.j  a va2  s  .  c  o m
    }
    if ((unixMode & UNIX_EXEC_MASK) != 0) {
        boolean ownerOnly = (unixMode & UNIX_EXEC_MASK) == UNIX_EXEC_MASK_OWNER;
        file.setExecutable(true, ownerOnly);
    }
}

From source file:com.google.dart.tools.core.utilities.io.FileUtilities.java

/**
 * Attempt to make the given file executable.
 * /*from  ww w . ja va2  s .  c o m*/
 * @param file the file to be made executable
 * @return {@code true} if the file is executable
 */
public static boolean makeExecutable(File file) {
    // Try to make the file executable for all users.
    if (file.setExecutable(true, false)) {
        return true;
    }
    // If that fails, then try to make it executable for the current user.
    return file.setExecutable(true, true);
}

From source file:jnative.JNativeCodeLoader.java

/**
 * Locates the native library in the jar (loadble by the classloader really),
 * unpacks it in a temp location, and returns that file. If the native library
 * is not found by the classloader, returns null.
 *///from ww w . j  av a  2 s . c o  m
private static File unpackBinaries() {
    // locate the binaries inside the jar
    String fileName = System.mapLibraryName(LIBRARY_NAME);
    String directory = getDirectoryLocation();
    // use the current defining classloader to load the resource
    InputStream is = JNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
    if (is == null) {
        // specific to mac
        // on mac the filename can be either .dylib or .jnilib: try again with the
        // alternate name
        if (getOsName().contains("Mac")) {
            if (fileName.endsWith(".dylib")) {
                fileName = fileName.replace(".dylib", ".jnilib");
            } else if (fileName.endsWith(".jnilib")) {
                fileName = fileName.replace(".jnilib", ".dylib");
            }
            is = JNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
        }
        // the OS-specific library was not found: fall back on the library path
        if (is == null) {
            return null;
        }
    }

    // write the file
    byte[] buffer = new byte[8192];
    OutputStream os = null;
    try {
        // prepare the unpacked file location
        File unpackedFile = File.createTempFile("unpacked-", "-" + fileName);
        // ensure the file gets cleaned up
        unpackedFile.deleteOnExit();

        os = new FileOutputStream(unpackedFile);
        int read = 0;
        while ((read = is.read(buffer)) != -1) {
            os.write(buffer, 0, read);
        }

        // set the execution permission
        unpackedFile.setExecutable(true, false);
        LOG.debug("temporary unpacked path: " + unpackedFile);
        // return the file
        return unpackedFile;
    } catch (IOException e) {
        LOG.error("could not unpack the binaries", e);
        return null;
    } finally {
        try {
            is.close();
        } catch (IOException ignore) {
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
}

From source file:com.hadoop.compression.lzo.GPLNativeCodeLoader.java

/**
 * Locates the native library in the jar (loadble by the classloader really),
 * unpacks it in a temp location, and returns that file. If the native library
 * is not found by the classloader, returns null.
 *///from  ww  w.ja  v a 2s. c om
private static File unpackBinaries() {
    // locate the binaries inside the jar
    String fileName = System.mapLibraryName(LIBRARY_NAME);
    String directory = getDirectoryLocation();
    // use the current defining classloader to load the resource
    InputStream is = GPLNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
    if (is == null) {
        // specific to mac
        // on mac the filename can be either .dylib or .jnilib: try again with the
        // alternate name
        if (getOsName().contains("Mac")) {
            if (fileName.endsWith(".dylib")) {
                fileName = fileName.replace(".dylib", ".jnilib");
            } else if (fileName.endsWith(".jnilib")) {
                fileName = fileName.replace(".jnilib", ".dylib");
            }
            is = GPLNativeCodeLoader.class.getResourceAsStream(directory + "/" + fileName);
        }
        // the OS-specific library was not found: fall back on the library path
        if (is == null) {
            return null;
        }
    }

    // write the file
    byte[] buffer = new byte[8192];
    OutputStream os = null;
    try {
        // prepare the unpacked file location
        File unpackedFile = File.createTempFile("unpacked-", "-" + fileName);
        // ensure the file gets cleaned up
        unpackedFile.deleteOnExit();

        os = new FileOutputStream(unpackedFile);
        int read = 0;
        while ((read = is.read(buffer)) != -1) {
            os.write(buffer, 0, read);
        }

        // set the execution permission
        unpackedFile.setExecutable(true, false);
        LOG.debug("temporary unpacked path: " + unpackedFile);
        // return the file
        return unpackedFile;
    } catch (IOException e) {
        LOG.error("could not unpack the binaries", e);
        return null;
    } finally {
        try {
            is.close();
        } catch (IOException ignore) {
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignore) {
            }
        }
    }
}

From source file:com.meltmedia.cadmium.core.FileSystemManager.java

public static void copyAllContent(final String source, final String target, final boolean ignoreHidden)
        throws Exception {
    File sourceFile = new File(source);
    File targetFile = new File(target);
    if (!targetFile.exists()) {
        targetFile.mkdirs();/*  www  .  j  a v  a  2  s  .c o  m*/
    }
    if (sourceFile.exists() && sourceFile.canRead() && sourceFile.isDirectory() && targetFile.exists()
            && targetFile.canWrite() && targetFile.isDirectory()) {
        List<File> copyList = new ArrayList<File>();
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File file, String name) {
                return !ignoreHidden || !name.startsWith(".");
            }

        };
        File files[] = sourceFile.listFiles(filter);
        if (files.length > 0) {
            copyList.addAll(Arrays.asList(files));
        }
        for (int index = 0; index < copyList.size(); index++) {
            File aFile = copyList.get(index);
            String relativePath = aFile.getAbsoluteFile().getAbsolutePath()
                    .replaceFirst(sourceFile.getAbsoluteFile().getAbsolutePath(), "");
            if (aFile.isDirectory()) {
                File newDir = new File(target, relativePath);
                if (newDir.mkdir()) {
                    newDir.setExecutable(aFile.canExecute(), false);
                    newDir.setReadable(aFile.canRead(), false);
                    newDir.setWritable(aFile.canWrite(), false);
                    newDir.setLastModified(aFile.lastModified());
                    files = aFile.listFiles(filter);
                    if (files.length > 0) {
                        copyList.addAll(index + 1, Arrays.asList(files));
                    }
                } else {
                    log.warn("Failed to create new subdirectory \"{}\" in the target path \"{}\".",
                            relativePath, target);
                }
            } else {
                File newFile = new File(target, relativePath);
                if (newFile.createNewFile()) {
                    FileInputStream inStream = null;
                    FileOutputStream outStream = null;
                    try {
                        inStream = new FileInputStream(aFile);
                        outStream = new FileOutputStream(newFile);
                        streamCopy(inStream, outStream);
                    } finally {
                        if (inStream != null) {
                            try {
                                inStream.close();
                            } catch (Exception e) {
                            }
                        }
                        if (outStream != null) {
                            try {
                                outStream.flush();
                            } catch (Exception e) {
                            }
                            try {
                                outStream.close();
                            } catch (Exception e) {
                            }
                        }
                    }
                    newFile.setExecutable(aFile.canExecute(), false);
                    newFile.setReadable(aFile.canRead(), false);
                    newFile.setWritable(aFile.canWrite(), false);
                    newFile.setLastModified(aFile.lastModified());
                }
            }
        }
    }
}

From source file:org.everit.osgi.dev.maven.util.FileManager.java

private static void setPermissionsOnFileInNonPosixSystem(final File file,
        final Set<PosixFilePermission> perms) {

    if (perms.contains(PosixFilePermission.OWNER_EXECUTE)) {
        file.setExecutable(true, !perms.contains(PosixFilePermission.OTHERS_EXECUTE));
    }//from   w  ww .j  a  va  2 s.  c om

    if (perms.contains(PosixFilePermission.OWNER_READ)) {
        file.setReadable(true, !perms.contains(PosixFilePermission.OTHERS_READ));
    }

    if (perms.contains(PosixFilePermission.OWNER_WRITE)) {
        file.setWritable(true, !perms.contains(PosixFilePermission.OTHERS_WRITE));
    }

}

From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java

private static boolean makeExecutable(File file) {

    UpdateCore.logInfo("Setting execute bit for " + file.getAbsolutePath());

    // First try and set executable for all users.
    if (file.setExecutable(true, false)) {
        // success
        return true;
    }/*from   w  ww  . j  av a2 s.  co m*/

    // Then try only for the current user.
    return file.setExecutable(true, true);
}