protected void DeleteOrRecycleFolder(DirectoryInfo di) { if (di == null) { return; } if (_tidyup.DeleteEmptyIsRecycle) { Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory(di.FullName, Microsoft.VisualBasic.FileIO.UIOption.OnlyErrorDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin); } else { di.Delete(); } }
private void DumpRefresh(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string tempPathSysIo = Path.GetTempPath("DirectoryInfo.Refresh()-directory-SysIo-" + Path.GetRandomFileName()); string tempPath = Path.GetTempPath("DirectoryInfo.Refresh()-directory-AlphaFS-" + Path.GetRandomFileName()); if (!isLocal) { tempPathSysIo = Path.LocalToUnc(tempPathSysIo); } if (!isLocal) { tempPath = Path.LocalToUnc(tempPath); } Console.WriteLine("\nInput Directory Path: [{0}]", tempPath); #endregion // Setup #region Refresh try { System.IO.DirectoryInfo diSysIo = new System.IO.DirectoryInfo(tempPathSysIo); DirectoryInfo di = new DirectoryInfo(tempPath); bool existsSysIo = diSysIo.Exists; bool exists = di.Exists; Console.WriteLine("\nnew DirectoryInfo(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); diSysIo.Create(); di.Create(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Create(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); diSysIo.Refresh(); di.Refresh(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Refresh(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); diSysIo.Delete(); di.Delete(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Delete(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); diSysIo.Refresh(); di.Refresh(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Refresh(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); } finally { if (Directory.Exists(tempPathSysIo)) { Directory.Delete(tempPathSysIo); Assert.IsFalse(Directory.Exists(tempPathSysIo), "Cleanup failed: Directory should have been removed."); } if (Directory.Exists(tempPath)) { Directory.Delete(tempPath); Assert.IsFalse(Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed."); } Console.WriteLine(); } #endregion // Refresh }
static void CopyWebsiteToTempPath(Path websitePath, Path tempPath) { var originalWebsiteDirectory = new DirectoryInfo(websitePath.FullName); var tempDirectory = new DirectoryInfo(tempPath.FullName); if (tempDirectory.Exists) { Console.WriteLine("Deleting temp directory {0}", tempDirectory.FullName); tempDirectory.Delete(true); } tempDirectory.Refresh(); Console.WriteLine("Creating temp directory {0}", tempDirectory.FullName); tempDirectory.Create(); var bannedExtensionList = new List<string> { ".user", ".cs", ".csproj", ".dotSettings", ".suo", ".xproj", ".targets", ".nuspec", ".orig", ".ncrunchproject" }; var bannedFiles = new List<string> { "packages.config", "project.json", "project.lock.json", "config.json", "bower.json", "package.json", "gruntfile.json", "Microsoft.CodeAnalysis.Analyzers.dll", "Microsoft.CodeAnalysis.VisualBasic.dll", "Microsoft.Build.Tasks.CodeAnalysis.dll", "VBCSCompiler.exe", "web.debug.config", "web.release.config" }; var bannedDirectories = new List<string> {"obj", "node_modules", "bower_components"}; Predicate<FileInfo> bannedExtensions = file => bannedExtensionList.Any( extension => extension.Equals(file.Extension, StringComparison.InvariantCultureIgnoreCase)); Predicate<FileInfo> bannedFileNames = file => bannedFiles.Any( bannedFile => bannedFile.Equals(file.Name, StringComparison.InvariantCultureIgnoreCase)); IEnumerable<Predicate<FileInfo>> filesToExclude = new List<Predicate<FileInfo>> { bannedExtensions, bannedFileNames }; int itemsCopied = originalWebsiteDirectory.CopyTo(tempDirectory, filesToExclude: filesToExclude, directoriesToExclude: bannedDirectories); Console.WriteLine("Copied {0} items", itemsCopied); }
private void DumpRefresh(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string tempPathSysIo = Path.GetTempPath("DirectoryInfo.Refresh()-directory-SysIo-" + Path.GetRandomFileName()); string tempPath = Path.GetTempPath("DirectoryInfo.Refresh()-directory-AlphaFS-" + Path.GetRandomFileName()); if (!isLocal) tempPathSysIo = Path.LocalToUnc(tempPathSysIo); if (!isLocal) tempPath = Path.LocalToUnc(tempPath); Console.WriteLine("\nInput Directory Path: [{0}]", tempPath); #endregion // Setup #region Refresh try { System.IO.DirectoryInfo diSysIo = new System.IO.DirectoryInfo(tempPathSysIo); DirectoryInfo di = new DirectoryInfo(tempPath); bool existsSysIo = diSysIo.Exists; bool exists = di.Exists; Console.WriteLine("\nnew DirectoryInfo(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); diSysIo.Create(); di.Create(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Create(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); diSysIo.Refresh(); di.Refresh(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Refresh(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); diSysIo.Delete(); di.Delete(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Delete(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // true Assert.AreEqual(existsSysIo, exists); diSysIo.Refresh(); di.Refresh(); existsSysIo = diSysIo.Exists; exists = di.Exists; Console.WriteLine("\ndi.Refresh(): Exists (Should be {0}): [{1}]", existsSysIo, exists); // false Assert.AreEqual(existsSysIo, exists); } finally { if (Directory.Exists(tempPathSysIo)) { Directory.Delete(tempPathSysIo); Assert.IsFalse(Directory.Exists(tempPathSysIo), "Cleanup failed: Directory should have been removed."); } if (Directory.Exists(tempPath)) { Directory.Delete(tempPath); Assert.IsFalse(Directory.Exists(tempPath), "Cleanup failed: Directory should have been removed."); } Console.WriteLine(); } #endregion // Refresh }
public void SetAccessControl() { Console.WriteLine("Directory.SetAccessControl()"); //string path = SysDrive + @"\AlphaDirectory-" + Path.GetRandomFileName(); string path = Path.Combine(Path.GetTempPath(), "Directory.GetAccessControl()-" + Path.GetRandomFileName()); string pathAlpha = path; Directory.CreateDirectory(path); Console.WriteLine("\n\tDirectory: [{0}]", path); // Initial read. Console.WriteLine("\n\tInitial read."); DirectorySecurity dsAlpha = Directory.GetAccessControl(pathAlpha, AccessControlSections.Access); DirectorySecurity dsSystem = System.IO.Directory.GetAccessControl(path, AccessControlSections.Access); AuthorizationRuleCollection accessRulesSystem = dsSystem.GetAccessRules(true, true, typeof(NTAccount)); StopWatcher(true); AuthorizationRuleCollection accessRulesAlpha = dsAlpha.GetAccessRules(true, true, typeof(NTAccount)); Console.WriteLine("\t\tDirectory.GetAccessControl() rules found: [{0}]\n\t{1}", accessRulesAlpha.Count, Reporter()); Assert.AreEqual(accessRulesSystem.Count, accessRulesAlpha.Count); // Sanity check. DumpAccessRules(1, dsSystem, dsAlpha); // Remove inherited properties. // Passing true for first parameter protects the new permission from inheritance, and second parameter removes the existing inherited permissions. Console.WriteLine("\n\tRemove inherited properties and persist it."); dsAlpha.SetAccessRuleProtection(true, false); // Re-read, using instance methods. System.IO.DirectoryInfo diSystem = new System.IO.DirectoryInfo(Path.LocalToUnc(path)); DirectoryInfo diAlpha = new DirectoryInfo(Path.LocalToUnc(path)); dsSystem = diSystem.GetAccessControl(AccessControlSections.Access); dsAlpha = diAlpha.GetAccessControl(AccessControlSections.Access); // Sanity check. DumpAccessRules(2, dsSystem, dsAlpha); // Restore inherited properties. Console.WriteLine("\n\tRestore inherited properties and persist it."); dsAlpha.SetAccessRuleProtection(false, true); // Re-read. dsSystem = System.IO.Directory.GetAccessControl(path, AccessControlSections.Access); dsAlpha = Directory.GetAccessControl(pathAlpha, AccessControlSections.Access); // Sanity check. DumpAccessRules(3, dsSystem, dsAlpha); diAlpha.Delete(); diAlpha.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(diAlpha.Exists); }
private void DumpGetXxxTime(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string path = isLocal ? SysRoot32 : Path.LocalToUnc(SysRoot32); Console.WriteLine("\nInput Directory Path: [{0}]\n", path); #endregion // Setup StopWatcher(true); #region GetCreationTimeXxx DateTime actual = Directory.GetCreationTime(path); DateTime expected = System.IO.Directory.GetCreationTime(path); Console.WriteLine("\tGetCreationTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "GetCreationTime()"); actual = Directory.GetCreationTimeUtc(path); expected = System.IO.Directory.GetCreationTimeUtc(path); Console.WriteLine("\tGetCreationTimeUtc() : [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "GetCreationTimeUtc()"); #endregion // GetCreationTimeXxx #region GetLastAccessTimeXxx actual = Directory.GetLastAccessTime(path); expected = System.IO.Directory.GetLastAccessTime(path); Console.WriteLine("\tGetLastAccessTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "GetLastAccessTime()"); actual = Directory.GetLastAccessTimeUtc(path); expected = System.IO.Directory.GetLastAccessTimeUtc(path); Console.WriteLine("\tGetLastAccessTimeUtc(): [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "GetLastAccessTimeUtc()"); #endregion // GetLastAccessTimeXxx #region GetLastWriteTimeXxx actual = Directory.GetLastWriteTime(path); expected = System.IO.Directory.GetLastWriteTime(path); Console.WriteLine("\tGetLastWriteTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "GetLastWriteTime()"); actual = Directory.GetLastWriteTimeUtc(path); expected = System.IO.Directory.GetLastWriteTimeUtc(path); Console.WriteLine("\tGetLastWriteTimeUtc() : [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "GetLastWriteTimeUtc()"); #endregion // GetLastWriteTimeXxx #region GetChangeTimeXxx Console.WriteLine("\tGetChangeTime() : [{0}] System.IO: [N/A]", Directory.GetChangeTime(path)); Console.WriteLine("\tGetChangeTimeUtc() : [{0}] System.IO: [N/A]", Directory.GetChangeTimeUtc(path)); #endregion GetChangeTimeXxx Console.WriteLine(); Console.WriteLine(Reporter()); Console.WriteLine(); #region Trigger GetChangeTimeXxx // We can not compare ChangeTime against .NET because it does not exist. // Creating a directory and renaming it triggers ChangeTime, so test for that. path = Path.GetTempPath("Directory-GetChangeTimeXxx()-directory-" + Path.GetRandomFileName()); if (!isLocal) path = Path.LocalToUnc(path); DirectoryInfo di = new DirectoryInfo(path); di.Create(); string fileName = di.Name; DateTime lastAccessTimeActual = Directory.GetLastAccessTime(path); DateTime lastAccessTimeUtcActual = Directory.GetLastAccessTimeUtc(path); DateTime changeTimeActual = Directory.GetChangeTime(path); DateTime changeTimeUtcActual = Directory.GetChangeTimeUtc(path); Console.WriteLine("\nTesting ChangeTime on a temp directory."); Console.WriteLine("\nInput Directory Path: [{0}]\n", path); Console.WriteLine("\tGetChangeTime() : [{0}]\t", changeTimeActual); Console.WriteLine("\tGetChangeTimeUtc() : [{0}]\t", changeTimeUtcActual); di.MoveTo(di.FullName.Replace(fileName, fileName + "-Renamed")); // Pause for at least a second so that the difference in time can be seen. int sleep = new Random().Next(2000, 4000); Thread.Sleep(sleep); di.MoveTo(di.FullName.Replace(fileName + "-Renamed", fileName)); DateTime lastAccessTimeExpected = Directory.GetLastAccessTime(path); DateTime lastAccessTimeUtcExpected = Directory.GetLastAccessTimeUtc(path); DateTime changeTimeExpected = Directory.GetChangeTime(path); DateTime changeTimeUtcExpected = Directory.GetChangeTimeUtc(path); Console.WriteLine("\nTrigger ChangeTime by renaming the directory."); Console.WriteLine("For Unit Test, ChangeTime should differ approximately: [{0}] seconds.\n", sleep / 1000); Console.WriteLine("\tGetChangeTime() : [{0}]\t", changeTimeExpected); Console.WriteLine("\tGetChangeTimeUtc() : [{0}]\t\n", changeTimeUtcExpected); Assert.AreNotEqual(changeTimeActual, changeTimeExpected); Assert.AreNotEqual(changeTimeUtcActual, changeTimeUtcExpected); Assert.AreEqual(lastAccessTimeExpected, lastAccessTimeActual); Assert.AreEqual(lastAccessTimeUtcExpected, lastAccessTimeUtcActual); #endregion // Trigger GetChangeTimeXxx di.Delete(); di.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(di.Exists, "Cleanup failed: Directory should have been removed."); Console.WriteLine(); }
private void TransferItem(USNJournalSyncLog syncLog, SyncMountpoint syncFrom) { try { syncLog.ActionStartDate = DateTime.Now; syncLog.ActionFinishDate = null; Singleton.Instance.Repository.Update(syncLog); bool successfull = false; if (syncLog.Action.GetType() == typeof(DeleteAction)) { var path = Path.Get(syncFrom.Path, syncLog.Action.RelativePath); logger.Info($"[{syncLog.Id}] [D] " + path); if (path.Exists) { var destinationPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toDelete", syncLog.Id).FullPath; if (syncLog.Action.IsDirectory) { DirectoryInfo info = new DirectoryInfo(path.FullPath); info.MoveTo(destinationPath); info.Delete(true, true); } else { FileInfo info = new FileInfo(path.FullPath); info.MoveTo(destinationPath); info.Delete(); } } successfull = true; } else if (syncLog.Action.GetType() == typeof(RenameAction)) { var renameAction = syncLog.Action as RenameAction; logger.Info($"[{syncLog.Id}] [R] {renameAction.RenameFrom} to {renameAction.RelativePath}"); if (String.IsNullOrWhiteSpace(renameAction.RenameFrom)) { CopyFile(syncLog, syncFrom); } string pathFrom = Path.Get(syncFrom.Path, renameAction.RenameFrom).FullPath; string tempPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toRename", syncLog.Id).FullPath; string pathTo = Path.Get(syncFrom.Path, renameAction.RelativePath).FullPath; if (syncLog.Action.IsDirectory) { new DirectoryInfo(pathFrom).MoveTo(tempPath, MoveOptions.None); new DirectoryInfo(tempPath).MoveTo(pathTo, MoveOptions.None); } else { Alphaleonis.Win32.Filesystem.File.Move(pathFrom, tempPath, MoveOptions.None); Alphaleonis.Win32.Filesystem.File.Move(tempPath, pathTo, MoveOptions.None); //new FileInfo(pathFrom).MoveTo(tempPath, MoveOptions.WriteThrough); //new FileInfo(tempPath).MoveTo(pathTo, MoveOptions.WriteThrough); } successfull = true; } else { successfull = CopyFile(syncLog, syncFrom); } syncLog.ActionFinishDate = DateTime.Now; syncLog.Successfull = successfull; Singleton.Instance.Repository.Update(syncLog); foreach (var error in Singleton.Instance.Repository.Many <Error>(f => f.SyncLog.Id == syncLog.Id)) { Singleton.Instance.Repository.Delete(error); } } catch (FileNotFoundException ex) { syncLog.RequiresManualIntervention = true; syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(ex, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = ex; error.ItemId = syncLog.Id; error.Message = ex.Message; Singleton.Instance.Repository.Add(error); } catch (Exception e) { syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(e, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = e; error.ItemId = syncLog.Id; error.Message = e.Message; Singleton.Instance.Repository.Add(error); } }
protected void DoTidyup(DirectoryInfo di) { #if DEBUG Debug.Assert(this._tidyup != null); Debug.Assert(this._tidyup.DeleteEmpty); #else if (_tidyup == null || !_tidyup.DeleteEmpty) { return; } #endif // See if we should now delete the folder we just moved that file from. if (di == null) { return; } //if there are sub-directories then we shouldn't remove this one if (di.GetDirectories().Length > 0) { return; } //if the directory is the root download folder do not delete if (TVSettings.Instance.MonitorFolders && TVSettings.Instance.SearchFoldersNames.Contains(di.FullName)) { return; } // Do not delete any monitor folders either if (TVSettings.Instance.MonitorFoldersNames.Contains(di.FullName)) { return; } FileInfo[] files = di.GetFiles(); if (files.Length == 0) { // its empty, so just delete it di.Delete(); return; } if (_tidyup.EmptyIgnoreExtensions && !_tidyup.EmptyIgnoreWords) { return; // nope } foreach (FileInfo fi in files) { bool okToDelete = _tidyup.EmptyIgnoreExtensions && Array.FindIndex(_tidyup.EmptyIgnoreExtensionsArray, x => x == fi.Extension) != -1; if (okToDelete) { continue; // onto the next file } // look in the filename if (_tidyup.EmptyIgnoreWordsArray.Any(word => fi.Name.Contains(word))) { okToDelete = true; } if (!okToDelete) { return; } } if (_tidyup.EmptyMaxSizeCheck) { // how many MB are we deleting? long totalBytes = files.Sum(fi => fi.Length); if (totalBytes / (1024 * 1024) > _tidyup.EmptyMaxSizeMB) { return; // too much } } DeleteOrRecycleFolder(di); }