示例#1
0
        public void JoinProgressTemplate(string[] splitWimNames)
        {
            string[] splitWims = splitWimNames.Select(x => Path.Combine(TestSetup.SampleDir, x)).ToArray();
            string   destDir   = TestHelper.GetTempDir();
            string   destWim   = Path.Combine(destDir, "Dest.wim");

            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.WriteMetadataBegin:
                        Assert.IsNull(info);
                        _checked[0] = true;
                        break;

                    case ProgressMsg.WriteMetadataEnd:
                        Assert.IsNull(info);
                        _checked[1] = true;
                        break;

                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        Assert.IsNotNull(m);

                        Assert.AreEqual(m.CompressionType, CompressionType.LZX);
                        _checked[2] = true;
                    }
                    break;
                    }
                    return(CallbackStatus.Continue);
                }

                Wim.Join(splitWims, destWim, OpenFlags.None, WriteFlags.None, ProgressCallback);

                Assert.IsTrue(_checked.All(x => x));

                TestHelper.CheckWimPath(SampleSet.Src03, destWim);
            }
            finally
            {
                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }
            }
        }
示例#2
0
        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);
                }
            }
        }
示例#3
0
        public void ExportImageTemplate(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.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        Assert.IsNotNull(m);

                        Assert.AreEqual(m.CompressionType, CompressionType.LZMS);
                        _checked[0] = true;
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        Assert.IsNull(info);
                        _checked[1] = true;
                        break;

                    case ProgressMsg.WriteMetadataEnd:
                        Assert.IsNull(info);
                        _checked[2] = true;
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                using (Wim srcWim = Wim.OpenWim(srcWimPath, OpenFlags.None))
                {
                    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.None, Wim.DefaultThreads);
                    }

                    for (int i = 0; i < _checked.Length; i++)
                    {
                        _checked[i] = false;
                    }
                }

                using (Wim destWim = Wim.OpenWim(destWimPath, OpenFlags.None))
                {
                    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);
                }
            }
        }
示例#4
0
        public bool AddFileToImage(string wimFile, int imageIndex, string fileToAdd, string destination, IImaging.ProgressCallback progressCallback = null)
        {
            string title = $"Adding {destination} to {wimFile.Split('\\').Last()}...";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.ScanBegin:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.ScanEnd:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        break;

                    case ProgressMsg.UpdateBeginCommand:
                        break;

                    case ProgressMsg.UpdateEndCommand:
                        break;

                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.WriteMetadataEnd:
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.WriteAccess))
                {
                    wim.RegisterCallback(ProgressCallback);
                    wim.UpdateImage(
                        imageIndex,
                        UpdateCommand.SetAdd(fileToAdd, destination, null, AddFlags.None),
                        UpdateFlags.SendProgress);
                    wim.Overwrite(WriteFlags.None, Wim.DefaultThreads);
                }
            }
            catch
            {
                return(false);
            }
            return(ReseatWIMXml(wimFile));
        }
示例#5
0
        public bool CaptureImage(
            string wimFile,
            string imageName,
            string imageDescription,
            string imageFlag,
            string InputDirectory,
            string imageDisplayName                    = null,
            string imageDisplayDescription             = null,
            WimCompressionType compressionType         = WimCompressionType.Lzx,
            IImaging.ProgressCallback progressCallback = null,
            int UpdateFrom   = -1,
            bool PreserveACL = true)
        {
            string title = $"Creating {imageName} ({wimFile.Split('\\').Last()})";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.ScanBegin:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, {m.NumDirsScanned} Directories, {m.NumNonDirsScanned} Files, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.ScanDEntry:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, {m.NumDirsScanned} Directories, {m.NumNonDirsScanned} Files, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.ScanEnd:
                    {
                        ScanProgress m = (ScanProgress)info;
                        progressCallback?.Invoke($"Scanning files ({m.NumBytesScanned} bytes scanned, {m.NumDirsScanned} Directories, {m.NumNonDirsScanned} Files, Current directory: {m.CurPath})", 0, true);
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        break;

                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.WriteMetadataEnd:
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                if (File.Exists(wimFile))
                {
                    using (Wim wim = Wim.OpenWim(wimFile, OpenFlags.WriteAccess))
                    {
                        wim.RegisterCallback(ProgressCallback);
                        wim.AddImage(InputDirectory, imageName, null, PreserveACL ? AddFlags.StrictAcls : AddFlags.NoAcls);
                        if (!string.IsNullOrEmpty(imageDescription))
                        {
                            wim.SetImageDescription((int)wim.GetWimInfo().ImageCount, imageDescription);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayName))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYNAME", imageDisplayName);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayDescription))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYDESCRIPTION", imageDisplayDescription);
                        }
                        if (!string.IsNullOrEmpty(imageFlag))
                        {
                            wim.SetImageFlags((int)wim.GetWimInfo().ImageCount, imageFlag);
                        }
                        if (UpdateFrom != -1)
                        {
                            wim.ReferenceTemplateImage((int)wim.GetWimInfo().ImageCount, UpdateFrom);
                        }
                        wim.Overwrite(WriteFlags.None, Wim.DefaultThreads);
                    }
                }
                else
                {
                    var compression = CompressionType.None;
                    switch (compressionType)
                    {
                    case WimCompressionType.Lzms:
                    {
                        compression = CompressionType.LZMS;
                        break;
                    }

                    case WimCompressionType.Lzx:
                    {
                        compression = CompressionType.LZX;
                        break;
                    }

                    case WimCompressionType.None:
                    {
                        compression = CompressionType.None;
                        break;
                    }

                    case WimCompressionType.Xpress:
                    {
                        compression = CompressionType.XPRESS;
                        break;
                    }
                    }

                    using (Wim wim = Wim.CreateNewWim(compression))
                    {
                        wim.RegisterCallback(ProgressCallback);

                        string config = @"[ExclusionList]
\$ntfs.log
\hiberfil.sys
\pagefile.sys
\swapfile.sys
\System Volume Information";

                        var configpath = Path.GetTempFileName();
                        File.Delete(configpath);
                        File.WriteAllText(configpath, config);

                        wim.AddImage(InputDirectory, imageName, configpath, PreserveACL ? AddFlags.StrictAcls : AddFlags.NoAcls);
                        if (!string.IsNullOrEmpty(imageDescription))
                        {
                            wim.SetImageDescription((int)wim.GetWimInfo().ImageCount, imageDescription);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayName))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYNAME", imageDisplayName);
                        }
                        if (!string.IsNullOrEmpty(imageDisplayDescription))
                        {
                            wim.SetImageProperty((int)wim.GetWimInfo().ImageCount, "DISPLAYDESCRIPTION", imageDisplayDescription);
                        }
                        if (!string.IsNullOrEmpty(imageFlag))
                        {
                            wim.SetImageFlags((int)wim.GetWimInfo().ImageCount, imageFlag);
                        }
                        wim.Write(wimFile, Wim.AllImages, WriteFlags.None, Wim.DefaultThreads);
                        File.Delete(configpath);
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(ReseatWIMXml(wimFile));
        }
示例#6
0
        public bool ExportImage(string wimFile, string destinationWimFile, int imageIndex, IEnumerable <string> referenceWIMs = null, WimCompressionType compressionType = WimCompressionType.Lzx, IImaging.ProgressCallback progressCallback = null)
        {
            string title = $"Exporting {wimFile.Split('\\').Last()} - Index {imageIndex}";

            try
            {
                CallbackStatus ProgressCallback(ProgressMsg msg, object info, object progctx)
                {
                    switch (msg)
                    {
                    case ProgressMsg.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        progressCallback?.Invoke(title, (int)Math.Round((double)m.CompletedBytes / m.TotalBytes * 100), false);
                    }
                    break;

                    case ProgressMsg.WriteMetadataBegin:
                        break;

                    case ProgressMsg.WriteMetadataEnd:
                        break;
                    }
                    return(CallbackStatus.Continue);
                }

                using (Wim srcWim = Wim.OpenWim(wimFile, OpenFlags.None))
                {
                    string imageName        = srcWim.GetImageName(imageIndex);
                    string imageDescription = srcWim.GetImageDescription(imageIndex);

                    var compression = CompressionType.None;
                    switch (compressionType)
                    {
                    case WimCompressionType.Lzms:
                    {
                        compression = CompressionType.LZMS;
                        break;
                    }

                    case WimCompressionType.Lzx:
                    {
                        compression = CompressionType.LZX;
                        break;
                    }

                    case WimCompressionType.None:
                    {
                        compression = CompressionType.None;
                        break;
                    }

                    case WimCompressionType.Xpress:
                    {
                        compression = CompressionType.XPRESS;
                        break;
                    }
                    }

                    if (referenceWIMs != null && referenceWIMs.Count() > 0)
                    {
                        srcWim.ReferenceResourceFiles(referenceWIMs, RefFlags.None, OpenFlags.None);
                    }

                    if (File.Exists(destinationWimFile))
                    {
                        using (Wim destWim = Wim.OpenWim(destinationWimFile, OpenFlags.WriteAccess))
                        {
                            destWim.RegisterCallback(ProgressCallback);

                            if (destWim.IsImageNameInUse(imageName))
                            {
                                srcWim.ExportImage(imageIndex, destWim, imageName + " " + DateTime.UtcNow.ToString(), imageDescription, ExportFlags.None);
                            }
                            else
                            {
                                srcWim.ExportImage(imageIndex, destWim, imageName, imageDescription, ExportFlags.None);
                            }

                            destWim.Overwrite(WriteFlags.None, Wim.DefaultThreads);
                        }
                    }
                    else
                    {
                        using (Wim destWim = Wim.CreateNewWim(compression))
                        {
                            destWim.RegisterCallback(ProgressCallback);
                            srcWim.ExportImage(imageIndex, destWim, imageName, imageDescription, ExportFlags.None);
                            destWim.Write(destinationWimFile, Wim.AllImages, compression == CompressionType.LZMS ? WriteFlags.Solid : WriteFlags.None, Wim.DefaultThreads);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
            return(ReseatWIMXml(destinationWimFile));
        }
示例#7
0
        public void AddImageMultiSourceTemplate(CompressionType compType, string wimFileName, AddFlags addFlags = AddFlags.None)
        {
            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.ScanBegin:
                    {
                        ScanProgress m = (ScanProgress)info;
                        Assert.IsNotNull(m);

                        _checked[0] = true;
                    }
                    break;

                    case ProgressMsg.ScanEnd:
                    {
                        ScanProgress m = (ScanProgress)info;
                        Assert.IsNotNull(m);

                        _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);
                }

                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.None, 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);
                }
            }
        }
示例#8
0
        public void DeleteImageTemplate(string wimFileName, int deleteIndex, string deleteImageName)
        {
            string srcWim  = Path.Combine(TestSetup.SampleDir, wimFileName);
            string destDir = TestHelper.GetTempDir();
            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.WriteStreams:
                    {
                        WriteStreamsProgress m = (WriteStreamsProgress)info;
                        Assert.IsNotNull(m);

                        Assert.AreEqual(m.CompressionType, CompressionType.LZX);
                        _checked[0] = true;
                    }
                    break;

                    case ProgressMsg.Rename:
                    {
                        RenameProgress m = (RenameProgress)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.WriteAccess))
                {
                    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.None, 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);
                }
            }
        }