Exemplo n.º 1
0
 public WallpaperFolderManager(string folderPath)
 {
     _folderPath      = folderPath;
     _checksumFile    = new ChecksumFile(Path.Combine(_folderPath, ".lwr"));
     _masterChecksums = _checksumFile.GetChecksums();
     CreateFolderIfNotExists();
     GenerateFileLists();
 }
Exemplo n.º 2
0
        public static List <ChecksumFile> GetCache(ChecksumType checksumType, string baseDirectory)
        {
            if (!Program.Settings.General.UseCachedResults)
            {
                return(new List <ChecksumFile>());
            }

            string getCacheSql;

            if (checksumType == ChecksumType.MD5)
            {
                getCacheSql = "select FileName, LastWriteUtc, FileLength, MD5 as Checksum from MD5FileCache where FileName like @Directory + '%'";
            }
            else if (checksumType == ChecksumType.SFV)
            {
                getCacheSql = "select FileName, LastWriteUtc, FileLength, CRC32 as Checksum from CRC32FileCache where FileName like @Directory + '%'";
            }
            else if (checksumType == ChecksumType.SHA1)
            {
                getCacheSql = "select FileName, LastWriteUtc, FileLength, SHA1 as Checksum from SHA1FileCache where FileName like @Directory + '%'";
            }
            else
            {
                throw new Exception(string.Format("{0} not implemented", checksumType));
            }

            DataSet ds = new DataSet();

            using (SqlCeCommand cmd = new SqlCeCommand(getCacheSql, Program.GetOpenCacheConnection()))
                using (SqlCeDataAdapter da = new SqlCeDataAdapter(cmd))
                {
                    SqlCeParameter param = new SqlCeParameter("@Directory", SqlDbType.NVarChar);
                    param.Value = baseDirectory;

                    cmd.Parameters.Add(param);

                    da.Fill(ds);
                }

            List <ChecksumFile> files = new List <ChecksumFile>();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ChecksumFile file = new ChecksumFile(null);

                file.FileName          = Convert.ToString(dr["FileName"]);
                file.CacheLastWriteUtc = Convert.ToDateTime(dr["LastWriteUtc"]);
                file.CacheLength       = Convert.ToInt64(dr["FileLength"]);
                file.OriginalChecksum  = Convert.ToString(dr["Checksum"]);

                files.Add(file);
            }

            return(files);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a new object.
        /// </summary>
        public FileSystemPage(WebSite webSite) : base(webSite)
        {
            var runtime = Factory.Singleton.Resolve<IRuntimeEnvironment>().Singleton;
            var defaultSiteRoot = new SiteRoot() {
                Folder = String.Format("{0}{1}", Path.Combine(runtime.ExecutablePath, "Web"), Path.DirectorySeparatorChar),
                Priority = 0,
            };

            var checksumsFileName = Path.Combine(runtime.ExecutablePath, "Checksums.txt");
            if(!File.Exists(checksumsFileName)) {
                throw new FileNotFoundException($"Cannot find {checksumsFileName}");
            }
            defaultSiteRoot.Checksums.AddRange(ChecksumFile.Load(File.ReadAllText(checksumsFileName), enforceContentChecksum: true));

            AddSiteRoot(defaultSiteRoot);
        }
Exemplo n.º 4
0
        private void btnRemoveDuplicates_Click(object sender, EventArgs e)
        {
            var lang = Language.RemoveDuplicatesForm;

            if (lvwDuplicateFiles.SelectedItems.Count != 1)
            {
                return;
            }

            ListViewItem selectedItem = lvwDuplicateFiles.SelectedItems[0];
            ChecksumFile file         = (ChecksumFile)selectedItem.Tag;

            string sourceFullPath = Path.Combine(file.Set.Directory, file.FileName);

            if (!File.Exists(sourceFullPath))
            {
                MessageBox.Show(
                    string.Format(lang.SourceFileDoesNotExist_Message, sourceFullPath),
                    lang.SourceFileDoesNotExist_Title,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                    );
                return;
            }

            string       confirmMessage = string.Format(lang.ConfirmSourceFile_Message, file.FileName);
            DialogResult res            = MessageBox.Show(confirmMessage, lang.ConfirmSourceFile_Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (res != DialogResult.Yes)
            {
                return;
            }

            Cursor.Current = Cursors.WaitCursor;
            IGrouping <string, ChecksumFile> group = (IGrouping <string, ChecksumFile>)lvwGroupings.SelectedItems[0].Tag;
            List <ChecksumFile> groupList          = new List <ChecksumFile>(group);

            for (int i = 0; i < groupList.Count; i++)
            {
                ChecksumFile removeFile = groupList[i];
                if (string.Compare(removeFile.FileName, file.FileName, true) == 0)
                {
                    continue;
                }

                string fullPath = Path.Combine(file.Set.Directory, removeFile.FileName);
                if (File.Exists(fullPath))
                {
                    Program.SafeDelete(fullPath);
                }
                removeFile.IsDeleted = true;
            }
            Cursor.Current = Cursors.Default;

            foreach (ListViewItem badDirItem in lvwBadDirectories.SelectedItems)
            {
                Cursor.Current = Cursors.WaitCursor;
                string dir = badDirItem.Text;

                List <ChecksumFile> deleteFiles = new List <ChecksumFile>();
                foreach (IGrouping <string, ChecksumFile> checkGroup in _dupeSet)
                {
                    int checkGroupCount = checkGroup.Where(p => p.IsDeleted == false).Count();
                    if (checkGroupCount > 1)
                    {
                        int startsWithCount = 0;
                        foreach (ChecksumFile checkFile in checkGroup.Where(p => p.IsDeleted == false))
                        {
                            // see if any files start with dir
                            if (checkFile.FileName.StartsWith(dir))
                            {
                                startsWithCount++;
                            }
                        }

                        // if all files start with 'dir' we can't proceed
                        if (startsWithCount != checkGroupCount)
                        {
                            foreach (ChecksumFile checkFile in checkGroup.Where(p => p.IsDeleted == false))
                            {
                                // see if any files start with dir
                                if (checkFile.FileName.StartsWith(dir))
                                {
                                    deleteFiles.Add(checkFile);
                                }
                            }
                        }
                    }
                }

                if (deleteFiles.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(string.Format(lang.ConfirmDelete_Message, deleteFiles.Count, dir));
                    if (deleteFiles.Count <= 50)
                    {
                        sb.AppendLine();
                        sb.AppendLine(lang.ConfirmDelete_Message_FileListHeader);
                        sb.AppendLine();
                        foreach (ChecksumFile deleteFile in deleteFiles)
                        {
                            sb.AppendLine(deleteFile.FileName);
                        }
                    }

                    Cursor.Current = Cursors.Default;
                    DialogResult res2 = MessageBox.Show(sb.ToString(), lang.ConfirmDelete_Title, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (res2 == DialogResult.Yes)
                    {
                        Cursor.Current = Cursors.WaitCursor;
                        foreach (ChecksumFile removeFile in deleteFiles)
                        {
                            string fullPath = Path.Combine(file.Set.Directory, removeFile.FileName);
                            if (File.Exists(fullPath))
                            {
                                Program.SafeDelete(fullPath);
                            }
                            removeFile.IsDeleted = true;
                        }
                        Cursor.Current = Cursors.Default;
                    }
                }
            }

            Cursor.Current = Cursors.WaitCursor;
            RebuildGroupings();
            Cursor.Current = Cursors.Default;
        }
Exemplo n.º 5
0
        private void lvwDuplicateFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwDuplicateFiles.SelectedItems.Count == 1)
            {
                btnRemoveDuplicates.Enabled = true;
                ListViewItem selectedItem = lvwDuplicateFiles.SelectedItems[0];

                lvwBadDirectories.Items.Clear();
                foreach (ListViewItem item in lvwDuplicateFiles.Items)
                {
                    if (item == selectedItem)
                    {
                        continue;
                    }

                    ChecksumFile file = (ChecksumFile)item.Tag;

                    ListViewGroup group = new ListViewGroup(file.FileName);

                    string dir = Path.GetDirectoryName(file.FileName);
                    while (!string.IsNullOrEmpty(dir))
                    {
                        ListViewItem badDirItem   = new ListViewItem();
                        string       dirWithSlash = dir + @"\";
                        badDirItem.Text  = dirWithSlash;
                        badDirItem.Group = group;

                        if (selectedItem.Text.StartsWith(dirWithSlash))
                        {
                            break;
                        }

                        int dirRemoveCount = 0;
                        foreach (IGrouping <string, ChecksumFile> checkGroup in _dupeSet)
                        {
                            int checkGroupCount = checkGroup.Where(p => p.IsDeleted == false).Count();
                            if (checkGroupCount > 1)
                            {
                                int startsWithCount = 0;
                                foreach (ChecksumFile checkFile in checkGroup.Where(p => p.IsDeleted == false))
                                {
                                    // see if any files start with dir
                                    if (checkFile.FileName.StartsWith(dirWithSlash))
                                    {
                                        startsWithCount++;
                                    }
                                }

                                // if all files start with 'dir' we can't proceed
                                if (startsWithCount != checkGroupCount)
                                {
                                    dirRemoveCount += startsWithCount;
                                }
                            }
                        }
                        badDirItem.SubItems.Add(string.Format("{0:#,0}", dirRemoveCount));

                        lvwBadDirectories.Items.Add(badDirItem);

                        dir = Path.GetDirectoryName(dir);
                    }

                    if (lvwBadDirectories.Items.Count > 0)
                    {
                        int colWidth = lvwBadDirectories.Columns[0].Width;
                        lvwBadDirectories.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
                        if (lvwBadDirectories.Columns[0].Width < colWidth)
                        {
                            lvwBadDirectories.Columns[0].Width = colWidth;
                        }
                    }
                }
            }
            else
            {
                btnRemoveDuplicates.Enabled = false;
            }
        }
Exemplo n.º 6
0
        private void StartValidatingAsync()
        {
            var checksumFile = new ChecksumFile(this.tbChSumFile.Text);
            var entries = checksumFile.Parse().ToArray();
            this.InitProgressSafe(entries.Count());

            this.hashValidator = new HashValidator();
            this.hashValidator.BaseDir = this.tbDir.Text;
            this.hashValidator.DefaultHashType = CryptoUtils.InferHashTypeFromExtension(checksumFile.Ext);
            this.hashValidator.StartProcessingEntry += entry => this.LogThreadSafe(entry.Path + "... ");
            this.hashValidator.EntryProcessed += (result, processed, count) =>
                {
                    this.ShowTotalProgressSafe(processed, count);

                    Color color;
                    switch (result.ResultType)
                    {
                        case EntryResultType.Wrong:
                        case EntryResultType.Aborted:
                            color = Color.Red;
                            break;
                        case EntryResultType.NotFound:
                            color = Color.BlueViolet;
                            break;
                        default:
                            color = Color.Black;
                            break;
                    }

                    this.LogThreadSafe(result.ResultType.ToString() + Environment.NewLine, color);
                };

            this.hashValidator.EntryChunkProcessed += this.ShowEntryProcessedProgressSafe;

            this.checkingThread = new Thread(
                () =>
                    {
                        try
                        {
                            this.hashValidator.ValidateEntries(entries, checksumFile.Ext);

                            if (this.exiting)
                            {
                                return;
                            }

                            // Display final message
                            var endMessage = string.Format(
                                "{0}Correct: {1}, Wrong: {2}, Not Found: {3}, Total: {4}",
                                Environment.NewLine,
                                this.hashValidator.ValidCount,
                                this.hashValidator.WrongCount,
                                this.hashValidator.NotFoundCount,
                                this.hashValidator.EntriesCount);

                            Color color;
                            if (this.hashValidator.WrongCount > 0)
                            {
                                color = Color.Red;
                            }
                            else if (this.hashValidator.NotFoundCount > 0)
                            {
                                color = Color.BlueViolet;
                            }
                            else
                            {
                                color = Color.Black;
                            }

                            this.LogThreadSafe(endMessage, color);
                            this.SetToInitialStateThreadSafe();
                        }
                        catch (Exception ex)
                        {
                            this.ShowErrorThreadSafe(ex);
                        }
                    });

            this.checkingThread.Priority = this.SelectedThreadPriority;
            this.checkingThread.IsBackground = true;
            this.checkingThread.Start();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            var checksumFile = new ChecksumFile();
            var stats        = checksumFile.Stats;

            // Set starting locations
            var roots = new List <string>();

            if (args.Length == 0)
            {
                roots.Add(Directory.GetCurrentDirectory());
            }
            else
            {
                foreach (var arg in args)
                {
                    if (File.Exists(arg) || Directory.Exists(arg))
                    {
                        roots.Add(arg);
                    }
                    else
                    {
                        Console.WriteLine("Root does not exist: " + arg);
                        stats.ArgNotValid.Add(arg);
                    }
                }
            }

            // Get checksums
            var checksumCollections = checksumFile.ChecksumCollections;

            foreach (var root in roots)
            {
                Console.WriteLine("Root: " + root);
                if (Directory.Exists(root))
                {
                    var checksumCollection = new ChecksumCollection(root, RootType.Directory);
                    HashTree(root, checksumCollection, stats);
                    checksumCollections.Add(checksumCollection);
                }
                else if (File.Exists(root))
                {
                    Console.WriteLine("Root is a file.");
                    try
                    {
                        var checksumCollection = new ChecksumCollection(root, RootType.File);
                        checksumCollection.Add(Hasher.HashFile(root));
                        checksumCollections.Add(checksumCollection);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        stats.ChecksumFailure.Add("Unauthorized: " + root);
                    }
                    catch (Exception)
                    {
                        stats.ChecksumFailure.Add("Unknown cause: " + root);
                    }
                }
                else
                {
                    Console.WriteLine("Root disappeared?");
                    stats.RootWentMissing.Add(root);
                }
            }

            // Write stats
            Console.WriteLine($"Stats:");
            Console.WriteLine($"\tInvalid Arguments: {stats.ArgNotValid.Count}");
            foreach (var item in stats.ArgNotValid.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tRoots that disappeared: {stats.RootWentMissing.Count}");
            foreach (var item in stats.RootWentMissing.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tUnauthorized: {stats.Unauthorized.Count}");
            foreach (var item in stats.Unauthorized.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tFiles/Directories that disappeared: {stats.Disappeared.Count}");
            foreach (var item in stats.Disappeared.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tFiles that couldn't be checksummed: {stats.ChecksumFailure.Count}");
            foreach (var item in stats.ChecksumFailure.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"Total files checksummed: {checksumCollections.Select(ci => ci.Checksums.Count).Sum()}");

            // Serialize data
            var serialized = checksumFile.SerializeToUtf8Json();

            // Save to file with random name.
            do
            {
                var outfilename = string.Format(@"{0}.checksums.json", Guid.NewGuid());
                //outfilename = string.Format(@"{0}.checksums", Path.GetRandomFileName());

                try
                {
                    using var outfile = File.Open(outfilename, FileMode.CreateNew, FileAccess.Write);
                    outfile.Write(serialized);
                    Console.WriteLine("ChecksumFile written to: " + Path.GetFullPath(outfilename));
                    break;
                }
                catch (IOException)
                {
                    continue;
                }
            } while (true);
        }