Beispiel #1
0
        private void AddFile(string fileName, string packageDirPath, string suffix, CancellationToken cancellationToken)
        {
            _logger.LogDebug(string.Format("Processing add file entry {0}", fileName));

            var filePath = _localData.Path.PathCombine(fileName);

            _logger.LogTrace("filePath = " + filePath);
            var sourceFilePath = Path.Combine(packageDirPath, fileName + suffix);

            _logger.LogTrace("sourceFilePath = " + sourceFilePath);

            if (!File.Exists(sourceFilePath))
            {
                throw new MissingFileFromPackageException(string.Format("Cannot find file {0} in diff package.",
                                                                        fileName));
            }

            _logger.LogDebug("Creating file parent directories in local data...");
            var fileParentDirPath = Path.GetDirectoryName(filePath);

            _logger.LogTrace("fileParentDirPath = " + fileParentDirPath);
            //TODO: Assert that fileParentDirPath is not null
            // ReSharper disable once AssignNullToNotNullAttribute
            DirectoryOperations.CreateDirectory(fileParentDirPath, cancellationToken);
            _logger.LogDebug("File parent directories created in local data.");

            _logger.LogDebug("Copying file to local data (overwriting if needed)...");
            FileOperations.Copy(sourceFilePath, filePath, true, cancellationToken);
            _logger.LogDebug("File copied to local data.");

            _localMetaData.RegisterEntry(fileName, _versionId);

            _logger.LogDebug("Add file entry processed.");
        }
Beispiel #2
0
        private void ProcessAddedFiles(string packageDirPath, string suffix,
                                       CancellationToken cancellationToken)
        {
            DebugLogger.Log("Processing added files.");

            _addFilesStatusReporter.OnProgressChanged(0.0, "Installing package...");

            for (int i = 0; i < _versionDiffSummary.AddedFiles.Length; i++)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var entryName = _versionDiffSummary.AddedFiles[i];

                string entryPath = _localData.Path.PathCombine(entryName);

                if (entryName.EndsWith("/"))
                {
                    DirectoryOperations.CreateDirectory(entryPath);

                    // TODO: Uncomment this after fixing directory registration in install content command
                    //_localMetaData.RegisterEntry(entryName, _versionId);
                }
                else
                {
                    string sourceFilePath = Path.Combine(packageDirPath, entryName + suffix);

                    if (!File.Exists(sourceFilePath))
                    {
                        throw new InstallerException(string.Format("Cannot find file <{0}> in content package.", entryName));
                    }

                    DebugLogger.LogFormat("Copying {0} -> {1}", sourceFilePath, entryName);
                    DirectoryOperations.CreateParentDirectory(entryPath);
                    FileOperations.Copy(sourceFilePath, entryPath, true);

                    _localMetaData.RegisterEntry(entryName, _versionId);
                }

                _addFilesStatusReporter.OnProgressChanged((i + 1) / (double)_versionDiffSummary.AddedFiles.Length, "Installing package...");
            }

            _addFilesStatusReporter.OnProgressChanged(1.0, "Installing package...");
        }
Beispiel #3
0
        private void PatchFile(string fileName, string packageDirPath)
        {
            string filePath = _localData.Path.PathCombine(fileName);

            if (!File.Exists(filePath))
            {
                throw new InstallerException(string.Format("Couldn't patch file <{0}> - file doesn't exists.", fileName));
            }
            if (!_localMetaData.IsEntryRegistered(fileName))
            {
                throw new InstallerException(string.Format("Couldn't patch file <{0}> - file is not registered in meta data.", fileName));
            }

            int fileVersion = _localMetaData.GetEntryVersionId(fileName);

            if (fileVersion != _versionId - 1)
            {
                throw new InstallerException(string.Format(
                                                 "Couldn't patch file <{0}> - expected file with previous version ({1}) but the file version is {2}.",
                                                 fileName, _versionId - 1, fileVersion));
            }

            string newFilePath = _temporaryData.GetUniquePath();

            try
            {
                var filePatcher = new FilePatcher(filePath,
                                                  Path.Combine(packageDirPath, fileName), newFilePath);
                filePatcher.Patch();

                FileOperations.Copy(newFilePath, filePath, true);
            }
            finally
            {
                if (File.Exists(newFilePath))
                {
                    FileOperations.Delete(newFilePath);
                }
            }
        }
        public void TestCopy()
        {
            SystemPath destDir  = temporaryFolder.NewFolder().ToPath();
            SystemPath libraryA =
                Paths.Get(TestResources.GetResource("core/application/dependencies/libraryA.jar").ToURI());
            SystemPath libraryB =
                Paths.Get(TestResources.GetResource("core/application/dependencies/libraryB.jar").ToURI());
            SystemPath dirLayer = Paths.Get(TestResources.GetResource("core/layer").ToURI());

            FileOperations.Copy(ImmutableArray.Create(libraryA, libraryB, dirLayer), destDir);

            AssertFilesEqual(libraryA, destDir.Resolve("libraryA.jar"));
            AssertFilesEqual(libraryB, destDir.Resolve("libraryB.jar"));
            Assert.IsTrue(Files.Exists(destDir.Resolve("layer").Resolve("a").Resolve("b")));
            Assert.IsTrue(Files.Exists(destDir.Resolve("layer").Resolve("c")));
            AssertFilesEqual(
                dirLayer.Resolve("a").Resolve("b").Resolve("bar"),
                destDir.Resolve("layer").Resolve("a").Resolve("b").Resolve("bar"));
            AssertFilesEqual(
                dirLayer.Resolve("c").Resolve("cat"), destDir.Resolve("layer").Resolve("c").Resolve("cat"));
            AssertFilesEqual(dirLayer.Resolve("foo"), destDir.Resolve("layer").Resolve("foo"));
        }
Beispiel #5
0
        private void removeSignatures()
        {
            List <string>      documentsReadyToRemoveSignature = new List <string>();
            List <FileHistory> documentsToInteract             = new List <FileHistory>();

            switch (removeSignaturesActionType)
            {
            case removeSignaturesType.removeAllSignatures:
                documentsToInteract = selectedDocumentsToRemoveDigitalSignature;
                break;

            case removeSignaturesType.removeSelectedSignatures:
                if (selectedSignaturesInDocuments != null)
                {
                    bool          commonSignatures = false;
                    List <string> docs             = new List <string>();
                    foreach (Signers signers in selectedSignaturesInDocuments)
                    {
                        if (signers.Path != "commonSignatures")
                        {
                            docs.Add(signers.Path);
                        }
                        else
                        {
                            docs.Clear();
                            commonSignatures = true;
                            break;
                        }
                    }
                    if (!commonSignatures)
                    {
                        foreach (FileHistory fh in selectedDocumentsToRemoveDigitalSignature)
                        {
                            if (docs.Contains(fh.OriginalPath))
                            {
                                documentsToInteract.Add(fh);
                            }
                        }
                    }
                    else
                    {
                        documentsToInteract = selectedDocumentsToRemoveDigitalSignature;
                    }
                }
                break;
            }
            if (documentsToInteract.Count > 0)
            {
                if (chkCopyDocuments.Checked)
                {
                    documentsRemoveSignStatus = FileOperations.Copy(documentsToInteract, txtPath.Text, chkOverwrite.Checked);
                }
                else
                {
                    foreach (FileHistory documentToRemoveSignature in documentsToInteract)
                    {
                        FileStatus documentStatus = new FileStatus(documentToRemoveSignature.OriginalPath, documentToRemoveSignature.NewPath, FileUtils.Status.ModifiedButNotBackedUp);
                        documentsRemoveSignStatus.Add(documentStatus);
                    }
                }
            }

            switch (removeSignaturesActionType)
            {
            case removeSignaturesType.removeAllSignatures:
                foreach (FileStatus documentToRemoveSignature in documentsRemoveSignStatus)
                {
                    if ((documentToRemoveSignature.Status == Status.Success) || (documentToRemoveSignature.Status == Status.ModifiedButNotBackedUp))
                    {
                        try
                        {
                            loadDigitalSignature(documentToRemoveSignature.Path);
                            digitalSignature._RemoveAllSignatures();
                        }
                        catch (NullReferenceException)
                        {
                            documentToRemoveSignature.Status = Status.GenericError;
                        }
                        catch (FileFormatException)
                        {
                            documentToRemoveSignature.Status = Status.CorruptedContent;
                        }
                        catch (IOException)
                        {
                            documentToRemoveSignature.Status = Status.InUseByAnotherProcess;
                        }
                        catch (Exception)
                        {
                            documentToRemoveSignature.Status = Status.GenericError;
                        }

                        if (digitalSignature.DocumentType.Equals(Types.XpsDocument))
                        {
                            digitalSignature.xpsDocument.Close();
                        }
                        else
                        {
                            digitalSignature.package.Close();
                        }
                    }
                }
                break;

            case removeSignaturesType.removeSelectedSignatures:
                List <FileStatus> removeSignatureStatusList = new List <FileStatus>();
                FileStatus        selectedFileStatus        = null;
                foreach (FileStatus documentToRemoveSignature in documentsRemoveSignStatus)
                {
                    if (documentToRemoveSignature.Status == Status.Success ||
                        documentToRemoveSignature.Status == Status.ModifiedButNotBackedUp)
                    {
                        foreach (Signers document in selectedSignaturesInDocuments)
                        {
                            if ((documentToRemoveSignature.OldPath == document.Path) || (document.Path == "commonSignatures"))
                            {
                                try
                                {
                                    string documentPath;
                                    string documentOldPath = "";
                                    if (document.Path != "commonSignatures")
                                    {
                                        documentOldPath = documentToRemoveSignature.OldPath;
                                        documentPath    = documentToRemoveSignature.Path;
                                        foreach (Signer signer in document)
                                        {
                                            loadDigitalSignature(documentPath);
                                            Uri signUri = new Uri(signer.uri, UriKind.Relative);
                                            digitalSignature.RemoveUniqueSignatureFromFile(signUri);
                                        }
                                    }
                                    else
                                    {
                                        documentOldPath = documentToRemoveSignature.OldPath;
                                        documentPath    = documentToRemoveSignature.Path;
                                        foreach (Signer signer in document)
                                        {
                                            loadDigitalSignature(documentPath);
                                            string serialNumber = signer.serialNumber;
                                            digitalSignature.RemoveSignaturesFromFilesBySigner(serialNumber);
                                        }
                                    }
                                    if (chkCopyDocuments.Checked)
                                    {
                                        selectedFileStatus = new FileStatus(documentOldPath, documentPath, Status.Success);
                                    }
                                    else
                                    {
                                        selectedFileStatus = new FileStatus(documentOldPath, documentPath, Status.ModifiedButNotBackedUp);
                                    }
                                }
                                catch (NullReferenceException)
                                {
                                    selectedFileStatus = new FileStatus(document.Path, Status.GenericError);
                                }
                                catch (FileFormatException)
                                {
                                    selectedFileStatus = new FileStatus(document.Path, Status.CorruptedContent);
                                }
                                catch (IOException)
                                {
                                    selectedFileStatus = new FileStatus(document.Path, Status.InUseByAnotherProcess);
                                }
                                catch (Exception)
                                {
                                    selectedFileStatus = new FileStatus(document.Path, Status.GenericError);
                                }

                                if (!removeSignatureStatusList.Contains(selectedFileStatus))
                                {
                                    removeSignatureStatusList.Add(selectedFileStatus);
                                }
                                //break;
                            }
                        }
                    }
                }

                foreach (FileStatus documentReadyStatus in documentsRemoveSignStatus)
                {
                    if (removeSignatureStatusList.Contains(documentReadyStatus))
                    {
                        documentsRemoveSignStatus.Remove(documentReadyStatus);
                    }
                }
                break;
            }
        }
Beispiel #6
0
        private string[] signDocuments()
        {
            String officeDocument = Properties.Resources.OfficeObject;

            X509Certificate2 certificate = CertificateUtils.GetCertificate();

            if (certificate != null)
            {
                List <string> signedDocuments = new List <string>();
                if (chkCopyDocuments.Checked)
                {
                    documentsSignStatus = FileOperations.Copy(compatibleDocumentsList, txtPath.Text, chkOverwrite.Checked);
                    foreach (FileStatus documentToSign in documentsSignStatus)
                    {
                        if (documentToSign.Status == Status.Success)
                        {
                            try
                            {
                                loadDigitalSignature(documentToSign.Path);
                                bool signatureExists = digitalSignature.DocumentHasSignature(certificate);
                                if (signatureExists)
                                {
                                    if (!chkNotSignIfAlreadyExists.Checked)
                                    {
                                        digitalSignature.SetOfficeDocument(officeDocument);
                                        digitalSignature.SignDocument(certificate);
                                    }
                                    else
                                    {
                                        documentToSign.Status = Status.SignatureAlreadyExists;
                                    }
                                }
                                else
                                {
                                    digitalSignature.SetOfficeDocument(officeDocument);
                                    digitalSignature.SignDocument(certificate);
                                }
                                signedDocuments.Add(documentToSign.Path);
                                if (digitalSignature.DocumentType.Equals(Types.XpsDocument))
                                {
                                    digitalSignature.xpsDocument.Close();
                                }
                                else
                                {
                                    digitalSignature.package.Close();
                                }
                            }
                            catch (NullReferenceException)
                            {
                                documentToSign.Status = Status.GenericError;
                            }
                            catch (FileFormatException)
                            {
                                documentToSign.Status = Status.CorruptedContent;
                            }
                            catch (IOException e)
                            {
                                documentToSign.Status = Status.InUseByAnotherProcess;
                            }
                            catch (Exception e)
                            {
                                documentToSign.Status = Status.GenericError;
                            }
                        }
                    }
                }
                else
                {
                    foreach (string documentToSign in compatibleDocuments)
                    {
                        Status st = new Status();
                        try
                        {
                            loadDigitalSignature(documentToSign);
                            bool signatureExists = digitalSignature.DocumentHasSignature(certificate);
                            if (signatureExists)
                            {
                                if (!chkNotSignIfAlreadyExists.Checked)
                                {
                                    digitalSignature.SetOfficeDocument(officeDocument);
                                    digitalSignature.SignDocument(certificate);
                                    st = Status.ModifiedButNotBackedUp;
                                }
                                else
                                {
                                    st = Status.SignatureAlreadyExistsNotBackedUp;
                                }
                            }
                            else
                            {
                                digitalSignature.SetOfficeDocument(officeDocument);
                                digitalSignature.SignDocument(certificate);
                                st = Status.ModifiedButNotBackedUp;
                            }

                            signedDocuments.Add(documentToSign);

                            if (digitalSignature.DocumentType.Equals(Types.XpsDocument))
                            {
                                digitalSignature.xpsDocument.Close();
                            }
                            else
                            {
                                digitalSignature.package.Close();
                            }
                        }
                        catch
                        {
                            st = Status.NotSigned;
                        }

                        FileStatus documentSignStatus = new FileStatus(documentToSign, st);
                        documentsSignStatus.Add(documentSignStatus);
                    }
                }
                return(signedDocuments.ToArray());
            }
            return(null);
        }