// written by Sunita Kanchinadam. // This class is derived from Node class. // It implements the Accept() and GetTotalSize() abstract // methods from the Node class. // It maintains an array of all the files or subdirectories // belonging to this directory. import EDU.neu.ccs.demeter.dj.*; import DuVisitor; import Node; import HardLink; import SoftLink; import java.util.*; public class Directory extends Node { // constructor Directory() { setSize(11); } // constructor Directory(String name) { setName(name); setSize(11); } /******************************************************************/ // method : GetTotalSize // parameters : String // return value : integer // description : returns the total size of the directory // including all it's files and subdirectories. // Initialization for DJ, and DuVisitor to // traverse thru the directory structure // computing the total size. /******************************************************************/ public int GetTotalSize(String option) { Strategy sg = new Strategy( "from {Node, Directory, File, Link} to {File,Link}"); TraversalGraph tg = TraversalGraph.compute(OS.cg, sg); System.out.println(tg); DuVisitor v = new DuVisitor(option); tg.traverse(this, v); return (v.returnSize()); } /******************************************************************/ // method : getSize // parameters : void // return value : integer // description : returns the total size of the directory /******************************************************************/ public int getSize() { int s = _nSize; ListIterator iter; iter = list; Node n; iter = list; while (iter != null) { n = iter.first; if (n == null) System.out.print("n is null"); s += n.getSize(); iter = (ListIterator) iter.rest; } return (s); } /******************************************************************/ // method : Accept // parameters : DuVisitor // return value : void // description : Iterates thru each of the files or subdirectories // and calls accept on each of them in turn. /******************************************************************/ /* void Accept(DuVisitor du) { ListIterator iter; iter = list; Node n = list.first; while (n != null) { n.Accept(du); iter = (ListIterator)iter.rest; n = iter.first; } du.before(this); } */ /******************************************************************/ // method : Mkdir // parameters : String // return value : Directory // description : Checks if the user has write permissions // and creates a directory object and stores in the array. // If the user does not have permissions, // gives a message and returns null. /******************************************************************/ Directory Mkdir(String name) { // add some checking for path and to check if directory // already exists. if (hasWritePermissions()) { Directory dir = new Directory(name); if (list == null) list = new ListIterator(dir, null); else { list = new ListIterator(dir,(Iterator)list); System.out.println("Adding dir -"+dir.getName()); } length = length + 1; // listIterator[length] = dir; return (dir); } else { System.out.println("You don't have write permissions"); return (null); } } /******************************************************************/ // method : CreateFile // parameters : String // return value : void // description : Checks if the user has write permissions // and creates a file object and stores in the array. // If the user does not have permissions, // gives a message and returns. /******************************************************************/ void CreateFile(String name) { if (hasWritePermissions()) { File f = new File(name); if (list == null) { list = new ListIterator(f, null); System.out.println("Adding file -"+f.getName()); } else list = new ListIterator(f,(Iterator)list); // listIterator[length] = f; length = length + 1; } else System.out.println("You don't have write permissions"); } /******************************************************************/ // method : CreateHardLink // parameters : String, Node // return value : void // description : Creates a hardlink with the name passed as // parameter and stores in the array. /******************************************************************/ void CreateHardLink(String name , Node linkTo) { HardLink l = new HardLink(name); length = length + 1; if (list == null) list = new ListIterator(l, null); else { list = new ListIterator(l,(Iterator)list); System.out.println("Adding hard link - "+l.getName()); } // listIterator[length] = linkTo; } /******************************************************************/ // method : CreateSoftLink // parameters : String, Node // return value : void // description : Creates a softlink with the name passed as // parameter and stores in the array. /******************************************************************/ void CreateSoftLink(String name , Node linkTo) { SoftLink l = new SoftLink(name,linkTo); length = length + 1; if (list == null) list = new ListIterator(l, null); else { list = new ListIterator(l,(Iterator) list); System.out.println("Adding soft link - "+l.getName()); } // listIterator[length] = l; } /******************************************************************/ // method : List // parameters : void // return value : void // description : Iterates thru each of the files or subdirectories // and lists out the names. /******************************************************************/ void List() { ListIterator iter; iter = list; Node n; while (iter != null) { n = iter.first; if (n == null) System.out.print("n is null"); System.out.print(" " + n.getName()); iter = (ListIterator) iter.rest; } } /******************************************************************/ // method : moveFile // parameters : String, String // return value : void // description : Iterates thru each of the files/subdirectories, // comparing against the name passed as parameter, // and renames it to the new name. /******************************************************************/ void moveFile(String f1, String name) { /* for (int i = 1; i <= length; i++) { Node n = (Node) listIterator[i]; if (n.getName().equals(f1)) { n.setName(name); listIterator[i] = n; return; } } */ } /******************************************************************/ // method : hasWritePermissions // parameters : void // return value : boolean // description : Checks if the user has permission to access this // directory.Presently, it returns true in all cases // but, in future, it is expected to check if the user // logged in (that will be a global public member from // OS class) has permissions.Permissions should be // a private member of the Node class of type Byte. /******************************************************************/ public boolean hasWritePermissions() { return(true); } public ListIterator list; public int length = 0; }