package ncku.hpds.hadoop.fedhdfs.shell;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Lsr {
	
	public static void printFilesRecursively(String Uri, Configuration conf)
			throws IOException {
		try {

			FileSystem FS = FileSystem.get(URI.create(Uri), conf);
			FileStatus[] status = FS.listStatus(new Path(Uri));

			for (int i = 0; i < status.length; i++) {
				if (status[i].isDirectory()) {
					show.DirFileInfo(status[i].getPath().toString(), conf);
					printFilesRecursively(status[i].getPath().toString(), conf);

				} else {
					try {
						show.DirFileInfo(status[i].getPath().toString(), conf);
					} catch (Exception e) {
						System.err.println(e.toString());
					}
				}
			}
		} catch (IOException e) {
		}
	}
}

class show {
	
	public static void DirFileInfo(String Uri, Configuration conf) throws IOException {

		FileSystem FS = FileSystem.get(URI.create(Uri), conf);
		Path Path = new Path(Uri);
		FileStatus fileStatus = FS.getFileStatus(Path);
		
		int tmpUserIndex = fileStatus.getPath().toString().indexOf("/user");
		String userPath = fileStatus.getPath().toString().substring(tmpUserIndex, fileStatus.getPath().toString().length());

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println(fileStatus.getPermission().toString()
				+ String.format("%4d", fileStatus.getReplication())
				+ " "
				+ fileStatus.getOwner()
				+ " "
				+ fileStatus.getGroup()
				+ String.format("%11d", fileStatus.getLen())
				+ " "
				+ f.format(new Timestamp(fileStatus.getModificationTime()))
				+ " " + userPath); 
				/*fileStatus.getPath().getName());*/
	}
}

@@ -304,16 +304,59 @@
 fs.Path;
+%0Aimport org.apache.hadoop.util.StringUtils;
 %0A%0Apublic
@@ -1884,14 +1884,941 @@
 me());*/%0A%09%7D%0A
+%09%0A%09public static void DirFileInfoHuman(String Uri, Configuration conf) throws IOException %7B%0A%0A%09%09FileSystem FS = FileSystem.get(URI.create(Uri), conf);%0A%09%09Path Path = new Path(Uri);%0A%09%09FileStatus fileStatus = FS.getFileStatus(Path);%0A%09%09%0A%09%09int tmpUserIndex = fileStatus.getPath().toString().indexOf(%22/user%22);%0A%09%09String userPath = fileStatus.getPath().toString().substring(tmpUserIndex, fileStatus.getPath().toString().length());%0A%0A%09%09SimpleDateFormat f = new SimpleDateFormat(%22yyyy-MM-dd HH:mm%22);%0A%09%09System.out.println(fileStatus.getPermission().toString()%0A%09%09%09%09+ String.format(%22%254d%22, fileStatus.getReplication())%0A%09%09%09%09+ %22 %22%0A%09%09%09%09+ fileStatus.getOwner()%0A%09%09%09%09+ %22 %22%0A%09%09%09%09+ fileStatus.getGroup()%0A%09%09%09%09+ String.format(%22%2511d%22, fileStatus.getLen()) + %22 (%22 + StringUtils.byteDesc(fileStatus.getLen()) + %22)%22%0A%09%09%09%09+ %22 %22%0A%09%09%09%09+ f.format(new Timestamp(fileStatus.getModificationTime()))%0A%09%09%09%09+ %22 %22 + userPath); %0A%09%09%09%09/*fileStatus.getPath().getName());*/%0A%09%7D%0A
 %7D%0A

add human readable method from list file length