/*
 *  Copyright 2009 University Of Southern California
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package edu.usc.corral.util;

import java.io.File;
import java.io.IOException;

public class FilesystemUtil {
	
	public static boolean chmod(File file, int mode) {
		try {
			CommandLine chown = new CommandLine();
			chown.setCommand("chmod");
			chown.addArgument(String.valueOf(mode));
			chown.addArgument(file.getAbsolutePath());
			chown.execute();
			int exitCode = chown.getExitCode();
			if(exitCode != 0){
				return false;
			} else {
				return true;
			}
		} catch (IOException ioe) {
			return false;
		}
	}
	
	public static boolean chown(File file, String user) {
		try {
			CommandLine chown = new CommandLine();
			chown.setCommand("chown");
			if (file.isDirectory()) {
				chown.addArgument("-R");
			}
			chown.addArgument(user);
			chown.addArgument(file.getAbsolutePath());
			chown.execute();
			int exitCode = chown.getExitCode();
			if(exitCode != 0){
				return false;
			} else {
				return true;
			}
		} catch (IOException ioe) {
			return false;
		}
	}
	
	public static boolean rm(File file) {
		try {
			CommandLine rm = new CommandLine();
			rm.setCommand("rm");
			if (file.isDirectory()) {
				rm.addArgument("-r");
			}
			rm.addArgument("-f");
			rm.addArgument(file.getAbsolutePath());
			rm.execute();
			int exitCode = rm.getExitCode();
			if(exitCode != 0){
				return false;
			} else {
				return true;
			}
		} catch (IOException ioe) {
			return false;
		}
	}
}

@@ -777,16 +777,35 @@
 nt mode)
+ throws IOException
  %7B%0A%09%09try
@@ -816,35 +816,35 @@
 %09%09CommandLine ch
-own
+mod
  = new CommandLi
@@ -846,35 +846,35 @@
 andLine();%0A%09%09%09ch
-own
+mod
 .setCommand(%22chm
@@ -876,35 +876,35 @@
 (%22chmod%22);%0A%09%09%09ch
-own
+mod
 .addArgument(Str
@@ -920,35 +920,35 @@
 Of(mode));%0A%09%09%09ch
-own
+mod
 .addArgument(fil
@@ -966,35 +966,35 @@
 tePath());%0A%09%09%09ch
-own
+mod
 .execute();%0A%09%09%09i
@@ -1001,35 +1001,35 @@
 nt exitCode = ch
-own
+mod
 .getExitCode();%0A
@@ -1131,36 +1131,81 @@
 n ioe) %7B%0A%09%09%09
-return false
+throw new IOException(%22Unable to chmod file: %22+file, ioe)
 ;%0A%09%09%7D%0A%09%7D%0A%09%0A%09
@@ -1255,16 +1255,35 @@
 ng user)
+ throws IOException
  %7B%0A%09%09try
@@ -1656,36 +1656,81 @@
 n ioe) %7B%0A%09%09%09
-return false
+throw new IOException(%22Unable to chown file: %22+file, ioe)
 ;%0A%09%09%7D%0A%09%7D%0A%09%0A%09
@@ -1764,16 +1764,35 @@
 le file)
+ throws IOException
  %7B%0A%09%09try
@@ -2145,28 +2145,73 @@
 e) %7B%0A%09%09%09
-return false
+throw new IOException(%22Unable to chown file: %22+file, ioe)
 ;%0A%09%09%7D%0A%09%7D

Throw exceptions if chmod/chown/rm fail