Beispiel #1
0
        public async Task GetHashOfEmptyAsync()
        {
            var hashF   = new XXHash64();
            var hashing = new HashingService(hashF);
            var stream  = new MemoryStream(new byte[] { });
            var hash    = await hashing.GetFileHash(stream, CancellationToken.None);

            hash.Should().Be(hashF.HashOfEmpty);
        }
Beispiel #2
0
        private static void AddCheck(this CommandLineApplication app)
        {
            app.Command("check", command =>
            {
                command.Description =
                    "Checks that every file in the folder is named as its hash and deletes if it's not";
                command.HelpOption("-?|-h|--help");
                var pathArg = command.Argument("[path]", "Path to folder to check");

                command.OnExecute(async() =>
                {
                    var pathStr = pathArg.Value ?? "./hashed";
                    var path    = Path.Combine(Directory.GetCurrentDirectory(), pathStr);

                    var hashing = new HashingService(new XXHash64());

                    foreach (var file in hashing.GetFiles(new DirectoryInfo(path)).OrderBy(f => f.Length))
                    {
                        if (file.Length <= 0)
                        {
                            file.Delete();
                            continue;
                        }

                        var size         = file.Length.Bytes();
                        var hashFromName = Path.GetFileNameWithoutExtension(file.FullName);
                        Console.Write($"{size.Humanize("#").PadLeft(10)}: {hashFromName} | ");
                        HashValue hash;
                        using (var src = file.OpenRead())
                        {
                            var start = DateTime.Now;
                            hash      = await hashing.GetFileHash(src, CancellationToken.None);
                            var speed = size.Per(DateTime.Now - start);
                            Console.Write($"{hash} | {speed.Humanize("#").PadLeft(10)} | ");
                        }

                        if (hash.ToString() != hashFromName)
                        {
                            Console.Write("DELETE");
                            file.Delete();
                        }

                        Console.WriteLine();
                    }

                    return(0);
                });
            });
        }
Beispiel #3
0
        private static void AddColCheck(this CommandLineApplication app)
        {
            app.Command("collcheck", command =>
            {
                command.Description = "Checks files for collisions";
                command.HelpOption("-?|-h|--help");
                var pathArg = command.Argument("[path]", "Path to folder");

                command.OnExecute(() =>
                {
                    var pathStr          = pathArg.Value ?? ".";
                    var path             = Path.Combine(Directory.GetCurrentDirectory(), pathStr);
                    IHashFunction xxhash = new XXHash64();
                    var hashing          = new HashingService(xxhash);
                    hashing.GetFiles(new DirectoryInfo(path))
                    .Select(f =>
                    {
                        var hash = hashing.GetFileHash(f, CancellationToken.None).GetAwaiter().GetResult();
                        Console.WriteLine($"{hash} {Path.GetRelativePath(path, f.FullName)}");
                        return(new { f, hash });
                    })
                    .GroupBy(a => a.hash.ToString())
                    .Where(g => g.Count() > 1)
                    .ForEach(g =>
                    {
                        Console.WriteLine(g.Key);
                        foreach (var i in g)
                        {
                            Console.WriteLine($"    {i.f.FullName}");
                        }
                        Console.WriteLine();
                    });

                    Console.WriteLine("Done.");
                    return(0);
                });
            });
        }
Beispiel #4
0
        private static void AddImport(this CommandLineApplication app)
        {
            app.Command("import", command =>
            {
                command.Description = "Copies every file in the folder and renames it to its hash";
                command.HelpOption("-?|-h|--help");
                var pathArg     = command.Argument("[path]", "Path to folder to look for files in");
                var pathDestArg = command.Argument("[dest path]", "Path in which to copy the files");

                command.OnExecute(async() =>
                {
                    var pathStr     = pathArg.Value ?? ".";
                    var pathDestStr = pathDestArg.Value ?? "./hashed";
                    var path        = Path.Combine(Directory.GetCurrentDirectory(), pathStr);
                    var pathDest    = Path.Combine(Directory.GetCurrentDirectory(), pathDestStr);

                    var hashing = new HashingService(new XXHash64());

                    foreach (var file in hashing.GetFiles(new DirectoryInfo(path)))
                    {
                        if (file.Length <= 0)
                        {
                            continue;
                        }
                        var size = file.Length.Bytes();
                        Console.Write($"{size.Humanize("#").PadLeft(10)}: ");
                        using (var src = file.OpenRead())
                        {
                            var start    = DateTime.Now;
                            var hash     = await hashing.GetFileHash(src, CancellationToken.None);
                            var filehash = new FileWithHash(file, hash);
                            var speed    = size.Per(DateTime.Now - start);
                            Console.Write($"{hash} {speed.Humanize("#").PadLeft(10)}(hash) | ");
                            var fileDest = Path.Combine(pathDest, filehash.Hash.ToString());
                            try
                            {
                                if (new FileInfo(fileDest).Exists)
                                {
                                    Console.Write($"                 | {file.FullName}");
                                }
                                else
                                {
                                    using (var dest = new FileStream(fileDest, FileMode.Create, FileAccess.Write))
                                    {
                                        start        = DateTime.Now;
                                        src.Position = 0;
                                        await src.CopyToAsync(dest);
                                        Console.Write(
                                            $"{size.Per(DateTime.Now - start).Humanize("#").PadLeft(10)}(copy) | {file.FullName}");
                                    }
                                }
                            }
                            catch (IOException ex)
                            {
                                Console.WriteLine();
                                Console.WriteLine(ex.ToString());
                            }
                        }

                        Console.WriteLine();
                    }

                    return(0);
                });
            });
        }