Ejemplo n.º 1
0
        /*Update consistency is going to look through current version of its subdirectory
        * then compares the names of every file on the local machines's directory with what it has
        * saved at the location given by currentPath, If there is something new not added, it will add it,
         * if thing has been removed it will remove it*/
        private void updateConsistency(FolderObj workingDir, string currentPath)
        {
            List<FileObj> fileData = new List<FileObj>();
            FileInfo currFile;
            DirectoryInfo currDir;

            foreach (string folders in Directory.GetDirectories(currentPath))
            {
                currDir = new DirectoryInfo(folders);
                fileData.Add(new FolderObj(" ", "", "", 0, currDir.CreationTime.ToString(), currDir.Name, currentPath + currDir.Name + "/", null));
            }
            foreach (string files in Directory.GetFiles(currentPath))
            {
                currFile = new FileInfo(files);
                fileData.Add(new FileObj(currFile.IsReadOnly.ToString(), "", "", (UInt64)currFile.Length, currFile.CreationTime.ToString(), currFile.Name, currentPath));
            }

            //If the directory we are going to is empty, we don't need to do anything, except clear.
            if (fileData == null)
            {
                workingDir.subdirectory.Clear();
                return;
            }
            if (fileData.Count == 0)
            {
                workingDir.subdirectory.Clear();
                return;
            }
            //Sort the two list before performing the algorithm
            fileData.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
            workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
            int i = 0; //marker for currConsistency
            int j = 0; //marker for workingDir

            //The idea with this algorithm is to traverse both list simultaneously and
            //find any disparities
            while (i < fileData.Count && j < workingDir.subdirectory.Count)
            {
                if (string.Compare(fileData[i].fileinfo.name, workingDir.subdirectory[j].fileinfo.name) == 0)
                {
                    //Item exist in both list, so skip it
                    i++;
                    j++;
                }
                else if (string.Compare(fileData[i].fileinfo.name, workingDir.subdirectory[j].fileinfo.name) > 0)
                {
                    //remove working directory's jth entry
                    workingDir.subdirectory.RemoveAt(j);
                    workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                    j++;
                    i++;
                }
                else if (string.Compare(fileData[i].fileinfo.name, workingDir.subdirectory[j].fileinfo.name) < 0)
                {
                    //Add to working directory
                    workingDir.AddToSubDirectory(fileData[i].fileinfo);
                    workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                    j++;
                    i++;
                }
            }
            while (j < workingDir.subdirectory.Count)
            {
                //remove remaining items
                //remove working directory's jth entry
                workingDir.subdirectory.RemoveAt(j);
                workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                j++;
                i++;
            }
            while (i < fileData.Count)
            {
                //Add to working directory
                workingDir.AddToSubDirectory(fileData[i].fileinfo);
                workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                i++;
            }
        }
Ejemplo n.º 2
0
        /*Update consistency is going to look through current version of its subdirectory
        * then compares the names of every file on the server's directory with what it has
        * passed in as a currPath, If there is something new not added, it will add it, if thing
         * has been removed it will remove it*/
        public void updateConsistency(Client client, FolderObj workingDir, string currPath)
        {
            string tempPath = client.currDirectory;
            client.currDirectory = currPath;
            List<string> currConsistency = client.getCurrDetailedDirectory();
            //If the directory we are going to is empty, we don't need to do anything, except clear.
            if (currConsistency == null)
            {
                workingDir.subdirectory.Clear();
                return;
            }
            if (currConsistency.Count == 0)
            {
                workingDir.subdirectory.Clear();
                return;
            }

            List<FileObj.FileInfo> fileData = FileObj.parseFileInfo(currConsistency, client.currDirectory);
            //Sort the two list before performing the algorithm
            fileData.Sort((x, y) => x.name.CompareTo(y.name));
            workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
            int i = 0; //marker for currConsistency
            int j = 0; //marker for workingDir

            //The idea with this algorithm is to traverse both list simultaneously and
            //find any disparities
            while (i < currConsistency.Count && j < workingDir.subdirectory.Count)
            {
                if (string.Compare(fileData[i].name, workingDir.subdirectory[j].fileinfo.name) == 0)
                {
                    //Item exist in both list, so skip it
                    i++;
                    j++;
                }
                else if (string.Compare(fileData[i].name, workingDir.subdirectory[j].fileinfo.name) > 0)
                {
                    //remove working directory's jth entry
                    workingDir.subdirectory.RemoveAt(j);
                    workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                    j++;
                    i++;
                }
                else if (string.Compare(fileData[i].name, workingDir.subdirectory[j].fileinfo.name) < 0)
                {
                    //Add to working directory
                    workingDir.AddToSubDirectory(fileData[i]);
                    workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                    j++;
                    i++;
                }
            }
            while (j < workingDir.subdirectory.Count)
            {
                //remove remaining items
                //remove working directory's jth entry
                workingDir.subdirectory.RemoveAt(j);
                workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                j++;
            }
            while (i < fileData.Count)
            {
                //Add to working directory
                workingDir.AddToSubDirectory(fileData[i]);
                workingDir.subdirectory.Sort((x, y) => x.fileinfo.name.CompareTo(y.fileinfo.name));
                i++;
            }
            client.currDirectory = tempPath;
        }