Changeset 2105
- Timestamp:
- 5/15/2010 2:50:23 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser/Eraser.DefaultPlugins/ErasureTargets/FileSystemObjectErasureTarget.cs
r2085 r2105 228 228 for (int i = 0; i < paths.Count; ++i) 229 229 { 230 //Check that the file exists - we do not want to bother erasing nonexistant files 231 StreamInfo info = paths[i]; 232 if (!info.Exists) 233 { 234 Logger.Log(S._("The file {0} was not erased as the file does not exist.", 235 paths[i]), LogLevel.Notice); 236 continue; 237 } 238 239 //Get the filesystem provider to handle the secure file erasures 240 FileSystem fsManager = ManagerLibrary.Instance.FileSystemRegistrar[ 241 VolumeInfo.FromMountPoint(info.DirectoryName)]; 242 243 bool isReadOnly = false; 244 245 try 246 { 247 //Update the task progress 248 ProgressManager step = new ProgressManager(); 249 Progress.Steps.Add(new SteppedProgressManagerStep(step, 250 info.Length / (float)dataTotal, S._("Erasing files..."))); 251 OnProgressChanged(this, new ProgressChangedEventArgs(step, 252 new TaskProgressChangedEventArgs(info.FullName, 0, method.Passes))); 253 254 //Remove the read-only flag, if it is set. 255 if (isReadOnly = info.IsReadOnly) 256 info.IsReadOnly = false; 257 258 //Make sure the file does not have any attributes which may affect 259 //the erasure process 260 if ((info.Attributes & FileAttributes.Compressed) != 0 || 261 (info.Attributes & FileAttributes.Encrypted) != 0 || 262 (info.Attributes & FileAttributes.SparseFile) != 0) 263 { 264 //Log the error 265 Logger.Log(S._("The file {0} could not be erased because the file was " + 266 "either compressed, encrypted or a sparse file.", info.FullName), 267 LogLevel.Error); 268 continue; 230 ProgressManager step = new ProgressManager(); 231 Progress.Steps.Add(new SteppedProgressManagerStep(step, 232 paths[i].Length / (float)dataTotal, S._("Erasing files..."))); 233 EraseStream(paths[i], step); 234 } 235 } 236 237 /// <summary> 238 /// Erases the provided stream, and updates progress using tyhe provided 239 /// progress manager. 240 /// </summary> 241 /// <param name="info">The information regarding the stream that needs erasure.</param> 242 /// <param name="progress">The progress manager for the erasure of the current 243 /// stream.</param> 244 protected void EraseStream(StreamInfo info, ProgressManager progress) 245 { 246 //Check that the file exists - we do not want to bother erasing nonexistant files 247 if (!info.Exists) 248 { 249 Logger.Log(S._("The file {0} was not erased as the file does not exist.", 250 info.FileName), LogLevel.Notice); 251 return; 252 } 253 254 //Get the filesystem provider to handle the secure file erasures 255 FileSystem fsManager = ManagerLibrary.Instance.FileSystemRegistrar[ 256 VolumeInfo.FromMountPoint(info.DirectoryName)]; 257 258 bool isReadOnly = false; 259 260 try 261 { 262 //Update the task progress 263 ErasureMethod method = EffectiveMethod; 264 OnProgressChanged(this, new ProgressChangedEventArgs(progress, 265 new TaskProgressChangedEventArgs(info.FullName, 0, method.Passes))); 266 267 //Remove the read-only flag, if it is set. 268 if (isReadOnly = info.IsReadOnly) 269 info.IsReadOnly = false; 270 271 //Make sure the file does not have any attributes which may affect 272 //the erasure process 273 if ((info.Attributes & FileAttributes.Compressed) != 0 || 274 (info.Attributes & FileAttributes.Encrypted) != 0 || 275 (info.Attributes & FileAttributes.SparseFile) != 0) 276 { 277 //Log the error 278 Logger.Log(S._("The file {0} could not be erased because the file was " + 279 "either compressed, encrypted or a sparse file.", info.FullName), 280 LogLevel.Error); 281 return; 282 } 283 284 fsManager.EraseFileSystemObject(info, method, 285 delegate(long lastWritten, long totalData, int currentPass) 286 { 287 if (Task.Canceled) 288 throw new OperationCanceledException(S._("The task was cancelled.")); 289 290 progress.Total = totalData; 291 progress.Completed += lastWritten; 292 OnProgressChanged(this, new ProgressChangedEventArgs(progress, 293 new TaskProgressChangedEventArgs(info.FullName, currentPass, method.Passes))); 294 }); 295 296 //Remove the file. 297 FileInfo fileInfo = info.File; 298 if (fileInfo != null) 299 fsManager.DeleteFile(fileInfo); 300 progress.MarkComplete(); 301 } 302 catch (UnauthorizedAccessException) 303 { 304 Logger.Log(S._("The file {0} could not be erased because the file's " + 305 "permissions prevent access to the file.", info.FullName), LogLevel.Error); 306 } 307 catch (SharingViolationException) 308 { 309 if (!ManagerLibrary.Settings.ForceUnlockLockedFiles) 310 throw; 311 312 StringBuilder processStr = new StringBuilder(); 313 foreach (OpenHandle handle in OpenHandle.Close(info.FullName)) 314 { 315 try 316 { 317 processStr.AppendFormat( 318 System.Globalization.CultureInfo.InvariantCulture, 319 "{0}, ", System.Diagnostics.Process.GetProcessById(handle.ProcessId).MainModule.FileName); 269 320 } 270 271 fsManager.EraseFileSystemObject(info, method, 272 delegate(long lastWritten, long totalData, int currentPass) 273 { 274 if (Task.Canceled) 275 throw new OperationCanceledException(S._("The task was cancelled.")); 276 277 step.Total = totalData; 278 step.Completed += lastWritten; 279 OnProgressChanged(this, new ProgressChangedEventArgs(step, 280 new TaskProgressChangedEventArgs(info.FullName, currentPass, method.Passes))); 281 }); 282 283 //Remove the file. 284 FileInfo fileInfo = info.File; 285 if (fileInfo != null) 286 fsManager.DeleteFile(fileInfo); 287 step.MarkComplete(); 288 } 289 catch (UnauthorizedAccessException) 290 { 291 Logger.Log(S._("The file {0} could not be erased because the file's " + 292 "permissions prevent access to the file.", info.FullName), LogLevel.Error); 293 } 294 catch (SharingViolationException) 295 { 296 if (!ManagerLibrary.Settings.ForceUnlockLockedFiles) 297 throw; 298 299 StringBuilder processStr = new StringBuilder(); 300 foreach (OpenHandle handle in OpenHandle.Close(info.FullName)) 301 { 302 try 303 { 304 processStr.AppendFormat( 305 System.Globalization.CultureInfo.InvariantCulture, 306 "{0}, ", System.Diagnostics.Process.GetProcessById(handle.ProcessId).MainModule.FileName); 307 } 308 catch (System.ComponentModel.Win32Exception) 309 { 310 processStr.AppendFormat( 311 System.Globalization.CultureInfo.InvariantCulture, 312 "Process ID {0}, ", handle.ProcessId); 313 } 321 catch (System.ComponentModel.Win32Exception) 322 { 323 processStr.AppendFormat( 324 System.Globalization.CultureInfo.InvariantCulture, 325 "Process ID {0}, ", handle.ProcessId); 314 326 } 315 316 if (processStr.Length != 0) 317 Logger.Log(S._("Could not force closure of file \"{0}\" {1}",318 paths[i], S._("(locked by {0})",319 processStr.ToString().Remove(processStr.Length - 2)).Trim()),320 LogLevel.Error);321 }322 finally323 {324 //Re-set the read-only flag if the file exists (i.e. there was an error)325 if (isReadOnly && info.Exists && !info.IsReadOnly)326 info.IsReadOnly = isReadOnly;327 }327 } 328 329 if (processStr.Length != 0) 330 Logger.Log(S._("Could not force closure of file \"{0}\" {1}", 331 info.FileName, S._("(locked by {0})", 332 processStr.ToString().Remove(processStr.Length - 2)).Trim()), 333 LogLevel.Error); 334 } 335 finally 336 { 337 //Re-set the read-only flag if the file exists (i.e. there was an error) 338 if (isReadOnly && info.Exists && !info.IsReadOnly) 339 info.IsReadOnly = isReadOnly; 328 340 } 329 341 }
Note: See TracChangeset
for help on using the changeset viewer.
