public void SetWimInfo_Template(string wimFileName, uint bootIndex) { string srcWim = Path.Combine(TestSetup.SampleDir, wimFileName); string destWim = Path.GetTempFileName(); try { File.Copy(srcWim, destWim, true); using (Wim wim = Wim.OpenWim(destWim, OpenFlags.WRITE_ACCESS)) { WimInfo info = new WimInfo { BootIndex = bootIndex, }; wim.SetWimInfo(info, ChangeFlags.BOOT_INDEX); wim.Overwrite(WriteFlags.DEFAULT, Wim.DefaultThreads); } using (Wim wim = Wim.OpenWim(destWim, OpenFlags.DEFAULT)) { WimInfo info = wim.GetWimInfo(); Assert.IsTrue(info.BootIndex == bootIndex); } } finally { if (File.Exists(destWim)) { File.Delete(destWim); } } }
public void AddEmptyImage_Template(CompressionType compType, string wimFileName, AddFlags addFlags = AddFlags.DEFAULT) { string destDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[2]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.WRITE_METADATA_BEGIN: Assert.IsNull(info); _checked[0] = true; break; case ProgressMsg.WRITE_METADATA_END: Assert.IsNull(info); _checked[1] = true; break; } return(CallbackStatus.CONTINUE); } // Capture Wim string wimFile = Path.Combine(destDir, wimFileName); using (Wim wim = Wim.CreateNewWim(compType)) { wim.RegisterCallback(ProgressCallback); wim.AddEmptyImage("UnitTest"); wim.Write(wimFile, Wim.AllImages, WriteFlags.DEFAULT, Wim.DefaultThreads); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); } for (int i = 0; i < _checked.Length; i++) { Assert.IsTrue(_checked[i]); } } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void AddEmptyImageTemplate(CompressionType compType, string wimFileName) { string destDir = TestHelper.GetTempDir(); try { bool[] _checked = new bool[2]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.WriteMetadataBegin: Assert.IsNull(info); _checked[0] = true; break; case ProgressMsg.WriteMetadataEnd: Assert.IsNull(info); _checked[1] = true; break; } return(CallbackStatus.Continue); } // Capture Wim string wimFile = Path.Combine(destDir, wimFileName); using (Wim wim = Wim.CreateNewWim(compType)) { wim.RegisterCallback(ProgressCallback); wim.AddEmptyImage("UnitTest"); wim.Write(wimFile, Wim.AllImages, WriteFlags.None, Wim.DefaultThreads); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); } for (int i = 0; i < _checked.Length; i++) { Assert.IsTrue(_checked[i]); } } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void ReferenceTemplateImage_Template(string wimFileName, string captureDir, SampleSet set) { string destDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); try { Directory.CreateDirectory(destDir); string srcDir = Path.Combine(TestSetup.SampleDir, captureDir); string srcWimFile = Path.Combine(TestSetup.SampleDir, wimFileName); string destWimFile = Path.Combine(destDir, wimFileName); File.Copy(srcWimFile, destWimFile, true); int imageCount; using (Wim wim = Wim.OpenWim(destWimFile, OpenFlags.WRITE_ACCESS)) { WimInfo wi = wim.GetWimInfo(); imageCount = (int)wi.ImageCount; wim.AddImage(srcDir, "UnitTest", null, AddFlags.DEFAULT); wim.ReferenceTemplateImage(imageCount + 1, 1); wim.Overwrite(WriteFlags.DEFAULT, Wim.DefaultThreads); } List <Tuple <string, bool> > entries = new List <Tuple <string, bool> >(); CallbackStatus IterateCallback(DirEntry dentry, object userData) { string path = dentry.FullPath; bool isDir = (dentry.Attributes & FileAttribute.DIRECTORY) != 0; entries.Add(new Tuple <string, bool>(path, isDir)); return(CallbackStatus.CONTINUE); } string wimFile = Path.Combine(TestSetup.SampleDir, wimFileName); using (Wim wim = Wim.OpenWim(destWimFile, OpenFlags.DEFAULT)) { wim.IterateDirTree(imageCount + 1, Wim.RootPath, IterateFlags.RECURSIVE, IterateCallback); } TestHelper.CheckPathList(set, entries); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void GetAttributesTest() { WimInfo wimInfo = WimgApi.GetAttributes(TestWimHandle); wimInfo.ShouldNotBeNull(); wimInfo.Attributes.ShouldBe(WimInfoAttributes.Normal); wimInfo.BootIndex.ShouldBe(0); wimInfo.CompressionType.ShouldBe(WimCompressionType.Lzx); wimInfo.Guid.ShouldNotBe(Guid.Empty); wimInfo.ImageCount.ShouldBe(TestWimTemplate.ImageCount); wimInfo.PartNumber.ShouldBe(1); wimInfo.TotalParts.ShouldBe(1); }
public static void GetWimInfoTemplate(string fileName, CompressionType compType, bool boot) { string wimFile = Path.Combine(TestSetup.SampleDir, fileName); using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.None)) { WimInfo info = wim.GetWimInfo(); if (boot) { Assert.AreEqual(1u, info.BootIndex); } else { Assert.AreEqual(0u, info.BootIndex); } Assert.AreEqual(1u, info.ImageCount); Assert.AreEqual(compType, info.CompressionType); } }
public void GetWimInfo_Template(string fileName, CompressionType compType, bool boot) { string wimFile = Path.Combine(TestSetup.SampleDir, fileName); using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT)) { WimInfo info = wim.GetWimInfo(); if (boot) { Assert.IsTrue(info.BootIndex == 1); } else { Assert.IsTrue(info.BootIndex == 0); } Assert.IsTrue(info.ImageCount == 1); Assert.IsTrue(info.CompressionType == compType); } }
public void ExportImage_Template(string wimFileName, int imageIndex, string destImageName) { string srcWimPath = Path.Combine(TestSetup.SampleDir, wimFileName); string destDir = TestHelper.GetTempDir(); string destWimPath = Path.Combine(destDir, wimFileName); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[3]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.WRITE_STREAMS: { ProgressInfo_WriteStreams m = (ProgressInfo_WriteStreams)info; Assert.IsNotNull(m); Assert.AreEqual(m.CompressionType, CompressionType.LZMS); _checked[0] = true; } break; case ProgressMsg.WRITE_METADATA_BEGIN: Assert.IsNull(info); _checked[1] = true; break; case ProgressMsg.WRITE_METADATA_END: Assert.IsNull(info); _checked[2] = true; break; } return(CallbackStatus.CONTINUE); } using (Wim srcWim = Wim.OpenWim(srcWimPath, OpenFlags.DEFAULT)) { WimInfo swi = srcWim.GetWimInfo(); Assert.IsTrue(swi.ImageCount == 3); string imageName = srcWim.GetImageName(imageIndex); Assert.IsTrue(imageName.Equals(destImageName, StringComparison.Ordinal)); using (Wim destWim = Wim.CreateNewWim(CompressionType.LZMS)) { destWim.RegisterCallback(ProgressCallback); srcWim.ExportImage(imageIndex, destWim, destImageName, null, ExportFlags.GIFT); destWim.Write(destWimPath, Wim.AllImages, WriteFlags.DEFAULT, Wim.DefaultThreads); } for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } } using (Wim destWim = Wim.OpenWim(destWimPath, OpenFlags.DEFAULT)) { WimInfo dwi = destWim.GetWimInfo(); Assert.IsTrue(dwi.ImageCount == 1); string imageName = destWim.GetImageName(1); Assert.IsTrue(imageName.Equals(destImageName, StringComparison.Ordinal)); } long srcSize = new FileInfo(srcWimPath).Length; long destSize = new FileInfo(destWimPath).Length; Assert.IsTrue(destSize < srcSize); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void SetOutputChunkSizeTemplate(string wimFileName, CompressionType compType, uint chunkSize, bool success) { string destDir = TestHelper.GetTempDir(); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[5]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.ScanBegin: { ScanProgress m = (ScanProgress)info; Assert.IsNotNull(info); _checked[0] = true; } break; case ProgressMsg.ScanEnd: { ScanProgress m = (ScanProgress)info; Assert.IsNotNull(info); _checked[1] = true; } break; case ProgressMsg.WriteMetadataBegin: Assert.IsNull(info); _checked[2] = true; break; case ProgressMsg.WriteStreams: { WriteStreamsProgress m = (WriteStreamsProgress)info; Assert.IsNotNull(m); _checked[3] = true; } break; case ProgressMsg.WriteMetadataEnd: Assert.IsNull(info); _checked[4] = true; break; } return(CallbackStatus.Continue); } // Capture Wim string srcDir = Path.Combine(TestSetup.SampleDir, "Src01"); string wimFile = Path.Combine(destDir, wimFileName); using (Wim wim = Wim.CreateNewWim(compType)) { wim.RegisterCallback(ProgressCallback); try { wim.SetOutputChunkSize(chunkSize); } catch (WimLibException) { if (success) { Assert.Fail(); } else { return; } } wim.AddImage(srcDir, "UnitTest", null, AddFlags.None); wim.Write(wimFile, Wim.AllImages, WriteFlags.None, Wim.DefaultThreads); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); } TestHelper.CheckWimPath(SampleSet.Src01, wimFile); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void AddImageMultiSource_Template(CompressionType compType, string wimFileName, AddFlags addFlags = AddFlags.DEFAULT) { string destDir = TestHelper.GetTempDir(); try { bool[] _checked = new bool[5]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.SCAN_BEGIN: { ProgressInfo_Scan m = (ProgressInfo_Scan)info; Assert.IsNotNull(m); _checked[0] = true; } break; case ProgressMsg.SCAN_END: { ProgressInfo_Scan m = (ProgressInfo_Scan)info; Assert.IsNotNull(m); _checked[1] = true; } break; case ProgressMsg.WRITE_METADATA_BEGIN: Assert.IsNull(info); _checked[2] = true; break; case ProgressMsg.WRITE_STREAMS: { ProgressInfo_WriteStreams m = (ProgressInfo_WriteStreams)info; Assert.IsNotNull(m); _checked[3] = true; } break; case ProgressMsg.WRITE_METADATA_END: Assert.IsNull(info); _checked[4] = true; break; } return(CallbackStatus.CONTINUE); } string srcDir1 = Path.Combine(TestSetup.SampleDir, "Src01"); string srcDir3 = Path.Combine(TestSetup.SampleDir, "Src03"); string wimFile = Path.Combine(destDir, wimFileName); using (Wim wim = Wim.CreateNewWim(compType)) { wim.RegisterCallback(ProgressCallback); CaptureSource[] srcs = new CaptureSource[] { new CaptureSource(srcDir1, @"\A"), new CaptureSource(srcDir3, @"\Z"), }; wim.AddImageMultiSource(srcs, "UnitTest", null, addFlags); wim.Write(wimFile, Wim.AllImages, WriteFlags.DEFAULT, Wim.DefaultThreads); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); Assert.IsTrue(wim.DirExists(1, "A")); Assert.IsTrue(wim.DirExists(1, "Z")); } for (int i = 0; i < _checked.Length; i++) { Assert.IsTrue(_checked[i]); } } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void AddImage_Template(string wimFileName, CompressionType compType, AddFlags addFlags = AddFlags.DEFAULT) { string srcDir = Path.Combine(TestSetup.SampleDir, "Src01"); string destDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); string wimFile = Path.Combine(destDir, wimFileName); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[5]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.SCAN_BEGIN: { ProgressInfo_Scan m = (ProgressInfo_Scan)info; Assert.IsNotNull(info); _checked[0] = true; } break; case ProgressMsg.SCAN_END: { ProgressInfo_Scan m = (ProgressInfo_Scan)info; Assert.IsNotNull(info); _checked[1] = true; } break; case ProgressMsg.WRITE_METADATA_BEGIN: Assert.IsNull(info); _checked[2] = true; break; case ProgressMsg.WRITE_STREAMS: { ProgressInfo_WriteStreams m = (ProgressInfo_WriteStreams)info; Assert.IsNotNull(m); _checked[3] = true; } break; case ProgressMsg.WRITE_METADATA_END: Assert.IsNull(info); _checked[4] = true; break; } return(CallbackStatus.CONTINUE); } using (Wim wim = Wim.CreateNewWim(compType)) { wim.RegisterCallback(ProgressCallback); wim.AddImage(srcDir, "UnitTest", null, addFlags); wim.Write(wimFile, Wim.AllImages, WriteFlags.DEFAULT, Wim.DefaultThreads); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); } Assert.IsTrue(_checked.All(x => x)); TestHelper.CheckWimPath(SampleSet.Src01, wimFile); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void Split_Template(string testSet, ulong partSize) { string srcDir = Path.Combine(TestSetup.SampleDir, testSet); string destDir = TestHelper.GetTempDir(); string wimFile = Path.Combine(destDir, "LZX.wim"); string splitWimFile = Path.Combine(destDir, "Split.swm"); string splitWildcard = Path.Combine(destDir, "Split*.swm"); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[2]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.SPLIT_BEGIN_PART: { ProgressInfo_Split m = (ProgressInfo_Split)info; Assert.IsNotNull(m); _checked[0] = true; } break; case ProgressMsg.SPLIT_END_PART: { ProgressInfo_Split m = (ProgressInfo_Split)info; Assert.IsNotNull(m); _checked[1] = true; } break; } return(CallbackStatus.CONTINUE); } using (Wim wim = Wim.CreateNewWim(CompressionType.LZX)) { wim.AddImage(srcDir, "UnitTest", null, AddFlags.NO_ACLS); wim.Write(wimFile, Wim.AllImages, WriteFlags.DEFAULT, Wim.DefaultThreads); } TestHelper.CheckWimPath(SampleSet.Src03, wimFile); using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT, ProgressCallback)) { wim.Split(splitWimFile, partSize, WriteFlags.DEFAULT); } Assert.IsTrue(_checked.All(x => x)); List <Tuple <string, bool> > entries = new List <Tuple <string, bool> >(); CallbackStatus IterateCallback(DirEntry dentry, object userData) { string path = dentry.FullPath; bool isDir = (dentry.Attributes & FileAttribute.DIRECTORY) != 0; entries.Add(new Tuple <string, bool>(path, isDir)); return(CallbackStatus.CONTINUE); } using (Wim wim = Wim.OpenWim(splitWimFile, OpenFlags.DEFAULT)) { wim.ReferenceResourceFile(splitWildcard, RefFlags.GLOB_ENABLE | RefFlags.GLOB_ERR_ON_NOMATCH, OpenFlags.DEFAULT); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); wim.IterateDirTree(1, Wim.RootPath, IterateFlags.RECURSIVE, IterateCallback); } TestHelper.CheckPathList(SampleSet.Src03, entries); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void ExtractImageTemplate(string wimFileName) { string wimFile = Path.Combine(TestSetup.SampleDir, wimFileName); string destDir = TestHelper.GetTempDir(); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[5]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.ExtractImageBegin: { ExtractProgress m = (ExtractProgress)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[0] = true; } break; case ProgressMsg.ExtractImageEnd: { ExtractProgress m = (ExtractProgress)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[1] = true; } break; case ProgressMsg.ExtractFileStructure: { ExtractProgress m = (ExtractProgress)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[2] = true; } break; case ProgressMsg.ExtractStreams: { ExtractProgress m = (ExtractProgress)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[3] = true; } break; case ProgressMsg.ExtractMetadata: { ExtractProgress m = (ExtractProgress)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[4] = true; } break; } return(CallbackStatus.Continue); } using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.None)) { wim.RegisterCallback(ProgressCallback); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); wim.ExtractImage(1, destDir, ExtractFlags.None); } Assert.IsTrue(_checked.All(x => x)); TestHelper.CheckFileSystem(SampleSet.Src01, destDir); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void SetOutputPackCompressionType_Template(string wimFileName, CompressionType compType) { string destDir = TestHelper.GetTempDir(); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[5]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.SCAN_BEGIN: { ProgressInfo_Scan m = (ProgressInfo_Scan)info; Assert.IsNotNull(m); _checked[0] = true; } break; case ProgressMsg.SCAN_END: { ProgressInfo_Scan m = (ProgressInfo_Scan)info; Assert.IsNotNull(m); _checked[1] = true; } break; case ProgressMsg.WRITE_METADATA_BEGIN: Assert.IsNull(info); _checked[2] = true; break; case ProgressMsg.WRITE_STREAMS: { ProgressInfo_WriteStreams m = (ProgressInfo_WriteStreams)info; Assert.IsNotNull(m); _checked[3] = true; } break; case ProgressMsg.WRITE_METADATA_END: Assert.IsNull(info); _checked[4] = true; break; } return(CallbackStatus.CONTINUE); } // Capture Wim string srcDir = Path.Combine(TestSetup.SampleDir, "Src01"); string wimFile = Path.Combine(destDir, wimFileName); using (Wim wim = Wim.CreateNewWim(CompressionType.NONE)) { wim.RegisterCallback(ProgressCallback); wim.SetOutputCompressionType(compType); wim.SetOutputPackCompressionType(compType); wim.AddImage(srcDir, "UnitTest", null, AddFlags.DEFAULT); wim.Write(wimFile, Wim.AllImages, WriteFlags.SOLID, Wim.DefaultThreads); } using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT)) { WimInfo wi = wim.GetWimInfo(); Assert.AreEqual(wi.ImageCount, 1u); Assert.AreEqual(wi.CompressionType, compType); } TestHelper.CheckWimPath(SampleSet.Src01, wimFile); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void ExtractImage_Template(string wimFileName) { string wimFile = Path.Combine(TestSetup.SampleDir, wimFileName); string destDir = TestHelper.GetTempDir(); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[5]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.EXTRACT_IMAGE_BEGIN: { ProgressInfo_Extract m = (ProgressInfo_Extract)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[0] = true; } break; case ProgressMsg.EXTRACT_IMAGE_END: { ProgressInfo_Extract m = (ProgressInfo_Extract)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[1] = true; } break; case ProgressMsg.EXTRACT_FILE_STRUCTURE: { ProgressInfo_Extract m = (ProgressInfo_Extract)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[2] = true; } break; case ProgressMsg.EXTRACT_STREAMS: { ProgressInfo_Extract m = (ProgressInfo_Extract)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[3] = true; } break; case ProgressMsg.EXTRACT_METADATA: { ProgressInfo_Extract m = (ProgressInfo_Extract)info; Assert.IsNotNull(m); Assert.IsTrue(m.ImageName.Equals("Sample", StringComparison.Ordinal)); _checked[4] = true; } break; } return(CallbackStatus.CONTINUE); } using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT)) { wim.RegisterCallback(ProgressCallback); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); wim.ExtractImage(1, destDir, ExtractFlags.DEFAULT); } Assert.IsTrue(_checked.All(x => x)); TestHelper.CheckFileSystem(SampleSet.Src01, destDir); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public void DeleteImage_Template(string wimFileName, int deleteIndex, string deleteImageName) { string srcWim = Path.Combine(TestSetup.SampleDir, wimFileName); string destDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); string destWim = Path.Combine(destDir, wimFileName); try { Directory.CreateDirectory(destDir); File.Copy(srcWim, destWim, true); bool[] _checked = new bool[2]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.WRITE_STREAMS: { ProgressInfo_WriteStreams m = (ProgressInfo_WriteStreams)info; Assert.IsNotNull(m); Assert.AreEqual(m.CompressionType, CompressionType.LZX); _checked[0] = true; } break; case ProgressMsg.RENAME: { ProgressInfo_Rename m = (ProgressInfo_Rename)info; Assert.IsNotNull(m); Assert.IsNotNull(m.From); Assert.IsNotNull(m.To); _checked[1] = true; } break; } return(CallbackStatus.CONTINUE); } using (Wim wim = Wim.OpenWim(destWim, OpenFlags.WRITE_ACCESS)) { wim.RegisterCallback(ProgressCallback); WimInfo swi = wim.GetWimInfo(); Assert.IsTrue(swi.ImageCount == 3); string imageName = wim.GetImageName(deleteIndex); Assert.IsTrue(imageName.Equals(deleteImageName, StringComparison.Ordinal)); wim.DeleteImage(deleteIndex); wim.Overwrite(WriteFlags.DEFAULT, Wim.DefaultThreads); for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } WimInfo dwi = wim.GetWimInfo(); Assert.IsTrue(dwi.ImageCount == 2); for (int i = 1; i <= dwi.ImageCount; i++) { imageName = wim.GetImageName(i); Assert.IsFalse(imageName.Equals(deleteImageName, StringComparison.Ordinal)); } } } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }
public static bool CheckBranchCondition(EngineState s, BranchCondition c, out string logMessage) { { bool match = false; switch (c.Type) { case BranchConditionType.Equal: case BranchConditionType.Smaller: case BranchConditionType.Bigger: case BranchConditionType.SmallerEqual: case BranchConditionType.BiggerEqual: case BranchConditionType.EqualX: { string compArg1 = StringEscaper.Preprocess(s, c.Arg1); string compArg2 = StringEscaper.Preprocess(s, c.Arg2); bool ignoreCase = true; if (c.Type == BranchConditionType.EqualX) { ignoreCase = false; } NumberHelper.CompareStringNumberResult comp = NumberHelper.CompareStringNumber(compArg1, compArg2, ignoreCase); switch (comp) { case NumberHelper.CompareStringNumberResult.Equal: // For String and Number { if (c.Type == BranchConditionType.Equal && !c.NotFlag || c.Type == BranchConditionType.SmallerEqual && !c.NotFlag || c.Type == BranchConditionType.BiggerEqual && !c.NotFlag || c.Type == BranchConditionType.Smaller && c.NotFlag || c.Type == BranchConditionType.Bigger && c.NotFlag || c.Type == BranchConditionType.EqualX && !c.NotFlag) { match = true; } logMessage = $"[{compArg1}] is equal to [{compArg2}]"; } break; case NumberHelper.CompareStringNumberResult.Smaller: // For Number { if (c.Type == BranchConditionType.Smaller && !c.NotFlag || c.Type == BranchConditionType.SmallerEqual && !c.NotFlag || c.Type == BranchConditionType.Bigger && c.NotFlag || c.Type == BranchConditionType.BiggerEqual && c.NotFlag || c.Type == BranchConditionType.Equal && c.NotFlag || c.Type == BranchConditionType.EqualX && c.NotFlag) { match = true; } logMessage = $"[{compArg1}] is smaller than [{compArg2}]"; } break; case NumberHelper.CompareStringNumberResult.Bigger: // For Number { if (c.Type == BranchConditionType.Bigger && !c.NotFlag || c.Type == BranchConditionType.BiggerEqual && !c.NotFlag || c.Type == BranchConditionType.Smaller && c.NotFlag || c.Type == BranchConditionType.SmallerEqual && c.NotFlag || c.Type == BranchConditionType.Equal && c.NotFlag || c.Type == BranchConditionType.EqualX && c.NotFlag) { match = true; } logMessage = $"[{compArg1}] is bigger than [{compArg2}]"; } break; case NumberHelper.CompareStringNumberResult.NotEqual: // For String { if (c.Type == BranchConditionType.Equal && c.NotFlag || c.Type == BranchConditionType.EqualX && c.NotFlag) { match = true; } logMessage = $"[{compArg1}] is not equal to [{compArg2}]"; } break; default: throw new InternalException($"Cannot compare [{compArg1}] and [{compArg2}]"); } } break; case BranchConditionType.ExistFile: { string filePath = StringEscaper.Preprocess(s, c.Arg1); // Check filePath contains wildcard bool containsWildcard = true; if (Path.GetFileName(filePath).IndexOfAny(new char[] { '*', '?' }) == -1) // No wildcard { containsWildcard = false; } // Check if file exists if (filePath.Trim().Equals(string.Empty, StringComparison.Ordinal)) { match = false; } else if (containsWildcard) { if (Directory.Exists(FileHelper.GetDirNameEx(filePath)) == false) { match = false; } else { string[] list = Directory.GetFiles(FileHelper.GetDirNameEx(filePath), Path.GetFileName(filePath)); if (0 < list.Length) { match = true; } else { match = false; } } } else { match = File.Exists(filePath); } if (match) { logMessage = $"File [{filePath}] exists"; } else { logMessage = $"File [{filePath}] does not exist"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistDir: { string dirPath = StringEscaper.Preprocess(s, c.Arg1); // Check filePath contains wildcard bool containsWildcard = true; if (Path.GetFileName(dirPath).IndexOfAny(new char[] { '*', '?' }) == -1) // No wildcard { containsWildcard = false; } // Check if directory exists if (dirPath.Trim().Equals(string.Empty, StringComparison.Ordinal)) { match = false; } else if (containsWildcard) { if (Directory.Exists(FileHelper.GetDirNameEx(dirPath)) == false) { match = false; } else { string[] list = Directory.GetDirectories(FileHelper.GetDirNameEx(dirPath), Path.GetFileName(dirPath)); if (0 < list.Length) { match = true; } else { match = false; } } } else { match = Directory.Exists(dirPath); } if (match) { logMessage = $"Directory [{dirPath}] exists"; } else { logMessage = $"Directory [{dirPath}] does not exist"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistSection: { string iniFile = StringEscaper.Preprocess(s, c.Arg1); string section = StringEscaper.Preprocess(s, c.Arg2); match = Ini.CheckSectionExist(iniFile, section); if (match) { logMessage = $"Section [{section}] exists in INI file [{iniFile}]"; } else { logMessage = $"Section [{section}] does not exist in INI file [{iniFile}]"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistRegSection: case BranchConditionType.ExistRegSubKey: { string rootKey = StringEscaper.Preprocess(s, c.Arg1); string subKey = StringEscaper.Preprocess(s, c.Arg2); RegistryKey regRoot = RegistryHelper.ParseStringToRegKey(rootKey); if (regRoot == null) { throw new InvalidRegKeyException($"Invalid registry root key [{rootKey}]"); } using (RegistryKey regSubKey = regRoot.OpenSubKey(subKey)) { match = (regSubKey != null); if (match) { logMessage = $"Registry SubKey [{rootKey}\\{subKey}] exists"; } else { logMessage = $"Registry SubKey [{rootKey}\\{subKey}] does not exist"; } } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistRegKey: case BranchConditionType.ExistRegValue: { string rootKey = StringEscaper.Preprocess(s, c.Arg1); string subKey = StringEscaper.Preprocess(s, c.Arg2); string valueName = StringEscaper.Preprocess(s, c.Arg3); match = true; RegistryKey regRoot = RegistryHelper.ParseStringToRegKey(rootKey); if (regRoot == null) { throw new InvalidRegKeyException($"Invalid registry root key [{rootKey}]"); } using (RegistryKey regSubKey = regRoot.OpenSubKey(subKey)) { if (regSubKey == null) { match = false; } else { object value = regSubKey.GetValue(valueName); if (value == null) { match = false; } } if (match) { logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] exists"; } else { logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] does not exist"; } } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistRegMulti: { string rootKey = StringEscaper.Preprocess(s, c.Arg1); string subKey = StringEscaper.Preprocess(s, c.Arg2); string valueName = StringEscaper.Preprocess(s, c.Arg3); string subStr = StringEscaper.Preprocess(s, c.Arg4); match = false; RegistryKey regRoot = RegistryHelper.ParseStringToRegKey(rootKey); if (regRoot == null) { throw new InvalidRegKeyException($"Invalid registry root key [{rootKey}]"); } using (RegistryKey regSubKey = regRoot.OpenSubKey(subKey)) { if (regSubKey == null) { logMessage = $"Registry SubKey [{rootKey}\\{subKey}] does not exist"; } else { object valueData = regSubKey.GetValue(valueName, null); if (valueData == null) { logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] does not exist"; } else { RegistryValueKind kind = regSubKey.GetValueKind(valueName); if (kind != RegistryValueKind.MultiString) { logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] is not REG_MULTI_SZ"; } else { string[] strs = (string[])valueData; if (strs.Contains(subStr, StringComparer.OrdinalIgnoreCase)) { match = true; logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] contains substring [{subStr}]"; } else { logMessage = $"Registry Value [{rootKey}\\{subKey}\\{valueName}] does not contain substring [{subStr}]"; } } } } } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistVar: { Variables.VarKeyType type = Variables.DetermineType(c.Arg1); if (type == Variables.VarKeyType.Variable) { match = s.Variables.ContainsKey(Variables.TrimPercentMark(c.Arg1)); if (match) { logMessage = $"Variable [{c.Arg1}] exists"; } else { logMessage = $"Variable [{c.Arg1}] does not exist"; } } else { match = false; logMessage = $"[{c.Arg1}] is not a variable"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.ExistMacro: { string macroName = StringEscaper.Preprocess(s, c.Arg1); match = s.Macro.MacroDict.ContainsKey(macroName) || s.Macro.LocalDict.ContainsKey(macroName); if (match) { logMessage = $"Macro [{macroName}] exists"; } else { logMessage = $"Macro [{macroName}] does not exist"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.WimExistIndex: { string wimFile = StringEscaper.Preprocess(s, c.Arg1); string imageIndexStr = StringEscaper.Preprocess(s, c.Arg2); if (!NumberHelper.ParseInt32(imageIndexStr, out int imageIndex)) { logMessage = $"Index [{imageIndexStr}] is not a positive integer"; } else if (imageIndex < 1) { logMessage = $"Index [{imageIndexStr}] is not a positive integer"; } else { if (File.Exists(wimFile)) { try { using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT)) { WimInfo wi = wim.GetWimInfo(); if (imageIndex <= wi.ImageCount) { match = true; logMessage = $"ImageIndex [{imageIndex}] exists in [{wimFile}]"; } else { logMessage = $"ImageIndex [{imageIndex}] does not exist in [{wimFile}]"; } } } catch (WimLibException e) { logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]"; } } else { logMessage = $"Wim [{wimFile}] does not exist"; } } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.WimExistFile: { string wimFile = StringEscaper.Preprocess(s, c.Arg1); string imageIndexStr = StringEscaper.Preprocess(s, c.Arg2); string filePath = StringEscaper.Preprocess(s, c.Arg3); if (!NumberHelper.ParseInt32(imageIndexStr, out int imageIndex)) { logMessage = $"Index [{imageIndexStr}] is not a positive integer"; } else if (imageIndex < 1) { logMessage = $"Index [{imageIndexStr}] is not a positive integer"; } else { if (File.Exists(wimFile)) { try { using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT)) { bool isFile = false; CallbackStatus WimExistFileCallback(DirEntry dentry, object userData) { if ((dentry.Attributes & FileAttribute.DIRECTORY) == 0) { isFile = true; } return(CallbackStatus.CONTINUE); } try { wim.IterateDirTree(imageIndex, filePath, IterateFlags.DEFAULT, WimExistFileCallback, null); if (isFile) { match = true; logMessage = $"File [{filePath}] exists in [{wimFile}]"; } else { logMessage = $"File [{filePath}] does not exist in [{wimFile}]"; } } catch (WimLibException e) { switch (e.ErrorCode) { case ErrorCode.INVALID_IMAGE: logMessage = $"File [{filePath}] does not have image index [{imageIndex}]"; break; case ErrorCode.PATH_DOES_NOT_EXIST: logMessage = $"File [{filePath}] does not exist in [{wimFile}]"; break; default: logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]"; break; } } } } catch (WimLibException e) { logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]"; } } else { logMessage = $"Wim [{wimFile}] does not exist"; } } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.WimExistDir: { string wimFile = StringEscaper.Preprocess(s, c.Arg1); string imageIndexStr = StringEscaper.Preprocess(s, c.Arg2); string dirPath = StringEscaper.Preprocess(s, c.Arg3); if (!NumberHelper.ParseInt32(imageIndexStr, out int imageIndex)) { logMessage = $"Index [{imageIndexStr}] is not a positive integer"; } else if (imageIndex < 1) { logMessage = $"Index [{imageIndexStr}] is not a positive integer"; } else { if (File.Exists(wimFile)) { try { using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.DEFAULT)) { bool isDir = false; CallbackStatus WimExistFileCallback(DirEntry dentry, object userData) { if ((dentry.Attributes & FileAttribute.DIRECTORY) != 0) { isDir = true; } return(CallbackStatus.CONTINUE); } try { wim.IterateDirTree(imageIndex, dirPath, IterateFlags.DEFAULT, WimExistFileCallback, null); if (isDir) { match = true; logMessage = $"Dir [{dirPath}] exists in [{wimFile}]"; } else { logMessage = $"Dir [{dirPath}] does not exist in [{wimFile}]"; } } catch (WimLibException e) { switch (e.ErrorCode) { case ErrorCode.INVALID_IMAGE: logMessage = $"Dir [{dirPath}] does not have image index [{imageIndex}]"; break; case ErrorCode.PATH_DOES_NOT_EXIST: logMessage = $"Dir [{dirPath}] does not exist in [{wimFile}]"; break; default: logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]"; break; } } } } catch (WimLibException e) { logMessage = $"Error [{e.ErrorCode}] occured while handling [{wimFile}]"; } } else { logMessage = $"Wim [{wimFile}] does not exist"; } } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.Ping: { string host = StringEscaper.Preprocess(s, c.Arg1); Ping pinger = new Ping(); try { try { PingReply reply = pinger.Send(host); if (reply.Status == IPStatus.Success) { match = true; } else { match = false; } } catch { match = false; } if (match) { logMessage = $"[{host}] responded to Ping"; } else { logMessage = $"[{host}] did not respond to Ping"; } } catch (PingException e) { match = false; logMessage = $"Error while pinging [{host}] : [{e.Message}]"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.Online: { // Note that system connected only to local network also returns true match = NetworkInterface.GetIsNetworkAvailable(); if (match) { logMessage = "System is online"; } else { logMessage = "System is offline"; } if (c.NotFlag) { match = !match; } } break; case BranchConditionType.Question: // can have 1 or 3 argument { string question = StringEscaper.Preprocess(s, c.Arg1); bool autoTimeout = false; if (c.Arg2 != null && c.Arg3 != null) { autoTimeout = true; } int timeout = 0; bool defaultChoice = false; if (autoTimeout) { string timeoutStr = StringEscaper.Preprocess(s, c.Arg2); if (NumberHelper.ParseInt32(timeoutStr, out timeout) == false) { autoTimeout = false; } if (timeout <= 0) { autoTimeout = false; } string defaultChoiceStr = StringEscaper.Preprocess(s, c.Arg3); if (defaultChoiceStr.Equals("True", StringComparison.OrdinalIgnoreCase)) { defaultChoice = true; } else if (defaultChoiceStr.Equals("False", StringComparison.OrdinalIgnoreCase)) { defaultChoice = false; } } System.Windows.Shell.TaskbarItemProgressState oldTaskbarItemProgressState = s.MainViewModel.TaskbarProgressState; // Save our progress state s.MainViewModel.TaskbarProgressState = System.Windows.Shell.TaskbarItemProgressState.Paused; if (autoTimeout) { MessageBoxResult result = MessageBoxResult.None; Application.Current.Dispatcher.Invoke(() => { result = CustomMessageBox.Show(question, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question, timeout); }); if (result == MessageBoxResult.None) { match = defaultChoice; if (defaultChoice) { logMessage = "[Yes] was automatically chosen"; } else { logMessage = "[No] was automatically chosen"; } } else if (result == MessageBoxResult.Yes) { match = true; logMessage = "[Yes] was chosen"; } else if (result == MessageBoxResult.No) { match = false; logMessage = "[No] was chosen"; } else { throw new InternalException("Internal Error at Check() of If,Question"); } } else { MessageBoxResult result = MessageBox.Show(question, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { match = true; logMessage = "[Yes] was chosen"; } else if (result == MessageBoxResult.No) { match = false; logMessage = "[No] was chosen"; } else { throw new InternalException("Internal Error at Check() of If,Question"); } } if (c.NotFlag) { match = !match; } s.MainViewModel.TaskbarProgressState = oldTaskbarItemProgressState; } break; default: throw new InternalException($"Internal BranchCondition check error"); } return(match); } }
public void SplitTemplate(string testSet, ulong partSize) { string srcDir = Path.Combine(TestSetup.SampleDir, testSet); string destDir = TestHelper.GetTempDir(); string wimFile = Path.Combine(destDir, "LZX.wim"); string splitWimFile = Path.Combine(destDir, "Split.swm"); string splitWildcard = Path.Combine(destDir, "Split*.swm"); try { Directory.CreateDirectory(destDir); bool[] _checked = new bool[2]; for (int i = 0; i < _checked.Length; i++) { _checked[i] = false; } CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx) { switch (msg) { case ProgressMsg.SplitBeginPart: { SplitProgress m = (SplitProgress)info; Assert.IsNotNull(m); _checked[0] = true; } break; case ProgressMsg.SplitEndPart: { SplitProgress m = (SplitProgress)info; Assert.IsNotNull(m); _checked[1] = true; } break; } return(CallbackStatus.Continue); } using (Wim wim = Wim.CreateNewWim(CompressionType.LZX)) { wim.AddImage(srcDir, "UnitTest", null, AddFlags.NoAcls); wim.Write(wimFile, Wim.AllImages, WriteFlags.None, Wim.DefaultThreads); } TestHelper.CheckWimPath(SampleSet.Src03, wimFile); using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.None, ProgressCallback)) { wim.Split(splitWimFile, partSize, WriteFlags.None); } Assert.IsTrue(_checked.All(x => x)); List <Tuple <string, bool> > entries = new List <Tuple <string, bool> >(); int IterateCallback(DirEntry dentry, object userData) { string path = dentry.FullPath; bool isDir = (dentry.Attributes & FileAttributes.Directory) != 0; entries.Add(new Tuple <string, bool>(path, isDir)); return(Wim.IterateCallbackSuccess); } using (Wim wim = Wim.OpenWim(splitWimFile, OpenFlags.None)) { wim.ReferenceResourceFile(splitWildcard, RefFlags.GlobEnable | RefFlags.GlobErrOnNoMatch, OpenFlags.None); WimInfo wi = wim.GetWimInfo(); Assert.IsTrue(wi.ImageCount == 1); wim.IterateDirTree(1, Wim.RootPath, IterateDirTreeFlags.Recursive, IterateCallback); } TestHelper.CheckPathList(SampleSet.Src03, entries); } finally { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } } }