Exemple #1
0
		static public void AddRoot (FileModel root)
		{
			if (! root.IsRoot)
				throw new Exception ("Attempted to add non-root as root");

			roots.Add (root);
		}
Exemple #2
0
		//////////////////////////////////////////////////////////////

		// Useful utility functions

		static private string PickName (FileModel p)
		{
			string pick;
			do {
				pick = Token.GetRandom ();
			} while (p.children.Contains (pick));
			return pick;
		}
Exemple #3
0
		// 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;
		}
Exemple #4
0
		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;
		}
Exemple #5
0
		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;
		}
Exemple #6
0
		// 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;
		}
Exemple #7
0
		public static FileModel NewRoot ()
		{
			FileModel root = new FileModel ();
			root.name = PathFinder.HomeDir;
			root.children = new Hashtable ();
			root.mtime = Directory.GetLastWriteTimeUtc (root.FullName);
			return root;
		}
Exemple #8
0
		// IsBelow is easier to thik about than IsDescendantOf
		public bool IsBelow (FileModel other)
		{
			return other.IsAbove (this);
		}
Exemple #9
0
		// IsAbove is easier to think about than IsAncestorOf
		public bool IsAbove (FileModel other)
		{
			return this == other.parent 
				|| (other.parent != null && IsAbove (other.parent));
		}
Exemple #10
0
 // IsBelow is easier to thik about than IsDescendantOf
 public bool IsBelow(FileModel other)
 {
     return(other.IsAbove(this));
 }
Exemple #11
0
 // IsAbove is easier to think about than IsAncestorOf
 public bool IsAbove(FileModel other)
 {
     return(this == other.parent ||
            (other.parent != null && IsAbove(other.parent)));
 }