Example usage for java.io File canExecute

List of usage examples for java.io File canExecute

Introduction

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

Prototype

public boolean canExecute() 

Source Link

Document

Tests whether the application can execute the file denoted by this abstract pathname.

Usage

From source file:org.jumpmind.symmetric.util.SnapshotUtil.java

protected static void printDirectoryContents(File dir, StringBuilder output) throws IOException {
    output.append("\n");
    output.append(dir.getCanonicalPath());
    output.append("\n");

    File[] files = dir.listFiles();
    for (File file : files) {
        output.append("  ");
        output.append(file.canRead() ? "r" : "-");
        output.append(file.canWrite() ? "w" : "-");
        output.append(file.canExecute() ? "x" : "-");
        output.append(StringUtils.leftPad(file.length() + "", 11));
        output.append(" ");
        output.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(file.lastModified())));
        output.append(" ");
        output.append(file.getName());// w  ww .j  a v  a 2s  .co  m
        output.append("\n");
    }

    for (File file : files) {
        if (file.isDirectory()) {
            printDirectoryContents(file, output);
        }
    }

}

From source file:com.fizzed.stork.assembly.AssemblyUtils.java

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;/*from w  w  w.j a v  a 2 s  .  c om*/
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        try (FileInputStream in = new FileInputStream(f)) {
            IOUtils.copy(in, tgzout);
        }
        tgzout.closeArchiveEntry();
    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}

From source file:com.fizzed.stork.util.AssemblyUtils.java

static public void addFileToTGZStream(TarArchiveOutputStream tgzout, File f, String base, boolean appendName)
        throws IOException {
    //File f = new File(path);
    String entryName = base;/*  w ww.  j  a  v  a2s  .  c  o m*/
    if (appendName) {
        if (!entryName.equals("")) {
            if (!entryName.endsWith("/")) {
                entryName += "/" + f.getName();
            } else {
                entryName += f.getName();
            }
        } else {
            entryName += f.getName();
        }
    }
    TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

    if (f.isFile()) {
        if (f.canExecute()) {
            // -rwxr-xr-x
            tarEntry.setMode(493);
        } else {
            // keep default mode
        }
    }

    tgzout.putArchiveEntry(tarEntry);

    if (f.isFile()) {
        FileInputStream in = new FileInputStream(f);
        IOUtils.copy(in, tgzout);
        in.close();
        tgzout.closeArchiveEntry();

    } else {
        tgzout.closeArchiveEntry();
        File[] children = f.listFiles();
        if (children != null) {
            for (File child : children) {
                logger.info(" adding: " + entryName + "/" + child.getName());
                addFileToTGZStream(tgzout, child, entryName + "/", true);
            }
        }
    }
}

From source file:com.jhash.oimadmin.Utils.java

public static void extractJarFile(String directory, String jarFileName) {
    File baseDir = new File(directory);
    if (baseDir.exists()) {
        if (!baseDir.isDirectory()) {
            throw new InvalidParameterException("Destination directory " + directory + " to expand Jar file "
                    + jarFileName + " is not a directory");
        }//from ww  w.  j av a  2s  .  co m
        if (!baseDir.canWrite() || !baseDir.canWrite() || !baseDir.canExecute()) {
            throw new InvalidParameterException("Destination directory " + directory + " to expand Jar file "
                    + jarFileName + " does not have rwx access for user");
        }
    } else {
        baseDir.mkdirs();
    }
    try (JarFile jar = new JarFile(jarFileName)) {
        Enumeration enumEntries = jar.entries();
        while (enumEntries.hasMoreElements()) {
            JarEntry file = (JarEntry) enumEntries.nextElement();
            File f = new File(directory + File.separator + file.getName());
            if (file.isDirectory()) { // if its a directory, create it
                f.mkdirs();
                continue;
            }
            try (java.io.InputStream is = jar.getInputStream(file);
                    java.io.FileOutputStream fos = new java.io.FileOutputStream(f)) {
                // get the input stream
                while (is.available() > 0) { // write contents of 'is' to 'fos'
                    fos.write(is.read());
                }
                fos.close();
                is.close();
            } catch (Exception exception) {
                throw new OIMAdminException("Failed to write the jar file entry " + file + " to location " + f,
                        exception);
            }
        }
    } catch (Exception exception) {
        throw new OIMAdminException("Failed to extract jar file " + jarFileName + " to directory " + directory,
                exception);
    }
}

From source file:com.docd.purefm.test.CommandLineFileTest.java

private static void testAgainstJavaIoFile(final CommandLineFile genericFile, final File javaFile,
        final boolean testDate) throws Throwable {
    assertEquals(javaFile, genericFile.toFile());
    assertEquals(javaFile.getName(), genericFile.getName());
    assertEquals(javaFile.getAbsolutePath(), genericFile.getAbsolutePath());
    assertEquals(javaFile.exists(), genericFile.exists());
    assertEquals(javaFile.canRead(), genericFile.canRead());
    assertEquals(javaFile.canWrite(), genericFile.canWrite());
    assertEquals(javaFile.canExecute(), genericFile.canExecute());
    assertEquals(javaFile.getPath(), genericFile.getPath());
    assertEquals(javaFile.getParent(), genericFile.getParent());
    final File parentFile;
    final GenericFile genericParentFile = genericFile.getParentFile();
    if (genericParentFile == null) {
        parentFile = null;//from w  w w .  j  a  v a 2 s  .c o  m
    } else {
        parentFile = genericParentFile.toFile();
    }
    assertEquals(javaFile.getParentFile(), parentFile);
    assertEquals(javaFile.length(), genericFile.length());
    try {
        assertEquals(FileUtils.isSymlink(javaFile), genericFile.isSymlink());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        assertEquals(javaFile.getCanonicalPath(), genericFile.getCanonicalPath());
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertEquals(javaFile.length(), genericFile.length());
    assertEquals(javaFile.isDirectory(), genericFile.isDirectory());
    if (genericFile.isDirectory()) {
        assertTrue(listedPathsEqual(javaFile.list(), genericFile.list()));
        assertTrue(listedFilesEqual(javaFile.listFiles(), genericFile.listFiles()));
    }

    if (testDate) {
        assertEquals(PFMTextUtils.humanReadableDate(javaFile.lastModified(), false),
                PFMTextUtils.humanReadableDate(genericFile.lastModified(), true));
    }
}

From source file:emperior.Main.java

private static void checkBatFile() throws Exception {
    File batFile = new File(mainFrame.getBatFilePath());
    if (!batFile.exists())
        throw new Exception("Bat file does not exist (" + batFile.getAbsolutePath() + ")");
    if (!batFile.canExecute())
        throw new Exception("[Linux] Bat file can not be executed - set it as application ("
                + batFile.getAbsolutePath() + ")");
}

From source file:org.apache.cloudstack.storage.configdrive.ConfigDriveBuilder.java

/**
 *  Checks if the 'genisoimage' or 'mkisofs' is available and return the full qualified path for the program.
 *  The path checked are the following:/*from w  w  w. j a va 2  s  . c o  m*/
 *  <ul>
 *  <li> /usr/bin/genisoimage
 *  <li> /usr/bin/mkisofs
 * </ul> /usr/local/bin/mkisofs
 */
static String getProgramToGenerateIso() throws IOException {
    File isoCreator = new File("/usr/bin/genisoimage");
    if (!isoCreator.exists()) {
        isoCreator = new File("/usr/bin/mkisofs");
        if (!isoCreator.exists()) {
            isoCreator = new File("/usr/local/bin/mkisofs");
        }
    }
    if (!isoCreator.exists()) {
        throw new CloudRuntimeException(
                "Cannot create iso for config drive using any know tool. Known paths [/usr/bin/genisoimage, /usr/bin/mkisofs, /usr/local/bin/mkisofs]");
    }
    if (!isoCreator.canExecute()) {
        throw new CloudRuntimeException(
                "Cannot create iso for config drive using: " + isoCreator.getCanonicalPath());
    }
    return isoCreator.getCanonicalPath();
}

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();//from  w  ww .ja v  a2s  . co 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:net.community.chest.gitcloud.facade.ConfigUtils.java

/**
 * Checks that a give property value that represents a folder is valid as follows:</BR>
 * <UL>/*from w  w w.  j a va 2s  .  c om*/
 *      <LI>If exists, then it must be a folder with read/write/execute permissions</LI>
 *      <LI>Otherwise, it is created along with its parents</LI>
 * <UL>
 * @param propName The property name (used for meaningful exception message)
 * @param propValue The {@link File} to verify
 * @return <code>false</code> if this is an already existing folder, <code>true</code>
 * if had to create it (and its parents)
 * @throws IllegalStateException if any of the validation tests fails
 * @see File#mkdirs()
 */
public static final boolean verifyFolderProperty(String propName, File propValue) throws IllegalStateException {
    if (propValue.exists()) {
        if (!propValue.isDirectory()) {
            throw new IllegalStateException("verifyFolderProperty(" + propName + ") not a folder: "
                    + ExtendedFileUtils.toString(propValue));
        }

        if (!propValue.canRead()) {
            throw new IllegalStateException("verifyFolderProperty(" + propName + ") non-readable: "
                    + ExtendedFileUtils.toString(propValue));
        }

        if (!propValue.canWrite()) {
            throw new IllegalStateException("verifyFolderProperty(" + propName + ") non-writeable: "
                    + ExtendedFileUtils.toString(propValue));
        }

        if (!propValue.canExecute()) {
            throw new IllegalStateException("verifyFolderProperty(" + propName + ") non-listable: "
                    + ExtendedFileUtils.toString(propValue));
        }

        return false;
    } else {
        if (!propValue.mkdirs()) {
            throw new IllegalStateException("verifyFolderProperty(" + propName + ") failed to create: "
                    + ExtendedFileUtils.toString(propValue));
        }

        return true;
    }
}

From source file:com.boundlessgeo.wps.grass.GrassProcesses.java

private static File bin(String command) {
    File exec;
    if (SYSTEM == Env.WINDOWS) {
        exec = new File(new File(BIN), command + ".bat");
        if (!exec.exists()) {
            exec = new File(new File(BIN), command + ".exe");
        }//from w  w w.  j  a  va 2  s .  c  o  m
    } else {
        exec = new File(new File(BIN), command);
    }

    if (!exec.exists()) {
        throw new IllegalStateException(command + " not found:" + exec);
    }
    if (!exec.canExecute()) {
        throw new IllegalStateException(command + " not executable:" + exec);
    }
    return exec;
}