Esempio n. 1
0
        public void ApplyImageTest_Abort()
        {
            using (WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None))
            {
                WimgApi.SetTemporaryPath(wimHandle, TempPath);

                WimMessageResult MessageCallback(WimMessageType messageType, object message, object userData) => messageType == WimMessageType.Process ? WimMessageResult.Abort : WimMessageResult.Done;

                WimgApi.RegisterMessageCallback(wimHandle, MessageCallback);

                try
                {
                    using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1))
                    {
                        WimHandle imageHandleCopy = imageHandle;

                        Should.Throw <OperationCanceledException>(() =>
                                                                  WimgApi.ApplyImage(imageHandleCopy, ApplyPath, WimApplyImageOptions.NoApply));
                    }
                }
                finally
                {
                    WimgApi.UnregisterMessageCallback(wimHandle, MessageCallback);
                }
            }
        }
Esempio n. 2
0
        public void ApplyImageTest()
        {
            using (WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read | WimFileAccess.Write | WimFileAccess.Mount, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None))
            {
                WimgApi.SetTemporaryPath(wimHandle, TempPath);

                using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1))
                {
                    WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.Index | WimApplyImageOptions.DisableDirectoryAcl | WimApplyImageOptions.DisableFileAcl | WimApplyImageOptions.DisableRPFix);
                }
            }

            Directory.EnumerateFiles(ApplyPath).Count().ShouldBe(TestWimTemplate.FileCount);
        }
Esempio n. 3
0
        private void ApplyImage()
        {
            using (var wimHandle = WimgApi.CreateFile(WimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting,
                                                      WimCreateFileOptions.None, WimCompressionType.None))
            {
                WimgApi.SetTemporaryPath(wimHandle, Path.GetTempPath());

                var loadImageIndex   = 1;
                var imageInformation = WimgApi.GetImageInformation(wimHandle).CreateNavigator();
                if (imageInformation == null)
                {
                    throw new NullReferenceException("Unable to get Image Information");
                }

                var imageCount = WimgApi.GetImageCount(wimHandle);
                if (imageCount > 1 && !string.IsNullOrWhiteSpace(Edition))
                {
                    loadImageIndex = 1;
                }

                var xpath         = string.Format("//WIM/IMAGE[@INDEX={0}]/NAME", loadImageIndex);
                var imageNameNode = imageInformation.SelectSingleNode(xpath);
                if (imageNameNode == null)
                {
                    throw new InvalidDataException("Unable to read WIM image name");
                }

                _progressRecord = new ProgressRecord(0, string.Format("Applying Windows Image - {0}", imageNameNode.TypedValue), "Starting");

                WimgApi.RegisterMessageCallback(wimHandle, MessageCallback);

                try
                {
                    using (var imageHandle = WimgApi.LoadImage(wimHandle, loadImageIndex))
                        WimgApi.ApplyImage(imageHandle, TargetPath, WimApplyImageOptions.None);
                }
                finally
                {
                    _autoResetEvent.Set();
                    WimgApi.UnregisterMessageCallback(wimHandle, MessageCallback);
                }
            }
        }
Esempio n. 4
0
        public void ApplyImageTest_NoApply()
        {
            WimMessageCallback messageCallback = (messageType, message, userData) =>
            {
                if (messageType == WimMessageType.SetRange)
                {
                    _noApplyFileCount = ((WimMessageSetRange)message).FileCount;
                }

                return(WimMessageResult.Done);
            };

            var applyPath = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Apply");

            using (var wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None))
            {
                WimgApi.SetTemporaryPath(wimHandle, TempPath);

                WimgApi.RegisterMessageCallback(wimHandle, messageCallback);

                try
                {
                    using (var imageHandle = WimgApi.LoadImage(wimHandle, 1))
                    {
                        WimgApi.ApplyImage(imageHandle, applyPath, WimApplyImageOptions.NoApply);
                    }
                }
                finally
                {
                    WimgApi.UnregisterMessageCallback(wimHandle, messageCallback);
                }
            }

            var fileCount = Directory.EnumerateFiles(ApplyPath).Count();

            fileCount.ShouldBe(0);

            _noApplyFileCount.ShouldBe(TestWimFileCount);
        }
        private IEnumerable <string> GetImageFiles(WimHandle imageHandle)
        {
            List <string> files = new List <String>();

            WimMessageResult MessageCallback(WimMessageType messageType, object message, object userData)
            {
                if (messageType == WimMessageType.FileInfo)
                {
                    WimMessageFileInfo messageFileInfo = (WimMessageFileInfo)message;

                    if ((messageFileInfo.FileInfo.Attributes | FileAttributes.Directory) != FileAttributes.Directory)
                    {
                        ((List <String>)userData).Add(messageFileInfo.Path.Replace(ApplyPath, string.Empty));
                    }
                }

                return(WimMessageResult.Done);
            }

            WimgApi.RegisterMessageCallback(TestWimHandle, MessageCallback, files);

            try
            {
                WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.NoApply | WimApplyImageOptions.FileInfo);
            }
            catch (Win32Exception win32Exception)
            {
                if (win32Exception.NativeErrorCode != 1235)
                {
                    throw;
                }
            }
            finally
            {
                WimgApi.UnregisterMessageCallback(TestWimHandle, MessageCallback);
            }

            return(files);
        }
Esempio n. 6
0
        public void ApplyImageTest_NoApply()
        {
            WimMessageResult MessageCallback(WimMessageType messageType, object message, object userData)
            {
                if (messageType == WimMessageType.SetRange)
                {
                    _noApplyFileCount = ((WimMessageSetRange)message).FileCount;
                }

                return(WimMessageResult.Done);
            }

            using (WimHandle wimHandle = WimgApi.CreateFile(TestWimPath, WimFileAccess.Read, WimCreationDisposition.OpenExisting, WimCreateFileOptions.None, WimCompressionType.None))
            {
                WimgApi.SetTemporaryPath(wimHandle, TempPath);

                WimgApi.RegisterMessageCallback(wimHandle, MessageCallback);

                try
                {
                    using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, 1))
                    {
                        WimgApi.ApplyImage(imageHandle, ApplyPath, WimApplyImageOptions.NoApply);
                    }
                }
                finally
                {
                    WimgApi.UnregisterMessageCallback(wimHandle, MessageCallback);
                }
            }

            int fileCount = Directory.EnumerateFiles(ApplyPath).Count();

            fileCount.ShouldBe(0);

            _noApplyFileCount.ShouldBe(TestWimTemplate.FileCount);
        }
Esempio n. 7
0
 public void ApplyImageTest_ThrowsArgumentNullException_imageHandle()
 {
     ShouldThrow <ArgumentNullException>("imageHandle", () =>
                                         WimgApi.ApplyImage(null, string.Empty, WimApplyImageOptions.None));
 }
Esempio n. 8
0
        public bool ApplyImage(
            string wimFile,
            int imageIndex,
            string OutputDirectory,
            IEnumerable <string> referenceWIMs = null,
            bool PreserveACL = true,
            IImaging.ProgressCallback progressCallback = null)
        {
            return(WIMLibImaging.ApplyImage(wimFile, imageIndex, OutputDirectory, referenceWIMs, PreserveACL, progressCallback));

            string title = $"Applying {wimFile.Split('\\').Last()} - Index {imageIndex}";

            try
            {
                WimMessageResult callback2(WimMessageType messageType, object message, object userData)
                {
                    switch (messageType)
                    {
                    case WimMessageType.Progress:
                    {
                        WimMessageProgress progressMessage = (WimMessageProgress)message;
                        progressCallback?.Invoke($"{title} (Estimated time remaining: {progressMessage.EstimatedTimeRemaining})", progressMessage.PercentComplete, false);
                        break;
                    }
                    }

                    return(WimMessageResult.Success);
                }

                using (var wimHandle = WimgApi.CreateFile(
                           wimFile,
                           WimFileAccess.Read,
                           WimCreationDisposition.OpenExisting,
                           WimCreateFileOptions.Chunked,
                           WimCompressionType.None))
                {
                    // Always set a temporary path
                    //
                    WimgApi.SetTemporaryPath(wimHandle, Environment.GetEnvironmentVariable("TEMP"));

                    if (referenceWIMs != null)
                    {
                        foreach (var referenceFile in referenceWIMs)
                        {
                            WimgApi.SetReferenceFile(wimHandle, referenceFile, WimSetReferenceMode.Append, WimSetReferenceOptions.Chunked);
                        }
                    }

                    // Register a method to be called while actions are performed by WIMGAPi for this .wim file
                    //
                    WimgApi.RegisterMessageCallback(wimHandle, callback2);

                    try
                    {
                        using (WimHandle imageHandle = WimgApi.LoadImage(wimHandle, imageIndex))
                        {
                            var    wiminfo      = WimgApi.GetImageInformationAsString(imageHandle);
                            var    wiminfoclass = WIMInformationXML.DeserializeIMAGE(wiminfo);
                            string imagename    = wiminfoclass.DISPLAYNAME == null ? wiminfoclass.NAME : wiminfoclass.DISPLAYNAME;

                            title = $"Applying {imagename} ({wimFile.Split('\\').Last()} - Index {imageIndex})";

                            // Apply the image contents
                            // This call is blocking but WIMGAPI will be calling MyCallbackMethod() during the process
                            //
                            WimgApi.ApplyImage(imageHandle, OutputDirectory, PreserveACL ? WimApplyImageOptions.None : WimApplyImageOptions.DisableDirectoryAcl | WimApplyImageOptions.DisableFileAcl);
                        }
                    }
                    finally
                    {
                        // Be sure to unregister the callback method
                        //
                        WimgApi.UnregisterMessageCallback(wimHandle, callback2);
                    }
                }
            }
            catch (Exception ex)
            {
                // A priv is not held by the client
                if (ex.HResult == -2147467259)
                {
                    return(WIMLibImaging.ApplyImage(wimFile, imageIndex, OutputDirectory, referenceWIMs, PreserveACL, progressCallback));
                }

                return(false);
            }
            return(true);
        }