protected void assertAreEqual(DirectoryObject dir1, DirectoryObject dir2)
        {
            Assert.IsNotNull(dir1);
            Assert.IsNotNull(dir2);

            Assert.AreEqual<DateTime>(dir1.Created, dir2.Created);
            Assert.AreEqual<DateTime>(dir1.LastEdited, dir2.LastEdited);
            Assert.AreEqual<string>(dir1.Name, dir2.Name);
            Assert.AreEqual<string>(dir1.Path, dir2.Path);
            Assert.IsFalse(dir1.Name.IsNullOrEmpty());

            var dir1Dirs = dir1.Directories.ToList<DirectoryObject>();
            var dir2Dirs = dir2.Directories.ToList<DirectoryObject>();

            Assert.AreEqual<int>(dir1Dirs.Count, dir2Dirs.Count);
            for (int i = 0; i < dir1Dirs.Count; i++)
            {
                assertAreEqual(dir1Dirs[i], dir2Dirs[i]);
            }

            var dir1Files = dir1.Files.ToList<FileObject>();
            var dir2Files = dir2.Files.ToList<FileObject>();

            Assert.AreEqual<int>(dir1Files.Count, dir2Files.Count);

            for (int i = 0; i < dir1Files.Count; i++)
            {
                assertAreEqual(dir1Files[i], dir2Files[i]);
            }
        }
Esempio n. 2
0
        public static DirectoryObjectDiff CompareDirectories(DirectoryObject left, DirectoryObject right, string leftName = "", string rightName = "")
        {
            var result = new DirectoryObjectDiff() { LeftName = leftName, RightName = rightName };


            compareDirectoriesHelper(left, right, result);
            

            return result;
        }
        public void TestDirectoryObjectSerialization()
        {
            var dir = new DirectoryObject() { LocalPath = ".." };
            dir.LoadFromDisk();
            dir.Path = "/";

            var xml = dir.Serialize();

            var roundTrip = new DirectoryObject();
            Assert.IsTrue(roundTrip.Validate(xml));
            roundTrip.Deserialize(xml);

            assertAreEqual(dir, roundTrip);
        }
        private void removeUnpermittedDirs(DirectoryObject dir)
        {
            bool childPermitted = false;
            foreach (DirectoryObject item in dir.Directories.ToList<DirectoryObject>())
            {
                bool itemPermitted = checkPermission(item.Path) || anyChildPermitted(item.Path);

                childPermitted = childPermitted || itemPermitted;

                if (!itemPermitted)
                {
                    dir.RemoveDirectory(item.Name);
                }
                else
                {
                    removeUnpermittedDirs(item);
                }
            }

            if (!checkPermission(dir.Path))
            {
                dir.ClearFiles();
            }
        }
        private DirectoryObject loadVirtualPath(string virtualPath, int depth = -1)
        {
            var localPath = getLocalPath(virtualPath);
            if (localPath.IsNullOrEmpty())
            {
                throw new DirectoryNotFoundException();
            }

            var dir = new DirectoryObject();
            dir.LocalPath = localPath;
            dir.LoadFromDisk(depth);

            //load files and directories from other mounts that match the specified path
            var subMounts = mounts.Where(x => x.MountPoint.ToLower().StartsWith(virtualPath.ToLower()));
            foreach (var item in subMounts)
            {
                var itemMountPoint = item.MountPoint.Remove(0, virtualPath.Length);

                if (itemMountPoint.IsNullOrEmpty())
                    continue;

                if (itemMountPoint.StartsWith("/"))
                    itemMountPoint = itemMountPoint.RemoveFirstChar();

                if (itemMountPoint.Split('/').Length > depth && depth >= 0)
                {
                    continue;
                }

                var subDir = new DirectoryObject();
                subDir.LocalPath = item.LocalPath;

                int scanDepth = (depth < 0) ? -1 : Math.Max(0, (depth - itemMountPoint.Split('/').Length));

                subDir.LoadFromDisk(scanDepth);
                var parent = dir.GetDirectory(Path.GetDirectoryName(itemMountPoint));

                if (parent.Directories.Any(x => x.Name.ToLower() == subDir.Name.ToLower()))
                    parent.RemoveDirectory(subDir.Name);

                parent.AddDirectory(subDir);
            }

            return dir;
        }
Esempio n. 6
0
        private void printDirectoryObject(DirectoryObject dir)
        {
            if (dir == null)
            {
                Program.ErrorLine(this, "DirectoryObject NULL");
                return;
            }

            if (!dir.Directories.Any() && !dir.Files.Any())
            {
                Program.OutputLine(this, "");
                return;
            }

            foreach (DirectoryObject item in dir.Directories)
            {
                Program.OutputLine(this, " <DIR> {0}", item.Name.FormatEscape());
            }

            foreach (FileObject item in dir.Files)
            {
                Program.OutputLine(this, "       {0}", item.Name.FormatEscape());
            }
        }
Esempio n. 7
0
        private static void compareDirectoriesHelper(DirectoryObject left, DirectoryObject right, DirectoryObjectDiff result)
        {

            //      ##  Items missing in left Directory  ##

            //find directories that exist in right but not in left
            var dirsMissingLeft = right.Directories.Where(x => !left.Directories.Any(y => y.Name.ToLower() == x.Name.ToLower()))                                                
                                                   .Select(x => getAllSubdirectories(x))   
                                                   .Aggregate((l1, l2) => l1.Union<DirectoryObject>(l2));

            //find files that exist in right but not in left
            var filesMissingLeft = right.Files.Where(x => !left.Files.Any(y => y.Name.ToLower() == x.Name.ToLower()));
            //add all files from all the directories missing from left
            filesMissingLeft = filesMissingLeft.Union(dirsMissingLeft.Select(x => x.Files).Aggregate((l1, l2) => l1.Union(l2)));

            //copy directories in the list and remove the files
            dirsMissingLeft = dirsMissingLeft.Select(x => { var copy = x.Clone(0); copy.ClearFiles(); return copy; });



            //      ##  Items missing in right Directory    ##  

            //find directories that exist in left but not in right
            var dirsMissingRight = left.Directories.Where(x => !right.Directories.Any(y => y.Name.ToLower() == x.Name.ToLower()))
                                                   .Select(x => getAllSubdirectories(x))
                                                   .Aggregate((l1, l2) => l1.Union<DirectoryObject>(l2));
                                                  

            //find files that exist in left but not in right
            var filesMissingRight = left.Files.Where(x => !right.Files.Any(y => y.Name.ToLower() == x.Name.ToLower()));
            //add all files from all the directories missing from right
            filesMissingRight = filesMissingRight.Union(dirsMissingRight.Select(x => x.Files).Aggregate((l1, l2) => l1.Union(l2)));

            //make dirsMissingLeft (list of trees) to a simple list of directories (minus files)
            dirsMissingRight = dirsMissingRight.Select(x => { var copy = x.Clone(0); copy.ClearFiles(); return copy; });



            //      ##  Conflicts   ##

            //find files that exist in both directories but are not equal
            var fileConflicts = right.Files.Where(x => left.Files.Any(y => y.Name.ToLower() == x.Name.ToLower()))
                                           .Select(x => new Tuple<FileObject, FileObject>(left.GetFile(x.Name), x))
                                           .Where(x => !compareFileObject(x.Item1, x.Item2));


            //add the newy-found conflicts to the existing result
            result.DirectoriesMissigFromLeft = result.DirectoriesMissigFromLeft.Union<DirectoryObject>(dirsMissingLeft).ToList<DirectoryObject>();
            result.DirectoriesMissingFromRight = result.DirectoriesMissingFromRight.Union<DirectoryObject>(dirsMissingRight).ToList<DirectoryObject>();

            result.FilesMissingFromLeft = result.FilesMissingFromLeft.Union<FileObject>(filesMissingLeft);
            result.FilesMissingFromRight = result.FilesMissingFromRight.Union<FileObject>(filesMissingRight);


            //      ##  Recursion   ##

            foreach (var item in left.Directories.Where(x => right.Directories.Any(y => y.Name.ToLower() == x.Name.ToLower())))
            {
                compareDirectoriesHelper(item, right.GetDirectory(item.Name), result);
            }


        }
Esempio n. 8
0
        private static IEnumerable<FileObject> getAllFiles(DirectoryObject dir)
        {
            var dirs = getAllSubdirectories(dir);

            return dirs.Select(x => x.Files).Aggregate((l1, l2) => l1.Union(l2));
        }
Esempio n. 9
0
        private static IEnumerable<DirectoryObject> getAllSubdirectories(DirectoryObject dir)
        {

            var result = new List<DirectoryObject>();
        
            result.Add(dir);

            for (int i = 0; i < result.Count; i++)
            {
                foreach (var item in result[i].Directories)
                {
                    result.Add(item);
                }
            }

            return result;

        }
Esempio n. 10
0
        private static void compareDirectoriesHelper(DirectoryObject left, DirectoryObject right, DirectoryObjectDiff result)
        {
            //      ##  Items missing in left Directory  ##

            //find directories that exist in right but not in left
            var dirsMissingLeft = right.Directories.Where(x => !left.Directories.Any(y => y.Name.ToLower() == x.Name.ToLower()))
                                  .Select(x => getAllSubdirectories(x))
                                  .Aggregate((l1, l2) => l1.Union <DirectoryObject>(l2));

            //find files that exist in right but not in left
            var filesMissingLeft = right.Files.Where(x => !left.Files.Any(y => y.Name.ToLower() == x.Name.ToLower()));

            //add all files from all the directories missing from left
            filesMissingLeft = filesMissingLeft.Union(dirsMissingLeft.Select(x => x.Files).Aggregate((l1, l2) => l1.Union(l2)));

            //copy directories in the list and remove the files
            dirsMissingLeft = dirsMissingLeft.Select(x => { var copy = x.Clone(0); copy.ClearFiles(); return(copy); });



            //      ##  Items missing in right Directory    ##

            //find directories that exist in left but not in right
            var dirsMissingRight = left.Directories.Where(x => !right.Directories.Any(y => y.Name.ToLower() == x.Name.ToLower()))
                                   .Select(x => getAllSubdirectories(x))
                                   .Aggregate((l1, l2) => l1.Union <DirectoryObject>(l2));


            //find files that exist in left but not in right
            var filesMissingRight = left.Files.Where(x => !right.Files.Any(y => y.Name.ToLower() == x.Name.ToLower()));

            //add all files from all the directories missing from right
            filesMissingRight = filesMissingRight.Union(dirsMissingRight.Select(x => x.Files).Aggregate((l1, l2) => l1.Union(l2)));

            //make dirsMissingLeft (list of trees) to a simple list of directories (minus files)
            dirsMissingRight = dirsMissingRight.Select(x => { var copy = x.Clone(0); copy.ClearFiles(); return(copy); });



            //      ##  Conflicts   ##

            //find files that exist in both directories but are not equal
            var fileConflicts = right.Files.Where(x => left.Files.Any(y => y.Name.ToLower() == x.Name.ToLower()))
                                .Select(x => new Tuple <FileObject, FileObject>(left.GetFile(x.Name), x))
                                .Where(x => !compareFileObject(x.Item1, x.Item2));


            //add the newy-found conflicts to the existing result
            result.DirectoriesMissigFromLeft   = result.DirectoriesMissigFromLeft.Union <DirectoryObject>(dirsMissingLeft).ToList <DirectoryObject>();
            result.DirectoriesMissingFromRight = result.DirectoriesMissingFromRight.Union <DirectoryObject>(dirsMissingRight).ToList <DirectoryObject>();

            result.FilesMissingFromLeft  = result.FilesMissingFromLeft.Union <FileObject>(filesMissingLeft);
            result.FilesMissingFromRight = result.FilesMissingFromRight.Union <FileObject>(filesMissingRight);


            //      ##  Recursion   ##

            foreach (var item in left.Directories.Where(x => right.Directories.Any(y => y.Name.ToLower() == x.Name.ToLower())))
            {
                compareDirectoriesHelper(item, right.GetDirectory(item.Name), result);
            }
        }
Esempio n. 11
0
        private static IEnumerable <FileObject> getAllFiles(DirectoryObject dir)
        {
            var dirs = getAllSubdirectories(dir);

            return(dirs.Select(x => x.Files).Aggregate((l1, l2) => l1.Union(l2)));
        }
Esempio n. 12
0
        public virtual void LoadFromDisk(int depth = -1)
        {
            logger.Info("Loading Directory {0}", LocalPath);

            if (!Directory.Exists(this.LocalPath))
                throw new DirectoryNotFoundException(String.Format("Directory '{0}' not found", LocalPath));

            DirectoryInfo info = new DirectoryInfo(LocalPath);

            this.Name = info.Name;
            this.Created = info.CreationTimeUtc;
            this.LastEdited = info.LastWriteTimeUtc;

            //  ##  Update files    ##
            try
            {
                foreach (string item in Directory.GetFiles(LocalPath))
                {
                    string fileName = System.IO.Path.GetFileName(item);

                    if (files.ContainsKey(fileName))
                    {
                        files[fileName].LoadFromDisk();
                    }
                    else
                    {
                        FileObject newFile = new FileObject();
                        newFile.Name = fileName;
                        newFile.LocalPath = item;
                        AddFile(newFile);
                        newFile.LoadFromDisk();
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                files = new Dictionary<string, FileObject>();
            }

            //TODO test if this really works!
            files.Values.Where(x => !File.Exists(LocalPath)).Select(x => files.Remove(x.Name));

            //  ##  Update directories ##
            try
            {
                foreach (string item in Directory.GetDirectories(LocalPath))
                {
                    try
                    {
                        string dirName = ExtendedPath.GetDirectoryName(item);

                        if (!directories.ContainsKey(dirName))
                        {
                            DirectoryObject newDir = new DirectoryObject();

                            newDir.Name = dirName;
                            newDir.LocalPath = item;

                            if (depth > 1 || depth < 0)
                            {
                                newDir.LoadFromDisk(depth - 1);
                            }

                            AddDirectory(newDir);
                        }
                    }
                    catch (IOException)
                    {
                    }
                }
            }
            catch (DirectoryNotFoundException)
            {
                directories = new Dictionary<string, DirectoryObject>();
            }

            directories.Values.Where(x => !Directory.Exists(x.LocalPath)).Select(x => directories.Remove(x.Name));
            directories.Values.Select(x => { x.LoadFromDisk(); return x; });
        }
Esempio n. 13
0
        public override void Deserialize(XElement xmlData)
        {
            base.Deserialize(xmlData);

            files = new Dictionary<string, FileObject>();

            foreach (XElement item in xmlData.Element(XmlNamesExtended.Files).Elements(XmlNamesExtended.FileObject))
            {
                FileObject newFile = new FileObject();
                newFile.Deserialize(item);
                files.Add(newFile.Name.ToLower(), newFile);
            }

            directories = new Dictionary<string, DirectoryObject>();
            foreach (XElement item in xmlData.Element(XmlNamesExtended.Directories).Elements(XmlNamesExtended.DirectoryObject))
            {
                DirectoryObject newDir = new DirectoryObject();
                newDir.Deserialize(item);
                directories.Add(newDir.Name.ToLower(), newDir);
            }
        }
Esempio n. 14
0
        /// <summary>
        /// Clones the directory and all it's files and directoris to a certain depth
        /// </summary>
        /// <param name="depth">Specifies how many levels to go down (items deeper in the hierachy will neither be cloned nor be referenced uncloned)</param>
        /// <returns>Returns a new DirectoryObjects which's member are all copies of this DirectoryObject</returns>
        public DirectoryObject Clone(int depth)
        {
            DirectoryObject copy = new DirectoryObject();

            //Clone ServiceName values
            copy.Created = this.Created;
            copy.LastEdited = this.LastEdited;
            copy.LocalPath = this.LocalPath;
            copy.Name = this.Name;
            copy.Parent = this.Parent;

            //Clone all child directories
            if (depth > 0)
            {
                foreach (DirectoryObject dir in directories.Values)
                {
                    copy.AddDirectory(dir.Clone(depth-1));
                }
            }

            //Clones files in this directory
            foreach (FileObject file in files.Values)
            {
                copy.AddFile(file.Clone());
            }

            return copy;
        }
Esempio n. 15
0
 /// <summary>
 /// Adds a directory to the data-structure
 /// </summary>
 /// <param name="dir"></param>
 public void AddDirectory(DirectoryObject dir)
 {
     if (!files.ContainsKey(dir.Name.ToLower()) && !directories.ContainsKey(dir.Name.ToLower()))
     {
         directories.Add(dir.Name.ToLower(), dir);
         dir.Parent = this;
     }
 }