protected static void ReadIgnore(List<IgnoreEntry> ignores, RepositoryInfo repInfo, XmlNode node) { string pattern = node.Attributes["pattern"].Value.ToString(); ignores.Add(new IgnoreEntry(repInfo, pattern)); }
public static void WriteManifest(string manifestFile, Dictionary<string, ManifestEntry> entries, List<IgnoreEntry> ignores, RepositoryInfo repInfo) { XmlDocument doc = new XmlDocument(); XmlNode pdNode = doc.CreateElement("patch_data"); XmlAttribute revAttr = doc.CreateAttribute("revision"); revAttr.Value = repInfo.version; pdNode.Attributes.Append(revAttr); XmlAttribute urlAttr = doc.CreateAttribute("url"); urlAttr.Value = repInfo.url; pdNode.Attributes.Append(urlAttr); foreach (IgnoreEntry entry in ignores) { if (entry.Repository != repInfo) // not an ignore for this repository - don't write it to this file continue; XmlNode node = doc.CreateElement("ignore"); XmlAttribute attr = doc.CreateAttribute("pattern"); attr.Value = entry.Pattern; node.Attributes.Append(attr); pdNode.AppendChild(node); } foreach (IgnoreEntry entry in repInfo.excludes) { if (entry.Repository != repInfo) // not an exclude for this repository - don't write it to this file continue; XmlNode node = doc.CreateElement("exclude"); XmlAttribute attr = doc.CreateAttribute("pattern"); attr.Value = entry.Pattern; node.Attributes.Append(attr); pdNode.AppendChild(node); } foreach (ManifestEntry entry in entries.Values) { if (entry.repository != repInfo) // not an entry for this repository - don't write it to this file continue; XmlNode node = doc.CreateElement("entry"); XmlAttribute nameAttr = doc.CreateAttribute("name"); nameAttr.Value = entry.name; node.Attributes.Append(nameAttr); XmlAttribute kindAttr = doc.CreateAttribute("kind"); kindAttr.Value = entry.type; node.Attributes.Append(kindAttr); if (entry.type == "file") { XmlAttribute digestAttr = doc.CreateAttribute("sha1_digest"); digestAttr.Value = entry.DigestString; node.Attributes.Append(digestAttr); XmlAttribute sizeAttr = doc.CreateAttribute("size"); sizeAttr.Value = entry.length.ToString(); node.Attributes.Append(sizeAttr); } pdNode.AppendChild(node); } doc.AppendChild(pdNode); // Write the document to the current manifest file Stream stream = File.Open(manifestFile, FileMode.Create); try { StreamWriter writer = new StreamWriter(stream); try { XmlWriter xmlWriter = XmlWriter.Create(writer); doc.WriteTo(xmlWriter); xmlWriter.Close(); } finally { writer.Close(); } } finally { stream.Close(); } }
protected static void ReadEntry(Dictionary<string, ManifestEntry> entries, RepositoryInfo repInfo, XmlNode node) { ManifestEntry entry = new ManifestEntry(); entry.repository = repInfo; entry.name = node.Attributes["name"].Value.ToString(); entry.type = node.Attributes["kind"].Value.ToString(); if (entry.type == "file") { entry.DigestString = node.Attributes["sha1_digest"].Value.ToString(); entry.length = long.Parse(node.Attributes["size"].Value); } entries[entry.name] = entry; }
/// <summary> /// Build a dictionary with entries for the various files and directories. /// Then write that information out to a file. /// </summary> /// <param name="currentManifestFile">file to which we will write the current manifest data</param> /// <param name="baseDir">full directory to start in (e.g. C:\Foo\Bar)</param> /// <param name="prefix">directory corresponding to baseDir in the manifest</param> /// <param name="repInfo">info about the version and source of data</param> private void BuildManifestFile(string currentManifestFile, string baseDir, string prefix, RepositoryInfo repInfo) { BuildManifestDict(baseDir, prefix, repInfo); WriteManifest(currentManifestFile, currentManifestDict, currentIgnorePatterns, repInfo); }
public IgnoreEntry(RepositoryInfo rep, string pattern) { this.repository = rep; this.pattern = pattern; this.regex = new Regex(pattern); }
/// <summary> /// Build up information about the existing files /// </summary> /// <param name="prefix">prefix to limit the search to</param> /// <param name="fullScan">true to scan all files or false to use the exising manifest file</param> private void BuildManifest(string prefix, bool fullScan, List<IgnoreEntry> excludes) { string baseDir = baseDirectory; string currentManifestFile = Path.Combine(baseDir, CurrentManifest); OnStateChanged(UpdateState.ScanningFiles); if (!fullScan) { try { LoadManifest(currentManifestFile); } catch (FileNotFoundException) { fullScan = true; } } if (fullScan) { RepositoryInfo nullRep = new RepositoryInfo(); nullRep.url = "http://localhost/"; nullRep.version = "0"; foreach (IgnoreEntry entry in excludes) nullRep.excludes.Add(new IgnoreEntry(nullRep, entry.Pattern)); BuildManifestFile(currentManifestFile, baseDir, prefix, nullRep); } }
/// <summary> /// Process the given file and add an entry for it in the /// currentManifestDict map /// </summary> /// <param name="name">name of the file (relative to base dir)</param> /// <param name="file">full path of the file</param> /// <param name="repInfo">info about the repository</param> protected void ProcessFile(string name, string file, RepositoryInfo repInfo) { ManifestEntry entry = new ManifestEntry(); entry.repository = repInfo; entry.name = name; entry.type = "file"; try { Stream data = File.Open(file, FileMode.Open); try { long fileLength = data.Length; entry.length = fileLength; HashAlgorithm digester = new SHA1CryptoServiceProvider(); byte[] digest = digester.ComputeHash(data); entry.digest = digest; } finally { data.Close(); } } catch (IOException e) { Trace.TraceError("Failed to access file: {0} - {1}", file, e.Message); entry.length = 0; entry.digest = null; } currentManifestDict[entry.name] = entry; }
/// <summary> /// Process a directory (including subdirectories) to generate /// information about the files and directories that are present. /// This will be used in a later pass to determine what actions /// must be taken to bring this in line with the patch info. /// An entry for the directory will be added to the /// currentManifestDict map. /// </summary> /// <param name="baseDir">Base directory to patch (e.g. C:\Foo\Bar)</param> /// <param name="dir">directory we are scanning (e.g. C:\Foo\Bar\Interface\FrameXML)</param> /// <param name="ignores">list of entries that should be ignored</param> /// <param name="repInfo">information about the repository</param> protected void ProcessDir(string baseDir, string dir, List<IgnoreEntry> ignores, RepositoryInfo repInfo) { string[] dirs = Directory.GetDirectories(dir); string[] files = Directory.GetFiles(dir); // Add an entry for our directory string shortDir = dir.Substring(baseDir.Length); shortDir = shortDir.Replace('\\', '/'); if (shortDir.StartsWith("/")) shortDir = shortDir.Substring(1); // If we are excluded from the manifest, just return // Do not process subdirectories or files if (IsExcluded(repInfo.excludes, shortDir)) return; if (!IsIgnored(ignores, shortDir)) { ManifestEntry entry = new ManifestEntry(); entry.repository = repInfo; entry.name = shortDir; entry.type = "dir"; entry.length = 0; entry.digest = null; currentManifestDict[entry.name] = entry; } // Add entries for the files in this directory foreach (string file in files) { if (file.StartsWith(baseDir)) { string name = file.Substring(baseDir.Length); name = name.Replace('\\', '/'); if (name.StartsWith("/")) name = name.Substring(1); if (IsIgnored(ignores, name)) continue; ProcessFile(name, file, repInfo); } } // Process our subdirectories foreach (string subdir in dirs) ProcessDir(baseDir, subdir, ignores, repInfo); }
/// <summary> /// Build a dictionary with entries for the various files and directories. /// This method assumes that all the files covered by this prefix are in one /// repository, though that may not be the case. Better support for multiple /// repositories in these methods will be added later. /// </summary> /// <param name="baseDir">Base directory to patch (e.g. C:\Foo\Bar)</param> /// <param name="prefix">Prefix to limit the scan</param> /// <param name="repInfo">Info about the repository this will be based on</param> protected void BuildManifestDict(string baseDir, string prefix, RepositoryInfo repInfo) { string[] dirs; if (prefix == null) { dirs = new string[1]; dirs[0] = baseDir; } else { dirs = Directory.GetDirectories(baseDir, prefix); } List<IgnoreEntry> ignores = new List<IgnoreEntry>(); // Regardless of what is in the patch data, ignore these three files ignores.Add(new IgnoreEntry(repInfo, CurrentManifest)); ignores.Add(new IgnoreEntry(repInfo, NewManifest)); ignores.Add(new IgnoreEntry(repInfo, Patcher)); ignores.Add(new IgnoreEntry(repInfo, "")); // Ignore the existing name foreach (string dir in dirs) ProcessDir(baseDir, dir, ignores, repInfo); }
protected static void ReadPatchData(Dictionary<string, ManifestEntry> entries, List<IgnoreEntry> ignores, List<IgnoreEntry> excludes, XmlNode node) { RepositoryInfo repInfo = new RepositoryInfo(); repInfo.version = node.Attributes["revision"].Value.ToString(); repInfo.url = node.Attributes["url"].Value.ToString(); foreach (XmlNode childNode in node.ChildNodes) { switch (childNode.Name) { case "entry": ReadEntry(entries, repInfo, childNode); break; case "ignore": ReadIgnore(ignores, repInfo, childNode); break; case "exclude": ReadExclude(excludes, repInfo, childNode); break; default: Debug.Assert(false, "Invalid xml"); break; } } }