Пример #1
0
        public void PruneEncryptedDirectory(EncryptOptions options, IOutputHandler outputHandler)
        {
            outputHandler.WriteVerboseLine();
            outputHandler.WriteVerbose("Pruning files");

            int count       = 0;
            var fileNames   = CreateFileList(options.SourceDir, options.IgnoreFilePath);
            var validFiles  = EncryptOperationFactory.Build(options.SourceDir, fileNames, options.BinaryKey[0], options.Levels).Select(x => x.EncFileName);
            var validHashes = new HashSet <string>(validFiles.Select(Path.GetFileName));

            foreach (var file in _fileSource.GetFilesRecursive(options.DestinationDir))
            {
                var fileName = Path.GetFileName(file);
                if (!string.IsNullOrWhiteSpace(fileName) && !validHashes.Contains(fileName))
                {
                    if (count == 0)
                    {
                        outputHandler.WriteVerboseLine();
                    }
                    outputHandler.WriteVerboseLine("\t{0}", fileName);
                    File.Delete(file);
                    count++;
                }
            }

            outputHandler.WriteVerboseLine(count == 0 ? " (no files found)" : "Pruned " + count + " files");
        }
Пример #2
0
        public long PerformEncryption(CommonOptions options, IOutputHandler outputHandler, EncryptOperation o, byte[] key)
        {
            outputHandler.WriteVerbose("\t{0} => ", o.RelFileName);

            var result = FileEncrypter.EncryptFile(options.DestinationDir, o, key);

            if (result.HasResult)
            {
                outputHandler.WriteVerboseLine(o.EncFileName);
                return(result.Result.Item2);
            }

            outputHandler.WriteVerboseOrNormalLine("\t{0} => Failed to encrypt file! ({1})", "Failed to encrypt file! ({1})", o.RelFileName, result.ErrorMessage);
            return(0);
        }
Пример #3
0
        public static long PerformDecryption(CommonOptions options, IOutputHandler outputHandler, string file, byte[] key)
        {
            outputHandler.WriteVerbose("{0} => ", file.Replace(options.SourceDir, "").TrimStart(new[] { Path.DirectorySeparatorChar }));

            var result = FileEncrypter.DecryptFile(options.DestinationDir, file, key);

            if (result.HasResult)
            {
                outputHandler.WriteVerboseLine(result.Result.Item1.Replace(options.DestinationDir, "").TrimStart(new[] { Path.DirectorySeparatorChar }));
                return(result.Result.Item2);
            }

            outputHandler.WriteLine("Failed to decrypt file! ({0})", result.ErrorMessage);
            return(0);
        }
Пример #4
0
        private static void PerformCommand <T>(CommonOptions options, IOutputHandler outputHandler, string action, Func <IList <T> > fileList, Func <CommonOptions, IOutputHandler, T, byte[], long> operation)
        {
            outputHandler.Write("{0} started", action);

            var sw = Stopwatch.StartNew();

            var files = fileList();

            if (files == null || files.Count == 0)
            {
                outputHandler.WriteLine(" (no files found)");
                return;
            }

            outputHandler.WriteVerboseOrNormalLine("", " ({0} files)", files.Count);
            var totalSize = files.Sum(o => operation(options, outputHandler, o, options.BinaryKey));

            sw.Stop();

            if (totalSize != 0)
            {
                if (options.Verbose)
                {
                    var avgSpeed    = "--";
                    var elapsedSecs = sw.ElapsedMilliseconds / 1000;
                    totalSize = totalSize / (1024 * 1024);

                    if (totalSize != 0 && elapsedSecs != 0)
                    {
                        avgSpeed = (totalSize / elapsedSecs).ToString(CultureInfo.InvariantCulture);
                    }

                    outputHandler.WriteVerboseLine("{0} of {1} files complete ({2}MB in {3:hh\\:mm\\:ss}, avg {4}MB/s)", action, files.Count, totalSize, sw.Elapsed, avgSpeed);
                }
                else
                {
                    outputHandler.WriteLine("{0} of {1} files complete", action, files.Count);
                }
            }
        }