public static void copyDir(File srcDir, File destDir) throws IOException, InterruptedException { if (!Os.isMicrosoftWindows()) { String cmd = "cp -R '"+srcDir.getAbsolutePath()+"' '"+destDir.getAbsolutePath()+"'"; Process proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); if (proc.exitValue() == 0) return; } FileUtils.copyDirectory(srcDir, destDir); }
public static void moveDir(File srcDir, File destDir) throws IOException, InterruptedException { if (!Os.isMicrosoftWindows()) { String cmd = "mv '"+srcDir.getAbsolutePath()+"' '"+destDir.getAbsolutePath()+"'"; Process proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); if (proc.exitValue() == 0) return; } FileUtils.moveDirectory(srcDir, destDir); }
private static void logSetFilePermissionsFailure(final String permissions, File file, IOException ex) { if (loggedSetFilePermissionsWarning) { if (LOG.isTraceEnabled()) LOG.trace("Failed to set permissions to {} for file {}: {}", new Object[] {permissions, file.getAbsolutePath(), ex}); } else { if (Os.isMicrosoftWindows()) { if (LOG.isDebugEnabled()) LOG.debug("Failed to set permissions to {} for file {}; expected behaviour on Windows; {}; subsequent failures (on any file) will be logged at trace", new Object[] {permissions, file.getAbsolutePath(), ex}); } else { LOG.warn("Failed to set permissions to {} for file {}: {}; subsequent failures (on any file) will be logged at trace", new Object[] {permissions, file.getAbsolutePath(), ex}); } loggedSetFilePermissionsWarning = true; } }
public void apply(File file) throws IOException { Path filePath = file.toPath(); // the appropriate condition is actually Files.getFileStore(filePath).supportsFileAttributeView(PosixFileAttributeView.class) // but that downs performance to ~9000 calls per second boolean done = false; try { // ~59000 calls per sec if (!Os.isMicrosoftWindows()) { Files.setPosixFilePermissions(filePath, posixPermissions); } done = true; } catch (UnsupportedOperationException ex) {} if (!done) { // ~42000 calls per sec // TODO: what happens to group permissions ? boolean setRead = file.setReadable(posixPermissions.contains(OTHERS_READ), false) & file.setReadable(posixPermissions.contains(OWNER_READ), true); boolean setWrite = file.setWritable(posixPermissions.contains(OTHERS_WRITE), false) & file.setWritable(posixPermissions.contains(OWNER_WRITE), true); boolean setExec = file.setExecutable(posixPermissions.contains(OTHERS_EXECUTE), false) & file.setExecutable(posixPermissions.contains(OWNER_EXECUTE), true); if (!(setRead && setWrite && setExec)) { throw new IOException("setting file permissions failed: read=" + setRead + " write=" + setWrite + " exec=" + setExec); } } }
private List<String> getTestCommand() { if(Os.isMicrosoftWindows()) { return Arrays.asList("cmd", "/c", "echo", "hello", "world"); } else { return Arrays.asList("echo", "hello", "world"); } }
/** * This utility will be deleted when we move to Java 7 * * @return The file permission (in a form like "-rwxr--r--"), or null if the permissions could not be determined. */ @Beta public static Maybe<String> getFilePermissions(File file) { if (!file.exists()) return Maybe.absent("File "+file+" does not exist"); if (Os.isMicrosoftWindows()) { return Maybe.absent("Cannot determine permissions on windows"); } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); int exitcode = exec(ImmutableList.of("ls", "-ld", file.getAbsolutePath()), out, err); if (exitcode != 0) { if (LOG.isDebugEnabled()) LOG.debug("Could not determine permissions of file "+file+"; exit code "+exitcode+"; stderr "+new String(err.toByteArray())); return Maybe.absent("Could not determine permission of file "+file+"; exit code "+exitcode); } String stdout = new String(out.toByteArray()); return (stdout.trim().isEmpty() ? Maybe.<String>absent("empty output") : Maybe.of(stdout.split("\\s")[0])); } }