/// <summary>
 /// Enumerate all files listed as active, checking for status changes and take appropriate actions such as updating status
 /// in the FileSystemState, re-encrypting or deleting temporary plaintext copies.
 /// </summary>
 /// <param name="fileSystemState">The FileSystemState to enumerate and possibly update.</param>
 /// <param name="mode">Under what circumstances is the FileSystemState.Changed event raised.</param>
 /// <param name="progress">The ProgressContext to provide visual progress feedback via.</param>
 public static void CheckActiveFiles(this FileSystemState fileSystemState, ChangedEventMode mode, ProgressContext progress)
 {
     progress.NotifyLevelStart();
     progress.AddTotal(fileSystemState.ActiveFileCount);
     fileSystemState.ForEach(mode, (ActiveFile activeFile) =>
     {
         try
         {
             if (FileLock.IsLocked(activeFile.DecryptedFileInfo, activeFile.EncryptedFileInfo))
             {
                 return activeFile;
             }
             if (OS.Current.UtcNow - activeFile.LastActivityTimeUtc <= new TimeSpan(0, 0, 5))
             {
                 return activeFile;
             }
             activeFile = fileSystemState.CheckActiveFileActions(activeFile, progress);
             return activeFile;
         }
         finally
         {
             progress.AddCount(1);
         }
     });
     progress.NotifyLevelFinished();
 }
Exemplo n.º 2
0
        public static void TestAddCountAfterFinished()
        {
            ProgressContext progress = new ProgressContext();
            progress.NotifyLevelStart();
            progress.NotifyLevelFinished();

            Assert.Throws<InvalidOperationException>(() => { progress.AddCount(1); });
        }
Exemplo n.º 3
0
        public static void TestAddingNegativeCount()
        {
            FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current;
            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(1000);

            ProgressContext progress = new ProgressContext(TimeSpan.FromMilliseconds(1000));
            int percent = 0;
            progress.Progressing += (object sender, ProgressEventArgs e) =>
            {
                percent = e.Percent;
            };
            progress.AddTotal(2);
            Assert.That(percent, Is.EqualTo(0), "No progress yet - should be zero.");
            progress.AddCount(-100);
            Assert.That(percent, Is.EqualTo(0), "Nothing should happen adding negative counts.");
            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(2000);
            progress.AddCount(1);
            Assert.That(percent, Is.EqualTo(50), "1 of 2 is 50 percent.");
        }
 public static void TestProgress()
 {
     ProgressContext progress = new ProgressContext(TimeSpan.Zero);
     progress.AddTotal(100);
     bool wasHere = false;
     progress.Progressing += (object sender, ProgressEventArgs e) =>
     {
         wasHere = true;
     };
     progress.AddCount(1);
     Assert.That(wasHere, "The event should be raised.");
 }
Exemplo n.º 5
0
 public static void TestCancel()
 {
     ProgressContext progress = new ProgressContext();
     progress.NotifyLevelStart();
     progress.AddTotal(100);
     progress.AddCount(10);
     progress.Cancel = true;
     Assert.Throws<OperationCanceledException>(() => { progress.NotifyLevelStart(); });
     Assert.Throws<OperationCanceledException>(() => { progress.NotifyLevelFinished(); });
     Assert.Throws<OperationCanceledException>(() => { progress.AddTotal(50); });
     Assert.Throws<OperationCanceledException>(() => { progress.AddCount(10); });
 }
Exemplo n.º 6
0
 public static void TestSeveralCallsToCount()
 {
     ProgressContext progress = new ProgressContext(TimeSpan.Zero);
     int percent = -1;
     progress.Progressing += (object sender, ProgressEventArgs e) =>
     {
         percent = e.Percent;
     };
     FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current;
     progress.AddTotal(500);
     progress.AddCount(50);
     progress.AddCount(50);
     fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(1);
     progress.AddCount(50);
     progress.AddCount(50);
     fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(2);
     progress.AddCount(50);
     fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromSeconds(2);
     Assert.That(percent, Is.EqualTo(50), "When halfway, the percent should be 50.");
 }
Exemplo n.º 7
0
        public static void TestProgressTo100AndAboveShouldOnlyReturn99BeforeFinishedPercent()
        {
            FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current;
            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(1000);

            ProgressContext progress = new ProgressContext(TimeSpan.FromMilliseconds(1000));
            progress.NotifyLevelStart();
            int percent = 0;
            progress.Progressing += (object sender, ProgressEventArgs e) =>
                {
                    percent = e.Percent;
                };
            progress.AddTotal(2);
            Assert.That(percent, Is.EqualTo(0), "No progress yet - should be zero.");
            progress.AddCount(1);
            Assert.That(percent, Is.EqualTo(50), "Halfway should be 50 percent.");
            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(2000);
            progress.AddCount(1);
            Assert.That(percent, Is.EqualTo(99), "Even at 100 should report 99 percent.");
            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(3000);
            progress.AddCount(1000);
            Assert.That(percent, Is.EqualTo(99), "Even at very much above 100 should report 99 percent.");
            progress.NotifyLevelFinished();
            Assert.That(percent, Is.EqualTo(100), "Only when NotifyFinished() is called should 100 percent be reported.");
        }
Exemplo n.º 8
0
 public static void TestProgressNoMax()
 {
     ProgressContext progress = new ProgressContext(TimeSpan.Zero);
     int percent = -1;
     progress.Progressing += (object sender, ProgressEventArgs e) =>
     {
         percent = e.Percent;
     };
     progress.AddCount(100);
     Assert.That(percent, Is.EqualTo(0), "Since there is no Total set, the percentage should always be zero.");
 }
Exemplo n.º 9
0
 public static void TestPercent()
 {
     ProgressContext progress = new ProgressContext(TimeSpan.Zero);
     int percent = -1;
     progress.Progressing += (object sender, ProgressEventArgs e) =>
     {
         percent = e.Percent;
     };
     progress.AddTotal(200);
     progress.AddCount(100);
     Assert.That(percent, Is.EqualTo(50), "When halfway, the percent should be 50.");
 }
Exemplo n.º 10
0
 public static void TestMultipleAddTotal()
 {
     int percent = 0;
     ProgressContext progress = new ProgressContext(TimeSpan.Zero);
     progress.Progressing += (object sender, ProgressEventArgs e) =>
     {
         percent = e.Percent;
     };
     progress.AddTotal(50);
     progress.AddTotal(50);
     progress.AddCount(50);
     Assert.That(percent, Is.EqualTo(50), "The total should be 100, so 50 is 50% and the progressing event should be raised at the first progress.");
 }
Exemplo n.º 11
0
        public static void TestFirstDelay()
        {
            FakeRuntimeEnvironment fakeEnvironment = (FakeRuntimeEnvironment)OS.Current;
            ProgressContext progress = new ProgressContext(TimeSpan.FromMilliseconds(13));
            bool wasHere = false;
            progress.Progressing += (object sender, ProgressEventArgs e) =>
            {
                wasHere = true;
            };
            progress.AddTotal(100);
            progress.AddCount(50);
            Assert.That(wasHere, Is.False, "No progress should be raised, since the first delay time has not elapsed as yet.");

            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(12);
            progress.AddCount(1);
            Assert.That(wasHere, Is.False, "No progress should be raised, since the first delay time has not elapsed as yet.");

            fakeEnvironment.CurrentTiming.CurrentTiming = TimeSpan.FromMilliseconds(13);
            progress.AddCount(1);
            Assert.That(wasHere, Is.True, "Progress should be raised, since the first delay time has now elapsed.");
        }
Exemplo n.º 12
0
        private static long CopyToWithCount(Stream inputStream, Stream outputStream, Stream realInputStream, ProgressContext progress)
        {
            progress.NotifyLevelStart();
            if (realInputStream.CanSeek)
            {
                progress.AddTotal(realInputStream.Length - realInputStream.Position);
            }

            long totalCount = 0;
            byte[] buffer = new byte[OS.Current.StreamBufferSize];
            int offset = 0;
            int length = buffer.Length;
            while (true)
            {
                int count = inputStream.Read(buffer, offset, length);
                offset += count;
                length -= count;
                if (length > 0 && count > 0)
                {
                    continue;
                }
                if (offset == 0)
                {
                    break;
                }
                outputStream.Write(buffer, 0, offset);
                outputStream.Flush();
                progress.AddCount(offset);
                totalCount += offset;
                offset = 0;
                length = buffer.Length;
            }
            progress.NotifyLevelFinished();
            return totalCount;
        }
 public static void CheckWatchedFolders(this FileSystemState fileSystemState, ProgressContext progress)
 {
     if (fileSystemState == null)
     {
         throw new ArgumentNullException("fileSystemState");
     }
     AesKey encryptionKey = fileSystemState.KnownKeys.DefaultEncryptionKey;
     if (encryptionKey == null)
     {
         return;
     }
     IEnumerable<IRuntimeFileInfo> files = fileSystemState.DecryptedFilesInWatchedFolders();
     progress.NotifyLevelStart();
     try
     {
         progress.AddTotal(files.Count());
         foreach (IRuntimeFileInfo fileInfo in files)
         {
             IRuntimeFileInfo destinationFileInfo = fileInfo.CreateEncryptedName();
             AxCryptFile.EncryptFileWithBackupAndWipe(fileInfo, destinationFileInfo, encryptionKey, progress);
             progress.AddCount(1);
         }
     }
     finally
     {
         progress.NotifyLevelFinished();
     }
 }
 public static void RemoveRecentFiles(this FileSystemState fileSystemState, IEnumerable<string> encryptedPaths, ProgressContext progress)
 {
     progress.NotifyLevelStart();
     progress.AddTotal(encryptedPaths.Count());
     foreach (string encryptedPath in encryptedPaths)
     {
         ActiveFile activeFile = fileSystemState.FindEncryptedPath(encryptedPath);
         if (activeFile != null)
         {
             fileSystemState.Remove(activeFile);
         }
         progress.AddCount(1);
     }
     fileSystemState.Save();
     progress.NotifyLevelFinished();
 }
Exemplo n.º 15
0
        public static void Wipe(IRuntimeFileInfo fileInfo, ProgressContext progress)
        {
            if (fileInfo == null)
            {
                throw new ArgumentNullException("fileInfo");
            }
            if (!fileInfo.Exists)
            {
                return;
            }
            if (OS.Log.IsInfoEnabled)
            {
                OS.Log.LogInfo("Wiping '{0}'.".InvariantFormat(fileInfo.Name));
            }
            bool cancelPending = false;
            progress.NotifyLevelStart();
            using (Stream stream = fileInfo.OpenWrite())
            {
                long length = stream.Length + OS.Current.StreamBufferSize - stream.Length % OS.Current.StreamBufferSize;
                progress.AddTotal(length);
                for (long position = 0; position < length; position += OS.Current.StreamBufferSize)
                {
                    byte[] random = OS.Current.GetRandomBytes(OS.Current.StreamBufferSize);
                    stream.Write(random, 0, random.Length);
                    stream.Flush();
                    try
                    {
                        progress.AddCount(random.Length);
                    }
                    catch (OperationCanceledException)
                    {
                        cancelPending = true;
                        progress.Cancel = false;
                        progress.AddCount(random.Length);
                    }
                }
            }
            string randomName;
            do
            {
                randomName = GenerateRandomFileName(fileInfo.FullName);
            } while (OS.Current.FileInfo(randomName).Exists);

            IRuntimeFileInfo moveToFileInfo = OS.Current.FileInfo(fileInfo.FullName);
            moveToFileInfo.MoveTo(randomName);
            moveToFileInfo.Delete();
            progress.NotifyLevelFinished();
            if (cancelPending)
            {
                progress.Cancel = true;
                throw new OperationCanceledException("Delayed cancel during wipe.");
            }
        }