static public void AddRoot (FileModel root) { if (! root.IsRoot) throw new Exception ("Attempted to add non-root as root"); roots.Add (root); }
////////////////////////////////////////////////////////////// // Useful utility functions static private string PickName (FileModel p) { string pick; do { pick = Token.GetRandom (); } while (p.children.Contains (pick)); return pick; }
// If the move would cause a filename collision or is // otherwise impossible, return false. Return true if the // move actually happens. public bool MoveTo (FileModel new_parent, // or null, to just rename string new_name) // or null, to just move { if (! new_parent.IsDirectory) throw new ArgumentException ("Parent must be a directory"); if (this.IsRoot) throw new ArgumentException ("Can't move a root"); // Impossible if (this == new_parent || this.IsAbove (new_parent)) return false; string old_path; old_path = this.FullName; if (new_parent == null) new_parent = this.parent; if (new_name == null) new_name = this.name; // check for a filename collision if (new_parent.children.Contains (new_name)) return false; // modify the data structure this.parent.children.Remove (this.name); this.parent = new_parent; this.name = new_name; this.parent.children [this.name] = this; string new_path; new_path = Path.Combine (new_parent.FullName, new_name); if (this.IsDirectory) Directory.Move (old_path, new_path); else File.Move (old_path, new_path); return true; }
public void Delete () { if (IsRoot) throw new Exception ("Can't delete the root!"); if (IsDirectory) Directory.Delete (FullName, true); // recursive else File.Delete (FullName); parent.children.Remove (name); parent = null; }
public FileModel NewFile () { if (! IsDirectory) throw new ArgumentException ("parent must be a directory"); // no more names left if (children.Count == Token.Count) return null; FileModel child; child = new FileModel (); child.name = PickName (this); child.body = NewBody (10); child.parent = this; children [child.name] = child; // Create the file child.Write (); return child; }
// Creates a randomly-named new directory. // Avoid name collisions with existing files. public FileModel NewDirectory () { if (! IsDirectory) throw new ArgumentException ("parent must be a directory"); // no more names left if (children.Count == Token.Count) return null; FileModel child; child = new FileModel (); child.name = PickName (this); child.children = new Hashtable (); child.parent = this; children [child.name] = child; // Actually create the directory Directory.CreateDirectory (child.FullName); child.mtime = Directory.GetLastWriteTimeUtc (child.FullName); return child; }
public static FileModel NewRoot () { FileModel root = new FileModel (); root.name = PathFinder.HomeDir; root.children = new Hashtable (); root.mtime = Directory.GetLastWriteTimeUtc (root.FullName); return root; }
// IsBelow is easier to thik about than IsDescendantOf public bool IsBelow (FileModel other) { return other.IsAbove (this); }
// IsAbove is easier to think about than IsAncestorOf public bool IsAbove (FileModel other) { return this == other.parent || (other.parent != null && IsAbove (other.parent)); }
// IsBelow is easier to thik about than IsDescendantOf public bool IsBelow(FileModel other) { return(other.IsAbove(this)); }
// IsAbove is easier to think about than IsAncestorOf public bool IsAbove(FileModel other) { return(this == other.parent || (other.parent != null && IsAbove(other.parent))); }