Example #1
0
 public DiffData(DIFF_STATE state, string baseDir, Win32.FIND_DATA src, Win32.FIND_DATA trg)
 {
     this.state   = state;
     this.baseDir = baseDir;
     this.src     = src;
     this.trg     = trg;
 }
Example #2
0
 private Tuple <DIFF_STATE, BOCmp> CrtTup(DIFF_STATE ds, string name, int edt)
 {
     return(new Tuple <DIFF_STATE, BOCmp>(ds, new BOCmp()
     {
         Name = name, Edition = edt
     }));
 }
Example #3
0
        /// <summary>
        /// ATTENZIONE!!!! --- DANGER-ZONE --- MULTI-THREADING AHEAD!!!
        /// </summary>
        public void DiffCallback(DIFF_STATE state, string basedir, ref Win32.FIND_DATA find_data_src, ref Win32.FIND_DATA find_data_trg)
        {
            Win32.FIND_DATA?File_Data_ToUse;
            Win32.FIND_DATA?File_Data_NewDel;
            TextWriter      toWriteTo;

            ProcessDiffState_UpdateCounters(state, ref find_data_src, ref find_data_trg,
                                            out toWriteTo, out File_Data_ToUse, out File_Data_NewDel);

            if (!_writeSameSame && state == DIFF_STATE.SAMESAME)
            {
                return;
            }

            if (toWriteTo == null)
            {
                return;
            }

            string filenameToPrint = (state == DIFF_STATE.NEW) ? find_data_src.cFileName : find_data_trg.cFileName;
            string FullFilename    = Path.Combine(basedir, filenameToPrint);

            if (File_Data_ToUse.HasValue)
            {
                var findData = File_Data_ToUse.Value;
                if (state == DIFF_STATE.MODIFY)
                {
                    WriteModifyLine(toWriteTo, ref find_data_src, ref find_data_trg, FullFilename);
                }
                else
                {
                    WriteFindDataLine(toWriteTo, ref findData, FullFilename);
                }
            }
            else
            {
                toWriteTo.WriteLine($"{FullFilename}");
            }
        }
Example #4
0
        private void ProcessDiffState_UpdateCounters(DIFF_STATE state,
                                                     ref Win32.FIND_DATA find_data_src, ref Win32.FIND_DATA find_data_trg,
                                                     out TextWriter toWriteTo,
                                                     out Win32.FIND_DATA?File_Data_ToUse, out Win32.FIND_DATA?File_Data_NewDel)
        {
            File_Data_NewDel = null;

            switch (state)
            {
            default:
                throw new Exception($"internal error. no such writer for this kind of state. [{state.ToString()}]");

            case DIFF_STATE.SAMESAME:
                if (!Spi.Misc.IsDirectory(find_data_src))
                {
                    Interlocked.Increment(ref _stats.FilesSame);
                    Interlocked.Add(ref _stats.FilesSameBytes, (long)find_data_src.FileSize);
                    toWriteTo       = _writers.sameWriter;
                    File_Data_ToUse = find_data_src;
                }
                else
                {
                    toWriteTo       = null;
                    File_Data_ToUse = null;
                }
                break;

            case DIFF_STATE.NEW:
                if (Spi.Misc.IsDirectory(find_data_src))
                {
                    Interlocked.Increment(ref _stats.DirsNew);
                    toWriteTo       = _writers.newDirWriter;
                    File_Data_ToUse = null;
                }
                else
                {
                    Interlocked.Increment(ref _stats.FilesNew);
                    Interlocked.Add(ref _stats.FilesNewBytes, (long)find_data_src.FileSize);
                    toWriteTo        = _writers.newWriter;
                    File_Data_ToUse  = find_data_src;
                    File_Data_NewDel = find_data_src;
                }

                break;

            case DIFF_STATE.MODIFY:
                toWriteTo = _writers.modWriter;
                Interlocked.Increment(ref _stats.FilesMod);
                Interlocked.Add(ref _stats.FilesModBytes, (long)find_data_trg.FileSize - (long)find_data_src.FileSize);
                File_Data_ToUse = find_data_trg;
                break;

            case DIFF_STATE.DELETE:
                if (Spi.Misc.IsDirectory(find_data_trg))
                {
                    toWriteTo       = _writers.delDirWriter;
                    File_Data_ToUse = null;
                    Interlocked.Increment(ref _stats.DirsDel);
                }
                else
                {
                    toWriteTo = _writers.delWriter;
                    Interlocked.Increment(ref _stats.FilesDel);
                    Interlocked.Add(ref _stats.FilesDelBytes, (long)find_data_trg.FileSize);
                    File_Data_ToUse  = find_data_trg;
                    File_Data_NewDel = find_data_trg;
                }
                break;
            }
        }
Example #5
0
        private static uint _internal_DiffSortedEnumerables <T, K, A, C> (
            IEnumerable <T> ListA,
            IEnumerable <T> ListB,
            Func <T, K> KeySelector,
            Func <K, K, int> KeyComparer,
            Func <T, A> AttributeSelector,
            Func <A, A, int> AttributeComparer,
            Action <DIFF_STATE, T, T, C> OnCompared,
            bool checkSortOrder,
            bool ReportSameSame,
            C context)
        {
            if (KeyComparer == null)
            {
                throw new ArgumentNullException(nameof(KeyComparer));
            }
            if (KeySelector == null)
            {
                throw new ArgumentNullException(nameof(KeySelector));
            }
            if (OnCompared == null)
            {
                throw new ArgumentNullException(nameof(OnCompared));
            }

            using (IEnumerator <T> IterA = ListA.GetEnumerator())
                using (IEnumerator <T> IterB = ListB.GetEnumerator())
                {
                    bool hasMoreA = IterA.MoveNext();
                    bool hasMoreB = IterB.MoveNext();

                    uint CountDifferences = 0;

                    K LastKeyA = default(K);
                    K LastKeyB = default(K);
                    K keyA     = hasMoreA ? KeySelector(IterA.Current) : default(K);
                    K keyB     = hasMoreB ? KeySelector(IterB.Current) : default(K);

                    while (hasMoreA || hasMoreB)
                    {
                        DIFF_STATE DeltaState = DIFF_STATE.SAMESAME;
                        if (hasMoreA && hasMoreB)
                        {
                            DeltaState = ItemCompareFunc(KeyComparer(keyA, keyB), IterA.Current, IterB.Current, AttributeSelector, AttributeComparer);
                            if (DeltaState != DIFF_STATE.SAMESAME || ReportSameSame)
                            {
                                OnCompared(DeltaState, IterA.Current, IterB.Current, context);
                            }
                            LastKeyA = keyA;
                            LastKeyB = keyB;
                        }
                        else if (hasMoreA && !hasMoreB)
                        {
                            DeltaState = DIFF_STATE.DELETE;
                            OnCompared(DeltaState, IterA.Current, default(T), context);
                            LastKeyA = keyA;
                            LastKeyB = default(K);
                        }
                        else if (!hasMoreA && hasMoreB)
                        {
                            DeltaState = DIFF_STATE.NEW;
                            OnCompared(DeltaState, default(T), IterB.Current, context);
                            LastKeyA = default(K);
                            LastKeyB = keyB;
                        }

                        if (DeltaState != DIFF_STATE.SAMESAME)
                        {
                            CountDifferences += 1;
                        }

                        MoveIterators(IterA, IterB, ref hasMoreA, ref hasMoreB, DeltaState);

                        if (hasMoreA)
                        {
                            keyA = KeySelector(IterA.Current);
                        }
                        if (hasMoreB)
                        {
                            keyB = KeySelector(IterB.Current);
                        }
                        if (checkSortOrder)
                        {
                            CheckSortOrderOfItems(KeyComparer, LastKeyA, keyA, 'A');
                            CheckSortOrderOfItems(KeyComparer, LastKeyB, keyB, 'B');
                        }
                    }
                    return(CountDifferences);
                }
        }
Example #6
0
        private static void MoveIterators <T>(IEnumerator <T> IterA, IEnumerator <T> IterB, ref bool hasMoreA, ref bool hasMoreB, DIFF_STATE DeltaState)
        {
            switch (DeltaState)
            {
            case DIFF_STATE.SAMESAME:
            case DIFF_STATE.MODIFY:
                hasMoreA = IterA.MoveNext();
                hasMoreB = IterB.MoveNext();
                break;

            case DIFF_STATE.NEW:
                hasMoreB = IterB.MoveNext();
                break;

            case DIFF_STATE.DELETE:
                hasMoreA = IterA.MoveNext();
                break;
            }
        }
Example #7
0
        private static uint _internal_DiffSortedEnumerables <T, K, A, C> (
            IEnumerable <T> ListA,
            IEnumerable <T> ListB,
            Func <T, K> KeySelector,
            Func <K, K, int> KeyComparer,
            Func <T, A> AttributeSelector,
            Func <A, A, int> AttributeComparer,
            Action <DIFF_STATE, T, T, C> OnCompared,
            bool checkSortOrder,
            C context)
        {
            if (KeyComparer == null)
            {
                throw new ArgumentNullException(nameof(KeyComparer));
            }
            if (KeySelector == null)
            {
                throw new ArgumentNullException(nameof(KeySelector));
            }
            if (AttributeComparer == null)
            {
                throw new ArgumentNullException(nameof(AttributeComparer));
            }
            if (AttributeSelector == null)
            {
                throw new ArgumentNullException(nameof(AttributeSelector));
            }
            if (OnCompared == null)
            {
                throw new ArgumentNullException(nameof(OnCompared));
            }

            using (IEnumerator <T> IterA = ListA.GetEnumerator())
                using (IEnumerator <T> IterB = ListB.GetEnumerator())
                {
                    bool hasMoreA = IterA.MoveNext();
                    bool hasMoreB = IterB.MoveNext();

                    uint CountDifferences = 0;

                    K LastKeyA = default(K);
                    K LastKeyB = default(K);
                    K keyA     = hasMoreA ? KeySelector(IterA.Current) : default(K);
                    K keyB     = hasMoreB ? KeySelector(IterB.Current) : default(K);

                    while (hasMoreA || hasMoreB)
                    {
                        DIFF_STATE DeltaState = DIFF_STATE.SAMESAME;
                        if (hasMoreA && hasMoreB)
                        {
                            DeltaState = ItemCompareFunc(KeyComparer(keyA, keyB), IterA.Current, IterB.Current, AttributeSelector, AttributeComparer);
                            OnCompared(DeltaState, IterA.Current, IterB.Current, context);
                            LastKeyA = keyA;
                            LastKeyB = keyB;
                        }
                        else if (hasMoreA && !hasMoreB)
                        {
                            DeltaState = DIFF_STATE.DELETE;
                            OnCompared(DeltaState, IterA.Current, default(T), context);
                            LastKeyA = keyA;
                            LastKeyB = default(K);
                        }
                        else if (!hasMoreA && hasMoreB)
                        {
                            DeltaState = DIFF_STATE.NEW;
                            OnCompared(DeltaState, default(T), IterB.Current, context);
                            LastKeyA = default(K);
                            LastKeyB = keyB;
                        }

                        if (DeltaState != DIFF_STATE.SAMESAME)
                        {
                            CountDifferences += 1;
                        }
                        // move the iterators based on the diff result
                        switch (DeltaState)
                        {
                        case DIFF_STATE.SAMESAME:
                        case DIFF_STATE.MODIFY:
                            hasMoreA = IterA.MoveNext();
                            hasMoreB = IterB.MoveNext();
                            break;

                        case DIFF_STATE.NEW:
                            hasMoreB = IterB.MoveNext();
                            break;

                        case DIFF_STATE.DELETE:
                            hasMoreA = IterA.MoveNext();
                            break;
                        }
                        if (checkSortOrder)
                        {
                            // check if the sortorder is given and throw an exception if not
                            if (hasMoreA)
                            {
                                keyA = KeySelector(IterA.Current);
                                CheckSortOrderOfItems(KeyComparer, LastKeyA, keyA, 'A');
                            }
                            if (hasMoreB)
                            {
                                keyB = KeySelector(IterB.Current);
                                CheckSortOrderOfItems(KeyComparer, LastKeyB, keyB, 'B');
                            }
                        }
                    }
                    return(CountDifferences);
                }
        }