public void Resolving_Path_If_Root_Has_Terminating_Separator_Char_Should_Work()
    {
      root = new DirectoryInfo(@"C:\Test\");

      //test with forward and backward slashes
      Assert.IsTrue(root.IsParentOf(@"C:\Test\folder\readme.txt"));
      Assert.IsTrue(root.IsParentOf(@"C:\Test\folder\subfolder\"));

      Assert.IsTrue(root.IsParentOf(@"C:/Test/folder/readme.txt"));
      Assert.IsTrue(root.IsParentOf(@"C:/Test/folder/subfolder/"));

      root = new DirectoryInfo(@"C:\Test/");

      Assert.IsTrue(root.IsParentOf(@"C:\Test\folder\readme.txt"));
      Assert.IsTrue(root.IsParentOf(@"C:\Test\folder\subfolder\"));

      Assert.IsTrue(root.IsParentOf(@"C:/Test/folder/readme.txt"));
      Assert.IsTrue(root.IsParentOf(@"C:/Test/folder/subfolder/"));
    }
 public void Resolving_Path_If_Test_Has_Terminating_Separator_Char_Should_Work()
 {
   root = new DirectoryInfo(@"C:\Test");
   Assert.IsFalse(root.IsParentOf(@"C:\Test\"));
 }
 public void Validation_Should_Work_If_Root_Is_A_Drive()
 {
   root = new DirectoryInfo(@"C:\");
   Assert.IsTrue(root.IsParentOf(@"C:\Test"));
   Assert.IsFalse(root.IsParentOf(@"C:\"));
 }
 public void Submitting_Path_Of_An_Existing_File_Should_Work()
 {
   //the validation routine internally converts the path to a directory
   //make sure this will work, even if the path links to an existing file
   FileInfo fi = new FileInfo(GetType().Assembly.Location);
   root = fi.Directory;
   Assert.IsTrue(root.IsParentOf(fi.FullName));
   Assert.IsFalse(root.IsParentOf(fi.Directory.FullName));
 }
    private VirtualFolderInfo PerformFolderOperation(string virtualFolderPath, string destinationPath, FileOperation operation)
    {
      if (virtualFolderPath == null) throw new ArgumentNullException("virtualFolderPath");
      if (destinationPath == null) throw new ArgumentNullException("destinationPath");

      //get folder info for source and destination, thus validating the scope of both
      string absoluteSource;
      var sourceFolder = GetFolderInfoInternal(virtualFolderPath, true, out absoluteSource);
      string absoluteDestination;
      GetFolderInfoInternal(destinationPath, false, out absoluteDestination);

      string operationName = operation == FileOperation.Move ? "move" : "copy";

      if (sourceFolder.IsRootFolder)
      {
        string msg = String.Format("Cannot {0} root folder (attempted destination: '{1}').", operationName, destinationPath);
        VfsLog.Debug(msg);
        throw new ResourceAccessException(msg);
      }

      if (String.Equals(absoluteSource, absoluteDestination, StringComparison.InvariantCultureIgnoreCase))
      {
        string msg = String.Format("Cannot {0} folder to '{1}' - source and destination are the same.", operationName, destinationPath);
        VfsLog.Debug(msg);
        throw new ResourceAccessException(msg);
      }

      var sourceDir = new DirectoryInfo(absoluteSource);
      if (sourceDir.IsParentOf(absoluteDestination))
      {
        string msg = String.Format("Cannot {0} folder '{1}' to '{2}' - destination folder is a child of the folder.", operationName, virtualFolderPath, destinationPath);
        VfsLog.Debug(msg);
        throw new ResourceAccessException(msg);
      }

      if (Directory.Exists(absoluteDestination))
      {
        string msg = "Cannot {0} folder '{1}' to '{2}' - the destination folder already exists.";
        msg = String.Format(msg, operationName, virtualFolderPath, destinationPath);
        VfsLog.Debug(msg);
        throw new ResourceOverwriteException(msg);
      }


      try
      {
        switch(operation)
        {
          case FileOperation.Move:
            Directory.Move(absoluteSource, absoluteDestination);
            break;
          case FileOperation.Copy:
            PathUtil.CopyDirectory(absoluteSource, absoluteDestination, false);
            break;
          default:
            VfsLog.Fatal("Unsupported file operation received: {0}", operation);
            throw new ArgumentOutOfRangeException("operation");
        }

        return GetFolderInfo(absoluteDestination);
      }
      catch (Exception e)
      {
        string msg = String.Format("An error occurred while trying to {0} directory '{1}' to '{2}'.",
                                          operationName, virtualFolderPath, destinationPath);
        VfsLog.Warn(e, msg);
        throw new ResourceAccessException(msg, e);
      }
    }