C# question

Anonymous
timer Asked: Oct 21st, 2013
account_balance_wallet $5

Question Description

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;

  }

  }

}

This question has not been answered.

Create a free account to get help with this and any other question!

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors