// written by Sunita Kanchinadam. // This class is a test driver to create a pseudo file system and // implement some of the OS commands. import EDU.neu.ccs.demeter.dj.*; import DuVisitor; import File; import Directory; import java.io.*; public class OS { // constructor OS() { } /******************************************************************/ // method : main // parameters : String array // return value : void // description : creates filesystem, and lets the user run the // implemented commands on the file system. /******************************************************************/ public static void main(String argv[]) { // creates a class graph object to define traversals cg = new ClassGraph(); System.out.println("The class graph is"); System.out.println(cg); System.out.println("End class graph"); // creates a pseudo file system. OS myos = new OS(); Directory fs = myos.CreateFileSystem(); System.out.println("The commands are - "); System.out.println("ls "); System.out.println("cp "); System.out.println("mv "); System.out.println("mkdir "); System.out.println("du [ -L ]"); System.out.println("exit "); // lets the user login. // this does not perform any security checks, but has been // implemented as a hook for any future implementation. try { System.out.print("login : "); while ((numread = System.in.read(cmd)) >= 0) { login = new String(cmd, 0, numread); break; } } catch (IOException ie) { System.out.println("OS crashed , Please reboot\n"); System.exit(0); } System.out.print("\nprompt > "); String exit = new String("exit\n"); // waits in an infifite loop for the next command // and executes the command till the user // exits out of the system. while (true) { try { // read from the inputstream till end of line while ((numread = System.in.read(cmd)) >= 0) { System.out.write(cmd, 0, numread); break; } String command = new String(cmd, 0, numread); // if exit is typed , get out of OS if (command.equals(exit)) System.exit(0); // if ls is typed list all the files and subdirs if (command.equals("ls\n")) fs.List(); // if cp is typed copy the file. if ( numread >= 2) { if (command.substring(0,2).equals("cp")) { String f1 = command.substring(3,command.indexOf(" ",4)); String name = command.substring(f1.length()+3, command.indexOf("\n",f1.length()+3)); fs.CreateFile(name); } // if mv is typed replace the existing file // with the file with new name. else if (command.substring(0,2).equals("mv")) { String f1 = command.substring(3,command.indexOf(" ",4)); String name = command.substring(f1.length()+3, command.indexOf("\n",f1.length()+3)); fs.moveFile(f1,name); } else if (command.substring(0,2).equals("du")) { DuVisitor du; String name; if ( numread >= 4 ) { name = command.substring(3, command.indexOf("\n",3)); System.out.println("option is " + name ); // du = new DuVisitor("-L"); } else { // System.out.println("option is "); // du = new DuVisitor("-r"); name = "-r"; } System.out.println("Total Size is " + fs.GetTotalSize(name)); } } // if mkdir is typed, create a new directory. if (numread >= 5) { if (command.substring(0,5).equals("mkdir")) fs.Mkdir(command.substring(5,command.indexOf("\n",5))); } System.out.print("\nprompt > "); } catch (IOException ie) { System.out.println("Exception raised"); System.out.println("Your OS crashed, Please reboot."); System.exit(0); } } } /******************************************************************/ // method : CreateFileSystem // parameters : void // return value : Directory // description : Returns the directory that has been built with // files and sub directories. /******************************************************************/ public Directory CreateFileSystem() { Directory myFileSystem1 = new Directory("sunita1"); // myFileSystem1.setOwner("root"); Directory d1 = myFileSystem1.Mkdir("sk1"); if ( d1 != null) { d1.CreateFile("sk11.java"); Directory d11 = d1.Mkdir("sk11"); if (d11 != null) { d11.CreateFile("sk111.java"); } d1.CreateFile("sk12.java"); } Directory d2 = myFileSystem1.Mkdir("sk2"); if ( d2 != null) { d2.CreateFile("sk21.java"); d2.CreateFile("sk22.java"); d2.CreateFile("sk23.java"); } Directory d3 = myFileSystem1.Mkdir("sk3"); if (d3 != null) { d3.CreateFile("sk31.java"); } Directory myFileSystem2 = new Directory("sunita2"); myFileSystem2.CreateFile("sk12link.java"); Directory dlink1 = myFileSystem2.Mkdir("skl2"); if (dlink1 != null) { dlink1.CreateFile("sklink1.java"); dlink1.CreateFile("sklink2.java"); } // myFileSystem1.CreateHardLink("sunitalink",myFileSystem2); // myFileSystem1.CreateSoftLink("sunitalink",myFileSystem2); return (myFileSystem1); } private static byte cmd[] = new byte[1000]; private static int numread; private static String login; public static ClassGraph cg; }