3 using System.Collections.Generic;
\r
5 using System.Security.AccessControl;
\r
6 using System.Security.Principal;
\r
8 using Trinet.Core.IO.Ntfs;
\r
9 using CompleteEraser.Properties;
\r
11 namespace CompleteEraser
\r
15 const long ShirnkLength = 1;
\r
17 public static void BreakFileOrFolder(string path)
\r
19 if (Directory.Exists(path))
\r
21 else if (File.Exists(path))
\r
25 public static void BreakFile(string file)
\r
27 FileAttributes attr = File.GetAttributes(file);
\r
28 if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
\r
29 File.SetAttributes(file, attr & ~FileAttributes.ReadOnly);
\r
31 string ext = Path.GetExtension(file);
\r
34 FileInfo info = new FileInfo(file);
\r
35 foreach (AlternateDataStreamInfo alt in info.ListAlternateDataStreams())
\r
37 fs = alt.Open(FileMode.Open);
\r
38 BreakFileSlowSpeed(fs);
\r
42 fs = new FileStream(file, FileMode.Open);
\r
43 if (Settings.Default.AlwaysSlowSpeed || Settings.Default.SlowSpeedExt.IndexOf(ext) != -1)
\r
44 BreakFileSlowSpeed(fs);
\r
46 BreakFileHiSpeed(fs);
\r
49 string newFile = GenerateRandoName(Path.GetDirectoryName(file), Path.GetFileName(file).Length);
\r
50 if(File.Exists(newFile) == false)
\r
51 File.Move(file, newFile);
\r
53 File.Delete(newFile);
\r
56 private static void BreakFileSlowSpeed(FileStream fs)
\r
58 fs.Seek(0, SeekOrigin.Begin);
\r
59 byte[] data = new byte[Settings.Default.FillLengthAtHiSpeed];
\r
60 for (long i = 0; i < fs.Length; i += data.Length)
\r
61 fs.Write(data,0,data.Length);
\r
63 fs.SetLength(ShirnkLength);
\r
66 private static void BreakFileHiSpeed(FileStream fs)
\r
68 fs.Seek(0, SeekOrigin.Begin);
\r
69 byte[] data = new byte[Settings.Default.FillLengthAtHiSpeed];
\r
70 fs.Write(data, 0, data.Length);
\r
71 fs.Seek(-data.Length, SeekOrigin.End);
\r
72 fs.Write(data, 0, data.Length);
\r
74 fs.SetLength(ShirnkLength);
\r
77 public static void BreakFolder(string folder)
\r
79 IEnumerable<string> files = Directory.EnumerateFiles(folder, "*.*", SearchOption.TopDirectoryOnly);
\r
80 foreach (string file in files)
\r
82 IEnumerable<string> dirs = Directory.EnumerateDirectories(folder, "*.*", SearchOption.TopDirectoryOnly);
\r
83 foreach (string dir in dirs)
\r
85 string newName = RenameFolderName(folder);
\r
86 Directory.Delete(newName);
\r
89 public static string RenameFolderName(string dir)
\r
91 string newName = GenerateRandoName(Path.GetDirectoryName(dir), Path.GetFileName(dir).Length);
\r
92 if (Directory.Exists(newName))
\r
94 if ((FileAccessPermissionHelper.GetCurrentAccessRule(dir).FileSystemRights & FileSystemRights.Modify) == FileSystemRights.Modify)
\r
95 Directory.Move(dir, newName);
\r
97 throw new UnauthorizedAccessException();
\r
101 private static string GenerateRandoName(string dirpath, int length)
\r
103 string list = "abcdefghijklmnopqrstuvwxyz0123456789";
\r
104 Random rnd = new Random();
\r
105 StringBuilder output = new StringBuilder();
\r
106 for (int i = 0; i < length; i++)
\r
107 output.Append(list[rnd.Next(list.Length - 1)]);
\r
108 return dirpath + "\\" + output.ToString();
\r
112 public static class FileAccessPermissionHelper
\r
115 public static bool IsGotPermission(string path)
\r
117 var rule = GetCurrentAccessRule(path);
\r
118 return ((rule != null) && ((rule.FileSystemRights & FileSystemRights.Write) == FileSystemRights.Write));
\r
121 // 現在のユーザーが持っている指定パスのFileSystemAccessRuleを得る
\r
122 public static FileSystemAccessRule GetCurrentAccessRule(string path)
\r
124 var fileSecurity = File.GetAccessControl(path);
\r
125 var rules = fileSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier)).OfType<FileSystemAccessRule>();
\r
126 var currentIdentity = WindowsIdentity.GetCurrent();
\r
127 var sids = new[] { currentIdentity.User }.Concat(currentIdentity.Groups);
\r
129 // アクセスルール内にユーザーSIDがある?無ければグループSIDも探す
\r
130 return rules.FirstOrDefault(rule => sids.Contains(rule.IdentityReference));
\r