import java.util.*; public class FileSystem { CompoundFile dirs; CompoundFile current_dir; // constructor FileSystem(CompoundFile starter) { dirs = starter; current_dir = starter; } public synchronized void exitSystem() { // exit shell System.exit(0); } public synchronized void MakeDirectory (String str) { //add the new file to the end of the compound file //list , only if no file exist with the same name if (current_dir.isExist(str)) System.out.println("mkdir:" + str + ":File exists"); else{ CompoundFile newFile=new CompoundFile(current_dir,str); current_dir.File_list.add(current_dir.File_list.size(),newFile); } } // end of MakeDirectory() public synchronized void ChangeDirectoryUp() { CompoundFile upper_dir = (CompoundFile)(current_dir.getParentDir()); if (upper_dir != null) // not under root directory current_dir = upper_dir; } // end of ChangeDirectoryUp() public synchronized void ChangeDirectoryDown(String name) { ArrayList tmpList = current_dir.File_list; if (current_dir.isExist(name)) { for (int k = 0; k < tmpList.size(); k ++) { File tmpFile = (File)(tmpList.get(k)); String nameFound=tmpFile.getFileName().getI(); if (nameFound.compareTo(name) == 0) { if (tmpFile.getDir() == null) { System.out.println(name + ": Not a directory."); } else { current_dir = tmpFile.getDir(); } } } } else System.out.println(name + ": No such file or directory."); } // end of ChangeDirectoryDown(String) public synchronized void DiskUsage() { ArrayList tmpList = current_dir.File_list; String str = "./"; if (tmpList.isEmpty()) System.out.println("."); else { for (int k = 0; k < tmpList.size(); k ++) { File tmpFile = (File)(tmpList.get(k)); tmpFile.print(str); } System.out.println("."); } } // end of DiskUsage() public synchronized void MakeLink(String a, String b){ ArrayList tmpList = current_dir.File_list; if (!current_dir.isExist(b)) { if (current_dir.isExist(a)) { for (int k = 0; k < tmpList.size(); k ++) { File tmpFile = (File) tmpList.get(k); String nameFound = tmpFile.getFileName().getI(); if (nameFound.compareTo(a) == 0) { Link newLink = new Link(tmpFile,b); current_dir.File_list.add(tmpList.size(),newLink); } } } // if both a and b don't exist, create a empty file b else { SimpleFile newFile = new SimpleFile(current_dir,b); current_dir.File_list.add(current_dir.File_list.size(),newFile); } } // if there is already a file having name b, report the error else System.out.println("ln : cannot create "+b+":File exists"); } // end of MakeLink public synchronized void RemoveDir(String name){ ArrayList tmpList = current_dir.File_list; if (current_dir.isExist(name)) { for (int k = 0; k < tmpList.size(); k ++ ) { File tmpFile = (File)(tmpList.get(k)); String nameFound=tmpFile.getFileName().getI(); if (nameFound.compareTo(name) == 0) { if ((tmpFile.getDir() == null) || (tmpFile.getLink() != null)) System.out.println("rmdir: directory " + name + ": Path component not a directory"); else current_dir.File_list.remove(k); } } } else System.out.println("rmdir:directory "+name+": Directory does not exist"); } // end of RemoveDir() public synchronized void RemoveSmFile(String name){ ArrayList tmpList = current_dir.File_list; if (current_dir.isExist(name)) { for (int k = 0; k < tmpList.size(); k ++ ) { File tmpFile = (File)(tmpList.get(k)); String nameFound=tmpFile.getFileName().getI(); if (nameFound.compareTo(name) == 0) { if ((tmpFile.getLink()!=null) || (tmpFile.getDir() == null)) current_dir.File_list.remove(k); else System.out.println("rm:" + name + " is a directory"); } } } else System.out.println(name +": No such file or directory"); }// end of RemoveSmFile() public synchronized void CreatEmpFile(String name) { ArrayList tmpList = current_dir.File_list; if (!current_dir.isExist(name)) { SimpleFile newFile = new SimpleFile(current_dir,name); current_dir.File_list.add(current_dir.File_list.size(),newFile); } else System.out.println("Creat-empty-file:"+name + ": File exists"); } // end of CreatEmpFile( ) } // end of class FileSystem