}
private static Path checkDest(String srcName, FileSystem dstFS, Path dst, boolean overwrite) throws IOException { if (dstFS.exists(dst)) {
FileStatus sdst = dstFS.getFileStatus(dst); if (sdst.isDir()) { if (null == srcName) {
throw new IOException(\ }
return checkDest(null, dstFS, new Path(dst, srcName), overwrite); } else if (!overwrite) {
throw new IOException(\ }
} else if (dst.toString().isEmpty()) {
return checkDest(null, dstFS, new Path(srcName), overwrite); }
return dst; } /**
* This class is only used on windows to invoke the cygpath command. */
private static class CygPathCommand extends Shell { String[] command; String result;
CygPathCommand(String path) throws IOException { command = new String[]{\ run(); }
String getResult() throws IOException { return result; }
protected String[] getExecString() { return command;
}
protected void parseExecResult(BufferedReader lines) throws IOException { String line = lines.readLine(); if (line == null) {
throw new IOException(\ \ }
result = line; } } /**
* Convert a os-native filename to a path that works for the shell. * @param filename The filename to convert * @return The unix pathname
* @throws IOException on windows, there can be problems with the subprocess */
public static String makeShellPath(String filename) throws IOException { if (Path.WINDOWS) {
return new CygPathCommand(filename).getResult(); } else {
return filename; } } /**
* Convert a os-native filename to a path that works for the shell. * @param file The filename to convert * @return The unix pathname
* @throws IOException on windows, there can be problems with the subprocess */
public static String makeShellPath(File file) throws IOException { return makeShellPath(file, false); }
/**
* Convert a os-native filename to a path that works for the shell. * @param file The filename to convert * @param makeCanonicalPath
* Whether to make canonical path for the file passed * @return The unix pathname
* @throws IOException on windows, there can be problems with the subprocess */
public static String makeShellPath(File file, boolean makeCanonicalPath) throws IOException { if (makeCanonicalPath) {
return makeShellPath(file.getCanonicalPath()); } else {
return makeShellPath(file.toString()); } } /**
* Takes an input dir and returns the du on that local directory. Very basic * implementation. *
* @param dir
* The input dir to get the disk space of this local dir * @return The total disk space of the input local directory */
public static long getDU(File dir) { long size = 0; if (!dir.exists()) return 0;
if (!dir.isDirectory()) { return dir.length(); } else {
File[] allFiles = dir.listFiles(); if(allFiles != null) {
for (int i = 0; i < allFiles.length; i++) {
boolean isSymLink; try {
isSymLink = org.apache.commons.io.FileUtils.isSymlink(allFiles[i]); } catch(IOException ioe) { isSymLink = true; }
if(!isSymLink) {
size += getDU(allFiles[i]); } } }
return size; } } /**
* Given a File input it will unzip the file in a the unzip directory * passed as the second parameter * @param inFile The zip file as input
* @param unzipDir The unzip directory where to unzip the zip file. * @throws IOException */
public static void unZip(File inFile, File unzipDir) throws IOException { Enumeration extends ZipEntry> entries; ZipFile zipFile = new ZipFile(inFile); try {
entries = zipFile.entries();
while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.isDirectory()) {
InputStream in = zipFile.getInputStream(entry); try {
File file = new File(unzipDir, entry.getName()); if (!file.getParentFile().mkdirs()) {
if (!file.getParentFile().isDirectory()) {
throw new IOException(\ file.getParentFile().toString()); } }
OutputStream out = new FileOutputStream(file); try {
byte[] buffer = new byte[8192]; int i;
while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { out.close(); } } finally { in.close(); } } } } finally { zipFile.close(); } } /**
* Given a Tar File as input it will untar the file in a the untar directory * passed as the second parameter *
* This utility will untar \ *
* @param inFile The tar file as input.
* @param untarDir The untar directory where to untar the tar file. * @throws IOException */