Time remaining:
C# question

Programming
Tutor: None Selected Time limit: 0 Hours

hidden

What does this code do? Are there any bugs? does the code require refactoring?

static void Cache(WindowsIdentity identity, FileInfo Source, FileInfo Target, Dictionary<string, ReplaceContents> rc)

{

  lock (lockThis)

  {

  List<string> children = null;

  string text = null;

  byte[] data = null;

  DateTime? Source_LastWriteTime = null;

  // Get target last-modification time

  // Note: .Exists returns false for Directories, hence the Attribute checking

  //  .Attributes returns -1 on error (such as doesnt exist). Perhaps I should simply check this value? (would need further testing).

  bool Target_Exists = Target.Exists || (((int)Target.Attributes != -1) && ((Target.Attributes & FileAttributes.Directory) == FileAttributes.Directory));

  DateTime? Target_LastWriteTime = null;

  if (Target_Exists)

   Target_LastWriteTime = Target.LastWriteTime;

  // Read/Enumerate Source using impersonation

  using (identity.Impersonate())

  {

  try

  {

  if (((int)Source.Attributes != -1) && ((Source.Attributes & FileAttributes.Directory) == FileAttributes.Directory))

  {

  children = new List<string>();

  foreach (var child in Directory.EnumerateFileSystemEntries(Source.FullName))

  children.Add(new FileInfo(child).Name);

  }

  else if (Source.Exists && ((Target_LastWriteTime == null) || (Target_LastWriteTime.Value < Source.LastWriteTime)))

  {

  Source_LastWriteTime = Source.LastWriteTime;

  if (rc.ContainsKey(Source.Extension))

  text = File.ReadAllText(Source.FullName);

  else

  data = File.ReadAllBytes(Source.FullName);

  }

   }

  catch (Exception ex)

  {

  // Only swallow error if target already exists (assume cache has current version of file/folder)

  if (Target_Exists)

  return;

  else

   throw;

  }

  }

  // Source is a Directory?

  if (children != null)

  {

  if (Target.Exists)

  Target.Delete();

  else if (((int)Target.Attributes == -1) || ((Target.Attributes & FileAttributes.Directory) != FileAttributes.Directory))

  Directory.CreateDirectory(Target.FullName);

  foreach (var child in children)

  Cache(identity, new FileInfo(Path.Combine(Source.FullName, child)), new FileInfo(Path.Combine(Target.FullName, child)), rc);

  }

  // Source is a text file with a matching delegate for replacing its contents

  else if (text != null)

  {

  Directory.CreateDirectory(Target.DirectoryName);

   text = rc[Source.Extension](Source.FullName, Target.FullName, text);

  File.WriteAllText(Target.FullName, text);

  Target.LastWriteTime = Source_LastWriteTime.Value;

  }

  // Source is a file

  else if (data != null)

  {

  Directory.CreateDirectory(Target.DirectoryName);

  File.WriteAllBytes(Target.FullName, data);

  Target.LastWriteTime = Source_LastWriteTime.Value;

  }

  }

}

Oct 21st, 2013

hi 


can you able to provide more details of the code where you wants implement this code.



Aug 27th, 2014

Did you know? You can earn $20 for every friend you invite to Studypool!
Click here to
Refer a Friend
hidden
...
Oct 21st, 2013
...
Oct 21st, 2013
Mar 25th, 2017
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer