// COM3336 Operating Systems // Homework 2 part 1 // John Sung // Started: 10/22/1999 // import java.util.*; import java.io.*; import File; import ComplexFile; // The simple file to signify a simple file. It really doesn't // add much to the abstract File class. public class SymbolicLink extends File { ////////////////////////////////////////////////////////////// // Private Data Members // the name of the file in which we'll be making a link to. String DestName; // link to the parent ComplexFile ParentDir; // the default constructor public SymbolicLink(String destName, String linkName, File parent) { super(linkName); DestName = destName; ParentDir = (ComplexFile)parent; } // the get method for the DestName public String getDestName() { return DestName; } ////////////////////////////////////////////////////////////// // Abstract methods from the abstract File class // a function to recursively copy a File, with a file // as the parent of the copy. Just returns the // copy of itself. public File recursiveCopy(File Parent) { return new SymbolicLink(getDestName(), getFileName(), Parent); } // function to find a particular file and print it to an // ostream public void recursiveFindAndPrint(String prefix, String fileName, PrintStream out) { // if the 2 filenames are same, then print // the full path to this file if (getFileName().compareTo(fileName)==0) out.println(prefix + "/" + fileName + " -> ./" + getDestName()); } // function to implement the "du ." command. The object that // implements this function sould print itself if it's // a directory to the ostream. public void recursivePrintDirectory(String prefix, PrintStream out) { // don't do anything since, I'm not a directory } // function to implement the "du -a" command. The object that // implements this function sould print itself to the ostream. public void recursivePrintAll(String prefix, PrintStream out) { // print myself out.println(prefix + "/" + getFileName() + " -> ./" +getDestName()); } // function to implement the directory down command, // returns null if something goes wrong. public File changeDirectory(String subDirName, PrintStream err) { // attempt to change directory File newDir = ParentDir.findSubDir(DestName, err); // if the newDir is null then print a message if (newDir == null) { err.println("Change Dir to symbolic link failed.."); // return null to signify an error. return null; } // return the new directory return newDir; } // function returns the parent, unless it doesn't have a // parent. Used to implement cd .. public File getParent() { // print some error since this method should // never have been called. System.out.println("Error getParent() called to a SymbolicLink"); // return null to signify an error. return null; } // function used to implement the touch command. // it attempts to create a file. Outputs any errors // to err. public void createFile(String newFileName, PrintStream err) { // print some error since this method should // never have been called. err.println("Error createFile() called to a SymbolicLink"); // return null to signify an error. return; } // function used to implement the mkdir command. // it attempts to create a directory. Outputs any errors // to err. public void createDir(String newDirName, PrintStream err) { // print some error since this method should // never have been called. err.println("Error createDir() called to a SymbolicLink"); // return null to signify an error. return; } // function used to implement rmdir. Outputs error to // the err PrintStream if something goes wrong. public boolean removeDir(String dirName, PrintStream err) { if (dirName.compareTo(getFileName())==0) { err.println("removeDir: " + dirName + "is a file. Not removed."); return false; } return false; } // function used to implement rm. Outputs error to // the err PrintStream if something goes wrong. public boolean removeFile(String fileName, PrintStream err) { // if the name matches then remove myself from the parent. if (fileName.compareTo(getFileName())==0) { return true; } return false; } }