Пример #1
0
        /// <summary>
        /// Class constructor
        /// </summary>
        private DirectoryDiffRoot()
        {
            _rootEntry      = new DirectoryDiffEntry();
            _DifferentFiles = new DirectoryDiffEntryCollection();

            CountFilesAdded   = 0;
            CountFilesDeleted = 0;
            CountFilesChanged = 0;
        }
Пример #2
0
        /// <summary>
        /// Converts a <see cref="MergedEntry"/> <paramref name="item"/> into a
        /// <see cref="IDirectoryDiffEntry"/> item and returns it.
        /// </summary>
        /// <param name="basePath"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        private IDirectoryDiffEntry ConvertDirEntry(string basePath, MergedEntry item)
        {
            DirectoryDiffEntry newEntry = null;

            DateTime lastUpdateA = default(DateTime);
            DateTime lastUpdateB = default(DateTime);

            if (item.InfoA != null)
            {
                lastUpdateA = item.InfoA.LastWriteTime;
            }

            if (item.InfoB != null)
            {
                lastUpdateB = item.InfoB.LastWriteTime;
            }

            if (item.InfoA != null && item.InfoB != null)
            {
                // The item is in both directories
                if (this._ShowDifferent || this._ShowSame)
                {
                    newEntry = new DirectoryDiffEntry(basePath, item.InfoA.Name, false, true, true,
                                                      lastUpdateA, lastUpdateB);
                }
            }
            else if (item.InfoA != null && item.InfoB == null)
            {
                // The item is only in A
                if (this._ShowOnlyInA)
                {
                    newEntry = new DirectoryDiffEntry(basePath, item.InfoA.Name, false, true, false,
                                                      lastUpdateA, lastUpdateB);
                }
            }
            else
            {
                // The item is only in B
                if (this._ShowOnlyInB)
                {
                    newEntry = new DirectoryDiffEntry(basePath, item.InfoB.Name, false, false, true,
                                                      lastUpdateA, lastUpdateB);
                }
            }

            return(newEntry);
        }
Пример #3
0
        /// <summary>
        /// Compares 2 sets of aligned <see cref="FileSystemInfo"/> objects and returns their
        /// status in terms of difference in the <paramref name="node"/> parameter.
        /// </summary>
        /// <param name="root"></param>
        /// <param name="mergeIndex">Contains the 2 sets of objects to compare in a merged sorted list</param>
        /// <param name="node">Contains the directory base entry and resulting list of files</param>
        /// <param name="checkIfFilesAreDifferent"></param>
        /// <param name="lengthSumA"></param>
        /// <param name="lengthSumB"></param>
        private void DiffFiles(DirectoryDiffRoot root,
                               Merge.MergeIndex mergeIndex,
                               IDirectoryDiffEntry node,
                               bool checkIfFilesAreDifferent,
                               out double lengthSumA, out double lengthSumB)
        {
            lengthSumA = 0;
            lengthSumB = 0;

            foreach (var item in mergeIndex.MergedEntries)
            {
                DateTime lastUpdateA = default(DateTime);
                DateTime lastUpdateB = default(DateTime);
                double   lengthA = 0.0, lengthB = 0.0;

                if (item.InfoA != null)
                {
                    lastUpdateA = item.InfoA.LastWriteTime;

                    try
                    {
                        if (item.InfoA is IFileInfo)
                        {
                            lengthA     = ((IFileInfo)item.InfoA).Length;
                            lengthSumA += lengthA;
                        }
                    }
                    catch
                    {
                        lengthA = 0;
                    }
                }

                if (item.InfoB != null)
                {
                    lastUpdateB = item.InfoB.LastWriteTime;

                    try
                    {
                        if (item.InfoB is IFileInfo)
                        {
                            lengthB     = ((IFileInfo)item.InfoB).Length;
                            lengthSumB += lengthB;
                        }
                    }
                    catch
                    {
                        lengthB = 0;
                    }
                }

                string basePath              = GetBasePath(root.RootPathA, item.InfoA, root.RootPathB, item.InfoB);
                IDirectoryDiffEntry newEntry = null;

                // The item is in both directories
                if (item.InfoA != null && item.InfoB != null)
                {
                    if (_ShowDifferent || _ShowSame)
                    {
                        bool different = false;

                        // Are these different by byte length and/or time stamp already?
                        if ((root.DiffMode & DiffDirFileMode.ByteLength) != 0)
                        {
                            if (Math.Abs(lengthA - lengthB) > _epsilon)
                            {
                                different = true;
                            }
                        }

                        if ((root.DiffMode & DiffDirFileMode.LastUpdate) != 0)
                        {
                            // Precision of TimeStamp depends on backend filesystem
                            // https://superuser.com/questions/937380/get-creation-time-of-file-in-milliseconds
                            if (Math.Abs(Math.Round((lastUpdateA - lastUpdateB).TotalSeconds)) > this._LastUpDatePrecision)
                            {
                                different = true;
                            }
                        }

                        Exception except = null;

                        // Byte by Byte checking is rather slow. Do it only if faster checks have not
                        // determined in-equality and byte-by-byte diff mode is active
                        if (checkIfFilesAreDifferent &&
                            different == false &&
                            (root.DiffMode & DiffDirFileMode.AllBytes) != 0)
                        {
                            try
                            {
                                if (root.Source.AreFilesDifferent(item.InfoA.FullName, item.InfoB.FullName) == true)
                                {
                                    different = true;
                                }
                            }
                            catch (System.IO.IOException ex)
                            {
                                except = ex;
                            }
                            catch (UnauthorizedAccessException ex)
                            {
                                except = ex;
                            }
                        }

                        if ((different && _ShowDifferent) || (!different && _ShowSame))
                        {
                            newEntry = new DirectoryDiffEntry(basePath, item.InfoA.Name, true, true, true,
                                                              lastUpdateA, lastUpdateB, lengthA, lengthB);

                            newEntry.Different = different;

                            if (except != null)
                            {
                                newEntry.Error = string.Format("'{0}' -> '{1}'", except.Message, except.StackTrace);
                            }
                        }
                    }
                }
                else if (item.InfoA != null && item.InfoB == null)
                {
                    // The item is only in A
                    if (this._ShowOnlyInA)
                    {
                        newEntry = new DirectoryDiffEntry(basePath, item.InfoA.Name, true, true, false,
                                                          lastUpdateA, default(DateTime), lengthA, 0.0);
                    }
                }
                else
                {
                    // The item is only in B
                    if (this._ShowOnlyInB)
                    {
                        newEntry = new DirectoryDiffEntry(basePath, item.InfoB.Name, true, false, true,
                                                          default(DateTime), lastUpdateB, 0.0, lengthB);
                    }
                }

                if (newEntry != null)
                {
                    // Mark directory as different if containing files are different
                    if (newEntry.Different == true)
                    {
                        node.Different = true;
                        root.AddDiffFile(newEntry);                           // Add into collection of different files
                    }

                    node.AddSubEntry(newEntry);
                }
            }
        }