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:com.facebook.buck.util.unarchive.Untar.java

/** Sets the modification time and the execution bit on a file */
private void setAttributes(ProjectFilesystem filesystem, Path path, TarArchiveEntry entry) throws IOException {
    Path filePath = filesystem.getRootPath().resolve(path);
    File file = filePath.toFile();
    file.setLastModified(entry.getModTime().getTime());
    Set<PosixFilePermission> posixPermissions = MorePosixFilePermissions.fromMode(entry.getMode());
    if (posixPermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
        // setting posix file permissions on a symlink does not work, so use File API instead
        if (entry.isSymbolicLink()) {
            file.setExecutable(true, true);
        } else {//from w w w . jav  a 2  s .c om
            MostFiles.makeExecutable(filePath);
        }
    }
}

From source file:org.eclipse.acute.OmnisharpStreamConnectionProvider.java

/**
 *
 * @return path to server, unzipping it if necessary. Can be null is fragment is missing.
 *///from w ww  .j av  a  2 s .  c om
private @Nullable File getServer() throws IOException {
    File serverPath = new File(AcutePlugin.getDefault().getStateLocation().toFile(), "omnisharp-roslyn"); //$NON-NLS-1$
    if (!serverPath.exists()) {
        serverPath.mkdirs();
        try (InputStream stream = FileLocator.openStream(AcutePlugin.getDefault().getBundle(),
                new Path("omnisharp-roslyn.tar"), true); //$NON-NLS-1$
                TarArchiveInputStream tarStream = new TarArchiveInputStream(stream);) {
            TarArchiveEntry entry = null;
            while ((entry = tarStream.getNextTarEntry()) != null) {
                if (!entry.isDirectory()) {
                    File targetFile = new File(serverPath, entry.getName());
                    targetFile.getParentFile().mkdirs();
                    InputStream in = new BoundedInputStream(tarStream, entry.getSize()); // mustn't be closed
                    try (FileOutputStream out = new FileOutputStream(targetFile);) {
                        IOUtils.copy(in, out);
                        if (!Platform.OS_WIN32.equals(Platform.getOS())) {
                            int xDigit = entry.getMode() % 10;
                            targetFile.setExecutable(xDigit > 0, (xDigit & 1) == 1);
                            int wDigit = (entry.getMode() / 10) % 10;
                            targetFile.setWritable(wDigit > 0, (wDigit & 1) == 1);
                            int rDigit = (entry.getMode() / 100) % 10;
                            targetFile.setReadable(rDigit > 0, (rDigit & 1) == 1);
                        }
                    }
                }
            }
        }
    }
    return serverPath;
}

From source file:com.anthemengineering.mojo.infer.InferMojo.java

/**
 * Extracts a given infer.tar.xz file to the given directory.
 *
 * @param tarXzToExtract the file to extract
 * @param inferDownloadDir the directory to extract the file to
 */// ww w .j  av a  2  s  . c om
private static void extract(File tarXzToExtract, File inferDownloadDir) throws IOException {

    FileInputStream fin = null;
    BufferedInputStream in = null;
    XZCompressorInputStream xzIn = null;
    TarArchiveInputStream tarIn = null;

    try {
        fin = new FileInputStream(tarXzToExtract);
        in = new BufferedInputStream(fin);
        xzIn = new XZCompressorInputStream(in);
        tarIn = new TarArchiveInputStream(xzIn);

        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final File fileToWrite = new File(inferDownloadDir, entry.getName());

            if (entry.isDirectory()) {
                FileUtils.forceMkdir(fileToWrite);
            } else {
                BufferedOutputStream out = null;
                try {
                    out = new BufferedOutputStream(new FileOutputStream(fileToWrite));
                    final byte[] buffer = new byte[4096];
                    int n = 0;
                    while (-1 != (n = tarIn.read(buffer))) {
                        out.write(buffer, 0, n);
                    }
                } finally {
                    if (out != null) {
                        out.close();
                    }
                }
            }

            // assign file permissions
            final int mode = entry.getMode();

            fileToWrite.setReadable((mode & 0004) != 0, false);
            fileToWrite.setReadable((mode & 0400) != 0, true);
            fileToWrite.setWritable((mode & 0002) != 0, false);
            fileToWrite.setWritable((mode & 0200) != 0, true);
            fileToWrite.setExecutable((mode & 0001) != 0, false);
            fileToWrite.setExecutable((mode & 0100) != 0, true);
        }
    } finally {
        if (tarIn != null) {
            tarIn.close();
        }
    }
}

From source file:com.vmware.photon.controller.deployer.deployengine.ScriptRunnerTest.java

private void createScript(File scriptFile, String content) throws IOException {
    scriptFile.createNewFile();//from   w w w.jav a2s. co m
    scriptFile.setExecutable(true, true);

    FileOutputStream outputStream = new FileOutputStream(scriptFile);
    outputStream.write(content.getBytes());
    outputStream.flush();
    outputStream.close();
}

From source file:org.openbase.bco.ontology.lib.testing.Measurement.java

private void putIntoExcelFile(final String sheetName, final List<Long> simpleQuMeasuredValues,
        final List<Long> complexQuMeasuredValues, int daysCurCount) {
    // https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
    XSSFWorkbook workbook;//from   w ww .j ava 2  s  . c  o m
    XSSFSheet sheet;
    Row row;

    try {
        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        workbook = new XSSFWorkbook(excelFile);
        sheet = workbook.getSheet(sheetName);
    } catch (IOException ex) {
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet(sheetName);
        row = sheet.createRow(daysCurCount);

        row.createCell(0).setCellValue("Days");
        row.createCell(1).setCellValue("Triple");
        row.createCell(2).setCellValue("Mean of simple trigger");
        row.createCell(3).setCellValue("Mean of complex trigger");
    }

    row = sheet.createRow(daysCurCount + 1);

    System.out.println("simple: " + simpleQuMeasuredValues);
    System.out.println("simple count: " + simpleQuMeasuredValues.size());
    System.out.println("complex: " + complexQuMeasuredValues);
    System.out.println("complex count: " + complexQuMeasuredValues.size());

    long sumSimple = 0L;
    long sumComplex = 0L;

    for (final long valueSimple : simpleQuMeasuredValues) {
        sumSimple += valueSimple;
    }
    for (final long valueComplex : complexQuMeasuredValues) {
        sumComplex += valueComplex;
    }

    // number of days
    final Cell cellDay = row.createCell(0);
    cellDay.setCellValue(daysCurCount + 1);

    // number of triple
    final Cell cellTriple = row.createCell(1);
    cellTriple.setCellValue(numberOfTriple);

    // mean of simple trigger time
    final Cell cellMeanSimple = row.createCell(2);
    cellMeanSimple.setCellValue(sumSimple / simpleQuMeasuredValues.size());

    // mean of complex trigger time
    final Cell cellMeanComplex = row.createCell(3);
    cellMeanComplex.setCellValue(sumComplex / complexQuMeasuredValues.size());

    try {
        final File file = new File(FILE_NAME);
        file.setReadable(true, false);
        file.setExecutable(true, false);
        file.setWritable(true, false);
        System.out.println("Save data row ...");
        final FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        workbook.close();
    } catch (IOException ex) {
        ExceptionPrinter.printHistory(ex, LOGGER);
    }
}

From source file:org.openbase.bco.ontology.lib.testing.Measurement.java

private void saveMemoryTestValues(final String sheetName, final List<Long> simpleQuMeasuredValues,
        final List<Long> complexQuMeasuredValues, final DataVolume dataVolume) {
    XSSFWorkbook workbook;/*w  ww  .  jav a 2 s .  c om*/
    XSSFSheet sheet;
    Row rowSimple;
    Row rowComplex;

    try {
        FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
        workbook = new XSSFWorkbook(excelFile);
        sheet = workbook.getSheet(sheetName);
        rowSimple = sheet.getRow(1);
        rowComplex = sheet.getRow(2);
    } catch (IOException ex) {
        workbook = new XSSFWorkbook();
        sheet = workbook.createSheet(sheetName);
        final Row row = sheet.createRow(0);
        rowSimple = sheet.createRow(1);
        rowComplex = sheet.createRow(2);

        row.createCell(1).setCellValue("ConfigData only");
        row.createCell(2).setCellValue("ConfigData and dayData");
        row.createCell(3).setCellValue("ConfigData and 4x dayData");
    }

    long sumSimple = 0L;
    long sumComplex = 0L;

    for (final long valueSimple : simpleQuMeasuredValues) {
        sumSimple += valueSimple;
    }
    for (final long valueComplex : complexQuMeasuredValues) {
        sumComplex += valueComplex;
    }

    int column = 0;

    switch (dataVolume) {
    case CONFIG:
        column = 1;
        break;
    case CONFIG_DAY:
        column = 2;
        break;
    case CONFIG_WEEK:
        column = 3;
        break;
    default:
        break;
    }

    System.out.println("Save date in column: " + column);

    // mean of simple trigger time
    final Cell cellMeanSimple = rowSimple.createCell(column);
    cellMeanSimple.setCellValue(sumSimple / simpleQuMeasuredValues.size());

    // mean of complex trigger time
    final Cell cellMeanComplex = rowComplex.createCell(column);
    cellMeanComplex.setCellValue(sumComplex / complexQuMeasuredValues.size());

    try {
        final File file = new File(FILE_NAME);
        file.setReadable(true, false);
        file.setExecutable(true, false);
        file.setWritable(true, false);
        System.out.println("Save data row ...");
        final FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
        workbook.close();
    } catch (IOException ex) {
        ExceptionPrinter.printHistory(ex, LOGGER);
    }
}

From source file:com.chaschev.install.InstallMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    try {/*from  ww  w  . j  a  v a  2  s  .  co  m*/
        //            FindAvailableVersions.main(null);
        initialize();

        Artifact artifact = new DefaultArtifact(artifactName);
        DependencyResult dependencyResult = resolveArtifact(artifact);

        artifact = dependencyResult.getRoot().getArtifact();

        List<ArtifactResult> dependencies = dependencyResult.getArtifactResults();

        if (className != null) {
            new ExecObject(getLog(), artifact, dependencies, className, parseArgs(), systemProperties)
                    .execute();
        }

        Class<?> installation = new URLClassLoader(new URL[] { artifact.getFile().toURI().toURL() })
                .loadClass("Installation");

        List<Object[]> entries = (List<Object[]>) OpenBean2.getStaticFieldValue(installation, "shortcuts");

        if (installTo == null) {
            installTo = findPath();
        }

        File installToDir = new File(installTo);

        File classPathFile = writeClasspath(artifact, dependencies, installToDir);

        for (Object[] entry : entries) {
            String shortCut = (String) entry[0];
            String className = entry[1] instanceof String ? entry[1].toString() : ((Class) entry[1]).getName();

            File file;
            if (SystemUtils.IS_OS_WINDOWS) {
                file = new File(installToDir, shortCut + ".bat");
                FileUtils.writeStringToFile(file, createLaunchScript(className, classPathFile));
            } else {
                file = new File(installToDir, shortCut);
                FileUtils.writeStringToFile(file, createLaunchScript(className, classPathFile));
                try {
                    file.setExecutable(true, false);
                } catch (Exception e) {
                    getLog().warn(
                            "could not make '" + file.getAbsolutePath() + "' executable: " + e.toString());
                }
            }

            getLog().info("created a shortcut: " + file.getAbsolutePath() + " -> " + className);
        }
    } catch (Exception e) {
        if (e instanceof RuntimeException) {
            throw (RuntimeException) e;
        } else {
            getLog().error(e.toString(), e);
            throw new MojoExecutionException(e.toString());
        }
    }
}

From source file:com.oneops.inductor.AbstractOrderExecutor.java

/**
 * writes private key//w w w.j  a va  2  s . c om
 *
 * @param key String
 */
protected String writePrivateKey(String key) {
    String uuid = UUID.randomUUID().toString();
    String fileName = config.getDataDir() + "/" + uuid;
    try (BufferedWriter bw = Files.newBufferedWriter(Paths.get(fileName))) {
        bw.write(key);
        bw.close();
        File f = new File(fileName);
        f.setExecutable(false, false);
        f.setReadable(false, false);
        f.setWritable(false, false);
        f.setReadable(true, true);
        f.setWritable(true, true);
        logger.debug("file: " + fileName);
    } catch (IOException e) {
        logger.error("could not write file: " + fileName + " msg:" + e.getMessage());
    }
    return fileName;
}

From source file:com.oneops.inductor.AbstractOrderExecutor.java

/**
 * Creates a chef config file for unique lockfile by ci.
 * returns chef config full path./*from  w w  w.ja  v a2s .  c om*/
 *
 * @param ci
 * @param cookbookRelativePath chef config full path
 * @param logLevel
 * @return
 */
protected String writeChefConfig(String ci, String cookbookRelativePath, String logLevel) {
    String filename = "/tmp/chef-" + ci;

    String cookbookDir = config.getCircuitDir();
    if (!cookbookRelativePath.equals(""))
        cookbookDir = config.getCircuitDir().replace("packer", cookbookRelativePath);

    cookbookDir += "/components/cookbooks";
    String sharedDir = config.getCircuitDir().replace("packer", "shared/cookbooks");

    String content = "cookbook_path [\"" + cookbookDir + "\",\"" + sharedDir + "\"]\n";
    content += "lockfile \"" + filename + ".lock\"\n";
    content += "file_cache_path \"/tmp\"\n";
    content += "log_level :" + logLevel + "\n";
    content += "verify_api_cert true\n";
    FileWriter fstream;
    try {
        fstream = new FileWriter(filename);
        BufferedWriter bw = new BufferedWriter(fstream);
        bw.write(content);
        bw.close();

        File f = new File(filename);
        f.setExecutable(false, false);
        f.setReadable(false, false);
        f.setWritable(false, false);
        f.setReadable(true, true);
        f.setWritable(true, true);
        logger.debug("file: " + filename);

    } catch (IOException e) {
        logger.error("could not write file: " + filename + " msg:" + e.getMessage());
    }
    return filename;
}

From source file:ezbake.protect.ezca.EzCABootstrap.java

public void ensureDirectory(String path) throws IOException {
    File directory = new File(path);

    if (directory.exists() && !directory.isDirectory()) {
        throw new IOException("Unable to create directory. " + path + " already exists and is not a directory");
    }//www .  ja  v a2s. c  o m

    if (!directory.mkdirs() && !directory.isDirectory()) {
        throw new IOException("Unable to make directories, mkdirs returned false");
    }

    if (!directory.setExecutable(false, false) || !directory.setExecutable(true, true)) {
        throw new IOException("Created directory, but unable to set executable");
    }
    if (!directory.setReadable(false, false) || !directory.setReadable(true, true)) {
        throw new IOException("Created directory, but unable to set readable");
    }
    if (!directory.setWritable(false, false) || !directory.setWritable(true, true)) {
        throw new IOException("Created directory, but unable to set writable");
    }
}